xref: /openbsd-src/sys/kern/subr_evcount.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: subr_evcount.c,v 1.10 2010/09/20 06:33:46 matthew 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 = TAILQ_HEAD_INITIALIZER(evcount_list);
37 
38 void
39 evcount_attach(struct evcount *ec, const char *name, void *data)
40 {
41 	static int nextid = 0;
42 
43 	memset(ec, 0, sizeof(*ec));
44 	ec->ec_name = name;
45 	ec->ec_id = ++nextid;
46 	ec->ec_data = data;
47 	TAILQ_INSERT_TAIL(&evcount_list, ec, next);
48 }
49 
50 void
51 evcount_detach(struct evcount *ec)
52 {
53 	TAILQ_REMOVE(&evcount_list, ec, next);
54 }
55 
56 #ifndef	SMALL_KERNEL
57 
58 int
59 evcount_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
60     void *newp, size_t newlen)
61 {
62 	int error = 0, s, nintr, i;
63 	struct evcount *ec;
64 	u_int64_t count;
65 
66 	if (newp != NULL)
67 		return (EPERM);
68 
69 	if (name[0] != KERN_INTRCNT_NUM) {
70 		if (namelen != 2)
71 			return (ENOTDIR);
72 		if (name[1] < 0)
73 			return (EINVAL);
74 		i = name[1];
75 	} else
76 		i = -1;
77 
78 	nintr = 0;
79 	TAILQ_FOREACH(ec, &evcount_list, next) {
80 		if (nintr++ == i)
81 			break;
82 	}
83 
84 	switch (name[0]) {
85 	case KERN_INTRCNT_NUM:
86 		error = sysctl_rdint(oldp, oldlenp, NULL, nintr);
87 		break;
88 	case KERN_INTRCNT_CNT:
89 		if (ec == NULL)
90 			return (ENOENT);
91 		s = splhigh();
92 		count = ec->ec_count;
93 		splx(s);
94 		error = sysctl_rdquad(oldp, oldlenp, NULL, count);
95 		break;
96 	case KERN_INTRCNT_NAME:
97 		if (ec == NULL)
98 			return (ENOENT);
99 		error = sysctl_rdstring(oldp, oldlenp, NULL, ec->ec_name);
100 		break;
101 	case KERN_INTRCNT_VECTOR:
102 		if (ec == NULL || ec->ec_data == NULL)
103 			return (ENOENT);
104 		error = sysctl_rdint(oldp, oldlenp, NULL,
105 		    *((int *)ec->ec_data));
106 		break;
107 	default:
108 		error = EOPNOTSUPP;
109 		break;
110 	}
111 
112 	return (error);
113 }
114 #endif	/* SMALL_KERNEL */
115