blob: 2847b8bfde2a6a6cd699de3ca5432260f93d5604 [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
38struct timeval
39timeval_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
paule04ab742003-01-17 23:47:00 +000090static unsigned int
91cpu_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;
107 new = XMALLOC( MTYPE_TMP/*XXX*/, sizeof *new);
108 memset(new, 0, sizeof *new);
109 new->func = a->func;
110 new->funcname = XSTRDUP(MTYPE_TMP/*XXX*/,a->funcname);
111 return new;
112}
113
114static inline void
115vty_out_cpu_thread_history(struct vty* vty,
116 struct cpu_thread_history *a)
117{
118 vty_out(vty, " %7ld.%03ld %9d %8ld %10ld %c%c%c%c%c %s%s",
119 a->total/1000, a->total%1000, a->total_calls,
120 a->total/a->total_calls, a->max,
121 a->types & (1 << THREAD_READ) ? 'R':' ',
122 a->types & (1 << THREAD_WRITE) ? 'W':' ',
123 a->types & (1 << THREAD_TIMER) ? 'T':' ',
124 a->types & (1 << THREAD_EVENT) ? 'E':' ',
125 a->types & (1 << THREAD_EXECUTE) ? 'X':' ',
126 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;
137
138
139 a = bucket->data;
140 if ( !(a->types & *filter) )
141 return;
142 vty_out_cpu_thread_history(vty,a);
143 totals->total += a->total;
144 totals->total_calls += a->total_calls;
145 if (totals->max < a->max)
146 totals->max = a->max;
147}
148
149static void
150cpu_record_print(struct vty *vty, unsigned char filter)
151{
152 struct cpu_thread_history tmp;
153 void *args[3] = {&tmp, vty, &filter};
154
155 memset(&tmp, 0, sizeof tmp);
156 tmp.funcname = "TOTAL";
157 tmp.types = filter;
158
159 vty_out(vty,
160 " Runtime(ms) Invoked Avg uSecs Max uSecs Type Thread%s",
161 VTY_NEWLINE);
162 hash_iterate(cpu_record,
163 (void(*)(struct hash_backet*,void*))cpu_record_hash_print,
164 args);
165
166 if (tmp.total_calls > 0)
167 vty_out_cpu_thread_history(vty, &tmp);
168}
169
170DEFUN(show_thread_cpu,
171 show_thread_cpu_cmd,
172 "show thread cpu [FILTER]",
173 SHOW_STR
174 "Thread information\n"
175 "Thread CPU usage\n"
176 "Display filter (rwtex)\n")
177{
178 int i = 0;
179 unsigned char filter = 0xff;
180
181 if (argc > 0)
182 {
183 filter = 0;
184 while (argv[0][i] != '\0')
185 {
186 switch ( argv[0][i] )
187 {
188 case 'r':
189 case 'R':
190 filter |= (1 << THREAD_READ);
191 break;
192 case 'w':
193 case 'W':
194 filter |= (1 << THREAD_WRITE);
195 break;
196 case 't':
197 case 'T':
198 filter |= (1 << THREAD_TIMER);
199 break;
200 case 'e':
201 case 'E':
202 filter |= (1 << THREAD_EVENT);
203 break;
204 case 'x':
205 case 'X':
206 filter |= (1 << THREAD_EXECUTE);
207 break;
208 default:
209 break;
210 }
211 ++i;
212 }
213 if (filter == 0)
214 {
215 vty_out(vty, "Invalid filter \"%s\" specified, must contain at least one of 'RWTEX'%s",
216 argv[0], VTY_NEWLINE);
217 return CMD_WARNING;
218 }
219 }
220
221 cpu_record_print(vty, filter);
222 return CMD_SUCCESS;
223}
224
paul718e3742002-12-13 20:15:29 +0000225/* List allocation and head/tail print out. */
226static void
227thread_list_debug (struct thread_list *list)
228{
229 printf ("count [%d] head [%p] tail [%p]\n",
230 list->count, list->head, list->tail);
231}
232
233/* Debug print for thread_master. */
234void
235thread_master_debug (struct thread_master *m)
236{
237 printf ("-----------\n");
238 printf ("readlist : ");
239 thread_list_debug (&m->read);
240 printf ("writelist : ");
241 thread_list_debug (&m->write);
242 printf ("timerlist : ");
243 thread_list_debug (&m->timer);
244 printf ("eventlist : ");
245 thread_list_debug (&m->event);
246 printf ("unuselist : ");
247 thread_list_debug (&m->unuse);
248 printf ("total alloc: [%ld]\n", m->alloc);
249 printf ("-----------\n");
250}
251
252/* Allocate new thread master. */
253struct thread_master *
254thread_master_create ()
255{
paule04ab742003-01-17 23:47:00 +0000256 if (cpu_record == NULL)
257 {
258 cpu_record = hash_create_size( 1011, cpu_record_hash_key, cpu_record_hash_cmp);
259 }
paul718e3742002-12-13 20:15:29 +0000260 return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER,
261 sizeof (struct thread_master));
262}
263
264/* Add a new thread to the list. */
265static void
266thread_list_add (struct thread_list *list, struct thread *thread)
267{
268 thread->next = NULL;
269 thread->prev = list->tail;
270 if (list->tail)
271 list->tail->next = thread;
272 else
273 list->head = thread;
274 list->tail = thread;
275 list->count++;
276}
277
278/* Add a new thread just before the point. */
279static void
280thread_list_add_before (struct thread_list *list,
281 struct thread *point,
282 struct thread *thread)
283{
284 thread->next = point;
285 thread->prev = point->prev;
286 if (point->prev)
287 point->prev->next = thread;
288 else
289 list->head = thread;
290 point->prev = thread;
291 list->count++;
292}
293
294/* Delete a thread from the list. */
295static struct thread *
296thread_list_delete (struct thread_list *list, struct thread *thread)
297{
298 if (thread->next)
299 thread->next->prev = thread->prev;
300 else
301 list->tail = thread->prev;
302 if (thread->prev)
303 thread->prev->next = thread->next;
304 else
305 list->head = thread->next;
306 thread->next = thread->prev = NULL;
307 list->count--;
308 return thread;
309}
310
311/* Move thread to unuse list. */
312static void
313thread_add_unuse (struct thread_master *m, struct thread *thread)
314{
315 assert (m != NULL);
316 assert (thread->next == NULL);
317 assert (thread->prev == NULL);
318 assert (thread->type == THREAD_UNUSED);
319 thread_list_add (&m->unuse, thread);
320}
321
322/* Free all unused thread. */
323static void
324thread_list_free (struct thread_master *m, struct thread_list *list)
325{
326 struct thread *t;
327 struct thread *next;
328
329 for (t = list->head; t; t = next)
330 {
331 next = t->next;
pauld5e86ad2003-03-12 05:40:11 +0000332 XFREE (MTYPE_STRVEC, t->funcname);
paul718e3742002-12-13 20:15:29 +0000333 XFREE (MTYPE_THREAD, t);
334 list->count--;
335 m->alloc--;
336 }
337}
338
339/* Stop thread scheduler. */
340void
341thread_master_free (struct thread_master *m)
342{
343 thread_list_free (m, &m->read);
344 thread_list_free (m, &m->write);
345 thread_list_free (m, &m->timer);
346 thread_list_free (m, &m->event);
347 thread_list_free (m, &m->ready);
348 thread_list_free (m, &m->unuse);
349
350 XFREE (MTYPE_THREAD_MASTER, m);
351}
352
353/* Delete top of the list and return it. */
354static struct thread *
355thread_trim_head (struct thread_list *list)
356{
357 if (list->head)
358 return thread_list_delete (list, list->head);
359 return NULL;
360}
361
362/* Thread list is empty or not. */
363int
364thread_empty (struct thread_list *list)
365{
366 return list->head ? 0 : 1;
367}
368
369/* Return remain time in second. */
370unsigned long
371thread_timer_remain_second (struct thread *thread)
372{
373 struct timeval timer_now;
374
375 gettimeofday (&timer_now, NULL);
376
377 if (thread->u.sands.tv_sec - timer_now.tv_sec > 0)
378 return thread->u.sands.tv_sec - timer_now.tv_sec;
379 else
380 return 0;
381}
382
paule04ab742003-01-17 23:47:00 +0000383/* Trim blankspace and "()"s */
384static char *
hasso8c328f12004-10-05 21:01:23 +0000385strip_funcname (const char *funcname)
paule04ab742003-01-17 23:47:00 +0000386{
387 char buff[100];
388 char tmp, *ret, *e, *b = buff;
389
390 strncpy(buff, funcname, sizeof(buff));
391 buff[ sizeof(buff) -1] = '\0';
392 e = buff +strlen(buff) -1;
393
394 /* Wont work for funcname == "Word (explanation)" */
395
396 while (*b == ' ' || *b == '(')
397 ++b;
398 while (*e == ' ' || *e == ')')
399 --e;
400 e++;
401
402 tmp = *e;
403 *e = '\0';
pauld5e86ad2003-03-12 05:40:11 +0000404 ret = XSTRDUP (MTYPE_STRVEC, b);
paule04ab742003-01-17 23:47:00 +0000405 *e = tmp;
406
407 return ret;
408}
409
paul718e3742002-12-13 20:15:29 +0000410/* Get new thread. */
411static struct thread *
412thread_get (struct thread_master *m, u_char type,
hasso8c328f12004-10-05 21:01:23 +0000413 int (*func) (struct thread *), void *arg, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000414{
415 struct thread *thread;
416
417 if (m->unuse.head)
paul2946f652003-03-27 23:48:24 +0000418 {
419 thread = thread_trim_head (&m->unuse);
420 if (thread->funcname)
421 XFREE(MTYPE_STRVEC, thread->funcname);
422 }
paul718e3742002-12-13 20:15:29 +0000423 else
424 {
425 thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
426 m->alloc++;
427 }
428 thread->type = type;
paule04ab742003-01-17 23:47:00 +0000429 thread->add_type = type;
paul718e3742002-12-13 20:15:29 +0000430 thread->master = m;
431 thread->func = func;
432 thread->arg = arg;
433
paule04ab742003-01-17 23:47:00 +0000434 thread->funcname = strip_funcname(funcname);
435
paul718e3742002-12-13 20:15:29 +0000436 return thread;
437}
438
439/* Add new read thread. */
440struct thread *
paule04ab742003-01-17 23:47:00 +0000441funcname_thread_add_read (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000442 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000443{
444 struct thread *thread;
445
446 assert (m != NULL);
447
448 if (FD_ISSET (fd, &m->readfd))
449 {
450 zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
451 return NULL;
452 }
453
paule04ab742003-01-17 23:47:00 +0000454 thread = thread_get (m, THREAD_READ, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000455 FD_SET (fd, &m->readfd);
456 thread->u.fd = fd;
457 thread_list_add (&m->read, thread);
458
459 return thread;
460}
461
462/* Add new write thread. */
463struct thread *
paule04ab742003-01-17 23:47:00 +0000464funcname_thread_add_write (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000465 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000466{
467 struct thread *thread;
468
469 assert (m != NULL);
470
471 if (FD_ISSET (fd, &m->writefd))
472 {
473 zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
474 return NULL;
475 }
476
paule04ab742003-01-17 23:47:00 +0000477 thread = thread_get (m, THREAD_WRITE, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000478 FD_SET (fd, &m->writefd);
479 thread->u.fd = fd;
480 thread_list_add (&m->write, thread);
481
482 return thread;
483}
484
paul98c91ac2004-10-05 14:57:50 +0000485static struct thread *
486funcname_thread_add_timer_timeval (struct thread_master *m,
487 int (*func) (struct thread *),
488 void *arg,
489 struct timeval *time_relative,
hasso8c328f12004-10-05 21:01:23 +0000490 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000491{
paul718e3742002-12-13 20:15:29 +0000492 struct thread *thread;
paul98c91ac2004-10-05 14:57:50 +0000493 struct timeval timer_now;
paul718e3742002-12-13 20:15:29 +0000494#ifndef TIMER_NO_SORT
495 struct thread *tt;
496#endif /* TIMER_NO_SORT */
497
498 assert (m != NULL);
499
paule04ab742003-01-17 23:47:00 +0000500 thread = thread_get (m, THREAD_TIMER, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000501
502 /* Do we need jitter here? */
503 gettimeofday (&timer_now, NULL);
paul98c91ac2004-10-05 14:57:50 +0000504 timer_now.tv_sec += time_relative->tv_sec;
505 timer_now.tv_usec += time_relative->tv_usec;
506 timeval_adjust (timer_now);
paul718e3742002-12-13 20:15:29 +0000507 thread->u.sands = timer_now;
508
509 /* Sort by timeval. */
510#ifdef TIMER_NO_SORT
511 thread_list_add (&m->timer, thread);
512#else
513 for (tt = m->timer.head; tt; tt = tt->next)
514 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
515 break;
516
517 if (tt)
518 thread_list_add_before (&m->timer, tt, thread);
519 else
520 thread_list_add (&m->timer, thread);
521#endif /* TIMER_NO_SORT */
522
523 return thread;
524}
525
paul98c91ac2004-10-05 14:57:50 +0000526
527/* Add timer event thread. */
jardin9e867fe2003-12-23 08:56:18 +0000528struct thread *
paul98c91ac2004-10-05 14:57:50 +0000529funcname_thread_add_timer (struct thread_master *m,
530 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000531 void *arg, long timer, const char* funcname)
jardin9e867fe2003-12-23 08:56:18 +0000532{
paul98c91ac2004-10-05 14:57:50 +0000533 struct timeval trel;
jardin9e867fe2003-12-23 08:56:18 +0000534
535 assert (m != NULL);
536
paul98c91ac2004-10-05 14:57:50 +0000537 trel.tv_sec += timer;
538 trel.tv_usec = 0;
539
540 return funcname_thread_add_timer_timeval (m, func, arg, &trel, funcname);
541}
542
543/* Add timer event thread with "millisecond" resolution */
544struct thread *
545funcname_thread_add_timer_msec (struct thread_master *m,
546 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000547 void *arg, long timer, const char* funcname)
paul98c91ac2004-10-05 14:57:50 +0000548{
549 struct timeval trel;
550
551 assert (m != NULL);
jardin9e867fe2003-12-23 08:56:18 +0000552
553 timer = 1000*timer; /* milli -> micro */
554
paul98c91ac2004-10-05 14:57:50 +0000555 trel.tv_sec += timer / TIMER_SECOND_MICRO;
556 trel.tv_usec += (timer % TIMER_SECOND_MICRO);
jardin9e867fe2003-12-23 08:56:18 +0000557
paul98c91ac2004-10-05 14:57:50 +0000558 return funcname_thread_add_timer_timeval (m, func, arg, &trel, funcname);
jardin9e867fe2003-12-23 08:56:18 +0000559}
560
paul718e3742002-12-13 20:15:29 +0000561/* Add simple event thread. */
562struct thread *
paule04ab742003-01-17 23:47:00 +0000563funcname_thread_add_event (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000564 int (*func) (struct thread *), void *arg, int val, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000565{
566 struct thread *thread;
567
568 assert (m != NULL);
569
paule04ab742003-01-17 23:47:00 +0000570 thread = thread_get (m, THREAD_EVENT, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000571 thread->u.val = val;
572 thread_list_add (&m->event, thread);
573
574 return thread;
575}
576
577/* Cancel thread from scheduler. */
578void
579thread_cancel (struct thread *thread)
580{
581 switch (thread->type)
582 {
583 case THREAD_READ:
584 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
585 FD_CLR (thread->u.fd, &thread->master->readfd);
586 thread_list_delete (&thread->master->read, thread);
587 break;
588 case THREAD_WRITE:
589 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
590 FD_CLR (thread->u.fd, &thread->master->writefd);
591 thread_list_delete (&thread->master->write, thread);
592 break;
593 case THREAD_TIMER:
594 thread_list_delete (&thread->master->timer, thread);
595 break;
596 case THREAD_EVENT:
597 thread_list_delete (&thread->master->event, thread);
598 break;
599 case THREAD_READY:
600 thread_list_delete (&thread->master->ready, thread);
601 break;
602 default:
603 break;
604 }
605 thread->type = THREAD_UNUSED;
606 thread_add_unuse (thread->master, thread);
607}
608
609/* Delete all events which has argument value arg. */
610void
611thread_cancel_event (struct thread_master *m, void *arg)
612{
613 struct thread *thread;
614
615 thread = m->event.head;
616 while (thread)
617 {
618 struct thread *t;
619
620 t = thread;
621 thread = t->next;
622
623 if (t->arg == arg)
624 {
625 thread_list_delete (&m->event, t);
626 t->type = THREAD_UNUSED;
627 thread_add_unuse (m, t);
628 }
629 }
630}
631
632#ifdef TIMER_NO_SORT
633struct timeval *
634thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
635{
636 struct timeval timer_now;
637 struct timeval timer_min;
638 struct timeval *timer_wait;
639
640 gettimeofday (&timer_now, NULL);
641
642 timer_wait = NULL;
643 for (thread = m->timer.head; thread; thread = thread->next)
644 {
645 if (! timer_wait)
646 timer_wait = &thread->u.sands;
647 else if (timeval_cmp (thread->u.sands, *timer_wait) < 0)
648 timer_wait = &thread->u.sands;
649 }
650
651 if (m->timer.head)
652 {
653 timer_min = *timer_wait;
654 timer_min = timeval_subtract (timer_min, timer_now);
655 if (timer_min.tv_sec < 0)
656 {
657 timer_min.tv_sec = 0;
658 timer_min.tv_usec = 10;
659 }
660 timer_wait = &timer_min;
661 }
662 else
663 timer_wait = NULL;
664
665 if (timer_wait)
666 {
667 *timer_val = timer_wait;
668 return timer_val;
669 }
670 return NULL;
671}
672#else /* ! TIMER_NO_SORT */
673struct timeval *
674thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
675{
676 struct timeval timer_now;
677 struct timeval timer_min;
678
679 if (m->timer.head)
680 {
681 gettimeofday (&timer_now, NULL);
682 timer_min = m->timer.head->u.sands;
683 timer_min = timeval_subtract (timer_min, timer_now);
684 if (timer_min.tv_sec < 0)
685 {
686 timer_min.tv_sec = 0;
687 timer_min.tv_usec = 10;
688 }
689 *timer_val = timer_min;
690 return timer_val;
691 }
692 return NULL;
693}
694#endif /* TIMER_NO_SORT */
695
696struct thread *
697thread_run (struct thread_master *m, struct thread *thread,
698 struct thread *fetch)
699{
700 *fetch = *thread;
701 thread->type = THREAD_UNUSED;
702 thread_add_unuse (m, thread);
703 return fetch;
704}
705
706int
707thread_process_fd (struct thread_master *m, struct thread_list *list,
708 fd_set *fdset, fd_set *mfdset)
709{
710 struct thread *thread;
711 struct thread *next;
712 int ready = 0;
713
714 for (thread = list->head; thread; thread = next)
715 {
716 next = thread->next;
717
718 if (FD_ISSET (THREAD_FD (thread), fdset))
719 {
720 assert (FD_ISSET (THREAD_FD (thread), mfdset));
721 FD_CLR(THREAD_FD (thread), mfdset);
722 thread_list_delete (list, thread);
723 thread_list_add (&m->ready, thread);
724 thread->type = THREAD_READY;
725 ready++;
726 }
727 }
728 return ready;
729}
730
731/* Fetch next ready thread. */
732struct thread *
733thread_fetch (struct thread_master *m, struct thread *fetch)
734{
735 int num;
736 int ready;
737 struct thread *thread;
738 fd_set readfd;
739 fd_set writefd;
740 fd_set exceptfd;
741 struct timeval timer_now;
742 struct timeval timer_val;
743 struct timeval *timer_wait;
744 struct timeval timer_nowait;
745
746 timer_nowait.tv_sec = 0;
747 timer_nowait.tv_usec = 0;
748
749 while (1)
750 {
paul05c447d2004-07-22 19:14:27 +0000751 /* Signals are highest priority */
752 quagga_sigevent_process ();
753
754 /* Normal event are the next highest priority. */
paul718e3742002-12-13 20:15:29 +0000755 if ((thread = thread_trim_head (&m->event)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000756 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000757
758 /* Execute timer. */
759 gettimeofday (&timer_now, NULL);
760
761 for (thread = m->timer.head; thread; thread = thread->next)
paul05c447d2004-07-22 19:14:27 +0000762 if (timeval_cmp (timer_now, thread->u.sands) >= 0)
763 {
764 thread_list_delete (&m->timer, thread);
765 return thread_run (m, thread, fetch);
766 }
paul718e3742002-12-13 20:15:29 +0000767
768 /* If there are any ready threads, process top of them. */
769 if ((thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000770 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000771
772 /* Structure copy. */
773 readfd = m->readfd;
774 writefd = m->writefd;
775 exceptfd = m->exceptfd;
776
777 /* Calculate select wait timer. */
778 timer_wait = thread_timer_wait (m, &timer_val);
779
780 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
781
782 if (num == 0)
paul05c447d2004-07-22 19:14:27 +0000783 continue;
paul718e3742002-12-13 20:15:29 +0000784
785 if (num < 0)
paul05c447d2004-07-22 19:14:27 +0000786 {
787 if (errno == EINTR)
788 {
789 /* signal received */
790 quagga_sigevent_process ();
791 continue;
792 }
paul718e3742002-12-13 20:15:29 +0000793
paul05c447d2004-07-22 19:14:27 +0000794 zlog_warn ("select() error: %s", strerror (errno));
795 return NULL;
796 }
paul718e3742002-12-13 20:15:29 +0000797
798 /* Normal priority read thead. */
799 ready = thread_process_fd (m, &m->read, &readfd, &m->readfd);
800
801 /* Write thead. */
802 ready = thread_process_fd (m, &m->write, &writefd, &m->writefd);
803
804 if ((thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000805 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000806 }
807}
808
809static unsigned long
810thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
811{
812 unsigned long thread_time;
813
814#ifdef HAVE_RUSAGE
815 /* This is 'user + sys' time. */
816 thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
817 thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
818#else
819 /* When rusage is not available, simple elapsed time is used. */
820 thread_time = timeval_elapsed (*now, *start);
821#endif /* HAVE_RUSAGE */
822
823 return thread_time;
824}
825
826/* We should aim to yield after THREAD_YIELD_TIME_SLOT
827 milliseconds. */
828int
829thread_should_yield (struct thread *thread)
830{
831 RUSAGE_T ru;
832
833 GETRUSAGE (&ru);
834
835 if (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT)
836 return 1;
837 else
838 return 0;
839}
840
841/* We check thread consumed time. If the system has getrusage, we'll
842 use that to get indepth stats on the performance of the thread. If
843 not - we'll use gettimeofday for some guestimation. */
844void
845thread_call (struct thread *thread)
846{
847 unsigned long thread_time;
848 RUSAGE_T ru;
paule04ab742003-01-17 23:47:00 +0000849 struct cpu_thread_history tmp, *cpu;
850
851 tmp.func = thread->func;
852 tmp.funcname = thread->funcname;
853 cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc);
paul718e3742002-12-13 20:15:29 +0000854
855 GETRUSAGE (&thread->ru);
856
857 (*thread->func) (thread);
858
859 GETRUSAGE (&ru);
860
861 thread_time = thread_consumed_time (&ru, &thread->ru);
paule04ab742003-01-17 23:47:00 +0000862 cpu->total += thread_time;
863 if (cpu->max < thread_time)
864 cpu->max = thread_time;
865
866 ++cpu->total_calls;
867 cpu->types |= (1 << thread->add_type);
paul718e3742002-12-13 20:15:29 +0000868
869#ifdef THREAD_CONSUMED_TIME_CHECK
870 if (thread_time > 200000L)
871 {
872 /*
873 * We have a CPU Hog on our hands.
874 * Whinge about it now, so we're aware this is yet another task
875 * to fix.
876 */
paule04ab742003-01-17 23:47:00 +0000877 zlog_err ("CPU HOG task %s (%lx) ran for %ldms",
878 thread->funcname,
paul718e3742002-12-13 20:15:29 +0000879 (unsigned long) thread->func,
880 thread_time / 1000L);
881 }
882#endif /* THREAD_CONSUMED_TIME_CHECK */
883}
884
885/* Execute thread */
886struct thread *
paule04ab742003-01-17 23:47:00 +0000887funcname_thread_execute (struct thread_master *m,
paul718e3742002-12-13 20:15:29 +0000888 int (*func)(struct thread *),
889 void *arg,
paule04ab742003-01-17 23:47:00 +0000890 int val,
hasso8c328f12004-10-05 21:01:23 +0000891 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000892{
893 struct thread dummy;
894
895 memset (&dummy, 0, sizeof (struct thread));
896
897 dummy.type = THREAD_EVENT;
paule04ab742003-01-17 23:47:00 +0000898 dummy.add_type = THREAD_EXECUTE;
paul718e3742002-12-13 20:15:29 +0000899 dummy.master = NULL;
900 dummy.func = func;
901 dummy.arg = arg;
902 dummy.u.val = val;
paule04ab742003-01-17 23:47:00 +0000903 dummy.funcname = strip_funcname (funcname);
paul718e3742002-12-13 20:15:29 +0000904 thread_call (&dummy);
905
paul2946f652003-03-27 23:48:24 +0000906 XFREE (MTYPE_STRVEC, dummy.funcname);
907
paul718e3742002-12-13 20:15:29 +0000908 return NULL;
909}