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.
20 /* This file implements a simple form of coverage instrumentation. Points in
21 * source code that are of interest must be explicitly annotated with
22 * COVERAGE_INC. The coverage counters may be logged at any time with
25 * This form of coverage instrumentation is intended to be so lightweight that
26 * it can be enabled in production builds. It is obviously not a substitute
27 * for traditional coverage instrumentation with e.g. "gcov", but it is still
28 * a useful debugging tool. */
30 #include "ovs-thread.h"
33 /* A coverage counter. */
34 struct coverage_counter {
35 const char *const name; /* Textual name. */
36 unsigned int (*const count)(void); /* Gets, zeros this thread's count. */
37 unsigned long long int total; /* Total count. */
40 /* Defines COUNTER. There must be exactly one such definition at file scope
41 * within a program. */
42 #if USE_LINKER_SECTIONS
43 #define COVERAGE_DEFINE(COUNTER) \
44 DEFINE_STATIC_PER_THREAD_DATA(unsigned int, \
45 counter_##COUNTER, 0); \
46 static unsigned int COUNTER##_count(void) \
48 unsigned int *countp = counter_##COUNTER##_get(); \
49 unsigned int count = *countp; \
53 static inline void COUNTER##_add(unsigned int n) \
55 *counter_##COUNTER##_get() += n; \
57 extern struct coverage_counter counter_##COUNTER; \
58 struct coverage_counter counter_##COUNTER \
59 = { #COUNTER, COUNTER##_count, 0 }; \
60 extern struct coverage_counter *counter_ptr_##COUNTER; \
61 struct coverage_counter *counter_ptr_##COUNTER \
62 __attribute__((section("coverage"))) = &counter_##COUNTER
64 #define COVERAGE_DEFINE(COUNTER) \
65 DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, \
67 static inline void COUNTER##_add(unsigned int n) \
69 *counter_##COUNTER##_get() += n; \
71 extern struct coverage_counter counter_##COUNTER
74 /* Adds 1 to COUNTER. */
75 #define COVERAGE_INC(COUNTER) COVERAGE_ADD(COUNTER, 1)
77 /* Adds AMOUNT to COUNTER. */
78 #define COVERAGE_ADD(COUNTER, AMOUNT) COUNTER##_add(AMOUNT)
80 void coverage_init(void);
81 void coverage_log(void);
82 void coverage_clear(void);
84 /* Implementation detail. */
85 #define COVERAGE_DEFINE__(COUNTER) \
87 #endif /* coverage.h */