Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / coverage.h
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 "vlog.h"
31
32 /* A coverage counter. */
33 struct coverage_counter {
34     const char *name;           /* Textual name. */
35     unsigned int count;         /* Count within the current epoch. */
36     unsigned long long int total; /* Total count over all epochs. */
37 };
38
39 /* Increments the counter with the given NAME.  Coverage counters need not be
40  * declared explicitly, but when you add the first coverage counter to a given
41  * file, you must also add that file to COVERAGE_FILES in lib/automake.mk. */
42 #define COVERAGE_INC(NAME)                              \
43     do {                                                \
44         extern struct coverage_counter NAME##_count;    \
45         NAME##_count.count++;                           \
46     } while (0)
47
48 /* Adds AMOUNT to the coverage counter with the given NAME. */
49 #define COVERAGE_ADD(NAME, AMOUNT)                      \
50     do {                                                \
51         extern struct coverage_counter NAME##_count;    \
52         NAME##_count.count += AMOUNT;                   \
53     } while (0)
54
55 void coverage_log(enum vlog_level);
56 void coverage_clear(void);
57
58 #endif /* coverage.h */