xref: /freebsd/sys/kern/subr_devstat.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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.10 1999/04/11 02:27:06 eivind 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 static 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, const char *dev_name,
53 		  int unit_number, u_int32_t block_size,
54 		  devstat_support_flags flags,
55 		  devstat_type_flags device_type,
56 		  devstat_priority priority)
57 {
58 	int s;
59 	struct devstatlist *devstat_head;
60 	struct devstat *ds_tmp;
61 
62 	if (ds == NULL)
63 		return;
64 
65 	if (devstat_num_devs == 0)
66 		STAILQ_INIT(&device_statq);
67 
68 	devstat_generation++;
69 	devstat_num_devs++;
70 
71 	devstat_head = &device_statq;
72 
73 	/*
74 	 * Priority sort.  Each driver passes in its priority when it adds
75 	 * its devstat entry.  Drivers are sorted first by priority, and
76 	 * then by probe order.
77 	 *
78 	 * For the first device, we just insert it, since the priority
79 	 * doesn't really matter yet.  Subsequent devices are inserted into
80 	 * the list using the order outlined above.
81 	 */
82 	if (devstat_num_devs == 1)
83 		STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
84 	else {
85 		for (ds_tmp = STAILQ_FIRST(devstat_head); ds_tmp != NULL;
86 		     ds_tmp = STAILQ_NEXT(ds_tmp, dev_links)) {
87 			struct devstat *ds_next;
88 
89 			ds_next = STAILQ_NEXT(ds_tmp, dev_links);
90 
91 			/*
92 			 * If we find a break between higher and lower
93 			 * priority items, and if this item fits in the
94 			 * break, insert it.  This also applies if the
95 			 * "lower priority item" is the end of the list.
96 			 */
97 			if ((priority <= ds_tmp->priority)
98 			 && ((ds_next == NULL)
99 			   || (priority > ds_next->priority))) {
100 				STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
101 						    dev_links);
102 				break;
103 			} else if (priority > ds_tmp->priority) {
104 				/*
105 				 * If this is the case, we should be able
106 				 * to insert ourselves at the head of the
107 				 * list.  If we can't, something is wrong.
108 				 */
109 				if (ds_tmp == STAILQ_FIRST(devstat_head)) {
110 					STAILQ_INSERT_HEAD(devstat_head,
111 							   ds, dev_links);
112 					break;
113 				} else {
114 					STAILQ_INSERT_TAIL(devstat_head,
115 							   ds, dev_links);
116 					printf("devstat_add_entry: HELP! "
117 					       "sorting problem detected "
118 					       "for %s%d\n", dev_name,
119 					       unit_number);
120 					break;
121 				}
122 			}
123 		}
124 	}
125 
126 	ds->device_number = devstat_current_devnumber++;
127 	ds->unit_number = unit_number;
128 	strncpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
129 	ds->device_name[DEVSTAT_NAME_LEN - 1] = '\0';
130 	ds->block_size = block_size;
131 	ds->flags = flags;
132 	ds->device_type = device_type;
133 	ds->priority = priority;
134 
135 	s = splclock();
136 	getmicrotime(&ds->dev_creation_time);
137 	splx(s);
138 }
139 
140 /*
141  * Remove a devstat structure from the list of devices.
142  */
143 void
144 devstat_remove_entry(struct devstat *ds)
145 {
146 	struct devstatlist *devstat_head;
147 
148 	if (ds == NULL)
149 		return;
150 
151 	devstat_generation++;
152 	devstat_num_devs--;
153 
154 	devstat_head = &device_statq;
155 
156 	/* Remove this entry from the devstat queue */
157 	STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
158 }
159 
160 /*
161  * Record a transaction start.
162  */
163 void
164 devstat_start_transaction(struct devstat *ds)
165 {
166 	int s;
167 
168 	/* sanity check */
169 	if (ds == NULL)
170 		return;
171 
172 	/*
173 	 * We only want to set the start time when we are going from idle
174 	 * to busy.  The start time is really the start of the latest busy
175 	 * period.
176 	 */
177 	if (ds->busy_count == 0) {
178 		s = splclock();
179 		getmicrouptime(&ds->start_time);
180 		splx(s);
181 	}
182 	ds->busy_count++;
183 }
184 
185 /*
186  * Record the ending of a transaction, and incrment the various counters.
187  */
188 void
189 devstat_end_transaction(struct devstat *ds, u_int32_t bytes,
190 			devstat_tag_type tag_type, devstat_trans_flags flags)
191 {
192 	int s;
193 	struct timeval busy_time;
194 
195 	/* sanity check */
196 	if (ds == NULL)
197 		return;
198 
199 	s = splclock();
200 	getmicrouptime(&ds->last_comp_time);
201 	splx(s);
202 
203 	ds->busy_count--;
204 
205 	/*
206 	 * There might be some transactions (DEVSTAT_NO_DATA) that don't
207 	 * transfer any data.
208 	 */
209 	if (flags == DEVSTAT_READ) {
210 		ds->bytes_read += bytes;
211 		ds->num_reads++;
212 	} else if (flags == DEVSTAT_WRITE) {
213 		ds->bytes_written += bytes;
214 		ds->num_writes++;
215 	} else
216 		ds->num_other++;
217 
218 	/*
219 	 * Keep a count of the various tag types sent.
220 	 */
221 	if (tag_type != DEVSTAT_TAG_NONE)
222 		ds->tag_types[tag_type]++;
223 
224 	/*
225 	 * We only update the busy time when we go idle.  Otherwise, this
226 	 * calculation would require many more clock cycles.
227 	 */
228 	if (ds->busy_count == 0) {
229 		/* Calculate how long we were busy */
230 		busy_time = ds->last_comp_time;
231 		timevalsub(&busy_time, &ds->start_time);
232 
233 		/* Add our busy time to the total busy time. */
234 		timevaladd(&ds->busy_time, &busy_time);
235 	} else if (ds->busy_count < 0)
236 		printf("devstat_end_transaction: HELP!! busy_count "
237 		       "for %s%d is < 0 (%d)!\n", ds->device_name,
238 		       ds->unit_number, ds->busy_count);
239 }
240 
241 /*
242  * This is the sysctl handler for the devstat package.  The data pushed out
243  * on the kern.devstat.all sysctl variable consists of the current devstat
244  * generation number, and then an array of devstat structures, one for each
245  * device in the system.
246  *
247  * I'm really not too fond of this method of doing things, but there really
248  * aren't that many alternatives.  We must have some method of making sure
249  * that the generation number the user gets corresponds with the data the
250  * user gets.  If the user makes a separate sysctl call to get the
251  * generation, and then a sysctl call to get the device statistics, the
252  * device list could have changed in that brief period of time.  By
253  * supplying the generation number along with the statistics output, we can
254  * guarantee that the generation number and the statistics match up.
255  */
256 static int
257 sysctl_devstat SYSCTL_HANDLER_ARGS
258 {
259 	int error, i;
260 	struct devstat *nds;
261 	struct devstatlist *devstat_head;
262 
263 	if (devstat_num_devs == 0)
264 		return(EINVAL);
265 
266 	error = 0;
267 	devstat_head = &device_statq;
268 
269 	/*
270 	 * First push out the generation number.
271 	 */
272 	error = SYSCTL_OUT(req, &devstat_generation, sizeof(long));
273 
274 	/*
275 	 * Now push out all the devices.
276 	 */
277 	for (i = 0, nds = devstat_head->stqh_first;
278 	    (nds != NULL) && (i < devstat_num_devs) && (error == 0);
279 	     nds = nds->dev_links.stqe_next, i++)
280 		error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
281 
282 	return(error);
283 }
284 
285 /*
286  * Sysctl entries for devstat.  The first one is a node that all the rest
287  * hang off of.
288  */
289 SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, 0, "Device Statistics");
290 
291 SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE,
292     0, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list");
293 /*
294  * Export the number of devices in the system so that userland utilities
295  * can determine how much memory to allocate to hold all the devices.
296  */
297 SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
298     &devstat_num_devs, 0, "Number of devices in the devstat list");
299 SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
300     &devstat_generation, "Devstat list generation");
301 SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
302     &devstat_version, 0, "Devstat list version number");
303