From 6fde9c5ce6f76dee44d9a76480eb364a3593676c Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 2 Jul 2020 20:30:41 +0100 Subject: [PATCH] libsysprof-capture: Add sysprof_collector_mark_{v,}printf() methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These are just like `sysprof_collector_mark()`, but do the printf formatting of the message internally, and only once the collector has been fetched — so there is no overhead from the printf if sysprof is not enabled at runtime. Signed-off-by: Philip Withnall --- src/libsysprof-capture/sysprof-collector.c | 67 ++++++++++++++++++++++ src/libsysprof-capture/sysprof-collector.h | 16 ++++++ 2 files changed, 83 insertions(+) diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c index d95aee8c..6d9e6b1c 100644 --- a/src/libsysprof-capture/sysprof-collector.c +++ b/src/libsysprof-capture/sysprof-collector.c @@ -621,6 +621,73 @@ sysprof_collector_mark (int64_t time, } COLLECTOR_END; } +void +sysprof_collector_mark_printf (int64_t time, + int64_t duration, + const char *group, + const char *mark, + const char *message_format, + ...) +{ + va_list args; + + va_start (args, message_format); + sysprof_collector_mark_vprintf (time, duration, group, mark, message_format, args); + va_end (args); +} + +void +sysprof_collector_mark_vprintf (int64_t time, + int64_t duration, + const char *group, + const char *mark, + const char *message_format, + va_list args) +{ + COLLECTOR_BEGIN { + SysprofCaptureMark *ev; + size_t len; + size_t sl; + va_list args2; + + /* Need to take a copy of @args since we iterate through it twice, once to + * work out the formatted string length, and once to format it. */ + va_copy (args2, args); + + if (group == NULL) + group = ""; + + if (mark == NULL) + mark = ""; + + if (message_format == NULL) + message_format = ""; + + /* Work out the formatted message length */ + sl = vsnprintf (NULL, 0, message_format, args); + + len = realign (sizeof *ev + sl + 1); + + if ((ev = mapped_ring_buffer_allocate (collector->buffer, len))) + { + ev->frame.len = len; + ev->frame.type = SYSPROF_CAPTURE_FRAME_MARK; + ev->frame.cpu = _do_getcpu (); + ev->frame.pid = collector->pid; + ev->frame.time = time; + ev->duration = duration; + _sysprof_strlcpy (ev->group, group, sizeof ev->group); + _sysprof_strlcpy (ev->name, mark, sizeof ev->name); + vsnprintf (ev->message, sl + 1, message_format, args2); + ev->message[sl] = 0; + + mapped_ring_buffer_advance (collector->buffer, ev->frame.len); + } + + va_end (args2); + } COLLECTOR_END; +} + void sysprof_collector_log (int severity, const char *domain, diff --git a/src/libsysprof-capture/sysprof-collector.h b/src/libsysprof-capture/sysprof-collector.h index b91a5a7c..577c3bf7 100644 --- a/src/libsysprof-capture/sysprof-collector.h +++ b/src/libsysprof-capture/sysprof-collector.h @@ -56,6 +56,8 @@ #pragma once +#include + #include "sysprof-capture-types.h" #include "sysprof-macros.h" @@ -77,6 +79,20 @@ void sysprof_collector_mark (int64_t time, const char *group, const char *mark, const char *message); +SYSPROF_AVAILABLE_IN_3_38 +void sysprof_collector_mark_printf (int64_t time, + int64_t duration, + const char *group, + const char *mark, + const char *message_format, + ...) SYSPROF_PRINTF(5, 6); +SYSPROF_AVAILABLE_IN_3_38 +void sysprof_collector_mark_vprintf (int64_t time, + int64_t duration, + const char *group, + const char *mark, + const char *message_format, + va_list args) SYSPROF_PRINTF(5, 0); SYSPROF_AVAILABLE_IN_3_36 void sysprof_collector_log (int severity, const char *domain,