remove version in all files
[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  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <sys/queue.h>
43
44 #include <rte_log.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_launch.h>
48 #include <rte_tailq.h>
49 #include <rte_eal.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_debug.h>
53
54 #include "eal_private.h"
55
56 #define PROC_CPUINFO "/proc/cpuinfo"
57 #define PROC_PROCESSOR_FMT ""
58
59 /* parse one line and try to match "processor : %d". */
60 static int
61 parse_processor_id(const char *buf, unsigned *lcore_id)
62 {
63         static const char _processor[] = "processor";
64         const char *s;
65
66         if (strncmp(buf, _processor, sizeof(_processor) - 1) != 0)
67                 return -1;
68
69         s = strchr(buf, ':');
70         if (s == NULL)
71                 return -1;
72
73         errno = 0;
74         *lcore_id = strtoul(s+1, NULL, 10);
75         if (errno != 0) {
76                 *lcore_id = -1;
77                 return -1;
78         }
79
80         return 0;
81 }
82
83 /* parse one line and try to match "physical id : %d". */
84 static int
85 parse_socket_id(const char *buf, unsigned *socket_id)
86 {
87         static const char _physical_id[] = "physical id";
88         const char *s;
89
90         if (strncmp(buf, _physical_id, sizeof(_physical_id) - 1) != 0)
91                 return -1;
92
93         s = strchr(buf, ':');
94         if (s == NULL)
95                 return -1;
96
97         errno = 0;
98         *socket_id = strtoul(s+1, NULL, 10);
99         if (errno != 0)
100                 return -1;
101
102         return 0;
103 }
104
105 /*
106  * Parse /proc/cpuinfo to get the number of physical and logical
107  * processors on the machine. The function will fill the cpu_info
108  * structure.
109  */
110 int
111 rte_eal_cpu_init(void)
112 {
113         struct rte_config *config;
114         FILE *f;
115         char buf[BUFSIZ];
116         unsigned lcore_id = 0;
117         unsigned socket_id = 0;
118         unsigned count = 0;
119
120         /* get pointer to global configuration */
121         config = rte_eal_get_configuration();
122
123         /* open /proc/cpuinfo */
124         f = fopen(PROC_CPUINFO, "r");
125         if (f == NULL) {
126                 RTE_LOG(ERR, EAL, "%s(): Cannot find "PROC_CPUINFO"\n", __func__);
127                 return -1;
128         }
129
130         /*
131          * browse lines of /proc/cpuinfo and fill memseg entries in
132          * global configuration
133          */
134         while (fgets(buf, sizeof(buf), f) != NULL) {
135
136                 if (parse_processor_id(buf, &lcore_id) == 0)
137                         continue;
138
139                 if (parse_socket_id(buf, &socket_id) == 0)
140                         continue;
141
142                 if (buf[0] == '\n') {
143                         RTE_LOG(DEBUG, EAL, "Detected lcore %u on socket %u\n",
144                                 lcore_id, socket_id);
145                         if (lcore_id >= RTE_MAX_LCORE) {
146                                 RTE_LOG(DEBUG, EAL,
147                                         "Skip lcore %u >= RTE_MAX_LCORE\n",
148                                           lcore_id);
149                                 continue;
150                         }
151
152                         /*
153                          * In a virtualization environment, the socket ID
154                          * reported by the system may not be linked to a real
155                          * physical socket ID, and may be incoherent. So in this
156                          * case, a default socket ID of 0 is assigned.
157                          */
158                         if (socket_id >= RTE_MAX_NUMA_NODES) {
159 #ifdef CONFIG_RTE_EAL_ALLOW_INV_SOCKET_ID
160                                 socket_id = 0;
161 #else
162                                 rte_panic("Socket ID (%u) is greater than "
163                                     "RTE_MAX_NUMA_NODES (%d)\n",
164                                     socket_id, RTE_MAX_NUMA_NODES);
165 #endif
166                         }
167
168                         lcore_config[lcore_id].detected = 1;
169                         lcore_config[lcore_id].socket_id = socket_id;
170
171                 }
172         }
173
174         fclose(f);
175
176         /* disable lcores that were not detected */
177         RTE_LCORE_FOREACH(lcore_id) {
178
179                 if (lcore_config[lcore_id].detected == 0) {
180                         RTE_LOG(DEBUG, EAL, "Skip lcore %u (not detected)\n",
181                                 lcore_id);
182                         config->lcore_role[lcore_id] = ROLE_OFF;
183                 }
184                 else
185                         count ++;
186         }
187
188         config->lcore_count = count;
189
190         return 0;
191 }