first public release
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_lcore.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <stdint.h>
40 #include <inttypes.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <sys/queue.h>
44
45 #include <rte_log.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_launch.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_debug.h>
54
55 #include "eal_private.h"
56
57 #define PROC_CPUINFO "/proc/cpuinfo"
58 #define PROC_PROCESSOR_FMT ""
59
60 /* parse one line and try to match "processor : %d". */
61 static int
62 parse_processor_id(const char *buf, unsigned *lcore_id)
63 {
64         static const char _processor[] = "processor";
65         const char *s;
66
67         if (strncmp(buf, _processor, sizeof(_processor) - 1) != 0)
68                 return -1;
69
70         s = strchr(buf, ':');
71         if (s == NULL)
72                 return -1;
73
74         errno = 0;
75         *lcore_id = strtoul(s+1, NULL, 10);
76         if (errno != 0) {
77                 *lcore_id = -1;
78                 return -1;
79         }
80
81         return 0;
82 }
83
84 /* parse one line and try to match "physical id : %d". */
85 static int
86 parse_socket_id(const char *buf, unsigned *socket_id)
87 {
88         static const char _physical_id[] = "physical id";
89         const char *s;
90
91         if (strncmp(buf, _physical_id, sizeof(_physical_id) - 1) != 0)
92                 return -1;
93
94         s = strchr(buf, ':');
95         if (s == NULL)
96                 return -1;
97
98         errno = 0;
99         *socket_id = strtoul(s+1, NULL, 10);
100         if (errno != 0)
101                 return -1;
102
103         return 0;
104 }
105
106 /*
107  * Parse /proc/cpuinfo to get the number of physical and logical
108  * processors on the machine. The function will fill the cpu_info
109  * structure.
110  */
111 int
112 rte_eal_cpu_init(void)
113 {
114         struct rte_config *config;
115         FILE *f;
116         char buf[BUFSIZ];
117         unsigned lcore_id = 0;
118         unsigned socket_id = 0;
119         unsigned count = 0;
120
121         /* get pointer to global configuration */
122         config = rte_eal_get_configuration();
123
124         /* open /proc/cpuinfo */
125         f = fopen(PROC_CPUINFO, "r");
126         if (f == NULL) {
127                 RTE_LOG(ERR, EAL, "%s(): Cannot find "PROC_CPUINFO"\n", __func__);
128                 return -1;
129         }
130
131         /*
132          * browse lines of /proc/cpuinfo and fill memseg entries in
133          * global configuration
134          */
135         while (fgets(buf, sizeof(buf), f) != NULL) {
136
137                 if (parse_processor_id(buf, &lcore_id) == 0)
138                         continue;
139
140                 if (parse_socket_id(buf, &socket_id) == 0)
141                         continue;
142
143                 if (buf[0] == '\n') {
144                         RTE_LOG(DEBUG, EAL, "Detected lcore %u on socket %u\n",
145                                 lcore_id, socket_id);
146                         if (lcore_id >= RTE_MAX_LCORE) {
147                                 RTE_LOG(DEBUG, EAL,
148                                         "Skip lcore %u >= RTE_MAX_LCORE\n",
149                                           lcore_id);
150                                 continue;
151                         }
152
153                         /*
154                          * In a virtualization environment, the socket ID
155                          * reported by the system may not be linked to a real
156                          * physical socket ID, and may be incoherent. So in this
157                          * case, a default socket ID of 0 is assigned.
158                          */
159                         if (socket_id >= RTE_MAX_NUMA_NODES) {
160 #ifdef CONFIG_RTE_EAL_ALLOW_INV_SOCKET_ID
161                                 socket_id = 0;
162 #else
163                                 rte_panic("Socket ID (%u) is greater than "
164                                     "RTE_MAX_NUMA_NODES (%d)\n",
165                                     socket_id, RTE_MAX_NUMA_NODES);
166 #endif
167                         }
168
169                         lcore_config[lcore_id].detected = 1;
170                         lcore_config[lcore_id].socket_id = socket_id;
171
172                 }
173         }
174
175         fclose(f);
176
177         /* disable lcores that were not detected */
178         RTE_LCORE_FOREACH(lcore_id) {
179
180                 if (lcore_config[lcore_id].detected == 0) {
181                         RTE_LOG(DEBUG, EAL, "Skip lcore %u (not detected)\n",
182                                 lcore_id);
183                         config->lcore_role[lcore_id] = ROLE_OFF;
184                 }
185                 else
186                         count ++;
187         }
188
189         config->lcore_count = count;
190
191         return 0;
192 }