geom_stats.c revision 110545
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: head/lib/libgeom/geom_stats.c 110545 2003-02-08 15:15:56Z phk $ 30 */ 31 32#include <paths.h> 33#include <errno.h> 34#include <fcntl.h> 35#include <stdlib.h> 36#include <string.h> 37#include <unistd.h> 38#include <libgeom.h> 39 40#include <sys/mman.h> 41#include <sys/time.h> 42#include <sys/types.h> 43 44#include <geom/geom_stats.h> 45#if 0 46#include <stdio.h> 47#include <stdint.h> 48#include <err.h> 49#endif 50 51/************************************************************/ 52static uint npages, pagesize, spp; 53static int statsfd = -1; 54static u_char *statp; 55 56void 57geom_stats_close(void) 58{ 59 if (statsfd == -1) 60 return; 61 munmap(statp, npages *pagesize); 62 statp = NULL; 63 close (statsfd); 64 statsfd = -1; 65} 66 67void 68geom_stats_resync(void) 69{ 70 void *p; 71 72 if (statsfd == -1) 73 return; 74 for (;;) { 75 p = mmap(statp, (npages + 1) * pagesize, 76 PROT_READ, 0, statsfd, 0); 77 if (p == MAP_FAILED) 78 break; 79 else 80 statp = p; 81 npages++; 82 } 83} 84 85int 86geom_stats_open(void) 87{ 88 int error; 89 void *p; 90 91 if (statsfd != -1) 92 return (EBUSY); 93 statsfd = open(_PATH_DEV GEOM_STATS_DEVICE, O_RDONLY); 94 if (statsfd < 0) 95 return (errno); 96 pagesize = getpagesize(); 97 spp = pagesize / sizeof(struct g_stat); 98 p = mmap(NULL, pagesize, PROT_READ, 0, statsfd, 0); 99 if (p == MAP_FAILED) { 100 error = errno; 101 close(statsfd); 102 statsfd = -1; 103 errno = error; 104 return (error); 105 } 106 statp = p; 107 npages = 1; 108 geom_stats_resync(); 109 return (0); 110} 111 112struct snapshot { 113 u_char *ptr; 114 uint pages; 115 uint pagesize; 116 uint perpage; 117 struct timespec time; 118 /* used by getnext: */ 119 uint u, v; 120}; 121 122void * 123geom_stats_snapshot_get(void) 124{ 125 struct snapshot *sp; 126 127 sp = malloc(sizeof *sp); 128 if (sp == NULL) 129 return (NULL); 130 memset(sp, 0, sizeof *sp); 131 sp->ptr = malloc(pagesize * npages); 132 if (sp->ptr == NULL) { 133 free(sp); 134 return (NULL); 135 } 136 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */ 137 clock_gettime(CLOCK_REALTIME, &sp->time); 138 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */ 139 memcpy(sp->ptr, statp, pagesize * npages); 140 sp->pages = npages; 141 sp->perpage = spp; 142 sp->pagesize = pagesize; 143 return (sp); 144} 145 146void 147geom_stats_snapshot_free(void *arg) 148{ 149 struct snapshot *sp; 150 151 sp = arg; 152 free(sp->ptr); 153 free(sp); 154} 155 156void 157geom_stats_snapshot_timestamp(void *arg, struct timespec *tp) 158{ 159 struct snapshot *sp; 160 161 sp = arg; 162 *tp = sp->time; 163} 164 165void 166geom_stats_snapshot_reset(void *arg) 167{ 168 struct snapshot *sp; 169 170 sp = arg; 171 sp->u = sp->v = 0; 172} 173 174struct g_stat * 175geom_stats_snapshot_next(void *arg) 176{ 177 struct g_stat *gsp; 178 struct snapshot *sp; 179 180 sp = arg; 181 gsp = (struct g_stat *) 182 (sp->ptr + sp->u * pagesize + sp->v * sizeof *gsp); 183 if (++sp->v >= sp->perpage) { 184 if (++sp->u >= sp->pages) 185 return (NULL); 186 else 187 sp->v = 0; 188 } 189 return (gsp); 190} 191