blob: 402167cf1c750f8f7d9e5574afacc7a6da655eca [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"
31
32static struct hash *cpu_record = NULL;
paul718e3742002-12-13 20:15:29 +000033
34/* Struct timeval's tv_usec one second value. */
35#define TIMER_SECOND_MICRO 1000000L
36
37struct timeval
38timeval_adjust (struct timeval a)
39{
40 while (a.tv_usec >= TIMER_SECOND_MICRO)
41 {
42 a.tv_usec -= TIMER_SECOND_MICRO;
43 a.tv_sec++;
44 }
45
46 while (a.tv_usec < 0)
47 {
48 a.tv_usec += TIMER_SECOND_MICRO;
49 a.tv_sec--;
50 }
51
52 if (a.tv_sec < 0)
53 {
54 a.tv_sec = 0;
55 a.tv_usec = 10;
56 }
57
58 if (a.tv_sec > TIMER_SECOND_MICRO)
59 a.tv_sec = TIMER_SECOND_MICRO;
60
61 return a;
62}
63
64static struct timeval
65timeval_subtract (struct timeval a, struct timeval b)
66{
67 struct timeval ret;
68
69 ret.tv_usec = a.tv_usec - b.tv_usec;
70 ret.tv_sec = a.tv_sec - b.tv_sec;
71
72 return timeval_adjust (ret);
73}
74
75static int
76timeval_cmp (struct timeval a, struct timeval b)
77{
78 return (a.tv_sec == b.tv_sec
79 ? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
80}
81
82static unsigned long
83timeval_elapsed (struct timeval a, struct timeval b)
84{
85 return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO)
86 + (a.tv_usec - b.tv_usec));
87}
88
paule04ab742003-01-17 23:47:00 +000089static unsigned int
90cpu_record_hash_key (struct cpu_thread_history *a)
91{
92 return (unsigned int) a->func;
93}
94
95static int
96cpu_record_hash_cmp (struct cpu_thread_history *a,
97 struct cpu_thread_history *b)
98{
99 return a->func == b->func;
100}
101
102static void*
103cpu_record_hash_alloc (struct cpu_thread_history *a)
104{
105 struct cpu_thread_history *new;
106 new = XMALLOC( MTYPE_TMP/*XXX*/, sizeof *new);
107 memset(new, 0, sizeof *new);
108 new->func = a->func;
109 new->funcname = XSTRDUP(MTYPE_TMP/*XXX*/,a->funcname);
110 return new;
111}
112
113static inline void
114vty_out_cpu_thread_history(struct vty* vty,
115 struct cpu_thread_history *a)
116{
117 vty_out(vty, " %7ld.%03ld %9d %8ld %10ld %c%c%c%c%c %s%s",
118 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':' ',
125 a->funcname, VTY_NEWLINE);
126}
127
128static void
129cpu_record_hash_print(struct hash_backet *bucket,
130 void *args[])
131{
132 struct cpu_thread_history *totals = args[0];
133 struct vty *vty = args[1];
134 unsigned char *filter = args[2];
135 struct cpu_thread_history *a = bucket->data;
136
137
138 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"
175 "Display filter (rwtex)\n")
176{
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;
207 default:
208 break;
209 }
210 ++i;
211 }
212 if (filter == 0)
213 {
214 vty_out(vty, "Invalid filter \"%s\" specified, must contain at least one of 'RWTEX'%s",
215 argv[0], VTY_NEWLINE);
216 return CMD_WARNING;
217 }
218 }
219
220 cpu_record_print(vty, filter);
221 return CMD_SUCCESS;
222}
223
paul718e3742002-12-13 20:15:29 +0000224/* List allocation and head/tail print out. */
225static void
226thread_list_debug (struct thread_list *list)
227{
228 printf ("count [%d] head [%p] tail [%p]\n",
229 list->count, list->head, list->tail);
230}
231
232/* Debug print for thread_master. */
233void
234thread_master_debug (struct thread_master *m)
235{
236 printf ("-----------\n");
237 printf ("readlist : ");
238 thread_list_debug (&m->read);
239 printf ("writelist : ");
240 thread_list_debug (&m->write);
241 printf ("timerlist : ");
242 thread_list_debug (&m->timer);
243 printf ("eventlist : ");
244 thread_list_debug (&m->event);
245 printf ("unuselist : ");
246 thread_list_debug (&m->unuse);
247 printf ("total alloc: [%ld]\n", m->alloc);
248 printf ("-----------\n");
249}
250
251/* Allocate new thread master. */
252struct thread_master *
253thread_master_create ()
254{
paule04ab742003-01-17 23:47:00 +0000255 if (cpu_record == NULL)
256 {
257 cpu_record = hash_create_size( 1011, cpu_record_hash_key, cpu_record_hash_cmp);
258 }
paul718e3742002-12-13 20:15:29 +0000259 return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER,
260 sizeof (struct thread_master));
261}
262
263/* Add a new thread to the list. */
264static void
265thread_list_add (struct thread_list *list, struct thread *thread)
266{
267 thread->next = NULL;
268 thread->prev = list->tail;
269 if (list->tail)
270 list->tail->next = thread;
271 else
272 list->head = thread;
273 list->tail = thread;
274 list->count++;
275}
276
277/* Add a new thread just before the point. */
278static void
279thread_list_add_before (struct thread_list *list,
280 struct thread *point,
281 struct thread *thread)
282{
283 thread->next = point;
284 thread->prev = point->prev;
285 if (point->prev)
286 point->prev->next = thread;
287 else
288 list->head = thread;
289 point->prev = thread;
290 list->count++;
291}
292
293/* Delete a thread from the list. */
294static struct thread *
295thread_list_delete (struct thread_list *list, struct thread *thread)
296{
297 if (thread->next)
298 thread->next->prev = thread->prev;
299 else
300 list->tail = thread->prev;
301 if (thread->prev)
302 thread->prev->next = thread->next;
303 else
304 list->head = thread->next;
305 thread->next = thread->prev = NULL;
306 list->count--;
307 return thread;
308}
309
310/* Move thread to unuse list. */
311static void
312thread_add_unuse (struct thread_master *m, struct thread *thread)
313{
314 assert (m != NULL);
315 assert (thread->next == NULL);
316 assert (thread->prev == NULL);
317 assert (thread->type == THREAD_UNUSED);
318 thread_list_add (&m->unuse, thread);
319}
320
321/* Free all unused thread. */
322static void
323thread_list_free (struct thread_master *m, struct thread_list *list)
324{
325 struct thread *t;
326 struct thread *next;
327
328 for (t = list->head; t; t = next)
329 {
330 next = t->next;
pauld5e86ad2003-03-12 05:40:11 +0000331 XFREE (MTYPE_STRVEC, t->funcname);
paul718e3742002-12-13 20:15:29 +0000332 XFREE (MTYPE_THREAD, t);
333 list->count--;
334 m->alloc--;
335 }
336}
337
338/* Stop thread scheduler. */
339void
340thread_master_free (struct thread_master *m)
341{
342 thread_list_free (m, &m->read);
343 thread_list_free (m, &m->write);
344 thread_list_free (m, &m->timer);
345 thread_list_free (m, &m->event);
346 thread_list_free (m, &m->ready);
347 thread_list_free (m, &m->unuse);
348
349 XFREE (MTYPE_THREAD_MASTER, m);
350}
351
352/* Delete top of the list and return it. */
353static struct thread *
354thread_trim_head (struct thread_list *list)
355{
356 if (list->head)
357 return thread_list_delete (list, list->head);
358 return NULL;
359}
360
361/* Thread list is empty or not. */
362int
363thread_empty (struct thread_list *list)
364{
365 return list->head ? 0 : 1;
366}
367
368/* Return remain time in second. */
369unsigned long
370thread_timer_remain_second (struct thread *thread)
371{
372 struct timeval timer_now;
373
374 gettimeofday (&timer_now, NULL);
375
376 if (thread->u.sands.tv_sec - timer_now.tv_sec > 0)
377 return thread->u.sands.tv_sec - timer_now.tv_sec;
378 else
379 return 0;
380}
381
paule04ab742003-01-17 23:47:00 +0000382/* Trim blankspace and "()"s */
383static char *
384strip_funcname (char *funcname)
385{
386 char buff[100];
387 char tmp, *ret, *e, *b = buff;
388
389 strncpy(buff, funcname, sizeof(buff));
390 buff[ sizeof(buff) -1] = '\0';
391 e = buff +strlen(buff) -1;
392
393 /* Wont work for funcname == "Word (explanation)" */
394
395 while (*b == ' ' || *b == '(')
396 ++b;
397 while (*e == ' ' || *e == ')')
398 --e;
399 e++;
400
401 tmp = *e;
402 *e = '\0';
pauld5e86ad2003-03-12 05:40:11 +0000403 ret = XSTRDUP (MTYPE_STRVEC, b);
paule04ab742003-01-17 23:47:00 +0000404 *e = tmp;
405
406 return ret;
407}
408
paul718e3742002-12-13 20:15:29 +0000409/* Get new thread. */
410static struct thread *
411thread_get (struct thread_master *m, u_char type,
paule04ab742003-01-17 23:47:00 +0000412 int (*func) (struct thread *), void *arg, char* funcname)
paul718e3742002-12-13 20:15:29 +0000413{
414 struct thread *thread;
415
416 if (m->unuse.head)
paul2946f652003-03-27 23:48:24 +0000417 {
418 thread = thread_trim_head (&m->unuse);
419 if (thread->funcname)
420 XFREE(MTYPE_STRVEC, thread->funcname);
421 }
paul718e3742002-12-13 20:15:29 +0000422 else
423 {
424 thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
425 m->alloc++;
426 }
427 thread->type = type;
paule04ab742003-01-17 23:47:00 +0000428 thread->add_type = type;
paul718e3742002-12-13 20:15:29 +0000429 thread->master = m;
430 thread->func = func;
431 thread->arg = arg;
432
paule04ab742003-01-17 23:47:00 +0000433 thread->funcname = strip_funcname(funcname);
434
paul718e3742002-12-13 20:15:29 +0000435 return thread;
436}
437
438/* Add new read thread. */
439struct thread *
paule04ab742003-01-17 23:47:00 +0000440funcname_thread_add_read (struct thread_master *m,
441 int (*func) (struct thread *), void *arg, int fd, char* funcname)
paul718e3742002-12-13 20:15:29 +0000442{
443 struct thread *thread;
444
445 assert (m != NULL);
446
447 if (FD_ISSET (fd, &m->readfd))
448 {
449 zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
450 return NULL;
451 }
452
paule04ab742003-01-17 23:47:00 +0000453 thread = thread_get (m, THREAD_READ, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000454 FD_SET (fd, &m->readfd);
455 thread->u.fd = fd;
456 thread_list_add (&m->read, thread);
457
458 return thread;
459}
460
461/* Add new write thread. */
462struct thread *
paule04ab742003-01-17 23:47:00 +0000463funcname_thread_add_write (struct thread_master *m,
464 int (*func) (struct thread *), void *arg, int fd, char* funcname)
paul718e3742002-12-13 20:15:29 +0000465{
466 struct thread *thread;
467
468 assert (m != NULL);
469
470 if (FD_ISSET (fd, &m->writefd))
471 {
472 zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
473 return NULL;
474 }
475
paule04ab742003-01-17 23:47:00 +0000476 thread = thread_get (m, THREAD_WRITE, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000477 FD_SET (fd, &m->writefd);
478 thread->u.fd = fd;
479 thread_list_add (&m->write, thread);
480
481 return thread;
482}
483
484/* Add timer event thread. */
485struct thread *
paule04ab742003-01-17 23:47:00 +0000486funcname_thread_add_timer (struct thread_master *m,
487 int (*func) (struct thread *), void *arg, long timer, char* funcname)
paul718e3742002-12-13 20:15:29 +0000488{
489 struct timeval timer_now;
490 struct thread *thread;
491#ifndef TIMER_NO_SORT
492 struct thread *tt;
493#endif /* TIMER_NO_SORT */
494
495 assert (m != NULL);
496
paule04ab742003-01-17 23:47:00 +0000497 thread = thread_get (m, THREAD_TIMER, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000498
499 /* Do we need jitter here? */
500 gettimeofday (&timer_now, NULL);
501 timer_now.tv_sec += timer;
502 thread->u.sands = timer_now;
503
504 /* Sort by timeval. */
505#ifdef TIMER_NO_SORT
506 thread_list_add (&m->timer, thread);
507#else
508 for (tt = m->timer.head; tt; tt = tt->next)
509 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
510 break;
511
512 if (tt)
513 thread_list_add_before (&m->timer, tt, thread);
514 else
515 thread_list_add (&m->timer, thread);
516#endif /* TIMER_NO_SORT */
517
518 return thread;
519}
520
521/* Add simple event thread. */
522struct thread *
paule04ab742003-01-17 23:47:00 +0000523funcname_thread_add_event (struct thread_master *m,
524 int (*func) (struct thread *), void *arg, int val, char* funcname)
paul718e3742002-12-13 20:15:29 +0000525{
526 struct thread *thread;
527
528 assert (m != NULL);
529
paule04ab742003-01-17 23:47:00 +0000530 thread = thread_get (m, THREAD_EVENT, func, arg, funcname);
paul718e3742002-12-13 20:15:29 +0000531 thread->u.val = val;
532 thread_list_add (&m->event, thread);
533
534 return thread;
535}
536
537/* Cancel thread from scheduler. */
538void
539thread_cancel (struct thread *thread)
540{
541 switch (thread->type)
542 {
543 case THREAD_READ:
544 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
545 FD_CLR (thread->u.fd, &thread->master->readfd);
546 thread_list_delete (&thread->master->read, thread);
547 break;
548 case THREAD_WRITE:
549 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
550 FD_CLR (thread->u.fd, &thread->master->writefd);
551 thread_list_delete (&thread->master->write, thread);
552 break;
553 case THREAD_TIMER:
554 thread_list_delete (&thread->master->timer, thread);
555 break;
556 case THREAD_EVENT:
557 thread_list_delete (&thread->master->event, thread);
558 break;
559 case THREAD_READY:
560 thread_list_delete (&thread->master->ready, thread);
561 break;
562 default:
563 break;
564 }
565 thread->type = THREAD_UNUSED;
566 thread_add_unuse (thread->master, thread);
567}
568
569/* Delete all events which has argument value arg. */
570void
571thread_cancel_event (struct thread_master *m, void *arg)
572{
573 struct thread *thread;
574
575 thread = m->event.head;
576 while (thread)
577 {
578 struct thread *t;
579
580 t = thread;
581 thread = t->next;
582
583 if (t->arg == arg)
584 {
585 thread_list_delete (&m->event, t);
586 t->type = THREAD_UNUSED;
587 thread_add_unuse (m, t);
588 }
589 }
590}
591
592#ifdef TIMER_NO_SORT
593struct timeval *
594thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
595{
596 struct timeval timer_now;
597 struct timeval timer_min;
598 struct timeval *timer_wait;
599
600 gettimeofday (&timer_now, NULL);
601
602 timer_wait = NULL;
603 for (thread = m->timer.head; thread; thread = thread->next)
604 {
605 if (! timer_wait)
606 timer_wait = &thread->u.sands;
607 else if (timeval_cmp (thread->u.sands, *timer_wait) < 0)
608 timer_wait = &thread->u.sands;
609 }
610
611 if (m->timer.head)
612 {
613 timer_min = *timer_wait;
614 timer_min = timeval_subtract (timer_min, timer_now);
615 if (timer_min.tv_sec < 0)
616 {
617 timer_min.tv_sec = 0;
618 timer_min.tv_usec = 10;
619 }
620 timer_wait = &timer_min;
621 }
622 else
623 timer_wait = NULL;
624
625 if (timer_wait)
626 {
627 *timer_val = timer_wait;
628 return timer_val;
629 }
630 return NULL;
631}
632#else /* ! TIMER_NO_SORT */
633struct timeval *
634thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
635{
636 struct timeval timer_now;
637 struct timeval timer_min;
638
639 if (m->timer.head)
640 {
641 gettimeofday (&timer_now, NULL);
642 timer_min = m->timer.head->u.sands;
643 timer_min = timeval_subtract (timer_min, timer_now);
644 if (timer_min.tv_sec < 0)
645 {
646 timer_min.tv_sec = 0;
647 timer_min.tv_usec = 10;
648 }
649 *timer_val = timer_min;
650 return timer_val;
651 }
652 return NULL;
653}
654#endif /* TIMER_NO_SORT */
655
656struct thread *
657thread_run (struct thread_master *m, struct thread *thread,
658 struct thread *fetch)
659{
660 *fetch = *thread;
661 thread->type = THREAD_UNUSED;
662 thread_add_unuse (m, thread);
663 return fetch;
664}
665
666int
667thread_process_fd (struct thread_master *m, struct thread_list *list,
668 fd_set *fdset, fd_set *mfdset)
669{
670 struct thread *thread;
671 struct thread *next;
672 int ready = 0;
673
674 for (thread = list->head; thread; thread = next)
675 {
676 next = thread->next;
677
678 if (FD_ISSET (THREAD_FD (thread), fdset))
679 {
680 assert (FD_ISSET (THREAD_FD (thread), mfdset));
681 FD_CLR(THREAD_FD (thread), mfdset);
682 thread_list_delete (list, thread);
683 thread_list_add (&m->ready, thread);
684 thread->type = THREAD_READY;
685 ready++;
686 }
687 }
688 return ready;
689}
690
691/* Fetch next ready thread. */
692struct thread *
693thread_fetch (struct thread_master *m, struct thread *fetch)
694{
695 int num;
696 int ready;
697 struct thread *thread;
698 fd_set readfd;
699 fd_set writefd;
700 fd_set exceptfd;
701 struct timeval timer_now;
702 struct timeval timer_val;
703 struct timeval *timer_wait;
704 struct timeval timer_nowait;
705
706 timer_nowait.tv_sec = 0;
707 timer_nowait.tv_usec = 0;
708
709 while (1)
710 {
711 /* Normal event is the highest priority. */
712 if ((thread = thread_trim_head (&m->event)) != NULL)
713 return thread_run (m, thread, fetch);
714
715 /* Execute timer. */
716 gettimeofday (&timer_now, NULL);
717
718 for (thread = m->timer.head; thread; thread = thread->next)
719 if (timeval_cmp (timer_now, thread->u.sands) >= 0)
720 {
721 thread_list_delete (&m->timer, thread);
722 return thread_run (m, thread, fetch);
723 }
724
725 /* If there are any ready threads, process top of them. */
726 if ((thread = thread_trim_head (&m->ready)) != NULL)
727 return thread_run (m, thread, fetch);
728
729 /* Structure copy. */
730 readfd = m->readfd;
731 writefd = m->writefd;
732 exceptfd = m->exceptfd;
733
734 /* Calculate select wait timer. */
735 timer_wait = thread_timer_wait (m, &timer_val);
736
737 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
738
739 if (num == 0)
740 continue;
741
742 if (num < 0)
743 {
744 if (errno == EINTR)
745 continue;
746
747 zlog_warn ("select() error: %s", strerror (errno));
748 return NULL;
749 }
750
751 /* Normal priority read thead. */
752 ready = thread_process_fd (m, &m->read, &readfd, &m->readfd);
753
754 /* Write thead. */
755 ready = thread_process_fd (m, &m->write, &writefd, &m->writefd);
756
757 if ((thread = thread_trim_head (&m->ready)) != NULL)
758 return thread_run (m, thread, fetch);
759 }
760}
761
762static unsigned long
763thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
764{
765 unsigned long thread_time;
766
767#ifdef HAVE_RUSAGE
768 /* This is 'user + sys' time. */
769 thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
770 thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
771#else
772 /* When rusage is not available, simple elapsed time is used. */
773 thread_time = timeval_elapsed (*now, *start);
774#endif /* HAVE_RUSAGE */
775
776 return thread_time;
777}
778
779/* We should aim to yield after THREAD_YIELD_TIME_SLOT
780 milliseconds. */
781int
782thread_should_yield (struct thread *thread)
783{
784 RUSAGE_T ru;
785
786 GETRUSAGE (&ru);
787
788 if (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT)
789 return 1;
790 else
791 return 0;
792}
793
794/* We check thread consumed time. If the system has getrusage, we'll
795 use that to get indepth stats on the performance of the thread. If
796 not - we'll use gettimeofday for some guestimation. */
797void
798thread_call (struct thread *thread)
799{
800 unsigned long thread_time;
801 RUSAGE_T ru;
paule04ab742003-01-17 23:47:00 +0000802 struct cpu_thread_history tmp, *cpu;
803
804 tmp.func = thread->func;
805 tmp.funcname = thread->funcname;
806 cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc);
paul718e3742002-12-13 20:15:29 +0000807
808 GETRUSAGE (&thread->ru);
809
810 (*thread->func) (thread);
811
812 GETRUSAGE (&ru);
813
814 thread_time = thread_consumed_time (&ru, &thread->ru);
paule04ab742003-01-17 23:47:00 +0000815 cpu->total += thread_time;
816 if (cpu->max < thread_time)
817 cpu->max = thread_time;
818
819 ++cpu->total_calls;
820 cpu->types |= (1 << thread->add_type);
paul718e3742002-12-13 20:15:29 +0000821
822#ifdef THREAD_CONSUMED_TIME_CHECK
823 if (thread_time > 200000L)
824 {
825 /*
826 * We have a CPU Hog on our hands.
827 * Whinge about it now, so we're aware this is yet another task
828 * to fix.
829 */
paule04ab742003-01-17 23:47:00 +0000830 zlog_err ("CPU HOG task %s (%lx) ran for %ldms",
831 thread->funcname,
paul718e3742002-12-13 20:15:29 +0000832 (unsigned long) thread->func,
833 thread_time / 1000L);
834 }
835#endif /* THREAD_CONSUMED_TIME_CHECK */
836}
837
838/* Execute thread */
839struct thread *
paule04ab742003-01-17 23:47:00 +0000840funcname_thread_execute (struct thread_master *m,
paul718e3742002-12-13 20:15:29 +0000841 int (*func)(struct thread *),
842 void *arg,
paule04ab742003-01-17 23:47:00 +0000843 int val,
844 char* funcname)
paul718e3742002-12-13 20:15:29 +0000845{
846 struct thread dummy;
847
848 memset (&dummy, 0, sizeof (struct thread));
849
850 dummy.type = THREAD_EVENT;
paule04ab742003-01-17 23:47:00 +0000851 dummy.add_type = THREAD_EXECUTE;
paul718e3742002-12-13 20:15:29 +0000852 dummy.master = NULL;
853 dummy.func = func;
854 dummy.arg = arg;
855 dummy.u.val = val;
paule04ab742003-01-17 23:47:00 +0000856 dummy.funcname = strip_funcname (funcname);
paul718e3742002-12-13 20:15:29 +0000857 thread_call (&dummy);
858
paul2946f652003-03-27 23:48:24 +0000859 XFREE (MTYPE_STRVEC, dummy.funcname);
860
paul718e3742002-12-13 20:15:29 +0000861 return NULL;
862}