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 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 38 | static struct timeval |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 90 | static unsigned int |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 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 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 117 | vty_out(vty, " %7ld.%03ld %9d %8ld %10ld %c%c%c%c%c%c %s%s", |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 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':' ', |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 125 | a->types & (1 << THREAD_BACKGROUND) ? 'B' : ' ', |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 126 | a->funcname, VTY_NEWLINE); |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | cpu_record_hash_print(struct hash_backet *bucket, |
| 131 | void *args[]) |
| 132 | { |
| 133 | struct cpu_thread_history *totals = args[0]; |
| 134 | struct vty *vty = args[1]; |
| 135 | unsigned char *filter = args[2]; |
| 136 | struct cpu_thread_history *a = bucket->data; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 137 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 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" |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 175 | "Display filter (rwtexb)\n") |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 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; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 207 | case 'b': |
| 208 | case 'B': |
| 209 | filter |= (1 << THREAD_BACKGROUND); |
| 210 | break; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | ++i; |
| 215 | } |
| 216 | if (filter == 0) |
| 217 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 218 | vty_out(vty, "Invalid filter \"%s\" specified," |
| 219 | " must contain at least one of 'RWTEXB'%s", |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 220 | argv[0], VTY_NEWLINE); |
| 221 | return CMD_WARNING; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | cpu_record_print(vty, filter); |
| 226 | return CMD_SUCCESS; |
| 227 | } |
| 228 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 229 | /* List allocation and head/tail print out. */ |
| 230 | static void |
| 231 | thread_list_debug (struct thread_list *list) |
| 232 | { |
| 233 | printf ("count [%d] head [%p] tail [%p]\n", |
| 234 | list->count, list->head, list->tail); |
| 235 | } |
| 236 | |
| 237 | /* Debug print for thread_master. */ |
| 238 | void |
| 239 | thread_master_debug (struct thread_master *m) |
| 240 | { |
| 241 | printf ("-----------\n"); |
| 242 | printf ("readlist : "); |
| 243 | thread_list_debug (&m->read); |
| 244 | printf ("writelist : "); |
| 245 | thread_list_debug (&m->write); |
| 246 | printf ("timerlist : "); |
| 247 | thread_list_debug (&m->timer); |
| 248 | printf ("eventlist : "); |
| 249 | thread_list_debug (&m->event); |
| 250 | printf ("unuselist : "); |
| 251 | thread_list_debug (&m->unuse); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 252 | printf ("bgndlist : "); |
| 253 | thread_list_debug (&m->background); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | printf ("total alloc: [%ld]\n", m->alloc); |
| 255 | printf ("-----------\n"); |
| 256 | } |
| 257 | |
| 258 | /* Allocate new thread master. */ |
| 259 | struct thread_master * |
| 260 | thread_master_create () |
| 261 | { |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 262 | if (cpu_record == NULL) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 263 | cpu_record = hash_create_size (1011, cpu_record_hash_key, |
| 264 | cpu_record_hash_cmp); |
| 265 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER, |
| 267 | sizeof (struct thread_master)); |
| 268 | } |
| 269 | |
| 270 | /* Add a new thread to the list. */ |
| 271 | static void |
| 272 | thread_list_add (struct thread_list *list, struct thread *thread) |
| 273 | { |
| 274 | thread->next = NULL; |
| 275 | thread->prev = list->tail; |
| 276 | if (list->tail) |
| 277 | list->tail->next = thread; |
| 278 | else |
| 279 | list->head = thread; |
| 280 | list->tail = thread; |
| 281 | list->count++; |
| 282 | } |
| 283 | |
| 284 | /* Add a new thread just before the point. */ |
| 285 | static void |
| 286 | thread_list_add_before (struct thread_list *list, |
| 287 | struct thread *point, |
| 288 | struct thread *thread) |
| 289 | { |
| 290 | thread->next = point; |
| 291 | thread->prev = point->prev; |
| 292 | if (point->prev) |
| 293 | point->prev->next = thread; |
| 294 | else |
| 295 | list->head = thread; |
| 296 | point->prev = thread; |
| 297 | list->count++; |
| 298 | } |
| 299 | |
| 300 | /* Delete a thread from the list. */ |
| 301 | static struct thread * |
| 302 | thread_list_delete (struct thread_list *list, struct thread *thread) |
| 303 | { |
| 304 | if (thread->next) |
| 305 | thread->next->prev = thread->prev; |
| 306 | else |
| 307 | list->tail = thread->prev; |
| 308 | if (thread->prev) |
| 309 | thread->prev->next = thread->next; |
| 310 | else |
| 311 | list->head = thread->next; |
| 312 | thread->next = thread->prev = NULL; |
| 313 | list->count--; |
| 314 | return thread; |
| 315 | } |
| 316 | |
| 317 | /* Move thread to unuse list. */ |
| 318 | static void |
| 319 | thread_add_unuse (struct thread_master *m, struct thread *thread) |
| 320 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 321 | assert (m != NULL && thread != NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | assert (thread->next == NULL); |
| 323 | assert (thread->prev == NULL); |
| 324 | assert (thread->type == THREAD_UNUSED); |
| 325 | thread_list_add (&m->unuse, thread); |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 326 | /* XXX: Should we deallocate funcname here? */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* Free all unused thread. */ |
| 330 | static void |
| 331 | thread_list_free (struct thread_master *m, struct thread_list *list) |
| 332 | { |
| 333 | struct thread *t; |
| 334 | struct thread *next; |
| 335 | |
| 336 | for (t = list->head; t; t = next) |
| 337 | { |
| 338 | next = t->next; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 339 | XFREE (MTYPE_THREAD_FUNCNAME, t->funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | XFREE (MTYPE_THREAD, t); |
| 341 | list->count--; |
| 342 | m->alloc--; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* Stop thread scheduler. */ |
| 347 | void |
| 348 | thread_master_free (struct thread_master *m) |
| 349 | { |
| 350 | thread_list_free (m, &m->read); |
| 351 | thread_list_free (m, &m->write); |
| 352 | thread_list_free (m, &m->timer); |
| 353 | thread_list_free (m, &m->event); |
| 354 | thread_list_free (m, &m->ready); |
| 355 | thread_list_free (m, &m->unuse); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 356 | thread_list_free (m, &m->background); |
| 357 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 358 | XFREE (MTYPE_THREAD_MASTER, m); |
| 359 | } |
| 360 | |
| 361 | /* Delete top of the list and return it. */ |
| 362 | static struct thread * |
| 363 | thread_trim_head (struct thread_list *list) |
| 364 | { |
| 365 | if (list->head) |
| 366 | return thread_list_delete (list, list->head); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | /* Thread list is empty or not. */ |
| 371 | int |
| 372 | thread_empty (struct thread_list *list) |
| 373 | { |
| 374 | return list->head ? 0 : 1; |
| 375 | } |
| 376 | |
| 377 | /* Return remain time in second. */ |
| 378 | unsigned long |
| 379 | thread_timer_remain_second (struct thread *thread) |
| 380 | { |
| 381 | struct timeval timer_now; |
| 382 | |
| 383 | gettimeofday (&timer_now, NULL); |
| 384 | |
| 385 | if (thread->u.sands.tv_sec - timer_now.tv_sec > 0) |
| 386 | return thread->u.sands.tv_sec - timer_now.tv_sec; |
| 387 | else |
| 388 | return 0; |
| 389 | } |
| 390 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 391 | /* Trim blankspace and "()"s */ |
| 392 | static char * |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 393 | strip_funcname (const char *funcname) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 394 | { |
| 395 | char buff[100]; |
| 396 | char tmp, *ret, *e, *b = buff; |
| 397 | |
| 398 | strncpy(buff, funcname, sizeof(buff)); |
| 399 | buff[ sizeof(buff) -1] = '\0'; |
| 400 | e = buff +strlen(buff) -1; |
| 401 | |
| 402 | /* Wont work for funcname == "Word (explanation)" */ |
| 403 | |
| 404 | while (*b == ' ' || *b == '(') |
| 405 | ++b; |
| 406 | while (*e == ' ' || *e == ')') |
| 407 | --e; |
| 408 | e++; |
| 409 | |
| 410 | tmp = *e; |
| 411 | *e = '\0'; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 412 | ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 413 | *e = tmp; |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 418 | /* Get new thread. */ |
| 419 | static struct thread * |
| 420 | thread_get (struct thread_master *m, u_char type, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 421 | int (*func) (struct thread *), void *arg, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 422 | { |
| 423 | struct thread *thread; |
| 424 | |
| 425 | if (m->unuse.head) |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 426 | { |
| 427 | thread = thread_trim_head (&m->unuse); |
| 428 | if (thread->funcname) |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 429 | XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 430 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 431 | else |
| 432 | { |
| 433 | thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread)); |
| 434 | m->alloc++; |
| 435 | } |
| 436 | thread->type = type; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 437 | thread->add_type = type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 438 | thread->master = m; |
| 439 | thread->func = func; |
| 440 | thread->arg = arg; |
| 441 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 442 | thread->funcname = strip_funcname(funcname); |
| 443 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | return thread; |
| 445 | } |
| 446 | |
| 447 | /* Add new read thread. */ |
| 448 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 449 | funcname_thread_add_read (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 450 | int (*func) (struct thread *), void *arg, int fd, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 451 | { |
| 452 | struct thread *thread; |
| 453 | |
| 454 | assert (m != NULL); |
| 455 | |
| 456 | if (FD_ISSET (fd, &m->readfd)) |
| 457 | { |
| 458 | zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd); |
| 459 | return NULL; |
| 460 | } |
| 461 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 462 | thread = thread_get (m, THREAD_READ, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 463 | FD_SET (fd, &m->readfd); |
| 464 | thread->u.fd = fd; |
| 465 | thread_list_add (&m->read, thread); |
| 466 | |
| 467 | return thread; |
| 468 | } |
| 469 | |
| 470 | /* Add new write thread. */ |
| 471 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 472 | funcname_thread_add_write (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 473 | int (*func) (struct thread *), void *arg, int fd, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 474 | { |
| 475 | struct thread *thread; |
| 476 | |
| 477 | assert (m != NULL); |
| 478 | |
| 479 | if (FD_ISSET (fd, &m->writefd)) |
| 480 | { |
| 481 | zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 485 | thread = thread_get (m, THREAD_WRITE, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 486 | FD_SET (fd, &m->writefd); |
| 487 | thread->u.fd = fd; |
| 488 | thread_list_add (&m->write, thread); |
| 489 | |
| 490 | return thread; |
| 491 | } |
| 492 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 493 | static struct thread * |
| 494 | funcname_thread_add_timer_timeval (struct thread_master *m, |
| 495 | int (*func) (struct thread *), |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 496 | int type, |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 497 | void *arg, |
| 498 | struct timeval *time_relative, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 499 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 500 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | struct thread *thread; |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 502 | struct timeval timer_now; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 503 | struct thread_list *list; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 504 | struct thread *tt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 505 | |
| 506 | assert (m != NULL); |
| 507 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 508 | assert (type == THREAD_TIMER || THREAD_BACKGROUND); |
| 509 | assert (time_relative); |
| 510 | |
| 511 | switch (type) |
| 512 | { |
| 513 | case THREAD_TIMER: |
| 514 | list = &m->timer; |
| 515 | break; |
| 516 | case THREAD_BACKGROUND: |
| 517 | list = &m->background; |
| 518 | break; |
| 519 | default: |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | thread = thread_get (m, type, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | |
| 525 | /* Do we need jitter here? */ |
| 526 | gettimeofday (&timer_now, NULL); |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 527 | timer_now.tv_sec += time_relative->tv_sec; |
| 528 | timer_now.tv_usec += time_relative->tv_usec; |
| 529 | timeval_adjust (timer_now); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 530 | thread->u.sands = timer_now; |
| 531 | |
| 532 | /* Sort by timeval. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 533 | for (tt = list->head; tt; tt = tt->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0) |
| 535 | break; |
| 536 | |
| 537 | if (tt) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 538 | thread_list_add_before (list, tt, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | else |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 540 | thread_list_add (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | |
| 542 | return thread; |
| 543 | } |
| 544 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 545 | |
| 546 | /* Add timer event thread. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 547 | struct thread * |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 548 | funcname_thread_add_timer (struct thread_master *m, |
| 549 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 550 | void *arg, long timer, const char* funcname) |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 551 | { |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 552 | struct timeval trel; |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 553 | |
| 554 | assert (m != NULL); |
| 555 | |
paul | 9076fbd | 2004-10-11 09:40:58 +0000 | [diff] [blame] | 556 | trel.tv_sec = timer; |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 557 | trel.tv_usec = 0; |
| 558 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 559 | return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, |
| 560 | &trel, funcname); |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Add timer event thread with "millisecond" resolution */ |
| 564 | struct thread * |
| 565 | funcname_thread_add_timer_msec (struct thread_master *m, |
| 566 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 567 | void *arg, long timer, const char* funcname) |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 568 | { |
| 569 | struct timeval trel; |
| 570 | |
| 571 | assert (m != NULL); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 572 | |
ajs | af04bd7 | 2004-12-28 17:00:12 +0000 | [diff] [blame] | 573 | trel.tv_sec = timer / 1000; |
| 574 | trel.tv_usec = 1000*(timer % 1000); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 575 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 576 | return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, |
| 577 | arg, &trel, funcname); |
| 578 | } |
| 579 | |
| 580 | /* Add a background thread, with an optional millisec delay */ |
| 581 | struct thread * |
| 582 | funcname_thread_add_background (struct thread_master *m, |
| 583 | int (*func) (struct thread *), |
| 584 | void *arg, long delay, |
| 585 | const char *funcname) |
| 586 | { |
| 587 | struct timeval trel; |
| 588 | |
| 589 | assert (m != NULL); |
| 590 | |
| 591 | if (delay) |
| 592 | { |
| 593 | trel.tv_sec = delay / 1000; |
| 594 | trel.tv_usec = 1000*(delay % 1000); |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | trel.tv_sec = 0; |
| 599 | trel.tv_usec = 0; |
| 600 | } |
| 601 | |
| 602 | return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND, |
| 603 | arg, &trel, funcname); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 604 | } |
| 605 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 606 | /* Add simple event thread. */ |
| 607 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 608 | funcname_thread_add_event (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 609 | int (*func) (struct thread *), void *arg, int val, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 610 | { |
| 611 | struct thread *thread; |
| 612 | |
| 613 | assert (m != NULL); |
| 614 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 615 | thread = thread_get (m, THREAD_EVENT, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 616 | thread->u.val = val; |
| 617 | thread_list_add (&m->event, thread); |
| 618 | |
| 619 | return thread; |
| 620 | } |
| 621 | |
| 622 | /* Cancel thread from scheduler. */ |
| 623 | void |
| 624 | thread_cancel (struct thread *thread) |
| 625 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 626 | struct thread_list *list; |
| 627 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 628 | switch (thread->type) |
| 629 | { |
| 630 | case THREAD_READ: |
| 631 | assert (FD_ISSET (thread->u.fd, &thread->master->readfd)); |
| 632 | FD_CLR (thread->u.fd, &thread->master->readfd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 633 | list = &thread->master->read; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | break; |
| 635 | case THREAD_WRITE: |
| 636 | assert (FD_ISSET (thread->u.fd, &thread->master->writefd)); |
| 637 | FD_CLR (thread->u.fd, &thread->master->writefd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 638 | list = &thread->master->write; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 639 | break; |
| 640 | case THREAD_TIMER: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 641 | list = &thread->master->timer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 642 | break; |
| 643 | case THREAD_EVENT: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 644 | list = &thread->master->event; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 645 | break; |
| 646 | case THREAD_READY: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 647 | list = &thread->master->ready; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | break; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 649 | case THREAD_BACKGROUND: |
| 650 | list = &thread->master->background; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | default: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 652 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 653 | break; |
| 654 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 655 | thread_list_delete (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | thread->type = THREAD_UNUSED; |
| 657 | thread_add_unuse (thread->master, thread); |
| 658 | } |
| 659 | |
| 660 | /* Delete all events which has argument value arg. */ |
| 661 | void |
| 662 | thread_cancel_event (struct thread_master *m, void *arg) |
| 663 | { |
| 664 | struct thread *thread; |
| 665 | |
| 666 | thread = m->event.head; |
| 667 | while (thread) |
| 668 | { |
| 669 | struct thread *t; |
| 670 | |
| 671 | t = thread; |
| 672 | thread = t->next; |
| 673 | |
| 674 | if (t->arg == arg) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 675 | { |
| 676 | thread_list_delete (&m->event, t); |
| 677 | t->type = THREAD_UNUSED; |
| 678 | thread_add_unuse (m, t); |
| 679 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 683 | static struct timeval * |
| 684 | thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 685 | { |
| 686 | struct timeval timer_now; |
| 687 | struct timeval timer_min; |
| 688 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 689 | if (tlist->head) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | { |
| 691 | gettimeofday (&timer_now, NULL); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 692 | timer_min = tlist->head->u.sands; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 693 | timer_min = timeval_subtract (timer_min, timer_now); |
| 694 | if (timer_min.tv_sec < 0) |
| 695 | { |
| 696 | timer_min.tv_sec = 0; |
| 697 | timer_min.tv_usec = 10; |
| 698 | } |
| 699 | *timer_val = timer_min; |
| 700 | return timer_val; |
| 701 | } |
| 702 | return NULL; |
| 703 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 704 | |
| 705 | struct thread * |
| 706 | thread_run (struct thread_master *m, struct thread *thread, |
| 707 | struct thread *fetch) |
| 708 | { |
| 709 | *fetch = *thread; |
| 710 | thread->type = THREAD_UNUSED; |
| 711 | thread_add_unuse (m, thread); |
| 712 | return fetch; |
| 713 | } |
| 714 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 715 | static int |
| 716 | thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 717 | { |
| 718 | struct thread *thread; |
| 719 | struct thread *next; |
| 720 | int ready = 0; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 721 | |
| 722 | assert (list); |
| 723 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | for (thread = list->head; thread; thread = next) |
| 725 | { |
| 726 | next = thread->next; |
| 727 | |
| 728 | if (FD_ISSET (THREAD_FD (thread), fdset)) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 729 | { |
| 730 | assert (FD_ISSET (THREAD_FD (thread), mfdset)); |
| 731 | FD_CLR(THREAD_FD (thread), mfdset); |
| 732 | thread_list_delete (list, thread); |
| 733 | thread_list_add (&thread->master->ready, thread); |
| 734 | thread->type = THREAD_READY; |
| 735 | ready++; |
| 736 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | } |
| 738 | return ready; |
| 739 | } |
| 740 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 741 | /* fetch next timer-like thread thread from the list and return it */ |
| 742 | static unsigned int |
| 743 | thread_timer_process (struct thread_list *list, struct timeval *timenow) |
| 744 | { |
| 745 | struct thread *thread; |
| 746 | unsigned int ready = 0; |
| 747 | |
| 748 | assert (list && timenow); |
| 749 | |
| 750 | for (thread = list->head; thread; thread = thread->next) |
| 751 | if (timeval_cmp (*timenow, thread->u.sands) >= 0) |
| 752 | { |
| 753 | thread_list_delete (list, thread); |
| 754 | assert (thread->next == thread->prev && thread->next == NULL); |
| 755 | thread->next = thread->prev = NULL; |
| 756 | thread->type = THREAD_READY; |
| 757 | thread_list_add (&thread->master->ready, thread); |
| 758 | ready++; |
| 759 | } |
| 760 | return ready; |
| 761 | } |
| 762 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 763 | /* Fetch next ready thread. */ |
| 764 | struct thread * |
| 765 | thread_fetch (struct thread_master *m, struct thread *fetch) |
| 766 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | struct thread *thread; |
| 768 | fd_set readfd; |
| 769 | fd_set writefd; |
| 770 | fd_set exceptfd; |
| 771 | struct timeval timer_now; |
| 772 | struct timeval timer_val; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 773 | struct timeval timer_val_bg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 774 | struct timeval *timer_wait; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 775 | struct timeval *timer_wait_bg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 776 | |
| 777 | while (1) |
| 778 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 779 | int num = 0; |
| 780 | int ready = m->ready.count; |
| 781 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 782 | /* Signals are highest priority */ |
| 783 | quagga_sigevent_process (); |
| 784 | |
| 785 | /* Normal event are the next highest priority. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 786 | if ((thread = thread_trim_head (&m->event)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 787 | return thread_run (m, thread, fetch); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 788 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 789 | /* Execute timer. */ |
| 790 | gettimeofday (&timer_now, NULL); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 791 | |
| 792 | /* Timer threads */ |
| 793 | ready += thread_timer_process (&m->timer, &timer_now); |
| 794 | |
| 795 | /* If there are any ready threads from previous scheduler runs, |
| 796 | * process top of them. |
| 797 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 799 | return thread_run (m, thread, fetch); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 800 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 801 | /* Structure copy. */ |
| 802 | readfd = m->readfd; |
| 803 | writefd = m->writefd; |
| 804 | exceptfd = m->exceptfd; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 805 | |
| 806 | /* Calculate select wait timer if nothing else to do */ |
| 807 | if (ready == 0) |
| 808 | { |
| 809 | timer_wait = thread_timer_wait (&m->timer, &timer_val); |
| 810 | timer_wait_bg = thread_timer_wait (&m->background, &timer_val_bg); |
| 811 | |
| 812 | if (timer_wait && timer_wait_bg && |
| 813 | (timeval_cmp (*timer_wait, *timer_wait_bg) > 0)) |
| 814 | timer_wait = timer_wait_bg; |
| 815 | else if (!timer_wait && timer_wait_bg) |
| 816 | timer_wait = timer_wait_bg; |
| 817 | } |
| 818 | else |
| 819 | { |
| 820 | timer_val.tv_sec = timer_val.tv_usec = 0; |
| 821 | timer_wait = &timer_val; |
| 822 | } |
| 823 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 824 | num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 825 | |
| 826 | /* Signals should get quick treatment */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 827 | if (num < 0) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 828 | { |
| 829 | if (errno == EINTR) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 830 | continue; /* signal received - process it */ |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 831 | zlog_warn ("select() error: %s", safe_strerror (errno)); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 832 | return NULL; |
| 833 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 834 | |
| 835 | /* Got IO, process it */ |
| 836 | if (num > 0) |
| 837 | { |
| 838 | /* Normal priority read thead. */ |
| 839 | ready += thread_process_fd (&m->read, &readfd, &m->readfd); |
| 840 | /* Write thead. */ |
| 841 | ready += thread_process_fd (&m->write, &writefd, &m->writefd); |
| 842 | } |
| 843 | |
| 844 | /* Background timer/events, lowest priority */ |
| 845 | ready += thread_timer_process (&m->background, &timer_now); |
| 846 | |
| 847 | /* if any threads were made ready above.. */ |
| 848 | if ((ready > 0) && (thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 849 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 853 | unsigned long |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 854 | thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start) |
| 855 | { |
| 856 | unsigned long thread_time; |
| 857 | |
| 858 | #ifdef HAVE_RUSAGE |
| 859 | /* This is 'user + sys' time. */ |
| 860 | thread_time = timeval_elapsed (now->ru_utime, start->ru_utime); |
| 861 | thread_time += timeval_elapsed (now->ru_stime, start->ru_stime); |
| 862 | #else |
| 863 | /* When rusage is not available, simple elapsed time is used. */ |
| 864 | thread_time = timeval_elapsed (*now, *start); |
| 865 | #endif /* HAVE_RUSAGE */ |
| 866 | |
| 867 | return thread_time; |
| 868 | } |
| 869 | |
| 870 | /* We should aim to yield after THREAD_YIELD_TIME_SLOT |
| 871 | milliseconds. */ |
| 872 | int |
| 873 | thread_should_yield (struct thread *thread) |
| 874 | { |
| 875 | RUSAGE_T ru; |
| 876 | |
| 877 | GETRUSAGE (&ru); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 878 | |
| 879 | return (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* We check thread consumed time. If the system has getrusage, we'll |
| 883 | use that to get indepth stats on the performance of the thread. If |
| 884 | not - we'll use gettimeofday for some guestimation. */ |
| 885 | void |
| 886 | thread_call (struct thread *thread) |
| 887 | { |
| 888 | unsigned long thread_time; |
| 889 | RUSAGE_T ru; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 890 | struct cpu_thread_history tmp, *cpu; |
| 891 | |
| 892 | tmp.func = thread->func; |
| 893 | tmp.funcname = thread->funcname; |
| 894 | cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 895 | |
| 896 | GETRUSAGE (&thread->ru); |
| 897 | |
| 898 | (*thread->func) (thread); |
| 899 | |
| 900 | GETRUSAGE (&ru); |
| 901 | |
| 902 | thread_time = thread_consumed_time (&ru, &thread->ru); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 903 | cpu->total += thread_time; |
| 904 | if (cpu->max < thread_time) |
| 905 | cpu->max = thread_time; |
| 906 | |
| 907 | ++cpu->total_calls; |
| 908 | cpu->types |= (1 << thread->add_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 909 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 910 | #ifdef CONSUMED_TIME_CHECK |
| 911 | if (thread_time > CONSUMED_TIME_CHECK) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 912 | { |
| 913 | /* |
| 914 | * We have a CPU Hog on our hands. |
| 915 | * Whinge about it now, so we're aware this is yet another task |
| 916 | * to fix. |
| 917 | */ |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 918 | zlog_warn ("CPU HOG: task %s (%lx) ran for %ldms", |
| 919 | thread->funcname, |
| 920 | (unsigned long) thread->func, |
| 921 | thread_time / 1000L); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 922 | } |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 923 | #endif /* CONSUMED_TIME_CHECK */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* Execute thread */ |
| 927 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 928 | funcname_thread_execute (struct thread_master *m, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 929 | int (*func)(struct thread *), |
| 930 | void *arg, |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 931 | int val, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 932 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 933 | { |
| 934 | struct thread dummy; |
| 935 | |
| 936 | memset (&dummy, 0, sizeof (struct thread)); |
| 937 | |
| 938 | dummy.type = THREAD_EVENT; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 939 | dummy.add_type = THREAD_EXECUTE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 940 | dummy.master = NULL; |
| 941 | dummy.func = func; |
| 942 | dummy.arg = arg; |
| 943 | dummy.u.val = val; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 944 | dummy.funcname = strip_funcname (funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 945 | thread_call (&dummy); |
| 946 | |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 947 | XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 948 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | return NULL; |
| 950 | } |