blob: 333312424c683ba5131a7e450ebe41c90729dc61 [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#ifndef TIMER_NO_SORT
505 struct thread *tt;
506#endif /* TIMER_NO_SORT */
507
508 assert (m != NULL);
509
paula48b4e62005-04-22 00:43:47 +0000510 assert (type == THREAD_TIMER || THREAD_BACKGROUND);
511 assert (time_relative);
512
513 switch (type)
514 {
515 case THREAD_TIMER:
516 list = &m->timer;
517 break;
518 case THREAD_BACKGROUND:
519 list = &m->background;
520 break;
521 default:
522 return NULL;
523 }
524
525 thread = thread_get (m, type, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000526
527 /* Do we need jitter here? */
528 gettimeofday (&timer_now, NULL);
paul98c91ac2004-10-05 14:57:50 +0000529 timer_now.tv_sec += time_relative->tv_sec;
530 timer_now.tv_usec += time_relative->tv_usec;
531 timeval_adjust (timer_now);
paul718e3742002-12-13 20:15:29 +0000532 thread->u.sands = timer_now;
533
534 /* Sort by timeval. */
535#ifdef TIMER_NO_SORT
paula48b4e62005-04-22 00:43:47 +0000536 thread_list_add (list, thread);
paul718e3742002-12-13 20:15:29 +0000537#else
paula48b4e62005-04-22 00:43:47 +0000538 for (tt = list->head; tt; tt = tt->next)
paul718e3742002-12-13 20:15:29 +0000539 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
540 break;
541
542 if (tt)
paula48b4e62005-04-22 00:43:47 +0000543 thread_list_add_before (list, tt, thread);
paul718e3742002-12-13 20:15:29 +0000544 else
paula48b4e62005-04-22 00:43:47 +0000545 thread_list_add (list, thread);
paul718e3742002-12-13 20:15:29 +0000546#endif /* TIMER_NO_SORT */
547
548 return thread;
549}
550
paul98c91ac2004-10-05 14:57:50 +0000551
552/* Add timer event thread. */
jardin9e867fe2003-12-23 08:56:18 +0000553struct thread *
paul98c91ac2004-10-05 14:57:50 +0000554funcname_thread_add_timer (struct thread_master *m,
555 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000556 void *arg, long timer, const char* funcname)
jardin9e867fe2003-12-23 08:56:18 +0000557{
paul98c91ac2004-10-05 14:57:50 +0000558 struct timeval trel;
jardin9e867fe2003-12-23 08:56:18 +0000559
560 assert (m != NULL);
561
paul9076fbd2004-10-11 09:40:58 +0000562 trel.tv_sec = timer;
paul98c91ac2004-10-05 14:57:50 +0000563 trel.tv_usec = 0;
564
paula48b4e62005-04-22 00:43:47 +0000565 return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg,
566 &trel, funcname);
paul98c91ac2004-10-05 14:57:50 +0000567}
568
569/* Add timer event thread with "millisecond" resolution */
570struct thread *
571funcname_thread_add_timer_msec (struct thread_master *m,
572 int (*func) (struct thread *),
hasso8c328f12004-10-05 21:01:23 +0000573 void *arg, long timer, const char* funcname)
paul98c91ac2004-10-05 14:57:50 +0000574{
575 struct timeval trel;
576
577 assert (m != NULL);
jardin9e867fe2003-12-23 08:56:18 +0000578
ajsaf04bd72004-12-28 17:00:12 +0000579 trel.tv_sec = timer / 1000;
580 trel.tv_usec = 1000*(timer % 1000);
jardin9e867fe2003-12-23 08:56:18 +0000581
paula48b4e62005-04-22 00:43:47 +0000582 return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER,
583 arg, &trel, funcname);
584}
585
586/* Add a background thread, with an optional millisec delay */
587struct thread *
588funcname_thread_add_background (struct thread_master *m,
589 int (*func) (struct thread *),
590 void *arg, long delay,
591 const char *funcname)
592{
593 struct timeval trel;
594
595 assert (m != NULL);
596
597 if (delay)
598 {
599 trel.tv_sec = delay / 1000;
600 trel.tv_usec = 1000*(delay % 1000);
601 }
602 else
603 {
604 trel.tv_sec = 0;
605 trel.tv_usec = 0;
606 }
607
608 return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND,
609 arg, &trel, funcname);
jardin9e867fe2003-12-23 08:56:18 +0000610}
611
paul718e3742002-12-13 20:15:29 +0000612/* Add simple event thread. */
613struct thread *
paule04ab742003-01-17 23:47:00 +0000614funcname_thread_add_event (struct thread_master *m,
hasso8c328f12004-10-05 21:01:23 +0000615 int (*func) (struct thread *), void *arg, int val, const char* funcname)
paul718e3742002-12-13 20:15:29 +0000616{
617 struct thread *thread;
618
619 assert (m != NULL);
620
paule04ab742003-01-17 23:47:00 +0000621 thread = thread_get (m, THREAD_EVENT, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000622 thread->u.val = val;
623 thread_list_add (&m->event, thread);
624
625 return thread;
626}
627
628/* Cancel thread from scheduler. */
629void
630thread_cancel (struct thread *thread)
631{
paula48b4e62005-04-22 00:43:47 +0000632 struct thread_list *list;
633
paul718e3742002-12-13 20:15:29 +0000634 switch (thread->type)
635 {
636 case THREAD_READ:
637 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
638 FD_CLR (thread->u.fd, &thread->master->readfd);
paula48b4e62005-04-22 00:43:47 +0000639 list = &thread->master->read;
paul718e3742002-12-13 20:15:29 +0000640 break;
641 case THREAD_WRITE:
642 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
643 FD_CLR (thread->u.fd, &thread->master->writefd);
paula48b4e62005-04-22 00:43:47 +0000644 list = &thread->master->write;
paul718e3742002-12-13 20:15:29 +0000645 break;
646 case THREAD_TIMER:
paula48b4e62005-04-22 00:43:47 +0000647 list = &thread->master->timer;
paul718e3742002-12-13 20:15:29 +0000648 break;
649 case THREAD_EVENT:
paula48b4e62005-04-22 00:43:47 +0000650 list = &thread->master->event;
paul718e3742002-12-13 20:15:29 +0000651 break;
652 case THREAD_READY:
paula48b4e62005-04-22 00:43:47 +0000653 list = &thread->master->ready;
paul718e3742002-12-13 20:15:29 +0000654 break;
paula48b4e62005-04-22 00:43:47 +0000655 case THREAD_BACKGROUND:
656 list = &thread->master->background;
paul718e3742002-12-13 20:15:29 +0000657 default:
paula48b4e62005-04-22 00:43:47 +0000658 return;
paul718e3742002-12-13 20:15:29 +0000659 break;
660 }
paula48b4e62005-04-22 00:43:47 +0000661 thread_list_delete (list, thread);
paul718e3742002-12-13 20:15:29 +0000662 thread->type = THREAD_UNUSED;
663 thread_add_unuse (thread->master, thread);
664}
665
666/* Delete all events which has argument value arg. */
667void
668thread_cancel_event (struct thread_master *m, void *arg)
669{
670 struct thread *thread;
671
672 thread = m->event.head;
673 while (thread)
674 {
675 struct thread *t;
676
677 t = thread;
678 thread = t->next;
679
680 if (t->arg == arg)
paula48b4e62005-04-22 00:43:47 +0000681 {
682 thread_list_delete (&m->event, t);
683 t->type = THREAD_UNUSED;
684 thread_add_unuse (m, t);
685 }
paul718e3742002-12-13 20:15:29 +0000686 }
687}
688
689#ifdef TIMER_NO_SORT
paula48b4e62005-04-22 00:43:47 +0000690static struct timeval *
691thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val)
paul718e3742002-12-13 20:15:29 +0000692{
693 struct timeval timer_now;
694 struct timeval timer_min;
695 struct timeval *timer_wait;
696
697 gettimeofday (&timer_now, NULL);
698
699 timer_wait = NULL;
paula48b4e62005-04-22 00:43:47 +0000700 for (thread = tlist->head; thread; thread = thread->next)
paul718e3742002-12-13 20:15:29 +0000701 {
702 if (! timer_wait)
703 timer_wait = &thread->u.sands;
704 else if (timeval_cmp (thread->u.sands, *timer_wait) < 0)
705 timer_wait = &thread->u.sands;
706 }
707
paula48b4e62005-04-22 00:43:47 +0000708 if (tlist->head)
paul718e3742002-12-13 20:15:29 +0000709 {
710 timer_min = *timer_wait;
711 timer_min = timeval_subtract (timer_min, timer_now);
712 if (timer_min.tv_sec < 0)
713 {
714 timer_min.tv_sec = 0;
715 timer_min.tv_usec = 10;
716 }
717 timer_wait = &timer_min;
718 }
719 else
720 timer_wait = NULL;
721
722 if (timer_wait)
723 {
724 *timer_val = timer_wait;
725 return timer_val;
726 }
727 return NULL;
728}
729#else /* ! TIMER_NO_SORT */
paula48b4e62005-04-22 00:43:47 +0000730static struct timeval *
731thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val)
paul718e3742002-12-13 20:15:29 +0000732{
733 struct timeval timer_now;
734 struct timeval timer_min;
735
paula48b4e62005-04-22 00:43:47 +0000736 if (tlist->head)
paul718e3742002-12-13 20:15:29 +0000737 {
738 gettimeofday (&timer_now, NULL);
paula48b4e62005-04-22 00:43:47 +0000739 timer_min = tlist->head->u.sands;
paul718e3742002-12-13 20:15:29 +0000740 timer_min = timeval_subtract (timer_min, timer_now);
741 if (timer_min.tv_sec < 0)
742 {
743 timer_min.tv_sec = 0;
744 timer_min.tv_usec = 10;
745 }
746 *timer_val = timer_min;
747 return timer_val;
748 }
749 return NULL;
750}
751#endif /* TIMER_NO_SORT */
752
753struct thread *
754thread_run (struct thread_master *m, struct thread *thread,
755 struct thread *fetch)
756{
757 *fetch = *thread;
758 thread->type = THREAD_UNUSED;
759 thread_add_unuse (m, thread);
760 return fetch;
761}
762
paula48b4e62005-04-22 00:43:47 +0000763static int
764thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset)
paul718e3742002-12-13 20:15:29 +0000765{
766 struct thread *thread;
767 struct thread *next;
768 int ready = 0;
paula48b4e62005-04-22 00:43:47 +0000769
770 assert (list);
771
paul718e3742002-12-13 20:15:29 +0000772 for (thread = list->head; thread; thread = next)
773 {
774 next = thread->next;
775
776 if (FD_ISSET (THREAD_FD (thread), fdset))
paula48b4e62005-04-22 00:43:47 +0000777 {
778 assert (FD_ISSET (THREAD_FD (thread), mfdset));
779 FD_CLR(THREAD_FD (thread), mfdset);
780 thread_list_delete (list, thread);
781 thread_list_add (&thread->master->ready, thread);
782 thread->type = THREAD_READY;
783 ready++;
784 }
paul718e3742002-12-13 20:15:29 +0000785 }
786 return ready;
787}
788
paula48b4e62005-04-22 00:43:47 +0000789/* fetch next timer-like thread thread from the list and return it */
790static unsigned int
791thread_timer_process (struct thread_list *list, struct timeval *timenow)
792{
793 struct thread *thread;
794 unsigned int ready = 0;
795
796 assert (list && timenow);
797
798 for (thread = list->head; thread; thread = thread->next)
799 if (timeval_cmp (*timenow, thread->u.sands) >= 0)
800 {
801 thread_list_delete (list, thread);
802 assert (thread->next == thread->prev && thread->next == NULL);
803 thread->next = thread->prev = NULL;
804 thread->type = THREAD_READY;
805 thread_list_add (&thread->master->ready, thread);
806 ready++;
807 }
808 return ready;
809}
810
paul718e3742002-12-13 20:15:29 +0000811/* Fetch next ready thread. */
812struct thread *
813thread_fetch (struct thread_master *m, struct thread *fetch)
814{
paul718e3742002-12-13 20:15:29 +0000815 struct thread *thread;
816 fd_set readfd;
817 fd_set writefd;
818 fd_set exceptfd;
819 struct timeval timer_now;
820 struct timeval timer_val;
paula48b4e62005-04-22 00:43:47 +0000821 struct timeval timer_val_bg;
paul718e3742002-12-13 20:15:29 +0000822 struct timeval *timer_wait;
paula48b4e62005-04-22 00:43:47 +0000823 struct timeval *timer_wait_bg;
paul718e3742002-12-13 20:15:29 +0000824
825 while (1)
826 {
paula48b4e62005-04-22 00:43:47 +0000827 int num = 0;
828 int ready = m->ready.count;
829
paul05c447d2004-07-22 19:14:27 +0000830 /* Signals are highest priority */
831 quagga_sigevent_process ();
832
833 /* Normal event are the next highest priority. */
paul718e3742002-12-13 20:15:29 +0000834 if ((thread = thread_trim_head (&m->event)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000835 return thread_run (m, thread, fetch);
paula48b4e62005-04-22 00:43:47 +0000836
paul718e3742002-12-13 20:15:29 +0000837 /* Execute timer. */
838 gettimeofday (&timer_now, NULL);
paula48b4e62005-04-22 00:43:47 +0000839
840 /* Timer threads */
841 ready += thread_timer_process (&m->timer, &timer_now);
842
843 /* If there are any ready threads from previous scheduler runs,
844 * process top of them.
845 */
paul718e3742002-12-13 20:15:29 +0000846 if ((thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000847 return thread_run (m, thread, fetch);
paula48b4e62005-04-22 00:43:47 +0000848
paul718e3742002-12-13 20:15:29 +0000849 /* Structure copy. */
850 readfd = m->readfd;
851 writefd = m->writefd;
852 exceptfd = m->exceptfd;
paula48b4e62005-04-22 00:43:47 +0000853
854 /* Calculate select wait timer if nothing else to do */
855 if (ready == 0)
856 {
857 timer_wait = thread_timer_wait (&m->timer, &timer_val);
858 timer_wait_bg = thread_timer_wait (&m->background, &timer_val_bg);
859
860 if (timer_wait && timer_wait_bg &&
861 (timeval_cmp (*timer_wait, *timer_wait_bg) > 0))
862 timer_wait = timer_wait_bg;
863 else if (!timer_wait && timer_wait_bg)
864 timer_wait = timer_wait_bg;
865 }
866 else
867 {
868 timer_val.tv_sec = timer_val.tv_usec = 0;
869 timer_wait = &timer_val;
870 }
871
paul718e3742002-12-13 20:15:29 +0000872 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
paula48b4e62005-04-22 00:43:47 +0000873
874 /* Signals should get quick treatment */
paul718e3742002-12-13 20:15:29 +0000875 if (num < 0)
paul05c447d2004-07-22 19:14:27 +0000876 {
877 if (errno == EINTR)
paula48b4e62005-04-22 00:43:47 +0000878 continue; /* signal received - process it */
ajs6099b3b2004-11-20 02:06:59 +0000879 zlog_warn ("select() error: %s", safe_strerror (errno));
paul05c447d2004-07-22 19:14:27 +0000880 return NULL;
881 }
paula48b4e62005-04-22 00:43:47 +0000882
883 /* Got IO, process it */
884 if (num > 0)
885 {
886 /* Normal priority read thead. */
887 ready += thread_process_fd (&m->read, &readfd, &m->readfd);
888 /* Write thead. */
889 ready += thread_process_fd (&m->write, &writefd, &m->writefd);
890 }
891
892 /* Background timer/events, lowest priority */
893 ready += thread_timer_process (&m->background, &timer_now);
894
895 /* if any threads were made ready above.. */
896 if ((ready > 0) && (thread = thread_trim_head (&m->ready)) != NULL)
paul05c447d2004-07-22 19:14:27 +0000897 return thread_run (m, thread, fetch);
paul718e3742002-12-13 20:15:29 +0000898 }
899}
900
ajs924b9222005-04-16 17:11:24 +0000901unsigned long
paul718e3742002-12-13 20:15:29 +0000902thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
903{
904 unsigned long thread_time;
905
906#ifdef HAVE_RUSAGE
907 /* This is 'user + sys' time. */
908 thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
909 thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
910#else
911 /* When rusage is not available, simple elapsed time is used. */
912 thread_time = timeval_elapsed (*now, *start);
913#endif /* HAVE_RUSAGE */
914
915 return thread_time;
916}
917
918/* We should aim to yield after THREAD_YIELD_TIME_SLOT
919 milliseconds. */
920int
921thread_should_yield (struct thread *thread)
922{
923 RUSAGE_T ru;
924
925 GETRUSAGE (&ru);
paula48b4e62005-04-22 00:43:47 +0000926
927 return (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT);
paul718e3742002-12-13 20:15:29 +0000928}
929
930/* We check thread consumed time. If the system has getrusage, we'll
931 use that to get indepth stats on the performance of the thread. If
932 not - we'll use gettimeofday for some guestimation. */
933void
934thread_call (struct thread *thread)
935{
936 unsigned long thread_time;
937 RUSAGE_T ru;
paule04ab742003-01-17 23:47:00 +0000938 struct cpu_thread_history tmp, *cpu;
939
940 tmp.func = thread->func;
941 tmp.funcname = thread->funcname;
942 cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc);
paul718e3742002-12-13 20:15:29 +0000943
944 GETRUSAGE (&thread->ru);
945
946 (*thread->func) (thread);
947
948 GETRUSAGE (&ru);
949
950 thread_time = thread_consumed_time (&ru, &thread->ru);
paule04ab742003-01-17 23:47:00 +0000951 cpu->total += thread_time;
952 if (cpu->max < thread_time)
953 cpu->max = thread_time;
954
955 ++cpu->total_calls;
956 cpu->types |= (1 << thread->add_type);
paul718e3742002-12-13 20:15:29 +0000957
ajs924b9222005-04-16 17:11:24 +0000958#ifdef CONSUMED_TIME_CHECK
959 if (thread_time > CONSUMED_TIME_CHECK)
paul718e3742002-12-13 20:15:29 +0000960 {
961 /*
962 * We have a CPU Hog on our hands.
963 * Whinge about it now, so we're aware this is yet another task
964 * to fix.
965 */
ajs924b9222005-04-16 17:11:24 +0000966 zlog_warn ("CPU HOG: task %s (%lx) ran for %ldms",
967 thread->funcname,
968 (unsigned long) thread->func,
969 thread_time / 1000L);
paul718e3742002-12-13 20:15:29 +0000970 }
ajs924b9222005-04-16 17:11:24 +0000971#endif /* CONSUMED_TIME_CHECK */
paul718e3742002-12-13 20:15:29 +0000972}
973
974/* Execute thread */
975struct thread *
paule04ab742003-01-17 23:47:00 +0000976funcname_thread_execute (struct thread_master *m,
paul718e3742002-12-13 20:15:29 +0000977 int (*func)(struct thread *),
978 void *arg,
paule04ab742003-01-17 23:47:00 +0000979 int val,
hasso8c328f12004-10-05 21:01:23 +0000980 const char* funcname)
paul718e3742002-12-13 20:15:29 +0000981{
982 struct thread dummy;
983
984 memset (&dummy, 0, sizeof (struct thread));
985
986 dummy.type = THREAD_EVENT;
paule04ab742003-01-17 23:47:00 +0000987 dummy.add_type = THREAD_EXECUTE;
paul718e3742002-12-13 20:15:29 +0000988 dummy.master = NULL;
989 dummy.func = func;
990 dummy.arg = arg;
991 dummy.u.val = val;
paule04ab742003-01-17 23:47:00 +0000992 dummy.funcname = strip_funcname (funcname);
paul718e3742002-12-13 20:15:29 +0000993 thread_call (&dummy);
994
paul9d11a192004-10-31 16:19:24 +0000995 XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname);
paul2946f652003-03-27 23:48:24 +0000996
paul718e3742002-12-13 20:15:29 +0000997 return NULL;
998}