|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <pthread.h> |
1 | 3 | #include <stdio.h> |
2 | 4 | #include <string.h> |
3 | 5 | #include <stdint.h> |
@@ -114,45 +116,83 @@ static void init_signals(void) |
114 | 116 | sigaction(SIGUSR2, &sigact, NULL); |
115 | 117 | } |
116 | 118 |
|
| 119 | +static int set_thread_affinity(void) |
| 120 | +{ |
| 121 | + int s; |
| 122 | + uint8_t cid; |
| 123 | + pthread_t tid; |
| 124 | + cpu_set_t cpuset; |
| 125 | + unsigned long long cpumask = 0; |
| 126 | + |
| 127 | + tid = pthread_self(); |
| 128 | + CPU_ZERO(&cpuset); |
| 129 | + for (cid = 0; cid < RTE_MAX_LCORE; ++cid) { |
| 130 | + if (!rte_lcore_is_enabled(cid)) { |
| 131 | + CPU_SET(cid, &cpuset); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + s = pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset); |
| 136 | + if (s != 0) { |
| 137 | + log_msg(LOG_ERR, "fail to set thread affinty, errno=%d, errinfo=%s\n", errno, strerror(errno)); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + CPU_ZERO(&cpuset); |
| 142 | + s = pthread_getaffinity_np(tid, sizeof(cpu_set_t), &cpuset); |
| 143 | + if (s != 0) { |
| 144 | + log_msg(LOG_ERR, "fail to get thread affinity, errno=%d, errinfo=%s\n", errno, strerror(errno)); |
| 145 | + return -2; |
| 146 | + } |
| 147 | + |
| 148 | + for (cid = 0; cid < RTE_MAX_LCORE; cid++) { |
| 149 | + if (CPU_ISSET(cid, &cpuset)) { |
| 150 | + cpumask |= (1LL << cid); |
| 151 | + } |
| 152 | + } |
| 153 | + log_msg(LOG_INFO, "current thread affinity is set to %llX\n", cpumask); |
| 154 | + |
| 155 | + return 0; |
| 156 | +} |
117 | 157 |
|
118 | 158 | int main(int argc, char **argv) |
119 | 159 | { |
120 | | - dns_procname = parse_progname(argv[0]); |
121 | | - |
122 | | - parse_args(argc, argv); |
123 | 160 | if (check_pid(PIDFILE) < 0) { |
124 | | - exit(0); |
| 161 | + exit(0); |
125 | 162 | } |
126 | 163 | write_pid(PIDFILE); |
127 | | - |
| 164 | + |
| 165 | + dns_procname = parse_progname(argv[0]); |
| 166 | + parse_args(argc, argv); |
128 | 167 | config_file_load(dns_cfgfile,dns_procname); |
129 | | - |
| 168 | + |
130 | 169 | log_open(g_dns_cfg->comm.log_file); |
131 | | - |
| 170 | + |
132 | 171 | dns_dpdk_init(); |
133 | | - |
134 | | - unsigned lcore_id = rte_lcore_id(); |
135 | 172 |
|
136 | | - remote_sock_init(g_dns_cfg->comm.fwd_addrs,g_dns_cfg->comm.fwd_def_addrs,g_dns_cfg->comm.fwd_threads); |
| 173 | + if (set_thread_affinity() != 0) { |
| 174 | + log_msg(LOG_ERR, "set_thread_affinity failed\n"); |
| 175 | + exit(EXIT_FAILURE); |
| 176 | + } |
137 | 177 |
|
| 178 | + remote_sock_init(g_dns_cfg->comm.fwd_addrs,g_dns_cfg->comm.fwd_def_addrs,g_dns_cfg->comm.fwd_threads); |
138 | 179 |
|
139 | 180 | netif_queue_core_bind(); |
140 | 181 |
|
141 | | - // struct sigaction action; |
142 | | - /* Setup the signal handling... */ |
143 | | - init_signals(); |
144 | | - rte_pdump_init("/var/run/.dpdk"); |
145 | | - |
| 182 | + // struct sigaction action; |
| 183 | + /* Setup the signal handling... */ |
| 184 | + init_signals(); |
| 185 | + rte_pdump_init("/var/run/.dpdk"); |
146 | 186 |
|
| 187 | + unsigned lcore_id; |
147 | 188 | RTE_LCORE_FOREACH_SLAVE(lcore_id) { |
148 | | - if(kdns_init(lcore_id) < 0){ |
| 189 | + if (kdns_init(lcore_id) < 0) { |
149 | 190 | log_msg(LOG_ERR, "Error:kdns_init lcore_id =%d\n",lcore_id); |
150 | 191 | exit(-1); |
151 | 192 | } |
152 | 193 | rte_eal_remote_launch(process_slave, NULL, lcore_id); |
153 | 194 | } |
154 | 195 |
|
155 | | - |
156 | 196 | dns_tcp_process_init(g_dns_cfg->netdev.kni_vip); |
157 | 197 |
|
158 | 198 | process_master(NULL); |
|
0 commit comments