ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/memory_stats.c
Revision: 1.7
Committed: Tue May 21 16:21:38 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.6: +2 -2 lines
Log Message:
forgot to change the scanf format to long long

File Contents

# User Rev Content
1 tdb 1.5 /*
2     * i-scream central monitoring system
3     * Copyright (C) 2000-2002 i-scream
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19    
20 pajs 1.1 #include <stdio.h>
21 pajs 1.2 #ifdef LINUX
22 pajs 1.1 #include <string.h>
23 pajs 1.2 #endif
24     #ifdef SOLARIS
25     #include <unistd.h>
26     #include <kstat.h>
27     #endif
28     #ifdef FREEBSD
29     #include <sys/types.h>
30     #include <sys/sysctl.h>
31     #include <unistd.h>
32     #endif
33 tdb 1.3 #include "ukcprog.h"
34 pajs 1.1
35     char *get_memory_stats(){
36 pajs 1.6 long long total=0;
37     long long free=0;
38     long long used=0;
39     long long cache=0;
40 pajs 1.1 char *xml_memory_stats;
41     #ifdef LINUX
42     FILE *f;
43     char *line;
44 pajs 1.7 long long buffers;
45 pajs 1.1 #endif
46 pajs 1.2 #ifdef SOLARIS
47     kstat_ctl_t *kc;
48     kstat_t *ksp;
49     kstat_named_t *kn;
50     u_long totalmem;
51     int pagesize;
52     #endif
53     #ifdef FREEBSD
54     #define TOTAL_MEM_NAME "hw.physmem"
55     #define FREE_MEM_NAME "vm.stats.vm.v_free_count"
56     #define INACTIVE_MEM_NAME "vm.stats.vm.v_inactive_count"
57     #define CACHE_MEM_NAME "vm.stats.vm.v_cache_count"
58     int pagesize;
59     long inactive;
60     size_t size;
61     #endif
62 pajs 1.1
63     #ifdef LINUX
64     if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
65     errf("Failed to open memory stats (%m)");
66     return NULL;
67     }
68     while((line=fpgetline(f)) != NULL){
69     if (((strncmp(line,"Mem:",4)) == 0)) {
70 pajs 1.7 if((sscanf(line, "%*s %lld %lld %lld %*s %lld %lld", &total, &used, &free, &buffers, &cache)) != 5){
71 pajs 1.1 errf("Failed to read memory details (%m)");
72     return NULL;
73     }
74     break;
75     }
76     }
77     if ((fclose(f)) != 0) {
78     errf("Failed to close file (%m)");
79     return NULL;
80     }
81    
82     cache=cache+buffers;
83     used=total-(free+cache);
84    
85     total=(total/1024)/1024;
86     used=(used/1024)/1024;
87     free=(free/1024)/1024;
88     cache=(free/1024)/1024;
89     #endif
90 pajs 1.2 #ifdef SOLARIS
91     if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
92     errf("Failed to get pagesize (%m)");
93     return NULL;
94     }
95    
96     if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
97     errf("Failed to get total memory");
98     return NULL;
99     }
100    
101     if ((kc = kstat_open()) == NULL) {
102     errf("kstat_open failure (%m)");
103     return NULL;
104     }
105     if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){
106     errf("kstat lookup for \"system_pages\" (%m)");
107     return NULL;
108     }
109     if (kstat_read(kc, ksp, 0) == -1) {
110     errf("Failed to read kstat details (%m)");
111     return NULL;
112     }
113     if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
114     errf("Failed to get free memory (%m)");
115     return NULL;
116     }
117 pajs 1.4 if((kstat_close(kc)) != 0){
118     errf("Failed to close kstat control structure (%m)");
119     return NULL;
120     }
121 pajs 1.2 total=((totalmem*pagesize)/1024)/1024;
122     free=(((kn->value.ul)*pagesize)/1024)/1024;
123     used=total-free;
124     #endif
125     #ifdef FREEBSD
126     if ((pagesize=getpagesize()) == -1){
127     errf("Failed to get page size (%m)");
128     return NULL;
129     }
130    
131     if (sysctlbyname(TOTAL_MEM_NAME, NULL, &size, NULL, NULL) < 0){
132     errf("sysctlbyname failed to get total memory (%m)");
133     return NULL;
134     }
135     if (sysctlbyname(TOTAL_MEM_NAME, &total, &size, NULL, NULL) < 0){
136     errf("Failed to get total memory stats (%m)");
137     return NULL;
138     }
139    
140     if (sysctlbyname(FREE_MEM_NAME, NULL, &size, NULL, NULL) < 0){
141     errf("sysctlbyname failed to get free memory (%m)");
142     return NULL;
143     }
144     if (sysctlbyname(FREE_MEM_NAME, &free, &size, NULL, NULL) < 0){
145     errf("Failed to get free memory stats (%m)");
146     return NULL;
147     }
148    
149     if (sysctlbyname(INACTIVE_MEM_NAME, NULL, &size, NULL, NULL) < 0){
150     errf("sysctlbyname failed to get inactive memory (%m)");
151     return NULL;
152     }
153     if (sysctlbyname(INACTIVE_MEM_NAME, &inactive, &size, NULL, NULL) < 0){
154     errf("Failed to get inactive memory stats (%m)");
155     return NULL;
156     }
157    
158     if (sysctlbyname(CACHE_MEM_NAME, NULL, &size, NULL, NULL) < 0){
159     errf("sysctlbyname failed to get cache memory (%m)");
160     return NULL;
161     }
162     if (sysctlbyname(CACHE_MEM_NAME, &cache, &size, NULL, NULL) < 0){
163     errf("Failed to get cache memory stats (%m)");
164     return NULL;
165     }
166    
167     total/=(1024*1024);
168     free=((free*pagesize)/(1024*1024))+((inactive*pagesize)/(1024*1024));
169     cache=((cache*pagesize)/(1024*1024));
170    
171     /* used should also be cache+active+wired but its less effort this way :) */
172     used=total-free;
173    
174     #endif
175    
176 pajs 1.1
177     if((xml_memory_stats=strf("<memory><total>%ld</total><free>%ld</free><used>%ld</used><cache>%ld</cache></memory>", total, free, used, cache)) == NULL){
178     errf("strf failed (%m)");
179     return NULL;
180     }
181 pajs 1.2 return xml_memory_stats;
182 pajs 1.1 }