Skip to content

Commit 9e9afd8

Browse files
committed
DPL: add ability to have singposts controlled actions
1 parent b3ac562 commit 9e9afd8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Framework/Foundation/include/Framework/Signpost.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,20 @@ bool _o2_lock_free_stack_push(_o2_lock_free_stack& stack, const int& value, bool
202202
bool _o2_lock_free_stack_pop(_o2_lock_free_stack& stack, int& value, bool spin = false);
203203
void* _o2_log_create(char const* name, int stacktrace);
204204
void _o2_signpost_event_emit(_o2_log_t* log, _o2_signpost_id_t id, char const* name, char const* const format, ...);
205+
void _o2_singpost_action(_o2_log_t* log, void (*callback)(void*));
205206
void _o2_signpost_interval_begin(_o2_log_t* log, _o2_signpost_id_t id, char const* name, char const* const format, ...);
206207
void _o2_signpost_interval_end(_o2_log_t* log, _o2_signpost_id_t id, char const* name, char const* const format, ...);
207208
void _o2_log_set_stacktrace(_o2_log_t* log, int stacktrace);
208209

210+
// Helper to invoke a callback when the signpost is enabled. The callback
211+
// gets passed some previously stored context (nullptr for now).
212+
// TODO: I use a separate function because in the future this might change and I might
213+
// allow to store some context as part of the activity.
214+
inline void _o2_signpost_action(_o2_log_t* log, void (*callback)(void*))
215+
{
216+
callback(nullptr);
217+
}
218+
209219
// This generates a unique id for a signpost. Do not use this directly, use O2_SIGNPOST_ID_GENERATE instead.
210220
// Notice that this is only valid on a given computer.
211221
// This is guaranteed to be unique at 5 GHz for at least 63 years, if my math is correct.
@@ -488,6 +498,17 @@ void o2_debug_log_set_stacktrace(_o2_log_t* log, int stacktrace)
488498
})
489499
#define O2_SIGNPOST_ID_FROM_POINTER(name, log, pointer) _o2_signpost_id_t name = _o2_signpost_id_make_with_pointer(private_o2_log_##log, pointer)
490500
#define O2_SIGNPOST_ID_GENERATE(name, log) _o2_signpost_id_t name = _o2_signpost_id_generate_local(private_o2_log_##log)
501+
502+
// Execute the provided callback if the log is enabled. Useful e.g. to dump IgProf profiles
503+
// only if the signpost is enabled or to add remote telemetry for certain events.
504+
#define O2_SIGNPOST_ACTION(log, callback) __extension__({ \
505+
if (O2_BUILTIN_UNLIKELY(O2_SIGNPOST_ENABLED_MAC(log))) { \
506+
_o2_signpost_action(private_o2_log_##log, callback); \
507+
} else if (O2_BUILTIN_UNLIKELY(private_o2_log_##log->stacktrace)) { \
508+
_o2_signpost_action(private_o2_log_##log, callback); \
509+
} \
510+
})
511+
491512
// In case Instruments is attached, we switch to the Apple signpost API otherwise, both one
492513
// mac and on linux we use our own implementation, using the logger. We can use the same ids because
493514
// they are compatible between the two implementations, we also use remove_engineering_type to remove

Framework/Foundation/test/test_SignpostLogger.cxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,11 @@ int main(int argc, char** argv)
6363
O2_SIGNPOST_ID_GENERATE(idStacktrace, SignpostStacktrace);
6464
O2_LOG_ENABLE(SignpostStacktrace);
6565
O2_SIGNPOST_EVENT_EMIT_ERROR(SignpostStacktrace, idStacktrace, "Test category", "An error with stacktrace %d \n", 1);
66+
// Test actions associtated to a given debug stream.
67+
static bool testMustCall = false;
68+
static bool testMustNotCall = false;
69+
O2_SIGNPOST_ACTION(SignpostStacktrace, [](void *) { testMustCall = true; });
70+
O2_LOG_DISABLE(SignpostStacktrace);
71+
O2_SIGNPOST_ACTION(SignpostStacktrace, [](void *) { testMustNotCall = true; });
72+
return testMustCall && (!testMustNotCall) ? 0 : 1;
6673
}

0 commit comments

Comments
 (0)