xref: /openbsd-src/sys/kern/subr_evcount.c (revision 756b05efc19cfbb1c7a85881c4468342b662366a)
1 /*	$OpenBSD: subr_evcount.c,v 1.5 2004/11/26 21:23:06 miod Exp $ */
2 /*
3  * Copyright (c) 2004 Artur Grabowski <art@openbsd.org>
4  * Copyright (c) 2004 Aaron Campbell <aaron@openbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/evcount.h>
30 #include <sys/timeout.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/sysctl.h>
34 
35 static TAILQ_HEAD(,evcount) evcount_list;
36 static struct evcount *evcount_next_sync;
37 
38 /*
39  * Standard evcount parents.
40  */
41 struct evcount evcount_intr;
42 
43 void evcount_timeout(void *);
44 void evcount_init(void);
45 
46 void
47 evcount_init(void)
48 {
49 	TAILQ_INIT(&evcount_list);
50 
51 	evcount_attach(&evcount_intr, "intr", NULL, NULL);
52 }
53 
54 
55 void
56 evcount_attach(ec, name, data, parent)
57 	struct evcount *ec;
58 	const char *name;
59 	void *data;
60 	struct evcount *parent;
61 {
62 	static int nextid = 0;
63 
64 	if (nextid == 0) {
65 		nextid++;		/* start with 1 */
66 		evcount_init();
67 	}
68 
69 	memset(ec, 0, sizeof(*ec));
70 	ec->ec_name = name;
71 	ec->ec_parent = parent;
72 	ec->ec_id = nextid++;
73 	ec->ec_data = data;
74 	TAILQ_INSERT_TAIL(&evcount_list, ec, next);
75 }
76 
77 void
78 evcount_detach(ec)
79 	struct evcount *ec;
80 {
81 	if (evcount_next_sync == ec)
82 		evcount_next_sync = TAILQ_NEXT(ec, next);
83 
84 	TAILQ_REMOVE(&evcount_list, ec, next);
85 }
86 
87 int
88 evcount_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
89 	int *name;
90 	u_int namelen;
91 	void *oldp;
92 	size_t *oldlenp;
93 	void *newp;
94 	size_t newlen;
95 {
96 	int error = 0, s, nintr, i;
97 	struct evcount *ec;
98 	u_int64_t count;
99 
100 	if (newp != NULL)
101 		return (EPERM);
102 
103 	if (name[0] != KERN_INTRCNT_NUM) {
104 		if (namelen != 2)
105 			return (ENOTDIR);
106 		if (name[1] < 0)
107 			return (EINVAL);
108 		i = name[1];
109 	} else
110 		i = -1;
111 
112 	nintr = 0;
113 	TAILQ_FOREACH(ec, &evcount_list, next) {
114 		if (ec->ec_parent != &evcount_intr)
115 			continue;
116 		if (nintr++ == i)
117 			break;
118 	}
119 
120 	switch (name[0]) {
121 	case KERN_INTRCNT_NUM:
122 		error = sysctl_rdint(oldp, oldlenp, NULL, nintr);
123 		break;
124 	case KERN_INTRCNT_CNT:
125 		if (ec == NULL)
126 			return (ENOENT);
127 		s = splhigh();
128 		count = ec->ec_count;
129 		splx(s);
130 		error = sysctl_rdquad(oldp, oldlenp, NULL, count);
131 		break;
132 	case KERN_INTRCNT_NAME:
133 		if (ec == NULL)
134 			return (ENOENT);
135 		error = sysctl_rdstring(oldp, oldlenp, NULL, ec->ec_name);
136 		break;
137 	case KERN_INTRCNT_VECTOR:
138 		if (ec == NULL || ec->ec_data == NULL)
139 			return (ENOENT);
140 		error = sysctl_rdint(oldp, oldlenp, NULL,
141 		    *((int *)ec->ec_data));
142 		break;
143 	default:
144 		error = EOPNOTSUPP;
145 		break;
146 	}
147 
148 	return (error);
149 }
150