Provide ability to retrieve coverage information
[sliver-openvswitch.git] / lib / coverage.c
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "coverage.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include "coverage-counters.h"
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "unixctl.h"
25 #include "util.h"
26
27 #define THIS_MODULE VLM_coverage
28 #include "vlog.h"
29
30 static unsigned int epoch;
31
32 static void
33 coverage_unixctl_log(struct unixctl_conn *conn, const char *args UNUSED)
34 {
35     coverage_log(VLL_WARN, false);
36     unixctl_command_reply(conn, 200, NULL);
37 }
38
39 static void
40 coverage_unixctl_clear(struct unixctl_conn *conn, const char *args UNUSED)
41 {
42     coverage_clear();
43     unixctl_command_reply(conn, 200, NULL);
44 }
45
46 void
47 coverage_init(void)
48 {
49     unixctl_command_register("coverage/log", coverage_unixctl_log);
50     unixctl_command_register("coverage/clear", coverage_unixctl_clear);
51 }
52
53 /* Sorts coverage counters in descending order by count, within equal counts
54  * alphabetically by name. */
55 static int
56 compare_coverage_counters(const void *a_, const void *b_)
57 {
58     const struct coverage_counter *const *ap = a_;
59     const struct coverage_counter *const *bp = b_;
60     const struct coverage_counter *a = *ap;
61     const struct coverage_counter *b = *bp;
62     if (a->count != b->count) {
63         return a->count < b->count ? 1 : -1;
64     } else {
65         return strcmp(a->name, b->name);
66     }
67 }
68
69 static uint32_t
70 coverage_hash(void)
71 {
72     struct coverage_counter **c;
73     uint32_t hash = 0;
74     int n_groups, i;
75
76     /* Sort coverage counters into groups with equal counts. */
77     c = xmalloc(coverage_n_counters * sizeof *c);
78     for (i = 0; i < coverage_n_counters; i++) {
79         c[i] = coverage_counters[i];
80     }
81     qsort(c, coverage_n_counters, sizeof *c, compare_coverage_counters);
82
83     /* Hash the names in each group along with the rank. */
84     n_groups = 0;
85     for (i = 0; i < coverage_n_counters; ) {
86         int j;
87
88         if (!c[i]->count) {
89             break;
90         }
91         n_groups++;
92         hash = hash_int(i, hash);
93         for (j = i; j < coverage_n_counters; j++) {
94             if (c[j]->count != c[i]->count) {
95                 break;
96             }
97             hash = hash_string(c[j]->name, hash);
98         }
99         i = j;
100     }
101
102     free(c);
103
104     return hash_int(n_groups, hash);
105 }
106
107 static bool
108 coverage_hit(uint32_t hash)
109 {
110     enum { HIT_BITS = 1024, BITS_PER_WORD = 32 };
111     static uint32_t hit[HIT_BITS / BITS_PER_WORD];
112     BUILD_ASSERT_DECL(IS_POW2(HIT_BITS));
113
114     unsigned int bit_index = hash & (HIT_BITS - 1);
115     unsigned int word_index = bit_index / BITS_PER_WORD;
116     unsigned int word_mask = 1u << (bit_index % BITS_PER_WORD);
117
118     if (hit[word_index] & word_mask) {
119         return true;
120     } else {
121         hit[word_index] |= word_mask;
122         return false;
123     }
124 }
125
126 static void
127 coverage_log_counter(enum vlog_level level, const struct coverage_counter *c)
128 {
129     VLOG(level, "%-24s %5u / %9llu", c->name, c->count, c->count + c->total);
130 }
131
132 /* Logs the coverage counters at the given vlog 'level'.  If
133  * 'suppress_dups' is true, then duplicate events are not displayed. */
134 void
135 coverage_log(enum vlog_level level, bool suppress_dups)
136 {
137     size_t n_never_hit;
138     uint32_t hash;
139     size_t i;
140
141     if (!vlog_is_enabled(THIS_MODULE, level)) {
142         return;
143     }
144
145     if (suppress_dups) {
146         hash = coverage_hash();
147         if (coverage_hit(hash)) {
148             VLOG(level, "Skipping details of duplicate event coverage for "
149                  "hash=%08"PRIx32" in epoch %u", hash, epoch);
150             return;
151         }
152     }
153
154     n_never_hit = 0;
155     VLOG(level, "Event coverage (epoch %u/entire run), hash=%08"PRIx32":",
156          epoch, hash);
157     for (i = 0; i < coverage_n_counters; i++) {
158         struct coverage_counter *c = coverage_counters[i];
159         if (c->count) {
160             coverage_log_counter(level, c);
161         }
162     }
163     for (i = 0; i < coverage_n_counters; i++) {
164         struct coverage_counter *c = coverage_counters[i];
165         if (!c->count) {
166             if (c->total) {
167                 coverage_log_counter(level, c);
168             } else {
169                 n_never_hit++;
170             }
171         }
172     }
173     VLOG(level, "%zu events never hit", n_never_hit);
174 }
175
176 /* Advances to the next epoch of coverage, resetting all the counters to 0. */
177 void
178 coverage_clear(void)
179 {
180     size_t i;
181
182     epoch++;
183     for (i = 0; i < coverage_n_counters; i++) {
184         struct coverage_counter *c = coverage_counters[i];
185         c->total += c->count;
186         c->count = 0;
187     }
188 }