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