2 * Copyright (c) 2009, 2010, 2011, 2012 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"
28 VLOG_DEFINE_THIS_MODULE(coverage
);
30 /* The coverage counters. */
31 #if USE_LINKER_SECTIONS
32 extern struct coverage_counter
*__start_coverage
[];
33 extern struct coverage_counter
*__stop_coverage
[];
34 #define coverage_counters __start_coverage
35 #define n_coverage_counters (__stop_coverage - __start_coverage)
36 #else /* !USE_LINKER_SECTIONS */
37 #define COVERAGE_COUNTER(NAME) COVERAGE_DEFINE__(NAME);
38 #include "coverage.def"
39 #undef COVERAGE_COUNTER
41 struct coverage_counter
*coverage_counters
[] = {
42 #define COVERAGE_COUNTER(NAME) &counter_##NAME,
43 #include "coverage.def"
44 #undef COVERAGE_COUNTER
46 #define n_coverage_counters ARRAY_SIZE(coverage_counters)
47 #endif /* !USE_LINKER_SECTIONS */
49 static unsigned int epoch
;
52 coverage_unixctl_log(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
53 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
55 coverage_log(VLL_WARN
, false);
56 unixctl_command_reply(conn
, NULL
);
62 unixctl_command_register("coverage/log", "", 0, 0,
63 coverage_unixctl_log
, NULL
);
66 /* Sorts coverage counters in descending order by count, within equal counts
67 * alphabetically by name. */
69 compare_coverage_counters(const void *a_
, const void *b_
)
71 const struct coverage_counter
*const *ap
= a_
;
72 const struct coverage_counter
*const *bp
= b_
;
73 const struct coverage_counter
*a
= *ap
;
74 const struct coverage_counter
*b
= *bp
;
75 if (a
->count
!= b
->count
) {
76 return a
->count
< b
->count
? 1 : -1;
78 return strcmp(a
->name
, b
->name
);
85 struct coverage_counter
**c
;
89 /* Sort coverage counters into groups with equal counts. */
90 c
= xmalloc(n_coverage_counters
* sizeof *c
);
91 for (i
= 0; i
< n_coverage_counters
; i
++) {
92 c
[i
] = coverage_counters
[i
];
94 qsort(c
, n_coverage_counters
, sizeof *c
, compare_coverage_counters
);
96 /* Hash the names in each group along with the rank. */
98 for (i
= 0; i
< n_coverage_counters
; ) {
105 hash
= hash_int(i
, hash
);
106 for (j
= i
; j
< n_coverage_counters
; j
++) {
107 if (c
[j
]->count
!= c
[i
]->count
) {
110 hash
= hash_string(c
[j
]->name
, hash
);
117 return hash_int(n_groups
, hash
);
121 coverage_hit(uint32_t hash
)
123 enum { HIT_BITS
= 1024, BITS_PER_WORD
= 32 };
124 static uint32_t hit
[HIT_BITS
/ BITS_PER_WORD
];
125 BUILD_ASSERT_DECL(IS_POW2(HIT_BITS
));
127 static long long int next_clear
= LLONG_MIN
;
129 unsigned int bit_index
= hash
& (HIT_BITS
- 1);
130 unsigned int word_index
= bit_index
/ BITS_PER_WORD
;
131 unsigned int word_mask
= 1u << (bit_index
% BITS_PER_WORD
);
133 /* Expire coverage hash suppression once a day. */
134 if (time_msec() >= next_clear
) {
135 memset(hit
, 0, sizeof hit
);
136 next_clear
= time_msec() + 60 * 60 * 24 * 1000LL;
139 if (hit
[word_index
] & word_mask
) {
142 hit
[word_index
] |= word_mask
;
148 coverage_log_counter(enum vlog_level level
, const struct coverage_counter
*c
)
150 VLOG(level
, "%-24s %5u / %9llu", c
->name
, c
->count
, c
->count
+ c
->total
);
153 /* Logs the coverage counters at the given vlog 'level'. If
154 * 'suppress_dups' is true, then duplicate events are not displayed.
155 * Care should be taken in the value used for 'level'. Depending on the
156 * configuration, syslog can write changes synchronously, which can
157 * cause the coverage messages to take several seconds to write. */
159 coverage_log(enum vlog_level level
, bool suppress_dups
)
165 if (!vlog_is_enabled(THIS_MODULE
, level
)) {
169 hash
= coverage_hash();
171 if (coverage_hit(hash
)) {
172 VLOG(level
, "Skipping details of duplicate event coverage for "
173 "hash=%08"PRIx32
" in epoch %u", hash
, epoch
);
179 VLOG(level
, "Event coverage (epoch %u/entire run), hash=%08"PRIx32
":",
181 for (i
= 0; i
< n_coverage_counters
; i
++) {
182 struct coverage_counter
*c
= coverage_counters
[i
];
184 coverage_log_counter(level
, c
);
187 for (i
= 0; i
< n_coverage_counters
; i
++) {
188 struct coverage_counter
*c
= coverage_counters
[i
];
191 coverage_log_counter(level
, c
);
197 VLOG(level
, "%zu events never hit", n_never_hit
);
200 /* Advances to the next epoch of coverage, resetting all the counters to 0. */
207 for (i
= 0; i
< n_coverage_counters
; i
++) {
208 struct coverage_counter
*c
= coverage_counters
[i
];
209 c
->total
+= c
->count
;