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 | #ifndef TIMER_NO_SORT |
| 505 | struct thread *tt; |
| 506 | #endif /* TIMER_NO_SORT */ |
| 507 | |
| 508 | assert (m != NULL); |
| 509 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 510 | assert (type == THREAD_TIMER || THREAD_BACKGROUND); |
| 511 | assert (time_relative); |
| 512 | |
| 513 | switch (type) |
| 514 | { |
| 515 | case THREAD_TIMER: |
| 516 | list = &m->timer; |
| 517 | break; |
| 518 | case THREAD_BACKGROUND: |
| 519 | list = &m->background; |
| 520 | break; |
| 521 | default: |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | thread = thread_get (m, type, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | |
| 527 | /* Do we need jitter here? */ |
| 528 | gettimeofday (&timer_now, NULL); |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 529 | timer_now.tv_sec += time_relative->tv_sec; |
| 530 | timer_now.tv_usec += time_relative->tv_usec; |
| 531 | timeval_adjust (timer_now); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | thread->u.sands = timer_now; |
| 533 | |
| 534 | /* Sort by timeval. */ |
| 535 | #ifdef TIMER_NO_SORT |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 536 | thread_list_add (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | #else |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 538 | for (tt = list->head; tt; tt = tt->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0) |
| 540 | break; |
| 541 | |
| 542 | if (tt) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 543 | thread_list_add_before (list, tt, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 544 | else |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 545 | thread_list_add (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | #endif /* TIMER_NO_SORT */ |
| 547 | |
| 548 | return thread; |
| 549 | } |
| 550 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 551 | |
| 552 | /* Add timer event thread. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 553 | struct thread * |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 554 | funcname_thread_add_timer (struct thread_master *m, |
| 555 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 556 | void *arg, long timer, const char* funcname) |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 557 | { |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 558 | struct timeval trel; |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 559 | |
| 560 | assert (m != NULL); |
| 561 | |
paul | 9076fbd | 2004-10-11 09:40:58 +0000 | [diff] [blame] | 562 | trel.tv_sec = timer; |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 563 | trel.tv_usec = 0; |
| 564 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 565 | return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, |
| 566 | &trel, funcname); |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* Add timer event thread with "millisecond" resolution */ |
| 570 | struct thread * |
| 571 | funcname_thread_add_timer_msec (struct thread_master *m, |
| 572 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 573 | void *arg, long timer, const char* funcname) |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 574 | { |
| 575 | struct timeval trel; |
| 576 | |
| 577 | assert (m != NULL); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 578 | |
ajs | af04bd7 | 2004-12-28 17:00:12 +0000 | [diff] [blame] | 579 | trel.tv_sec = timer / 1000; |
| 580 | trel.tv_usec = 1000*(timer % 1000); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 581 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 582 | return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, |
| 583 | arg, &trel, funcname); |
| 584 | } |
| 585 | |
| 586 | /* Add a background thread, with an optional millisec delay */ |
| 587 | struct thread * |
| 588 | funcname_thread_add_background (struct thread_master *m, |
| 589 | int (*func) (struct thread *), |
| 590 | void *arg, long delay, |
| 591 | const char *funcname) |
| 592 | { |
| 593 | struct timeval trel; |
| 594 | |
| 595 | assert (m != NULL); |
| 596 | |
| 597 | if (delay) |
| 598 | { |
| 599 | trel.tv_sec = delay / 1000; |
| 600 | trel.tv_usec = 1000*(delay % 1000); |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | trel.tv_sec = 0; |
| 605 | trel.tv_usec = 0; |
| 606 | } |
| 607 | |
| 608 | return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND, |
| 609 | arg, &trel, funcname); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 610 | } |
| 611 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 612 | /* Add simple event thread. */ |
| 613 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 614 | funcname_thread_add_event (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 615 | int (*func) (struct thread *), void *arg, int val, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 616 | { |
| 617 | struct thread *thread; |
| 618 | |
| 619 | assert (m != NULL); |
| 620 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 621 | thread = thread_get (m, THREAD_EVENT, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | thread->u.val = val; |
| 623 | thread_list_add (&m->event, thread); |
| 624 | |
| 625 | return thread; |
| 626 | } |
| 627 | |
| 628 | /* Cancel thread from scheduler. */ |
| 629 | void |
| 630 | thread_cancel (struct thread *thread) |
| 631 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 632 | struct thread_list *list; |
| 633 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | switch (thread->type) |
| 635 | { |
| 636 | case THREAD_READ: |
| 637 | assert (FD_ISSET (thread->u.fd, &thread->master->readfd)); |
| 638 | FD_CLR (thread->u.fd, &thread->master->readfd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 639 | list = &thread->master->read; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 640 | break; |
| 641 | case THREAD_WRITE: |
| 642 | assert (FD_ISSET (thread->u.fd, &thread->master->writefd)); |
| 643 | FD_CLR (thread->u.fd, &thread->master->writefd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 644 | list = &thread->master->write; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 645 | break; |
| 646 | case THREAD_TIMER: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 647 | list = &thread->master->timer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | break; |
| 649 | case THREAD_EVENT: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 650 | list = &thread->master->event; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | break; |
| 652 | case THREAD_READY: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 653 | list = &thread->master->ready; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 654 | break; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 655 | case THREAD_BACKGROUND: |
| 656 | list = &thread->master->background; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 657 | default: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 658 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 659 | break; |
| 660 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 661 | thread_list_delete (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 662 | thread->type = THREAD_UNUSED; |
| 663 | thread_add_unuse (thread->master, thread); |
| 664 | } |
| 665 | |
| 666 | /* Delete all events which has argument value arg. */ |
| 667 | void |
| 668 | thread_cancel_event (struct thread_master *m, void *arg) |
| 669 | { |
| 670 | struct thread *thread; |
| 671 | |
| 672 | thread = m->event.head; |
| 673 | while (thread) |
| 674 | { |
| 675 | struct thread *t; |
| 676 | |
| 677 | t = thread; |
| 678 | thread = t->next; |
| 679 | |
| 680 | if (t->arg == arg) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 681 | { |
| 682 | thread_list_delete (&m->event, t); |
| 683 | t->type = THREAD_UNUSED; |
| 684 | thread_add_unuse (m, t); |
| 685 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | #ifdef TIMER_NO_SORT |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 690 | static struct timeval * |
| 691 | thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 692 | { |
| 693 | struct timeval timer_now; |
| 694 | struct timeval timer_min; |
| 695 | struct timeval *timer_wait; |
| 696 | |
| 697 | gettimeofday (&timer_now, NULL); |
| 698 | |
| 699 | timer_wait = NULL; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 700 | for (thread = tlist->head; thread; thread = thread->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 701 | { |
| 702 | if (! timer_wait) |
| 703 | timer_wait = &thread->u.sands; |
| 704 | else if (timeval_cmp (thread->u.sands, *timer_wait) < 0) |
| 705 | timer_wait = &thread->u.sands; |
| 706 | } |
| 707 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 708 | if (tlist->head) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | { |
| 710 | timer_min = *timer_wait; |
| 711 | timer_min = timeval_subtract (timer_min, timer_now); |
| 712 | if (timer_min.tv_sec < 0) |
| 713 | { |
| 714 | timer_min.tv_sec = 0; |
| 715 | timer_min.tv_usec = 10; |
| 716 | } |
| 717 | timer_wait = &timer_min; |
| 718 | } |
| 719 | else |
| 720 | timer_wait = NULL; |
| 721 | |
| 722 | if (timer_wait) |
| 723 | { |
| 724 | *timer_val = timer_wait; |
| 725 | return timer_val; |
| 726 | } |
| 727 | return NULL; |
| 728 | } |
| 729 | #else /* ! TIMER_NO_SORT */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 730 | static struct timeval * |
| 731 | thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 732 | { |
| 733 | struct timeval timer_now; |
| 734 | struct timeval timer_min; |
| 735 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 736 | if (tlist->head) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | { |
| 738 | gettimeofday (&timer_now, NULL); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 739 | timer_min = tlist->head->u.sands; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 740 | timer_min = timeval_subtract (timer_min, timer_now); |
| 741 | if (timer_min.tv_sec < 0) |
| 742 | { |
| 743 | timer_min.tv_sec = 0; |
| 744 | timer_min.tv_usec = 10; |
| 745 | } |
| 746 | *timer_val = timer_min; |
| 747 | return timer_val; |
| 748 | } |
| 749 | return NULL; |
| 750 | } |
| 751 | #endif /* TIMER_NO_SORT */ |
| 752 | |
| 753 | struct thread * |
| 754 | thread_run (struct thread_master *m, struct thread *thread, |
| 755 | struct thread *fetch) |
| 756 | { |
| 757 | *fetch = *thread; |
| 758 | thread->type = THREAD_UNUSED; |
| 759 | thread_add_unuse (m, thread); |
| 760 | return fetch; |
| 761 | } |
| 762 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 763 | static int |
| 764 | thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 765 | { |
| 766 | struct thread *thread; |
| 767 | struct thread *next; |
| 768 | int ready = 0; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 769 | |
| 770 | assert (list); |
| 771 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 772 | for (thread = list->head; thread; thread = next) |
| 773 | { |
| 774 | next = thread->next; |
| 775 | |
| 776 | if (FD_ISSET (THREAD_FD (thread), fdset)) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 777 | { |
| 778 | assert (FD_ISSET (THREAD_FD (thread), mfdset)); |
| 779 | FD_CLR(THREAD_FD (thread), mfdset); |
| 780 | thread_list_delete (list, thread); |
| 781 | thread_list_add (&thread->master->ready, thread); |
| 782 | thread->type = THREAD_READY; |
| 783 | ready++; |
| 784 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 785 | } |
| 786 | return ready; |
| 787 | } |
| 788 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 789 | /* fetch next timer-like thread thread from the list and return it */ |
| 790 | static unsigned int |
| 791 | thread_timer_process (struct thread_list *list, struct timeval *timenow) |
| 792 | { |
| 793 | struct thread *thread; |
| 794 | unsigned int ready = 0; |
| 795 | |
| 796 | assert (list && timenow); |
| 797 | |
| 798 | for (thread = list->head; thread; thread = thread->next) |
| 799 | if (timeval_cmp (*timenow, thread->u.sands) >= 0) |
| 800 | { |
| 801 | thread_list_delete (list, thread); |
| 802 | assert (thread->next == thread->prev && thread->next == NULL); |
| 803 | thread->next = thread->prev = NULL; |
| 804 | thread->type = THREAD_READY; |
| 805 | thread_list_add (&thread->master->ready, thread); |
| 806 | ready++; |
| 807 | } |
| 808 | return ready; |
| 809 | } |
| 810 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 811 | /* Fetch next ready thread. */ |
| 812 | struct thread * |
| 813 | thread_fetch (struct thread_master *m, struct thread *fetch) |
| 814 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | struct thread *thread; |
| 816 | fd_set readfd; |
| 817 | fd_set writefd; |
| 818 | fd_set exceptfd; |
| 819 | struct timeval timer_now; |
| 820 | struct timeval timer_val; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 821 | struct timeval timer_val_bg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 822 | struct timeval *timer_wait; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 823 | struct timeval *timer_wait_bg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 824 | |
| 825 | while (1) |
| 826 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 827 | int num = 0; |
| 828 | int ready = m->ready.count; |
| 829 | |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 830 | /* Signals are highest priority */ |
| 831 | quagga_sigevent_process (); |
| 832 | |
| 833 | /* Normal event are the next highest priority. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 834 | if ((thread = thread_trim_head (&m->event)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 835 | return thread_run (m, thread, fetch); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 836 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 837 | /* Execute timer. */ |
| 838 | gettimeofday (&timer_now, NULL); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 839 | |
| 840 | /* Timer threads */ |
| 841 | ready += thread_timer_process (&m->timer, &timer_now); |
| 842 | |
| 843 | /* If there are any ready threads from previous scheduler runs, |
| 844 | * process top of them. |
| 845 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 846 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 847 | return thread_run (m, thread, fetch); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 848 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 849 | /* Structure copy. */ |
| 850 | readfd = m->readfd; |
| 851 | writefd = m->writefd; |
| 852 | exceptfd = m->exceptfd; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 853 | |
| 854 | /* Calculate select wait timer if nothing else to do */ |
| 855 | if (ready == 0) |
| 856 | { |
| 857 | timer_wait = thread_timer_wait (&m->timer, &timer_val); |
| 858 | timer_wait_bg = thread_timer_wait (&m->background, &timer_val_bg); |
| 859 | |
| 860 | if (timer_wait && timer_wait_bg && |
| 861 | (timeval_cmp (*timer_wait, *timer_wait_bg) > 0)) |
| 862 | timer_wait = timer_wait_bg; |
| 863 | else if (!timer_wait && timer_wait_bg) |
| 864 | timer_wait = timer_wait_bg; |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | timer_val.tv_sec = timer_val.tv_usec = 0; |
| 869 | timer_wait = &timer_val; |
| 870 | } |
| 871 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 872 | num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 873 | |
| 874 | /* Signals should get quick treatment */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 875 | if (num < 0) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 876 | { |
| 877 | if (errno == EINTR) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 878 | continue; /* signal received - process it */ |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 879 | zlog_warn ("select() error: %s", safe_strerror (errno)); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 880 | return NULL; |
| 881 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 882 | |
| 883 | /* Got IO, process it */ |
| 884 | if (num > 0) |
| 885 | { |
| 886 | /* Normal priority read thead. */ |
| 887 | ready += thread_process_fd (&m->read, &readfd, &m->readfd); |
| 888 | /* Write thead. */ |
| 889 | ready += thread_process_fd (&m->write, &writefd, &m->writefd); |
| 890 | } |
| 891 | |
| 892 | /* Background timer/events, lowest priority */ |
| 893 | ready += thread_timer_process (&m->background, &timer_now); |
| 894 | |
| 895 | /* if any threads were made ready above.. */ |
| 896 | if ((ready > 0) && (thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 897 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 901 | unsigned long |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 902 | thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start) |
| 903 | { |
| 904 | unsigned long thread_time; |
| 905 | |
| 906 | #ifdef HAVE_RUSAGE |
| 907 | /* This is 'user + sys' time. */ |
| 908 | thread_time = timeval_elapsed (now->ru_utime, start->ru_utime); |
| 909 | thread_time += timeval_elapsed (now->ru_stime, start->ru_stime); |
| 910 | #else |
| 911 | /* When rusage is not available, simple elapsed time is used. */ |
| 912 | thread_time = timeval_elapsed (*now, *start); |
| 913 | #endif /* HAVE_RUSAGE */ |
| 914 | |
| 915 | return thread_time; |
| 916 | } |
| 917 | |
| 918 | /* We should aim to yield after THREAD_YIELD_TIME_SLOT |
| 919 | milliseconds. */ |
| 920 | int |
| 921 | thread_should_yield (struct thread *thread) |
| 922 | { |
| 923 | RUSAGE_T ru; |
| 924 | |
| 925 | GETRUSAGE (&ru); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame^] | 926 | |
| 927 | return (thread_consumed_time (&ru, &thread->ru) > THREAD_YIELD_TIME_SLOT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /* We check thread consumed time. If the system has getrusage, we'll |
| 931 | use that to get indepth stats on the performance of the thread. If |
| 932 | not - we'll use gettimeofday for some guestimation. */ |
| 933 | void |
| 934 | thread_call (struct thread *thread) |
| 935 | { |
| 936 | unsigned long thread_time; |
| 937 | RUSAGE_T ru; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 938 | struct cpu_thread_history tmp, *cpu; |
| 939 | |
| 940 | tmp.func = thread->func; |
| 941 | tmp.funcname = thread->funcname; |
| 942 | cpu = hash_get(cpu_record, &tmp, cpu_record_hash_alloc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | |
| 944 | GETRUSAGE (&thread->ru); |
| 945 | |
| 946 | (*thread->func) (thread); |
| 947 | |
| 948 | GETRUSAGE (&ru); |
| 949 | |
| 950 | thread_time = thread_consumed_time (&ru, &thread->ru); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 951 | cpu->total += thread_time; |
| 952 | if (cpu->max < thread_time) |
| 953 | cpu->max = thread_time; |
| 954 | |
| 955 | ++cpu->total_calls; |
| 956 | cpu->types |= (1 << thread->add_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 957 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 958 | #ifdef CONSUMED_TIME_CHECK |
| 959 | if (thread_time > CONSUMED_TIME_CHECK) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 960 | { |
| 961 | /* |
| 962 | * We have a CPU Hog on our hands. |
| 963 | * Whinge about it now, so we're aware this is yet another task |
| 964 | * to fix. |
| 965 | */ |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 966 | zlog_warn ("CPU HOG: task %s (%lx) ran for %ldms", |
| 967 | thread->funcname, |
| 968 | (unsigned long) thread->func, |
| 969 | thread_time / 1000L); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 970 | } |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 971 | #endif /* CONSUMED_TIME_CHECK */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /* Execute thread */ |
| 975 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 976 | funcname_thread_execute (struct thread_master *m, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 977 | int (*func)(struct thread *), |
| 978 | void *arg, |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 979 | int val, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 980 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | { |
| 982 | struct thread dummy; |
| 983 | |
| 984 | memset (&dummy, 0, sizeof (struct thread)); |
| 985 | |
| 986 | dummy.type = THREAD_EVENT; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 987 | dummy.add_type = THREAD_EXECUTE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 988 | dummy.master = NULL; |
| 989 | dummy.func = func; |
| 990 | dummy.arg = arg; |
| 991 | dummy.u.val = val; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 992 | dummy.funcname = strip_funcname (funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 993 | thread_call (&dummy); |
| 994 | |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 995 | XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 996 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 997 | return NULL; |
| 998 | } |