Skip to content

Commit 028909b

Browse files
Merge pull request #39 from yeze/master
add thread name and fix thread affinity
2 parents 52c1fc5 + c583eb9 commit 028909b

File tree

3 files changed

+81
-35
lines changed

3 files changed

+81
-35
lines changed

kdns/src/forward.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,28 @@ int remote_sock_init(char * fwd_addrs, char * fwd_def_addr,int fwd_threads){
303303
/* create a separate thread to send task status as quick as possible */
304304
int i =0;
305305
for( ;i< fwd_threads;i++){
306-
int * remote_sock = (int *) xalloc(sizeof(int));
307-
pthread_t *thread_id = (pthread_t *) xalloc(sizeof(pthread_t));
308-
*remote_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
309-
struct timeval tv = {2, 0};
310-
311-
if (setsockopt(*remote_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
312-
313-
log_msg(LOG_ERR,"socket option SO_RCVTIMEO not support\n");
314-
exit(-1);
315-
}
316-
317-
pthread_create(thread_id, NULL, thread_fwd_pkt_process, (void*)remote_sock);
306+
int * remote_sock = (int *) xalloc(sizeof(int));
307+
pthread_t *thread_id = (pthread_t *) xalloc(sizeof(pthread_t));
308+
*remote_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
309+
struct timeval tv = {2, 0};
310+
311+
if (setsockopt(*remote_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
312+
313+
log_msg(LOG_ERR,"socket option SO_RCVTIMEO not support\n");
314+
exit(-1);
315+
}
316+
317+
pthread_create(thread_id, NULL, thread_fwd_pkt_process, (void*)remote_sock);
318+
319+
char tname[16];
320+
snprintf(tname, sizeof(tname), "kdns_udp_fwd_%d", i);
321+
pthread_setname_np(*thread_id, tname);
318322
}
319-
320-
// cache date expired clean up thread
321-
pthread_t *thread_cache_expired = (pthread_t *) xalloc(sizeof(pthread_t));
322-
pthread_create(thread_cache_expired, NULL, thread_fwd_cache_expired_cleanup, (void*)NULL);
323+
324+
// cache date expired clean up thread
325+
pthread_t *thread_cache_expired = (pthread_t *) xalloc(sizeof(pthread_t));
326+
pthread_create(thread_cache_expired, NULL, thread_fwd_cache_expired_cleanup, (void*)NULL);
327+
pthread_setname_np(*thread_cache_expired, "kdns_fwd_cache");
323328

324329
return 0;
325330
}

kdns/src/main.c

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define _GNU_SOURCE
2+
#include <pthread.h>
13
#include <stdio.h>
24
#include <string.h>
35
#include <stdint.h>
@@ -114,45 +116,83 @@ static void init_signals(void)
114116
sigaction(SIGUSR2, &sigact, NULL);
115117
}
116118

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+
}
117157

118158
int main(int argc, char **argv)
119159
{
120-
dns_procname = parse_progname(argv[0]);
121-
122-
parse_args(argc, argv);
123160
if (check_pid(PIDFILE) < 0) {
124-
exit(0);
161+
exit(0);
125162
}
126163
write_pid(PIDFILE);
127-
164+
165+
dns_procname = parse_progname(argv[0]);
166+
parse_args(argc, argv);
128167
config_file_load(dns_cfgfile,dns_procname);
129-
168+
130169
log_open(g_dns_cfg->comm.log_file);
131-
170+
132171
dns_dpdk_init();
133-
134-
unsigned lcore_id = rte_lcore_id();
135172

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+
}
137177

178+
remote_sock_init(g_dns_cfg->comm.fwd_addrs,g_dns_cfg->comm.fwd_def_addrs,g_dns_cfg->comm.fwd_threads);
138179

139180
netif_queue_core_bind();
140181

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");
146186

187+
unsigned lcore_id;
147188
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
148-
if(kdns_init(lcore_id) < 0){
189+
if (kdns_init(lcore_id) < 0) {
149190
log_msg(LOG_ERR, "Error:kdns_init lcore_id =%d\n",lcore_id);
150191
exit(-1);
151192
}
152193
rte_eal_remote_launch(process_slave, NULL, lcore_id);
153194
}
154195

155-
156196
dns_tcp_process_init(g_dns_cfg->netdev.kni_vip);
157197

158198
process_master(NULL);

kdns/src/tcp_process.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ int dns_tcp_process_init(char *ip){
273273
return -1;
274274
}
275275

276-
pthread_t *thread_cache_expired = (pthread_t *) xalloc(sizeof(pthread_t));
277-
pthread_create(thread_cache_expired, NULL, dns_tcp_process, (void*)ip);
276+
pthread_t *thread_tcp_process = (pthread_t *) xalloc(sizeof(pthread_t));
277+
pthread_create(thread_tcp_process, NULL, dns_tcp_process, (void*)ip);
278+
pthread_setname_np(*thread_tcp_process, "kdns_tcp_proc");
278279
return 0;
279280
}
280281

0 commit comments

Comments
 (0)