coverage: Make thread-safe.
[cascardo/ovs.git] / lib / coverage.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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 "svec.h"
24 #include "timeval.h"
25 #include "unixctl.h"
26 #include "util.h"
27 #include "vlog.h"
28
29 VLOG_DEFINE_THIS_MODULE(coverage);
30
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,                    \
40                                        counter_##COUNTER);              \
41         DEFINE_EXTERN_PER_THREAD_DATA(counter_##COUNTER, 0);            \
42         static unsigned int COUNTER##_count(void)                       \
43         {                                                               \
44             unsigned int *countp = counter_##COUNTER##_get();           \
45             unsigned int count = *countp;                               \
46             *countp = 0;                                                \
47             return count;                                               \
48         }                                                               \
49         struct coverage_counter counter_##COUNTER                       \
50             = { #COUNTER, COUNTER##_count, 0 };
51 #include "coverage.def"
52 #undef COVERAGE_COUNTER
53
54 struct coverage_counter *coverage_counters[] = {
55 #define COVERAGE_COUNTER(NAME) &counter_##NAME,
56 #include "coverage.def"
57 #undef COVERAGE_COUNTER
58 };
59 #define n_coverage_counters ARRAY_SIZE(coverage_counters)
60 #endif  /* !USE_LINKER_SECTIONS */
61
62 static struct ovs_mutex coverage_mutex = OVS_MUTEX_INITIALIZER;
63
64 static void coverage_read(struct svec *);
65
66 static void
67 coverage_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
68                      const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
69 {
70     struct svec lines;
71     char *reply;
72
73     svec_init(&lines);
74     coverage_read(&lines);
75     reply = svec_join(&lines, "\n", "\n");
76     unixctl_command_reply(conn, reply);
77     free(reply);
78     svec_destroy(&lines);
79 }
80
81 void
82 coverage_init(void)
83 {
84     unixctl_command_register("coverage/show", "", 0, 0,
85                              coverage_unixctl_show, NULL);
86 }
87
88 /* Sorts coverage counters in descending order by total, within equal
89  * totals alphabetically by name. */
90 static int
91 compare_coverage_counters(const void *a_, const void *b_)
92 {
93     const struct coverage_counter *const *ap = a_;
94     const struct coverage_counter *const *bp = b_;
95     const struct coverage_counter *a = *ap;
96     const struct coverage_counter *b = *bp;
97     if (a->total != b->total) {
98         return a->total < b->total ? 1 : -1;
99     } else {
100         return strcmp(a->name, b->name);
101     }
102 }
103
104 static uint32_t
105 coverage_hash(void)
106 {
107     struct coverage_counter **c;
108     uint32_t hash = 0;
109     int n_groups, i;
110
111     /* Sort coverage counters into groups with equal totals. */
112     c = xmalloc(n_coverage_counters * sizeof *c);
113     ovs_mutex_lock(&coverage_mutex);
114     for (i = 0; i < n_coverage_counters; i++) {
115         c[i] = coverage_counters[i];
116     }
117     ovs_mutex_unlock(&coverage_mutex);
118     qsort(c, n_coverage_counters, sizeof *c, compare_coverage_counters);
119
120     /* Hash the names in each group along with the rank. */
121     n_groups = 0;
122     for (i = 0; i < n_coverage_counters; ) {
123         int j;
124
125         if (!c[i]->total) {
126             break;
127         }
128         n_groups++;
129         hash = hash_int(i, hash);
130         for (j = i; j < n_coverage_counters; j++) {
131             if (c[j]->total != c[i]->total) {
132                 break;
133             }
134             hash = hash_string(c[j]->name, hash);
135         }
136         i = j;
137     }
138
139     free(c);
140
141     return hash_int(n_groups, hash);
142 }
143
144 static bool
145 coverage_hit(uint32_t hash)
146 {
147     enum { HIT_BITS = 1024, BITS_PER_WORD = 32 };
148     static uint32_t hit[HIT_BITS / BITS_PER_WORD];
149     BUILD_ASSERT_DECL(IS_POW2(HIT_BITS));
150
151     static long long int next_clear = LLONG_MIN;
152
153     unsigned int bit_index = hash & (HIT_BITS - 1);
154     unsigned int word_index = bit_index / BITS_PER_WORD;
155     unsigned int word_mask = 1u << (bit_index % BITS_PER_WORD);
156
157     /* Expire coverage hash suppression once a day. */
158     if (time_msec() >= next_clear) {
159         memset(hit, 0, sizeof hit);
160         next_clear = time_msec() + 60 * 60 * 24 * 1000LL;
161     }
162
163     if (hit[word_index] & word_mask) {
164         return true;
165     } else {
166         hit[word_index] |= word_mask;
167         return false;
168     }
169 }
170
171 /* Logs the coverage counters, unless a similar set of events has already been
172  * logged.
173  *
174  * This function logs at log level VLL_INFO.  Use care before adjusting this
175  * level, because depending on its configuration, syslogd can write changes
176  * synchronously, which can cause the coverage messages to take several seconds
177  * to write. */
178 void
179 coverage_log(void)
180 {
181     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 3);
182
183     if (!VLOG_DROP_INFO(&rl)) {
184         uint32_t hash = coverage_hash();
185         if (coverage_hit(hash)) {
186             VLOG_INFO("Skipping details of duplicate event coverage for "
187                       "hash=%08"PRIx32, hash);
188         } else {
189             struct svec lines;
190             const char *line;
191             size_t i;
192
193             svec_init(&lines);
194             coverage_read(&lines);
195             SVEC_FOR_EACH (i, line, &lines) {
196                 VLOG_INFO("%s", line);
197             }
198             svec_destroy(&lines);
199         }
200     }
201 }
202
203 /* Adds coverage counter information to 'lines'. */
204 static void
205 coverage_read(struct svec *lines)
206 {
207     unsigned long long int *totals;
208     size_t n_never_hit;
209     uint32_t hash;
210     size_t i;
211
212     hash = coverage_hash();
213
214     n_never_hit = 0;
215     svec_add_nocopy(lines,
216                     xasprintf("Event coverage, hash=%08"PRIx32":", hash));
217
218     totals = xmalloc(n_coverage_counters * sizeof *totals);
219     ovs_mutex_lock(&coverage_mutex);
220     for (i = 0; i < n_coverage_counters; i++) {
221         totals[i] = coverage_counters[i]->total;
222     }
223     ovs_mutex_unlock(&coverage_mutex);
224
225     for (i = 0; i < n_coverage_counters; i++) {
226         if (totals[i]) {
227             svec_add_nocopy(lines, xasprintf("%-24s %9llu",
228                                              coverage_counters[i]->name,
229                                              totals[i]));
230         } else {
231             n_never_hit++;
232         }
233     }
234     svec_add_nocopy(lines, xasprintf("%zu events never hit", n_never_hit));
235     free(totals);
236 }
237
238 void
239 coverage_clear(void)
240 {
241     size_t i;
242
243     ovs_mutex_lock(&coverage_mutex);
244     for (i = 0; i < n_coverage_counters; i++) {
245         struct coverage_counter *c = coverage_counters[i];
246         c->total += c->count();
247     }
248     ovs_mutex_unlock(&coverage_mutex);
249 }