ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/memory_stats.c
Revision: 1.5
Committed: Wed Mar 5 21:00:59 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.4: +36 -1 lines
Log Message:
Updated Memory stats to work with linux.

File Contents

# User Rev Content
1 pajs 1.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     #ifdef SOLARIS
27     #include <unistd.h>
28     #include <kstat.h>
29     #endif
30 pajs 1.5 #ifdef LINUX
31     #include <stdio.h>
32     #include <string.h>
33     #include "tools.h"
34     #endif
35 pajs 1.1
36     mem_stat_t *get_memory_stats(){
37    
38     static mem_stat_t mem_stat;
39    
40     #ifdef SOLARIS
41     kstat_ctl_t *kc;
42     kstat_t *ksp;
43     kstat_named_t *kn;
44     long totalmem;
45     int pagesize;
46     #endif
47 pajs 1.5 #ifdef LINUX
48     char *line_ptr;
49     FILE *f;
50     #endif
51 pajs 1.1
52     #ifdef SOLARIS
53     if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
54     return NULL;
55     }
56    
57     if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
58     return NULL;
59     }
60    
61     if ((kc = kstat_open()) == NULL) {
62     return NULL;
63     }
64     if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){
65     return NULL;
66     }
67     if (kstat_read(kc, ksp, 0) == -1) {
68     return NULL;
69     }
70     if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
71     return NULL;
72     }
73 pajs 1.2 kstat_close(kc);
74    
75 pajs 1.1 mem_stat.total = (long long)totalmem * (long long)pagesize;
76     mem_stat.free = ((long long)kn->value.ul) * (long long)pagesize;
77     mem_stat.used = mem_stat.total - mem_stat.free;
78 pajs 1.5 #endif
79    
80     #ifdef LINUX
81     f=fopen("/proc/meminfo", "r");
82     if(f==NULL){
83     return NULL;
84     }
85    
86     if((line_ptr=f_read_line(f, "Mem:"))==NULL){
87     fclose(f);
88     return NULL;
89     }
90    
91     fclose(f);
92    
93     /* Linux actually stores this as a unsigned long long, but
94     * our structures are just long longs. This shouldn't be a
95     * problem for sometime yet :)
96     */
97     if((sscanf(line_ptr,"Mem: %lld %lld %lld %*d %*d %lld", \
98     &mem_stat.total, \
99     &mem_stat.used, \
100     &mem_stat.free, \
101     &mem_stat.cache))!=4){
102     return NULL;
103     }
104    
105 pajs 1.1 #endif
106    
107     return &mem_stat;
108    
109     }