2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "dynamic-string.h"
27 VLOG_DEFINE_THIS_MODULE(coverage);
29 /* The coverage counters. */
30 #if USE_LINKER_SECTIONS
31 extern struct coverage_counter *__start_coverage[];
32 extern struct coverage_counter *__stop_coverage[];
33 #define coverage_counters __start_coverage
34 #define n_coverage_counters (__stop_coverage - __start_coverage)
35 #else /* !USE_LINKER_SECTIONS */
36 #define COVERAGE_COUNTER(NAME) COVERAGE_DEFINE__(NAME);
37 #include "coverage.def"
38 #undef COVERAGE_COUNTER
40 struct coverage_counter *coverage_counters[] = {
41 #define COVERAGE_COUNTER(NAME) &counter_##NAME,
42 #include "coverage.def"
43 #undef COVERAGE_COUNTER
45 #define n_coverage_counters ARRAY_SIZE(coverage_counters)
46 #endif /* !USE_LINKER_SECTIONS */
48 static unsigned int epoch;
51 coverage_unixctl_log(struct unixctl_conn *conn, int argc OVS_UNUSED,
52 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
54 coverage_log(VLL_WARN, false);
55 unixctl_command_reply(conn, 200, NULL);
61 unixctl_command_register("coverage/log", "", 0, 0,
62 coverage_unixctl_log, NULL);
65 /* Sorts coverage counters in descending order by count, within equal counts
66 * alphabetically by name. */
68 compare_coverage_counters(const void *a_, const void *b_)
70 const struct coverage_counter *const *ap = a_;
71 const struct coverage_counter *const *bp = b_;
72 const struct coverage_counter *a = *ap;
73 const struct coverage_counter *b = *bp;
74 if (a->count != b->count) {
75 return a->count < b->count ? 1 : -1;
77 return strcmp(a->name, b->name);
84 struct coverage_counter **c;
88 /* Sort coverage counters into groups with equal counts. */
89 c = xmalloc(n_coverage_counters * sizeof *c);
90 for (i = 0; i < n_coverage_counters; i++) {
91 c[i] = coverage_counters[i];
93 qsort(c, n_coverage_counters, sizeof *c, compare_coverage_counters);
95 /* Hash the names in each group along with the rank. */
97 for (i = 0; i < n_coverage_counters; ) {
104 hash = hash_int(i, hash);
105 for (j = i; j < n_coverage_counters; j++) {
106 if (c[j]->count != c[i]->count) {
109 hash = hash_string(c[j]->name, hash);
116 return hash_int(n_groups, hash);
120 coverage_hit(uint32_t hash)
122 enum { HIT_BITS = 1024, BITS_PER_WORD = 32 };
123 static uint32_t hit[HIT_BITS / BITS_PER_WORD];
124 BUILD_ASSERT_DECL(IS_POW2(HIT_BITS));
126 unsigned int bit_index = hash & (HIT_BITS - 1);
127 unsigned int word_index = bit_index / BITS_PER_WORD;
128 unsigned int word_mask = 1u << (bit_index % BITS_PER_WORD);
130 if (hit[word_index] & word_mask) {
133 hit[word_index] |= word_mask;
139 coverage_log_counter(enum vlog_level level, const struct coverage_counter *c)
141 VLOG(level, "%-24s %5u / %9llu", c->name, c->count, c->count + c->total);
144 /* Logs the coverage counters at the given vlog 'level'. If
145 * 'suppress_dups' is true, then duplicate events are not displayed.
146 * Care should be taken in the value used for 'level'. Depending on the
147 * configuration, syslog can write changes synchronously, which can
148 * cause the coverage messages to take several seconds to write. */
150 coverage_log(enum vlog_level level, bool suppress_dups)
156 if (!vlog_is_enabled(THIS_MODULE, level)) {
160 hash = coverage_hash();
162 if (coverage_hit(hash)) {
163 VLOG(level, "Skipping details of duplicate event coverage for "
164 "hash=%08"PRIx32" in epoch %u", hash, epoch);
170 VLOG(level, "Event coverage (epoch %u/entire run), hash=%08"PRIx32":",
172 for (i = 0; i < n_coverage_counters; i++) {
173 struct coverage_counter *c = coverage_counters[i];
175 coverage_log_counter(level, c);
178 for (i = 0; i < n_coverage_counters; i++) {
179 struct coverage_counter *c = coverage_counters[i];
182 coverage_log_counter(level, c);
188 VLOG(level, "%zu events never hit", n_never_hit);
191 /* Advances to the next epoch of coverage, resetting all the counters to 0. */
198 for (i = 0; i < n_coverage_counters; i++) {
199 struct coverage_counter *c = coverage_counters[i];
200 c->total += c->count;