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