1 /*- 2 * Copyright (c) 2003 Poul-Henning Kamp 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 names of the authors may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/types.h> 33 #include <sys/devicestat.h> 34 #include <sys/mman.h> 35 #include <sys/time.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <paths.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <libgeom.h> 43 44 /************************************************************/ 45 static uint npages, spp; 46 static int pagesize, statsfd = -1; 47 static u_char *statp; 48 49 void 50 geom_stats_close(void) 51 { 52 if (statsfd == -1) 53 return; 54 munmap(statp, npages *pagesize); 55 statp = NULL; 56 close (statsfd); 57 statsfd = -1; 58 } 59 60 void 61 geom_stats_resync(void) 62 { 63 void *p; 64 65 if (statsfd == -1) 66 return; 67 for (;;) { 68 p = mmap(statp, (npages + 1) * pagesize, 69 PROT_READ, MAP_SHARED, statsfd, 0); 70 if (p == MAP_FAILED) 71 break; 72 else 73 statp = p; 74 npages++; 75 } 76 } 77 78 int 79 geom_stats_open(void) 80 { 81 int error; 82 void *p; 83 84 if (statsfd != -1) 85 return (EBUSY); 86 statsfd = open(_PATH_DEV DEVSTAT_DEVICE_NAME, O_RDONLY); 87 if (statsfd < 0) 88 return (errno); 89 pagesize = getpagesize(); 90 spp = pagesize / sizeof(struct devstat); 91 p = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, statsfd, 0); 92 if (p == MAP_FAILED) { 93 error = errno; 94 close(statsfd); 95 statsfd = -1; 96 errno = error; 97 return (error); 98 } 99 statp = p; 100 npages = 1; 101 geom_stats_resync(); 102 return (0); 103 } 104 105 struct snapshot { 106 u_char *ptr; 107 uint pages; 108 uint pagesize; 109 uint perpage; 110 struct timespec time; 111 /* used by getnext: */ 112 uint u, v; 113 }; 114 115 void * 116 geom_stats_snapshot_get(void) 117 { 118 struct snapshot *sp; 119 120 sp = malloc(sizeof *sp); 121 if (sp == NULL) 122 return (NULL); 123 memset(sp, 0, sizeof *sp); 124 sp->ptr = malloc(pagesize * npages); 125 if (sp->ptr == NULL) { 126 free(sp); 127 return (NULL); 128 } 129 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */ 130 clock_gettime(CLOCK_REALTIME, &sp->time); 131 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */ 132 memcpy(sp->ptr, statp, pagesize * npages); 133 sp->pages = npages; 134 sp->perpage = spp; 135 sp->pagesize = pagesize; 136 return (sp); 137 } 138 139 void 140 geom_stats_snapshot_free(void *arg) 141 { 142 struct snapshot *sp; 143 144 sp = arg; 145 free(sp->ptr); 146 free(sp); 147 } 148 149 void 150 geom_stats_snapshot_timestamp(void *arg, struct timespec *tp) 151 { 152 struct snapshot *sp; 153 154 sp = arg; 155 *tp = sp->time; 156 } 157 158 void 159 geom_stats_snapshot_reset(void *arg) 160 { 161 struct snapshot *sp; 162 163 sp = arg; 164 sp->u = sp->v = 0; 165 } 166 167 struct devstat * 168 geom_stats_snapshot_next(void *arg) 169 { 170 struct devstat *gsp; 171 struct snapshot *sp; 172 173 sp = arg; 174 gsp = (struct devstat *) 175 (sp->ptr + sp->u * pagesize + sp->v * sizeof *gsp); 176 if (++sp->v >= sp->perpage) { 177 if (++sp->u >= sp->pages) 178 return (NULL); 179 else 180 sp->v = 0; 181 } 182 return (gsp); 183 } 184