3a4249
From 517a37ae7beeb77e2b4870be11611f82c1200b3c Mon Sep 17 00:00:00 2001
3a4249
From: Matthew Almond <malmond@fb.com>
3a4249
Date: Thu, 11 Jun 2020 13:01:04 -0700
3a4249
Subject: [PATCH 2/2] Measure plugin
3a4249
3a4249
---
3a4249
 macros.in           |   1 +
3a4249
 plugins/Makefile.am |   4 +
3a4249
 plugins/measure.c   | 231 ++++++++++++++++++++++++++++++++++++++++++++
3a4249
 3 files changed, 236 insertions(+)
3a4249
 create mode 100644 plugins/measure.c
3a4249
3a4249
diff --git a/macros.in b/macros.in
3a4249
index 3cc8a3555..c8a087959 100644
3a4249
--- a/macros.in
3a4249
+++ b/macros.in
3a4249
@@ -1173,5 +1173,6 @@ package or when debugging this package.\
3a4249
 # Transaction plugin macros
3a4249
 %__plugindir		%{_libdir}/rpm-plugins
3a4249
+%__transaction_measure		%{__plugindir}/measure.so
3a4249
 %__transaction_systemd_inhibit	%{__plugindir}/systemd_inhibit.so
3a4249
 %__transaction_selinux		%{__plugindir}/selinux.so
3a4249
 %__transaction_syslog		%{__plugindir}/syslog.so
3a4249
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
3a4249
index 06393ce8d..daed6423c 100644
3a4249
--- a/plugins/Makefile.am
3a4249
+++ b/plugins/Makefile.am
3a4249
@@ -29,6 +29,10 @@ systemd_inhibit_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/
3a4249
 plugins_LTLIBRARIES += systemd_inhibit.la
3a4249
 endif
3a4249
 
3a4249
+measure_la_SOURCES = measure.c
3a4249
+measure_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
3a4249
+plugins_LTLIBRARIES += measure.la
3a4249
+
3a4249
 prioreset_la_SOURCES = prioreset.c
3a4249
 prioreset_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
3a4249
 plugins_LTLIBRARIES += prioreset.la
3a4249
diff --git a/plugins/measure.c b/plugins/measure.c
3a4249
new file mode 100644
3a4249
index 000000000..2cdfc885e
3a4249
--- /dev/null
3a4249
+++ b/plugins/measure.c
3a4249
@@ -0,0 +1,231 @@
3a4249
+#include "system.h"
3a4249
+#include "time.h"
3a4249
+
3a4249
+#include <rpm/rpmlog.h>
3a4249
+#include <rpm/rpmmacro.h>
3a4249
+#include <rpm/rpmts.h>
3a4249
+#include "lib/rpmlib.h"
3a4249
+
3a4249
+#include "lib/rpmplugin.h"
3a4249
+
3a4249
+struct measurestat {
3a4249
+    /* We're counting psm not packages because packages often run psm_pre/post
3a4249
+       more than once and we want to accumulate the time
3a4249
+    */
3a4249
+    unsigned int psm_count;
3a4249
+    unsigned int scriptlet_count;
3a4249
+    struct timespec plugin_start;
3a4249
+    struct timespec psm_start;
3a4249
+    struct timespec scriptlet_start;
3a4249
+};
3a4249
+
3a4249
+static rpmRC push(const char *format, const char *value, const char *formatted)
3a4249
+{
3a4249
+    char *key = NULL;
3a4249
+    rpmRC rc = RPMRC_FAIL;
3a4249
+    if (formatted == NULL) {
3a4249
+        /* yes we're okay with discarding const here */
3a4249
+        key = (char *)format;
3a4249
+    } else {
3a4249
+        if (rasprintf(&key, format, formatted) == -1) {
3a4249
+            rpmlog(
3a4249
+                RPMLOG_ERR,
3a4249
+                _("measure: Failed to allocate formatted key %s, %s\n"),
3a4249
+                format,
3a4249
+                formatted
3a4249
+            );
3a4249
+            goto exit;
3a4249
+        }
3a4249
+    }
3a4249
+    if (rpmPushMacro(NULL, key, NULL, value, RMIL_GLOBAL)) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to set %s\n"), key);
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    rc = RPMRC_OK;
3a4249
+exit:
3a4249
+    if (formatted != NULL) {
3a4249
+        free(key);
3a4249
+    }
3a4249
+    return rc;
3a4249
+}
3a4249
+
3a4249
+static rpmRC diff_ms(char **ms, struct timespec *start, struct timespec *end)
3a4249
+{
3a4249
+    if (rasprintf(ms, "%ld", (
3a4249
+        (end->tv_sec - start->tv_sec) * 1000 +
3a4249
+        (end->tv_nsec - start->tv_nsec) / 1000000
3a4249
+    )) == -1) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted ms\n"));
3a4249
+        return RPMRC_FAIL;
3a4249
+    }
3a4249
+    return RPMRC_OK;
3a4249
+}
3a4249
+
3a4249
+static rpmRC measure_init(rpmPlugin plugin, rpmts ts)
3a4249
+{
3a4249
+    rpmRC rc = RPMRC_FAIL;
3a4249
+    struct measurestat *state = rcalloc(1, sizeof(*state));
3a4249
+    if (clock_gettime(CLOCK_MONOTONIC, &state->plugin_start)) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get plugin_start time\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    state->psm_count = 0;
3a4249
+    state->scriptlet_count = 0;
3a4249
+    rpmPluginSetData(plugin, state);
3a4249
+    rc = RPMRC_OK;
3a4249
+exit:
3a4249
+    return rc;
3a4249
+}
3a4249
+
3a4249
+static void measure_cleanup(rpmPlugin plugin)
3a4249
+{
3a4249
+    struct measurestat *state = rpmPluginGetData(plugin);
3a4249
+    free(state);
3a4249
+}
3a4249
+
3a4249
+static rpmRC measure_tsm_post(rpmPlugin plugin, rpmts ts, int res)
3a4249
+{
3a4249
+    struct measurestat *state = rpmPluginGetData(plugin);
3a4249
+    char *psm_count = NULL, *scriptlet_count = NULL;
3a4249
+    rpmRC rc = RPMRC_FAIL;
3a4249
+
3a4249
+    if (rasprintf(&psm_count, "%d", state->psm_count) == -1) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted psm_count\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (rasprintf(&scriptlet_count, "%d", state->scriptlet_count) == -1) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted scriptlet_count\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("_measure_plugin_psm_count", psm_count, NULL) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("_measure_plugin_scriptlet_count", scriptlet_count, NULL) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    rc = RPMRC_OK;
3a4249
+exit:
3a4249
+    free(psm_count);
3a4249
+    free(scriptlet_count);
3a4249
+    return rc;
3a4249
+}
3a4249
+
3a4249
+static rpmRC measure_psm_pre(rpmPlugin plugin, rpmte te)
3a4249
+{
3a4249
+    struct measurestat *state = rpmPluginGetData(plugin);
3a4249
+    rpmRC rc = RPMRC_FAIL;
3a4249
+
3a4249
+    if (clock_gettime(CLOCK_MONOTONIC, &state->psm_start)) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get psm_start time\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    rc = RPMRC_OK;
3a4249
+exit:
3a4249
+    return rc;
3a4249
+}
3a4249
+
3a4249
+static rpmRC measure_psm_post(rpmPlugin plugin, rpmte te, int res)
3a4249
+{
3a4249
+    struct measurestat *state = rpmPluginGetData(plugin);
3a4249
+    struct timespec end;
3a4249
+    char *offset = NULL, *duration = NULL, *prefix = NULL;
3a4249
+    Header h = rpmteHeader(te);
3a4249
+    rpmRC rc = RPMRC_FAIL;
3a4249
+
3a4249
+    if (clock_gettime(CLOCK_MONOTONIC, &end)) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get psm end time\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (rasprintf(&prefix, "_measure_plugin_package_%u", state->psm_count) == -1) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate prefix\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (diff_ms(&offset, &state->plugin_start, &state->psm_start) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (diff_ms(&duration, &state->psm_start, &end) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_nevra", rpmteNEVRA(te), prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_compressor", headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR), prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_offset", offset, prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_ms", duration, prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    state->psm_count += 1;
3a4249
+    rc = RPMRC_OK;
3a4249
+exit:
3a4249
+    headerFree(h);
3a4249
+    free(prefix);
3a4249
+    free(duration);
3a4249
+    free(offset);
3a4249
+    return rc;
3a4249
+}
3a4249
+
3a4249
+static rpmRC measure_scriptlet_pre(rpmPlugin plugin,
3a4249
+					const char *s_name, int type)
3a4249
+{
3a4249
+    struct measurestat *state = rpmPluginGetData(plugin);
3a4249
+    if (clock_gettime(CLOCK_MONOTONIC, &state->scriptlet_start)) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get scriptlet_start time\n"));
3a4249
+        return RPMRC_FAIL;
3a4249
+    }
3a4249
+    return RPMRC_OK;
3a4249
+}
3a4249
+
3a4249
+static rpmRC measure_scriptlet_post(rpmPlugin plugin,
3a4249
+					const char *s_name, int type, int res)
3a4249
+{
3a4249
+    struct measurestat *state = rpmPluginGetData(plugin);
3a4249
+    struct timespec end;
3a4249
+    char *offset = NULL, *duration = NULL, *prefix = NULL;
3a4249
+    rpmRC rc = RPMRC_FAIL;
3a4249
+
3a4249
+    if (clock_gettime(CLOCK_MONOTONIC, &end)) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to get end time\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+
3a4249
+    if (rasprintf(&prefix, "_measure_plugin_scriptlet_%d", state->scriptlet_count) == -1) {
3a4249
+        rpmlog(RPMLOG_ERR, _("measure: Failed to allocate formatted prefix\n"));
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (diff_ms(&offset, &state->plugin_start, &state->scriptlet_start) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (diff_ms(&duration, &state->scriptlet_start, &end) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_name", s_name, prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_offset", offset, prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    if (push("%s_ms", duration, prefix) != RPMRC_OK) {
3a4249
+        goto exit;
3a4249
+    }
3a4249
+    state->scriptlet_count += 1;
3a4249
+    rc = RPMRC_OK;
3a4249
+exit:
3a4249
+    free(prefix);
3a4249
+    free(duration);
3a4249
+    free(offset);
3a4249
+    return rc;
3a4249
+}
3a4249
+
3a4249
+struct rpmPluginHooks_s measure_hooks = {
3a4249
+    .init = measure_init,
3a4249
+    .cleanup = measure_cleanup,
3a4249
+    .tsm_post = measure_tsm_post,
3a4249
+    .psm_pre = measure_psm_pre,
3a4249
+    .psm_post = measure_psm_post,
3a4249
+    .scriptlet_pre = measure_scriptlet_pre,
3a4249
+    .scriptlet_post = measure_scriptlet_post,
3a4249
+};
3a4249
-- 
3a4249
2.24.1
3a4249