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