]> git.proxmox.com Git - mirror_ovs.git/blame - lib/coverage.h
cfm: Notify connectivity_seq on cfm_set_fault
[mirror_ovs.git] / lib / coverage.h
CommitLineData
064af421 1/*
857165b5 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
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
857165b5 30#include "ovs-thread.h"
064af421 31#include "vlog.h"
5521e08e 32#include "compiler.h"
064af421 33
98cf638b
AW
34/* Makes coverage_run run every 5000 ms (5 seconds).
35 * If this value is redefined, the new value must
36 * divide 60000 (1 minute). */
37#define COVERAGE_RUN_INTERVAL 5000
38BUILD_ASSERT_DECL(60000 % COVERAGE_RUN_INTERVAL == 0);
39
bc0d88ee
JS
40#define COVERAGE_CLEAR_INTERVAL 1000
41BUILD_ASSERT_DECL(COVERAGE_RUN_INTERVAL % COVERAGE_CLEAR_INTERVAL == 0);
42
98cf638b
AW
43/* Defines the moving average array length. */
44#define MIN_AVG_LEN (60000/COVERAGE_RUN_INTERVAL)
45#define HR_AVG_LEN 60
46
064af421
BP
47/* A coverage counter. */
48struct coverage_counter {
857165b5
BP
49 const char *const name; /* Textual name. */
50 unsigned int (*const count)(void); /* Gets, zeros this thread's count. */
51 unsigned long long int total; /* Total count. */
98cf638b
AW
52 unsigned long long int last_total;
53 /* The moving average arrays. */
54 unsigned int min[MIN_AVG_LEN];
55 unsigned int hr[HR_AVG_LEN];
064af421
BP
56};
57
5521e08e
HS
58void coverage_counter_register(struct coverage_counter*);
59
d76f09ea
BP
60/* Defines COUNTER. There must be exactly one such definition at file scope
61 * within a program. */
d76f09ea 62#define COVERAGE_DEFINE(COUNTER) \
857165b5
BP
63 DEFINE_STATIC_PER_THREAD_DATA(unsigned int, \
64 counter_##COUNTER, 0); \
65 static unsigned int COUNTER##_count(void) \
66 { \
67 unsigned int *countp = counter_##COUNTER##_get(); \
68 unsigned int count = *countp; \
69 *countp = 0; \
70 return count; \
71 } \
72 static inline void COUNTER##_add(unsigned int n) \
73 { \
74 *counter_##COUNTER##_get() += n; \
75 } \
76 extern struct coverage_counter counter_##COUNTER; \
77 struct coverage_counter counter_##COUNTER \
98cf638b 78 = { #COUNTER, COUNTER##_count, 0, 0, {0}, {0} }; \
5521e08e
HS
79 OVS_CONSTRUCTOR(COUNTER##_init) { \
80 coverage_counter_register(&counter_##COUNTER); \
81 }
d76f09ea
BP
82
83/* Adds 1 to COUNTER. */
857165b5 84#define COVERAGE_INC(COUNTER) COVERAGE_ADD(COUNTER, 1)
d76f09ea
BP
85
86/* Adds AMOUNT to COUNTER. */
857165b5 87#define COVERAGE_ADD(COUNTER, AMOUNT) COUNTER##_add(AMOUNT)
064af421 88
f5c6854a 89void coverage_init(void);
a5f607bc 90void coverage_log(void);
064af421 91void coverage_clear(void);
98cf638b 92void coverage_run(void);
064af421 93
d76f09ea
BP
94/* Implementation detail. */
95#define COVERAGE_DEFINE__(COUNTER) \
d76f09ea 96
064af421 97#endif /* coverage.h */