coverage: Time out the coverage hash suppression once a day.
[sliver-openvswitch.git] / lib / coverage.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 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 "dynamic-string.h"
22 #include "hash.h"
23 #include "timeval.h"
24 #include "unixctl.h"
25 #include "util.h"
26 #include "vlog.h"
27
28 VLOG_DEFINE_THIS_MODULE(coverage);
29
30 /* The coverage counters. */
31 #if USE_LINKER_SECTIONS
32 extern struct coverage_counter *__start_coverage[];
33 extern struct coverage_counter *__stop_coverage[];
34 #define coverage_counters __start_coverage
35 #define n_coverage_counters  (__stop_coverage - __start_coverage)
36 #else  /* !USE_LINKER_SECTIONS */
37 #define COVERAGE_COUNTER(NAME) COVERAGE_DEFINE__(NAME);
38 #include "coverage.def"
39 #undef COVERAGE_COUNTER
40
41 struct coverage_counter *coverage_counters[] = {
42 #define COVERAGE_COUNTER(NAME) &counter_##NAME,
43 #include "coverage.def"
44 #undef COVERAGE_COUNTER
45 };
46 #define n_coverage_counters ARRAY_SIZE(coverage_counters)
47 #endif  /* !USE_LINKER_SECTIONS */
48
49 static unsigned int epoch;
50
51 static void
52 coverage_unixctl_log(struct unixctl_conn *conn, int argc OVS_UNUSED,
53                      const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
54 {
55     coverage_log(VLL_WARN, false);
56     unixctl_command_reply(conn, 200, NULL);
57 }
58
59 void
60 coverage_init(void)
61 {
62     unixctl_command_register("coverage/log", "", 0, 0,
63                              coverage_unixctl_log, NULL);
64 }
65
66 /* Sorts coverage counters in descending order by count, within equal counts
67  * alphabetically by name. */
68 static int
69 compare_coverage_counters(const void *a_, const void *b_)
70 {
71     const struct coverage_counter *const *ap = a_;
72     const struct coverage_counter *const *bp = b_;
73     const struct coverage_counter *a = *ap;
74     const struct coverage_counter *b = *bp;
75     if (a->count != b->count) {
76         return a->count < b->count ? 1 : -1;
77     } else {
78         return strcmp(a->name, b->name);
79     }
80 }
81
82 static uint32_t
83 coverage_hash(void)
84 {
85     struct coverage_counter **c;
86     uint32_t hash = 0;
87     int n_groups, i;
88
89     /* Sort coverage counters into groups with equal counts. */
90     c = xmalloc(n_coverage_counters * sizeof *c);
91     for (i = 0; i < n_coverage_counters; i++) {
92         c[i] = coverage_counters[i];
93     }
94     qsort(c, n_coverage_counters, sizeof *c, compare_coverage_counters);
95
96     /* Hash the names in each group along with the rank. */
97     n_groups = 0;
98     for (i = 0; i < n_coverage_counters; ) {
99         int j;
100
101         if (!c[i]->count) {
102             break;
103         }
104         n_groups++;
105         hash = hash_int(i, hash);
106         for (j = i; j < n_coverage_counters; j++) {
107             if (c[j]->count != c[i]->count) {
108                 break;
109             }
110             hash = hash_string(c[j]->name, hash);
111         }
112         i = j;
113     }
114
115     free(c);
116
117     return hash_int(n_groups, hash);
118 }
119
120 static bool
121 coverage_hit(uint32_t hash)
122 {
123     enum { HIT_BITS = 1024, BITS_PER_WORD = 32 };
124     static uint32_t hit[HIT_BITS / BITS_PER_WORD];
125     BUILD_ASSERT_DECL(IS_POW2(HIT_BITS));
126
127     static long long int next_clear = LLONG_MIN;
128
129     unsigned int bit_index = hash & (HIT_BITS - 1);
130     unsigned int word_index = bit_index / BITS_PER_WORD;
131     unsigned int word_mask = 1u << (bit_index % BITS_PER_WORD);
132
133     /* Expire coverage hash suppression once a day. */
134     if (time_msec() >= next_clear) {
135         memset(hit, 0, sizeof hit);
136         next_clear = time_msec() + 60 * 60 * 24 * 1000LL;
137     }
138
139     if (hit[word_index] & word_mask) {
140         return true;
141     } else {
142         hit[word_index] |= word_mask;
143         return false;
144     }
145 }
146
147 static void
148 coverage_log_counter(enum vlog_level level, const struct coverage_counter *c)
149 {
150     VLOG(level, "%-24s %5u / %9llu", c->name, c->count, c->count + c->total);
151 }
152
153 /* Logs the coverage counters at the given vlog 'level'.  If
154  * 'suppress_dups' is true, then duplicate events are not displayed.
155  * Care should be taken in the value used for 'level'.  Depending on the
156  * configuration, syslog can write changes synchronously, which can
157  * cause the coverage messages to take several seconds to write. */
158 void
159 coverage_log(enum vlog_level level, bool suppress_dups)
160 {
161     size_t n_never_hit;
162     uint32_t hash;
163     size_t i;
164
165     if (!vlog_is_enabled(THIS_MODULE, level)) {
166         return;
167     }
168
169     hash = coverage_hash();
170     if (suppress_dups) {
171         if (coverage_hit(hash)) {
172             VLOG(level, "Skipping details of duplicate event coverage for "
173                  "hash=%08"PRIx32" in epoch %u", hash, epoch);
174             return;
175         }
176     }
177
178     n_never_hit = 0;
179     VLOG(level, "Event coverage (epoch %u/entire run), hash=%08"PRIx32":",
180          epoch, hash);
181     for (i = 0; i < n_coverage_counters; i++) {
182         struct coverage_counter *c = coverage_counters[i];
183         if (c->count) {
184             coverage_log_counter(level, c);
185         }
186     }
187     for (i = 0; i < n_coverage_counters; i++) {
188         struct coverage_counter *c = coverage_counters[i];
189         if (!c->count) {
190             if (c->total) {
191                 coverage_log_counter(level, c);
192             } else {
193                 n_never_hit++;
194             }
195         }
196     }
197     VLOG(level, "%zu events never hit", n_never_hit);
198 }
199
200 /* Advances to the next epoch of coverage, resetting all the counters to 0. */
201 void
202 coverage_clear(void)
203 {
204     size_t i;
205
206     epoch++;
207     for (i = 0; i < n_coverage_counters; i++) {
208         struct coverage_counter *c = coverage_counters[i];
209         c->total += c->count;
210         c->count = 0;
211     }
212 }