teknoraver / rpms / rpm

Forked from rpms/rpm 4 months ago
Clone

Blame 0001-Measure-plugin.patch

2733a1
From cf758158efa2832b38a49770f3489f408cb93b41 Mon Sep 17 00:00:00 2001
2733a1
From: Matthew Almond <malmond@meta.com>
2733a1
Date: Fri, 6 Dec 2024 08:00:31 +0100
2733a1
Subject: [PATCH] Measure plugin
2733a1
2733a1
---
2733a1
 macros.in              |   1 +
2733a1
 plugins/CMakeLists.txt |   1 +
2733a1
 plugins/measure.c      | 232 +++++++++++++++++++++++++++++++++++++++++
2733a1
 3 files changed, 234 insertions(+)
2733a1
 create mode 100644 plugins/measure.c
2733a1
2733a1
diff --git a/macros.in b/macros.in
2733a1
index df7defe..1af187a 100644
2733a1
--- a/macros.in
2733a1
+++ b/macros.in
2733a1
@@ -1189,6 +1189,7 @@ Supplements:   (%{name} = %{version}-%{release} and langpacks-%{1})\
2733a1
 # Transaction plugin macros
2733a1
 %__plugindir		%{_libdir}/rpm-plugins
2733a1
 %__transaction_reflink		%{__plugindir}/reflink.so
2733a1
+%__transaction_measure		%{__plugindir}/measure.so
2733a1
 %__transaction_systemd_inhibit	%{__plugindir}/systemd_inhibit.so
2733a1
 %__transaction_selinux		%{__plugindir}/selinux.so
2733a1
 %__transaction_syslog		%{__plugindir}/syslog.so
2733a1
diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt
2733a1
index ed63a43..cda8f96 100644
2733a1
--- a/plugins/CMakeLists.txt
2733a1
+++ b/plugins/CMakeLists.txt
2733a1
@@ -1,6 +1,7 @@
2733a1
 add_library(prioreset MODULE prioreset.c)
2733a1
 add_library(syslog MODULE syslog.c)
2733a1
 add_library(reflink MODULE reflink.c)
2733a1
+add_library(measure MODULE measure.c)
2733a1
 
2733a1
 if(WITH_SELINUX)
2733a1
 	add_library(selinux MODULE selinux.c)
2733a1
diff --git a/plugins/measure.c b/plugins/measure.c
2733a1
new file mode 100644
2733a1
index 0000000..593e463
2733a1
--- /dev/null
2733a1
+++ b/plugins/measure.c
2733a1
@@ -0,0 +1,232 @@
2733a1
+#include "system.h"
2733a1
+#include "time.h"
2733a1
+
2733a1
+#include <rpm/rpmlog.h>
2733a1
+#include <rpm/rpmmacro.h>
2733a1
+#include <rpm/rpmts.h>
2733a1
+#include <rpm/rpmlib.h>
2733a1
+#include <rpm/rpmstring.h>
2733a1
+
2733a1
+#include "rpmplugin.h"
2733a1
+
2733a1
+struct measurestat {
2733a1
+    /* We're counting psm not packages because packages often run psm_pre/post
2733a1
+       more than once and we want to accumulate the time
2733a1
+    */
2733a1
+    unsigned int psm_count;
2733a1
+    unsigned int scriptlet_count;
2733a1
+    struct timespec plugin_start;
2733a1
+    struct timespec psm_start;
2733a1
+    struct timespec scriptlet_start;
2733a1
+};
2733a1
+
2733a1
+static rpmRC push(const char *format, const char *value, const char *formatted)
2733a1
+{
2733a1
+    char *key = NULL;
2733a1
+    rpmRC rc = RPMRC_FAIL;
2733a1
+    if (formatted == NULL) {
2733a1
+        /* yes we're okay with discarding const here */
2733a1
+        key = (char *)format;
2733a1
+    } else {
2733a1
+        if (rasprintf(&key, format, formatted) == -1) {
2733a1
+            rpmlog(
2733a1
+                RPMLOG_ERR,
2733a1
+                _("measure: Failed to allocate formatted key %s, %s\n"),
2733a1
+                format,
2733a1
+                formatted
2733a1
+            );
2733a1
+            goto exit;
2733a1
+        }
2733a1
+    }
2733a1
+    if (rpmPushMacro(NULL, key, NULL, value, RMIL_GLOBAL)) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to set %s\n"), key);
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    rc = RPMRC_OK;
2733a1
+exit:
2733a1
+    if (formatted != NULL) {
2733a1
+        free(key);
2733a1
+    }
2733a1
+    return rc;
2733a1
+}
2733a1
+
2733a1
+static rpmRC diff_ms(char **ms, struct timespec *start, struct timespec *end)
2733a1
+{
2733a1
+    if (rasprintf(ms, "%ld", (
2733a1
+        (end->tv_sec - start->tv_sec) * 1000 +
2733a1
+        (end->tv_nsec - start->tv_nsec) / 1000000
2733a1
+    )) == -1) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted ms\n"));
2733a1
+        return RPMRC_FAIL;
2733a1
+    }
2733a1
+    return RPMRC_OK;
2733a1
+}
2733a1
+
2733a1
+static rpmRC measure_init(rpmPlugin plugin, rpmts ts)
2733a1
+{
2733a1
+    rpmRC rc = RPMRC_FAIL;
2733a1
+    struct measurestat *state = rcalloc(1, sizeof(*state));
2733a1
+    if (clock_gettime(CLOCK_MONOTONIC, &state->plugin_start)) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get plugin_start time\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    state->psm_count = 0;
2733a1
+    state->scriptlet_count = 0;
2733a1
+    rpmPluginSetData(plugin, state);
2733a1
+    rc = RPMRC_OK;
2733a1
+exit:
2733a1
+    return rc;
2733a1
+}
2733a1
+
2733a1
+static void measure_cleanup(rpmPlugin plugin)
2733a1
+{
2733a1
+    struct measurestat *state = rpmPluginGetData(plugin);
2733a1
+    free(state);
2733a1
+}
2733a1
+
2733a1
+static rpmRC measure_tsm_post(rpmPlugin plugin, rpmts ts, int res)
2733a1
+{
2733a1
+    struct measurestat *state = rpmPluginGetData(plugin);
2733a1
+    char *psm_count = NULL, *scriptlet_count = NULL;
2733a1
+    rpmRC rc = RPMRC_FAIL;
2733a1
+
2733a1
+    if (rasprintf(&psm_count, "%d", state->psm_count) == -1) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted psm_count\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (rasprintf(&scriptlet_count, "%d", state->scriptlet_count) == -1) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted scriptlet_count\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("_measure_plugin_psm_count", psm_count, NULL) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("_measure_plugin_scriptlet_count", scriptlet_count, NULL) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    rc = RPMRC_OK;
2733a1
+exit:
2733a1
+    free(psm_count);
2733a1
+    free(scriptlet_count);
2733a1
+    return rc;
2733a1
+}
2733a1
+
2733a1
+static rpmRC measure_psm_pre(rpmPlugin plugin, rpmte te)
2733a1
+{
2733a1
+    struct measurestat *state = rpmPluginGetData(plugin);
2733a1
+    rpmRC rc = RPMRC_FAIL;
2733a1
+
2733a1
+    if (clock_gettime(CLOCK_MONOTONIC, &state->psm_start)) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get psm_start time\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    rc = RPMRC_OK;
2733a1
+exit:
2733a1
+    return rc;
2733a1
+}
2733a1
+
2733a1
+static rpmRC measure_psm_post(rpmPlugin plugin, rpmte te, int res)
2733a1
+{
2733a1
+    struct measurestat *state = rpmPluginGetData(plugin);
2733a1
+    struct timespec end;
2733a1
+    char *offset = NULL, *duration = NULL, *prefix = NULL;
2733a1
+    Header h = rpmteHeader(te);
2733a1
+    rpmRC rc = RPMRC_FAIL;
2733a1
+
2733a1
+    if (clock_gettime(CLOCK_MONOTONIC, &end)) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get psm end time\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (rasprintf(&prefix, "_measure_plugin_package_%u", state->psm_count) == -1) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate prefix\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (diff_ms(&offset, &state->plugin_start, &state->psm_start) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (diff_ms(&duration, &state->psm_start, &end) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_nevra", rpmteNEVRA(te), prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_compressor", headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR), prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_offset", offset, prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_ms", duration, prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    state->psm_count += 1;
2733a1
+    rc = RPMRC_OK;
2733a1
+exit:
2733a1
+    headerFree(h);
2733a1
+    free(prefix);
2733a1
+    free(duration);
2733a1
+    free(offset);
2733a1
+    return rc;
2733a1
+}
2733a1
+
2733a1
+static rpmRC measure_scriptlet_pre(rpmPlugin plugin,
2733a1
+					const char *s_name, int type)
2733a1
+{
2733a1
+    struct measurestat *state = rpmPluginGetData(plugin);
2733a1
+    if (clock_gettime(CLOCK_MONOTONIC, &state->scriptlet_start)) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get scriptlet_start time\n"));
2733a1
+        return RPMRC_FAIL;
2733a1
+    }
2733a1
+    return RPMRC_OK;
2733a1
+}
2733a1
+
2733a1
+static rpmRC measure_scriptlet_post(rpmPlugin plugin,
2733a1
+					const char *s_name, int type, int res)
2733a1
+{
2733a1
+    struct measurestat *state = rpmPluginGetData(plugin);
2733a1
+    struct timespec end;
2733a1
+    char *offset = NULL, *duration = NULL, *prefix = NULL;
2733a1
+    rpmRC rc = RPMRC_FAIL;
2733a1
+
2733a1
+    if (clock_gettime(CLOCK_MONOTONIC, &end)) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get end time\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+
2733a1
+    if (rasprintf(&prefix, "_measure_plugin_scriptlet_%d", state->scriptlet_count) == -1) {
2733a1
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted prefix\n"));
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (diff_ms(&offset, &state->plugin_start, &state->scriptlet_start) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (diff_ms(&duration, &state->scriptlet_start, &end) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_name", s_name, prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_offset", offset, prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    if (push("%s_ms", duration, prefix) != RPMRC_OK) {
2733a1
+        goto exit;
2733a1
+    }
2733a1
+    state->scriptlet_count += 1;
2733a1
+    rc = RPMRC_OK;
2733a1
+exit:
2733a1
+    free(prefix);
2733a1
+    free(duration);
2733a1
+    free(offset);
2733a1
+    return rc;
2733a1
+}
2733a1
+
2733a1
+struct rpmPluginHooks_s measure_hooks = {
2733a1
+    .init = measure_init,
2733a1
+    .cleanup = measure_cleanup,
2733a1
+    .tsm_post = measure_tsm_post,
2733a1
+    .psm_pre = measure_psm_pre,
2733a1
+    .psm_post = measure_psm_post,
2733a1
+    .scriptlet_pre = measure_scriptlet_pre,
2733a1
+    .scriptlet_post = measure_scriptlet_post,
2733a1
+};
2733a1
-- 
2733a1
2.47.1
2733a1