--- projects/libstatgrab/src/libstatgrab/page_stats.c 2003/10/24 17:26:43 1.10 +++ projects/libstatgrab/src/libstatgrab/page_stats.c 2004/04/07 21:08:40 1.21 @@ -1,21 +1,24 @@ -/* - * i-scream central monitoring system +/* + * i-scream libstatgrab * http://www.i-scream.org - * Copyright (C) 2000-2003 i-scream + * Copyright (C) 2000-2004 i-scream * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + * + * $Id: page_stats.c,v 1.21 2004/04/07 21:08:40 tdb Exp $ */ #ifdef HAVE_CONFIG_H @@ -30,48 +33,53 @@ #include #include #endif -#ifdef LINUX +#if defined(LINUX) || defined(CYGWIN) #include #include #endif -#ifdef FREEBSD +#if defined(FREEBSD) || defined(DFBSD) #include #include #endif +#if defined(NETBSD) || defined(OPENBSD) +#include +#include +#endif -static page_stat_t page_stats; +static sg_page_stats page_stats; static int page_stats_uninit=1; -page_stat_t *get_page_stats(){ +sg_page_stats *sg_get_page_stats(){ #ifdef SOLARIS - kstat_ctl_t *kc; - kstat_t *ksp; - cpu_stat_t cs; + kstat_ctl_t *kc; + kstat_t *ksp; + cpu_stat_t cs; #endif -#ifdef LINUX +#if defined(LINUX) || defined(CYGWIN) FILE *f; char *line_ptr; #endif -#ifdef FREEBSD +#if defined(FREEBSD) || defined(DFBSD) size_t size; #endif -#ifdef NETBSD +#if defined(NETBSD) || defined(OPENBSD) struct uvmexp *uvm; #endif page_stats.systime = time(NULL); - page_stats.pages_pagein=0; - page_stats.pages_pageout=0; + page_stats.pages_pagein=0; + page_stats.pages_pageout=0; #ifdef SOLARIS - if ((kc = kstat_open()) == NULL) { - return NULL; - } - for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { - if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; - if (kstat_read(kc, ksp, &cs) == -1) { - continue; - } + if ((kc = kstat_open()) == NULL) { + sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); + return NULL; + } + for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { + if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; + if (kstat_read(kc, ksp, &cs) == -1) { + continue; + } page_stats.pages_pagein+=(long long)cs.cpu_vminfo.pgpgin; page_stats.pages_pageout+=(long long)cs.cpu_vminfo.pgpgout; @@ -79,9 +87,9 @@ page_stat_t *get_page_stats(){ kstat_close(kc); #endif -#ifdef LINUX +#if defined(LINUX) || defined(CYGWIN) if ((f = fopen("/proc/vmstat", "r")) != NULL) { - while ((line_ptr = f_read_line(f, "")) != NULL) { + while ((line_ptr = sg_f_read_line(f, "")) != NULL) { long long value; if (sscanf(line_ptr, "%*s %lld", &value) != 1) { @@ -97,34 +105,41 @@ page_stat_t *get_page_stats(){ fclose(f); } else if ((f = fopen("/proc/stat", "r")) != NULL) { - if ((line_ptr = f_read_line(f, "page")) == NULL) { + if ((line_ptr = sg_f_read_line(f, "page")) == NULL) { + sg_set_error(SG_ERROR_PARSE, "page"); fclose(f); return NULL; } if (sscanf(line_ptr, "page %lld %lld", &page_stats.pages_pagein, &page_stats.pages_pageout) != 2) { + sg_set_error(SG_ERROR_PARSE, "page"); + fclose(f); return NULL; } fclose(f); } else { + sg_set_error(SG_ERROR_OPEN, "/proc/stat"); return NULL; } #endif -#ifdef FREEBSD +#if defined(FREEBSD) || defined(DFBSD) size = sizeof page_stats.pages_pagein; - if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){ - return NULL; - } + if (sysctlbyname("vm.stats.vm.v_swappgsin", &page_stats.pages_pagein, &size, NULL, 0) < 0){ + sg_set_error(SG_ERROR_SYSCTLBYNAME, "vm.stats.vm.v_swappgsin"); + return NULL; + } size = sizeof page_stats.pages_pageout; - if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){ - return NULL; - } + if (sysctlbyname("vm.stats.vm.v_swappgsout", &page_stats.pages_pageout, &size, NULL, 0) < 0){ + sg_set_error(SG_ERROR_SYSCTLBYNAME, "vm.stats.vm.v_swappgsout"); + return NULL; + } #endif -#ifdef NETBSD - if ((uvm = get_uvmexp()) == NULL) { +#if defined(NETBSD) || defined(OPENBSD) + if ((uvm = sg_get_uvmexp()) == NULL) { return NULL; } + page_stats.pages_pagein = uvm->pgswapin; page_stats.pages_pageout = uvm->pgswapout; #endif @@ -132,12 +147,12 @@ page_stat_t *get_page_stats(){ return &page_stats; } -page_stat_t *get_page_stats_diff(){ - page_stat_t *page_ptr; - static page_stat_t page_stats_diff; +sg_page_stats *sg_get_page_stats_diff(){ + sg_page_stats *page_ptr; + static sg_page_stats page_stats_diff; if(page_stats_uninit){ - page_ptr=get_page_stats(); + page_ptr=sg_get_page_stats(); if(page_ptr==NULL){ return NULL; } @@ -149,14 +164,14 @@ page_stat_t *get_page_stats_diff(){ page_stats_diff.pages_pageout=page_stats.pages_pageout; page_stats_diff.systime=page_stats.systime; - page_ptr=get_page_stats(); + page_ptr=sg_get_page_stats(); if(page_ptr==NULL){ return NULL; } - page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein; - page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout; - page_stats_diff.systime=page_stats.systime-page_stats_diff.systime; + page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein; + page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout; + page_stats_diff.systime=page_stats.systime-page_stats_diff.systime; return &page_stats_diff; }