ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/page_stats.c
Revision: 1.4
Committed: Fri Mar 7 14:44:38 2003 UTC (21 years, 2 months ago) by pajs
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_3_4, LIBSTATGRAB_0_3_3, LIBSTATGRAB_0_3_2, LIBSTATGRAB_0_3_1, LIBSTATGRAB_0_3
Changes since 1.3: +28 -9 lines
Log Message:
Removed getting of some data we didn't really need or use.
Updated page_stats to work with linux.
Fixed tools.h to compile nicely :)

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 i-scream
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "statgrab.h"
26 #include <time.h>
27 #ifdef SOLARIS
28 #include <kstat.h>
29 #include <sys/sysinfo.h>
30 #include <string.h>
31 #endif
32 #ifdef LINUX
33 #include <stdio.h>
34 #include "tools.h"
35 #endif
36
37
38 static page_stat_t page_stats;
39 static int page_stats_uninit=1;
40
41 page_stat_t *get_page_stats(){
42 #ifdef SOLARIS
43 kstat_ctl_t *kc;
44 kstat_t *ksp;
45 cpu_stat_t cs;
46 #endif
47 #ifdef LINUX
48 FILE *f;
49 char *line_ptr;
50 #endif
51
52 page_stats.pages_pagein=0;
53 page_stats.pages_pageout=0;
54
55 #ifdef SOLARIS
56 if ((kc = kstat_open()) == NULL) {
57 return NULL;
58 }
59 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
60 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue;
61 if (kstat_read(kc, ksp, &cs) == -1) {
62 continue;
63 }
64
65 page_stats.pages_pagein+=(long long)cs.cpu_vminfo.pgpgin;
66 page_stats.pages_pageout+=(long long)cs.cpu_vminfo.pgpgout;
67 }
68
69 page_stats.systime=time(NULL);
70
71 kstat_close(kc);
72 #endif
73 #ifdef LINUX
74 if((f=fopen("/proc/stat", "r"))==NULL){
75 return NULL;
76 }
77 if((line_ptr=f_read_line(f, "page"))==NULL){
78 fclose(f);
79 return NULL;
80 }
81 if((sscanf(line_ptr, "page %lld %lld", &page_stats.pages_pagein, &page_stats.pages_pageout))!=2){
82 return NULL;
83 }
84 page_stats.systime=time(NULL);
85 fclose(f);
86
87 #endif
88
89 return &page_stats;
90 }
91
92 page_stat_t *get_page_stats_diff(){
93 page_stat_t *page_ptr;
94 static page_stat_t page_stats_diff;
95
96 if(page_stats_uninit){
97 page_ptr=get_page_stats();
98 if(page_ptr==NULL){
99 return NULL;
100 }
101 page_stats_uninit=0;
102 return page_ptr;
103 }
104
105 page_stats_diff.pages_pagein=page_stats.pages_pagein;
106 page_stats_diff.pages_pageout=page_stats.pages_pageout;
107 page_stats_diff.systime=page_stats.systime;
108
109 page_ptr=get_page_stats();
110 if(page_ptr==NULL){
111 return NULL;
112 }
113
114 page_stats_diff.pages_pagein=page_stats.pages_pagein-page_stats_diff.pages_pagein;
115 page_stats_diff.pages_pageout=page_stats.pages_pageout-page_stats_diff.pages_pageout;
116 page_stats_diff.systime=page_stats.systime-page_stats_diff.systime;
117
118 return &page_stats_diff;
119 }