3 * Author: Mingqiang Zhuang
5 * Created on February 10, 2009
7 * (c) Copyright 2009, Schooner Information Technology, Inc.
8 * http://www.schoonerinfotech.com/
14 #include "ms_thread.h"
15 #include "ms_setting.h"
16 #include "ms_atomic.h"
19 pthread_key_t ms_thread_key
;
21 /* array of thread context structure, each thread has a thread context structure */
22 static ms_thread_ctx_t
*ms_thread_ctx
;
25 static void ms_set_current_time(void);
26 static void ms_check_sock_timeout(void);
27 static void ms_clock_handler(const int fd
, const short which
, void *arg
);
28 static int ms_set_thread_cpu_affinity(int cpu
);
29 static int ms_setup_thread(ms_thread_ctx_t
*thread_ctx
);
30 static void *ms_worker_libevent(void *arg
);
31 static void ms_create_worker(void *(*func
)(void *), void *arg
);
35 * time-sensitive callers can call it by hand with this,
36 * outside the normal ever-1-second timer
38 static void ms_set_current_time()
41 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
43 gettimeofday(&timer
, NULL
);
44 ms_thread
->curr_time
= (rel_time_t
)timer
.tv_sec
;
45 } /* ms_set_current_time */
49 * used to check whether UDP of command are waiting timeout
50 * by the ever-1-second timer
52 static void ms_check_sock_timeout(void)
54 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
58 for (int i
= 0; i
< ms_thread
->thread_ctx
->nconns
; i
++)
60 c
= &ms_thread
->conn
[i
];
64 time_diff
= (int)(ms_thread
->curr_time
- (rel_time_t
)c
->start_time
.tv_sec
);
67 if (time_diff
> SOCK_WAIT_TIMEOUT
)
69 /* calculate dropped packets count */
72 atomic_add_size(&ms_stats
.pkt_drop
, c
->packets
- c
->recvpkt
);
75 atomic_add_size(&ms_stats
.udp_timeout
, 1);
76 ms_reset_conn(c
, true);
80 } /* ms_check_sock_timeout */
83 /* if disconnect, the ever-1-second timer will call this function to reconnect */
84 static void ms_reconn_thread_socks(void)
86 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
87 for (int i
= 0; i
< ms_thread
->thread_ctx
->nconns
; i
++)
89 ms_reconn_socks(&ms_thread
->conn
[i
]);
91 } /* ms_reconn_thread_socks */
95 * the handler of the ever-1-second timer
97 * @param fd, the descriptors of the socket
98 * @param which, event flags
99 * @param arg, argument
101 static void ms_clock_handler(const int fd
, const short which
, void *arg
)
103 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
106 .tv_sec
= 1, .tv_usec
= 0
110 UNUSED_ARGUMENT(which
);
111 UNUSED_ARGUMENT(arg
);
113 ms_set_current_time();
115 if (ms_thread
->initialized
)
117 /* only delete the event if it's actually there. */
118 evtimer_del(&ms_thread
->clock_event
);
119 ms_check_sock_timeout();
123 ms_thread
->initialized
= true;
126 ms_reconn_thread_socks();
128 evtimer_set(&ms_thread
->clock_event
, ms_clock_handler
, 0);
129 event_base_set(ms_thread
->base
, &ms_thread
->clock_event
);
130 evtimer_add(&ms_thread
->clock_event
, &t
);
131 } /* ms_clock_handler */
135 * used to bind thread to CPU if the system supports
137 * @param cpu, cpu index
139 * @return if success, return 0, else return -1
141 static int ms_set_thread_cpu_affinity(int cpu
)
145 #ifdef HAVE_CPU_SET_T
148 CPU_SET(cpu
, &cpu_set
);
150 if (sched_setaffinity(0, sizeof(cpu_set_t
), &cpu_set
) == -1)
152 fprintf(stderr
, "WARNING: Could not set CPU Affinity, continuing...\n");
156 UNUSED_ARGUMENT(cpu
);
160 } /* ms_set_thread_cpu_affinity */
164 * Set up a thread's information.
166 * @param thread_ctx, pointer of the thread context structure
168 * @return if success, return 0, else return -1
170 static int ms_setup_thread(ms_thread_ctx_t
*thread_ctx
)
173 ms_thread_t
*ms_thread
= (ms_thread_t
*)calloc(sizeof(*ms_thread
), 1);
174 pthread_setspecific(ms_thread_key
, (void *)ms_thread
);
176 ms_thread
->thread_ctx
= thread_ctx
;
177 ms_thread
->nactive_conn
= thread_ctx
->nconns
;
178 ms_thread
->initialized
= false;
179 static volatile uint32_t cnt
= 0;
181 gettimeofday(&ms_thread
->startup_time
, NULL
);
183 ms_thread
->base
= event_init();
184 if (ms_thread
->base
== NULL
)
186 if (atomic_add_32_nv(&cnt
, 1) == 0)
188 fprintf(stderr
, "Can't allocate event base.\n");
195 (ms_conn_t
*)malloc((size_t)thread_ctx
->nconns
* sizeof(ms_conn_t
));
196 if (ms_thread
->conn
== NULL
)
198 if (atomic_add_32_nv(&cnt
, 1) == 0)
202 "Can't allocate concurrency structure for thread descriptors.");
207 memset(ms_thread
->conn
, 0, (size_t)thread_ctx
->nconns
* sizeof(ms_conn_t
));
209 for (int i
= 0; i
< thread_ctx
->nconns
; i
++)
211 ms_thread
->conn
[i
].conn_idx
= i
;
212 if (ms_setup_conn(&ms_thread
->conn
[i
]) != 0)
214 /* only output this error once */
215 if (atomic_add_32_nv(&cnt
, 1) == 0)
217 fprintf(stderr
, "Initializing connection failed.\n");
225 } /* ms_setup_thread */
229 * Worker thread: main event loop
231 * @param arg, the pointer of argument
235 static void *ms_worker_libevent(void *arg
)
237 ms_thread_t
*ms_thread
= pthread_getspecific(ms_thread_key
);
238 ms_thread_ctx_t
*thread_ctx
= (ms_thread_ctx_t
*)arg
;
241 * If system has more than one cpu and supports set cpu
242 * affinity, try to bind each thread to a cpu core;
244 if (ms_setting
.ncpu
> 1)
246 ms_set_thread_cpu_affinity(thread_ctx
->thd_idx
% ms_setting
.ncpu
);
249 if (ms_setup_thread(thread_ctx
) != 0)
254 /* each thread with a timer */
255 ms_clock_handler(0, 0, 0);
257 event_base_loop(ms_thread
->base
, 0);
260 } /* ms_worker_libevent */
264 * Creates a worker thread.
266 * @param func, the callback function
267 * @param arg, the argument to pass to the callback function
269 static void ms_create_worker(void *(*func
)(void *), void *arg
)
275 pthread_attr_init(&attr
);
277 if ((ret
= pthread_create(&thread
, &attr
, func
, arg
)) != 0)
279 fprintf(stderr
, "Can't create thread: %s.\n", strerror(ret
));
282 } /* ms_create_worker */
285 /* initialize threads */
286 void ms_thread_init()
289 (ms_thread_ctx_t
*)malloc(
290 sizeof(ms_thread_ctx_t
) * (size_t)ms_setting
.nthreads
);
291 if (ms_thread_ctx
== NULL
)
293 fprintf(stderr
, "Can't allocate thread descriptors.");
297 for (int i
= 0; i
< ms_setting
.nthreads
; i
++)
299 ms_thread_ctx
[i
].thd_idx
= i
;
300 ms_thread_ctx
[i
].nconns
= ms_setting
.nconns
/ ms_setting
.nthreads
;
303 * If only one server, all the connections in all threads
304 * connects the same server. For support multi-servers, simple
305 * distribute thread to server.
307 ms_thread_ctx
[i
].srv_idx
= i
% ms_setting
.srv_cnt
;
308 ms_thread_ctx
[i
].tps_perconn
= ms_setting
.expected_tps
310 ms_thread_ctx
[i
].exec_num_perconn
= ms_setting
.exec_num
314 if (pthread_key_create(&ms_thread_key
, NULL
))
316 fprintf(stderr
, "Can't create pthread keys. Major malfunction!\n");
319 /* Create threads after we've done all the epoll setup. */
320 for (int i
= 0; i
< ms_setting
.nthreads
; i
++)
322 ms_create_worker(ms_worker_libevent
, (void *)&ms_thread_ctx
[i
]);
324 } /* ms_thread_init */
327 /* cleanup some resource of threads when all the threads exit */
328 void ms_thread_cleanup()
330 if (ms_thread_ctx
!= NULL
)
334 pthread_key_delete(ms_thread_key
);
335 } /* ms_thread_cleanup */