xref: /freebsd/sys/kern/subr_devstat.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
1 /*
2  * Copyright (c) 1997, 1998 Kenneth D. Merry.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. 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 BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	$Id: subr_devstat.c,v 1.3 1998/10/06 04:16:07 ken Exp $
29  */
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 
38 #include <sys/devicestat.h>
39 
40 static int devstat_num_devs;
41 static long devstat_generation;
42 static int devstat_version = DEVSTAT_VERSION;
43 static int devstat_current_devnumber;
44 
45 STAILQ_HEAD(devstatlist, devstat) device_statq;
46 
47 /*
48  * Take a malloced and zeroed devstat structure given to us, fill it in
49  * and add it to the queue of devices.
50  */
51 void
52 devstat_add_entry(struct devstat *ds, char *dev_name,
53 		  int unit_number, u_int32_t block_size,
54 		  devstat_support_flags flags,
55 		  devstat_type_flags device_type)
56 {
57 	int s;
58 	struct devstatlist *devstat_head;
59 
60 	if (ds == NULL)
61 		return;
62 
63 	if (devstat_num_devs == 0)
64 		STAILQ_INIT(&device_statq);
65 
66 	devstat_generation++;
67 	devstat_num_devs++;
68 
69 	devstat_head = &device_statq;
70 
71 	STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
72 
73 	ds->device_number = devstat_current_devnumber++;
74 	ds->unit_number = unit_number;
75 	strncpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
76 	ds->block_size = block_size;
77 	ds->flags = flags;
78 	ds->device_type = device_type;
79 
80 	s = splclock();
81 	getmicrotime(&ds->dev_creation_time);
82 	splx(s);
83 }
84 
85 /*
86  * Remove a devstat structure from the list of devices.
87  */
88 void
89 devstat_remove_entry(struct devstat *ds)
90 {
91 	struct devstatlist *devstat_head;
92 
93 	if (ds == NULL)
94 		return;
95 
96 	devstat_generation++;
97 	devstat_num_devs--;
98 
99 	devstat_head = &device_statq;
100 
101 	/* Remove this entry from the devstat queue */
102 	STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
103 }
104 
105 /*
106  * Record a transaction start.
107  */
108 void
109 devstat_start_transaction(struct devstat *ds)
110 {
111 	int s;
112 
113 	/* sanity check */
114 	if (ds == NULL)
115 		return;
116 
117 	/*
118 	 * We only want to set the start time when we are going from idle
119 	 * to busy.  The start time is really the start of the latest busy
120 	 * period.
121 	 */
122 	if (ds->busy_count == 0) {
123 		s = splclock();
124 		getmicrouptime(&ds->start_time);
125 		splx(s);
126 	}
127 	ds->busy_count++;
128 }
129 
130 /*
131  * Record the ending of a transaction, and incrment the various counters.
132  */
133 void
134 devstat_end_transaction(struct devstat *ds, u_int32_t bytes,
135 			devstat_tag_type tag_type, devstat_trans_flags flags)
136 {
137 	int s;
138 	struct timeval busy_time;
139 
140 	/* sanity check */
141 	if (ds == NULL)
142 		return;
143 
144 	s = splclock();
145 	getmicrouptime(&ds->last_comp_time);
146 	splx(s);
147 
148 	ds->busy_count--;
149 
150 	/*
151 	 * There might be some transactions (DEVSTAT_NO_DATA) that don't
152 	 * transfer any data.
153 	 */
154 	if (flags == DEVSTAT_READ) {
155 		ds->bytes_read += bytes;
156 		ds->num_reads++;
157 	} else if (flags == DEVSTAT_WRITE) {
158 		ds->bytes_written += bytes;
159 		ds->num_writes++;
160 	} else
161 		ds->num_other++;
162 
163 	/*
164 	 * Keep a count of the various tag types sent.
165 	 */
166 	if (tag_type != DEVSTAT_TAG_NONE)
167 		ds->tag_types[tag_type]++;
168 
169 	/*
170 	 * We only update the busy time when we go idle.  Otherwise, this
171 	 * calculation would require many more clock cycles.
172 	 */
173 	if (ds->busy_count == 0) {
174 		/* Calculate how long we were busy */
175 		busy_time = ds->last_comp_time;
176 		timevalsub(&busy_time, &ds->start_time);
177 
178 		/* Add our busy time to the total busy time. */
179 		timevaladd(&ds->busy_time, &busy_time);
180 	}
181 	/*
182 	 * XXX KDM this is temporarily disabled to avoid causing
183 	 * unsophisticated users to panic.  There are unfixed bugs in the
184 	 * wd driver that will set off this error message.
185 	 */
186 #if 0
187 	else if (ds->busy_count < 0)
188 		printf("devstat_end_transaction: HELP!! busy_count "
189 		       "for %s%d is < 0 (%d)!\n", ds->device_name,
190 		       ds->unit_number, ds->busy_count);
191 #endif
192 }
193 
194 /*
195  * This is the sysctl handler for the devstat package.  The data pushed out
196  * on the kern.devstat.all sysctl variable consists of the current devstat
197  * generation number, and then an array of devstat structures, one for each
198  * device in the system.
199  *
200  * I'm really not too fond of this method of doing things, but there really
201  * aren't that many alternatives.  We must have some method of making sure
202  * that the generation number the user gets corresponds with the data the
203  * user gets.  If the user makes a separate sysctl call to get the
204  * generation, and then a sysctl call to get the device statistics, the
205  * device list could have changed in that brief period of time.  By
206  * supplying the generation number along with the statistics output, we can
207  * guarantee that the generation number and the statistics match up.
208  */
209 static int
210 sysctl_devstat SYSCTL_HANDLER_ARGS
211 {
212 	int error, i;
213 	struct devstat *nds;
214 	struct devstatlist *devstat_head;
215 
216 	if (devstat_num_devs == 0)
217 		return(EINVAL);
218 
219 	error = 0;
220 	devstat_head = &device_statq;
221 
222 	/*
223 	 * First push out the generation number.
224 	 */
225 	error = SYSCTL_OUT(req, &devstat_generation, sizeof(long));
226 
227 	/*
228 	 * Now push out all the devices.
229 	 */
230 	for (i = 0, nds = devstat_head->stqh_first;
231 	    (nds != NULL) && (i < devstat_num_devs) && (error == 0);
232 	     nds = nds->dev_links.stqe_next, i++)
233 		error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
234 
235 	return(error);
236 }
237 
238 /*
239  * Sysctl entries for devstat.  The first one is a node that all the rest
240  * hang off of.
241  */
242 SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, 0, "Device Statistics");
243 
244 SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE,
245 	    0, 0, sysctl_devstat, "S,devstat", "All Devices");
246 /*
247  * Export the number of devices in the system so that userland utilities
248  * can determine how much memory to allocate to hold all the devices.
249  */
250 SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD, &devstat_num_devs,
251 	  0, "Number of devices in the devstat list");
252 SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
253 	    &devstat_generation, 0, "Devstat list generation");
254 SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD, &devstat_version,
255 	  0, "Devstat list version number");
256