1*bf0d449cSmpi /* $OpenBSD: subr_evcount.c,v 1.16 2023/09/16 09:33:27 mpi Exp $ */
2d5765d4fSaaron /*
3d5765d4fSaaron * Copyright (c) 2004 Artur Grabowski <art@openbsd.org>
4d5765d4fSaaron * Copyright (c) 2004 Aaron Campbell <aaron@openbsd.org>
5d5765d4fSaaron * All rights reserved.
6d5765d4fSaaron *
7d5765d4fSaaron * Redistribution and use in source and binary forms, with or without
8d5765d4fSaaron * modification, are permitted provided that the following conditions
9d5765d4fSaaron * are met:
10d5765d4fSaaron *
11d5765d4fSaaron * 1. Redistributions of source code must retain the above copyright
12d5765d4fSaaron * notice, this list of conditions and the following disclaimer.
13d5765d4fSaaron * 2. The name of the author may not be used to endorse or promote products
14d5765d4fSaaron * derived from this software without specific prior written permission.
15d5765d4fSaaron *
16d5765d4fSaaron * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17d5765d4fSaaron * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18d5765d4fSaaron * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19d5765d4fSaaron * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20d5765d4fSaaron * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21d5765d4fSaaron * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22d5765d4fSaaron * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23d5765d4fSaaron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24d5765d4fSaaron * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25d5765d4fSaaron * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26d5765d4fSaaron */
27d5765d4fSaaron
28d5765d4fSaaron #include <sys/param.h>
29d5765d4fSaaron #include <sys/evcount.h>
30d5765d4fSaaron #include <sys/systm.h>
31d5765d4fSaaron #include <sys/sysctl.h>
32886cb583Sjmatthew #include <sys/percpu.h>
33d5765d4fSaaron
344667bebbSmatthew static TAILQ_HEAD(,evcount) evcount_list = TAILQ_HEAD_INITIALIZER(evcount_list);
35886cb583Sjmatthew static TAILQ_HEAD(,evcount) evcount_percpu_init_list =
36886cb583Sjmatthew TAILQ_HEAD_INITIALIZER(evcount_percpu_init_list);
37886cb583Sjmatthew static int evcount_percpu_done;
38d5765d4fSaaron
39d5765d4fSaaron void
evcount_attach(struct evcount * ec,const char * name,void * data)404667bebbSmatthew evcount_attach(struct evcount *ec, const char *name, void *data)
41d5765d4fSaaron {
42d5765d4fSaaron static int nextid = 0;
43d5765d4fSaaron
44d5765d4fSaaron memset(ec, 0, sizeof(*ec));
45d5765d4fSaaron ec->ec_name = name;
464667bebbSmatthew ec->ec_id = ++nextid;
47d5765d4fSaaron ec->ec_data = data;
48d5765d4fSaaron TAILQ_INSERT_TAIL(&evcount_list, ec, next);
49d5765d4fSaaron }
50d5765d4fSaaron
51d5765d4fSaaron void
evcount_percpu(struct evcount * ec)52886cb583Sjmatthew evcount_percpu(struct evcount *ec)
53886cb583Sjmatthew {
54886cb583Sjmatthew if (evcount_percpu_done == 0) {
55886cb583Sjmatthew TAILQ_REMOVE(&evcount_list, ec, next);
56886cb583Sjmatthew TAILQ_INSERT_TAIL(&evcount_percpu_init_list, ec, next);
57886cb583Sjmatthew } else {
58886cb583Sjmatthew ec->ec_percpu = counters_alloc(1);
59886cb583Sjmatthew }
60886cb583Sjmatthew }
61886cb583Sjmatthew
62886cb583Sjmatthew void
evcount_init_percpu(void)63886cb583Sjmatthew evcount_init_percpu(void)
64886cb583Sjmatthew {
65886cb583Sjmatthew struct evcount *ec;
66886cb583Sjmatthew
67886cb583Sjmatthew KASSERT(evcount_percpu_done == 0);
68886cb583Sjmatthew TAILQ_FOREACH(ec, &evcount_percpu_init_list, next) {
69886cb583Sjmatthew ec->ec_percpu = counters_alloc(1);
70886cb583Sjmatthew counters_add(ec->ec_percpu, 0, ec->ec_count);
71886cb583Sjmatthew ec->ec_count = 0;
72886cb583Sjmatthew }
73886cb583Sjmatthew TAILQ_CONCAT(&evcount_list, &evcount_percpu_init_list, next);
74886cb583Sjmatthew evcount_percpu_done = 1;
75886cb583Sjmatthew }
76886cb583Sjmatthew
77886cb583Sjmatthew void
evcount_detach(struct evcount * ec)78bf083c78Sjsg evcount_detach(struct evcount *ec)
79d5765d4fSaaron {
80d5765d4fSaaron TAILQ_REMOVE(&evcount_list, ec, next);
81886cb583Sjmatthew if (ec->ec_percpu != NULL) {
82886cb583Sjmatthew counters_free(ec->ec_percpu, 1);
83886cb583Sjmatthew ec->ec_percpu = NULL;
84886cb583Sjmatthew }
85886cb583Sjmatthew }
86886cb583Sjmatthew
87886cb583Sjmatthew void
evcount_inc(struct evcount * ec)88886cb583Sjmatthew evcount_inc(struct evcount *ec)
89886cb583Sjmatthew {
90886cb583Sjmatthew if (ec->ec_percpu != NULL)
91886cb583Sjmatthew counters_inc(ec->ec_percpu, 0);
92886cb583Sjmatthew else
93886cb583Sjmatthew ec->ec_count++;
94d5765d4fSaaron }
95d5765d4fSaaron
969e2e8501Smiod #ifndef SMALL_KERNEL
979e2e8501Smiod
98d5765d4fSaaron int
evcount_sysctl(int * name,u_int namelen,void * oldp,size_t * oldlenp,void * newp,size_t newlen)99bf083c78Sjsg evcount_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
100bf083c78Sjsg void *newp, size_t newlen)
101d5765d4fSaaron {
1025f058a8cSderaadt int error = 0, s, nintr, i;
103d5765d4fSaaron struct evcount *ec;
104*bf0d449cSmpi uint64_t count, scratch;
105d5765d4fSaaron
106d5765d4fSaaron if (newp != NULL)
107d5765d4fSaaron return (EPERM);
108d5765d4fSaaron
109d5765d4fSaaron if (name[0] != KERN_INTRCNT_NUM) {
110d5765d4fSaaron if (namelen != 2)
111d5765d4fSaaron return (ENOTDIR);
112d5765d4fSaaron if (name[1] < 0)
113d5765d4fSaaron return (EINVAL);
114d5765d4fSaaron i = name[1];
115d5765d4fSaaron } else
116d5765d4fSaaron i = -1;
117d5765d4fSaaron
118d5765d4fSaaron nintr = 0;
119d5765d4fSaaron TAILQ_FOREACH(ec, &evcount_list, next) {
120d5765d4fSaaron if (nintr++ == i)
121d5765d4fSaaron break;
122d5765d4fSaaron }
123d5765d4fSaaron
124d5765d4fSaaron switch (name[0]) {
125d5765d4fSaaron case KERN_INTRCNT_NUM:
126d5765d4fSaaron error = sysctl_rdint(oldp, oldlenp, NULL, nintr);
127d5765d4fSaaron break;
128d5765d4fSaaron case KERN_INTRCNT_CNT:
129d5765d4fSaaron if (ec == NULL)
130d5765d4fSaaron return (ENOENT);
131886cb583Sjmatthew if (ec->ec_percpu != NULL) {
132*bf0d449cSmpi counters_read(ec->ec_percpu, &count, 1, &scratch);
133886cb583Sjmatthew } else {
1345f058a8cSderaadt s = splhigh();
1355f058a8cSderaadt count = ec->ec_count;
1365f058a8cSderaadt splx(s);
137886cb583Sjmatthew }
1385d6f4fa7Sderaadt error = sysctl_rdquad(oldp, oldlenp, NULL, count);
139d5765d4fSaaron break;
140d5765d4fSaaron case KERN_INTRCNT_NAME:
141d5765d4fSaaron if (ec == NULL)
142d5765d4fSaaron return (ENOENT);
143d5765d4fSaaron error = sysctl_rdstring(oldp, oldlenp, NULL, ec->ec_name);
144d5765d4fSaaron break;
145d5765d4fSaaron case KERN_INTRCNT_VECTOR:
146d5765d4fSaaron if (ec == NULL || ec->ec_data == NULL)
147d5765d4fSaaron return (ENOENT);
148d5765d4fSaaron error = sysctl_rdint(oldp, oldlenp, NULL,
149d5765d4fSaaron *((int *)ec->ec_data));
150d5765d4fSaaron break;
151d5765d4fSaaron default:
152d5765d4fSaaron error = EOPNOTSUPP;
153d5765d4fSaaron break;
154d5765d4fSaaron }
155d5765d4fSaaron
156d5765d4fSaaron return (error);
157d5765d4fSaaron }
1589e2e8501Smiod #endif /* SMALL_KERNEL */
159