2 * Copyright (c) 2009, 2010, 2011, 2012 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(NAME) COVERAGE_DEFINE__(NAME);
39 #include "coverage.def"
40 #undef COVERAGE_COUNTER
42 struct coverage_counter
*coverage_counters
[] = {
43 #define COVERAGE_COUNTER(NAME) &counter_##NAME,
44 #include "coverage.def"
45 #undef COVERAGE_COUNTER
47 #define n_coverage_counters ARRAY_SIZE(coverage_counters)
48 #endif /* !USE_LINKER_SECTIONS */
50 static unsigned int epoch
;
52 static void coverage_read(struct svec
*);
55 coverage_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
56 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
62 coverage_read(&lines
);
63 reply
= svec_join(&lines
, "\n", "\n");
64 unixctl_command_reply(conn
, reply
);
72 unixctl_command_register("coverage/show", "", 0, 0,
73 coverage_unixctl_show
, NULL
);
76 /* Sorts coverage counters in descending order by count, within equal counts
77 * alphabetically by name. */
79 compare_coverage_counters(const void *a_
, const void *b_
)
81 const struct coverage_counter
*const *ap
= a_
;
82 const struct coverage_counter
*const *bp
= b_
;
83 const struct coverage_counter
*a
= *ap
;
84 const struct coverage_counter
*b
= *bp
;
85 if (a
->count
!= b
->count
) {
86 return a
->count
< b
->count
? 1 : -1;
88 return strcmp(a
->name
, b
->name
);
95 struct coverage_counter
**c
;
99 /* Sort coverage counters into groups with equal counts. */
100 c
= xmalloc(n_coverage_counters
* sizeof *c
);
101 for (i
= 0; i
< n_coverage_counters
; i
++) {
102 c
[i
] = coverage_counters
[i
];
104 qsort(c
, n_coverage_counters
, sizeof *c
, compare_coverage_counters
);
106 /* Hash the names in each group along with the rank. */
108 for (i
= 0; i
< n_coverage_counters
; ) {
115 hash
= hash_int(i
, hash
);
116 for (j
= i
; j
< n_coverage_counters
; j
++) {
117 if (c
[j
]->count
!= c
[i
]->count
) {
120 hash
= hash_string(c
[j
]->name
, hash
);
127 return hash_int(n_groups
, hash
);
131 coverage_hit(uint32_t hash
)
133 enum { HIT_BITS
= 1024, BITS_PER_WORD
= 32 };
134 static uint32_t hit
[HIT_BITS
/ BITS_PER_WORD
];
135 BUILD_ASSERT_DECL(IS_POW2(HIT_BITS
));
137 static long long int next_clear
= LLONG_MIN
;
139 unsigned int bit_index
= hash
& (HIT_BITS
- 1);
140 unsigned int word_index
= bit_index
/ BITS_PER_WORD
;
141 unsigned int word_mask
= 1u << (bit_index
% BITS_PER_WORD
);
143 /* Expire coverage hash suppression once a day. */
144 if (time_msec() >= next_clear
) {
145 memset(hit
, 0, sizeof hit
);
146 next_clear
= time_msec() + 60 * 60 * 24 * 1000LL;
149 if (hit
[word_index
] & word_mask
) {
152 hit
[word_index
] |= word_mask
;
157 /* Logs the coverage counters, unless a similar set of events has already been
160 * This function logs at log level VLL_INFO. Use care before adjusting this
161 * level, because depending on its configuration, syslogd can write changes
162 * synchronously, which can cause the coverage messages to take several seconds
167 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 3);
169 if (!VLOG_DROP_INFO(&rl
)) {
170 uint32_t hash
= coverage_hash();
171 if (coverage_hit(hash
)) {
172 VLOG_INFO("Skipping details of duplicate event coverage for "
173 "hash=%08"PRIx32
" in epoch %u", hash
, epoch
);
180 coverage_read(&lines
);
181 SVEC_FOR_EACH (i
, line
, &lines
) {
182 VLOG_INFO("%s", line
);
184 svec_destroy(&lines
);
190 coverage_read_counter(struct svec
*lines
, const struct coverage_counter
*c
)
192 svec_add_nocopy(lines
, xasprintf("%-24s %5u / %9llu",
193 c
->name
, c
->count
, c
->count
+ c
->total
));
196 /* Adds coverage counter information to 'lines'. */
198 coverage_read(struct svec
*lines
)
204 hash
= coverage_hash();
207 svec_add_nocopy(lines
, xasprintf("Event coverage (epoch %u/entire run), "
208 "hash=%08"PRIx32
":", epoch
, hash
));
209 for (i
= 0; i
< n_coverage_counters
; i
++) {
210 struct coverage_counter
*c
= coverage_counters
[i
];
212 coverage_read_counter(lines
, c
);
215 for (i
= 0; i
< n_coverage_counters
; i
++) {
216 struct coverage_counter
*c
= coverage_counters
[i
];
219 coverage_read_counter(lines
, c
);
225 svec_add_nocopy(lines
, xasprintf("%zu events never hit", n_never_hit
));
228 /* Advances to the next epoch of coverage, resetting all the counters to 0. */
235 for (i
= 0; i
< n_coverage_counters
; i
++) {
236 struct coverage_counter
*c
= coverage_counters
[i
];
237 c
->total
+= c
->count
;