ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/memory_stats.c
Revision: 1.6
Committed: Thu Apr 3 20:05:10 2003 UTC (21 years, 1 month ago) by pajs
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_4
Changes since 1.5: +59 -0 lines
Log Message:
Should now work happily on freebsd. sysctlbyname seems to return a lot of
things in long's. Im not completely trusting this code, but it does appear
to work, and its the same as the old implementation.

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.6 #ifdef FREEBSD
36     #include <sys/types.h>
37     #include <sys/sysctl.h>
38     #include <unistd.h>
39     #endif
40 pajs 1.1
41     mem_stat_t *get_memory_stats(){
42    
43     static mem_stat_t mem_stat;
44    
45     #ifdef SOLARIS
46     kstat_ctl_t *kc;
47     kstat_t *ksp;
48     kstat_named_t *kn;
49     long totalmem;
50     int pagesize;
51     #endif
52 pajs 1.5 #ifdef LINUX
53     char *line_ptr;
54     FILE *f;
55     #endif
56 pajs 1.6 #ifdef FREEBSD
57     long inactive;
58     size_t size;
59     int pagesize;
60     #endif
61 pajs 1.1
62     #ifdef SOLARIS
63     if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
64     return NULL;
65     }
66    
67     if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
68     return NULL;
69     }
70    
71     if ((kc = kstat_open()) == NULL) {
72     return NULL;
73     }
74     if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){
75     return NULL;
76     }
77     if (kstat_read(kc, ksp, 0) == -1) {
78     return NULL;
79     }
80     if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
81     return NULL;
82     }
83 pajs 1.2 kstat_close(kc);
84    
85 pajs 1.1 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 pajs 1.5 #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 pajs 1.6
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 pajs 1.5
164 pajs 1.1 #endif
165    
166     return &mem_stat;
167    
168     }