diff --git a/lib/libgeom/Makefile b/lib/libgeom/Makefile new file mode 100644 index 000000000000..041976c81f07 --- /dev/null +++ b/lib/libgeom/Makefile @@ -0,0 +1,11 @@ +# $FreeBSD$ + +LIB= geom +SRCS= geom_stats.c +INCS= libgeom.h + +WARNS?= 4 + +MAN= libgeom.3 + +.include diff --git a/lib/libgeom/geom_stats.c b/lib/libgeom/geom_stats.c new file mode 100644 index 000000000000..f4ada934459d --- /dev/null +++ b/lib/libgeom/geom_stats.c @@ -0,0 +1,190 @@ +/*- + * Copyright (c) 2003 Poul-Henning Kamp + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The names of the authors may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#if 0 +#include +#include +#include +#endif + +/************************************************************/ +static uint npages, pagesize, spp; +static int statsfd = -1; +static u_char *statp; + +void +geom_stats_close(void) +{ + if (statsfd == -1) + return; + munmap(statp, npages *pagesize); + statp = NULL; + close (statsfd); + statsfd = -1; +} + +void +geom_stats_resync(void) +{ + void *p; + + if (statsfd == -1) + return; + for (;;) { + p = mmap(statp, (npages + 1) * pagesize, + PROT_READ, 0, statsfd, 0); + if (p == MAP_FAILED) + break; + else + statp = p; + npages++; + } +} + +int +geom_stats_open(void) +{ + int error; + void *p; + + if (statsfd != -1) + return (EBUSY); + statsfd = open(_PATH_DEV GEOM_STATS_DEVICE, O_RDONLY); + if (statsfd < 0) + return (errno); + pagesize = getpagesize(); + spp = pagesize / sizeof(struct g_stat); + p = mmap(NULL, pagesize, PROT_READ, 0, statsfd, 0); + if (p == MAP_FAILED) { + error = errno; + close(statsfd); + statsfd = -1; + errno = error; + return (error); + } + statp = p; + npages = 1; + geom_stats_resync(); + return (0); +} + +struct snapshot { + u_char *ptr; + uint pages; + uint pagesize; + uint perpage; + struct timespec time; + /* used by getnext: */ + uint u, v; +}; + +void * +geom_stats_snapshot_get(void) +{ + struct snapshot *sp; + + sp = malloc(sizeof *sp); + if (sp == NULL) + return (NULL); + memset(sp, 0, sizeof *sp); + sp->ptr = malloc(pagesize * npages); + if (sp->ptr == NULL) { + free(sp); + return (NULL); + } + memset(sp->ptr, 0, pagesize * npages); /* page in, cache */ + clock_gettime(CLOCK_REALTIME, &sp->time); + memset(sp->ptr, 0, pagesize * npages); /* page in, cache */ + memcpy(sp->ptr, statp, pagesize * npages); + sp->pages = npages; + sp->perpage = spp; + sp->pagesize = pagesize; + return (sp); +} + +void +geom_stats_snapshot_free(void *arg) +{ + struct snapshot *sp; + + sp = arg; + free(sp->ptr); + free(sp); +} + +void +geom_stats_snapshot_timestamp(void *arg, struct timespec *tp) +{ + struct snapshot *sp; + + sp = arg; + *tp = sp->time; +} + +void +geom_stats_snapshot_reset(void *arg) +{ + struct snapshot *sp; + + sp = arg; + sp->u = sp->v = 0; +} + +struct g_stat * +geom_stats_snapshot_next(void *arg) +{ + struct g_stat *gsp; + struct snapshot *sp; + + sp = arg; + gsp = (struct g_stat *) + (sp->ptr + sp->u * pagesize + sp->v * sizeof *gsp); + if (++sp->v >= sp->perpage) { + if (++sp->u >= sp->pages) + return (NULL); + else + sp->v = 0; + } + return (gsp); +} diff --git a/lib/libgeom/libgeom.3 b/lib/libgeom/libgeom.3 new file mode 100644 index 000000000000..f22a9636fff3 --- /dev/null +++ b/lib/libgeom/libgeom.3 @@ -0,0 +1,116 @@ +.\" Copyright (c) 2003 Poul-Henning Kamp +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. The names of the authors may not be used to endorse or promote +.\" products derived from this software without specific prior written +.\" permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd February 8, 2003 +.Dt LIBGEOM 3 +.Os +.Sh NAME +.Nm geom_stats_open , +.Nm geom_stats_close , +.Nm geom_stats_resync , +.Nm geom_stats_snapshot_get , +.Nm geom_stats_snapshot_free , +.Nm geom_stats_snapshot_timestamp , +.Nm geom_stats_snapshot_reset , +.Nm geom_stats_snapshot_next +.Sh LIBRARY +.Lb geom +.Sh SYNOPSIS +.In libgeom.h +.Ss "Statistics functions" +.Ft void +.Fn geom_stats_close void +.Ft int +.Fn geom_stats_open void +.Ft void +.Fn geom_stats_resync void +.Ft void * +.Fn geom_stats_snapshot_get void +.Ft void +.Fn geom_stats_snapshot_free "void *arg" +.Ft void +.Fn geom_stats_snapshot_timestamp "void *arg" "struct timespec *tp" +.Ft void +.Fn geom_stats_snapshot_reset "void *arg" +.Ft struct g_stat * +.Fn geom_stats_snapshot_next "void *arg" +.Sh DESCRIPTION +.Nm Libgeom +is the library which contains the official and publicized API for +interacting with the GEOM subsystem in the kernel. +.Ss "Statistics functions" +GEOM collects statistics data for all consumers and providers, but does +not perform any normalization or presentation on the raw data, this is +left as an excercize for user-land presentation utilities. +.Pp +The +.Nm geom_stats_open +and +.Nm geom_stats_close +functions opens and closes the necessary pathways to access the raw +statistics information in the kernel. These functions are likely to +open one or more files and cache the filedescriptors locally. +.Nm geom_stats_open +returns zero on success, and sets errno if not. +.Pp +The +.Nm geom_stats_resync +function will check if more statistics collection points have been +added in the kernel since +.Nm geom_stats_open +or the previous call to +.Nm geom_stats_resync . +.Pp +.Nm geom_stats_snapshot_get +will aquire a snapshot of the raw data from the kernel and while a +reasonable effort is made to make this snapshot as atomic and consistent +as possible, no guarantee is given that it will actually be so. +The snapshot must be freed again using the +.Nm geom_stats_snapshot_free +function. +.Nm geom_stats_snapshot_get +returns NULL on failure. +.Pp +.Nm geom_stats_snapshot_timestamp +provides access to the timestamp aquired in the snapshot. +.Pp +.Nm geom_stats_snapshot_reset +and +.Nm geom_stats_snapshot_next +provides an iterator over the statistics slots in the snapshot. +.Nm geom_stats_snapshot_reset +forces the internal pointer in the snapshot back to before the first item. +.Nm geom_stats_snapshot_next +returns the next item and NULL if there are no more items in the snapshot. +.Sh AUTHORS +.An Poul-Henning Kamp Aq phk@FreeBSD.org +.Sh HISTORY +.Nm geom +library appeard in +.Fx 5.1 . diff --git a/lib/libgeom/libgeom.h b/lib/libgeom/libgeom.h new file mode 100644 index 000000000000..e9a9f28737ed --- /dev/null +++ b/lib/libgeom/libgeom.h @@ -0,0 +1,45 @@ +/*- + * Copyright (c) 2003 Poul-Henning Kamp + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The names of the authors may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ +#ifndef _LIBGEOM_H_ +#define _LIBGEOM_H_ + +#include + +void geom_stats_close(void); +void geom_stats_resync(void); +int geom_stats_open(void); +void *geom_stats_snapshot_get(void); +void geom_stats_snapshot_free(void *arg); +void geom_stats_snapshot_timestamp(void *arg, struct timespec *tp); +void geom_stats_snapshot_reset(void *arg); +struct g_stat *geom_stats_snapshot_next(void *arg); + +#endif /* _LIBGEOM_H_ */