blob: af954b838d434bbd06953f1e8404eaeefcbdc24b [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +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"
29
30/* Struct timeval's tv_usec one second value. */
31#define TIMER_SECOND_MICRO 1000000L
32
33struct timeval
34timeval_adjust (struct timeval a)
35{
36 while (a.tv_usec >= TIMER_SECOND_MICRO)
37 {
38 a.tv_usec -= TIMER_SECOND_MICRO;
39 a.tv_sec++;
40 }
41
42 while (a.tv_usec < 0)
43 {
44 a.tv_usec += TIMER_SECOND_MICRO;
45 a.tv_sec--;
46 }
47
48 if (a.tv_sec < 0)
49 {
50 a.tv_sec = 0;
51 a.tv_usec = 10;
52 }
53
54 if (a.tv_sec > TIMER_SECOND_MICRO)
55 a.tv_sec = TIMER_SECOND_MICRO;
56
57 return a;
58}
59
60static struct timeval
61timeval_subtract (struct timeval a, struct timeval b)
62{
63 struct timeval ret;
64
65 ret.tv_usec = a.tv_usec - b.tv_usec;
66 ret.tv_sec = a.tv_sec - b.tv_sec;
67
68 return timeval_adjust (ret);
69}
70
71static int
72timeval_cmp (struct timeval a, struct timeval b)
73{
74 return (a.tv_sec == b.tv_sec
75 ? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
76}
77
78static unsigned long
79timeval_elapsed (struct timeval a, struct timeval b)
80{
81 return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO)
82 + (a.tv_usec - b.tv_usec));
83}
84
85/* List allocation and head/tail print out. */
86static void
87thread_list_debug (struct thread_list *list)
88{
89 printf ("count [%d] head [%p] tail [%p]\n",
90 list->count, list->head, list->tail);
91}
92
93/* Debug print for thread_master. */
94void
95thread_master_debug (struct thread_master *m)
96{
97 printf ("-----------\n");
98 printf ("readlist : ");
99 thread_list_debug (&m->read);
100 printf ("writelist : ");
101 thread_list_debug (&m->write);
102 printf ("timerlist : ");
103 thread_list_debug (&m->timer);
104 printf ("eventlist : ");
105 thread_list_debug (&m->event);
106 printf ("unuselist : ");
107 thread_list_debug (&m->unuse);
108 printf ("total alloc: [%ld]\n", m->alloc);
109 printf ("-----------\n");
110}
111
112/* Allocate new thread master. */
113struct thread_master *
114thread_master_create ()
115{
116 return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER,
117 sizeof (struct thread_master));
118}
119
120/* Add a new thread to the list. */
121static void
122thread_list_add (struct thread_list *list, struct thread *thread)
123{
124 thread->next = NULL;
125 thread->prev = list->tail;
126 if (list->tail)
127 list->tail->next = thread;
128 else
129 list->head = thread;
130 list->tail = thread;
131 list->count++;
132}
133
134/* Add a new thread just before the point. */
135static void
136thread_list_add_before (struct thread_list *list,
137 struct thread *point,
138 struct thread *thread)
139{
140 thread->next = point;
141 thread->prev = point->prev;
142 if (point->prev)
143 point->prev->next = thread;
144 else
145 list->head = thread;
146 point->prev = thread;
147 list->count++;
148}
149
150/* Delete a thread from the list. */
151static struct thread *
152thread_list_delete (struct thread_list *list, struct thread *thread)
153{
154 if (thread->next)
155 thread->next->prev = thread->prev;
156 else
157 list->tail = thread->prev;
158 if (thread->prev)
159 thread->prev->next = thread->next;
160 else
161 list->head = thread->next;
162 thread->next = thread->prev = NULL;
163 list->count--;
164 return thread;
165}
166
167/* Move thread to unuse list. */
168static void
169thread_add_unuse (struct thread_master *m, struct thread *thread)
170{
171 assert (m != NULL);
172 assert (thread->next == NULL);
173 assert (thread->prev == NULL);
174 assert (thread->type == THREAD_UNUSED);
175 thread_list_add (&m->unuse, thread);
176}
177
178/* Free all unused thread. */
179static void
180thread_list_free (struct thread_master *m, struct thread_list *list)
181{
182 struct thread *t;
183 struct thread *next;
184
185 for (t = list->head; t; t = next)
186 {
187 next = t->next;
188 XFREE (MTYPE_THREAD, t);
189 list->count--;
190 m->alloc--;
191 }
192}
193
194/* Stop thread scheduler. */
195void
196thread_master_free (struct thread_master *m)
197{
198 thread_list_free (m, &m->read);
199 thread_list_free (m, &m->write);
200 thread_list_free (m, &m->timer);
201 thread_list_free (m, &m->event);
202 thread_list_free (m, &m->ready);
203 thread_list_free (m, &m->unuse);
204
205 XFREE (MTYPE_THREAD_MASTER, m);
206}
207
208/* Delete top of the list and return it. */
209static struct thread *
210thread_trim_head (struct thread_list *list)
211{
212 if (list->head)
213 return thread_list_delete (list, list->head);
214 return NULL;
215}
216
217/* Thread list is empty or not. */
218int
219thread_empty (struct thread_list *list)
220{
221 return list->head ? 0 : 1;
222}
223
224/* Return remain time in second. */
225unsigned long
226thread_timer_remain_second (struct thread *thread)
227{
228 struct timeval timer_now;
229
230 gettimeofday (&timer_now, NULL);
231
232 if (thread->u.sands.tv_sec - timer_now.tv_sec > 0)
233 return thread->u.sands.tv_sec - timer_now.tv_sec;
234 else
235 return 0;
236}
237
238/* Get new thread. */
239static struct thread *
240thread_get (struct thread_master *m, u_char type,
241 int (*func) (struct thread *), void *arg)
242{
243 struct thread *thread;
244
245 if (m->unuse.head)
246 thread = thread_trim_head (&m->unuse);
247 else
248 {
249 thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread));
250 m->alloc++;
251 }
252 thread->type = type;
253 thread->master = m;
254 thread->func = func;
255 thread->arg = arg;
256
257 return thread;
258}
259
260/* Add new read thread. */
261struct thread *
262thread_add_read (struct thread_master *m,
263 int (*func) (struct thread *), void *arg, int fd)
264{
265 struct thread *thread;
266
267 assert (m != NULL);
268
269 if (FD_ISSET (fd, &m->readfd))
270 {
271 zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd);
272 return NULL;
273 }
274
275 thread = thread_get (m, THREAD_READ, func, arg);
276 FD_SET (fd, &m->readfd);
277 thread->u.fd = fd;
278 thread_list_add (&m->read, thread);
279
280 return thread;
281}
282
283/* Add new write thread. */
284struct thread *
285thread_add_write (struct thread_master *m,
286 int (*func) (struct thread *), void *arg, int fd)
287{
288 struct thread *thread;
289
290 assert (m != NULL);
291
292 if (FD_ISSET (fd, &m->writefd))
293 {
294 zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd);
295 return NULL;
296 }
297
298 thread = thread_get (m, THREAD_WRITE, func, arg);
299 FD_SET (fd, &m->writefd);
300 thread->u.fd = fd;
301 thread_list_add (&m->write, thread);
302
303 return thread;
304}
305
306/* Add timer event thread. */
307struct thread *
308thread_add_timer (struct thread_master *m,
309 int (*func) (struct thread *), void *arg, long timer)
310{
311 struct timeval timer_now;
312 struct thread *thread;
313#ifndef TIMER_NO_SORT
314 struct thread *tt;
315#endif /* TIMER_NO_SORT */
316
317 assert (m != NULL);
318
319 thread = thread_get (m, THREAD_TIMER, func, arg);
320
321 /* Do we need jitter here? */
322 gettimeofday (&timer_now, NULL);
323 timer_now.tv_sec += timer;
324 thread->u.sands = timer_now;
325
326 /* Sort by timeval. */
327#ifdef TIMER_NO_SORT
328 thread_list_add (&m->timer, thread);
329#else
330 for (tt = m->timer.head; tt; tt = tt->next)
331 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
332 break;
333
334 if (tt)
335 thread_list_add_before (&m->timer, tt, thread);
336 else
337 thread_list_add (&m->timer, thread);
338#endif /* TIMER_NO_SORT */
339
340 return thread;
341}
342
343/* Add timer event thread with "millisecond" resolution */
344struct thread *
345thread_add_timer_msec (struct thread_master *m,
346 int (*func)(struct thread *),
347 void *arg, long timer)
348{
349 struct timeval timer_now;
350 struct thread *thread;
351#ifndef TIMER_NO_SORT
352 struct thread *tt;
353#endif /* TIMER_NO_SORT */
354
355 assert (m != NULL);
356
357 thread = thread_get (m, THREAD_TIMER, func, arg);
358
359 timer = 1000*timer; /* milli -> micro */
360
361 gettimeofday (&timer_now, NULL);
362 timer_now.tv_sec += timer / TIMER_SECOND_MICRO;
363 timer_now.tv_usec += (timer % TIMER_SECOND_MICRO);
364 thread->u.sands = timer_now;
365
366
367 /* Sort by timeval. */
368#ifdef TIMER_NO_SORT
369 thread_list_add (&m->timer, thread);
370#else
371 for (tt = m->timer.head; tt; tt = tt->next)
372 if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0)
373 break;
374
375 if (tt)
376 thread_list_add_before (&m->timer, tt, thread);
377 else
378 thread_list_add (&m->timer, thread);
379#endif /* TIMER_NO_SORT */
380
381 return thread;
382}
383
384
385/* Add simple event thread. */
386struct thread *
387thread_add_event (struct thread_master *m,
388 int (*func) (struct thread *), void *arg, int val)
389{
390 struct thread *thread;
391
392 assert (m != NULL);
393
394 thread = thread_get (m, THREAD_EVENT, func, arg);
395 thread->u.val = val;
396 thread_list_add (&m->event, thread);
397
398 return thread;
399}
400
401/* Cancel thread from scheduler. */
402void
403thread_cancel (struct thread *thread)
404{
405 switch (thread->type)
406 {
407 case THREAD_READ:
408 assert (FD_ISSET (thread->u.fd, &thread->master->readfd));
409 FD_CLR (thread->u.fd, &thread->master->readfd);
410 thread_list_delete (&thread->master->read, thread);
411 break;
412 case THREAD_WRITE:
413 assert (FD_ISSET (thread->u.fd, &thread->master->writefd));
414 FD_CLR (thread->u.fd, &thread->master->writefd);
415 thread_list_delete (&thread->master->write, thread);
416 break;
417 case THREAD_TIMER:
418 thread_list_delete (&thread->master->timer, thread);
419 break;
420 case THREAD_EVENT:
421 thread_list_delete (&thread->master->event, thread);
422 break;
423 case THREAD_READY:
424 thread_list_delete (&thread->master->ready, thread);
425 break;
426 case THREAD_UNUSED:
427 thread_list_delete (&thread->master->unuse, thread);
428 break;
429 default:
430 break;
431 }
432 thread->type = THREAD_UNUSED;
433 thread_add_unuse (thread->master, thread);
434}
435
436/* Delete all events which has argument value arg. */
437void
438thread_cancel_event (struct thread_master *m, void *arg)
439{
440 struct thread *thread;
441
442 thread = m->event.head;
443 while (thread)
444 {
445 struct thread *t;
446
447 t = thread;
448 thread = t->next;
449
450 if (t->arg == arg)
451 {
452 thread_list_delete (&m->event, t);
453 t->type = THREAD_UNUSED;
454 thread_add_unuse (m, t);
455 }
456 }
457}
458
459#ifdef TIMER_NO_SORT
460struct timeval *
461thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
462{
463 struct timeval timer_now;
464 struct timeval timer_min;
465 struct timeval *timer_wait;
466
467 gettimeofday (&timer_now, NULL);
468
469 timer_wait = NULL;
470 for (thread = m->timer.head; thread; thread = thread->next)
471 {
472 if (! timer_wait)
473 timer_wait = &thread->u.sands;
474 else if (timeval_cmp (thread->u.sands, *timer_wait) < 0)
475 timer_wait = &thread->u.sands;
476 }
477
478 if (m->timer.head)
479 {
480 timer_min = *timer_wait;
481 timer_min = timeval_subtract (timer_min, timer_now);
482 if (timer_min.tv_sec < 0)
483 {
484 timer_min.tv_sec = 0;
485 timer_min.tv_usec = 10;
486 }
487 timer_wait = &timer_min;
488 }
489 else
490 timer_wait = NULL;
491
492 if (timer_wait)
493 {
494 *timer_val = timer_wait;
495 return timer_val;
496 }
497 return NULL;
498}
499#else /* ! TIMER_NO_SORT */
500struct timeval *
501thread_timer_wait (struct thread_master *m, struct timeval *timer_val)
502{
503 struct timeval timer_now;
504 struct timeval timer_min;
505
506 if (m->timer.head)
507 {
508 gettimeofday (&timer_now, NULL);
509 timer_min = m->timer.head->u.sands;
510 timer_min = timeval_subtract (timer_min, timer_now);
511 if (timer_min.tv_sec < 0)
512 {
513 timer_min.tv_sec = 0;
514 timer_min.tv_usec = 10;
515 }
516 *timer_val = timer_min;
517 return timer_val;
518 }
519 return NULL;
520}
521#endif /* TIMER_NO_SORT */
522
523struct thread *
524thread_run (struct thread_master *m, struct thread *thread,
525 struct thread *fetch)
526{
527 *fetch = *thread;
528 thread->type = THREAD_UNUSED;
529 thread_add_unuse (m, thread);
530 return fetch;
531}
532
533int
534thread_process_fd (struct thread_master *m, struct thread_list *list,
535 fd_set *fdset, fd_set *mfdset)
536{
537 struct thread *thread;
538 struct thread *next;
539 int ready = 0;
540
541 for (thread = list->head; thread; thread = next)
542 {
543 next = thread->next;
544
545 if (FD_ISSET (THREAD_FD (thread), fdset))
546 {
547 assert (FD_ISSET (THREAD_FD (thread), mfdset));
548 FD_CLR(THREAD_FD (thread), mfdset);
549 thread_list_delete (list, thread);
550 thread_list_add (&m->ready, thread);
551 thread->type = THREAD_READY;
552 ready++;
553 }
554 }
555 return ready;
556}
557
558/* Fetch next ready thread. */
559struct thread *
560thread_fetch (struct thread_master *m, struct thread *fetch)
561{
562 int num;
563 int ready;
564 struct thread *thread;
565 fd_set readfd;
566 fd_set writefd;
567 fd_set exceptfd;
568 struct timeval timer_now;
569 struct timeval timer_val;
570 struct timeval *timer_wait;
571 struct timeval timer_nowait;
572
573 timer_nowait.tv_sec = 0;
574 timer_nowait.tv_usec = 0;
575
576 while (1)
577 {
578 /* Normal event is the highest priority. */
579 if ((thread = thread_trim_head (&m->event)) != NULL)
580 return thread_run (m, thread, fetch);
581
582 /* Execute timer. */
583 gettimeofday (&timer_now, NULL);
584
585 for (thread = m->timer.head; thread; thread = thread->next)
586 if (timeval_cmp (timer_now, thread->u.sands) >= 0)
587 {
588 thread_list_delete (&m->timer, thread);
589 return thread_run (m, thread, fetch);
590 }
591
592 /* If there are any ready threads, process top of them. */
593 if ((thread = thread_trim_head (&m->ready)) != NULL)
594 return thread_run (m, thread, fetch);
595
596 /* Structure copy. */
597 readfd = m->readfd;
598 writefd = m->writefd;
599 exceptfd = m->exceptfd;
600
601 /* Calculate select wait timer. */
602 timer_wait = thread_timer_wait (m, &timer_val);
603
604 num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
605
606 if (num == 0)
607 continue;
608
609 if (num < 0)
610 {
611 if (errno == EINTR)
612 continue;
613
614 zlog_warn ("select() error: %s", strerror (errno));
615 return NULL;
616 }
617
618 /* Normal priority read thead. */
619 ready = thread_process_fd (m, &m->read, &readfd, &m->readfd);
620
621 /* Write thead. */
622 ready = thread_process_fd (m, &m->write, &writefd, &m->writefd);
623
624 if ((thread = thread_trim_head (&m->ready)) != NULL)
625 return thread_run (m, thread, fetch);
626 }
627}
628
629static unsigned long
630thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start)
631{
632 unsigned long thread_time;
633
634#ifdef HAVE_RUSAGE
635 /* This is 'user + sys' time. */
636 thread_time = timeval_elapsed (now->ru_utime, start->ru_utime);
637 thread_time += timeval_elapsed (now->ru_stime, start->ru_stime);
638#else
639 /* When rusage is not available, simple elapsed time is used. */
640 thread_time = timeval_elapsed (*now, *start);
641#endif /* HAVE_RUSAGE */
642
643 return thread_time;
644}
645
646/* We should aim to yield after THREAD_YIELD_TIME_SLOT
647 milliseconds. */
648int
649thread_should_yield (struct thread *thread)
650{
651 RUSAGE_T ru;
652
653 GETRUSAGE (&ru);
654
655 if (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT)
656 return 1;
657 else
658 return 0;
659}
660
661/* We check thread consumed time. If the system has getrusage, we'll
662 use that to get indepth stats on the performance of the thread. If
663 not - we'll use gettimeofday for some guestimation. */
664void
665thread_call (struct thread *thread)
666{
667 unsigned long thread_time;
668 RUSAGE_T ru;
669
670 GETRUSAGE (&thread->ru);
671
672 (*thread->func) (thread);
673
674 GETRUSAGE (&ru);
675
676 thread_time = thread_consumed_time (&ru, &thread->ru);
677
678#ifdef THREAD_CONSUMED_TIME_CHECK
679 if (thread_time > 200000L)
680 {
681 /*
682 * We have a CPU Hog on our hands.
683 * Whinge about it now, so we're aware this is yet another task
684 * to fix.
685 */
686 zlog_err ("CPU HOG task %lx ran for %ldms",
687 /* FIXME: report the name of the function somehow */
688 (unsigned long) thread->func,
689 thread_time / 1000L);
690 }
691#endif /* THREAD_CONSUMED_TIME_CHECK */
692}
693
694/* Execute thread */
695struct thread *
696thread_execute (struct thread_master *m,
697 int (*func)(struct thread *),
698 void *arg,
699 int val)
700{
701 struct thread dummy;
702
703 memset (&dummy, 0, sizeof (struct thread));
704
705 dummy.type = THREAD_EVENT;
706 dummy.master = NULL;
707 dummy.func = func;
708 dummy.arg = arg;
709 dummy.u.val = val;
710 thread_call (&dummy);
711
712 return NULL;
713}