xref: /openbsd-src/sys/kern/subr_evcount.c (revision 9b074ffaba6c82e08722cbd87560a0334d22cc9f)
1 /*	$OpenBSD: subr_evcount.c,v 1.9 2010/04/20 22:05:43 tedu 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/proc.h>
34 #include <sys/sysctl.h>
35 
36 static TAILQ_HEAD(,evcount) evcount_list;
37 
38 /*
39  * Standard evcount parents.
40  */
41 struct evcount evcount_intr;
42 
43 void evcount_init(void);
44 
45 void
46 evcount_init(void)
47 {
48 	TAILQ_INIT(&evcount_list);
49 
50 	evcount_attach(&evcount_intr, "intr", NULL, NULL);
51 }
52 
53 
54 void
55 evcount_attach(struct evcount *ec, const char *name, void *data,
56     struct evcount *parent)
57 {
58 	static int nextid = 0;
59 
60 	if (nextid == 0) {
61 		nextid++;		/* start with 1 */
62 		evcount_init();
63 	}
64 
65 	memset(ec, 0, sizeof(*ec));
66 	ec->ec_name = name;
67 	ec->ec_parent = parent;
68 	ec->ec_id = nextid++;
69 	ec->ec_data = data;
70 	TAILQ_INSERT_TAIL(&evcount_list, ec, next);
71 }
72 
73 void
74 evcount_detach(struct evcount *ec)
75 {
76 	TAILQ_REMOVE(&evcount_list, ec, next);
77 }
78 
79 #ifndef	SMALL_KERNEL
80 
81 int
82 evcount_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
83     void *newp, size_t newlen)
84 {
85 	int error = 0, s, nintr, i;
86 	struct evcount *ec;
87 	u_int64_t count;
88 
89 	if (newp != NULL)
90 		return (EPERM);
91 
92 	if (name[0] != KERN_INTRCNT_NUM) {
93 		if (namelen != 2)
94 			return (ENOTDIR);
95 		if (name[1] < 0)
96 			return (EINVAL);
97 		i = name[1];
98 	} else
99 		i = -1;
100 
101 	nintr = 0;
102 	TAILQ_FOREACH(ec, &evcount_list, next) {
103 		if (ec->ec_parent != &evcount_intr)
104 			continue;
105 		if (nintr++ == i)
106 			break;
107 	}
108 
109 	switch (name[0]) {
110 	case KERN_INTRCNT_NUM:
111 		error = sysctl_rdint(oldp, oldlenp, NULL, nintr);
112 		break;
113 	case KERN_INTRCNT_CNT:
114 		if (ec == NULL)
115 			return (ENOENT);
116 		s = splhigh();
117 		count = ec->ec_count;
118 		splx(s);
119 		error = sysctl_rdquad(oldp, oldlenp, NULL, count);
120 		break;
121 	case KERN_INTRCNT_NAME:
122 		if (ec == NULL)
123 			return (ENOENT);
124 		error = sysctl_rdstring(oldp, oldlenp, NULL, ec->ec_name);
125 		break;
126 	case KERN_INTRCNT_VECTOR:
127 		if (ec == NULL || ec->ec_data == NULL)
128 			return (ENOENT);
129 		error = sysctl_rdint(oldp, oldlenp, NULL,
130 		    *((int *)ec->ec_data));
131 		break;
132 	default:
133 		error = EOPNOTSUPP;
134 		break;
135 	}
136 
137 	return (error);
138 }
139 #endif	/* SMALL_KERNEL */
140