Update Red Hat Copyright Notices
[nbdkit.git] / plugins / vddk / stats.c
blob7f63333a880b9bf643c08b061744e4972ce82cd8
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
40 #include <pthread.h>
42 #define NBDKIT_API_VERSION 2
43 #include <nbdkit-plugin.h>
45 #include "vector.h"
47 #include "vddk.h"
49 /* Debug flags. */
50 NBDKIT_DLL_PUBLIC int vddk_debug_stats;
52 pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
54 /* For each VDDK API define a variable to store the time taken (used
55 * to implement -D vddk.stats=1).
57 #define STUB(fn, ret, args) struct vddk_stat stats_##fn = { .name = #fn }
58 #define OPTIONAL_STUB(fn, ret, args) STUB (fn, ret, args)
59 #include "vddk-stubs.h"
60 #undef STUB
61 #undef OPTIONAL_STUB
63 DEFINE_VECTOR_TYPE (statlist, struct vddk_stat);
65 static int
66 stat_compare (const void *vp1, const void *vp2)
68 const struct vddk_stat *st1 = vp1;
69 const struct vddk_stat *st2 = vp2;
71 /* Note: sorts in reverse order of time spent in each API call. */
72 if (st1->usecs < st2->usecs) return 1;
73 else if (st1->usecs > st2->usecs) return -1;
74 else return 0;
77 static const char *
78 api_name_without_prefix (const char *name)
80 return strncmp (name, "VixDiskLib_", 11) == 0 ? name + 11 : name;
83 void
84 display_stats (void)
86 statlist stats = empty_vector;
87 size_t i;
89 if (!vddk_debug_stats) return;
91 #define STUB(fn, ret, args) statlist_append (&stats, stats_##fn)
92 #define OPTIONAL_STUB(fn, ret, args) STUB (fn, ret, args)
93 #include "vddk-stubs.h"
94 #undef STUB
95 #undef OPTIONAL_STUB
97 qsort (stats.ptr, stats.len, sizeof stats.ptr[0], stat_compare);
99 nbdkit_debug ("VDDK function stats (-D vddk.stats=1):");
100 nbdkit_debug ("%-24s %15s %5s %15s",
101 "VixDiskLib_...", "µs", "calls", "bytes");
102 for (i = 0; i < stats.len; ++i) {
103 if (stats.ptr[i].usecs) {
104 if (stats.ptr[i].bytes > 0)
105 nbdkit_debug (" %-22s %15" PRIi64 " %5" PRIu64 " %15" PRIu64,
106 api_name_without_prefix (stats.ptr[i].name),
107 stats.ptr[i].usecs,
108 stats.ptr[i].calls,
109 stats.ptr[i].bytes);
110 else
111 nbdkit_debug (" %-22s %15" PRIi64 " %5" PRIu64,
112 api_name_without_prefix (stats.ptr[i].name),
113 stats.ptr[i].usecs,
114 stats.ptr[i].calls);
117 statlist_reset (&stats);