ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/memory_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/memory_stats.c (file contents):
Revision 1.1 by pajs, Tue Feb 18 19:28:30 2003 UTC vs.
Revision 1.6 by pajs, Thu Apr 3 20:05:10 2003 UTC

# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 #include <stdio.h>
25   #include "statgrab.h"
27 #ifdef LINUX
28 #include <string.h>
29 #endif
26   #ifdef SOLARIS
27   #include <unistd.h>
28   #include <kstat.h>
29   #endif
30 + #ifdef LINUX
31 + #include <stdio.h>
32 + #include <string.h>
33 + #include "tools.h"
34 + #endif
35   #ifdef FREEBSD
36   #include <sys/types.h>
37   #include <sys/sysctl.h>
38   #include <unistd.h>
39   #endif
39 #include "ukcprog.h"
40  
41   mem_stat_t *get_memory_stats(){
42  
# Line 49 | Line 49 | mem_stat_t *get_memory_stats(){
49          long totalmem;
50          int pagesize;
51   #endif
52 + #ifdef LINUX
53 +        char *line_ptr;
54 +        FILE *f;
55 + #endif
56 + #ifdef FREEBSD
57 +        long inactive;
58 +        size_t size;
59 +        int pagesize;
60 + #endif
61  
62   #ifdef SOLARIS
63          if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
# Line 71 | Line 80 | mem_stat_t *get_memory_stats(){
80          if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
81                  return NULL;
82          }
83 <        if((kstat_close(kc)) != 0){
84 <                return NULL;
76 <        }      
83 >        kstat_close(kc);
84 >
85          mem_stat.total = (long long)totalmem * (long long)pagesize;
86          mem_stat.free = ((long long)kn->value.ul) * (long long)pagesize;
87          mem_stat.used = mem_stat.total - mem_stat.free;
88 + #endif
89 +
90 + #ifdef LINUX
91 +        f=fopen("/proc/meminfo", "r");
92 +        if(f==NULL){
93 +                return NULL;
94 +        }
95 +
96 +        if((line_ptr=f_read_line(f, "Mem:"))==NULL){
97 +                fclose(f);
98 +                return NULL;
99 +        }
100 +
101 +        fclose(f);
102 +
103 +        /* Linux actually stores this as a unsigned long long, but
104 +         * our structures are just long longs. This shouldn't be a
105 +         * problem for sometime yet :)
106 +         */
107 +        if((sscanf(line_ptr,"Mem:  %lld %lld %lld %*d %*d %lld", \
108 +                &mem_stat.total, \
109 +                &mem_stat.used, \
110 +                &mem_stat.free, \
111 +                &mem_stat.cache))!=4){
112 +                        return NULL;
113 +        }
114 +
115 + #endif
116 +
117 + #ifdef FREEBSD
118 +        /* Returns byes */
119 +        if (sysctlbyname("hw.physmem", NULL, &size, NULL, NULL) < 0){
120 +                return NULL;
121 +        }
122 +        if (sysctlbyname("hw.physmem", &mem_stat.total, &size, NULL, NULL) < 0){
123 +                return NULL;
124 +        }
125 +
126 +        /*returns pages*/
127 +        if (sysctlbyname("vm.stats.vm.v_free_count", NULL, &size, NULL, NULL) < 0){
128 +                return NULL;
129 +        }
130 +        if (sysctlbyname("vm.stats.vm.v_free_count", &mem_stat.free, &size, NULL, NULL) < 0){
131 +                return NULL;
132 +        }
133 +
134 +        if (sysctlbyname("vm.stats.vm.v_inactive_count", NULL, &size, NULL, NULL) < 0){
135 +                return NULL;
136 +        }
137 +        if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive , &size, NULL, NULL) < 0){
138 +                return NULL;
139 +        }
140 +
141 +        if (sysctlbyname("vm.stats.vm.v_cache_count", NULL, &size, NULL, NULL) < 0){
142 +                return NULL;
143 +        }
144 +        if (sysctlbyname("vm.stats.vm.v_cache_count", &mem_stat.cache, &size, NULL, NULL) < 0){
145 +                return NULL;
146 +        }
147 +
148 +        /* Because all the vm.stats returns pages, i need to get the page size.
149 +         * After that i then need to multiple the anything that used vm.stats to get
150 +         * the system statistics by pagesize
151 +         */
152 +        if ((pagesize=getpagesize()) == -1){
153 +                return NULL;
154 +        }
155 +
156 +        mem_stat.cache=mem_stat.cache*pagesize;
157 +        /* Of couse nothing is ever that simple :) And i have inactive pages to deal
158 +         * with too. So im goingto add them to free memory :)
159 +         */
160 +        mem_stat.free=(mem_stat.free*pagesize)+(inactive*pagesize);
161 +        
162 +        mem_stat.used=mem_stat.total-mem_stat.free;
163 +
164   #endif
165  
166          return &mem_stat;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines