blob: 96ec50d0ce09b3214b374b21dec7f473ad929169 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Thread management routine
2 * Copyright (C) 1998, 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22/* #define DEBUG */
23
24#include <zebra.h>
25
26#include "thread.h"
27#include "memory.h"
28#include "log.h"
paule04ab742003-01-17 23:47:00 +000029#include "hash.h"
30#include "command.h"
paul05c447d2004-07-22 19:14:27 +000031#include "sigevent.h"
paule04ab742003-01-17 23:47:00 +000032
33static struct hash *cpu_record = NULL;
paul718e3742002-12-13 20:15:29 +000034
35/* Struct timeval's tv_usec one second value. */
36#define TIMER_SECOND_MICRO 1000000L
37
paula48b4e62005-04-22 00:43:47 +000038static struct timeval
paul718e3742002-12-13 20:15:29 +000039timeval_adjust (struct timeval a)
40{
41 while (a.tv_usec >= TIMER_SECOND_MICRO)
42 {
43 a.tv_usec -= TIMER_SECOND_MICRO;
44 a.tv_sec++;
45 }
46
47 while (a.tv_usec < 0)
48 {
49 a.tv_usec += TIMER_SECOND_MICRO;
50 a.tv_sec--;
51 }
52
53 if (a.tv_sec < 0)
54 {
55 a.tv_sec = 0;
56 a.tv_usec = 10;
57 }
58
59 if (a.tv_sec > TIMER_SECOND_MICRO)
60 a.tv_sec = TIMER_SECOND_MICRO;
61
62 return a;
63}
64
65static struct timeval
66timeval_subtract (struct timeval a, struct timeval b)
67{
68 struct timeval ret;
69
70 ret.tv_usec = a.tv_usec - b.tv_usec;
71 ret.tv_sec = a.tv_sec - b.tv_sec;
72
73 return timeval_adjust (ret);
74}
75
76static int
77timeval_cmp (struct timeval a, struct timeval b)
78{
79 return (a.tv_sec == b.tv_sec
80 ? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
81}
82
83static unsigned long
84timeval_elapsed (struct timeval a, struct timeval b)
85{
86 return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO)
87 + (a.tv_usec - b.tv_usec));
88}
89
paula48b4e62005-04-22 00:43:47 +000090static unsigned int
paule04ab742003-01-17 23:47:00 +000091cpu_record_hash_key (struct cpu_thread_history *a)
92{
93 return (unsigned int) a->func;
94}
95
96static int
97cpu_record_hash_cmp (struct cpu_thread_history *a,
98 struct cpu_thread_history *b)
99{
100 return a->func == b->func;
101}
102
103static void*
104cpu_record_hash_alloc (struct cpu_thread_history *a)
105{
106 struct cpu_thread_history *new;
paul039b9572004-10-31 16:43:17 +0000107 new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history));
paule04ab742003-01-17 23:47:00 +0000108 new->func = a->func;
paul9d11a192004-10-31 16:19:24 +0000109 new->funcname = XSTRDUP(MTYPE_THREAD_FUNCNAME, a->funcname);
paule04ab742003-01-17 23:47:00 +0000110 return new;
111}
112
113static inline void
114vty_out_cpu_thread_history(struct vty* vty,
115 struct cpu_thread_history *a)
116{
paula48b4e62005-04-22 00:43:47 +0000117 vty_out(vty, " %7ld.%03ld %9d %8ld %10ld %c%c%c%c%c%c %s%s",
paule04ab742003-01-17 23:47:00 +0000118 a->total/1000, a->total%1000, a->total_calls,
119 a->total/a->total_calls, a->max,
120 a->types & (1 << THREAD_READ) ? 'R':' ',
121 a->types & (1 << THREAD_WRITE) ? 'W':' ',
122 a->types & (1 << THREAD_TIMER) ? 'T':' ',
123 a->types & (1 << THREAD_EVENT) ? 'E':' ',
124 a->types & (1 << THREAD_EXECUTE) ? 'X':' ',
paula48b4e62005-04-22 00:43:47 +0000125 a->types & (1 << THREAD_BACKGROUND) ? 'B' : ' ',
paule04ab742003-01-17 23:47:00 +0000126 a->funcname, VTY_NEWLINE);
127}
128
129static void
130cpu_record_hash_print(struct hash_backet *bucket,
131 void *args[])
132{
133 struct cpu_thread_history *totals = args[0];
134 struct vty *vty = args[1];
135 unsigned char *filter = args[2];
136 struct cpu_thread_history *a = bucket->data;
paula48b4e62005-04-22 00:43:47 +0000137
paule04ab742003-01-17 23:47:00 +0000138 a = bucket->data;
139 if ( !(a->types & *filter) )
140 return;
141 vty_out_cpu_thread_history(vty,a);
142 totals->total += a->total;
143 totals->total_calls += a->total_calls;
144 if (totals->max < a->max)
145 totals->max = a->max;
146}
147
148static void
149cpu_record_print(struct vty *vty, unsigned char filter)
150{
151 struct cpu_thread_history tmp;
152 void *args[3] = {&tmp, vty, &filter};
153
154 memset(&tmp, 0, sizeof tmp);
155 tmp.funcname = "TOTAL";
156 tmp.types = filter;
157
158 vty_out(vty,
159 " Runtime(ms) Invoked Avg uSecs Max uSecs Type Thread%s",
160 VTY_NEWLINE);
161 hash_iterate(cpu_record,
162 (void(*)(struct hash_backet*,void*))cpu_record_hash_print,
163 args);
164
165 if (tmp.total_calls > 0)
166 vty_out_cpu_thread_history(vty, &tmp);
167}
168
169DEFUN(show_thread_cpu,
170 show_thread_cpu_cmd,
171 "show thread cpu [FILTER]",
172 SHOW_STR
173 "Thread information\n"
174 "Thread CPU usage\n"
paula48b4e62005-04-22 00:43:47 +0000175 "Display filter (rwtexb)\n")
paule04ab742003-01-17 23:47:00 +0000176{
177 int i = 0;
178 unsigned char filter = 0xff;
179
180 if (argc > 0)
181 {
182 filter = 0;
183 while (argv[0][i] != '\0')
184 {
185 switch ( argv[0][i] )
186 {
187 case 'r':
188 case 'R':
189 filter |= (1 << THREAD_READ);
190 break;
191 case 'w':
192 case 'W':
193 filter |= (1 << THREAD_WRITE);
194 break;
195 case 't':
196 case 'T':
197 filter |= (1 << THREAD_TIMER);
198 break;
199 case 'e':
200 case 'E':
201 filter |= (1 << THREAD_EVENT);
202 break;
203 case 'x':
204 case 'X':
205 filter |= (1 << THREAD_EXECUTE);
206 break;
paula48b4e62005-04-22 00:43:47 +0000207 case 'b':
208 case 'B':
209 filter |= (1 << THREAD_BACKGROUND);
210 break;
paule04ab742003-01-17 23:47:00 +0000211 default:
212 break;
213 }
214 ++i;
215 }
216 if (filter == 0)
217 {
paula48b4e62005-04-22 00:43:47 +0000218 vty_out(vty, "Invalid filter \"%s\" specified,"
219 " must contain at least one of 'RWTEXB'%s",
paule04ab742003-01-17 23:47:00 +0000220 argv[0], VTY_NEWLINE);
221 return CMD_WARNING;
222 }
223 }
224
225 cpu_record_print(vty, filter);
226 return CMD_SUCCESS;
227}
228
paul718e3742002-12-13 20:15:29 +0000229/* List allocation and head/tail print out. */
230static void
231thread_list_debug (struct thread_list *list)
232{
233 printf ("count [%d] head [%p] tail [%p]\n",
234 list->count, list->head, list->tail);
235}
236
237/* Debug print for thread_master. */
238void
239thread_master_debug (struct thread_master *m)
240{
241 printf ("-----------\n");
242 printf ("readlist : ");
243 thread_list_debug (&m->read);
244 printf ("writelist : ");
245 thread_list_debug (&m->write);
246 printf ("timerlist : ");
247 thread_list_debug (&m->timer);
248 printf ("eventlist : ");
249 thread_list_debug (&m->event);
250 printf ("unuselist : ");
251 thread_list_debug (&m->unuse);
paula48b4e62005-04-22 00:43:47 +0000252 printf ("bgndlist : ");
253 thread_list_debug (&m->background);
paul718e3742002-12-13 20:15:29 +0000254 printf ("total alloc: [%ld]\n", m->alloc);
255 printf ("-----------\n");
256}
257
258/* Allocate new thread master. */
259struct thread_master *
260thread_master_create ()
261{
paule04ab742003-01-17 23:47:00 +0000262 if (cpu_record == NULL)
paula48b4e62005-04-22 00:43:47 +0000263 cpu_record = hash_create_size (1011, cpu_record_hash_key,
264 cpu_record_hash_cmp);
265
paul718e3742002-12-13 20:15:29 +0000266 return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER,
267 sizeof (struct thread_master));
268}
269
270/* Add a new thread to the list. */
271static void
272thread_list_add (struct thread_list *list, struct thread *thread)
273{
274 thread->next = NULL;
275 thread->prev = list->tail;
276 if (list->tail)
277 list->tail->next = thread;
278 else
279 list->head = thread;
280 list->tail = thread;
281 list->count++;
282}
283
284/* Add a new thread just before the point. */
285static void
286thread_list_add_before (struct thread_list *list,
287 struct thread *point,
288 struct thread *thread)
289{
290 thread->next = point;
291 thread->prev = point->prev;
292 if (point->prev)
293 point->prev->next = thread;
294 else
295 list->head = thread;
296 point->prev = thread;
297 list->count++;
298}
299
300/* Delete a thread from the list. */
301static struct thread *
302thread_list_delete (struct thread_list *list, struct thread *thread)
303{
304 if (thread->next)
305 thread->next->prev = thread->prev;
306 else
307 list->tail = thread->prev;
308 if (thread->prev)
309 thread->prev->next = thread->next;
310 else
311 list->head = thread->next;
312 thread->next = thread->prev = NULL;
313 list->count--;
314 return thread;
315}
316
317/* Move thread to unuse list. */
318static void
319thread_add_unuse (struct thread_master *m, struct thread *thread)
320{
paula48b4e62005-04-22 00:43:47 +0000321 assert (m != NULL && thread != NULL);
paul718e3742002-12-13 20:15:29 +0000322 assert (thread->next == NULL);
323 assert (thread->prev == NULL);
324 assert (thread->type == THREAD_UNUSED);
325 thread_list_add (&m->unuse, thread);
paul9d11a192004-10-31 16:19:24 +0000326 /* XXX: Should we deallocate funcname here? */
paul718e3742002-12-13 20:15:29 +0000327}
328
329/* Free all unused thread. */
330static void
331thread_list_free (struct thread_master *m, struct thread_list *list)
332{
333 struct thread *t;
334 struct thread *next;
335
336 for (t = list->head; t; t = next)
337 {
338 next = t->next;
paul9d11a192004-10-31 16:19:24 +0000339 XFREE (MTYPE_THREAD_FUNCNAME, t->funcname);
paul718e3742002-12-13 20:15:29 +0000340 XFREE (MTYPE_THREAD, t);
341 list->count--;
342 m->alloc--;
343 }
344}
345
346/* Stop thread scheduler. */
347void
348thread_master_free (struct thread_master *m)
349{
350 thread_list_free (m, &m->read);
351 thread_list_free (m, &m->write);
352 thread_list_free (m, &m->timer);
353 thread_list_free (m, &m->event);
354 thread_list_free (m, &m->ready);
355 thread_list_free (m, &m->unuse);
paula48b4e62005-04-22 00:43:47 +0000356 thread_list_free (m, &m->background);
357
paul718e3742002-12-13 20:15:29 +0000358 XFREE (MTYPE_THREAD_MASTER, m);
359}
360
361/* Delete top of the list and return it. */
362static struct thread *
363thread_trim_head (struct thread_list *list)
364{
365 if (list->head)
366 return thread_list_delete (list, list->head);
367 return NULL;
368}
369
370/* Thread list is empty or not. */
371int
372thread_empty (struct thread_list *list)
373{
374 return list->head ? 0 : 1;
375}
376
377/* Return remain time in second. */
378unsigned long
379thread_timer_remain_second (struct thread *thread)
380{
381 struct timeval timer_now;
382
383 gettimeofday (&timer_now, NULL);
384
385 if (thread->u.sands.tv_sec - timer_now.tv_sec > 0)
386 return thread->u.sands.tv_sec - timer_now.tv_sec;
387 else
388 return 0;
389}
390
paule04ab742003-01-17 23:47:00 +0000391/* Trim blankspace and "()"s */
392static char *
hasso8c328f12004-10-05 21:01:23 +0000393strip_funcname (const char *funcname)
paule04ab742003-01-17 23:47:00 +0000394{
395 char buff[100];
396 char tmp, *ret, *e, *b = buff;
397
398 strncpy(buff, funcname, sizeof(buff));
399 buff[ sizeof(buff) -1] = '\0';
400 e = buff +strlen(buff) -1;
401
402 /* Wont work for funcname == "Word (explanation)" */
403
404 while (*b == ' ' || *b == '(')
405 ++b;
406 while (*e == ' ' || *e == ')')
407 --e;
408 e++;
409
410 tmp = *e;
411 *e = '\0';
paul9d11a192004-10-31 16:19:24 +0000412 ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b);
paule04ab742003-01-17 23:47:00 +0000413 *e = tmp;
414
415 return ret;
416}
417
paul718e3742002-12-13 20:15:29 +0000418/* Get new thread. */
419static struct thread *
420thread_get (struct thread_master *m, u_char type,
hasso8c328f12004-10-05 21:01:23 +0000421 int (*func) (struct thread *), void *arg, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000422{
423 struct thread *thread;
424
425 if (m->unuse.head)
paul2946f652003-03-27 23:48:24 +0000426 {
427 thread = thread_trim_head (&m->unuse);
428 if (thread->funcname)
paul9d11a192004-10-31 16:19:24 +0000429 XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname);
paul2946f652003-03-27 23:48:24 +0000430 }
paul718e3742002-12-13 20:15:29 +0000431 else
432 {
433 thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
434 m->alloc++;
435 }
436 thread->type = type;
paule04ab742003-01-17 23:47:00 +0000437 thread->add_type = type;
paul718e3742002-12-13 20:15:29 +0000438 thread->master = m;
439 thread->func = func;
440 thread->arg = arg;
441
paule04ab742003-01-17 23:47:00 +0000442 thread->funcname = strip_funcname(funcname);
443
paul718e3742002-12-13 20:15:29 +0000444 return thread;
445}
446
447/* Add new read thread. */
448struct thread *
paule04ab742003-01-17 23:47:00 +0000449funcname_thread_add_read (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000450 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000451{
452 struct thread *thread;
453
454 assert (m != NULL);
455
456 if (FD_ISSET (fd, &m->readfd))
457 {
458 zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
459 return NULL;
460 }
461
paule04ab742003-01-17 23:47:00 +0000462 thread = thread_get (m, THREAD_READ, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000463 FD_SET (fd, &m->readfd);
464 thread->u.fd = fd;
465 thread_list_add (&m->read, thread);
466
467 return thread;
468}
469
470/* Add new write thread. */
471struct thread *
paule04ab742003-01-17 23:47:00 +0000472funcname_thread_add_write (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000473 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000474{
475 struct thread *thread;
476
477 assert (m != NULL);
478
479 if (FD_ISSET (fd, &m->writefd))
480 {
481 zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
482 return NULL;
483 }
484
paule04ab742003-01-17 23:47:00 +0000485 thread = thread_get (m, THREAD_WRITE, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000486 FD_SET (fd, &m->writefd);
487 thread->u.fd = fd;
488 thread_list_add (&m->write, thread);
489
490 return thread;
491}
492
paul98c91ac2004-10-05 14:57:50 +0000493static struct thread *
494funcname_thread_add_timer_timeval (struct thread_master *m,
495 int (*func) (struct thread *),
paula48b4e62005-04-22 00:43:47 +0000496 int type,
paul98c91ac2004-10-05 14:57:50 +0000497 void *arg,
498 struct timeval *time_relative,
hasso8c328f12004-10-05 21:01:23 +0000499 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000500{
paul718e3742002-12-13 20:15:29 +0000501 struct thread *thread;
paul98c91ac2004-10-05 14:57:50 +0000502 struct timeval timer_now;
paula48b4e62005-04-22 00:43:47 +0000503 struct thread_list *list;
paul718e3742002-12-13 20:15:29 +0000504 struct thread *tt;
paul718e3742002-12-13 20:15:29 +0000505
506 assert (m != NULL);
507
paula48b4e62005-04-22 00:43:47 +0000508 assert (type == THREAD_TIMER || THREAD_BACKGROUND);
509 assert (time_relative);
510
511 switch (type)
512 {
513 case THREAD_TIMER:
514 list = &m->timer;
515 break;
516 case THREAD_BACKGROUND:
517 list = &m->background;
518 break;
519 default:
520 return NULL;
521 }
522
523 thread = thread_get (m, type, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000524
525 /* Do we need jitter here? */
526 gettimeofday (&timer_now, NULL);
paul98c91ac2004-10-05 14:57:50 +0000527 timer_now.tv_sec += time_relative->tv_sec;
528 timer_now.tv_usec += time_relative->tv_usec;
529 timeval_adjust (timer_now);
paul718e3742002-12-13 20:15:29 +0000530 thread->u.sands = timer_now;
531
532 /* Sort by timeval. */
paula48b4e62005-04-22 00:43:47 +0000533 for (tt = list->head; tt; tt = tt->next)
paul718e3742002-12-13 20:15:29 +0000534 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
535 break;
536
537 if (tt)
paula48b4e62005-04-22 00:43:47 +0000538 thread_list_add_before (list, tt, thread);
paul718e3742002-12-13 20:15:29 +0000539 else
paula48b4e62005-04-22 00:43:47 +0000540 thread_list_add (list, thread);
paul718e3742002-12-13 20:15:29 +0000541
542 return thread;
543}
544
paul98c91ac2004-10-05 14:57:50 +0000545
546/* Add timer event thread. */
jardin9e867fe2003-12-23 08:56:18 +0000547struct thread *
paul98c91ac2004-10-05 14:57:50 +0000548funcname_thread_add_timer (struct thread_master *m,
549 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000550 void *arg, long timer, const char* funcname)
jardin9e867fe2003-12-23 08:56:18 +0000551{
paul98c91ac2004-10-05 14:57:50 +0000552 struct timeval trel;
jardin9e867fe2003-12-23 08:56:18 +0000553
554 assert (m != NULL);
555
paul9076fbd2004-10-11 09:40:58 +0000556 trel.tv_sec = timer;
paul98c91ac2004-10-05 14:57:50 +0000557 trel.tv_usec = 0;
558
paula48b4e62005-04-22 00:43:47 +0000559 return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg,
560 &trel, funcname);
paul98c91ac2004-10-05 14:57:50 +0000561}
562
563/* Add timer event thread with "millisecond" resolution */
564struct thread *
565funcname_thread_add_timer_msec (struct thread_master *m,
566 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000567 void *arg, long timer, const char* funcname)
paul98c91ac2004-10-05 14:57:50 +0000568{
569 struct timeval trel;
570
571 assert (m != NULL);
jardin9e867fe2003-12-23 08:56:18 +0000572
ajsaf04bd72004-12-28 17:00:12 +0000573 trel.tv_sec = timer / 1000;
574 trel.tv_usec = 1000*(timer % 1000);
jardin9e867fe2003-12-23 08:56:18 +0000575
paula48b4e62005-04-22 00:43:47 +0000576 return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER,
577 arg, &trel, funcname);
578}
579
580/* Add a background thread, with an optional millisec delay */
581struct thread *
582funcname_thread_add_background (struct thread_master *m,
583 int (*func) (struct thread *),
584 void *arg, long delay,
585 const char *funcname)
586{
587 struct timeval trel;
588
589 assert (m != NULL);
590
591 if (delay)
592 {
593 trel.tv_sec = delay / 1000;
594 trel.tv_usec = 1000*(delay % 1000);
595 }
596 else
597 {
598 trel.tv_sec = 0;
599 trel.tv_usec = 0;
600 }
601
602 return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND,
603 arg, &trel, funcname);
jardin9e867fe2003-12-23 08:56:18 +0000604}
605
paul718e3742002-12-13 20:15:29 +0000606/* Add simple event thread. */
607struct thread *
paule04ab742003-01-17 23:47:00 +0000608funcname_thread_add_event (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000609 int (*func) (struct thread *), void *arg, int val, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000610{
611 struct thread *thread;
612
613 assert (m != NULL);
614
paule04ab742003-01-17 23:47:00 +0000615 thread = thread_get (m, THREAD_EVENT, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000616 thread->u.val = val;
617 thread_list_add (&m->event, thread);
618
619 return thread;
620}
621
622/* Cancel thread from scheduler. */
623void
624thread_cancel (struct thread *thread)
625{
paula48b4e62005-04-22 00:43:47 +0000626 struct thread_list *list;
627
paul718e3742002-12-13 20:15:29 +0000628 switch (thread->type)
629 {
630 case THREAD_READ:
631 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
632 FD_CLR (thread->u.fd, &thread->master->readfd);
paula48b4e62005-04-22 00:43:47 +0000633 list = &thread->master->read;
paul718e3742002-12-13 20:15:29 +0000634 break;
635 case THREAD_WRITE:
636 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
637 FD_CLR (thread->u.fd, &thread->master->writefd);
paula48b4e62005-04-22 00:43:47 +0000638 list = &thread->master->write;
paul718e3742002-12-13 20:15:29 +0000639 break;
640 case THREAD_TIMER:
paula48b4e62005-04-22 00:43:47 +0000641 list = &thread->master->timer;
paul718e3742002-12-13 20:15:29 +0000642 break;
643 case THREAD_EVENT:
paula48b4e62005-04-22 00:43:47 +0000644 list = &thread->master->event;
paul718e3742002-12-13 20:15:29 +0000645 break;
646 case THREAD_READY:
paula48b4e62005-04-22 00:43:47 +0000647 list = &thread->master->ready;
paul718e3742002-12-13 20:15:29 +0000648 break;
paula48b4e62005-04-22 00:43:47 +0000649 case THREAD_BACKGROUND:
650 list = &thread->master->background;
paul718e3742002-12-13 20:15:29 +0000651 default:
paula48b4e62005-04-22 00:43:47 +0000652 return;
paul718e3742002-12-13 20:15:29 +0000653 break;
654 }
paula48b4e62005-04-22 00:43:47 +0000655 thread_list_delete (list, thread);
paul718e3742002-12-13 20:15:29 +0000656 thread->type = THREAD_UNUSED;
657 thread_add_unuse (thread->master, thread);
658}
659
660/* Delete all events which has argument value arg. */
661void
662thread_cancel_event (struct thread_master *m, void *arg)
663{
664 struct thread *thread;
665
666 thread = m->event.head;
667 while (thread)
668 {
669 struct thread *t;
670
671 t = thread;
672 thread = t->next;
673
674 if (t->arg == arg)
paula48b4e62005-04-22 00:43:47 +0000675 {
676 thread_list_delete (&m->event, t);
677 t->type = THREAD_UNUSED;
678 thread_add_unuse (m, t);
679 }
paul718e3742002-12-13 20:15:29 +0000680 }
681}
682
paula48b4e62005-04-22 00:43:47 +0000683static struct timeval *
684thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val)
paul718e3742002-12-13 20:15:29 +0000685{
686 struct timeval timer_now;
687 struct timeval timer_min;
688
paula48b4e62005-04-22 00:43:47 +0000689 if (tlist->head)
paul718e3742002-12-13 20:15:29 +0000690 {
691 gettimeofday (&timer_now, NULL);
paula48b4e62005-04-22 00:43:47 +0000692 timer_min = tlist->head->u.sands;
paul718e3742002-12-13 20:15:29 +0000693 timer_min = timeval_subtract (timer_min, timer_now);
694 if (timer_min.tv_sec < 0)
695 {
696 timer_min.tv_sec = 0;
697 timer_min.tv_usec = 10;
698 }
699 *timer_val = timer_min;
700 return timer_val;
701 }
702 return NULL;
703}
paul718e3742002-12-13 20:15:29 +0000704
705struct thread *
706thread_run (struct thread_master *m, struct thread *thread,
707 struct thread *fetch)
708{
709 *fetch = *thread;
710 thread->type = THREAD_UNUSED;
711 thread_add_unuse (m, thread);
712 return fetch;
713}
714
paula48b4e62005-04-22 00:43:47 +0000715static int
716thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset)
paul718e3742002-12-13 20:15:29 +0000717{
718 struct thread *thread;
719 struct thread *next;
720 int ready = 0;
paula48b4e62005-04-22 00:43:47 +0000721
722 assert (list);
723
paul718e3742002-12-13 20:15:29 +0000724 for (thread = list->head; thread; thread = next)
725 {
726 next = thread->next;
727
728 if (FD_ISSET (THREAD_FD (thread), fdset))
paula48b4e62005-04-22 00:43:47 +0000729 {
730 assert (FD_ISSET (THREAD_FD (thread), mfdset));
731 FD_CLR(THREAD_FD (thread), mfdset);
732 thread_list_delete (list, thread);
733 thread_list_add (&thread->master->ready, thread);
734 thread->type = THREAD_READY;
735 ready++;
736 }
paul718e3742002-12-13 20:15:29 +0000737 }
738 return ready;
739}
740
paula48b4e62005-04-22 00:43:47 +0000741/* fetch next timer-like thread thread from the list and return it */
742static unsigned int
743thread_timer_process (struct thread_list *list, struct timeval *timenow)
744{
745 struct thread *thread;
746 unsigned int ready = 0;
747
748 assert (list && timenow);
749
750 for (thread = list->head; thread; thread = thread->next)
751 if (timeval_cmp (*timenow, thread->u.sands) >= 0)
752 {
753 thread_list_delete (list, thread);
754 assert (thread->next == thread->prev && thread->next == NULL);
755 thread->next = thread->prev = NULL;
756 thread->type = THREAD_READY;
757 thread_list_add (&thread->master->ready, thread);
758 ready++;
759 }
760 return ready;
761}
762
paul718e3742002-12-13 20:15:29 +0000763/* Fetch next ready thread. */
764struct thread *
765thread_fetch (struct thread_master *m, struct thread *fetch)
766{
paul718e3742002-12-13 20:15:29 +0000767 struct thread *thread;
768 fd_set readfd;
769 fd_set writefd;
770 fd_set exceptfd;
771 struct timeval timer_now;
772 struct timeval timer_val;
paula48b4e62005-04-22 00:43:47 +0000773 struct timeval timer_val_bg;
paul718e3742002-12-13 20:15:29 +0000774 struct timeval *timer_wait;
paula48b4e62005-04-22 00:43:47 +0000775 struct timeval *timer_wait_bg;
paul718e3742002-12-13 20:15:29 +0000776
777 while (1)
778 {
paula48b4e62005-04-22 00:43:47 +0000779 int num = 0;
780 int ready = m->ready.count;
781
paul05c447d2004-07-22 19:14:27 +0000782 /* Signals are highest priority */
783 quagga_sigevent_process ();
784
785 /* Normal event are the next highest priority. */
paul718e3742002-12-13 20:15:29 +0000786 if ((thread = thread_trim_head (&m->event)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000787 return thread_run (m, thread, fetch);
paula48b4e62005-04-22 00:43:47 +0000788
paul718e3742002-12-13 20:15:29 +0000789 /* Execute timer. */
790 gettimeofday (&timer_now, NULL);
paula48b4e62005-04-22 00:43:47 +0000791
792 /* Timer threads */
793 ready += thread_timer_process (&m->timer, &timer_now);
794
795 /* If there are any ready threads from previous scheduler runs,
796 * process top of them.
797 */
paul718e3742002-12-13 20:15:29 +0000798 if ((thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000799 return thread_run (m, thread, fetch);
paula48b4e62005-04-22 00:43:47 +0000800
paul718e3742002-12-13 20:15:29 +0000801 /* Structure copy. */
802 readfd = m->readfd;
803 writefd = m->writefd;
804 exceptfd = m->exceptfd;
paula48b4e62005-04-22 00:43:47 +0000805
806 /* Calculate select wait timer if nothing else to do */
807 if (ready == 0)
808 {
809 timer_wait = thread_timer_wait (&m->timer, &timer_val);
810 timer_wait_bg = thread_timer_wait (&m->background, &timer_val_bg);
811
812 if (timer_wait && timer_wait_bg &&
813 (timeval_cmp (*timer_wait, *timer_wait_bg) > 0))
814 timer_wait = timer_wait_bg;
815 else if (!timer_wait && timer_wait_bg)
816 timer_wait = timer_wait_bg;
817 }
818 else
819 {
820 timer_val.tv_sec = timer_val.tv_usec = 0;
821 timer_wait = &timer_val;
822 }
823
paul718e3742002-12-13 20:15:29 +0000824 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
paula48b4e62005-04-22 00:43:47 +0000825
826 /* Signals should get quick treatment */
paul718e3742002-12-13 20:15:29 +0000827 if (num < 0)
paul05c447d2004-07-22 19:14:27 +0000828 {
829 if (errno == EINTR)
paula48b4e62005-04-22 00:43:47 +0000830 continue; /* signal received - process it */
ajs6099b3b2004-11-20 02:06:59 +0000831 zlog_warn ("select() error: %s", safe_strerror (errno));
paul05c447d2004-07-22 19:14:27 +0000832 return NULL;
833 }
paula48b4e62005-04-22 00:43:47 +0000834
835 /* Got IO, process it */
836 if (num > 0)
837 {
838 /* Normal priority read thead. */
839 ready += thread_process_fd (&m->read, &readfd, &m->readfd);
840 /* Write thead. */
841 ready += thread_process_fd (&m->write, &writefd, &m->writefd);
842 }
843
844 /* Background timer/events, lowest priority */
845 ready += thread_timer_process (&m->background, &timer_now);
846
847 /* if any threads were made ready above.. */
848 if ((ready > 0) && (thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000849 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000850 }
851}
852
ajs924b9222005-04-16 17:11:24 +0000853unsigned long
paul718e3742002-12-13 20:15:29 +0000854thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
855{
856 unsigned long thread_time;
857
858#ifdef HAVE_RUSAGE
859 /* This is 'user + sys' time. */
860 thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
861 thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
862#else
863 /* When rusage is not available, simple elapsed time is used. */
864 thread_time = timeval_elapsed (*now, *start);
865#endif /* HAVE_RUSAGE */
866
867 return thread_time;
868}
869
870/* We should aim to yield after THREAD_YIELD_TIME_SLOT
871 milliseconds. */
872int
873thread_should_yield (struct thread *thread)
874{
875 RUSAGE_T ru;
876
877 GETRUSAGE (&ru);
paula48b4e62005-04-22 00:43:47 +0000878
879 return (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT);
paul718e3742002-12-13 20:15:29 +0000880}
881
882/* We check thread consumed time. If the system has getrusage, we'll
883 use that to get indepth stats on the performance of the thread. If
884 not - we'll use gettimeofday for some guestimation. */
885void
886thread_call (struct thread *thread)
887{
888 unsigned long thread_time;
889 RUSAGE_T ru;
paule04ab742003-01-17 23:47:00 +0000890 struct cpu_thread_history tmp, *cpu;
891
892 tmp.func = thread->func;
893 tmp.funcname = thread->funcname;
894 cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc);
paul718e3742002-12-13 20:15:29 +0000895
896 GETRUSAGE (&thread->ru);
897
898 (*thread->func) (thread);
899
900 GETRUSAGE (&ru);
901
902 thread_time = thread_consumed_time (&ru, &thread->ru);
paule04ab742003-01-17 23:47:00 +0000903 cpu->total += thread_time;
904 if (cpu->max < thread_time)
905 cpu->max = thread_time;
906
907 ++cpu->total_calls;
908 cpu->types |= (1 << thread->add_type);
paul718e3742002-12-13 20:15:29 +0000909
ajs924b9222005-04-16 17:11:24 +0000910#ifdef CONSUMED_TIME_CHECK
911 if (thread_time > CONSUMED_TIME_CHECK)
paul718e3742002-12-13 20:15:29 +0000912 {
913 /*
914 * We have a CPU Hog on our hands.
915 * Whinge about it now, so we're aware this is yet another task
916 * to fix.
917 */
ajs924b9222005-04-16 17:11:24 +0000918 zlog_warn ("CPU HOG: task %s (%lx) ran for %ldms",
919 thread->funcname,
920 (unsigned long) thread->func,
921 thread_time / 1000L);
paul718e3742002-12-13 20:15:29 +0000922 }
ajs924b9222005-04-16 17:11:24 +0000923#endif /* CONSUMED_TIME_CHECK */
paul718e3742002-12-13 20:15:29 +0000924}
925
926/* Execute thread */
927struct thread *
paule04ab742003-01-17 23:47:00 +0000928funcname_thread_execute (struct thread_master *m,
paul718e3742002-12-13 20:15:29 +0000929 int (*func)(struct thread *),
930 void *arg,
paule04ab742003-01-17 23:47:00 +0000931 int val,
hasso8c328f12004-10-05 21:01:23 +0000932 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000933{
934 struct thread dummy;
935
936 memset (&dummy, 0, sizeof (struct thread));
937
938 dummy.type = THREAD_EVENT;
paule04ab742003-01-17 23:47:00 +0000939 dummy.add_type = THREAD_EXECUTE;
paul718e3742002-12-13 20:15:29 +0000940 dummy.master = NULL;
941 dummy.func = func;
942 dummy.arg = arg;
943 dummy.u.val = val;
paule04ab742003-01-17 23:47:00 +0000944 dummy.funcname = strip_funcname (funcname);
paul718e3742002-12-13 20:15:29 +0000945 thread_call (&dummy);
946
paul9d11a192004-10-31 16:19:24 +0000947 XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname);
paul2946f652003-03-27 23:48:24 +0000948
paul718e3742002-12-13 20:15:29 +0000949 return NULL;
950}