libsysprof-capture: Add sysprof_collector_mark_{v,}printf() methods

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 <withnall@endlessm.com>
This commit is contained in:
Philip Withnall
2020-07-02 20:30:41 +01:00
parent e8790bc3d6
commit 6fde9c5ce6
2 changed files with 83 additions and 0 deletions

View File

@ -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,

View File

@ -56,6 +56,8 @@
#pragma once
#include <stdarg.h>
#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,