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.3
Committed: Thu May 16 15:25:45 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -6 lines
Log Message:
Few tidy ups.

File Contents

# User Rev Content
1 pajs 1.1 #include <stdio.h>
2 pajs 1.2 #ifdef LINUX
3 pajs 1.1 #include <string.h>
4 pajs 1.2 #endif
5     #ifdef SOLARIS
6     #include <unistd.h>
7     #include <kstat.h>
8     #endif
9     #ifdef FREEBSD
10     #include <sys/types.h>
11     #include <sys/sysctl.h>
12     #include <unistd.h>
13     #endif
14 tdb 1.3 #include "ukcprog.h"
15 pajs 1.1
16     char *get_memory_stats(){
17     long total=0;
18     long free=0;
19     long used=0;
20     long cache=0;
21     char *xml_memory_stats;
22     #ifdef LINUX
23     FILE *f;
24     char *line;
25     long buffers;
26     #endif
27 pajs 1.2 #ifdef SOLARIS
28     kstat_ctl_t *kc;
29     kstat_t *ksp;
30     kstat_named_t *kn;
31     u_long totalmem;
32     int pagesize;
33     #endif
34     #ifdef FREEBSD
35     #define TOTAL_MEM_NAME "hw.physmem"
36     #define FREE_MEM_NAME "vm.stats.vm.v_free_count"
37     #define INACTIVE_MEM_NAME "vm.stats.vm.v_inactive_count"
38     #define CACHE_MEM_NAME "vm.stats.vm.v_cache_count"
39     int pagesize;
40     long inactive;
41     size_t size;
42     #endif
43 pajs 1.1
44     #ifdef LINUX
45     if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
46     errf("Failed to open memory stats (%m)");
47     return NULL;
48     }
49     while((line=fpgetline(f)) != NULL){
50     if (((strncmp(line,"Mem:",4)) == 0)) {
51     if((sscanf(line, "%*s %ld %ld %ld %*s %ld %ld", &total, &used, &free, &buffers, &cache)) != 5){
52     errf("Failed to read memory details (%m)");
53     return NULL;
54     }
55     break;
56     }
57     }
58     if ((fclose(f)) != 0) {
59     errf("Failed to close file (%m)");
60     return NULL;
61     }
62    
63     cache=cache+buffers;
64     used=total-(free+cache);
65    
66     total=(total/1024)/1024;
67     used=(used/1024)/1024;
68     free=(free/1024)/1024;
69     cache=(free/1024)/1024;
70     #endif
71 pajs 1.2 #ifdef SOLARIS
72     if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
73     errf("Failed to get pagesize (%m)");
74     return NULL;
75     }
76    
77     if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
78     errf("Failed to get total memory");
79     return NULL;
80     }
81    
82     if ((kc = kstat_open()) == NULL) {
83     errf("kstat_open failure (%m)");
84     return NULL;
85     }
86     if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){
87     errf("kstat lookup for \"system_pages\" (%m)");
88     return NULL;
89     }
90     if (kstat_read(kc, ksp, 0) == -1) {
91     errf("Failed to read kstat details (%m)");
92     return NULL;
93     }
94     if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
95     errf("Failed to get free memory (%m)");
96     return NULL;
97     }
98     total=((totalmem*pagesize)/1024)/1024;
99     free=(((kn->value.ul)*pagesize)/1024)/1024;
100     used=total-free;
101     #endif
102     #ifdef FREEBSD
103     if ((pagesize=getpagesize()) == -1){
104     errf("Failed to get page size (%m)");
105     return NULL;
106     }
107    
108     if (sysctlbyname(TOTAL_MEM_NAME, NULL, &size, NULL, NULL) < 0){
109     errf("sysctlbyname failed to get total memory (%m)");
110     return NULL;
111     }
112     if (sysctlbyname(TOTAL_MEM_NAME, &total, &size, NULL, NULL) < 0){
113     errf("Failed to get total memory stats (%m)");
114     return NULL;
115     }
116    
117     if (sysctlbyname(FREE_MEM_NAME, NULL, &size, NULL, NULL) < 0){
118     errf("sysctlbyname failed to get free memory (%m)");
119     return NULL;
120     }
121     if (sysctlbyname(FREE_MEM_NAME, &free, &size, NULL, NULL) < 0){
122     errf("Failed to get free memory stats (%m)");
123     return NULL;
124     }
125    
126     if (sysctlbyname(INACTIVE_MEM_NAME, NULL, &size, NULL, NULL) < 0){
127     errf("sysctlbyname failed to get inactive memory (%m)");
128     return NULL;
129     }
130     if (sysctlbyname(INACTIVE_MEM_NAME, &inactive, &size, NULL, NULL) < 0){
131     errf("Failed to get inactive memory stats (%m)");
132     return NULL;
133     }
134    
135     if (sysctlbyname(CACHE_MEM_NAME, NULL, &size, NULL, NULL) < 0){
136     errf("sysctlbyname failed to get cache memory (%m)");
137     return NULL;
138     }
139     if (sysctlbyname(CACHE_MEM_NAME, &cache, &size, NULL, NULL) < 0){
140     errf("Failed to get cache memory stats (%m)");
141     return NULL;
142     }
143    
144     total/=(1024*1024);
145     free=((free*pagesize)/(1024*1024))+((inactive*pagesize)/(1024*1024));
146     cache=((cache*pagesize)/(1024*1024));
147    
148     /* used should also be cache+active+wired but its less effort this way :) */
149     used=total-free;
150    
151     #endif
152    
153 pajs 1.1
154     if((xml_memory_stats=strf("<memory><total>%ld</total><free>%ld</free><used>%ld</used><cache>%ld</cache></memory>", total, free, used, cache)) == NULL){
155     errf("strf failed (%m)");
156     return NULL;
157     }
158 pajs 1.2 return xml_memory_stats;
159 pajs 1.1 }