Zth (libzth)
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
1#ifndef ZTH_CONFIG_H
2#define ZTH_CONFIG_H
3/*
4 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 */
8
14#include <libzth/macros.h>
15
16#ifdef __cplusplus
17# include <libzth/init.h>
18
19# include <cstddef>
20# include <sys/time.h>
21
22# if __cplusplus >= 201103L
23# include <cstdint>
24# else
25# include <stdint.h>
26# endif
27
28# include <memory>
29
30namespace zth {
31
35
36bool config(int env /* one of Env::* */, bool whenUnset);
37
46# define zth_config(name) (::zth::config(::zth::Env::name, ::zth::Config::name))
47
48# if __cplusplus < 201103L
49# define ZTH_CONSTEXPR_RETURN(type, ...) \
50 type x = {__VA_ARGS__}; \
51 return x;
52# else // C++11 and up
53# define ZTH_CONSTEXPR_RETURN(type, ...) return {__VA_ARGS__};
54# endif // C++11 and up
55
58 static bool const Debug =
59# ifndef NDEBUG
60# ifdef ZTH_CONFIG_DEBUG
61 ZTH_CONFIG_DEBUG;
62# else
63 true;
64# endif
65# else
66 false;
67# endif
68
70 static bool const EnableAssert =
71# ifndef NDEBUG
72 Debug
73# ifdef ZTH_CONFIG_ENABLE_ASSERT
74 && ZTH_CONFIG_ENABLE_ASSERT
75# endif
76 ;
77# else
78 false;
79# endif
80
85 static bool const EnableFullAssert =
86# ifdef ZTH_CONFIG_ENABLE_FULL_ASSERT
87 EnableAssert && ZTH_CONFIG_ENABLE_FULL_ASSERT;
88# elif defined(ZTH_OS_BAREMETAL)
89 // Assume we are a bit short on memory.
90 false;
91# else
93# endif
94
98 static bool const EnableBacktrace =
99# ifdef ZTH_CONFIG_ENABLE_BACKTRACE
100 ZTH_CONFIG_ENABLE_BACKTRACE;
101# else
102 Debug;
103# endif
104
106 static bool const EnableThreads =
107# if ZTH_THREADS
108 true;
109# else
110 false;
111# endif
112
119 static bool const EnableDebugPrint =
120# ifdef ZTH_CONFIG_ENABLE_DEBUG_PRINT
121 ZTH_CONFIG_ENABLE_DEBUG_PRINT;
122# else
123 false;
124# endif
125
131 static bool const SupportDebugPrint =
132# ifdef ZTH_CONFIG_SUPPORT_DEBUG_PRINT
133 Debug && ZTH_CONFIG_SUPPORT_DEBUG_PRINT;
134# elif defined(ZTH_OS_BAREMETAL)
135 // Without OS, there is no environment to enable debugging when
136 // it is not enabled right away.
138# else
139 Debug;
140# endif
141
143 static bool const EnableColorLog =
144# ifdef ZTH_CONFIG_ENABLE_COLOR_LOG
145 ZTH_CONFIG_ENABLE_COLOR_LOG;
146# else
147 true;
148# endif
149
154 static int const Print_banner = 12; // bright blue
155 static int const Print_worker = 5; // magenta
156 static int const Print_waiter = 1; // red
157 static int const Print_io = 9; // bright red
158 static int const Print_perf = 6; // cyan
159 static int const Print_fiber = 10; // bright green
160 static int const Print_context = 2; // green
161 static int const Print_sync = 11; // bright yellow
162 static int const Print_list = 8; // bright black
163 static int const Print_zmq = 9; // bright red
164 static int const Print_fsm = 14; // bright cyan
165 static int const Print_thread = 3; // yellow
166 static int const Print_coro = 13; // bright magenta
167
169 static size_t const DefaultFiberStackSize =
170# ifdef ZTH_CONFIG_DEFAULT_FIBER_STACK_SIZE
171 ZTH_CONFIG_DEFAULT_FIBER_STACK_SIZE;
172# elif defined(ZTH_OS_BAREMETAL)
173 0x2000;
174# else
175 0x20000;
176# endif
177
179 static bool const EnableStackGuard =
180# ifdef ZTH_CONFIG_ENABLE_STACK_GUARD
181 ZTH_CONFIG_ENABLE_STACK_GUARD;
182# else
183 Debug;
184# endif
185
187 static bool const EnableStackWaterMark =
188# ifdef ZTH_CONFIG_ENABLE_STACK_WATER_MARK
189 ZTH_CONFIG_ENABLE_STACK_WATER_MARK;
190# else
191 Debug;
192# endif
193
195 static bool const ContextSignals =
196# if defined(ZTH_CONFIG_CONTEXT_SIGNALS) && !defined(ZTH_OS_BAREMETAL)
197 ZTH_CONFIG_CONTEXT_SIGNALS;
198# else
199 false;
200# endif
201
203 constexpr static struct timespec MinTimeslice()
204 {
205# ifdef ZTH_CONFIG_MIN_TIMESLICE
206# define ZTH_CONFIG_MIN_TIMESLICE_ ZTH_CONFIG_MIN_TIMESLICE
207# else
208# define ZTH_CONFIG_MIN_TIMESLICE_ 100000
209# endif
211# undef ZTH_CONFIG_MIN_TIMESLICE_
212 }
214 constexpr static struct timespec TimesliceOverrunReportThreshold()
215 {
216# ifdef ZTH_CONFIG_TIMESLICE_OVERRUN_REPORT_THRESHOLD
217# define ZTH_CONFIG_TIMESLICE_OVERRUN_REPORT_THRESHOLD_ \
218 ZTH_CONFIG_TIMESLICE_OVERRUN_REPORT_THRESHOLD
219# else
220# define ZTH_CONFIG_TIMESLICE_OVERRUN_REPORT_THRESHOLD_ 10000000
221# endif
224# undef ZTH_CONFIG_TIMESLICE_OVERRUN_REPORT_THRESHOLD_
225 }
226
228 static bool const CheckTimesliceOverrun =
229# ifdef ZTH_CONFIG_CHECK_TIMESLICE_OVERRUN
230 ZTH_CONFIG_CHECK_TIMESLICE_OVERRUN;
231# else
232 Debug;
233# endif
234
237
239 static size_t const PerfEventBufferSize =
240# ifdef ZTH_CONFIG_PERF_EVENT_BUFFER_SIZE
241 ZTH_CONFIG_PERF_EVENT_BUFFER_SIZE;
242# elif defined(ZTH_OS_BAREMETAL)
243 0x400;
244# else
245 0x4000;
246# endif
247
249 static size_t const PerfEventBufferSpare =
251
262 static bool const DoPerfEvent = false;
263
265 static bool const EnablePerfEvent =
266# ifdef ZTH_CONFIG_ENABLE_PERF_EVENT
267 ZTH_CONFIG_ENABLE_PERF_EVENT;
268# elif defined(ZTH_OS_BAREMETAL)
269 false;
270# else
271 true;
272# endif
274 static bool const PerfSyscall =
275# ifdef ZTH_CONFIG_PERF_SYSCALL
276 ZTH_CONFIG_PERF_SYSCALL;
277# else
278 true;
279# endif
280
282 static bool const NamedFsm = Debug || (SupportDebugPrint && Print_fsm > 0);
283
285 static bool const NamedObjects =
287
289 static bool const UseZMQ =
290# ifdef ZTH_HAVE_LIBZMQ
291 true;
292# else
293 false;
294# endif
295
297 static bool const UseLimitedFormatSpecifiers =
298# if defined(ZTH_FORMAT_LIMITED) && ZTH_FORMAT_LIMITED
299 true;
300# else
301 false;
302# endif
303
305 static bool const EnableExceptions =
306# if defined(__cpp_exceptions) && !defined(ZTH_DISABLE_EXCEPTIONS)
307 true;
308# else
309 false;
310# endif
311
319 template <typename T>
320 struct Allocator {
321 typedef std::allocator<T> type;
322 };
323};
324} // namespace zth
325#endif // __cplusplus
326
327#include "zth_config.h"
328
329#undef ZTH_CONSTEXPR_RETURN
330
331#ifdef __cplusplus
332namespace zth {
333
356
357void checkConfig(int check /* one if Check::Config_... */, size_t value);
358
359static inline void checkConfig()
360{
361 static bool checked;
362 if(checked)
363 return;
364
365# define ZTH_CHECK(x) checkConfig(Check::Config_##x, (size_t)Config::x);
366
367 ZTH_CHECK(Debug)
368 ZTH_CHECK(EnableAssert)
369 ZTH_CHECK(EnableFullAssert)
370 ZTH_CHECK(EnableBacktrace)
371 ZTH_CHECK(EnableThreads)
372 ZTH_CHECK(SupportDebugPrint)
373 ZTH_CHECK(EnableColorLog)
374 ZTH_CHECK(DefaultFiberStackSize)
375 ZTH_CHECK(EnableStackGuard)
376 ZTH_CHECK(EnableStackWaterMark)
377 ZTH_CHECK(ContextSignals)
378 ZTH_CHECK(CheckTimesliceOverrun)
379 ZTH_CHECK(PerfEventBufferSize)
380 ZTH_CHECK(EnablePerfEvent)
381 ZTH_CHECK(PerfSyscall)
382 ZTH_CHECK(UseZMQ)
383 ZTH_CHECK(UseLimitedFormatSpecifiers)
384 ZTH_CHECK(EnableExceptions)
385
386# undef ZTH_CHECK
387
388# ifdef __cpp_exceptions
389 static_assert(Config::EnableExceptions);
390# else
391 static_assert(!Config::EnableExceptions);
392# endif
393
394 checked = true;
395}
396
397ZTH_APP_INIT_CALL(checkConfig)
398
399} // namespace zth
400#endif // __cplusplus
401
402#endif // ZTH_CONFIG_H
#define ZTH_CHECK(x)
#define ZTH_CONSTEXPR_RETURN(type,...)
Definition config.h:53
#define ZTH_CONFIG_TIMESLICE_OVERRUN_REPORT_THRESHOLD_
#define ZTH_CONFIG_MIN_TIMESLICE_
#define ZTH_APP_INIT_CALL(f)
Definition init.h:91
@ Config_EnableFullAssert
Definition config.h:338
@ Config_EnableAssert
Definition config.h:337
@ Config_EnablePerfEvent
Definition config.h:349
@ Config_CheckTimesliceOverrun
Definition config.h:347
@ Config_PerfSyscall
Definition config.h:350
@ Config_EnableColorLog
Definition config.h:342
@ Config_SupportDebugPrint
Definition config.h:341
@ Config_DefaultFiberStackSize
Definition config.h:343
@ Config_EnableThreads
Definition config.h:340
@ Config_Debug
Definition config.h:336
@ Config_EnableStackGuard
Definition config.h:344
@ Config_PerfEventBufferSize
Definition config.h:348
@ Config_EnableBacktrace
Definition config.h:339
@ Config_ContextSignals
Definition config.h:346
@ Config_UseLimitedFormatSpecifiers
Definition config.h:352
@ Config_UseZMQ
Definition config.h:351
@ Config_EnableExceptions
Definition config.h:353
@ Config_EnableStackWaterMark
Definition config.h:345
std::allocator< T > type
Definition config.h:321
static int const Print_zmq
Definition config.h:163
static int const Print_perf
Definition config.h:158
static int const Print_context
Definition config.h:160
static bool const EnablePerfEvent
Enable (but not necessarily record) perf.
Definition config.h:265
static bool const DoPerfEvent
Record and output perf events to file automatically.
Definition config.h:262
static int const Print_coro
Definition config.h:166
static bool const EnableBacktrace
Enable backtrace support.
Definition config.h:98
static size_t const DefaultFiberStackSize
Default fiber stack size in bytes.
Definition config.h:169
static bool const EnableExceptions
Indicate if exceptions are supported.
Definition config.h:305
static bool const EnableDebugPrint
Actually do print the debug output.
Definition config.h:119
static int const Print_waiter
Definition config.h:156
static bool const Debug
This is a debug build when set to true.
Definition config.h:58
static bool const EnableAssert
When true, enable zth_assert().
Definition config.h:70
static int const Print_fiber
Definition config.h:159
static int const Print_worker
Definition config.h:155
static int const Print_banner
ANSI color used by zth_dbg(). Printing this category is disabled when set to 0.
Definition config.h:154
static size_t const PerfEventBufferSize
Buffer size for perf events.
Definition config.h:239
static bool const ContextSignals
Take POSIX signal into account when doing a context switch.
Definition config.h:195
static bool const EnableStackWaterMark
When true, enable stack watermark to detect maximum stack usage.
Definition config.h:187
static bool const EnableThreads
Add (Worker) thread support when true.
Definition config.h:106
static int const Print_thread
Definition config.h:165
static bool const NamedFsm
Use named FSM guards/actions.
Definition config.h:282
static bool const EnableStackGuard
When true, enable stack guards.
Definition config.h:179
static bool const CheckTimesliceOverrun
Check time slice overrun at every context switch.
Definition config.h:228
static bool const UseZMQ
Enable ZeroMQ support.
Definition config.h:289
static int const Print_list
Definition config.h:162
static int const Print_io
Definition config.h:157
static bool const NamedObjects
Use named objects.
Definition config.h:285
static bool const EnableColorLog
Enable colored output.
Definition config.h:143
static int const Print_sync
Definition config.h:161
static bool const PerfSyscall
Also record syscalls by perf.
Definition config.h:274
static constexpr struct timespec TimesliceOverrunReportThreshold()
Print an overrun reported when this timeslice is exceeded.
Definition config.h:214
static int const Print_fsm
Definition config.h:164
static bool const SupportDebugPrint
Add support to enable debug output prints.
Definition config.h:131
static bool const EnableFullAssert
Show failing expression in case of a failed assert.
Definition config.h:85
static constexpr struct timespec MinTimeslice()
Minimum time slice before zth::yield() actually yields.
Definition config.h:203
static bool const NamedSynchronizer
Save names for all zth::Synchronizer instances.
Definition config.h:236
static size_t const PerfEventBufferSpare
Minimum remaining space before perf event collection is stopped.
Definition config.h:249
static bool const UseLimitedFormatSpecifiers
Use limited formatting specifiers.
Definition config.h:297
@ DoPerfEvent
Definition config.h:33
@ PerfSyscall
Definition config.h:33
@ CheckTimesliceOverrun
Definition config.h:33
@ EnableDebugPrint
Definition config.h:33