]> git.proxmox.com Git - mirror_frr.git/blame - lib/thread.c
build: Quagga 0.99.23-rc1
[mirror_frr.git] / lib / thread.c
CommitLineData
718e3744 1/* 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"
e04ab74d 29#include "hash.h"
4becea72 30#include "pqueue.h"
e04ab74d 31#include "command.h"
05c447dd 32#include "sigevent.h"
d6be5fb9
VB
33
34#if defined HAVE_SNMP && defined SNMP_AGENTX
35#include <net-snmp/net-snmp-config.h>
36#include <net-snmp/net-snmp-includes.h>
37#include <net-snmp/agent/net-snmp-agent-includes.h>
38#include <net-snmp/agent/snmp_vars.h>
39
40extern int agentx_enabled;
41#endif
42
3b96b781
HT
43#if defined(__APPLE__)
44#include <mach/mach.h>
45#include <mach/mach_time.h>
46#endif
47
e04ab74d 48\f
db9c0df9 49/* Recent absolute time of day */
8b70d0b0 50struct timeval recent_time;
db9c0df9
PJ
51static struct timeval last_recent_time;
52/* Relative time, since startup */
53static struct timeval relative_time;
54static struct timeval relative_time_base;
55/* init flag */
56static unsigned short timers_inited;
57\f
e04ab74d 58static struct hash *cpu_record = NULL;
718e3744 59\f
60/* Struct timeval's tv_usec one second value. */
61#define TIMER_SECOND_MICRO 1000000L
62
8b70d0b0 63/* Adjust so that tv_usec is in the range [0,TIMER_SECOND_MICRO).
64 And change negative values to 0. */
a48b4e6d 65static struct timeval
718e3744 66timeval_adjust (struct timeval a)
67{
68 while (a.tv_usec >= TIMER_SECOND_MICRO)
69 {
70 a.tv_usec -= TIMER_SECOND_MICRO;
71 a.tv_sec++;
72 }
73
74 while (a.tv_usec < 0)
75 {
76 a.tv_usec += TIMER_SECOND_MICRO;
77 a.tv_sec--;
78 }
79
80 if (a.tv_sec < 0)
8b70d0b0 81 /* Change negative timeouts to 0. */
82 a.tv_sec = a.tv_usec = 0;
718e3744 83
84 return a;
85}
86
87static struct timeval
88timeval_subtract (struct timeval a, struct timeval b)
89{
90 struct timeval ret;
91
92 ret.tv_usec = a.tv_usec - b.tv_usec;
93 ret.tv_sec = a.tv_sec - b.tv_sec;
94
95 return timeval_adjust (ret);
96}
97
8b70d0b0 98static long
718e3744 99timeval_cmp (struct timeval a, struct timeval b)
100{
101 return (a.tv_sec == b.tv_sec
102 ? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
103}
104
105static unsigned long
106timeval_elapsed (struct timeval a, struct timeval b)
107{
108 return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO)
109 + (a.tv_usec - b.tv_usec));
110}
111\f
3b96b781 112#if !defined(HAVE_CLOCK_MONOTONIC) && !defined(__APPLE__)
db9c0df9
PJ
113static void
114quagga_gettimeofday_relative_adjust (void)
115{
116 struct timeval diff;
117 if (timeval_cmp (recent_time, last_recent_time) < 0)
118 {
119 relative_time.tv_sec++;
120 relative_time.tv_usec = 0;
121 }
122 else
123 {
124 diff = timeval_subtract (recent_time, last_recent_time);
125 relative_time.tv_sec += diff.tv_sec;
126 relative_time.tv_usec += diff.tv_usec;
127 relative_time = timeval_adjust (relative_time);
128 }
129 last_recent_time = recent_time;
130}
3b96b781 131#endif /* !HAVE_CLOCK_MONOTONIC && !__APPLE__ */
db9c0df9
PJ
132
133/* gettimeofday wrapper, to keep recent_time updated */
134static int
135quagga_gettimeofday (struct timeval *tv)
136{
137 int ret;
138
139 assert (tv);
140
141 if (!(ret = gettimeofday (&recent_time, NULL)))
142 {
143 /* init... */
144 if (!timers_inited)
145 {
146 relative_time_base = last_recent_time = recent_time;
147 timers_inited = 1;
148 }
149 /* avoid copy if user passed recent_time pointer.. */
150 if (tv != &recent_time)
151 *tv = recent_time;
152 return 0;
153 }
154 return ret;
155}
156
157static int
158quagga_get_relative (struct timeval *tv)
159{
160 int ret;
161
162#ifdef HAVE_CLOCK_MONOTONIC
163 {
164 struct timespec tp;
165 if (!(ret = clock_gettime (CLOCK_MONOTONIC, &tp)))
166 {
167 relative_time.tv_sec = tp.tv_sec;
168 relative_time.tv_usec = tp.tv_nsec / 1000;
169 }
170 }
3b96b781
HT
171#elif defined(__APPLE__)
172 {
173 uint64_t ticks;
174 uint64_t useconds;
175 static mach_timebase_info_data_t timebase_info;
176
177 ticks = mach_absolute_time();
178 if (timebase_info.denom == 0)
179 mach_timebase_info(&timebase_info);
180
181 useconds = ticks * timebase_info.numer / timebase_info.denom / 1000;
182 relative_time.tv_sec = useconds / 1000000;
183 relative_time.tv_usec = useconds % 1000000;
184
185 return 0;
186 }
187#else /* !HAVE_CLOCK_MONOTONIC && !__APPLE__ */
db9c0df9
PJ
188 if (!(ret = quagga_gettimeofday (&recent_time)))
189 quagga_gettimeofday_relative_adjust();
190#endif /* HAVE_CLOCK_MONOTONIC */
191
192 if (tv)
193 *tv = relative_time;
194
195 return ret;
196}
197
198/* Get absolute time stamp, but in terms of the internal timer
199 * Could be wrong, but at least won't go back.
200 */
201static void
202quagga_real_stabilised (struct timeval *tv)
203{
204 *tv = relative_time_base;
205 tv->tv_sec += relative_time.tv_sec;
206 tv->tv_usec += relative_time.tv_usec;
207 *tv = timeval_adjust (*tv);
208}
209
210/* Exported Quagga timestamp function.
211 * Modelled on POSIX clock_gettime.
212 */
213int
214quagga_gettime (enum quagga_clkid clkid, struct timeval *tv)
215{
216 switch (clkid)
217 {
218 case QUAGGA_CLK_REALTIME:
219 return quagga_gettimeofday (tv);
220 case QUAGGA_CLK_MONOTONIC:
221 return quagga_get_relative (tv);
222 case QUAGGA_CLK_REALTIME_STABILISED:
223 quagga_real_stabilised (tv);
224 return 0;
225 default:
226 errno = EINVAL;
227 return -1;
228 }
229}
230
231/* time_t value in terms of stabilised absolute time.
232 * replacement for POSIX time()
233 */
234time_t
235quagga_time (time_t *t)
236{
237 struct timeval tv;
238 quagga_real_stabilised (&tv);
239 if (t)
240 *t = tv.tv_sec;
241 return tv.tv_sec;
242}
243
244/* Public export of recent_relative_time by value */
245struct timeval
246recent_relative_time (void)
247{
248 return relative_time;
249}
250\f
a48b4e6d 251static unsigned int
e04ab74d 252cpu_record_hash_key (struct cpu_thread_history *a)
253{
8cc4198f 254 return (uintptr_t) a->func;
e04ab74d 255}
256
257static int
ffe11cfb
SH
258cpu_record_hash_cmp (const struct cpu_thread_history *a,
259 const struct cpu_thread_history *b)
e04ab74d 260{
261 return a->func == b->func;
262}
263
8cc4198f 264static void *
e04ab74d 265cpu_record_hash_alloc (struct cpu_thread_history *a)
266{
267 struct cpu_thread_history *new;
039b9577 268 new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history));
e04ab74d 269 new->func = a->func;
22714f99 270 strcpy(new->funcname, a->funcname);
e04ab74d 271 return new;
272}
273
228da428
CC
274static void
275cpu_record_hash_free (void *a)
276{
277 struct cpu_thread_history *hist = a;
278
228da428
CC
279 XFREE (MTYPE_THREAD_STATS, hist);
280}
281
f63f06da 282static void
e04ab74d 283vty_out_cpu_thread_history(struct vty* vty,
284 struct cpu_thread_history *a)
285{
8b70d0b0 286#ifdef HAVE_RUSAGE
287 vty_out(vty, "%7ld.%03ld %9d %8ld %9ld %8ld %9ld",
288 a->cpu.total/1000, a->cpu.total%1000, a->total_calls,
289 a->cpu.total/a->total_calls, a->cpu.max,
290 a->real.total/a->total_calls, a->real.max);
291#else
292 vty_out(vty, "%7ld.%03ld %9d %8ld %9ld",
293 a->real.total/1000, a->real.total%1000, a->total_calls,
294 a->real.total/a->total_calls, a->real.max);
295#endif
296 vty_out(vty, " %c%c%c%c%c%c %s%s",
e04ab74d 297 a->types & (1 << THREAD_READ) ? 'R':' ',
298 a->types & (1 << THREAD_WRITE) ? 'W':' ',
299 a->types & (1 << THREAD_TIMER) ? 'T':' ',
300 a->types & (1 << THREAD_EVENT) ? 'E':' ',
301 a->types & (1 << THREAD_EXECUTE) ? 'X':' ',
a48b4e6d 302 a->types & (1 << THREAD_BACKGROUND) ? 'B' : ' ',
e04ab74d 303 a->funcname, VTY_NEWLINE);
304}
305
306static void
307cpu_record_hash_print(struct hash_backet *bucket,
308 void *args[])
309{
310 struct cpu_thread_history *totals = args[0];
311 struct vty *vty = args[1];
41b2373c 312 thread_type *filter = args[2];
e04ab74d 313 struct cpu_thread_history *a = bucket->data;
a48b4e6d 314
e04ab74d 315 a = bucket->data;
316 if ( !(a->types & *filter) )
317 return;
318 vty_out_cpu_thread_history(vty,a);
e04ab74d 319 totals->total_calls += a->total_calls;
8b70d0b0 320 totals->real.total += a->real.total;
321 if (totals->real.max < a->real.max)
322 totals->real.max = a->real.max;
323#ifdef HAVE_RUSAGE
324 totals->cpu.total += a->cpu.total;
325 if (totals->cpu.max < a->cpu.max)
326 totals->cpu.max = a->cpu.max;
327#endif
e04ab74d 328}
329
330static void
41b2373c 331cpu_record_print(struct vty *vty, thread_type filter)
e04ab74d 332{
333 struct cpu_thread_history tmp;
334 void *args[3] = {&tmp, vty, &filter};
335
336 memset(&tmp, 0, sizeof tmp);
22714f99 337 strcpy(tmp.funcname, "TOTAL");
e04ab74d 338 tmp.types = filter;
339
8b70d0b0 340#ifdef HAVE_RUSAGE
341 vty_out(vty, "%21s %18s %18s%s",
342 "", "CPU (user+system):", "Real (wall-clock):", VTY_NEWLINE);
343#endif
344 vty_out(vty, "Runtime(ms) Invoked Avg uSec Max uSecs");
345#ifdef HAVE_RUSAGE
346 vty_out(vty, " Avg uSec Max uSecs");
347#endif
348 vty_out(vty, " Type Thread%s", VTY_NEWLINE);
e04ab74d 349 hash_iterate(cpu_record,
350 (void(*)(struct hash_backet*,void*))cpu_record_hash_print,
351 args);
352
353 if (tmp.total_calls > 0)
354 vty_out_cpu_thread_history(vty, &tmp);
355}
356
357DEFUN(show_thread_cpu,
358 show_thread_cpu_cmd,
359 "show thread cpu [FILTER]",
360 SHOW_STR
361 "Thread information\n"
362 "Thread CPU usage\n"
a48b4e6d 363 "Display filter (rwtexb)\n")
e04ab74d 364{
365 int i = 0;
41b2373c 366 thread_type filter = (thread_type) -1U;
e04ab74d 367
368 if (argc > 0)
369 {
370 filter = 0;
371 while (argv[0][i] != '\0')
372 {
373 switch ( argv[0][i] )
374 {
375 case 'r':
376 case 'R':
377 filter |= (1 << THREAD_READ);
378 break;
379 case 'w':
380 case 'W':
381 filter |= (1 << THREAD_WRITE);
382 break;
383 case 't':
384 case 'T':
385 filter |= (1 << THREAD_TIMER);
386 break;
387 case 'e':
388 case 'E':
389 filter |= (1 << THREAD_EVENT);
390 break;
391 case 'x':
392 case 'X':
393 filter |= (1 << THREAD_EXECUTE);
394 break;
a48b4e6d 395 case 'b':
396 case 'B':
397 filter |= (1 << THREAD_BACKGROUND);
398 break;
e04ab74d 399 default:
400 break;
401 }
402 ++i;
403 }
404 if (filter == 0)
405 {
a48b4e6d 406 vty_out(vty, "Invalid filter \"%s\" specified,"
407 " must contain at least one of 'RWTEXB'%s",
e04ab74d 408 argv[0], VTY_NEWLINE);
409 return CMD_WARNING;
410 }
411 }
412
413 cpu_record_print(vty, filter);
414 return CMD_SUCCESS;
415}
e276eb82
PJ
416
417static void
418cpu_record_hash_clear (struct hash_backet *bucket,
419 void *args)
420{
421 thread_type *filter = args;
422 struct cpu_thread_history *a = bucket->data;
423
424 a = bucket->data;
425 if ( !(a->types & *filter) )
426 return;
427
428 hash_release (cpu_record, bucket->data);
429}
430
431static void
432cpu_record_clear (thread_type filter)
433{
434 thread_type *tmp = &filter;
435 hash_iterate (cpu_record,
436 (void (*) (struct hash_backet*,void*)) cpu_record_hash_clear,
437 tmp);
438}
439
440DEFUN(clear_thread_cpu,
441 clear_thread_cpu_cmd,
442 "clear thread cpu [FILTER]",
443 "Clear stored data\n"
444 "Thread information\n"
445 "Thread CPU usage\n"
446 "Display filter (rwtexb)\n")
447{
448 int i = 0;
449 thread_type filter = (thread_type) -1U;
450
451 if (argc > 0)
452 {
453 filter = 0;
454 while (argv[0][i] != '\0')
455 {
456 switch ( argv[0][i] )
457 {
458 case 'r':
459 case 'R':
460 filter |= (1 << THREAD_READ);
461 break;
462 case 'w':
463 case 'W':
464 filter |= (1 << THREAD_WRITE);
465 break;
466 case 't':
467 case 'T':
468 filter |= (1 << THREAD_TIMER);
469 break;
470 case 'e':
471 case 'E':
472 filter |= (1 << THREAD_EVENT);
473 break;
474 case 'x':
475 case 'X':
476 filter |= (1 << THREAD_EXECUTE);
477 break;
478 case 'b':
479 case 'B':
480 filter |= (1 << THREAD_BACKGROUND);
481 break;
482 default:
483 break;
484 }
485 ++i;
486 }
487 if (filter == 0)
488 {
489 vty_out(vty, "Invalid filter \"%s\" specified,"
490 " must contain at least one of 'RWTEXB'%s",
491 argv[0], VTY_NEWLINE);
492 return CMD_WARNING;
493 }
494 }
495
496 cpu_record_clear (filter);
497 return CMD_SUCCESS;
498}
e04ab74d 499\f
4becea72
CF
500static int
501thread_timer_cmp(void *a, void *b)
502{
503 struct thread *thread_a = a;
504 struct thread *thread_b = b;
505
506 long cmp = timeval_cmp(thread_a->u.sands, thread_b->u.sands);
507
508 if (cmp < 0)
509 return -1;
510 if (cmp > 0)
511 return 1;
512 return 0;
513}
514
515static void
516thread_timer_update(void *node, int actual_position)
517{
518 struct thread *thread = node;
519
520 thread->index = actual_position;
521}
522
718e3744 523/* Allocate new thread master. */
524struct thread_master *
525thread_master_create ()
526{
4becea72
CF
527 struct thread_master *rv;
528
e04ab74d 529 if (cpu_record == NULL)
8cc4198f 530 cpu_record
90645f55
SH
531 = hash_create ((unsigned int (*) (void *))cpu_record_hash_key,
532 (int (*) (const void *, const void *))cpu_record_hash_cmp);
4becea72
CF
533
534 rv = XCALLOC (MTYPE_THREAD_MASTER, sizeof (struct thread_master));
535
536 /* Initialize the timer queues */
537 rv->timer = pqueue_create();
538 rv->background = pqueue_create();
539 rv->timer->cmp = rv->background->cmp = thread_timer_cmp;
540 rv->timer->update = rv->background->update = thread_timer_update;
541
542 return rv;
718e3744 543}
544
545/* Add a new thread to the list. */
546static void
547thread_list_add (struct thread_list *list, struct thread *thread)
548{
549 thread->next = NULL;
550 thread->prev = list->tail;
551 if (list->tail)
552 list->tail->next = thread;
553 else
554 list->head = thread;
555 list->tail = thread;
556 list->count++;
557}
558
718e3744 559/* Delete a thread from the list. */
560static struct thread *
561thread_list_delete (struct thread_list *list, struct thread *thread)
562{
563 if (thread->next)
564 thread->next->prev = thread->prev;
565 else
566 list->tail = thread->prev;
567 if (thread->prev)
568 thread->prev->next = thread->next;
569 else
570 list->head = thread->next;
571 thread->next = thread->prev = NULL;
572 list->count--;
573 return thread;
574}
575
576/* Move thread to unuse list. */
577static void
578thread_add_unuse (struct thread_master *m, struct thread *thread)
579{
a48b4e6d 580 assert (m != NULL && thread != NULL);
718e3744 581 assert (thread->next == NULL);
582 assert (thread->prev == NULL);
583 assert (thread->type == THREAD_UNUSED);
584 thread_list_add (&m->unuse, thread);
9d11a19e 585 /* XXX: Should we deallocate funcname here? */
718e3744 586}
587
588/* Free all unused thread. */
589static void
590thread_list_free (struct thread_master *m, struct thread_list *list)
591{
592 struct thread *t;
593 struct thread *next;
594
595 for (t = list->head; t; t = next)
596 {
597 next = t->next;
598 XFREE (MTYPE_THREAD, t);
599 list->count--;
600 m->alloc--;
601 }
602}
603
4becea72
CF
604static void
605thread_queue_free (struct thread_master *m, struct pqueue *queue)
606{
607 int i;
608
609 for (i = 0; i < queue->size; i++)
610 XFREE(MTYPE_THREAD, queue->array[i]);
611
612 m->alloc -= queue->size;
613 pqueue_delete(queue);
614}
615
718e3744 616/* Stop thread scheduler. */
617void
618thread_master_free (struct thread_master *m)
619{
620 thread_list_free (m, &m->read);
621 thread_list_free (m, &m->write);
4becea72 622 thread_queue_free (m, m->timer);
718e3744 623 thread_list_free (m, &m->event);
624 thread_list_free (m, &m->ready);
625 thread_list_free (m, &m->unuse);
4becea72 626 thread_queue_free (m, m->background);
a48b4e6d 627
718e3744 628 XFREE (MTYPE_THREAD_MASTER, m);
228da428
CC
629
630 if (cpu_record)
631 {
632 hash_clean (cpu_record, cpu_record_hash_free);
633 hash_free (cpu_record);
634 cpu_record = NULL;
635 }
718e3744 636}
637
8cc4198f 638/* Thread list is empty or not. */
f63f06da 639static int
8cc4198f 640thread_empty (struct thread_list *list)
641{
642 return list->head ? 0 : 1;
643}
644
718e3744 645/* Delete top of the list and return it. */
646static struct thread *
647thread_trim_head (struct thread_list *list)
648{
8cc4198f 649 if (!thread_empty (list))
718e3744 650 return thread_list_delete (list, list->head);
651 return NULL;
652}
653
718e3744 654/* Return remain time in second. */
655unsigned long
656thread_timer_remain_second (struct thread *thread)
657{
db9c0df9
PJ
658 quagga_get_relative (NULL);
659
660 if (thread->u.sands.tv_sec - relative_time.tv_sec > 0)
661 return thread->u.sands.tv_sec - relative_time.tv_sec;
718e3744 662 else
663 return 0;
664}
665
e04ab74d 666/* Trim blankspace and "()"s */
22714f99
JBD
667void
668strip_funcname (char *dest, const char *funcname)
e04ab74d 669{
22714f99
JBD
670 char buff[FUNCNAME_LEN];
671 char tmp, *e, *b = buff;
e04ab74d 672
673 strncpy(buff, funcname, sizeof(buff));
674 buff[ sizeof(buff) -1] = '\0';
675 e = buff +strlen(buff) -1;
676
677 /* Wont work for funcname == "Word (explanation)" */
678
679 while (*b == ' ' || *b == '(')
680 ++b;
681 while (*e == ' ' || *e == ')')
682 --e;
683 e++;
684
685 tmp = *e;
686 *e = '\0';
22714f99 687 strcpy (dest, b);
e04ab74d 688 *e = tmp;
e04ab74d 689}
690
718e3744 691/* Get new thread. */
692static struct thread *
693thread_get (struct thread_master *m, u_char type,
8c328f11 694 int (*func) (struct thread *), void *arg, const char* funcname)
718e3744 695{
64018324 696 struct thread *thread = thread_trim_head (&m->unuse);
718e3744 697
22714f99 698 if (! thread)
718e3744 699 {
700 thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
701 m->alloc++;
702 }
703 thread->type = type;
e04ab74d 704 thread->add_type = type;
718e3744 705 thread->master = m;
706 thread->func = func;
707 thread->arg = arg;
4becea72
CF
708 thread->index = -1;
709
22714f99 710 strip_funcname (thread->funcname, funcname);
e04ab74d 711
718e3744 712 return thread;
713}
714
715/* Add new read thread. */
716struct thread *
e04ab74d 717funcname_thread_add_read (struct thread_master *m,
8c328f11 718 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
718e3744 719{
720 struct thread *thread;
721
722 assert (m != NULL);
723
724 if (FD_ISSET (fd, &m->readfd))
725 {
726 zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
727 return NULL;
728 }
729
e04ab74d 730 thread = thread_get (m, THREAD_READ, func, arg, funcname);
718e3744 731 FD_SET (fd, &m->readfd);
732 thread->u.fd = fd;
733 thread_list_add (&m->read, thread);
734
735 return thread;
736}
737
738/* Add new write thread. */
739struct thread *
e04ab74d 740funcname_thread_add_write (struct thread_master *m,
8c328f11 741 int (*func) (struct thread *), void *arg, int fd, const char* funcname)
718e3744 742{
743 struct thread *thread;
744
745 assert (m != NULL);
746
747 if (FD_ISSET (fd, &m->writefd))
748 {
749 zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
750 return NULL;
751 }
752
e04ab74d 753 thread = thread_get (m, THREAD_WRITE, func, arg, funcname);
718e3744 754 FD_SET (fd, &m->writefd);
755 thread->u.fd = fd;
756 thread_list_add (&m->write, thread);
757
758 return thread;
759}
760
98c91ac6 761static struct thread *
762funcname_thread_add_timer_timeval (struct thread_master *m,
763 int (*func) (struct thread *),
a48b4e6d 764 int type,
98c91ac6 765 void *arg,
766 struct timeval *time_relative,
8c328f11 767 const char* funcname)
718e3744 768{
718e3744 769 struct thread *thread;
4becea72 770 struct pqueue *queue;
db9c0df9 771 struct timeval alarm_time;
718e3744 772
773 assert (m != NULL);
774
8b70d0b0 775 assert (type == THREAD_TIMER || type == THREAD_BACKGROUND);
a48b4e6d 776 assert (time_relative);
777
4becea72 778 queue = ((type == THREAD_TIMER) ? m->timer : m->background);
a48b4e6d 779 thread = thread_get (m, type, func, arg, funcname);
718e3744 780
781 /* Do we need jitter here? */
b8192765 782 quagga_get_relative (NULL);
db9c0df9
PJ
783 alarm_time.tv_sec = relative_time.tv_sec + time_relative->tv_sec;
784 alarm_time.tv_usec = relative_time.tv_usec + time_relative->tv_usec;
8b70d0b0 785 thread->u.sands = timeval_adjust(alarm_time);
718e3744 786
4becea72 787 pqueue_enqueue(thread, queue);
9e867fe6 788 return thread;
789}
790
98c91ac6 791
792/* Add timer event thread. */
9e867fe6 793struct thread *
98c91ac6 794funcname_thread_add_timer (struct thread_master *m,
795 int (*func) (struct thread *),
8c328f11 796 void *arg, long timer, const char* funcname)
9e867fe6 797{
98c91ac6 798 struct timeval trel;
9e867fe6 799
800 assert (m != NULL);
801
9076fbd3 802 trel.tv_sec = timer;
98c91ac6 803 trel.tv_usec = 0;
9e867fe6 804
a48b4e6d 805 return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg,
806 &trel, funcname);
98c91ac6 807}
9e867fe6 808
98c91ac6 809/* Add timer event thread with "millisecond" resolution */
810struct thread *
811funcname_thread_add_timer_msec (struct thread_master *m,
812 int (*func) (struct thread *),
8c328f11 813 void *arg, long timer, const char* funcname)
98c91ac6 814{
815 struct timeval trel;
9e867fe6 816
98c91ac6 817 assert (m != NULL);
718e3744 818
af04bd7c 819 trel.tv_sec = timer / 1000;
820 trel.tv_usec = 1000*(timer % 1000);
98c91ac6 821
a48b4e6d 822 return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER,
823 arg, &trel, funcname);
824}
825
826/* Add a background thread, with an optional millisec delay */
827struct thread *
828funcname_thread_add_background (struct thread_master *m,
829 int (*func) (struct thread *),
830 void *arg, long delay,
831 const char *funcname)
832{
833 struct timeval trel;
834
835 assert (m != NULL);
836
837 if (delay)
838 {
839 trel.tv_sec = delay / 1000;
840 trel.tv_usec = 1000*(delay % 1000);
841 }
842 else
843 {
844 trel.tv_sec = 0;
845 trel.tv_usec = 0;
846 }
847
848 return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND,
849 arg, &trel, funcname);
718e3744 850}
851
852/* Add simple event thread. */
853struct thread *
e04ab74d 854funcname_thread_add_event (struct thread_master *m,
8c328f11 855 int (*func) (struct thread *), void *arg, int val, const char* funcname)
718e3744 856{
857 struct thread *thread;
858
859 assert (m != NULL);
860
e04ab74d 861 thread = thread_get (m, THREAD_EVENT, func, arg, funcname);
718e3744 862 thread->u.val = val;
863 thread_list_add (&m->event, thread);
864
865 return thread;
866}
867
868/* Cancel thread from scheduler. */
869void
870thread_cancel (struct thread *thread)
871{
4becea72
CF
872 struct thread_list *list = NULL;
873 struct pqueue *queue = NULL;
a48b4e6d 874
718e3744 875 switch (thread->type)
876 {
877 case THREAD_READ:
878 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
879 FD_CLR (thread->u.fd, &thread->master->readfd);
a48b4e6d 880 list = &thread->master->read;
718e3744 881 break;
882 case THREAD_WRITE:
883 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
884 FD_CLR (thread->u.fd, &thread->master->writefd);
a48b4e6d 885 list = &thread->master->write;
718e3744 886 break;
887 case THREAD_TIMER:
4becea72 888 queue = thread->master->timer;
718e3744 889 break;
890 case THREAD_EVENT:
a48b4e6d 891 list = &thread->master->event;
718e3744 892 break;
893 case THREAD_READY:
a48b4e6d 894 list = &thread->master->ready;
718e3744 895 break;
a48b4e6d 896 case THREAD_BACKGROUND:
4becea72 897 queue = thread->master->background;
8b70d0b0 898 break;
718e3744 899 default:
a48b4e6d 900 return;
718e3744 901 break;
902 }
4becea72
CF
903
904 if (queue)
905 {
906 assert(thread->index >= 0);
907 assert(thread == queue->array[thread->index]);
908 pqueue_remove_at(thread->index, queue);
909 }
910 else if (list)
911 {
912 thread_list_delete (list, thread);
913 }
914 else
915 {
916 assert(!"Thread should be either in queue or list!");
917 }
918
718e3744 919 thread->type = THREAD_UNUSED;
920 thread_add_unuse (thread->master, thread);
921}
922
923/* Delete all events which has argument value arg. */
dc81807a 924unsigned int
718e3744 925thread_cancel_event (struct thread_master *m, void *arg)
926{
dc81807a 927 unsigned int ret = 0;
718e3744 928 struct thread *thread;
929
930 thread = m->event.head;
931 while (thread)
932 {
933 struct thread *t;
934
935 t = thread;
936 thread = t->next;
937
938 if (t->arg == arg)
a48b4e6d 939 {
dc81807a 940 ret++;
a48b4e6d 941 thread_list_delete (&m->event, t);
942 t->type = THREAD_UNUSED;
943 thread_add_unuse (m, t);
944 }
718e3744 945 }
1b79fcb6
JBD
946
947 /* thread can be on the ready list too */
948 thread = m->ready.head;
949 while (thread)
950 {
951 struct thread *t;
952
953 t = thread;
954 thread = t->next;
955
956 if (t->arg == arg)
957 {
958 ret++;
959 thread_list_delete (&m->ready, t);
960 t->type = THREAD_UNUSED;
961 thread_add_unuse (m, t);
962 }
963 }
dc81807a 964 return ret;
718e3744 965}
966
a48b4e6d 967static struct timeval *
4becea72 968thread_timer_wait (struct pqueue *queue, struct timeval *timer_val)
718e3744 969{
4becea72 970 if (queue->size)
718e3744 971 {
4becea72
CF
972 struct thread *next_timer = queue->array[0];
973 *timer_val = timeval_subtract (next_timer->u.sands, relative_time);
718e3744 974 return timer_val;
975 }
976 return NULL;
977}
718e3744 978
8cc4198f 979static struct thread *
718e3744 980thread_run (struct thread_master *m, struct thread *thread,
981 struct thread *fetch)
982{
983 *fetch = *thread;
984 thread->type = THREAD_UNUSED;
985 thread_add_unuse (m, thread);
986 return fetch;
987}
988
a48b4e6d 989static int
990thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset)
718e3744 991{
992 struct thread *thread;
993 struct thread *next;
994 int ready = 0;
a48b4e6d 995
996 assert (list);
997
718e3744 998 for (thread = list->head; thread; thread = next)
999 {
1000 next = thread->next;
1001
1002 if (FD_ISSET (THREAD_FD (thread), fdset))
a48b4e6d 1003 {
1004 assert (FD_ISSET (THREAD_FD (thread), mfdset));
1005 FD_CLR(THREAD_FD (thread), mfdset);
1006 thread_list_delete (list, thread);
1007 thread_list_add (&thread->master->ready, thread);
1008 thread->type = THREAD_READY;
1009 ready++;
1010 }
718e3744 1011 }
1012 return ready;
1013}
1014
8b70d0b0 1015/* Add all timers that have popped to the ready list. */
a48b4e6d 1016static unsigned int
4becea72 1017thread_timer_process (struct pqueue *queue, struct timeval *timenow)
a48b4e6d 1018{
1019 struct thread *thread;
1020 unsigned int ready = 0;
1021
4becea72 1022 while (queue->size)
8b70d0b0 1023 {
4becea72 1024 thread = queue->array[0];
8b70d0b0 1025 if (timeval_cmp (*timenow, thread->u.sands) < 0)
1026 return ready;
4becea72 1027 pqueue_dequeue(queue);
8b70d0b0 1028 thread->type = THREAD_READY;
1029 thread_list_add (&thread->master->ready, thread);
1030 ready++;
1031 }
a48b4e6d 1032 return ready;
1033}
1034
2613abe6
PJ
1035/* process a list en masse, e.g. for event thread lists */
1036static unsigned int
1037thread_process (struct thread_list *list)
1038{
1039 struct thread *thread;
b5043aab 1040 struct thread *next;
2613abe6
PJ
1041 unsigned int ready = 0;
1042
b5043aab 1043 for (thread = list->head; thread; thread = next)
2613abe6 1044 {
b5043aab 1045 next = thread->next;
2613abe6
PJ
1046 thread_list_delete (list, thread);
1047 thread->type = THREAD_READY;
1048 thread_list_add (&thread->master->ready, thread);
1049 ready++;
1050 }
1051 return ready;
1052}
1053
1054
718e3744 1055/* Fetch next ready thread. */
1056struct thread *
1057thread_fetch (struct thread_master *m, struct thread *fetch)
1058{
718e3744 1059 struct thread *thread;
1060 fd_set readfd;
1061 fd_set writefd;
1062 fd_set exceptfd;
2613abe6 1063 struct timeval timer_val = { .tv_sec = 0, .tv_usec = 0 };
a48b4e6d 1064 struct timeval timer_val_bg;
2613abe6 1065 struct timeval *timer_wait = &timer_val;
a48b4e6d 1066 struct timeval *timer_wait_bg;
718e3744 1067
1068 while (1)
1069 {
a48b4e6d 1070 int num = 0;
d6be5fb9
VB
1071#if defined HAVE_SNMP && defined SNMP_AGENTX
1072 struct timeval snmp_timer_wait;
1073 int snmpblock = 0;
1074 int fdsetsize;
1075#endif
a48b4e6d 1076
2613abe6 1077 /* Signals pre-empt everything */
05c447dd 1078 quagga_sigevent_process ();
1079
2613abe6
PJ
1080 /* Drain the ready queue of already scheduled jobs, before scheduling
1081 * more.
a48b4e6d 1082 */
718e3744 1083 if ((thread = thread_trim_head (&m->ready)) != NULL)
05c447dd 1084 return thread_run (m, thread, fetch);
a48b4e6d 1085
2613abe6
PJ
1086 /* To be fair to all kinds of threads, and avoid starvation, we
1087 * need to be careful to consider all thread types for scheduling
1088 * in each quanta. I.e. we should not return early from here on.
1089 */
1090
1091 /* Normal event are the next highest priority. */
1092 thread_process (&m->event);
1093
718e3744 1094 /* Structure copy. */
1095 readfd = m->readfd;
1096 writefd = m->writefd;
1097 exceptfd = m->exceptfd;
a48b4e6d 1098
1099 /* Calculate select wait timer if nothing else to do */
2613abe6
PJ
1100 if (m->ready.count == 0)
1101 {
1102 quagga_get_relative (NULL);
4becea72
CF
1103 timer_wait = thread_timer_wait (m->timer, &timer_val);
1104 timer_wait_bg = thread_timer_wait (m->background, &timer_val_bg);
2613abe6
PJ
1105
1106 if (timer_wait_bg &&
1107 (!timer_wait || (timeval_cmp (*timer_wait, *timer_wait_bg) > 0)))
1108 timer_wait = timer_wait_bg;
1109 }
a48b4e6d 1110
d6be5fb9
VB
1111#if defined HAVE_SNMP && defined SNMP_AGENTX
1112 /* When SNMP is enabled, we may have to select() on additional
1113 FD. snmp_select_info() will add them to `readfd'. The trick
1114 with this function is its last argument. We need to set it to
1115 0 if timer_wait is not NULL and we need to use the provided
1116 new timer only if it is still set to 0. */
1117 if (agentx_enabled)
1118 {
1119 fdsetsize = FD_SETSIZE;
1120 snmpblock = 1;
1121 if (timer_wait)
1122 {
1123 snmpblock = 0;
1124 memcpy(&snmp_timer_wait, timer_wait, sizeof(struct timeval));
1125 }
1126 snmp_select_info(&fdsetsize, &readfd, &snmp_timer_wait, &snmpblock);
1127 if (snmpblock == 0)
1128 timer_wait = &snmp_timer_wait;
1129 }
1130#endif
718e3744 1131 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
a48b4e6d 1132
1133 /* Signals should get quick treatment */
718e3744 1134 if (num < 0)
05c447dd 1135 {
1136 if (errno == EINTR)
a48b4e6d 1137 continue; /* signal received - process it */
6099b3b5 1138 zlog_warn ("select() error: %s", safe_strerror (errno));
05c447dd 1139 return NULL;
1140 }
8b70d0b0 1141
d6be5fb9
VB
1142#if defined HAVE_SNMP && defined SNMP_AGENTX
1143 if (agentx_enabled)
1144 {
1145 if (num > 0)
1146 snmp_read(&readfd);
1147 else if (num == 0)
1148 {
1149 snmp_timeout();
1150 run_alarms();
1151 }
1152 netsnmp_check_outstanding_agent_requests();
1153 }
1154#endif
1155
8b70d0b0 1156 /* Check foreground timers. Historically, they have had higher
1157 priority than I/O threads, so let's push them onto the ready
1158 list in front of the I/O threads. */
db9c0df9 1159 quagga_get_relative (NULL);
4becea72 1160 thread_timer_process (m->timer, &relative_time);
a48b4e6d 1161
1162 /* Got IO, process it */
1163 if (num > 0)
1164 {
1165 /* Normal priority read thead. */
8b70d0b0 1166 thread_process_fd (&m->read, &readfd, &m->readfd);
a48b4e6d 1167 /* Write thead. */
8b70d0b0 1168 thread_process_fd (&m->write, &writefd, &m->writefd);
a48b4e6d 1169 }
8b70d0b0 1170
1171#if 0
1172 /* If any threads were made ready above (I/O or foreground timer),
1173 perhaps we should avoid adding background timers to the ready
1174 list at this time. If this is code is uncommented, then background
1175 timer threads will not run unless there is nothing else to do. */
1176 if ((thread = thread_trim_head (&m->ready)) != NULL)
1177 return thread_run (m, thread, fetch);
1178#endif
1179
a48b4e6d 1180 /* Background timer/events, lowest priority */
4becea72 1181 thread_timer_process (m->background, &relative_time);
a48b4e6d 1182
8b70d0b0 1183 if ((thread = thread_trim_head (&m->ready)) != NULL)
05c447dd 1184 return thread_run (m, thread, fetch);
718e3744 1185 }
1186}
1187
924b9229 1188unsigned long
8b70d0b0 1189thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start, unsigned long *cputime)
718e3744 1190{
718e3744 1191#ifdef HAVE_RUSAGE
1192 /* This is 'user + sys' time. */
8b70d0b0 1193 *cputime = timeval_elapsed (now->cpu.ru_utime, start->cpu.ru_utime) +
1194 timeval_elapsed (now->cpu.ru_stime, start->cpu.ru_stime);
718e3744 1195#else
8b70d0b0 1196 *cputime = 0;
718e3744 1197#endif /* HAVE_RUSAGE */
8b70d0b0 1198 return timeval_elapsed (now->real, start->real);
1199}
1200
1201/* We should aim to yield after THREAD_YIELD_TIME_SLOT milliseconds.
1202 Note: we are using real (wall clock) time for this calculation.
1203 It could be argued that CPU time may make more sense in certain
1204 contexts. The things to consider are whether the thread may have
1205 blocked (in which case wall time increases, but CPU time does not),
1206 or whether the system is heavily loaded with other processes competing
1207 for CPU time. On balance, wall clock time seems to make sense.
1208 Plus it has the added benefit that gettimeofday should be faster
1209 than calling getrusage. */
718e3744 1210int
1211thread_should_yield (struct thread *thread)
1212{
db9c0df9 1213 quagga_get_relative (NULL);
41af338e 1214 return (timeval_elapsed(relative_time, thread->real) >
8b70d0b0 1215 THREAD_YIELD_TIME_SLOT);
718e3744 1216}
1217
db9c0df9
PJ
1218void
1219thread_getrusage (RUSAGE_T *r)
1220{
1221 quagga_get_relative (NULL);
1222#ifdef HAVE_RUSAGE
1223 getrusage(RUSAGE_SELF, &(r->cpu));
1224#endif
1225 r->real = relative_time;
1226
1227#ifdef HAVE_CLOCK_MONOTONIC
1228 /* quagga_get_relative() only updates recent_time if gettimeofday
1229 * based, not when using CLOCK_MONOTONIC. As we export recent_time
1230 * and guarantee to update it before threads are run...
1231 */
1232 quagga_gettimeofday(&recent_time);
1233#endif /* HAVE_CLOCK_MONOTONIC */
1234}
1235
718e3744 1236/* We check thread consumed time. If the system has getrusage, we'll
8b70d0b0 1237 use that to get in-depth stats on the performance of the thread in addition
1238 to wall clock time stats from gettimeofday. */
718e3744 1239void
1240thread_call (struct thread *thread)
1241{
8b70d0b0 1242 unsigned long realtime, cputime;
41af338e 1243 RUSAGE_T before, after;
cc8b13a0
PJ
1244
1245 /* Cache a pointer to the relevant cpu history thread, if the thread
1246 * does not have it yet.
1247 *
1248 * Callers submitting 'dummy threads' hence must take care that
1249 * thread->cpu is NULL
1250 */
1251 if (!thread->hist)
1252 {
1253 struct cpu_thread_history tmp;
1254
1255 tmp.func = thread->func;
22714f99 1256 strcpy(tmp.funcname, thread->funcname);
cc8b13a0
PJ
1257
1258 thread->hist = hash_get (cpu_record, &tmp,
1259 (void * (*) (void *))cpu_record_hash_alloc);
1260 }
718e3744 1261
41af338e
JBD
1262 GETRUSAGE (&before);
1263 thread->real = before.real;
718e3744 1264
1265 (*thread->func) (thread);
1266
41af338e 1267 GETRUSAGE (&after);
718e3744 1268
41af338e 1269 realtime = thread_consumed_time (&after, &before, &cputime);
cc8b13a0
PJ
1270 thread->hist->real.total += realtime;
1271 if (thread->hist->real.max < realtime)
1272 thread->hist->real.max = realtime;
8b70d0b0 1273#ifdef HAVE_RUSAGE
cc8b13a0
PJ
1274 thread->hist->cpu.total += cputime;
1275 if (thread->hist->cpu.max < cputime)
1276 thread->hist->cpu.max = cputime;
8b70d0b0 1277#endif
e04ab74d 1278
cc8b13a0
PJ
1279 ++(thread->hist->total_calls);
1280 thread->hist->types |= (1 << thread->add_type);
718e3744 1281
924b9229 1282#ifdef CONSUMED_TIME_CHECK
8b70d0b0 1283 if (realtime > CONSUMED_TIME_CHECK)
718e3744 1284 {
1285 /*
1286 * We have a CPU Hog on our hands.
1287 * Whinge about it now, so we're aware this is yet another task
1288 * to fix.
1289 */
8b70d0b0 1290 zlog_warn ("SLOW THREAD: task %s (%lx) ran for %lums (cpu time %lums)",
924b9229 1291 thread->funcname,
1292 (unsigned long) thread->func,
8b70d0b0 1293 realtime/1000, cputime/1000);
718e3744 1294 }
924b9229 1295#endif /* CONSUMED_TIME_CHECK */
718e3744 1296}
1297
1298/* Execute thread */
1299struct thread *
e04ab74d 1300funcname_thread_execute (struct thread_master *m,
718e3744 1301 int (*func)(struct thread *),
1302 void *arg,
e04ab74d 1303 int val,
8c328f11 1304 const char* funcname)
718e3744 1305{
1306 struct thread dummy;
1307
1308 memset (&dummy, 0, sizeof (struct thread));
1309
1310 dummy.type = THREAD_EVENT;
e04ab74d 1311 dummy.add_type = THREAD_EXECUTE;
718e3744 1312 dummy.master = NULL;
1313 dummy.func = func;
1314 dummy.arg = arg;
1315 dummy.u.val = val;
22714f99 1316 strip_funcname (dummy.funcname, funcname);
718e3744 1317 thread_call (&dummy);
1318
1319 return NULL;
1320}