coverage: Reimplement the "ovs-appctl coverage/show" command.
[sliver-openvswitch.git] / lib / coverage.h
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 #ifndef COVERAGE_H
18 #define COVERAGE_H 1
19
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
23  * coverage_log().
24  *
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. */
29
30 #include "ovs-thread.h"
31 #include "vlog.h"
32
33 /* Makes coverage_run run every 5000 ms (5 seconds).
34  * If this value is redefined, the new value must
35  * divide 60000 (1 minute). */
36 #define COVERAGE_RUN_INTERVAL    5000
37 BUILD_ASSERT_DECL(60000 % COVERAGE_RUN_INTERVAL == 0);
38
39 /* Defines the moving average array length. */
40 #define MIN_AVG_LEN (60000/COVERAGE_RUN_INTERVAL)
41 #define HR_AVG_LEN  60
42
43 /* A coverage counter. */
44 struct coverage_counter {
45     const char *const name;            /* Textual name. */
46     unsigned int (*const count)(void); /* Gets, zeros this thread's count. */
47     unsigned long long int total;      /* Total count. */
48     unsigned long long int last_total;
49     /* The moving average arrays. */
50     unsigned int min[MIN_AVG_LEN];
51     unsigned int hr[HR_AVG_LEN];
52 };
53
54 /* Defines COUNTER.  There must be exactly one such definition at file scope
55  * within a program. */
56 #if USE_LINKER_SECTIONS
57 #define COVERAGE_DEFINE(COUNTER)                                        \
58         DEFINE_STATIC_PER_THREAD_DATA(unsigned int,                     \
59                                       counter_##COUNTER, 0);            \
60         static unsigned int COUNTER##_count(void)                       \
61         {                                                               \
62             unsigned int *countp = counter_##COUNTER##_get();           \
63             unsigned int count = *countp;                               \
64             *countp = 0;                                                \
65             return count;                                               \
66         }                                                               \
67         static inline void COUNTER##_add(unsigned int n)                \
68         {                                                               \
69             *counter_##COUNTER##_get() += n;                            \
70         }                                                               \
71         extern struct coverage_counter counter_##COUNTER;               \
72         struct coverage_counter counter_##COUNTER                       \
73             = { #COUNTER, COUNTER##_count, 0, 0, {0}, {0} };            \
74         extern struct coverage_counter *counter_ptr_##COUNTER;          \
75         struct coverage_counter *counter_ptr_##COUNTER                  \
76             __attribute__((section("coverage"))) = &counter_##COUNTER
77 #else
78 #define COVERAGE_DEFINE(COUNTER)                                        \
79         DECLARE_EXTERN_PER_THREAD_DATA(unsigned int,                    \
80                                        counter_##COUNTER);              \
81         static inline void COUNTER##_add(unsigned int n)                \
82         {                                                               \
83             *counter_##COUNTER##_get() += n;                            \
84         }                                                               \
85         extern struct coverage_counter counter_##COUNTER
86 #endif
87
88 /* Adds 1 to COUNTER. */
89 #define COVERAGE_INC(COUNTER) COVERAGE_ADD(COUNTER, 1)
90
91 /* Adds AMOUNT to COUNTER. */
92 #define COVERAGE_ADD(COUNTER, AMOUNT) COUNTER##_add(AMOUNT)
93
94 void coverage_init(void);
95 void coverage_log(void);
96 void coverage_clear(void);
97 void coverage_run(void);
98
99 /* Implementation detail. */
100 #define COVERAGE_DEFINE__(COUNTER)                              \
101
102 #endif /* coverage.h */