summaryrefslogtreecommitdiff
path: root/kern/syscnt.c
blob: e0b213d77f74629cbf23b1b4e43f3d20ce4abaaf (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
/*
 * 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/>.
 */

#include <stdio.h>
#include <string.h>

#include <kern/atomic.h>
#include <kern/init.h>
#include <kern/list.h>
#include <kern/mutex.h>
#include <kern/shell.h>
#include <kern/spinlock.h>
#include <kern/syscnt.h>
#include <kern/thread.h>

/*
 * Global list of all registered counters.
 */
static struct list syscnt_list;
static struct mutex syscnt_lock;

#ifdef CONFIG_SHELL

static void
syscnt_shell_info(int argc, char **argv)
{
    char *prefix;

    prefix = (argc >= 2) ? argv[1] : NULL;
    syscnt_info(prefix);
}

static struct shell_cmd syscnt_shell_cmds[] = {
    SHELL_CMD_INITIALIZER("syscnt_info", syscnt_shell_info,
                          "syscnt_info [<prefix>]",
                          "display information about system counters"),
};

static int __init
syscnt_setup_shell(void)
{
    SHELL_REGISTER_CMDS(syscnt_shell_cmds);
    return 0;
}

INIT_OP_DEFINE(syscnt_setup_shell,
               INIT_OP_DEP(shell_setup, true),
               INIT_OP_DEP(syscnt_setup, true));

#endif /* CONFIG_SHELL */

static int __init
syscnt_setup(void)
{
    list_init(&syscnt_list);
    mutex_init(&syscnt_lock);
    return 0;
}

/*
 * Do not make initialization depend on mutex_setup, since mutex
 * modules may use system counters for debugging.
 */
INIT_OP_DEFINE(syscnt_setup,
               INIT_OP_DEP(mutex_bootstrap, true),
               INIT_OP_DEP(spinlock_setup, true));

void __init
syscnt_register(struct syscnt *syscnt, const char *name)
{
#ifndef ATOMIC_HAVE_64B_OPS
    spinlock_init(&syscnt->lock);
#endif
    syscnt->value = 0;
    strlcpy(syscnt->name, name, sizeof(syscnt->name));

    mutex_lock(&syscnt_lock);
    list_insert_tail(&syscnt_list, &syscnt->node);
    mutex_unlock(&syscnt_lock);
}

void
syscnt_info(const char *prefix)
{
    struct syscnt *syscnt;
    size_t length, prefix_length;
    uint64_t value;

    prefix_length = (prefix == NULL) ? 0 : strlen(prefix);

    mutex_lock(&syscnt_lock);

    list_for_each_entry(&syscnt_list, syscnt, node) {
        if (prefix_length != 0) {
            length = strlen(syscnt->name);

            if ((length < prefix_length)
                || (memcmp(syscnt->name, prefix, prefix_length) != 0)) {
                continue;
            }
        }

        value = syscnt_read(syscnt);

        printf("syscnt: %40s %20llu\n", syscnt->name,
               (unsigned long long)value);
    }

    mutex_unlock(&syscnt_lock);
}