blob: 94fdf1f52f2d8916819323871d1054b14ff4b0ce [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;
paul9d11a192004-10-31 16:19:24 +0000107 new = XMALLOC( MTYPE_THREAD_STATS, sizeof *new);
108 memset(new, 0, sizeof (struct cpu_thread_history));
paule04ab742003-01-17 23:47:00 +0000109 new->func = a->func;
paul9d11a192004-10-31 16:19:24 +0000110 new->funcname = XSTRDUP(MTYPE_THREAD_FUNCNAME, a->funcname);
paule04ab742003-01-17 23:47:00 +0000111 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);
paul9d11a192004-10-31 16:19:24 +0000320 /* XXX: Should we deallocate funcname here? */
paul718e3742002-12-13 20:15:29 +0000321}
322
323/* Free all unused thread. */
324static void
325thread_list_free (struct thread_master *m, struct thread_list *list)
326{
327 struct thread *t;
328 struct thread *next;
329
330 for (t = list->head; t; t = next)
331 {
332 next = t->next;
paul9d11a192004-10-31 16:19:24 +0000333 XFREE (MTYPE_THREAD_FUNCNAME, t->funcname);
paul718e3742002-12-13 20:15:29 +0000334 XFREE (MTYPE_THREAD, t);
335 list->count--;
336 m->alloc--;
337 }
338}
339
340/* Stop thread scheduler. */
341void
342thread_master_free (struct thread_master *m)
343{
344 thread_list_free (m, &m->read);
345 thread_list_free (m, &m->write);
346 thread_list_free (m, &m->timer);
347 thread_list_free (m, &m->event);
348 thread_list_free (m, &m->ready);
349 thread_list_free (m, &m->unuse);
350
351 XFREE (MTYPE_THREAD_MASTER, m);
352}
353
354/* Delete top of the list and return it. */
355static struct thread *
356thread_trim_head (struct thread_list *list)
357{
358 if (list->head)
359 return thread_list_delete (list, list->head);
360 return NULL;
361}
362
363/* Thread list is empty or not. */
364int
365thread_empty (struct thread_list *list)
366{
367 return list->head ? 0 : 1;
368}
369
370/* Return remain time in second. */
371unsigned long
372thread_timer_remain_second (struct thread *thread)
373{
374 struct timeval timer_now;
375
376 gettimeofday (&timer_now, NULL);
377
378 if (thread->u.sands.tv_sec - timer_now.tv_sec > 0)
379 return thread->u.sands.tv_sec - timer_now.tv_sec;
380 else
381 return 0;
382}
383
paule04ab742003-01-17 23:47:00 +0000384/* Trim blankspace and "()"s */
385static char *
hasso8c328f12004-10-05 21:01:23 +0000386strip_funcname (const char *funcname)
paule04ab742003-01-17 23:47:00 +0000387{
388 char buff[100];
389 char tmp, *ret, *e, *b = buff;
390
391 strncpy(buff, funcname, sizeof(buff));
392 buff[ sizeof(buff) -1] = '\0';
393 e = buff +strlen(buff) -1;
394
395 /* Wont work for funcname == "Word (explanation)" */
396
397 while (*b == ' ' || *b == '(')
398 ++b;
399 while (*e == ' ' || *e == ')')
400 --e;
401 e++;
402
403 tmp = *e;
404 *e = '\0';
paul9d11a192004-10-31 16:19:24 +0000405 ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b);
paule04ab742003-01-17 23:47:00 +0000406 *e = tmp;
407
408 return ret;
409}
410
paul718e3742002-12-13 20:15:29 +0000411/* Get new thread. */
412static struct thread *
413thread_get (struct thread_master *m, u_char type,
hasso8c328f12004-10-05 21:01:23 +0000414 int (*func) (struct thread *), void *arg, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000415{
416 struct thread *thread;
417
418 if (m->unuse.head)
paul2946f652003-03-27 23:48:24 +0000419 {
420 thread = thread_trim_head (&m->unuse);
421 if (thread->funcname)
paul9d11a192004-10-31 16:19:24 +0000422 XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname);
paul2946f652003-03-27 23:48:24 +0000423 }
paul718e3742002-12-13 20:15:29 +0000424 else
425 {
426 thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
427 m->alloc++;
428 }
429 thread->type = type;
paule04ab742003-01-17 23:47:00 +0000430 thread->add_type = type;
paul718e3742002-12-13 20:15:29 +0000431 thread->master = m;
432 thread->func = func;
433 thread->arg = arg;
434
paule04ab742003-01-17 23:47:00 +0000435 thread->funcname = strip_funcname(funcname);
436
paul718e3742002-12-13 20:15:29 +0000437 return thread;
438}
439
440/* Add new read thread. */
441struct thread *
paule04ab742003-01-17 23:47:00 +0000442funcname_thread_add_read (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000443 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000444{
445 struct thread *thread;
446
447 assert (m != NULL);
448
449 if (FD_ISSET (fd, &m->readfd))
450 {
451 zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
452 return NULL;
453 }
454
paule04ab742003-01-17 23:47:00 +0000455 thread = thread_get (m, THREAD_READ, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000456 FD_SET (fd, &m->readfd);
457 thread->u.fd = fd;
458 thread_list_add (&m->read, thread);
459
460 return thread;
461}
462
463/* Add new write thread. */
464struct thread *
paule04ab742003-01-17 23:47:00 +0000465funcname_thread_add_write (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000466 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000467{
468 struct thread *thread;
469
470 assert (m != NULL);
471
472 if (FD_ISSET (fd, &m->writefd))
473 {
474 zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
475 return NULL;
476 }
477
paule04ab742003-01-17 23:47:00 +0000478 thread = thread_get (m, THREAD_WRITE, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000479 FD_SET (fd, &m->writefd);
480 thread->u.fd = fd;
481 thread_list_add (&m->write, thread);
482
483 return thread;
484}
485
paul98c91ac2004-10-05 14:57:50 +0000486static struct thread *
487funcname_thread_add_timer_timeval (struct thread_master *m,
488 int (*func) (struct thread *),
489 void *arg,
490 struct timeval *time_relative,
hasso8c328f12004-10-05 21:01:23 +0000491 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000492{
paul718e3742002-12-13 20:15:29 +0000493 struct thread *thread;
paul98c91ac2004-10-05 14:57:50 +0000494 struct timeval timer_now;
paul718e3742002-12-13 20:15:29 +0000495#ifndef TIMER_NO_SORT
496 struct thread *tt;
497#endif /* TIMER_NO_SORT */
498
499 assert (m != NULL);
500
paule04ab742003-01-17 23:47:00 +0000501 thread = thread_get (m, THREAD_TIMER, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000502
503 /* Do we need jitter here? */
504 gettimeofday (&timer_now, NULL);
paul98c91ac2004-10-05 14:57:50 +0000505 timer_now.tv_sec += time_relative->tv_sec;
506 timer_now.tv_usec += time_relative->tv_usec;
507 timeval_adjust (timer_now);
paul718e3742002-12-13 20:15:29 +0000508 thread->u.sands = timer_now;
509
510 /* Sort by timeval. */
511#ifdef TIMER_NO_SORT
512 thread_list_add (&m->timer, thread);
513#else
514 for (tt = m->timer.head; tt; tt = tt->next)
515 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
516 break;
517
518 if (tt)
519 thread_list_add_before (&m->timer, tt, thread);
520 else
521 thread_list_add (&m->timer, thread);
522#endif /* TIMER_NO_SORT */
523
524 return thread;
525}
526
paul98c91ac2004-10-05 14:57:50 +0000527
528/* Add timer event thread. */
jardin9e867fe2003-12-23 08:56:18 +0000529struct thread *
paul98c91ac2004-10-05 14:57:50 +0000530funcname_thread_add_timer (struct thread_master *m,
531 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000532 void *arg, long timer, const char* funcname)
jardin9e867fe2003-12-23 08:56:18 +0000533{
paul98c91ac2004-10-05 14:57:50 +0000534 struct timeval trel;
jardin9e867fe2003-12-23 08:56:18 +0000535
536 assert (m != NULL);
537
paul9076fbd2004-10-11 09:40:58 +0000538 trel.tv_sec = timer;
paul98c91ac2004-10-05 14:57:50 +0000539 trel.tv_usec = 0;
540
541 return funcname_thread_add_timer_timeval (m, func, arg, &trel, funcname);
542}
543
544/* Add timer event thread with "millisecond" resolution */
545struct thread *
546funcname_thread_add_timer_msec (struct thread_master *m,
547 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000548 void *arg, long timer, const char* funcname)
paul98c91ac2004-10-05 14:57:50 +0000549{
550 struct timeval trel;
551
552 assert (m != NULL);
jardin9e867fe2003-12-23 08:56:18 +0000553
554 timer = 1000*timer; /* milli -> micro */
555
paul9076fbd2004-10-11 09:40:58 +0000556 trel.tv_sec = timer / TIMER_SECOND_MICRO;
557 trel.tv_usec = (timer % TIMER_SECOND_MICRO);
jardin9e867fe2003-12-23 08:56:18 +0000558
paul98c91ac2004-10-05 14:57:50 +0000559 return funcname_thread_add_timer_timeval (m, func, arg, &trel, funcname);
jardin9e867fe2003-12-23 08:56:18 +0000560}
561
paul718e3742002-12-13 20:15:29 +0000562/* Add simple event thread. */
563struct thread *
paule04ab742003-01-17 23:47:00 +0000564funcname_thread_add_event (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000565 int (*func) (struct thread *), void *arg, int val, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000566{
567 struct thread *thread;
568
569 assert (m != NULL);
570
paule04ab742003-01-17 23:47:00 +0000571 thread = thread_get (m, THREAD_EVENT, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000572 thread->u.val = val;
573 thread_list_add (&m->event, thread);
574
575 return thread;
576}
577
578/* Cancel thread from scheduler. */
579void
580thread_cancel (struct thread *thread)
581{
582 switch (thread->type)
583 {
584 case THREAD_READ:
585 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
586 FD_CLR (thread->u.fd, &thread->master->readfd);
587 thread_list_delete (&thread->master->read, thread);
588 break;
589 case THREAD_WRITE:
590 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
591 FD_CLR (thread->u.fd, &thread->master->writefd);
592 thread_list_delete (&thread->master->write, thread);
593 break;
594 case THREAD_TIMER:
595 thread_list_delete (&thread->master->timer, thread);
596 break;
597 case THREAD_EVENT:
598 thread_list_delete (&thread->master->event, thread);
599 break;
600 case THREAD_READY:
601 thread_list_delete (&thread->master->ready, thread);
602 break;
603 default:
604 break;
605 }
606 thread->type = THREAD_UNUSED;
607 thread_add_unuse (thread->master, thread);
608}
609
610/* Delete all events which has argument value arg. */
611void
612thread_cancel_event (struct thread_master *m, void *arg)
613{
614 struct thread *thread;
615
616 thread = m->event.head;
617 while (thread)
618 {
619 struct thread *t;
620
621 t = thread;
622 thread = t->next;
623
624 if (t->arg == arg)
625 {
626 thread_list_delete (&m->event, t);
627 t->type = THREAD_UNUSED;
628 thread_add_unuse (m, t);
629 }
630 }
631}
632
633#ifdef TIMER_NO_SORT
634struct timeval *
635thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
636{
637 struct timeval timer_now;
638 struct timeval timer_min;
639 struct timeval *timer_wait;
640
641 gettimeofday (&timer_now, NULL);
642
643 timer_wait = NULL;
644 for (thread = m->timer.head; thread; thread = thread->next)
645 {
646 if (! timer_wait)
647 timer_wait = &thread->u.sands;
648 else if (timeval_cmp (thread->u.sands, *timer_wait) < 0)
649 timer_wait = &thread->u.sands;
650 }
651
652 if (m->timer.head)
653 {
654 timer_min = *timer_wait;
655 timer_min = timeval_subtract (timer_min, timer_now);
656 if (timer_min.tv_sec < 0)
657 {
658 timer_min.tv_sec = 0;
659 timer_min.tv_usec = 10;
660 }
661 timer_wait = &timer_min;
662 }
663 else
664 timer_wait = NULL;
665
666 if (timer_wait)
667 {
668 *timer_val = timer_wait;
669 return timer_val;
670 }
671 return NULL;
672}
673#else /* ! TIMER_NO_SORT */
674struct timeval *
675thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
676{
677 struct timeval timer_now;
678 struct timeval timer_min;
679
680 if (m->timer.head)
681 {
682 gettimeofday (&timer_now, NULL);
683 timer_min = m->timer.head->u.sands;
684 timer_min = timeval_subtract (timer_min, timer_now);
685 if (timer_min.tv_sec < 0)
686 {
687 timer_min.tv_sec = 0;
688 timer_min.tv_usec = 10;
689 }
690 *timer_val = timer_min;
691 return timer_val;
692 }
693 return NULL;
694}
695#endif /* TIMER_NO_SORT */
696
697struct thread *
698thread_run (struct thread_master *m, struct thread *thread,
699 struct thread *fetch)
700{
701 *fetch = *thread;
702 thread->type = THREAD_UNUSED;
703 thread_add_unuse (m, thread);
704 return fetch;
705}
706
707int
708thread_process_fd (struct thread_master *m, struct thread_list *list,
709 fd_set *fdset, fd_set *mfdset)
710{
711 struct thread *thread;
712 struct thread *next;
713 int ready = 0;
714
715 for (thread = list->head; thread; thread = next)
716 {
717 next = thread->next;
718
719 if (FD_ISSET (THREAD_FD (thread), fdset))
720 {
721 assert (FD_ISSET (THREAD_FD (thread), mfdset));
722 FD_CLR(THREAD_FD (thread), mfdset);
723 thread_list_delete (list, thread);
724 thread_list_add (&m->ready, thread);
725 thread->type = THREAD_READY;
726 ready++;
727 }
728 }
729 return ready;
730}
731
732/* Fetch next ready thread. */
733struct thread *
734thread_fetch (struct thread_master *m, struct thread *fetch)
735{
736 int num;
737 int ready;
738 struct thread *thread;
739 fd_set readfd;
740 fd_set writefd;
741 fd_set exceptfd;
742 struct timeval timer_now;
743 struct timeval timer_val;
744 struct timeval *timer_wait;
745 struct timeval timer_nowait;
746
747 timer_nowait.tv_sec = 0;
748 timer_nowait.tv_usec = 0;
749
750 while (1)
751 {
paul05c447d2004-07-22 19:14:27 +0000752 /* Signals are highest priority */
753 quagga_sigevent_process ();
754
755 /* Normal event are the next highest priority. */
paul718e3742002-12-13 20:15:29 +0000756 if ((thread = thread_trim_head (&m->event)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000757 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000758
759 /* Execute timer. */
760 gettimeofday (&timer_now, NULL);
761
762 for (thread = m->timer.head; thread; thread = thread->next)
paul05c447d2004-07-22 19:14:27 +0000763 if (timeval_cmp (timer_now, thread->u.sands) >= 0)
764 {
765 thread_list_delete (&m->timer, thread);
766 return thread_run (m, thread, fetch);
767 }
paul718e3742002-12-13 20:15:29 +0000768
769 /* If there are any ready threads, process top of them. */
770 if ((thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000771 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000772
773 /* Structure copy. */
774 readfd = m->readfd;
775 writefd = m->writefd;
776 exceptfd = m->exceptfd;
777
778 /* Calculate select wait timer. */
779 timer_wait = thread_timer_wait (m, &timer_val);
780
781 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
782
783 if (num == 0)
paul05c447d2004-07-22 19:14:27 +0000784 continue;
paul718e3742002-12-13 20:15:29 +0000785
786 if (num < 0)
paul05c447d2004-07-22 19:14:27 +0000787 {
788 if (errno == EINTR)
789 {
790 /* signal received */
791 quagga_sigevent_process ();
792 continue;
793 }
paul718e3742002-12-13 20:15:29 +0000794
paul05c447d2004-07-22 19:14:27 +0000795 zlog_warn ("select() error: %s", strerror (errno));
796 return NULL;
797 }
paul718e3742002-12-13 20:15:29 +0000798
799 /* Normal priority read thead. */
800 ready = thread_process_fd (m, &m->read, &readfd, &m->readfd);
801
802 /* Write thead. */
803 ready = thread_process_fd (m, &m->write, &writefd, &m->writefd);
804
805 if ((thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000806 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000807 }
808}
809
810static unsigned long
811thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
812{
813 unsigned long thread_time;
814
815#ifdef HAVE_RUSAGE
816 /* This is 'user + sys' time. */
817 thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
818 thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
819#else
820 /* When rusage is not available, simple elapsed time is used. */
821 thread_time = timeval_elapsed (*now, *start);
822#endif /* HAVE_RUSAGE */
823
824 return thread_time;
825}
826
827/* We should aim to yield after THREAD_YIELD_TIME_SLOT
828 milliseconds. */
829int
830thread_should_yield (struct thread *thread)
831{
832 RUSAGE_T ru;
833
834 GETRUSAGE (&ru);
835
836 if (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT)
837 return 1;
838 else
839 return 0;
840}
841
842/* We check thread consumed time. If the system has getrusage, we'll
843 use that to get indepth stats on the performance of the thread. If
844 not - we'll use gettimeofday for some guestimation. */
845void
846thread_call (struct thread *thread)
847{
848 unsigned long thread_time;
849 RUSAGE_T ru;
paule04ab742003-01-17 23:47:00 +0000850 struct cpu_thread_history tmp, *cpu;
851
852 tmp.func = thread->func;
853 tmp.funcname = thread->funcname;
854 cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc);
paul718e3742002-12-13 20:15:29 +0000855
856 GETRUSAGE (&thread->ru);
857
858 (*thread->func) (thread);
859
860 GETRUSAGE (&ru);
861
862 thread_time = thread_consumed_time (&ru, &thread->ru);
paule04ab742003-01-17 23:47:00 +0000863 cpu->total += thread_time;
864 if (cpu->max < thread_time)
865 cpu->max = thread_time;
866
867 ++cpu->total_calls;
868 cpu->types |= (1 << thread->add_type);
paul718e3742002-12-13 20:15:29 +0000869
870#ifdef THREAD_CONSUMED_TIME_CHECK
871 if (thread_time > 200000L)
872 {
873 /*
874 * We have a CPU Hog on our hands.
875 * Whinge about it now, so we're aware this is yet another task
876 * to fix.
877 */
paule04ab742003-01-17 23:47:00 +0000878 zlog_err ("CPU HOG task %s (%lx) ran for %ldms",
879 thread->funcname,
paul718e3742002-12-13 20:15:29 +0000880 (unsigned long) thread->func,
881 thread_time / 1000L);
882 }
883#endif /* THREAD_CONSUMED_TIME_CHECK */
884}
885
886/* Execute thread */
887struct thread *
paule04ab742003-01-17 23:47:00 +0000888funcname_thread_execute (struct thread_master *m,
paul718e3742002-12-13 20:15:29 +0000889 int (*func)(struct thread *),
890 void *arg,
paule04ab742003-01-17 23:47:00 +0000891 int val,
hasso8c328f12004-10-05 21:01:23 +0000892 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000893{
894 struct thread dummy;
895
896 memset (&dummy, 0, sizeof (struct thread));
897
898 dummy.type = THREAD_EVENT;
paule04ab742003-01-17 23:47:00 +0000899 dummy.add_type = THREAD_EXECUTE;
paul718e3742002-12-13 20:15:29 +0000900 dummy.master = NULL;
901 dummy.func = func;
902 dummy.arg = arg;
903 dummy.u.val = val;
paule04ab742003-01-17 23:47:00 +0000904 dummy.funcname = strip_funcname (funcname);
paul718e3742002-12-13 20:15:29 +0000905 thread_call (&dummy);
906
paul9d11a192004-10-31 16:19:24 +0000907 XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname);
paul2946f652003-03-27 23:48:24 +0000908
paul718e3742002-12-13 20:15:29 +0000909 return NULL;
910}