summaryrefslogtreecommitdiff
path: root/kern/evcnt.h
blob: f585fad0584a46e82d40a6451c3590adf8b0feef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2014 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Generic event counters.
 */

#ifndef _KERN_EVCNT_H
#define _KERN_EVCNT_H

#include <kern/list.h>

/*
 * Size of the buffer storing an event counter name.
 */
#define EVCNT_NAME_SIZE 32

/*
 * Event counter structure.
 *
 * Event counters are guaranteed to be 64-bits wide.
 */
struct evcnt {
    unsigned long long count;
    struct list node;
    char name[EVCNT_NAME_SIZE];
};

/*
 * Initialize the evcnt module.
 *
 * This module is initialized by architecture-specific code. It is normally
 * safe to call this function very early at boot time.
 */
void evcnt_setup(void);

/*
 * Register the given counter.
 */
void evcnt_register(struct evcnt *evcnt, const char *name);

/*
 * Increment the given counter.
 *
 * It is the responsibility of the caller to synchronize access to the
 * counter.
 */
static inline void
evcnt_inc(struct evcnt *evcnt)
{
    evcnt->count++;
}

/*
 * Batched increment.
 *
 * It is the responsibility of the caller to synchronize access to the
 * counter.
 */
static inline void
evcnt_add(struct evcnt *evcnt, unsigned long long delta)
{
    evcnt->count += delta;
}

/*
 * Obtain the current value of the given counter.
 *
 * Since counters are 64-bits wide, retrieving them on 32-bits systems might
 * return invalid values, although this should be very rare. As long as users
 * don't rely on them for critical operations, this is completely harmless.
 */
static inline unsigned long long
evcnt_read(const struct evcnt *evcnt)
{
    return evcnt->count;
}

/*
 * Display the registered event counters.
 *
 * A pattern can be used to filter the output. The result will only include
 * counters for which the beginning of their name matches the pattern.
 * If NULL, all counters are reported.
 */
void evcnt_info(const char *pattern);

#endif /* _KERN_EVCNT_H */