Blame SOURCES/0001-vddk-Refactor-how-D-vddk.stats-1-is-collected.patch

6661d0
From 1d9a1341c6d9b0f2150a37ec3617eeee01bbfee2 Mon Sep 17 00:00:00 2001
7084e2
From: "Richard W.M. Jones" <rjones@redhat.com>
7084e2
Date: Thu, 21 Oct 2021 14:49:52 +0100
7084e2
Subject: [PATCH] vddk: Refactor how -D vddk.stats=1 is collected
7084e2
7084e2
In order to allow us to collect more per-API stats, introduce a global
7084e2
struct per API for storing these stats.
7084e2
7084e2
(cherry picked from commit 3d8657f3d9a2c1b59284333566428b4c7ce32a74)
7084e2
---
7084e2
 plugins/vddk/vddk.c | 36 ++++++++++++++++++------------------
7084e2
 1 file changed, 18 insertions(+), 18 deletions(-)
7084e2
7084e2
diff --git a/plugins/vddk/vddk.c b/plugins/vddk/vddk.c
7084e2
index 80f5870e..3d751544 100644
7084e2
--- a/plugins/vddk/vddk.c
7084e2
+++ b/plugins/vddk/vddk.c
7084e2
@@ -1,5 +1,5 @@
7084e2
 /* nbdkit
7084e2
- * Copyright (C) 2013-2020 Red Hat Inc.
7084e2
+ * Copyright (C) 2013-2021 Red Hat Inc.
7084e2
  *
7084e2
  * Redistribution and use in source and binary forms, with or without
7084e2
  * modification, are permitted provided that the following conditions are
7084e2
@@ -103,14 +103,23 @@ static bool is_remote;
7084e2
 /* For each VDDK API define a variable to store the time taken (used
7084e2
  * to implement -D vddk.stats=1).
7084e2
  */
7084e2
+struct vddk_stat {
7084e2
+  const char *name;             /* function name */
7084e2
+  int64_t usecs;                /* total number of usecs consumed */
7084e2
+};
7084e2
 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
7084e2
 static void display_stats (void);
7084e2
-#define STUB(fn,ret,args) static int64_t stats_##fn;
7084e2
-#define OPTIONAL_STUB(fn,ret,args) static int64_t stats_##fn;
7084e2
+#define STUB(fn,ret,args) \
7084e2
+  static struct vddk_stat stats_##fn = { .name = #fn }
7084e2
+#define OPTIONAL_STUB(fn,ret,args) \
7084e2
+  static struct vddk_stat stats_##fn = { .name = #fn }
7084e2
 #include "vddk-stubs.h"
7084e2
 #undef STUB
7084e2
 #undef OPTIONAL_STUB
7084e2
 
7084e2
+/* Macros to bracket each VDDK API call, for printing debugging
7084e2
+ * information and collecting statistics.
7084e2
+ */
7084e2
 #define VDDK_CALL_START(fn, fs, ...)                                    \
7084e2
   do {                                                                  \
7084e2
   struct timeval start_t, end_t;                                        \
7084e2
@@ -131,10 +140,11 @@ static void display_stats (void);
7084e2
   if (vddk_debug_stats) {                               \
7084e2
     gettimeofday (&end_t, NULL);                        \
7084e2
     ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&stats_lock);       \
7084e2
-    stats_##fn += tvdiff_usec (&start_t, &end_t);       \
7084e2
+    stats_##fn.usecs += tvdiff_usec (&start_t, &end_t); \
7084e2
   }                                                     \
7084e2
   } while (0)
7084e2
 
7084e2
+/* Print VDDK errors. */
7084e2
 #define VDDK_ERROR(err, fs, ...)                                \
7084e2
   do {                                                          \
7084e2
     char *vddk_err_msg;                                         \
7084e2
@@ -167,10 +177,6 @@ vddk_unload (void)
7084e2
   free (password);
7084e2
 }
7084e2
 
7084e2
-struct vddk_stat {
7084e2
-  const char *fn;
7084e2
-  int64_t usecs;
7084e2
-};
7084e2
 DEFINE_VECTOR_TYPE(statlist, struct vddk_stat)
7084e2
 
7084e2
 static int
7084e2
@@ -179,7 +185,7 @@ stat_compare (const void *vp1, const void *vp2)
7084e2
   const struct vddk_stat *st1 = vp1;
7084e2
   const struct vddk_stat *st2 = vp2;
7084e2
 
7084e2
-  /* Note: sorts in reverse order. */
7084e2
+  /* Note: sorts in reverse order of time spent in each API call. */
7084e2
   if (st1->usecs < st2->usecs) return 1;
7084e2
   else if (st1->usecs > st2->usecs) return -1;
7084e2
   else return 0;
7084e2
@@ -189,19 +195,13 @@ static void
7084e2
 display_stats (void)
7084e2
 {
7084e2
   statlist stats = empty_vector;
7084e2
-  struct vddk_stat st;
7084e2
   size_t i;
7084e2
 
7084e2
-#define ADD_ONE_STAT(fn_, usecs_)               \
7084e2
-  st.fn = fn_;                                  \
7084e2
-  st.usecs = usecs_;                            \
7084e2
-  statlist_append (&stats, st)
7084e2
-#define STUB(fn,ret,args) ADD_ONE_STAT (#fn, stats_##fn);
7084e2
-#define OPTIONAL_STUB(fn,ret,args) ADD_ONE_STAT (#fn, stats_##fn);
7084e2
+#define STUB(fn,ret,args) statlist_append (&stats, stats_##fn)
7084e2
+#define OPTIONAL_STUB(fn,ret,args) statlist_append (&stats, stats_##fn)
7084e2
 #include "vddk-stubs.h"
7084e2
 #undef STUB
7084e2
 #undef OPTIONAL_STUB
7084e2
-#undef ADD_ONE_STAT
7084e2
 
7084e2
   qsort (stats.ptr, stats.size, sizeof stats.ptr[0], stat_compare);
7084e2
 
7084e2
@@ -209,7 +209,7 @@ display_stats (void)
7084e2
   nbdkit_debug ("%-40s  %9s", "", "µs");
7084e2
   for (i = 0; i < stats.size; ++i) {
7084e2
     if (stats.ptr[i].usecs)
7084e2
-      nbdkit_debug ("%-40s %9" PRIi64, stats.ptr[i].fn, stats.ptr[i].usecs);
7084e2
+      nbdkit_debug ("%-40s %9" PRIi64, stats.ptr[i].name, stats.ptr[i].usecs);
7084e2
   }
7084e2
   statlist_reset (&stats);
7084e2
 }
7084e2
-- 
7084e2
2.31.1
7084e2