paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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" |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 29 | #include "hash.h" |
| 30 | #include "command.h" |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 31 | #include "sigevent.h" |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 32 | |
| 33 | static struct hash *cpu_record = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
| 35 | /* Struct timeval's tv_usec one second value. */ |
| 36 | #define TIMER_SECOND_MICRO 1000000L |
| 37 | |
| 38 | struct timeval |
| 39 | timeval_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 | |
| 65 | static struct timeval |
| 66 | timeval_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 | |
| 76 | static int |
| 77 | timeval_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 | |
| 83 | static unsigned long |
| 84 | timeval_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 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 90 | static unsigned int |
| 91 | cpu_record_hash_key (struct cpu_thread_history *a) |
| 92 | { |
| 93 | return (unsigned int) a->func; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | cpu_record_hash_cmp (struct cpu_thread_history *a, |
| 98 | struct cpu_thread_history *b) |
| 99 | { |
| 100 | return a->func == b->func; |
| 101 | } |
| 102 | |
| 103 | static void* |
| 104 | cpu_record_hash_alloc (struct cpu_thread_history *a) |
| 105 | { |
| 106 | struct cpu_thread_history *new; |
paul | 039b957 | 2004-10-31 16:43:17 +0000 | [diff] [blame] | 107 | new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history)); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 108 | new->func = a->func; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 109 | new->funcname = XSTRDUP(MTYPE_THREAD_FUNCNAME, a->funcname); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 110 | return new; |
| 111 | } |
| 112 | |
| 113 | static inline void |
| 114 | vty_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 | |
| 128 | static void |
| 129 | cpu_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 | |
| 148 | static void |
| 149 | cpu_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 | |
| 169 | DEFUN(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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | /* List allocation and head/tail print out. */ |
| 225 | static void |
| 226 | thread_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. */ |
| 233 | void |
| 234 | thread_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. */ |
| 252 | struct thread_master * |
| 253 | thread_master_create () |
| 254 | { |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 255 | if (cpu_record == NULL) |
| 256 | { |
| 257 | cpu_record = hash_create_size( 1011, cpu_record_hash_key, cpu_record_hash_cmp); |
| 258 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER, |
| 260 | sizeof (struct thread_master)); |
| 261 | } |
| 262 | |
| 263 | /* Add a new thread to the list. */ |
| 264 | static void |
| 265 | thread_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. */ |
| 278 | static void |
| 279 | thread_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. */ |
| 294 | static struct thread * |
| 295 | thread_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. */ |
| 311 | static void |
| 312 | thread_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); |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 319 | /* XXX: Should we deallocate funcname here? */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* Free all unused thread. */ |
| 323 | static void |
| 324 | thread_list_free (struct thread_master *m, struct thread_list *list) |
| 325 | { |
| 326 | struct thread *t; |
| 327 | struct thread *next; |
| 328 | |
| 329 | for (t = list->head; t; t = next) |
| 330 | { |
| 331 | next = t->next; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 332 | XFREE (MTYPE_THREAD_FUNCNAME, t->funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | XFREE (MTYPE_THREAD, t); |
| 334 | list->count--; |
| 335 | m->alloc--; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* Stop thread scheduler. */ |
| 340 | void |
| 341 | thread_master_free (struct thread_master *m) |
| 342 | { |
| 343 | thread_list_free (m, &m->read); |
| 344 | thread_list_free (m, &m->write); |
| 345 | thread_list_free (m, &m->timer); |
| 346 | thread_list_free (m, &m->event); |
| 347 | thread_list_free (m, &m->ready); |
| 348 | thread_list_free (m, &m->unuse); |
| 349 | |
| 350 | XFREE (MTYPE_THREAD_MASTER, m); |
| 351 | } |
| 352 | |
| 353 | /* Delete top of the list and return it. */ |
| 354 | static struct thread * |
| 355 | thread_trim_head (struct thread_list *list) |
| 356 | { |
| 357 | if (list->head) |
| 358 | return thread_list_delete (list, list->head); |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | /* Thread list is empty or not. */ |
| 363 | int |
| 364 | thread_empty (struct thread_list *list) |
| 365 | { |
| 366 | return list->head ? 0 : 1; |
| 367 | } |
| 368 | |
| 369 | /* Return remain time in second. */ |
| 370 | unsigned long |
| 371 | thread_timer_remain_second (struct thread *thread) |
| 372 | { |
| 373 | struct timeval timer_now; |
| 374 | |
| 375 | gettimeofday (&timer_now, NULL); |
| 376 | |
| 377 | if (thread->u.sands.tv_sec - timer_now.tv_sec > 0) |
| 378 | return thread->u.sands.tv_sec - timer_now.tv_sec; |
| 379 | else |
| 380 | return 0; |
| 381 | } |
| 382 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 383 | /* Trim blankspace and "()"s */ |
| 384 | static char * |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 385 | strip_funcname (const char *funcname) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 386 | { |
| 387 | char buff[100]; |
| 388 | char tmp, *ret, *e, *b = buff; |
| 389 | |
| 390 | strncpy(buff, funcname, sizeof(buff)); |
| 391 | buff[ sizeof(buff) -1] = '\0'; |
| 392 | e = buff +strlen(buff) -1; |
| 393 | |
| 394 | /* Wont work for funcname == "Word (explanation)" */ |
| 395 | |
| 396 | while (*b == ' ' || *b == '(') |
| 397 | ++b; |
| 398 | while (*e == ' ' || *e == ')') |
| 399 | --e; |
| 400 | e++; |
| 401 | |
| 402 | tmp = *e; |
| 403 | *e = '\0'; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 404 | ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 405 | *e = tmp; |
| 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 410 | /* Get new thread. */ |
| 411 | static struct thread * |
| 412 | thread_get (struct thread_master *m, u_char type, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 413 | int (*func) (struct thread *), void *arg, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | { |
| 415 | struct thread *thread; |
| 416 | |
| 417 | if (m->unuse.head) |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 418 | { |
| 419 | thread = thread_trim_head (&m->unuse); |
| 420 | if (thread->funcname) |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 421 | XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 422 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | else |
| 424 | { |
| 425 | thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread)); |
| 426 | m->alloc++; |
| 427 | } |
| 428 | thread->type = type; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 429 | thread->add_type = type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 430 | thread->master = m; |
| 431 | thread->func = func; |
| 432 | thread->arg = arg; |
| 433 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 434 | thread->funcname = strip_funcname(funcname); |
| 435 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 436 | return thread; |
| 437 | } |
| 438 | |
| 439 | /* Add new read thread. */ |
| 440 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 441 | funcname_thread_add_read (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 442 | int (*func) (struct thread *), void *arg, int fd, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | { |
| 444 | struct thread *thread; |
| 445 | |
| 446 | assert (m != NULL); |
| 447 | |
| 448 | if (FD_ISSET (fd, &m->readfd)) |
| 449 | { |
| 450 | zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd); |
| 451 | return NULL; |
| 452 | } |
| 453 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 454 | thread = thread_get (m, THREAD_READ, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 455 | FD_SET (fd, &m->readfd); |
| 456 | thread->u.fd = fd; |
| 457 | thread_list_add (&m->read, thread); |
| 458 | |
| 459 | return thread; |
| 460 | } |
| 461 | |
| 462 | /* Add new write thread. */ |
| 463 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 464 | funcname_thread_add_write (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 465 | int (*func) (struct thread *), void *arg, int fd, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | { |
| 467 | struct thread *thread; |
| 468 | |
| 469 | assert (m != NULL); |
| 470 | |
| 471 | if (FD_ISSET (fd, &m->writefd)) |
| 472 | { |
| 473 | zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd); |
| 474 | return NULL; |
| 475 | } |
| 476 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 477 | thread = thread_get (m, THREAD_WRITE, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | FD_SET (fd, &m->writefd); |
| 479 | thread->u.fd = fd; |
| 480 | thread_list_add (&m->write, thread); |
| 481 | |
| 482 | return thread; |
| 483 | } |
| 484 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 485 | static struct thread * |
| 486 | funcname_thread_add_timer_timeval (struct thread_master *m, |
| 487 | int (*func) (struct thread *), |
| 488 | void *arg, |
| 489 | struct timeval *time_relative, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 490 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 491 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | struct thread *thread; |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 493 | struct timeval timer_now; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | #ifndef TIMER_NO_SORT |
| 495 | struct thread *tt; |
| 496 | #endif /* TIMER_NO_SORT */ |
| 497 | |
| 498 | assert (m != NULL); |
| 499 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 500 | thread = thread_get (m, THREAD_TIMER, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | |
| 502 | /* Do we need jitter here? */ |
| 503 | gettimeofday (&timer_now, NULL); |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 504 | timer_now.tv_sec += time_relative->tv_sec; |
| 505 | timer_now.tv_usec += time_relative->tv_usec; |
| 506 | timeval_adjust (timer_now); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | thread->u.sands = timer_now; |
| 508 | |
| 509 | /* Sort by timeval. */ |
| 510 | #ifdef TIMER_NO_SORT |
| 511 | thread_list_add (&m->timer, thread); |
| 512 | #else |
| 513 | for (tt = m->timer.head; tt; tt = tt->next) |
| 514 | if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0) |
| 515 | break; |
| 516 | |
| 517 | if (tt) |
| 518 | thread_list_add_before (&m->timer, tt, thread); |
| 519 | else |
| 520 | thread_list_add (&m->timer, thread); |
| 521 | #endif /* TIMER_NO_SORT */ |
| 522 | |
| 523 | return thread; |
| 524 | } |
| 525 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 526 | |
| 527 | /* Add timer event thread. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 528 | struct thread * |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 529 | funcname_thread_add_timer (struct thread_master *m, |
| 530 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 531 | void *arg, long timer, const char* funcname) |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 532 | { |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 533 | struct timeval trel; |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 534 | |
| 535 | assert (m != NULL); |
| 536 | |
paul | 9076fbd | 2004-10-11 09:40:58 +0000 | [diff] [blame] | 537 | trel.tv_sec = timer; |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 538 | trel.tv_usec = 0; |
| 539 | |
| 540 | return funcname_thread_add_timer_timeval (m, func, arg, &trel, funcname); |
| 541 | } |
| 542 | |
| 543 | /* Add timer event thread with "millisecond" resolution */ |
| 544 | struct thread * |
| 545 | funcname_thread_add_timer_msec (struct thread_master *m, |
| 546 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 547 | void *arg, long timer, const char* funcname) |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 548 | { |
| 549 | struct timeval trel; |
| 550 | |
| 551 | assert (m != NULL); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 552 | |
ajs | af04bd7 | 2004-12-28 17:00:12 +0000 | [diff] [blame] | 553 | trel.tv_sec = timer / 1000; |
| 554 | trel.tv_usec = 1000*(timer % 1000); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 555 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 556 | return funcname_thread_add_timer_timeval (m, func, arg, &trel, funcname); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 557 | } |
| 558 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 559 | /* Add simple event thread. */ |
| 560 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 561 | funcname_thread_add_event (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 562 | int (*func) (struct thread *), void *arg, int val, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 563 | { |
| 564 | struct thread *thread; |
| 565 | |
| 566 | assert (m != NULL); |
| 567 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 568 | thread = thread_get (m, THREAD_EVENT, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 569 | thread->u.val = val; |
| 570 | thread_list_add (&m->event, thread); |
| 571 | |
| 572 | return thread; |
| 573 | } |
| 574 | |
| 575 | /* Cancel thread from scheduler. */ |
| 576 | void |
| 577 | thread_cancel (struct thread *thread) |
| 578 | { |
| 579 | switch (thread->type) |
| 580 | { |
| 581 | case THREAD_READ: |
| 582 | assert (FD_ISSET (thread->u.fd, &thread->master->readfd)); |
| 583 | FD_CLR (thread->u.fd, &thread->master->readfd); |
| 584 | thread_list_delete (&thread->master->read, thread); |
| 585 | break; |
| 586 | case THREAD_WRITE: |
| 587 | assert (FD_ISSET (thread->u.fd, &thread->master->writefd)); |
| 588 | FD_CLR (thread->u.fd, &thread->master->writefd); |
| 589 | thread_list_delete (&thread->master->write, thread); |
| 590 | break; |
| 591 | case THREAD_TIMER: |
| 592 | thread_list_delete (&thread->master->timer, thread); |
| 593 | break; |
| 594 | case THREAD_EVENT: |
| 595 | thread_list_delete (&thread->master->event, thread); |
| 596 | break; |
| 597 | case THREAD_READY: |
| 598 | thread_list_delete (&thread->master->ready, thread); |
| 599 | break; |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | thread->type = THREAD_UNUSED; |
| 604 | thread_add_unuse (thread->master, thread); |
| 605 | } |
| 606 | |
| 607 | /* Delete all events which has argument value arg. */ |
| 608 | void |
| 609 | thread_cancel_event (struct thread_master *m, void *arg) |
| 610 | { |
| 611 | struct thread *thread; |
| 612 | |
| 613 | thread = m->event.head; |
| 614 | while (thread) |
| 615 | { |
| 616 | struct thread *t; |
| 617 | |
| 618 | t = thread; |
| 619 | thread = t->next; |
| 620 | |
| 621 | if (t->arg == arg) |
| 622 | { |
| 623 | thread_list_delete (&m->event, t); |
| 624 | t->type = THREAD_UNUSED; |
| 625 | thread_add_unuse (m, t); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | #ifdef TIMER_NO_SORT |
| 631 | struct timeval * |
| 632 | thread_timer_wait (struct thread_master *m, struct timeval *timer_val) |
| 633 | { |
| 634 | struct timeval timer_now; |
| 635 | struct timeval timer_min; |
| 636 | struct timeval *timer_wait; |
| 637 | |
| 638 | gettimeofday (&timer_now, NULL); |
| 639 | |
| 640 | timer_wait = NULL; |
| 641 | for (thread = m->timer.head; thread; thread = thread->next) |
| 642 | { |
| 643 | if (! timer_wait) |
| 644 | timer_wait = &thread->u.sands; |
| 645 | else if (timeval_cmp (thread->u.sands, *timer_wait) < 0) |
| 646 | timer_wait = &thread->u.sands; |
| 647 | } |
| 648 | |
| 649 | if (m->timer.head) |
| 650 | { |
| 651 | timer_min = *timer_wait; |
| 652 | timer_min = timeval_subtract (timer_min, timer_now); |
| 653 | if (timer_min.tv_sec < 0) |
| 654 | { |
| 655 | timer_min.tv_sec = 0; |
| 656 | timer_min.tv_usec = 10; |
| 657 | } |
| 658 | timer_wait = &timer_min; |
| 659 | } |
| 660 | else |
| 661 | timer_wait = NULL; |
| 662 | |
| 663 | if (timer_wait) |
| 664 | { |
| 665 | *timer_val = timer_wait; |
| 666 | return timer_val; |
| 667 | } |
| 668 | return NULL; |
| 669 | } |
| 670 | #else /* ! TIMER_NO_SORT */ |
| 671 | struct timeval * |
| 672 | thread_timer_wait (struct thread_master *m, struct timeval *timer_val) |
| 673 | { |
| 674 | struct timeval timer_now; |
| 675 | struct timeval timer_min; |
| 676 | |
| 677 | if (m->timer.head) |
| 678 | { |
| 679 | gettimeofday (&timer_now, NULL); |
| 680 | timer_min = m->timer.head->u.sands; |
| 681 | timer_min = timeval_subtract (timer_min, timer_now); |
| 682 | if (timer_min.tv_sec < 0) |
| 683 | { |
| 684 | timer_min.tv_sec = 0; |
| 685 | timer_min.tv_usec = 10; |
| 686 | } |
| 687 | *timer_val = timer_min; |
| 688 | return timer_val; |
| 689 | } |
| 690 | return NULL; |
| 691 | } |
| 692 | #endif /* TIMER_NO_SORT */ |
| 693 | |
| 694 | struct thread * |
| 695 | thread_run (struct thread_master *m, struct thread *thread, |
| 696 | struct thread *fetch) |
| 697 | { |
| 698 | *fetch = *thread; |
| 699 | thread->type = THREAD_UNUSED; |
| 700 | thread_add_unuse (m, thread); |
| 701 | return fetch; |
| 702 | } |
| 703 | |
| 704 | int |
| 705 | thread_process_fd (struct thread_master *m, struct thread_list *list, |
| 706 | fd_set *fdset, fd_set *mfdset) |
| 707 | { |
| 708 | struct thread *thread; |
| 709 | struct thread *next; |
| 710 | int ready = 0; |
| 711 | |
| 712 | for (thread = list->head; thread; thread = next) |
| 713 | { |
| 714 | next = thread->next; |
| 715 | |
| 716 | if (FD_ISSET (THREAD_FD (thread), fdset)) |
| 717 | { |
| 718 | assert (FD_ISSET (THREAD_FD (thread), mfdset)); |
| 719 | FD_CLR(THREAD_FD (thread), mfdset); |
| 720 | thread_list_delete (list, thread); |
| 721 | thread_list_add (&m->ready, thread); |
| 722 | thread->type = THREAD_READY; |
| 723 | ready++; |
| 724 | } |
| 725 | } |
| 726 | return ready; |
| 727 | } |
| 728 | |
| 729 | /* Fetch next ready thread. */ |
| 730 | struct thread * |
| 731 | thread_fetch (struct thread_master *m, struct thread *fetch) |
| 732 | { |
| 733 | int num; |
| 734 | int ready; |
| 735 | struct thread *thread; |
| 736 | fd_set readfd; |
| 737 | fd_set writefd; |
| 738 | fd_set exceptfd; |
| 739 | struct timeval timer_now; |
| 740 | struct timeval timer_val; |
| 741 | struct timeval *timer_wait; |
| 742 | struct timeval timer_nowait; |
| 743 | |
| 744 | timer_nowait.tv_sec = 0; |
| 745 | timer_nowait.tv_usec = 0; |
| 746 | |
| 747 | while (1) |
| 748 | { |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 749 | /* Signals are highest priority */ |
| 750 | quagga_sigevent_process (); |
| 751 | |
| 752 | /* Normal event are the next highest priority. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 753 | if ((thread = thread_trim_head (&m->event)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 754 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | |
| 756 | /* Execute timer. */ |
| 757 | gettimeofday (&timer_now, NULL); |
| 758 | |
| 759 | for (thread = m->timer.head; thread; thread = thread->next) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 760 | if (timeval_cmp (timer_now, thread->u.sands) >= 0) |
| 761 | { |
| 762 | thread_list_delete (&m->timer, thread); |
| 763 | return thread_run (m, thread, fetch); |
| 764 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 765 | |
| 766 | /* If there are any ready threads, process top of them. */ |
| 767 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 768 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | |
| 770 | /* Structure copy. */ |
| 771 | readfd = m->readfd; |
| 772 | writefd = m->writefd; |
| 773 | exceptfd = m->exceptfd; |
| 774 | |
| 775 | /* Calculate select wait timer. */ |
| 776 | timer_wait = thread_timer_wait (m, &timer_val); |
| 777 | |
| 778 | num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait); |
| 779 | |
| 780 | if (num == 0) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 781 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 782 | |
| 783 | if (num < 0) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 784 | { |
| 785 | if (errno == EINTR) |
| 786 | { |
| 787 | /* signal received */ |
| 788 | quagga_sigevent_process (); |
| 789 | continue; |
| 790 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 792 | zlog_warn ("select() error: %s", safe_strerror (errno)); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 795 | |
| 796 | /* Normal priority read thead. */ |
| 797 | ready = thread_process_fd (m, &m->read, &readfd, &m->readfd); |
| 798 | |
| 799 | /* Write thead. */ |
| 800 | ready = thread_process_fd (m, &m->write, &writefd, &m->writefd); |
| 801 | |
| 802 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 803 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 807 | unsigned long |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 808 | thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start) |
| 809 | { |
| 810 | unsigned long thread_time; |
| 811 | |
| 812 | #ifdef HAVE_RUSAGE |
| 813 | /* This is 'user + sys' time. */ |
| 814 | thread_time = timeval_elapsed (now->ru_utime, start->ru_utime); |
| 815 | thread_time += timeval_elapsed (now->ru_stime, start->ru_stime); |
| 816 | #else |
| 817 | /* When rusage is not available, simple elapsed time is used. */ |
| 818 | thread_time = timeval_elapsed (*now, *start); |
| 819 | #endif /* HAVE_RUSAGE */ |
| 820 | |
| 821 | return thread_time; |
| 822 | } |
| 823 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 824 | #if 0 |
| 825 | |
| 826 | /* This function is not currently used: threads never yield! */ |
| 827 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | /* We should aim to yield after THREAD_YIELD_TIME_SLOT |
| 829 | milliseconds. */ |
| 830 | int |
| 831 | thread_should_yield (struct thread *thread) |
| 832 | { |
| 833 | RUSAGE_T ru; |
| 834 | |
| 835 | GETRUSAGE (&ru); |
| 836 | |
| 837 | if (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT) |
| 838 | return 1; |
| 839 | else |
| 840 | return 0; |
| 841 | } |
| 842 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 843 | #endif |
| 844 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 845 | /* We check thread consumed time. If the system has getrusage, we'll |
| 846 | use that to get indepth stats on the performance of the thread. If |
| 847 | not - we'll use gettimeofday for some guestimation. */ |
| 848 | void |
| 849 | thread_call (struct thread *thread) |
| 850 | { |
| 851 | unsigned long thread_time; |
| 852 | RUSAGE_T ru; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 853 | struct cpu_thread_history tmp, *cpu; |
| 854 | |
| 855 | tmp.func = thread->func; |
| 856 | tmp.funcname = thread->funcname; |
| 857 | cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 858 | |
| 859 | GETRUSAGE (&thread->ru); |
| 860 | |
| 861 | (*thread->func) (thread); |
| 862 | |
| 863 | GETRUSAGE (&ru); |
| 864 | |
| 865 | thread_time = thread_consumed_time (&ru, &thread->ru); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 866 | cpu->total += thread_time; |
| 867 | if (cpu->max < thread_time) |
| 868 | cpu->max = thread_time; |
| 869 | |
| 870 | ++cpu->total_calls; |
| 871 | cpu->types |= (1 << thread->add_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 872 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 873 | #ifdef CONSUMED_TIME_CHECK |
| 874 | if (thread_time > CONSUMED_TIME_CHECK) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 875 | { |
| 876 | /* |
| 877 | * We have a CPU Hog on our hands. |
| 878 | * Whinge about it now, so we're aware this is yet another task |
| 879 | * to fix. |
| 880 | */ |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 881 | zlog_warn ("CPU HOG: task %s (%lx) ran for %ldms", |
| 882 | thread->funcname, |
| 883 | (unsigned long) thread->func, |
| 884 | thread_time / 1000L); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 885 | } |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 886 | #endif /* CONSUMED_TIME_CHECK */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* Execute thread */ |
| 890 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 891 | funcname_thread_execute (struct thread_master *m, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 892 | int (*func)(struct thread *), |
| 893 | void *arg, |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 894 | int val, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 895 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 896 | { |
| 897 | struct thread dummy; |
| 898 | |
| 899 | memset (&dummy, 0, sizeof (struct thread)); |
| 900 | |
| 901 | dummy.type = THREAD_EVENT; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 902 | dummy.add_type = THREAD_EXECUTE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 903 | dummy.master = NULL; |
| 904 | dummy.func = func; |
| 905 | dummy.arg = arg; |
| 906 | dummy.u.val = val; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 907 | dummy.funcname = strip_funcname (funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 908 | thread_call (&dummy); |
| 909 | |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 910 | XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 911 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 912 | return NULL; |
| 913 | } |