New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / common / eal_common_lcore.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <limits.h>
7 #include <string.h>
8 #include <dirent.h>
9
10 #include <rte_log.h>
11 #include <rte_eal.h>
12 #include <rte_lcore.h>
13 #include <rte_common.h>
14 #include <rte_debug.h>
15
16 #include "eal_private.h"
17 #include "eal_thread.h"
18
19 /*
20  * Parse /sys/devices/system/cpu to get the number of physical and logical
21  * processors on the machine. The function will fill the cpu_info
22  * structure.
23  */
24 int
25 rte_eal_cpu_init(void)
26 {
27         /* pointer to global configuration */
28         struct rte_config *config = rte_eal_get_configuration();
29         unsigned lcore_id;
30         unsigned count = 0;
31
32         /*
33          * Parse the maximum set of logical cores, detect the subset of running
34          * ones and enable them by default.
35          */
36         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
37                 lcore_config[lcore_id].core_index = count;
38
39                 /* init cpuset for per lcore config */
40                 CPU_ZERO(&lcore_config[lcore_id].cpuset);
41
42                 /* in 1:1 mapping, record related cpu detected state */
43                 lcore_config[lcore_id].detected = eal_cpu_detected(lcore_id);
44                 if (lcore_config[lcore_id].detected == 0) {
45                         config->lcore_role[lcore_id] = ROLE_OFF;
46                         lcore_config[lcore_id].core_index = -1;
47                         continue;
48                 }
49
50                 /* By default, lcore 1:1 map to cpu id */
51                 CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset);
52
53                 /* By default, each detected core is enabled */
54                 config->lcore_role[lcore_id] = ROLE_RTE;
55                 lcore_config[lcore_id].core_role = ROLE_RTE;
56                 lcore_config[lcore_id].core_id = eal_cpu_core_id(lcore_id);
57                 lcore_config[lcore_id].socket_id = eal_cpu_socket_id(lcore_id);
58                 if (lcore_config[lcore_id].socket_id >= RTE_MAX_NUMA_NODES) {
59 #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID
60                         lcore_config[lcore_id].socket_id = 0;
61 #else
62                         RTE_LOG(ERR, EAL, "Socket ID (%u) is greater than "
63                                 "RTE_MAX_NUMA_NODES (%d)\n",
64                                 lcore_config[lcore_id].socket_id,
65                                 RTE_MAX_NUMA_NODES);
66                         return -1;
67 #endif
68                 }
69                 RTE_LOG(DEBUG, EAL, "Detected lcore %u as "
70                                 "core %u on socket %u\n",
71                                 lcore_id, lcore_config[lcore_id].core_id,
72                                 lcore_config[lcore_id].socket_id);
73                 count++;
74         }
75         /* Set the count of enabled logical cores of the EAL configuration */
76         config->lcore_count = count;
77         RTE_LOG(DEBUG, EAL,
78                 "Support maximum %u logical core(s) by configuration.\n",
79                 RTE_MAX_LCORE);
80         RTE_LOG(INFO, EAL, "Detected %u lcore(s)\n", config->lcore_count);
81
82         return 0;
83 }