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 | |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 33 | /* Recent absolute time of day */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 34 | struct timeval recent_time; |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 35 | static struct timeval last_recent_time; |
| 36 | /* Relative time, since startup */ |
| 37 | static struct timeval relative_time; |
| 38 | static struct timeval relative_time_base; |
| 39 | /* init flag */ |
| 40 | static unsigned short timers_inited; |
| 41 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 42 | static struct hash *cpu_record = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | |
| 44 | /* Struct timeval's tv_usec one second value. */ |
| 45 | #define TIMER_SECOND_MICRO 1000000L |
| 46 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 47 | /* Adjust so that tv_usec is in the range [0,TIMER_SECOND_MICRO). |
| 48 | And change negative values to 0. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 49 | static struct timeval |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | timeval_adjust (struct timeval a) |
| 51 | { |
| 52 | while (a.tv_usec >= TIMER_SECOND_MICRO) |
| 53 | { |
| 54 | a.tv_usec -= TIMER_SECOND_MICRO; |
| 55 | a.tv_sec++; |
| 56 | } |
| 57 | |
| 58 | while (a.tv_usec < 0) |
| 59 | { |
| 60 | a.tv_usec += TIMER_SECOND_MICRO; |
| 61 | a.tv_sec--; |
| 62 | } |
| 63 | |
| 64 | if (a.tv_sec < 0) |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 65 | /* Change negative timeouts to 0. */ |
| 66 | a.tv_sec = a.tv_usec = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | |
| 68 | return a; |
| 69 | } |
| 70 | |
| 71 | static struct timeval |
| 72 | timeval_subtract (struct timeval a, struct timeval b) |
| 73 | { |
| 74 | struct timeval ret; |
| 75 | |
| 76 | ret.tv_usec = a.tv_usec - b.tv_usec; |
| 77 | ret.tv_sec = a.tv_sec - b.tv_sec; |
| 78 | |
| 79 | return timeval_adjust (ret); |
| 80 | } |
| 81 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 82 | static long |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | timeval_cmp (struct timeval a, struct timeval b) |
| 84 | { |
| 85 | return (a.tv_sec == b.tv_sec |
| 86 | ? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec); |
| 87 | } |
| 88 | |
| 89 | static unsigned long |
| 90 | timeval_elapsed (struct timeval a, struct timeval b) |
| 91 | { |
| 92 | return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO) |
| 93 | + (a.tv_usec - b.tv_usec)); |
| 94 | } |
| 95 | |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 96 | #ifndef HAVE_CLOCK_MONOTONIC |
| 97 | static void |
| 98 | quagga_gettimeofday_relative_adjust (void) |
| 99 | { |
| 100 | struct timeval diff; |
| 101 | if (timeval_cmp (recent_time, last_recent_time) < 0) |
| 102 | { |
| 103 | relative_time.tv_sec++; |
| 104 | relative_time.tv_usec = 0; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | diff = timeval_subtract (recent_time, last_recent_time); |
| 109 | relative_time.tv_sec += diff.tv_sec; |
| 110 | relative_time.tv_usec += diff.tv_usec; |
| 111 | relative_time = timeval_adjust (relative_time); |
| 112 | } |
| 113 | last_recent_time = recent_time; |
| 114 | } |
| 115 | #endif /* !HAVE_CLOCK_MONOTONIC */ |
| 116 | |
| 117 | /* gettimeofday wrapper, to keep recent_time updated */ |
| 118 | static int |
| 119 | quagga_gettimeofday (struct timeval *tv) |
| 120 | { |
| 121 | int ret; |
| 122 | |
| 123 | assert (tv); |
| 124 | |
| 125 | if (!(ret = gettimeofday (&recent_time, NULL))) |
| 126 | { |
| 127 | /* init... */ |
| 128 | if (!timers_inited) |
| 129 | { |
| 130 | relative_time_base = last_recent_time = recent_time; |
| 131 | timers_inited = 1; |
| 132 | } |
| 133 | /* avoid copy if user passed recent_time pointer.. */ |
| 134 | if (tv != &recent_time) |
| 135 | *tv = recent_time; |
| 136 | return 0; |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int |
| 142 | quagga_get_relative (struct timeval *tv) |
| 143 | { |
| 144 | int ret; |
| 145 | |
| 146 | #ifdef HAVE_CLOCK_MONOTONIC |
| 147 | { |
| 148 | struct timespec tp; |
| 149 | if (!(ret = clock_gettime (CLOCK_MONOTONIC, &tp))) |
| 150 | { |
| 151 | relative_time.tv_sec = tp.tv_sec; |
| 152 | relative_time.tv_usec = tp.tv_nsec / 1000; |
| 153 | } |
| 154 | } |
| 155 | #else /* !HAVE_CLOCK_MONOTONIC */ |
| 156 | if (!(ret = quagga_gettimeofday (&recent_time))) |
| 157 | quagga_gettimeofday_relative_adjust(); |
| 158 | #endif /* HAVE_CLOCK_MONOTONIC */ |
| 159 | |
| 160 | if (tv) |
| 161 | *tv = relative_time; |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | /* Get absolute time stamp, but in terms of the internal timer |
| 167 | * Could be wrong, but at least won't go back. |
| 168 | */ |
| 169 | static void |
| 170 | quagga_real_stabilised (struct timeval *tv) |
| 171 | { |
| 172 | *tv = relative_time_base; |
| 173 | tv->tv_sec += relative_time.tv_sec; |
| 174 | tv->tv_usec += relative_time.tv_usec; |
| 175 | *tv = timeval_adjust (*tv); |
| 176 | } |
| 177 | |
| 178 | /* Exported Quagga timestamp function. |
| 179 | * Modelled on POSIX clock_gettime. |
| 180 | */ |
| 181 | int |
| 182 | quagga_gettime (enum quagga_clkid clkid, struct timeval *tv) |
| 183 | { |
| 184 | switch (clkid) |
| 185 | { |
| 186 | case QUAGGA_CLK_REALTIME: |
| 187 | return quagga_gettimeofday (tv); |
| 188 | case QUAGGA_CLK_MONOTONIC: |
| 189 | return quagga_get_relative (tv); |
| 190 | case QUAGGA_CLK_REALTIME_STABILISED: |
| 191 | quagga_real_stabilised (tv); |
| 192 | return 0; |
| 193 | default: |
| 194 | errno = EINVAL; |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* time_t value in terms of stabilised absolute time. |
| 200 | * replacement for POSIX time() |
| 201 | */ |
| 202 | time_t |
| 203 | quagga_time (time_t *t) |
| 204 | { |
| 205 | struct timeval tv; |
| 206 | quagga_real_stabilised (&tv); |
| 207 | if (t) |
| 208 | *t = tv.tv_sec; |
| 209 | return tv.tv_sec; |
| 210 | } |
| 211 | |
| 212 | /* Public export of recent_relative_time by value */ |
| 213 | struct timeval |
| 214 | recent_relative_time (void) |
| 215 | { |
| 216 | return relative_time; |
| 217 | } |
| 218 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 219 | static unsigned int |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 220 | cpu_record_hash_key (struct cpu_thread_history *a) |
| 221 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 222 | return (uintptr_t) a->func; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 226 | cpu_record_hash_cmp (const struct cpu_thread_history *a, |
| 227 | const struct cpu_thread_history *b) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 228 | { |
| 229 | return a->func == b->func; |
| 230 | } |
| 231 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 232 | static void * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 233 | cpu_record_hash_alloc (struct cpu_thread_history *a) |
| 234 | { |
| 235 | struct cpu_thread_history *new; |
paul | 039b957 | 2004-10-31 16:43:17 +0000 | [diff] [blame] | 236 | new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history)); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 237 | new->func = a->func; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 238 | new->funcname = XSTRDUP(MTYPE_THREAD_FUNCNAME, a->funcname); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 239 | return new; |
| 240 | } |
| 241 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 242 | static void |
| 243 | cpu_record_hash_free (void *a) |
| 244 | { |
| 245 | struct cpu_thread_history *hist = a; |
| 246 | |
| 247 | XFREE (MTYPE_THREAD_FUNCNAME, hist->funcname); |
| 248 | XFREE (MTYPE_THREAD_STATS, hist); |
| 249 | } |
| 250 | |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 251 | static void |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 252 | vty_out_cpu_thread_history(struct vty* vty, |
| 253 | struct cpu_thread_history *a) |
| 254 | { |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 255 | #ifdef HAVE_RUSAGE |
| 256 | vty_out(vty, "%7ld.%03ld %9d %8ld %9ld %8ld %9ld", |
| 257 | a->cpu.total/1000, a->cpu.total%1000, a->total_calls, |
| 258 | a->cpu.total/a->total_calls, a->cpu.max, |
| 259 | a->real.total/a->total_calls, a->real.max); |
| 260 | #else |
| 261 | vty_out(vty, "%7ld.%03ld %9d %8ld %9ld", |
| 262 | a->real.total/1000, a->real.total%1000, a->total_calls, |
| 263 | a->real.total/a->total_calls, a->real.max); |
| 264 | #endif |
| 265 | vty_out(vty, " %c%c%c%c%c%c %s%s", |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 266 | a->types & (1 << THREAD_READ) ? 'R':' ', |
| 267 | a->types & (1 << THREAD_WRITE) ? 'W':' ', |
| 268 | a->types & (1 << THREAD_TIMER) ? 'T':' ', |
| 269 | a->types & (1 << THREAD_EVENT) ? 'E':' ', |
| 270 | a->types & (1 << THREAD_EXECUTE) ? 'X':' ', |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 271 | a->types & (1 << THREAD_BACKGROUND) ? 'B' : ' ', |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 272 | a->funcname, VTY_NEWLINE); |
| 273 | } |
| 274 | |
| 275 | static void |
| 276 | cpu_record_hash_print(struct hash_backet *bucket, |
| 277 | void *args[]) |
| 278 | { |
| 279 | struct cpu_thread_history *totals = args[0]; |
| 280 | struct vty *vty = args[1]; |
Paul Jakma | 41b2373 | 2009-06-30 16:12:49 +0100 | [diff] [blame] | 281 | thread_type *filter = args[2]; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 282 | struct cpu_thread_history *a = bucket->data; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 283 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 284 | a = bucket->data; |
| 285 | if ( !(a->types & *filter) ) |
| 286 | return; |
| 287 | vty_out_cpu_thread_history(vty,a); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 288 | totals->total_calls += a->total_calls; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 289 | totals->real.total += a->real.total; |
| 290 | if (totals->real.max < a->real.max) |
| 291 | totals->real.max = a->real.max; |
| 292 | #ifdef HAVE_RUSAGE |
| 293 | totals->cpu.total += a->cpu.total; |
| 294 | if (totals->cpu.max < a->cpu.max) |
| 295 | totals->cpu.max = a->cpu.max; |
| 296 | #endif |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void |
Paul Jakma | 41b2373 | 2009-06-30 16:12:49 +0100 | [diff] [blame] | 300 | cpu_record_print(struct vty *vty, thread_type filter) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 301 | { |
| 302 | struct cpu_thread_history tmp; |
| 303 | void *args[3] = {&tmp, vty, &filter}; |
| 304 | |
| 305 | memset(&tmp, 0, sizeof tmp); |
Paul Jakma | 8526100 | 2010-01-11 16:30:45 +0000 | [diff] [blame] | 306 | tmp.funcname = (char *)"TOTAL"; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 307 | tmp.types = filter; |
| 308 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 309 | #ifdef HAVE_RUSAGE |
| 310 | vty_out(vty, "%21s %18s %18s%s", |
| 311 | "", "CPU (user+system):", "Real (wall-clock):", VTY_NEWLINE); |
| 312 | #endif |
| 313 | vty_out(vty, "Runtime(ms) Invoked Avg uSec Max uSecs"); |
| 314 | #ifdef HAVE_RUSAGE |
| 315 | vty_out(vty, " Avg uSec Max uSecs"); |
| 316 | #endif |
| 317 | vty_out(vty, " Type Thread%s", VTY_NEWLINE); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 318 | hash_iterate(cpu_record, |
| 319 | (void(*)(struct hash_backet*,void*))cpu_record_hash_print, |
| 320 | args); |
| 321 | |
| 322 | if (tmp.total_calls > 0) |
| 323 | vty_out_cpu_thread_history(vty, &tmp); |
| 324 | } |
| 325 | |
| 326 | DEFUN(show_thread_cpu, |
| 327 | show_thread_cpu_cmd, |
| 328 | "show thread cpu [FILTER]", |
| 329 | SHOW_STR |
| 330 | "Thread information\n" |
| 331 | "Thread CPU usage\n" |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 332 | "Display filter (rwtexb)\n") |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 333 | { |
| 334 | int i = 0; |
Paul Jakma | 41b2373 | 2009-06-30 16:12:49 +0100 | [diff] [blame] | 335 | thread_type filter = (thread_type) -1U; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 336 | |
| 337 | if (argc > 0) |
| 338 | { |
| 339 | filter = 0; |
| 340 | while (argv[0][i] != '\0') |
| 341 | { |
| 342 | switch ( argv[0][i] ) |
| 343 | { |
| 344 | case 'r': |
| 345 | case 'R': |
| 346 | filter |= (1 << THREAD_READ); |
| 347 | break; |
| 348 | case 'w': |
| 349 | case 'W': |
| 350 | filter |= (1 << THREAD_WRITE); |
| 351 | break; |
| 352 | case 't': |
| 353 | case 'T': |
| 354 | filter |= (1 << THREAD_TIMER); |
| 355 | break; |
| 356 | case 'e': |
| 357 | case 'E': |
| 358 | filter |= (1 << THREAD_EVENT); |
| 359 | break; |
| 360 | case 'x': |
| 361 | case 'X': |
| 362 | filter |= (1 << THREAD_EXECUTE); |
| 363 | break; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 364 | case 'b': |
| 365 | case 'B': |
| 366 | filter |= (1 << THREAD_BACKGROUND); |
| 367 | break; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 368 | default: |
| 369 | break; |
| 370 | } |
| 371 | ++i; |
| 372 | } |
| 373 | if (filter == 0) |
| 374 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 375 | vty_out(vty, "Invalid filter \"%s\" specified," |
| 376 | " must contain at least one of 'RWTEXB'%s", |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 377 | argv[0], VTY_NEWLINE); |
| 378 | return CMD_WARNING; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | cpu_record_print(vty, filter); |
| 383 | return CMD_SUCCESS; |
| 384 | } |
Paul Jakma | e276eb8 | 2010-01-09 16:15:00 +0000 | [diff] [blame] | 385 | |
| 386 | static void |
| 387 | cpu_record_hash_clear (struct hash_backet *bucket, |
| 388 | void *args) |
| 389 | { |
| 390 | thread_type *filter = args; |
| 391 | struct cpu_thread_history *a = bucket->data; |
| 392 | |
| 393 | a = bucket->data; |
| 394 | if ( !(a->types & *filter) ) |
| 395 | return; |
| 396 | |
| 397 | hash_release (cpu_record, bucket->data); |
| 398 | } |
| 399 | |
| 400 | static void |
| 401 | cpu_record_clear (thread_type filter) |
| 402 | { |
| 403 | thread_type *tmp = &filter; |
| 404 | hash_iterate (cpu_record, |
| 405 | (void (*) (struct hash_backet*,void*)) cpu_record_hash_clear, |
| 406 | tmp); |
| 407 | } |
| 408 | |
| 409 | DEFUN(clear_thread_cpu, |
| 410 | clear_thread_cpu_cmd, |
| 411 | "clear thread cpu [FILTER]", |
| 412 | "Clear stored data\n" |
| 413 | "Thread information\n" |
| 414 | "Thread CPU usage\n" |
| 415 | "Display filter (rwtexb)\n") |
| 416 | { |
| 417 | int i = 0; |
| 418 | thread_type filter = (thread_type) -1U; |
| 419 | |
| 420 | if (argc > 0) |
| 421 | { |
| 422 | filter = 0; |
| 423 | while (argv[0][i] != '\0') |
| 424 | { |
| 425 | switch ( argv[0][i] ) |
| 426 | { |
| 427 | case 'r': |
| 428 | case 'R': |
| 429 | filter |= (1 << THREAD_READ); |
| 430 | break; |
| 431 | case 'w': |
| 432 | case 'W': |
| 433 | filter |= (1 << THREAD_WRITE); |
| 434 | break; |
| 435 | case 't': |
| 436 | case 'T': |
| 437 | filter |= (1 << THREAD_TIMER); |
| 438 | break; |
| 439 | case 'e': |
| 440 | case 'E': |
| 441 | filter |= (1 << THREAD_EVENT); |
| 442 | break; |
| 443 | case 'x': |
| 444 | case 'X': |
| 445 | filter |= (1 << THREAD_EXECUTE); |
| 446 | break; |
| 447 | case 'b': |
| 448 | case 'B': |
| 449 | filter |= (1 << THREAD_BACKGROUND); |
| 450 | break; |
| 451 | default: |
| 452 | break; |
| 453 | } |
| 454 | ++i; |
| 455 | } |
| 456 | if (filter == 0) |
| 457 | { |
| 458 | vty_out(vty, "Invalid filter \"%s\" specified," |
| 459 | " must contain at least one of 'RWTEXB'%s", |
| 460 | argv[0], VTY_NEWLINE); |
| 461 | return CMD_WARNING; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | cpu_record_clear (filter); |
| 466 | return CMD_SUCCESS; |
| 467 | } |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 468 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 469 | /* List allocation and head/tail print out. */ |
| 470 | static void |
| 471 | thread_list_debug (struct thread_list *list) |
| 472 | { |
| 473 | printf ("count [%d] head [%p] tail [%p]\n", |
| 474 | list->count, list->head, list->tail); |
| 475 | } |
| 476 | |
| 477 | /* Debug print for thread_master. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 478 | static void __attribute__ ((unused)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 479 | thread_master_debug (struct thread_master *m) |
| 480 | { |
| 481 | printf ("-----------\n"); |
| 482 | printf ("readlist : "); |
| 483 | thread_list_debug (&m->read); |
| 484 | printf ("writelist : "); |
| 485 | thread_list_debug (&m->write); |
| 486 | printf ("timerlist : "); |
| 487 | thread_list_debug (&m->timer); |
| 488 | printf ("eventlist : "); |
| 489 | thread_list_debug (&m->event); |
| 490 | printf ("unuselist : "); |
| 491 | thread_list_debug (&m->unuse); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 492 | printf ("bgndlist : "); |
| 493 | thread_list_debug (&m->background); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 494 | printf ("total alloc: [%ld]\n", m->alloc); |
| 495 | printf ("-----------\n"); |
| 496 | } |
| 497 | |
| 498 | /* Allocate new thread master. */ |
| 499 | struct thread_master * |
| 500 | thread_master_create () |
| 501 | { |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 502 | if (cpu_record == NULL) |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 503 | cpu_record |
| 504 | = hash_create_size (1011, (unsigned int (*) (void *))cpu_record_hash_key, |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 505 | (int (*) (const void *, const void *))cpu_record_hash_cmp); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 506 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER, |
| 508 | sizeof (struct thread_master)); |
| 509 | } |
| 510 | |
| 511 | /* Add a new thread to the list. */ |
| 512 | static void |
| 513 | thread_list_add (struct thread_list *list, struct thread *thread) |
| 514 | { |
| 515 | thread->next = NULL; |
| 516 | thread->prev = list->tail; |
| 517 | if (list->tail) |
| 518 | list->tail->next = thread; |
| 519 | else |
| 520 | list->head = thread; |
| 521 | list->tail = thread; |
| 522 | list->count++; |
| 523 | } |
| 524 | |
| 525 | /* Add a new thread just before the point. */ |
| 526 | static void |
| 527 | thread_list_add_before (struct thread_list *list, |
| 528 | struct thread *point, |
| 529 | struct thread *thread) |
| 530 | { |
| 531 | thread->next = point; |
| 532 | thread->prev = point->prev; |
| 533 | if (point->prev) |
| 534 | point->prev->next = thread; |
| 535 | else |
| 536 | list->head = thread; |
| 537 | point->prev = thread; |
| 538 | list->count++; |
| 539 | } |
| 540 | |
| 541 | /* Delete a thread from the list. */ |
| 542 | static struct thread * |
| 543 | thread_list_delete (struct thread_list *list, struct thread *thread) |
| 544 | { |
| 545 | if (thread->next) |
| 546 | thread->next->prev = thread->prev; |
| 547 | else |
| 548 | list->tail = thread->prev; |
| 549 | if (thread->prev) |
| 550 | thread->prev->next = thread->next; |
| 551 | else |
| 552 | list->head = thread->next; |
| 553 | thread->next = thread->prev = NULL; |
| 554 | list->count--; |
| 555 | return thread; |
| 556 | } |
| 557 | |
| 558 | /* Move thread to unuse list. */ |
| 559 | static void |
| 560 | thread_add_unuse (struct thread_master *m, struct thread *thread) |
| 561 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 562 | assert (m != NULL && thread != NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 563 | assert (thread->next == NULL); |
| 564 | assert (thread->prev == NULL); |
| 565 | assert (thread->type == THREAD_UNUSED); |
| 566 | thread_list_add (&m->unuse, thread); |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 567 | /* XXX: Should we deallocate funcname here? */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* Free all unused thread. */ |
| 571 | static void |
| 572 | thread_list_free (struct thread_master *m, struct thread_list *list) |
| 573 | { |
| 574 | struct thread *t; |
| 575 | struct thread *next; |
| 576 | |
| 577 | for (t = list->head; t; t = next) |
| 578 | { |
| 579 | next = t->next; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 580 | if (t->funcname) |
| 581 | XFREE (MTYPE_THREAD_FUNCNAME, t->funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 582 | XFREE (MTYPE_THREAD, t); |
| 583 | list->count--; |
| 584 | m->alloc--; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* Stop thread scheduler. */ |
| 589 | void |
| 590 | thread_master_free (struct thread_master *m) |
| 591 | { |
| 592 | thread_list_free (m, &m->read); |
| 593 | thread_list_free (m, &m->write); |
| 594 | thread_list_free (m, &m->timer); |
| 595 | thread_list_free (m, &m->event); |
| 596 | thread_list_free (m, &m->ready); |
| 597 | thread_list_free (m, &m->unuse); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 598 | thread_list_free (m, &m->background); |
| 599 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 600 | XFREE (MTYPE_THREAD_MASTER, m); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 601 | |
| 602 | if (cpu_record) |
| 603 | { |
| 604 | hash_clean (cpu_record, cpu_record_hash_free); |
| 605 | hash_free (cpu_record); |
| 606 | cpu_record = NULL; |
| 607 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 608 | } |
| 609 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 610 | /* Thread list is empty or not. */ |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 611 | static int |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 612 | thread_empty (struct thread_list *list) |
| 613 | { |
| 614 | return list->head ? 0 : 1; |
| 615 | } |
| 616 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 617 | /* Delete top of the list and return it. */ |
| 618 | static struct thread * |
| 619 | thread_trim_head (struct thread_list *list) |
| 620 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 621 | if (!thread_empty (list)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | return thread_list_delete (list, list->head); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 626 | /* Return remain time in second. */ |
| 627 | unsigned long |
| 628 | thread_timer_remain_second (struct thread *thread) |
| 629 | { |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 630 | quagga_get_relative (NULL); |
| 631 | |
| 632 | if (thread->u.sands.tv_sec - relative_time.tv_sec > 0) |
| 633 | return thread->u.sands.tv_sec - relative_time.tv_sec; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | else |
| 635 | return 0; |
| 636 | } |
| 637 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 638 | /* Trim blankspace and "()"s */ |
| 639 | static char * |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 640 | strip_funcname (const char *funcname) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 641 | { |
| 642 | char buff[100]; |
| 643 | char tmp, *ret, *e, *b = buff; |
| 644 | |
| 645 | strncpy(buff, funcname, sizeof(buff)); |
| 646 | buff[ sizeof(buff) -1] = '\0'; |
| 647 | e = buff +strlen(buff) -1; |
| 648 | |
| 649 | /* Wont work for funcname == "Word (explanation)" */ |
| 650 | |
| 651 | while (*b == ' ' || *b == '(') |
| 652 | ++b; |
| 653 | while (*e == ' ' || *e == ')') |
| 654 | --e; |
| 655 | e++; |
| 656 | |
| 657 | tmp = *e; |
| 658 | *e = '\0'; |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 659 | ret = XSTRDUP (MTYPE_THREAD_FUNCNAME, b); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 660 | *e = tmp; |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | /* Get new thread. */ |
| 666 | static struct thread * |
| 667 | thread_get (struct thread_master *m, u_char type, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 668 | int (*func) (struct thread *), void *arg, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 669 | { |
Jorge Boncompte [DTI2] | 6401832 | 2012-05-07 16:53:13 +0000 | [diff] [blame^] | 670 | struct thread *thread = thread_trim_head (&m->unuse); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 671 | |
Jorge Boncompte [DTI2] | 6401832 | 2012-05-07 16:53:13 +0000 | [diff] [blame^] | 672 | if (thread) |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 673 | { |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 674 | if (thread->funcname) |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 675 | XFREE(MTYPE_THREAD_FUNCNAME, thread->funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 676 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | else |
| 678 | { |
| 679 | thread = XCALLOC (MTYPE_THREAD, sizeof (struct thread)); |
| 680 | m->alloc++; |
| 681 | } |
| 682 | thread->type = type; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 683 | thread->add_type = type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 684 | thread->master = m; |
| 685 | thread->func = func; |
| 686 | thread->arg = arg; |
| 687 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 688 | thread->funcname = strip_funcname(funcname); |
| 689 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | return thread; |
| 691 | } |
| 692 | |
| 693 | /* Add new read thread. */ |
| 694 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 695 | funcname_thread_add_read (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 696 | int (*func) (struct thread *), void *arg, int fd, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 697 | { |
| 698 | struct thread *thread; |
| 699 | |
| 700 | assert (m != NULL); |
| 701 | |
| 702 | if (FD_ISSET (fd, &m->readfd)) |
| 703 | { |
| 704 | zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd); |
| 705 | return NULL; |
| 706 | } |
| 707 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 708 | thread = thread_get (m, THREAD_READ, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | FD_SET (fd, &m->readfd); |
| 710 | thread->u.fd = fd; |
| 711 | thread_list_add (&m->read, thread); |
| 712 | |
| 713 | return thread; |
| 714 | } |
| 715 | |
| 716 | /* Add new write thread. */ |
| 717 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 718 | funcname_thread_add_write (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 719 | int (*func) (struct thread *), void *arg, int fd, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 720 | { |
| 721 | struct thread *thread; |
| 722 | |
| 723 | assert (m != NULL); |
| 724 | |
| 725 | if (FD_ISSET (fd, &m->writefd)) |
| 726 | { |
| 727 | zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd); |
| 728 | return NULL; |
| 729 | } |
| 730 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 731 | thread = thread_get (m, THREAD_WRITE, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 732 | FD_SET (fd, &m->writefd); |
| 733 | thread->u.fd = fd; |
| 734 | thread_list_add (&m->write, thread); |
| 735 | |
| 736 | return thread; |
| 737 | } |
| 738 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 739 | static struct thread * |
| 740 | funcname_thread_add_timer_timeval (struct thread_master *m, |
| 741 | int (*func) (struct thread *), |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 742 | int type, |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 743 | void *arg, |
| 744 | struct timeval *time_relative, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 745 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 747 | struct thread *thread; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 748 | struct thread_list *list; |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 749 | struct timeval alarm_time; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 750 | struct thread *tt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 751 | |
| 752 | assert (m != NULL); |
| 753 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 754 | assert (type == THREAD_TIMER || type == THREAD_BACKGROUND); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 755 | assert (time_relative); |
| 756 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 757 | list = ((type == THREAD_TIMER) ? &m->timer : &m->background); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 758 | thread = thread_get (m, type, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 759 | |
| 760 | /* Do we need jitter here? */ |
Joakim Tjernlund | b819276 | 2008-11-10 09:33:30 +0100 | [diff] [blame] | 761 | quagga_get_relative (NULL); |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 762 | alarm_time.tv_sec = relative_time.tv_sec + time_relative->tv_sec; |
| 763 | alarm_time.tv_usec = relative_time.tv_usec + time_relative->tv_usec; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 764 | thread->u.sands = timeval_adjust(alarm_time); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 765 | |
| 766 | /* Sort by timeval. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 767 | for (tt = list->head; tt; tt = tt->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 768 | if (timeval_cmp (thread->u.sands, tt->u.sands) <= 0) |
| 769 | break; |
| 770 | |
| 771 | if (tt) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 772 | thread_list_add_before (list, tt, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 773 | else |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 774 | thread_list_add (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 775 | |
| 776 | return thread; |
| 777 | } |
| 778 | |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 779 | |
| 780 | /* Add timer event thread. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 781 | struct thread * |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 782 | funcname_thread_add_timer (struct thread_master *m, |
| 783 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 784 | void *arg, long timer, const char* funcname) |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 785 | { |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 786 | struct timeval trel; |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 787 | |
| 788 | assert (m != NULL); |
| 789 | |
paul | 9076fbd | 2004-10-11 09:40:58 +0000 | [diff] [blame] | 790 | trel.tv_sec = timer; |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 791 | trel.tv_usec = 0; |
| 792 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 793 | return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, |
| 794 | &trel, funcname); |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* Add timer event thread with "millisecond" resolution */ |
| 798 | struct thread * |
| 799 | funcname_thread_add_timer_msec (struct thread_master *m, |
| 800 | int (*func) (struct thread *), |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 801 | void *arg, long timer, const char* funcname) |
paul | 98c91ac | 2004-10-05 14:57:50 +0000 | [diff] [blame] | 802 | { |
| 803 | struct timeval trel; |
| 804 | |
| 805 | assert (m != NULL); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 806 | |
ajs | af04bd7 | 2004-12-28 17:00:12 +0000 | [diff] [blame] | 807 | trel.tv_sec = timer / 1000; |
| 808 | trel.tv_usec = 1000*(timer % 1000); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 809 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 810 | return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, |
| 811 | arg, &trel, funcname); |
| 812 | } |
| 813 | |
| 814 | /* Add a background thread, with an optional millisec delay */ |
| 815 | struct thread * |
| 816 | funcname_thread_add_background (struct thread_master *m, |
| 817 | int (*func) (struct thread *), |
| 818 | void *arg, long delay, |
| 819 | const char *funcname) |
| 820 | { |
| 821 | struct timeval trel; |
| 822 | |
| 823 | assert (m != NULL); |
| 824 | |
| 825 | if (delay) |
| 826 | { |
| 827 | trel.tv_sec = delay / 1000; |
| 828 | trel.tv_usec = 1000*(delay % 1000); |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | trel.tv_sec = 0; |
| 833 | trel.tv_usec = 0; |
| 834 | } |
| 835 | |
| 836 | return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND, |
| 837 | arg, &trel, funcname); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 838 | } |
| 839 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 840 | /* Add simple event thread. */ |
| 841 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 842 | funcname_thread_add_event (struct thread_master *m, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 843 | int (*func) (struct thread *), void *arg, int val, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | { |
| 845 | struct thread *thread; |
| 846 | |
| 847 | assert (m != NULL); |
| 848 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 849 | thread = thread_get (m, THREAD_EVENT, func, arg, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 850 | thread->u.val = val; |
| 851 | thread_list_add (&m->event, thread); |
| 852 | |
| 853 | return thread; |
| 854 | } |
| 855 | |
| 856 | /* Cancel thread from scheduler. */ |
| 857 | void |
| 858 | thread_cancel (struct thread *thread) |
| 859 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 860 | struct thread_list *list; |
| 861 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 862 | switch (thread->type) |
| 863 | { |
| 864 | case THREAD_READ: |
| 865 | assert (FD_ISSET (thread->u.fd, &thread->master->readfd)); |
| 866 | FD_CLR (thread->u.fd, &thread->master->readfd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 867 | list = &thread->master->read; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 868 | break; |
| 869 | case THREAD_WRITE: |
| 870 | assert (FD_ISSET (thread->u.fd, &thread->master->writefd)); |
| 871 | FD_CLR (thread->u.fd, &thread->master->writefd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 872 | list = &thread->master->write; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 873 | break; |
| 874 | case THREAD_TIMER: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 875 | list = &thread->master->timer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 876 | break; |
| 877 | case THREAD_EVENT: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 878 | list = &thread->master->event; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 879 | break; |
| 880 | case THREAD_READY: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 881 | list = &thread->master->ready; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 882 | break; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 883 | case THREAD_BACKGROUND: |
| 884 | list = &thread->master->background; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 885 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 886 | default: |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 887 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 888 | break; |
| 889 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 890 | thread_list_delete (list, thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 891 | thread->type = THREAD_UNUSED; |
| 892 | thread_add_unuse (thread->master, thread); |
| 893 | } |
| 894 | |
| 895 | /* Delete all events which has argument value arg. */ |
paul | dc81807 | 2005-05-19 01:30:53 +0000 | [diff] [blame] | 896 | unsigned int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 897 | thread_cancel_event (struct thread_master *m, void *arg) |
| 898 | { |
paul | dc81807 | 2005-05-19 01:30:53 +0000 | [diff] [blame] | 899 | unsigned int ret = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 900 | struct thread *thread; |
| 901 | |
| 902 | thread = m->event.head; |
| 903 | while (thread) |
| 904 | { |
| 905 | struct thread *t; |
| 906 | |
| 907 | t = thread; |
| 908 | thread = t->next; |
| 909 | |
| 910 | if (t->arg == arg) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 911 | { |
paul | dc81807 | 2005-05-19 01:30:53 +0000 | [diff] [blame] | 912 | ret++; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 913 | thread_list_delete (&m->event, t); |
| 914 | t->type = THREAD_UNUSED; |
| 915 | thread_add_unuse (m, t); |
| 916 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | } |
Jorge Boncompte [DTI2] | 1b79fcb | 2012-05-07 15:17:31 +0000 | [diff] [blame] | 918 | |
| 919 | /* thread can be on the ready list too */ |
| 920 | thread = m->ready.head; |
| 921 | while (thread) |
| 922 | { |
| 923 | struct thread *t; |
| 924 | |
| 925 | t = thread; |
| 926 | thread = t->next; |
| 927 | |
| 928 | if (t->arg == arg) |
| 929 | { |
| 930 | ret++; |
| 931 | thread_list_delete (&m->ready, t); |
| 932 | t->type = THREAD_UNUSED; |
| 933 | thread_add_unuse (m, t); |
| 934 | } |
| 935 | } |
paul | dc81807 | 2005-05-19 01:30:53 +0000 | [diff] [blame] | 936 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 937 | } |
| 938 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 939 | static struct timeval * |
| 940 | thread_timer_wait (struct thread_list *tlist, struct timeval *timer_val) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 942 | if (!thread_empty (tlist)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | { |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 944 | *timer_val = timeval_subtract (tlist->head->u.sands, relative_time); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 945 | return timer_val; |
| 946 | } |
| 947 | return NULL; |
| 948 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 950 | static struct thread * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 951 | thread_run (struct thread_master *m, struct thread *thread, |
| 952 | struct thread *fetch) |
| 953 | { |
| 954 | *fetch = *thread; |
| 955 | thread->type = THREAD_UNUSED; |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 956 | thread->funcname = NULL; /* thread_call will free fetch's copied pointer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 957 | thread_add_unuse (m, thread); |
| 958 | return fetch; |
| 959 | } |
| 960 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 961 | static int |
| 962 | thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 963 | { |
| 964 | struct thread *thread; |
| 965 | struct thread *next; |
| 966 | int ready = 0; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 967 | |
| 968 | assert (list); |
| 969 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 970 | for (thread = list->head; thread; thread = next) |
| 971 | { |
| 972 | next = thread->next; |
| 973 | |
| 974 | if (FD_ISSET (THREAD_FD (thread), fdset)) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 975 | { |
| 976 | assert (FD_ISSET (THREAD_FD (thread), mfdset)); |
| 977 | FD_CLR(THREAD_FD (thread), mfdset); |
| 978 | thread_list_delete (list, thread); |
| 979 | thread_list_add (&thread->master->ready, thread); |
| 980 | thread->type = THREAD_READY; |
| 981 | ready++; |
| 982 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | } |
| 984 | return ready; |
| 985 | } |
| 986 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 987 | /* Add all timers that have popped to the ready list. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 988 | static unsigned int |
| 989 | thread_timer_process (struct thread_list *list, struct timeval *timenow) |
| 990 | { |
| 991 | struct thread *thread; |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 992 | struct thread *next; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 993 | unsigned int ready = 0; |
| 994 | |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 995 | for (thread = list->head; thread; thread = next) |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 996 | { |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 997 | next = thread->next; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 998 | if (timeval_cmp (*timenow, thread->u.sands) < 0) |
| 999 | return ready; |
| 1000 | thread_list_delete (list, thread); |
| 1001 | thread->type = THREAD_READY; |
| 1002 | thread_list_add (&thread->master->ready, thread); |
| 1003 | ready++; |
| 1004 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1005 | return ready; |
| 1006 | } |
| 1007 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1008 | /* process a list en masse, e.g. for event thread lists */ |
| 1009 | static unsigned int |
| 1010 | thread_process (struct thread_list *list) |
| 1011 | { |
| 1012 | struct thread *thread; |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 1013 | struct thread *next; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1014 | unsigned int ready = 0; |
| 1015 | |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 1016 | for (thread = list->head; thread; thread = next) |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1017 | { |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 1018 | next = thread->next; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1019 | thread_list_delete (list, thread); |
| 1020 | thread->type = THREAD_READY; |
| 1021 | thread_list_add (&thread->master->ready, thread); |
| 1022 | ready++; |
| 1023 | } |
| 1024 | return ready; |
| 1025 | } |
| 1026 | |
| 1027 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1028 | /* Fetch next ready thread. */ |
| 1029 | struct thread * |
| 1030 | thread_fetch (struct thread_master *m, struct thread *fetch) |
| 1031 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1032 | struct thread *thread; |
| 1033 | fd_set readfd; |
| 1034 | fd_set writefd; |
| 1035 | fd_set exceptfd; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1036 | struct timeval timer_val = { .tv_sec = 0, .tv_usec = 0 }; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1037 | struct timeval timer_val_bg; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1038 | struct timeval *timer_wait = &timer_val; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1039 | struct timeval *timer_wait_bg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1040 | |
| 1041 | while (1) |
| 1042 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1043 | int num = 0; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1044 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1045 | /* Signals pre-empt everything */ |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1046 | quagga_sigevent_process (); |
| 1047 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1048 | /* Drain the ready queue of already scheduled jobs, before scheduling |
| 1049 | * more. |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1050 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1051 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1052 | return thread_run (m, thread, fetch); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1053 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1054 | /* To be fair to all kinds of threads, and avoid starvation, we |
| 1055 | * need to be careful to consider all thread types for scheduling |
| 1056 | * in each quanta. I.e. we should not return early from here on. |
| 1057 | */ |
| 1058 | |
| 1059 | /* Normal event are the next highest priority. */ |
| 1060 | thread_process (&m->event); |
| 1061 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | /* Structure copy. */ |
| 1063 | readfd = m->readfd; |
| 1064 | writefd = m->writefd; |
| 1065 | exceptfd = m->exceptfd; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1066 | |
| 1067 | /* Calculate select wait timer if nothing else to do */ |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1068 | if (m->ready.count == 0) |
| 1069 | { |
| 1070 | quagga_get_relative (NULL); |
| 1071 | timer_wait = thread_timer_wait (&m->timer, &timer_val); |
| 1072 | timer_wait_bg = thread_timer_wait (&m->background, &timer_val_bg); |
| 1073 | |
| 1074 | if (timer_wait_bg && |
| 1075 | (!timer_wait || (timeval_cmp (*timer_wait, *timer_wait_bg) > 0))) |
| 1076 | timer_wait = timer_wait_bg; |
| 1077 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1078 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1079 | num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1080 | |
| 1081 | /* Signals should get quick treatment */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | if (num < 0) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1083 | { |
| 1084 | if (errno == EINTR) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1085 | continue; /* signal received - process it */ |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1086 | zlog_warn ("select() error: %s", safe_strerror (errno)); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1089 | |
| 1090 | /* Check foreground timers. Historically, they have had higher |
| 1091 | priority than I/O threads, so let's push them onto the ready |
| 1092 | list in front of the I/O threads. */ |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1093 | quagga_get_relative (NULL); |
| 1094 | thread_timer_process (&m->timer, &relative_time); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1095 | |
| 1096 | /* Got IO, process it */ |
| 1097 | if (num > 0) |
| 1098 | { |
| 1099 | /* Normal priority read thead. */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1100 | thread_process_fd (&m->read, &readfd, &m->readfd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1101 | /* Write thead. */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1102 | thread_process_fd (&m->write, &writefd, &m->writefd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1103 | } |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1104 | |
| 1105 | #if 0 |
| 1106 | /* If any threads were made ready above (I/O or foreground timer), |
| 1107 | perhaps we should avoid adding background timers to the ready |
| 1108 | list at this time. If this is code is uncommented, then background |
| 1109 | timer threads will not run unless there is nothing else to do. */ |
| 1110 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
| 1111 | return thread_run (m, thread, fetch); |
| 1112 | #endif |
| 1113 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1114 | /* Background timer/events, lowest priority */ |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1115 | thread_timer_process (&m->background, &relative_time); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1116 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1117 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1118 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1122 | unsigned long |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1123 | thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start, unsigned long *cputime) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1124 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1125 | #ifdef HAVE_RUSAGE |
| 1126 | /* This is 'user + sys' time. */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1127 | *cputime = timeval_elapsed (now->cpu.ru_utime, start->cpu.ru_utime) + |
| 1128 | timeval_elapsed (now->cpu.ru_stime, start->cpu.ru_stime); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1129 | #else |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1130 | *cputime = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1131 | #endif /* HAVE_RUSAGE */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1132 | return timeval_elapsed (now->real, start->real); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1135 | /* We should aim to yield after THREAD_YIELD_TIME_SLOT milliseconds. |
| 1136 | Note: we are using real (wall clock) time for this calculation. |
| 1137 | It could be argued that CPU time may make more sense in certain |
| 1138 | contexts. The things to consider are whether the thread may have |
| 1139 | blocked (in which case wall time increases, but CPU time does not), |
| 1140 | or whether the system is heavily loaded with other processes competing |
| 1141 | for CPU time. On balance, wall clock time seems to make sense. |
| 1142 | Plus it has the added benefit that gettimeofday should be faster |
| 1143 | than calling getrusage. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1144 | int |
| 1145 | thread_should_yield (struct thread *thread) |
| 1146 | { |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1147 | quagga_get_relative (NULL); |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1148 | return (timeval_elapsed(relative_time, thread->real) > |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1149 | THREAD_YIELD_TIME_SLOT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1152 | void |
| 1153 | thread_getrusage (RUSAGE_T *r) |
| 1154 | { |
| 1155 | quagga_get_relative (NULL); |
| 1156 | #ifdef HAVE_RUSAGE |
| 1157 | getrusage(RUSAGE_SELF, &(r->cpu)); |
| 1158 | #endif |
| 1159 | r->real = relative_time; |
| 1160 | |
| 1161 | #ifdef HAVE_CLOCK_MONOTONIC |
| 1162 | /* quagga_get_relative() only updates recent_time if gettimeofday |
| 1163 | * based, not when using CLOCK_MONOTONIC. As we export recent_time |
| 1164 | * and guarantee to update it before threads are run... |
| 1165 | */ |
| 1166 | quagga_gettimeofday(&recent_time); |
| 1167 | #endif /* HAVE_CLOCK_MONOTONIC */ |
| 1168 | } |
| 1169 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1170 | /* We check thread consumed time. If the system has getrusage, we'll |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1171 | use that to get in-depth stats on the performance of the thread in addition |
| 1172 | to wall clock time stats from gettimeofday. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1173 | void |
| 1174 | thread_call (struct thread *thread) |
| 1175 | { |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1176 | unsigned long realtime, cputime; |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1177 | RUSAGE_T before, after; |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* Cache a pointer to the relevant cpu history thread, if the thread |
| 1180 | * does not have it yet. |
| 1181 | * |
| 1182 | * Callers submitting 'dummy threads' hence must take care that |
| 1183 | * thread->cpu is NULL |
| 1184 | */ |
| 1185 | if (!thread->hist) |
| 1186 | { |
| 1187 | struct cpu_thread_history tmp; |
| 1188 | |
| 1189 | tmp.func = thread->func; |
| 1190 | tmp.funcname = thread->funcname; |
| 1191 | |
| 1192 | thread->hist = hash_get (cpu_record, &tmp, |
| 1193 | (void * (*) (void *))cpu_record_hash_alloc); |
| 1194 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1195 | |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1196 | GETRUSAGE (&before); |
| 1197 | thread->real = before.real; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1198 | |
| 1199 | (*thread->func) (thread); |
| 1200 | |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1201 | GETRUSAGE (&after); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1202 | |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1203 | realtime = thread_consumed_time (&after, &before, &cputime); |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1204 | thread->hist->real.total += realtime; |
| 1205 | if (thread->hist->real.max < realtime) |
| 1206 | thread->hist->real.max = realtime; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1207 | #ifdef HAVE_RUSAGE |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1208 | thread->hist->cpu.total += cputime; |
| 1209 | if (thread->hist->cpu.max < cputime) |
| 1210 | thread->hist->cpu.max = cputime; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1211 | #endif |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1212 | |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1213 | ++(thread->hist->total_calls); |
| 1214 | thread->hist->types |= (1 << thread->add_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1215 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1216 | #ifdef CONSUMED_TIME_CHECK |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1217 | if (realtime > CONSUMED_TIME_CHECK) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1218 | { |
| 1219 | /* |
| 1220 | * We have a CPU Hog on our hands. |
| 1221 | * Whinge about it now, so we're aware this is yet another task |
| 1222 | * to fix. |
| 1223 | */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1224 | zlog_warn ("SLOW THREAD: task %s (%lx) ran for %lums (cpu time %lums)", |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1225 | thread->funcname, |
| 1226 | (unsigned long) thread->func, |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1227 | realtime/1000, cputime/1000); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1228 | } |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1229 | #endif /* CONSUMED_TIME_CHECK */ |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1230 | |
| 1231 | XFREE (MTYPE_THREAD_FUNCNAME, thread->funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /* Execute thread */ |
| 1235 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1236 | funcname_thread_execute (struct thread_master *m, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1237 | int (*func)(struct thread *), |
| 1238 | void *arg, |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1239 | int val, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1240 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1241 | { |
| 1242 | struct thread dummy; |
| 1243 | |
| 1244 | memset (&dummy, 0, sizeof (struct thread)); |
| 1245 | |
| 1246 | dummy.type = THREAD_EVENT; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1247 | dummy.add_type = THREAD_EXECUTE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1248 | dummy.master = NULL; |
| 1249 | dummy.func = func; |
| 1250 | dummy.arg = arg; |
| 1251 | dummy.u.val = val; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1252 | dummy.funcname = strip_funcname (funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1253 | thread_call (&dummy); |
| 1254 | |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 1255 | XFREE (MTYPE_THREAD_FUNCNAME, dummy.funcname); |
paul | 2946f65 | 2003-03-27 23:48:24 +0000 | [diff] [blame] | 1256 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1257 | return NULL; |
| 1258 | } |