2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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"
29 VLOG_DEFINE_THIS_MODULE(coverage);
31 /* The coverage counters. */
32 #if USE_LINKER_SECTIONS
33 extern struct coverage_counter *__start_coverage[];
34 extern struct coverage_counter *__stop_coverage[];
35 #define coverage_counters __start_coverage
36 #define n_coverage_counters (__stop_coverage - __start_coverage)
37 #else /* !USE_LINKER_SECTIONS */
38 #define COVERAGE_COUNTER(COUNTER) \
39 DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, \
41 DEFINE_EXTERN_PER_THREAD_DATA(counter_##COUNTER, 0); \
42 static unsigned int COUNTER##_count(void) \
44 unsigned int *countp = counter_##COUNTER##_get(); \
45 unsigned int count = *countp; \
49 extern struct coverage_counter counter_##COUNTER; \
50 struct coverage_counter counter_##COUNTER \
51 = { #COUNTER, COUNTER##_count, 0 };
52 #include "coverage.def"
53 #undef COVERAGE_COUNTER
55 extern struct coverage_counter *coverage_counters[];
56 struct coverage_counter *coverage_counters[] = {
57 #define COVERAGE_COUNTER(NAME) &counter_##NAME,
58 #include "coverage.def"
59 #undef COVERAGE_COUNTER
61 #define n_coverage_counters ARRAY_SIZE(coverage_counters)
62 #endif /* !USE_LINKER_SECTIONS */
64 static struct ovs_mutex coverage_mutex = OVS_MUTEX_INITIALIZER;
66 static void coverage_read(struct svec *);
69 coverage_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
70 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
76 coverage_read(&lines);
77 reply = svec_join(&lines, "\n", "\n");
78 unixctl_command_reply(conn, reply);
86 unixctl_command_register("coverage/show", "", 0, 0,
87 coverage_unixctl_show, NULL);
90 /* Sorts coverage counters in descending order by total, within equal
91 * totals alphabetically by name. */
93 compare_coverage_counters(const void *a_, const void *b_)
95 const struct coverage_counter *const *ap = a_;
96 const struct coverage_counter *const *bp = b_;
97 const struct coverage_counter *a = *ap;
98 const struct coverage_counter *b = *bp;
99 if (a->total != b->total) {
100 return a->total < b->total ? 1 : -1;
102 return strcmp(a->name, b->name);
109 struct coverage_counter **c;
113 /* Sort coverage counters into groups with equal totals. */
114 c = xmalloc(n_coverage_counters * sizeof *c);
115 ovs_mutex_lock(&coverage_mutex);
116 for (i = 0; i < n_coverage_counters; i++) {
117 c[i] = coverage_counters[i];
119 ovs_mutex_unlock(&coverage_mutex);
120 qsort(c, n_coverage_counters, sizeof *c, compare_coverage_counters);
122 /* Hash the names in each group along with the rank. */
124 for (i = 0; i < n_coverage_counters; ) {
131 hash = hash_int(i, hash);
132 for (j = i; j < n_coverage_counters; j++) {
133 if (c[j]->total != c[i]->total) {
136 hash = hash_string(c[j]->name, hash);
143 return hash_int(n_groups, hash);
147 coverage_hit(uint32_t hash)
149 enum { HIT_BITS = 1024, BITS_PER_WORD = 32 };
150 static uint32_t hit[HIT_BITS / BITS_PER_WORD];
151 BUILD_ASSERT_DECL(IS_POW2(HIT_BITS));
153 static long long int next_clear = LLONG_MIN;
155 unsigned int bit_index = hash & (HIT_BITS - 1);
156 unsigned int word_index = bit_index / BITS_PER_WORD;
157 unsigned int word_mask = 1u << (bit_index % BITS_PER_WORD);
159 /* Expire coverage hash suppression once a day. */
160 if (time_msec() >= next_clear) {
161 memset(hit, 0, sizeof hit);
162 next_clear = time_msec() + 60 * 60 * 24 * 1000LL;
165 if (hit[word_index] & word_mask) {
168 hit[word_index] |= word_mask;
173 /* Logs the coverage counters, unless a similar set of events has already been
176 * This function logs at log level VLL_INFO. Use care before adjusting this
177 * level, because depending on its configuration, syslogd can write changes
178 * synchronously, which can cause the coverage messages to take several seconds
183 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 3);
185 if (!VLOG_DROP_INFO(&rl)) {
186 uint32_t hash = coverage_hash();
187 if (coverage_hit(hash)) {
188 VLOG_INFO("Skipping details of duplicate event coverage for "
189 "hash=%08"PRIx32, hash);
196 coverage_read(&lines);
197 SVEC_FOR_EACH (i, line, &lines) {
198 VLOG_INFO("%s", line);
200 svec_destroy(&lines);
205 /* Adds coverage counter information to 'lines'. */
207 coverage_read(struct svec *lines)
209 unsigned long long int *totals;
214 hash = coverage_hash();
217 svec_add_nocopy(lines,
218 xasprintf("Event coverage, hash=%08"PRIx32":", hash));
220 totals = xmalloc(n_coverage_counters * sizeof *totals);
221 ovs_mutex_lock(&coverage_mutex);
222 for (i = 0; i < n_coverage_counters; i++) {
223 totals[i] = coverage_counters[i]->total;
225 ovs_mutex_unlock(&coverage_mutex);
227 for (i = 0; i < n_coverage_counters; i++) {
229 svec_add_nocopy(lines, xasprintf("%-24s %9llu",
230 coverage_counters[i]->name,
236 svec_add_nocopy(lines, xasprintf("%zu events never hit", n_never_hit));
245 ovs_mutex_lock(&coverage_mutex);
246 for (i = 0; i < n_coverage_counters; i++) {
247 struct coverage_counter *c = coverage_counters[i];
248 c->total += c->count();
250 ovs_mutex_unlock(&coverage_mutex);