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" |
Vincent Bernat | d6be5fb | 2012-05-24 09:44:43 +0200 | [diff] [blame] | 32 | |
| 33 | #if defined HAVE_SNMP && defined SNMP_AGENTX |
| 34 | #include <net-snmp/net-snmp-config.h> |
| 35 | #include <net-snmp/net-snmp-includes.h> |
| 36 | #include <net-snmp/agent/net-snmp-agent-includes.h> |
| 37 | #include <net-snmp/agent/snmp_vars.h> |
| 38 | |
| 39 | extern int agentx_enabled; |
| 40 | #endif |
| 41 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 42 | |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 43 | /* Recent absolute time of day */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 44 | struct timeval recent_time; |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 45 | static struct timeval last_recent_time; |
| 46 | /* Relative time, since startup */ |
| 47 | static struct timeval relative_time; |
| 48 | static struct timeval relative_time_base; |
| 49 | /* init flag */ |
| 50 | static unsigned short timers_inited; |
| 51 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 52 | static struct hash *cpu_record = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 53 | |
| 54 | /* Struct timeval's tv_usec one second value. */ |
| 55 | #define TIMER_SECOND_MICRO 1000000L |
| 56 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 57 | /* Adjust so that tv_usec is in the range [0,TIMER_SECOND_MICRO). |
| 58 | And change negative values to 0. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 59 | static struct timeval |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | timeval_adjust (struct timeval a) |
| 61 | { |
| 62 | while (a.tv_usec >= TIMER_SECOND_MICRO) |
| 63 | { |
| 64 | a.tv_usec -= TIMER_SECOND_MICRO; |
| 65 | a.tv_sec++; |
| 66 | } |
| 67 | |
| 68 | while (a.tv_usec < 0) |
| 69 | { |
| 70 | a.tv_usec += TIMER_SECOND_MICRO; |
| 71 | a.tv_sec--; |
| 72 | } |
| 73 | |
| 74 | if (a.tv_sec < 0) |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 75 | /* Change negative timeouts to 0. */ |
| 76 | a.tv_sec = a.tv_usec = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | |
| 78 | return a; |
| 79 | } |
| 80 | |
| 81 | static struct timeval |
| 82 | timeval_subtract (struct timeval a, struct timeval b) |
| 83 | { |
| 84 | struct timeval ret; |
| 85 | |
| 86 | ret.tv_usec = a.tv_usec - b.tv_usec; |
| 87 | ret.tv_sec = a.tv_sec - b.tv_sec; |
| 88 | |
| 89 | return timeval_adjust (ret); |
| 90 | } |
| 91 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 92 | static long |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | timeval_cmp (struct timeval a, struct timeval b) |
| 94 | { |
| 95 | return (a.tv_sec == b.tv_sec |
| 96 | ? a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec); |
| 97 | } |
| 98 | |
| 99 | static unsigned long |
| 100 | timeval_elapsed (struct timeval a, struct timeval b) |
| 101 | { |
| 102 | return (((a.tv_sec - b.tv_sec) * TIMER_SECOND_MICRO) |
| 103 | + (a.tv_usec - b.tv_usec)); |
| 104 | } |
| 105 | |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 106 | #ifndef HAVE_CLOCK_MONOTONIC |
| 107 | static void |
| 108 | quagga_gettimeofday_relative_adjust (void) |
| 109 | { |
| 110 | struct timeval diff; |
| 111 | if (timeval_cmp (recent_time, last_recent_time) < 0) |
| 112 | { |
| 113 | relative_time.tv_sec++; |
| 114 | relative_time.tv_usec = 0; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | diff = timeval_subtract (recent_time, last_recent_time); |
| 119 | relative_time.tv_sec += diff.tv_sec; |
| 120 | relative_time.tv_usec += diff.tv_usec; |
| 121 | relative_time = timeval_adjust (relative_time); |
| 122 | } |
| 123 | last_recent_time = recent_time; |
| 124 | } |
| 125 | #endif /* !HAVE_CLOCK_MONOTONIC */ |
| 126 | |
| 127 | /* gettimeofday wrapper, to keep recent_time updated */ |
| 128 | static int |
| 129 | quagga_gettimeofday (struct timeval *tv) |
| 130 | { |
| 131 | int ret; |
| 132 | |
| 133 | assert (tv); |
| 134 | |
| 135 | if (!(ret = gettimeofday (&recent_time, NULL))) |
| 136 | { |
| 137 | /* init... */ |
| 138 | if (!timers_inited) |
| 139 | { |
| 140 | relative_time_base = last_recent_time = recent_time; |
| 141 | timers_inited = 1; |
| 142 | } |
| 143 | /* avoid copy if user passed recent_time pointer.. */ |
| 144 | if (tv != &recent_time) |
| 145 | *tv = recent_time; |
| 146 | return 0; |
| 147 | } |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | quagga_get_relative (struct timeval *tv) |
| 153 | { |
| 154 | int ret; |
| 155 | |
| 156 | #ifdef HAVE_CLOCK_MONOTONIC |
| 157 | { |
| 158 | struct timespec tp; |
| 159 | if (!(ret = clock_gettime (CLOCK_MONOTONIC, &tp))) |
| 160 | { |
| 161 | relative_time.tv_sec = tp.tv_sec; |
| 162 | relative_time.tv_usec = tp.tv_nsec / 1000; |
| 163 | } |
| 164 | } |
| 165 | #else /* !HAVE_CLOCK_MONOTONIC */ |
| 166 | if (!(ret = quagga_gettimeofday (&recent_time))) |
| 167 | quagga_gettimeofday_relative_adjust(); |
| 168 | #endif /* HAVE_CLOCK_MONOTONIC */ |
| 169 | |
| 170 | if (tv) |
| 171 | *tv = relative_time; |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | /* Get absolute time stamp, but in terms of the internal timer |
| 177 | * Could be wrong, but at least won't go back. |
| 178 | */ |
| 179 | static void |
| 180 | quagga_real_stabilised (struct timeval *tv) |
| 181 | { |
| 182 | *tv = relative_time_base; |
| 183 | tv->tv_sec += relative_time.tv_sec; |
| 184 | tv->tv_usec += relative_time.tv_usec; |
| 185 | *tv = timeval_adjust (*tv); |
| 186 | } |
| 187 | |
| 188 | /* Exported Quagga timestamp function. |
| 189 | * Modelled on POSIX clock_gettime. |
| 190 | */ |
| 191 | int |
| 192 | quagga_gettime (enum quagga_clkid clkid, struct timeval *tv) |
| 193 | { |
| 194 | switch (clkid) |
| 195 | { |
| 196 | case QUAGGA_CLK_REALTIME: |
| 197 | return quagga_gettimeofday (tv); |
| 198 | case QUAGGA_CLK_MONOTONIC: |
| 199 | return quagga_get_relative (tv); |
| 200 | case QUAGGA_CLK_REALTIME_STABILISED: |
| 201 | quagga_real_stabilised (tv); |
| 202 | return 0; |
| 203 | default: |
| 204 | errno = EINVAL; |
| 205 | return -1; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* time_t value in terms of stabilised absolute time. |
| 210 | * replacement for POSIX time() |
| 211 | */ |
| 212 | time_t |
| 213 | quagga_time (time_t *t) |
| 214 | { |
| 215 | struct timeval tv; |
| 216 | quagga_real_stabilised (&tv); |
| 217 | if (t) |
| 218 | *t = tv.tv_sec; |
| 219 | return tv.tv_sec; |
| 220 | } |
| 221 | |
| 222 | /* Public export of recent_relative_time by value */ |
| 223 | struct timeval |
| 224 | recent_relative_time (void) |
| 225 | { |
| 226 | return relative_time; |
| 227 | } |
| 228 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 229 | static unsigned int |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 230 | cpu_record_hash_key (struct cpu_thread_history *a) |
| 231 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 232 | return (uintptr_t) a->func; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 236 | cpu_record_hash_cmp (const struct cpu_thread_history *a, |
| 237 | const struct cpu_thread_history *b) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 238 | { |
| 239 | return a->func == b->func; |
| 240 | } |
| 241 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 242 | static void * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 243 | cpu_record_hash_alloc (struct cpu_thread_history *a) |
| 244 | { |
| 245 | struct cpu_thread_history *new; |
paul | 039b957 | 2004-10-31 16:43:17 +0000 | [diff] [blame] | 246 | new = XCALLOC (MTYPE_THREAD_STATS, sizeof (struct cpu_thread_history)); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 247 | new->func = a->func; |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 248 | strcpy(new->funcname, a->funcname); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 249 | return new; |
| 250 | } |
| 251 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 252 | static void |
| 253 | cpu_record_hash_free (void *a) |
| 254 | { |
| 255 | struct cpu_thread_history *hist = a; |
| 256 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 257 | XFREE (MTYPE_THREAD_STATS, hist); |
| 258 | } |
| 259 | |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 260 | static void |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 261 | vty_out_cpu_thread_history(struct vty* vty, |
| 262 | struct cpu_thread_history *a) |
| 263 | { |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 264 | #ifdef HAVE_RUSAGE |
| 265 | vty_out(vty, "%7ld.%03ld %9d %8ld %9ld %8ld %9ld", |
| 266 | a->cpu.total/1000, a->cpu.total%1000, a->total_calls, |
| 267 | a->cpu.total/a->total_calls, a->cpu.max, |
| 268 | a->real.total/a->total_calls, a->real.max); |
| 269 | #else |
| 270 | vty_out(vty, "%7ld.%03ld %9d %8ld %9ld", |
| 271 | a->real.total/1000, a->real.total%1000, a->total_calls, |
| 272 | a->real.total/a->total_calls, a->real.max); |
| 273 | #endif |
| 274 | vty_out(vty, " %c%c%c%c%c%c %s%s", |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 275 | a->types & (1 << THREAD_READ) ? 'R':' ', |
| 276 | a->types & (1 << THREAD_WRITE) ? 'W':' ', |
| 277 | a->types & (1 << THREAD_TIMER) ? 'T':' ', |
| 278 | a->types & (1 << THREAD_EVENT) ? 'E':' ', |
| 279 | a->types & (1 << THREAD_EXECUTE) ? 'X':' ', |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 280 | a->types & (1 << THREAD_BACKGROUND) ? 'B' : ' ', |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 281 | a->funcname, VTY_NEWLINE); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | cpu_record_hash_print(struct hash_backet *bucket, |
| 286 | void *args[]) |
| 287 | { |
| 288 | struct cpu_thread_history *totals = args[0]; |
| 289 | struct vty *vty = args[1]; |
Paul Jakma | 41b2373 | 2009-06-30 16:12:49 +0100 | [diff] [blame] | 290 | thread_type *filter = args[2]; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 291 | struct cpu_thread_history *a = bucket->data; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 292 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 293 | a = bucket->data; |
| 294 | if ( !(a->types & *filter) ) |
| 295 | return; |
| 296 | vty_out_cpu_thread_history(vty,a); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 297 | totals->total_calls += a->total_calls; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 298 | totals->real.total += a->real.total; |
| 299 | if (totals->real.max < a->real.max) |
| 300 | totals->real.max = a->real.max; |
| 301 | #ifdef HAVE_RUSAGE |
| 302 | totals->cpu.total += a->cpu.total; |
| 303 | if (totals->cpu.max < a->cpu.max) |
| 304 | totals->cpu.max = a->cpu.max; |
| 305 | #endif |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void |
Paul Jakma | 41b2373 | 2009-06-30 16:12:49 +0100 | [diff] [blame] | 309 | cpu_record_print(struct vty *vty, thread_type filter) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 310 | { |
| 311 | struct cpu_thread_history tmp; |
| 312 | void *args[3] = {&tmp, vty, &filter}; |
| 313 | |
| 314 | memset(&tmp, 0, sizeof tmp); |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 315 | strcpy(tmp.funcname, "TOTAL"); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 316 | tmp.types = filter; |
| 317 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 318 | #ifdef HAVE_RUSAGE |
| 319 | vty_out(vty, "%21s %18s %18s%s", |
| 320 | "", "CPU (user+system):", "Real (wall-clock):", VTY_NEWLINE); |
| 321 | #endif |
| 322 | vty_out(vty, "Runtime(ms) Invoked Avg uSec Max uSecs"); |
| 323 | #ifdef HAVE_RUSAGE |
| 324 | vty_out(vty, " Avg uSec Max uSecs"); |
| 325 | #endif |
| 326 | vty_out(vty, " Type Thread%s", VTY_NEWLINE); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 327 | hash_iterate(cpu_record, |
| 328 | (void(*)(struct hash_backet*,void*))cpu_record_hash_print, |
| 329 | args); |
| 330 | |
| 331 | if (tmp.total_calls > 0) |
| 332 | vty_out_cpu_thread_history(vty, &tmp); |
| 333 | } |
| 334 | |
| 335 | DEFUN(show_thread_cpu, |
| 336 | show_thread_cpu_cmd, |
| 337 | "show thread cpu [FILTER]", |
| 338 | SHOW_STR |
| 339 | "Thread information\n" |
| 340 | "Thread CPU usage\n" |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 341 | "Display filter (rwtexb)\n") |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 342 | { |
| 343 | int i = 0; |
Paul Jakma | 41b2373 | 2009-06-30 16:12:49 +0100 | [diff] [blame] | 344 | thread_type filter = (thread_type) -1U; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 345 | |
| 346 | if (argc > 0) |
| 347 | { |
| 348 | filter = 0; |
| 349 | while (argv[0][i] != '\0') |
| 350 | { |
| 351 | switch ( argv[0][i] ) |
| 352 | { |
| 353 | case 'r': |
| 354 | case 'R': |
| 355 | filter |= (1 << THREAD_READ); |
| 356 | break; |
| 357 | case 'w': |
| 358 | case 'W': |
| 359 | filter |= (1 << THREAD_WRITE); |
| 360 | break; |
| 361 | case 't': |
| 362 | case 'T': |
| 363 | filter |= (1 << THREAD_TIMER); |
| 364 | break; |
| 365 | case 'e': |
| 366 | case 'E': |
| 367 | filter |= (1 << THREAD_EVENT); |
| 368 | break; |
| 369 | case 'x': |
| 370 | case 'X': |
| 371 | filter |= (1 << THREAD_EXECUTE); |
| 372 | break; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 373 | case 'b': |
| 374 | case 'B': |
| 375 | filter |= (1 << THREAD_BACKGROUND); |
| 376 | break; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | ++i; |
| 381 | } |
| 382 | if (filter == 0) |
| 383 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 384 | vty_out(vty, "Invalid filter \"%s\" specified," |
| 385 | " must contain at least one of 'RWTEXB'%s", |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 386 | argv[0], VTY_NEWLINE); |
| 387 | return CMD_WARNING; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | cpu_record_print(vty, filter); |
| 392 | return CMD_SUCCESS; |
| 393 | } |
Paul Jakma | e276eb8 | 2010-01-09 16:15:00 +0000 | [diff] [blame] | 394 | |
| 395 | static void |
| 396 | cpu_record_hash_clear (struct hash_backet *bucket, |
| 397 | void *args) |
| 398 | { |
| 399 | thread_type *filter = args; |
| 400 | struct cpu_thread_history *a = bucket->data; |
| 401 | |
| 402 | a = bucket->data; |
| 403 | if ( !(a->types & *filter) ) |
| 404 | return; |
| 405 | |
| 406 | hash_release (cpu_record, bucket->data); |
| 407 | } |
| 408 | |
| 409 | static void |
| 410 | cpu_record_clear (thread_type filter) |
| 411 | { |
| 412 | thread_type *tmp = &filter; |
| 413 | hash_iterate (cpu_record, |
| 414 | (void (*) (struct hash_backet*,void*)) cpu_record_hash_clear, |
| 415 | tmp); |
| 416 | } |
| 417 | |
| 418 | DEFUN(clear_thread_cpu, |
| 419 | clear_thread_cpu_cmd, |
| 420 | "clear thread cpu [FILTER]", |
| 421 | "Clear stored data\n" |
| 422 | "Thread information\n" |
| 423 | "Thread CPU usage\n" |
| 424 | "Display filter (rwtexb)\n") |
| 425 | { |
| 426 | int i = 0; |
| 427 | thread_type filter = (thread_type) -1U; |
| 428 | |
| 429 | if (argc > 0) |
| 430 | { |
| 431 | filter = 0; |
| 432 | while (argv[0][i] != '\0') |
| 433 | { |
| 434 | switch ( argv[0][i] ) |
| 435 | { |
| 436 | case 'r': |
| 437 | case 'R': |
| 438 | filter |= (1 << THREAD_READ); |
| 439 | break; |
| 440 | case 'w': |
| 441 | case 'W': |
| 442 | filter |= (1 << THREAD_WRITE); |
| 443 | break; |
| 444 | case 't': |
| 445 | case 'T': |
| 446 | filter |= (1 << THREAD_TIMER); |
| 447 | break; |
| 448 | case 'e': |
| 449 | case 'E': |
| 450 | filter |= (1 << THREAD_EVENT); |
| 451 | break; |
| 452 | case 'x': |
| 453 | case 'X': |
| 454 | filter |= (1 << THREAD_EXECUTE); |
| 455 | break; |
| 456 | case 'b': |
| 457 | case 'B': |
| 458 | filter |= (1 << THREAD_BACKGROUND); |
| 459 | break; |
| 460 | default: |
| 461 | break; |
| 462 | } |
| 463 | ++i; |
| 464 | } |
| 465 | if (filter == 0) |
| 466 | { |
| 467 | vty_out(vty, "Invalid filter \"%s\" specified," |
| 468 | " must contain at least one of 'RWTEXB'%s", |
| 469 | argv[0], VTY_NEWLINE); |
| 470 | return CMD_WARNING; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | cpu_record_clear (filter); |
| 475 | return CMD_SUCCESS; |
| 476 | } |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 477 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | /* List allocation and head/tail print out. */ |
| 479 | static void |
| 480 | thread_list_debug (struct thread_list *list) |
| 481 | { |
| 482 | printf ("count [%d] head [%p] tail [%p]\n", |
| 483 | list->count, list->head, list->tail); |
| 484 | } |
| 485 | |
| 486 | /* Debug print for thread_master. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 487 | static void __attribute__ ((unused)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 488 | thread_master_debug (struct thread_master *m) |
| 489 | { |
| 490 | printf ("-----------\n"); |
| 491 | printf ("readlist : "); |
| 492 | thread_list_debug (&m->read); |
| 493 | printf ("writelist : "); |
| 494 | thread_list_debug (&m->write); |
| 495 | printf ("timerlist : "); |
| 496 | thread_list_debug (&m->timer); |
| 497 | printf ("eventlist : "); |
| 498 | thread_list_debug (&m->event); |
| 499 | printf ("unuselist : "); |
| 500 | thread_list_debug (&m->unuse); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 501 | printf ("bgndlist : "); |
| 502 | thread_list_debug (&m->background); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 503 | printf ("total alloc: [%ld]\n", m->alloc); |
| 504 | printf ("-----------\n"); |
| 505 | } |
| 506 | |
| 507 | /* Allocate new thread master. */ |
| 508 | struct thread_master * |
| 509 | thread_master_create () |
| 510 | { |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 511 | if (cpu_record == NULL) |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 512 | cpu_record |
| 513 | = hash_create_size (1011, (unsigned int (*) (void *))cpu_record_hash_key, |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 514 | (int (*) (const void *, const void *))cpu_record_hash_cmp); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 515 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 516 | return (struct thread_master *) XCALLOC (MTYPE_THREAD_MASTER, |
| 517 | sizeof (struct thread_master)); |
| 518 | } |
| 519 | |
| 520 | /* Add a new thread to the list. */ |
| 521 | static void |
| 522 | thread_list_add (struct thread_list *list, struct thread *thread) |
| 523 | { |
| 524 | thread->next = NULL; |
| 525 | thread->prev = list->tail; |
| 526 | if (list->tail) |
| 527 | list->tail->next = thread; |
| 528 | else |
| 529 | list->head = thread; |
| 530 | list->tail = thread; |
| 531 | list->count++; |
| 532 | } |
| 533 | |
| 534 | /* Add a new thread just before the point. */ |
| 535 | static void |
| 536 | thread_list_add_before (struct thread_list *list, |
| 537 | struct thread *point, |
| 538 | struct thread *thread) |
| 539 | { |
| 540 | thread->next = point; |
| 541 | thread->prev = point->prev; |
| 542 | if (point->prev) |
| 543 | point->prev->next = thread; |
| 544 | else |
| 545 | list->head = thread; |
| 546 | point->prev = thread; |
| 547 | list->count++; |
| 548 | } |
| 549 | |
| 550 | /* Delete a thread from the list. */ |
| 551 | static struct thread * |
| 552 | thread_list_delete (struct thread_list *list, struct thread *thread) |
| 553 | { |
| 554 | if (thread->next) |
| 555 | thread->next->prev = thread->prev; |
| 556 | else |
| 557 | list->tail = thread->prev; |
| 558 | if (thread->prev) |
| 559 | thread->prev->next = thread->next; |
| 560 | else |
| 561 | list->head = thread->next; |
| 562 | thread->next = thread->prev = NULL; |
| 563 | list->count--; |
| 564 | return thread; |
| 565 | } |
| 566 | |
| 567 | /* Move thread to unuse list. */ |
| 568 | static void |
| 569 | thread_add_unuse (struct thread_master *m, struct thread *thread) |
| 570 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 571 | assert (m != NULL && thread != NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 572 | assert (thread->next == NULL); |
| 573 | assert (thread->prev == NULL); |
| 574 | assert (thread->type == THREAD_UNUSED); |
| 575 | thread_list_add (&m->unuse, thread); |
paul | 9d11a19 | 2004-10-31 16:19:24 +0000 | [diff] [blame] | 576 | /* XXX: Should we deallocate funcname here? */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* Free all unused thread. */ |
| 580 | static void |
| 581 | thread_list_free (struct thread_master *m, struct thread_list *list) |
| 582 | { |
| 583 | struct thread *t; |
| 584 | struct thread *next; |
| 585 | |
| 586 | for (t = list->head; t; t = next) |
| 587 | { |
| 588 | next = t->next; |
| 589 | XFREE (MTYPE_THREAD, t); |
| 590 | list->count--; |
| 591 | m->alloc--; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* Stop thread scheduler. */ |
| 596 | void |
| 597 | thread_master_free (struct thread_master *m) |
| 598 | { |
| 599 | thread_list_free (m, &m->read); |
| 600 | thread_list_free (m, &m->write); |
| 601 | thread_list_free (m, &m->timer); |
| 602 | thread_list_free (m, &m->event); |
| 603 | thread_list_free (m, &m->ready); |
| 604 | thread_list_free (m, &m->unuse); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 605 | thread_list_free (m, &m->background); |
| 606 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 607 | XFREE (MTYPE_THREAD_MASTER, m); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 608 | |
| 609 | if (cpu_record) |
| 610 | { |
| 611 | hash_clean (cpu_record, cpu_record_hash_free); |
| 612 | hash_free (cpu_record); |
| 613 | cpu_record = NULL; |
| 614 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | } |
| 616 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 617 | /* Thread list is empty or not. */ |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 618 | static int |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 619 | thread_empty (struct thread_list *list) |
| 620 | { |
| 621 | return list->head ? 0 : 1; |
| 622 | } |
| 623 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | /* Delete top of the list and return it. */ |
| 625 | static struct thread * |
| 626 | thread_trim_head (struct thread_list *list) |
| 627 | { |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 628 | if (!thread_empty (list)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 629 | return thread_list_delete (list, list->head); |
| 630 | return NULL; |
| 631 | } |
| 632 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 633 | /* Return remain time in second. */ |
| 634 | unsigned long |
| 635 | thread_timer_remain_second (struct thread *thread) |
| 636 | { |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 637 | quagga_get_relative (NULL); |
| 638 | |
| 639 | if (thread->u.sands.tv_sec - relative_time.tv_sec > 0) |
| 640 | return thread->u.sands.tv_sec - relative_time.tv_sec; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 641 | else |
| 642 | return 0; |
| 643 | } |
| 644 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 645 | /* Trim blankspace and "()"s */ |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 646 | void |
| 647 | strip_funcname (char *dest, const char *funcname) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 648 | { |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 649 | char buff[FUNCNAME_LEN]; |
| 650 | char tmp, *e, *b = buff; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 651 | |
| 652 | strncpy(buff, funcname, sizeof(buff)); |
| 653 | buff[ sizeof(buff) -1] = '\0'; |
| 654 | e = buff +strlen(buff) -1; |
| 655 | |
| 656 | /* Wont work for funcname == "Word (explanation)" */ |
| 657 | |
| 658 | while (*b == ' ' || *b == '(') |
| 659 | ++b; |
| 660 | while (*e == ' ' || *e == ')') |
| 661 | --e; |
| 662 | e++; |
| 663 | |
| 664 | tmp = *e; |
| 665 | *e = '\0'; |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 666 | strcpy (dest, b); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 667 | *e = tmp; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 668 | } |
| 669 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 670 | /* Get new thread. */ |
| 671 | static struct thread * |
| 672 | thread_get (struct thread_master *m, u_char type, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 673 | int (*func) (struct thread *), void *arg, const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 674 | { |
Jorge Boncompte [DTI2] | 6401832 | 2012-05-07 16:53:13 +0000 | [diff] [blame] | 675 | struct thread *thread = thread_trim_head (&m->unuse); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 676 | |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 677 | if (! thread) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 688 | strip_funcname (thread->funcname, funcname); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 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; |
| 956 | thread_add_unuse (m, thread); |
| 957 | return fetch; |
| 958 | } |
| 959 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 960 | static int |
| 961 | thread_process_fd (struct thread_list *list, fd_set *fdset, fd_set *mfdset) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 962 | { |
| 963 | struct thread *thread; |
| 964 | struct thread *next; |
| 965 | int ready = 0; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 966 | |
| 967 | assert (list); |
| 968 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 969 | for (thread = list->head; thread; thread = next) |
| 970 | { |
| 971 | next = thread->next; |
| 972 | |
| 973 | if (FD_ISSET (THREAD_FD (thread), fdset)) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 974 | { |
| 975 | assert (FD_ISSET (THREAD_FD (thread), mfdset)); |
| 976 | FD_CLR(THREAD_FD (thread), mfdset); |
| 977 | thread_list_delete (list, thread); |
| 978 | thread_list_add (&thread->master->ready, thread); |
| 979 | thread->type = THREAD_READY; |
| 980 | ready++; |
| 981 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 982 | } |
| 983 | return ready; |
| 984 | } |
| 985 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 986 | /* Add all timers that have popped to the ready list. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 987 | static unsigned int |
| 988 | thread_timer_process (struct thread_list *list, struct timeval *timenow) |
| 989 | { |
| 990 | struct thread *thread; |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 991 | struct thread *next; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 992 | unsigned int ready = 0; |
| 993 | |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 994 | for (thread = list->head; thread; thread = next) |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 995 | { |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 996 | next = thread->next; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 997 | if (timeval_cmp (*timenow, thread->u.sands) < 0) |
| 998 | return ready; |
| 999 | thread_list_delete (list, thread); |
| 1000 | thread->type = THREAD_READY; |
| 1001 | thread_list_add (&thread->master->ready, thread); |
| 1002 | ready++; |
| 1003 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1004 | return ready; |
| 1005 | } |
| 1006 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1007 | /* process a list en masse, e.g. for event thread lists */ |
| 1008 | static unsigned int |
| 1009 | thread_process (struct thread_list *list) |
| 1010 | { |
| 1011 | struct thread *thread; |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 1012 | struct thread *next; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1013 | unsigned int ready = 0; |
| 1014 | |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 1015 | for (thread = list->head; thread; thread = next) |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1016 | { |
Paul Jakma | b5043aa | 2012-02-28 18:32:56 +0000 | [diff] [blame] | 1017 | next = thread->next; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1018 | thread_list_delete (list, thread); |
| 1019 | thread->type = THREAD_READY; |
| 1020 | thread_list_add (&thread->master->ready, thread); |
| 1021 | ready++; |
| 1022 | } |
| 1023 | return ready; |
| 1024 | } |
| 1025 | |
| 1026 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1027 | /* Fetch next ready thread. */ |
| 1028 | struct thread * |
| 1029 | thread_fetch (struct thread_master *m, struct thread *fetch) |
| 1030 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | struct thread *thread; |
| 1032 | fd_set readfd; |
| 1033 | fd_set writefd; |
| 1034 | fd_set exceptfd; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1035 | struct timeval timer_val = { .tv_sec = 0, .tv_usec = 0 }; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1036 | struct timeval timer_val_bg; |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1037 | struct timeval *timer_wait = &timer_val; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1038 | struct timeval *timer_wait_bg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1039 | |
| 1040 | while (1) |
| 1041 | { |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1042 | int num = 0; |
Vincent Bernat | d6be5fb | 2012-05-24 09:44:43 +0200 | [diff] [blame] | 1043 | #if defined HAVE_SNMP && defined SNMP_AGENTX |
| 1044 | struct timeval snmp_timer_wait; |
| 1045 | int snmpblock = 0; |
| 1046 | int fdsetsize; |
| 1047 | #endif |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1048 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1049 | /* Signals pre-empt everything */ |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1050 | quagga_sigevent_process (); |
| 1051 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1052 | /* Drain the ready queue of already scheduled jobs, before scheduling |
| 1053 | * more. |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1054 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1055 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1056 | return thread_run (m, thread, fetch); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1057 | |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1058 | /* To be fair to all kinds of threads, and avoid starvation, we |
| 1059 | * need to be careful to consider all thread types for scheduling |
| 1060 | * in each quanta. I.e. we should not return early from here on. |
| 1061 | */ |
| 1062 | |
| 1063 | /* Normal event are the next highest priority. */ |
| 1064 | thread_process (&m->event); |
| 1065 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1066 | /* Structure copy. */ |
| 1067 | readfd = m->readfd; |
| 1068 | writefd = m->writefd; |
| 1069 | exceptfd = m->exceptfd; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1070 | |
| 1071 | /* Calculate select wait timer if nothing else to do */ |
Paul Jakma | 2613abe | 2010-01-11 16:33:07 +0000 | [diff] [blame] | 1072 | if (m->ready.count == 0) |
| 1073 | { |
| 1074 | quagga_get_relative (NULL); |
| 1075 | timer_wait = thread_timer_wait (&m->timer, &timer_val); |
| 1076 | timer_wait_bg = thread_timer_wait (&m->background, &timer_val_bg); |
| 1077 | |
| 1078 | if (timer_wait_bg && |
| 1079 | (!timer_wait || (timeval_cmp (*timer_wait, *timer_wait_bg) > 0))) |
| 1080 | timer_wait = timer_wait_bg; |
| 1081 | } |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1082 | |
Vincent Bernat | d6be5fb | 2012-05-24 09:44:43 +0200 | [diff] [blame] | 1083 | #if defined HAVE_SNMP && defined SNMP_AGENTX |
| 1084 | /* When SNMP is enabled, we may have to select() on additional |
| 1085 | FD. snmp_select_info() will add them to `readfd'. The trick |
| 1086 | with this function is its last argument. We need to set it to |
| 1087 | 0 if timer_wait is not NULL and we need to use the provided |
| 1088 | new timer only if it is still set to 0. */ |
| 1089 | if (agentx_enabled) |
| 1090 | { |
| 1091 | fdsetsize = FD_SETSIZE; |
| 1092 | snmpblock = 1; |
| 1093 | if (timer_wait) |
| 1094 | { |
| 1095 | snmpblock = 0; |
| 1096 | memcpy(&snmp_timer_wait, timer_wait, sizeof(struct timeval)); |
| 1097 | } |
| 1098 | snmp_select_info(&fdsetsize, &readfd, &snmp_timer_wait, &snmpblock); |
| 1099 | if (snmpblock == 0) |
| 1100 | timer_wait = &snmp_timer_wait; |
| 1101 | } |
| 1102 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1103 | num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1104 | |
| 1105 | /* Signals should get quick treatment */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1106 | if (num < 0) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1107 | { |
| 1108 | if (errno == EINTR) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1109 | continue; /* signal received - process it */ |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1110 | zlog_warn ("select() error: %s", safe_strerror (errno)); |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1111 | return NULL; |
| 1112 | } |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1113 | |
Vincent Bernat | d6be5fb | 2012-05-24 09:44:43 +0200 | [diff] [blame] | 1114 | #if defined HAVE_SNMP && defined SNMP_AGENTX |
| 1115 | if (agentx_enabled) |
| 1116 | { |
| 1117 | if (num > 0) |
| 1118 | snmp_read(&readfd); |
| 1119 | else if (num == 0) |
| 1120 | { |
| 1121 | snmp_timeout(); |
| 1122 | run_alarms(); |
| 1123 | } |
| 1124 | netsnmp_check_outstanding_agent_requests(); |
| 1125 | } |
| 1126 | #endif |
| 1127 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1128 | /* Check foreground timers. Historically, they have had higher |
| 1129 | priority than I/O threads, so let's push them onto the ready |
| 1130 | list in front of the I/O threads. */ |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1131 | quagga_get_relative (NULL); |
| 1132 | thread_timer_process (&m->timer, &relative_time); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1133 | |
| 1134 | /* Got IO, process it */ |
| 1135 | if (num > 0) |
| 1136 | { |
| 1137 | /* Normal priority read thead. */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1138 | thread_process_fd (&m->read, &readfd, &m->readfd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1139 | /* Write thead. */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1140 | thread_process_fd (&m->write, &writefd, &m->writefd); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1141 | } |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1142 | |
| 1143 | #if 0 |
| 1144 | /* If any threads were made ready above (I/O or foreground timer), |
| 1145 | perhaps we should avoid adding background timers to the ready |
| 1146 | list at this time. If this is code is uncommented, then background |
| 1147 | timer threads will not run unless there is nothing else to do. */ |
| 1148 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
| 1149 | return thread_run (m, thread, fetch); |
| 1150 | #endif |
| 1151 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1152 | /* Background timer/events, lowest priority */ |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1153 | thread_timer_process (&m->background, &relative_time); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 1154 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1155 | if ((thread = thread_trim_head (&m->ready)) != NULL) |
paul | 05c447d | 2004-07-22 19:14:27 +0000 | [diff] [blame] | 1156 | return thread_run (m, thread, fetch); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1160 | unsigned long |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1161 | thread_consumed_time (RUSAGE_T *now, RUSAGE_T *start, unsigned long *cputime) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | #ifdef HAVE_RUSAGE |
| 1164 | /* This is 'user + sys' time. */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1165 | *cputime = timeval_elapsed (now->cpu.ru_utime, start->cpu.ru_utime) + |
| 1166 | timeval_elapsed (now->cpu.ru_stime, start->cpu.ru_stime); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | #else |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1168 | *cputime = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1169 | #endif /* HAVE_RUSAGE */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1170 | return timeval_elapsed (now->real, start->real); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1173 | /* We should aim to yield after THREAD_YIELD_TIME_SLOT milliseconds. |
| 1174 | Note: we are using real (wall clock) time for this calculation. |
| 1175 | It could be argued that CPU time may make more sense in certain |
| 1176 | contexts. The things to consider are whether the thread may have |
| 1177 | blocked (in which case wall time increases, but CPU time does not), |
| 1178 | or whether the system is heavily loaded with other processes competing |
| 1179 | for CPU time. On balance, wall clock time seems to make sense. |
| 1180 | Plus it has the added benefit that gettimeofday should be faster |
| 1181 | than calling getrusage. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1182 | int |
| 1183 | thread_should_yield (struct thread *thread) |
| 1184 | { |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1185 | quagga_get_relative (NULL); |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1186 | return (timeval_elapsed(relative_time, thread->real) > |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1187 | THREAD_YIELD_TIME_SLOT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Paul Jakma | db9c0df | 2006-08-27 06:44:02 +0000 | [diff] [blame] | 1190 | void |
| 1191 | thread_getrusage (RUSAGE_T *r) |
| 1192 | { |
| 1193 | quagga_get_relative (NULL); |
| 1194 | #ifdef HAVE_RUSAGE |
| 1195 | getrusage(RUSAGE_SELF, &(r->cpu)); |
| 1196 | #endif |
| 1197 | r->real = relative_time; |
| 1198 | |
| 1199 | #ifdef HAVE_CLOCK_MONOTONIC |
| 1200 | /* quagga_get_relative() only updates recent_time if gettimeofday |
| 1201 | * based, not when using CLOCK_MONOTONIC. As we export recent_time |
| 1202 | * and guarantee to update it before threads are run... |
| 1203 | */ |
| 1204 | quagga_gettimeofday(&recent_time); |
| 1205 | #endif /* HAVE_CLOCK_MONOTONIC */ |
| 1206 | } |
| 1207 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1208 | /* We check thread consumed time. If the system has getrusage, we'll |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1209 | use that to get in-depth stats on the performance of the thread in addition |
| 1210 | to wall clock time stats from gettimeofday. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1211 | void |
| 1212 | thread_call (struct thread *thread) |
| 1213 | { |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1214 | unsigned long realtime, cputime; |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1215 | RUSAGE_T before, after; |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1216 | |
| 1217 | /* Cache a pointer to the relevant cpu history thread, if the thread |
| 1218 | * does not have it yet. |
| 1219 | * |
| 1220 | * Callers submitting 'dummy threads' hence must take care that |
| 1221 | * thread->cpu is NULL |
| 1222 | */ |
| 1223 | if (!thread->hist) |
| 1224 | { |
| 1225 | struct cpu_thread_history tmp; |
| 1226 | |
| 1227 | tmp.func = thread->func; |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 1228 | strcpy(tmp.funcname, thread->funcname); |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1229 | |
| 1230 | thread->hist = hash_get (cpu_record, &tmp, |
| 1231 | (void * (*) (void *))cpu_record_hash_alloc); |
| 1232 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1233 | |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1234 | GETRUSAGE (&before); |
| 1235 | thread->real = before.real; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1236 | |
| 1237 | (*thread->func) (thread); |
| 1238 | |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1239 | GETRUSAGE (&after); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1240 | |
Jorge Boncompte [DTI2] | 41af338 | 2012-05-07 16:53:12 +0000 | [diff] [blame] | 1241 | realtime = thread_consumed_time (&after, &before, &cputime); |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1242 | thread->hist->real.total += realtime; |
| 1243 | if (thread->hist->real.max < realtime) |
| 1244 | thread->hist->real.max = realtime; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1245 | #ifdef HAVE_RUSAGE |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1246 | thread->hist->cpu.total += cputime; |
| 1247 | if (thread->hist->cpu.max < cputime) |
| 1248 | thread->hist->cpu.max = cputime; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1249 | #endif |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1250 | |
Paul Jakma | cc8b13a | 2006-07-25 20:40:40 +0000 | [diff] [blame] | 1251 | ++(thread->hist->total_calls); |
| 1252 | thread->hist->types |= (1 << thread->add_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1253 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1254 | #ifdef CONSUMED_TIME_CHECK |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1255 | if (realtime > CONSUMED_TIME_CHECK) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1256 | { |
| 1257 | /* |
| 1258 | * We have a CPU Hog on our hands. |
| 1259 | * Whinge about it now, so we're aware this is yet another task |
| 1260 | * to fix. |
| 1261 | */ |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1262 | zlog_warn ("SLOW THREAD: task %s (%lx) ran for %lums (cpu time %lums)", |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1263 | thread->funcname, |
| 1264 | (unsigned long) thread->func, |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 1265 | realtime/1000, cputime/1000); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1266 | } |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 1267 | #endif /* CONSUMED_TIME_CHECK */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* Execute thread */ |
| 1271 | struct thread * |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1272 | funcname_thread_execute (struct thread_master *m, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1273 | int (*func)(struct thread *), |
| 1274 | void *arg, |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1275 | int val, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 1276 | const char* funcname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1277 | { |
| 1278 | struct thread dummy; |
| 1279 | |
| 1280 | memset (&dummy, 0, sizeof (struct thread)); |
| 1281 | |
| 1282 | dummy.type = THREAD_EVENT; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 1283 | dummy.add_type = THREAD_EXECUTE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1284 | dummy.master = NULL; |
| 1285 | dummy.func = func; |
| 1286 | dummy.arg = arg; |
| 1287 | dummy.u.val = val; |
Jorge Boncompte [DTI2] | 22714f9 | 2012-05-07 16:53:14 +0000 | [diff] [blame] | 1288 | strip_funcname (dummy.funcname, funcname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1289 | thread_call (&dummy); |
| 1290 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1291 | return NULL; |
| 1292 | } |