summaryrefslogtreecommitdiff
path: root/kern/syscnt.h
blob: 167be09912ed695883e34e9d078b92c93bcf65b0 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (c) 2014-2017 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/>.
 *
 *
 * System counters.
 *
 * This module provides 64-bits general-purpose counters that can be
 * accessed and modified atomically from any context.
 */

#ifndef KERN_SYSCNT_H
#define KERN_SYSCNT_H

#include <stdint.h>

#include <kern/atomic.h>
#include <kern/init.h>
#include <kern/macros.h>
#include <kern/spinlock.h>

/*
 * Size of the buffer storing a system counter name.
 */
#define SYSCNT_NAME_SIZE 32

#include <kern/syscnt_types.h>

/*
 * System counter.
 */
struct syscnt;

/*
 * Initialize and register the given counter.
 *
 * The counter is set to 0.
 */
void syscnt_register(struct syscnt *syscnt, const char *name);

#ifdef ATOMIC_HAVE_64B_OPS

static inline void
syscnt_set(struct syscnt *syscnt, uint64_t value)
{
    atomic_store(&syscnt->value, value, ATOMIC_RELAXED);
}

static inline void
syscnt_add(struct syscnt *syscnt, int64_t delta)
{
    atomic_add(&syscnt->value, delta, ATOMIC_RELAXED);
}

static inline uint64_t
syscnt_read(const struct syscnt *syscnt)
{
    return atomic_load((uint64_t *)&syscnt->value, ATOMIC_RELAXED);
}

#else /* ATOMIC_HAVE_64B_OPS */

static inline void
syscnt_set(struct syscnt *syscnt, uint64_t value)
{
    unsigned long flags;

    spinlock_lock_intr_save(&syscnt->lock, &flags);
    syscnt->value = value;
    spinlock_unlock_intr_restore(&syscnt->lock, flags);
}

static inline void
syscnt_add(struct syscnt *syscnt, int64_t delta)
{
    unsigned long flags;

    spinlock_lock_intr_save(&syscnt->lock, &flags);
    syscnt->value += delta;
    spinlock_unlock_intr_restore(&syscnt->lock, flags);
}

static inline uint64_t
syscnt_read(struct syscnt *syscnt)
{
    unsigned long flags;
    uint64_t value;

    spinlock_lock_intr_save(&syscnt->lock, &flags);
    value = syscnt->value;
    spinlock_unlock_intr_restore(&syscnt->lock, flags);

    return value;
}

#endif /* ATOMIC_HAVE_64B_OPS */

static inline void
syscnt_inc(struct syscnt *syscnt)
{
    syscnt_add(syscnt, 1);
}

static inline void
syscnt_dec(struct syscnt *syscnt)
{
    syscnt_add(syscnt, -1);
}

/*
 * Display system counters.
 *
 * A prefix can be used to filter the output, where only counters with the
 * given prefix are displayed. If NULL, all counters are reported.
 */
void syscnt_info(const char *prefix);

/*
 * This init operation provides :
 *  - registration of system counters
 *  - module fully initialized
 */
INIT_OP_DECLARE(syscnt_setup);

#endif /* KERN_SYSCNT_H */