]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* Thread management routine header. |
3 | * Copyright (C) 1998 Kunihiro Ishiguro | |
718e3744 | 4 | */ |
5 | ||
6 | #ifndef _ZEBRA_THREAD_H | |
7 | #define _ZEBRA_THREAD_H | |
8 | ||
5734509c | 9 | #include <zebra.h> |
1189d95f | 10 | #include <pthread.h> |
75bcb355 QY |
11 | #include <poll.h> |
12 | #include "monotime.h" | |
fbcac826 | 13 | #include "frratomic.h" |
c284542b | 14 | #include "typesafe.h" |
60a3efec | 15 | #include "xref.h" |
5734509c | 16 | |
5e244469 RW |
17 | #ifdef __cplusplus |
18 | extern "C" { | |
19 | #endif | |
20 | ||
45f01188 DL |
21 | extern bool cputime_enabled; |
22 | extern unsigned long cputime_threshold; | |
23 | /* capturing wallclock time is always enabled since it is fast (reading | |
24 | * hardware TSC w/o syscalls) | |
25 | */ | |
26 | extern unsigned long walltime_threshold; | |
27 | ||
d62a17ae | 28 | struct rusage_t { |
6418e2d3 DL |
29 | #ifdef HAVE_CLOCK_THREAD_CPUTIME_ID |
30 | struct timespec cpu; | |
31 | #else | |
d62a17ae | 32 | struct rusage cpu; |
6418e2d3 | 33 | #endif |
d62a17ae | 34 | struct timeval real; |
8b70d0b0 | 35 | }; |
36 | #define RUSAGE_T struct rusage_t | |
37 | ||
db9c0df9 | 38 | #define GETRUSAGE(X) thread_getrusage(X) |
718e3744 | 39 | |
960b9a53 DL |
40 | PREDECL_LIST(thread_list); |
41 | PREDECL_HEAP(thread_timer_list); | |
4becea72 | 42 | |
d62a17ae | 43 | struct fd_handler { |
44 | /* number of pfd that fit in the allocated space of pfds. This is a | |
a9318a32 MS |
45 | * constant and is the same for both pfds and copy. |
46 | */ | |
d62a17ae | 47 | nfds_t pfdsize; |
48 | ||
49 | /* file descriptors to monitor for i/o */ | |
50 | struct pollfd *pfds; | |
51 | /* number of pollfds stored in pfds */ | |
52 | nfds_t pfdcount; | |
53 | ||
54 | /* chunk used for temp copy of pollfds */ | |
55 | struct pollfd *copy; | |
56 | /* number of pollfds stored in copy */ | |
57 | nfds_t copycount; | |
0a95a0d0 | 58 | }; |
0a95a0d0 | 59 | |
60a3efec DL |
60 | struct xref_threadsched { |
61 | struct xref xref; | |
62 | ||
63 | const char *funcname; | |
64 | const char *dest; | |
65 | uint32_t thread_type; | |
66 | }; | |
67 | ||
718e3744 | 68 | /* Master of the theads. */ |
d62a17ae | 69 | struct thread_master { |
70 | char *name; | |
71 | ||
72 | struct thread **read; | |
73 | struct thread **write; | |
27d29ced | 74 | struct thread_timer_list_head timer; |
c284542b | 75 | struct thread_list_head event, ready, unuse; |
d62a17ae | 76 | struct list *cancel_req; |
77 | bool canceled; | |
78 | pthread_cond_t cancel_cond; | |
79 | struct hash *cpu_record; | |
80 | int io_pipe[2]; | |
81 | int fd_limit; | |
82 | struct fd_handler handler; | |
83 | unsigned long alloc; | |
84 | long selectpoll_timeout; | |
85 | bool spin; | |
86 | bool handle_signals; | |
87 | pthread_mutex_t mtx; | |
88 | pthread_t owner; | |
5e822957 DS |
89 | |
90 | bool ready_run_loop; | |
91 | RUSAGE_T last_getrusage; | |
718e3744 | 92 | }; |
93 | ||
94 | /* Thread itself. */ | |
d62a17ae | 95 | struct thread { |
fbcac826 QY |
96 | uint8_t type; /* thread type */ |
97 | uint8_t add_type; /* thread type */ | |
c284542b | 98 | struct thread_list_item threaditem; |
27d29ced | 99 | struct thread_timer_list_item timeritem; |
d62a17ae | 100 | struct thread **ref; /* external reference (if given) */ |
101 | struct thread_master *master; /* pointer to the struct thread_master */ | |
cc9f21da | 102 | void (*func)(struct thread *); /* event function */ |
d62a17ae | 103 | void *arg; /* event argument */ |
104 | union { | |
105 | int val; /* second argument of the event. */ | |
106 | int fd; /* file descriptor in case of r/w */ | |
107 | struct timeval sands; /* rest of time sands value. */ | |
108 | } u; | |
d62a17ae | 109 | struct timeval real; |
110 | struct cpu_thread_history *hist; /* cache pointer to cpu_history */ | |
111 | unsigned long yield; /* yield time in microseconds */ | |
60a3efec | 112 | const struct xref_threadsched *xref; /* origin location */ |
d62a17ae | 113 | pthread_mutex_t mtx; /* mutex for thread.c functions */ |
e8b3a2f7 | 114 | bool ignore_timer_late; |
e04ab74d | 115 | }; |
116 | ||
f59e6882 DL |
117 | #ifdef _FRR_ATTRIBUTE_PRINTFRR |
118 | #pragma FRR printfrr_ext "%pTH" (struct thread *) | |
119 | #endif | |
120 | ||
d62a17ae | 121 | struct cpu_thread_history { |
cc9f21da | 122 | void (*func)(struct thread *); |
9b8e01ca DS |
123 | atomic_size_t total_cpu_warn; |
124 | atomic_size_t total_wall_warn; | |
1dd08c22 | 125 | atomic_size_t total_starv_warn; |
72327cf3 MS |
126 | atomic_size_t total_calls; |
127 | atomic_size_t total_active; | |
d62a17ae | 128 | struct time_stats { |
c8a65463 | 129 | atomic_size_t total, max; |
d62a17ae | 130 | } real; |
131 | struct time_stats cpu; | |
c8a65463 | 132 | atomic_uint_fast32_t types; |
d62a17ae | 133 | const char *funcname; |
718e3744 | 134 | }; |
135 | ||
ca1f4309 DS |
136 | /* Struct timeval's tv_usec one second value. */ |
137 | #define TIMER_SECOND_MICRO 1000000L | |
138 | ||
718e3744 | 139 | /* Thread types. */ |
140 | #define THREAD_READ 0 | |
141 | #define THREAD_WRITE 1 | |
142 | #define THREAD_TIMER 2 | |
143 | #define THREAD_EVENT 3 | |
144 | #define THREAD_READY 4 | |
a587d00b QY |
145 | #define THREAD_UNUSED 5 |
146 | #define THREAD_EXECUTE 6 | |
718e3744 | 147 | |
148 | /* Thread yield time. */ | |
17fc128d | 149 | #define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */ |
718e3744 | 150 | |
0447957e AK |
151 | #define THREAD_TIMER_STRLEN 12 |
152 | ||
718e3744 | 153 | /* Macros. */ |
154 | #define THREAD_ARG(X) ((X)->arg) | |
155 | #define THREAD_FD(X) ((X)->u.fd) | |
156 | #define THREAD_VAL(X) ((X)->u.val) | |
157 | ||
50478845 MS |
158 | /* |
159 | * Please consider this macro deprecated, and do not use it in new code. | |
160 | */ | |
161 | #define THREAD_OFF(thread) \ | |
162 | do { \ | |
163 | if ((thread)) \ | |
164 | thread_cancel(&(thread)); \ | |
165 | } while (0) | |
718e3744 | 166 | |
60a3efec DL |
167 | /* |
168 | * Macro wrappers to generate xrefs for all thread add calls. Includes | |
169 | * file/line/function info for debugging/tracing. | |
170 | */ | |
171 | #include "lib/xref.h" | |
172 | ||
173 | #define _xref_t_a(addfn, type, m, f, a, v, t) \ | |
174 | ({ \ | |
175 | static const struct xref_threadsched _xref \ | |
176 | __attribute__((used)) = { \ | |
177 | .xref = XREF_INIT(XREFT_THREADSCHED, NULL, __func__), \ | |
178 | .funcname = #f, \ | |
179 | .dest = #t, \ | |
180 | .thread_type = THREAD_ ## type, \ | |
181 | }; \ | |
182 | XREF_LINK(_xref.xref); \ | |
183 | _thread_add_ ## addfn(&_xref, m, f, a, v, t); \ | |
184 | }) \ | |
185 | /* end */ | |
186 | ||
187 | #define thread_add_read(m,f,a,v,t) _xref_t_a(read_write, READ, m,f,a,v,t) | |
188 | #define thread_add_write(m,f,a,v,t) _xref_t_a(read_write, WRITE, m,f,a,v,t) | |
189 | #define thread_add_timer(m,f,a,v,t) _xref_t_a(timer, TIMER, m,f,a,v,t) | |
190 | #define thread_add_timer_msec(m,f,a,v,t) _xref_t_a(timer_msec, TIMER, m,f,a,v,t) | |
191 | #define thread_add_timer_tv(m,f,a,v,t) _xref_t_a(timer_tv, TIMER, m,f,a,v,t) | |
44a5e507 | 192 | #define thread_add_event(m,f,a,v,t) _xref_t_a(event, EVENT, m,f,a,v,t) |
60a3efec DL |
193 | |
194 | #define thread_execute(m,f,a,v) \ | |
195 | ({ \ | |
196 | static const struct xref_threadsched _xref \ | |
197 | __attribute__((used)) = { \ | |
198 | .xref = XREF_INIT(XREFT_THREADSCHED, NULL, __func__), \ | |
199 | .funcname = #f, \ | |
200 | .dest = NULL, \ | |
201 | .thread_type = THREAD_EXECUTE, \ | |
202 | }; \ | |
203 | XREF_LINK(_xref.xref); \ | |
204 | _thread_execute(&_xref, m, f, a, v); \ | |
205 | }) /* end */ | |
fb9e46bb | 206 | |
718e3744 | 207 | /* Prototypes. */ |
d62a17ae | 208 | extern struct thread_master *thread_master_create(const char *); |
d8a8a8de | 209 | void thread_master_set_name(struct thread_master *master, const char *name); |
d62a17ae | 210 | extern void thread_master_free(struct thread_master *); |
495f0b13 | 211 | extern void thread_master_free_unused(struct thread_master *); |
8cc4198f | 212 | |
ee1455dd IR |
213 | extern void _thread_add_read_write(const struct xref_threadsched *xref, |
214 | struct thread_master *master, | |
cc9f21da | 215 | void (*fn)(struct thread *), void *arg, |
ee1455dd IR |
216 | int fd, struct thread **tref); |
217 | ||
218 | extern void _thread_add_timer(const struct xref_threadsched *xref, | |
219 | struct thread_master *master, | |
cc9f21da | 220 | void (*fn)(struct thread *), void *arg, long t, |
ee1455dd IR |
221 | struct thread **tref); |
222 | ||
223 | extern void _thread_add_timer_msec(const struct xref_threadsched *xref, | |
224 | struct thread_master *master, | |
cc9f21da | 225 | void (*fn)(struct thread *), void *arg, |
ee1455dd IR |
226 | long t, struct thread **tref); |
227 | ||
228 | extern void _thread_add_timer_tv(const struct xref_threadsched *xref, | |
229 | struct thread_master *master, | |
cc9f21da | 230 | void (*fn)(struct thread *), void *arg, |
ee1455dd IR |
231 | struct timeval *tv, struct thread **tref); |
232 | ||
233 | extern void _thread_add_event(const struct xref_threadsched *xref, | |
234 | struct thread_master *master, | |
cc9f21da | 235 | void (*fn)(struct thread *), void *arg, int val, |
ee1455dd | 236 | struct thread **tref); |
60a3efec DL |
237 | |
238 | extern void _thread_execute(const struct xref_threadsched *xref, | |
239 | struct thread_master *master, | |
cc9f21da | 240 | void (*fn)(struct thread *), void *arg, int val); |
9c7753e4 | 241 | |
b3d6bc6e | 242 | extern void thread_cancel(struct thread **event); |
d62a17ae | 243 | extern void thread_cancel_async(struct thread_master *, struct thread **, |
244 | void *); | |
a9318a32 MS |
245 | /* Cancel ready tasks with an arg matching 'arg' */ |
246 | extern void thread_cancel_event_ready(struct thread_master *m, void *arg); | |
247 | /* Cancel all tasks with an arg matching 'arg', including timers and io */ | |
248 | extern void thread_cancel_event(struct thread_master *m, void *arg); | |
d62a17ae | 249 | extern struct thread *thread_fetch(struct thread_master *, struct thread *); |
250 | extern void thread_call(struct thread *); | |
251 | extern unsigned long thread_timer_remain_second(struct thread *); | |
252 | extern struct timeval thread_timer_remain(struct thread *); | |
78ca0342 | 253 | extern unsigned long thread_timer_remain_msec(struct thread *); |
d62a17ae | 254 | extern int thread_should_yield(struct thread *); |
50596be0 | 255 | /* set yield time for thread */ |
d62a17ae | 256 | extern void thread_set_yield_time(struct thread *, unsigned long); |
718e3744 | 257 | |
55c72803 | 258 | /* Internal libfrr exports */ |
d62a17ae | 259 | extern void thread_getrusage(RUSAGE_T *); |
260 | extern void thread_cmd_init(void); | |
e04ab74d | 261 | |
8b70d0b0 | 262 | /* Returns elapsed real (wall clock) time. */ |
263 | extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before, | |
264 | unsigned long *cpu_time_elapsed); | |
265 | ||
d1265948 | 266 | /* only for use in logging functions! */ |
e0bebc7c | 267 | extern pthread_key_t thread_current; |
0447957e AK |
268 | extern char *thread_timer_to_hhmmss(char *buf, int buf_size, |
269 | struct thread *t_timer); | |
d1265948 | 270 | |
cfb9e0ee DS |
271 | static inline bool thread_is_scheduled(struct thread *thread) |
272 | { | |
273 | if (thread) | |
274 | return true; | |
275 | ||
276 | return false; | |
277 | } | |
278 | ||
1543c387 MS |
279 | /* Debug signal mask */ |
280 | void debug_signals(const sigset_t *sigs); | |
281 | ||
e8b3a2f7 DS |
282 | static inline void thread_ignore_late_timer(struct thread *thread) |
283 | { | |
284 | thread->ignore_timer_late = true; | |
285 | } | |
286 | ||
5e244469 RW |
287 | #ifdef __cplusplus |
288 | } | |
289 | #endif | |
290 | ||
718e3744 | 291 | #endif /* _ZEBRA_THREAD_H */ |