Changeset View
Changeset View
Standalone View
Standalone View
ksysguardd/Linux/Memory.c
Show All 32 Lines | |||||
33 | 33 | | |||
34 | #define MEMINFOBUFSIZE (2 * 1024) | 34 | #define MEMINFOBUFSIZE (2 * 1024) | ||
35 | 35 | | |||
36 | static char MemInfoBuf[ MEMINFOBUFSIZE ]; | 36 | static char MemInfoBuf[ MEMINFOBUFSIZE ]; | ||
37 | static int Dirty = 1; | 37 | static int Dirty = 1; | ||
38 | 38 | | |||
39 | static unsigned long long Total = 0; | 39 | static unsigned long long Total = 0; | ||
40 | static unsigned long long MFree = 0; | 40 | static unsigned long long MFree = 0; | ||
41 | static unsigned long long Available = 0; | ||||
41 | static unsigned long long Appl = 0; | 42 | static unsigned long long Appl = 0; | ||
42 | static unsigned long long Used = 0; | 43 | static unsigned long long Used = 0; | ||
43 | static unsigned long long Buffers = 0; | 44 | static unsigned long long Buffers = 0; | ||
44 | static unsigned long long Cached = 0; | 45 | static unsigned long long Cached = 0; | ||
46 | static unsigned long long Allocated = 0; | ||||
45 | static unsigned long long STotal = 0; | 47 | static unsigned long long STotal = 0; | ||
46 | static unsigned long long SFree = 0; | 48 | static unsigned long long SFree = 0; | ||
47 | static unsigned long long SUsed = 0; | 49 | static unsigned long long SUsed = 0; | ||
48 | static unsigned long long CDirty = 0; | 50 | static unsigned long long CDirty = 0; | ||
49 | static unsigned long long CWriteback = 0; | 51 | static unsigned long long CWriteback = 0; | ||
50 | 52 | | |||
51 | static void scan_one( const char* buff, const char *key, unsigned long long* val ) | 53 | static void scan_one( const char* buff, const char *key, unsigned long long* val ) | ||
52 | { | 54 | { | ||
53 | char *b = strstr( buff, key ); | 55 | char *b = strstr( buff, key ); | ||
54 | if ( b ) | 56 | if ( b ) | ||
55 | sscanf( b + strlen( key ), ": %llu", val ); | 57 | sscanf( b + strlen( key ), ": %llu", val ); | ||
56 | } | 58 | } | ||
57 | 59 | | |||
58 | static void processMemInfo() | 60 | static void processMemInfo() | ||
59 | { | 61 | { | ||
60 | unsigned long long Slab = 0; | 62 | unsigned long long Slab = 0; | ||
61 | scan_one( MemInfoBuf, "MemTotal", &Total ); | 63 | scan_one( MemInfoBuf, "MemTotal", &Total ); | ||
62 | scan_one( MemInfoBuf, "MemFree", &MFree ); | 64 | scan_one( MemInfoBuf, "MemFree", &MFree ); | ||
65 | scan_one( MemInfoBuf, "MemAvailable", &Available ); | ||||
63 | scan_one( MemInfoBuf, "Buffers", &Buffers ); | 66 | scan_one( MemInfoBuf, "Buffers", &Buffers ); | ||
64 | scan_one( MemInfoBuf, "Cached", &Cached ); | 67 | scan_one( MemInfoBuf, "Cached", &Cached ); | ||
65 | scan_one( MemInfoBuf, "Slab", &Slab ); | 68 | scan_one( MemInfoBuf, "Slab", &Slab ); | ||
66 | scan_one( MemInfoBuf, "SwapTotal", &STotal ); | 69 | scan_one( MemInfoBuf, "SwapTotal", &STotal ); | ||
67 | scan_one( MemInfoBuf, "SwapFree", &SFree ); | 70 | scan_one( MemInfoBuf, "SwapFree", &SFree ); | ||
68 | scan_one( MemInfoBuf, "Dirty", &CDirty ); | 71 | scan_one( MemInfoBuf, "Dirty", &CDirty ); | ||
69 | scan_one( MemInfoBuf, "Writeback", &CWriteback ); | 72 | scan_one( MemInfoBuf, "Writeback", &CWriteback ); | ||
70 | Cached += Slab; | 73 | Cached += Slab; | ||
71 | Used = Total - MFree; | 74 | Used = Total - MFree; | ||
72 | Appl = ( Used - ( Buffers + Cached ) ); | 75 | Appl = ( Used - ( Buffers + Cached ) ); | ||
73 | 76 | | |||
77 | Allocated = Total - Available; | ||||
78 | | ||||
74 | if ( STotal == 0 ) /* no swap activated */ | 79 | if ( STotal == 0 ) /* no swap activated */ | ||
75 | SUsed = 0; | 80 | SUsed = 0; | ||
76 | else | 81 | else | ||
77 | SUsed = STotal - SFree; | 82 | SUsed = STotal - SFree; | ||
78 | 83 | | |||
79 | Dirty = 0; | 84 | Dirty = 0; | ||
80 | } | 85 | } | ||
81 | 86 | | |||
82 | /* | 87 | /* | ||
83 | ================================ public part ================================= | 88 | ================================ public part ================================= | ||
84 | */ | 89 | */ | ||
85 | 90 | | |||
86 | void initMemory( struct SensorModul* sm ) | 91 | void initMemory( struct SensorModul* sm ) | ||
87 | { | 92 | { | ||
88 | /** | 93 | /** | ||
89 | Make sure that /proc/meminfo exists and is readable. If not we do | 94 | Make sure that /proc/meminfo exists and is readable. If not we do | ||
90 | not register any monitors for memory. | 95 | not register any monitors for memory. | ||
91 | */ | 96 | */ | ||
92 | if ( updateMemory() < 0 ) | 97 | if ( updateMemory() < 0 ) | ||
93 | return; | 98 | return; | ||
94 | 99 | | |||
100 | registerMonitor( "mem/physical/total", "integer", printTotal, printTotalInfo, sm ); | ||||
95 | registerMonitor( "mem/physical/free", "integer", printMFree, printMFreeInfo, sm ); | 101 | registerMonitor( "mem/physical/free", "integer", printMFree, printMFreeInfo, sm ); | ||
102 | registerMonitor( "mem/physical/available", "integer", printAvailable, printAvailableInfo, sm ); | ||||
96 | registerMonitor( "mem/physical/used", "integer", printUsed, printUsedInfo, sm ); | 103 | registerMonitor( "mem/physical/used", "integer", printUsed, printUsedInfo, sm ); | ||
97 | registerMonitor( "mem/physical/application", "integer", printAppl, printApplInfo, sm ); | 104 | registerMonitor( "mem/physical/application", "integer", printAppl, printApplInfo, sm ); | ||
98 | registerMonitor( "mem/physical/buf", "integer", printBuffers, printBuffersInfo, sm ); | 105 | registerMonitor( "mem/physical/buf", "integer", printBuffers, printBuffersInfo, sm ); | ||
99 | registerMonitor( "mem/physical/cached", "integer", printCached, printCachedInfo, sm ); | 106 | registerMonitor( "mem/physical/cached", "integer", printCached, printCachedInfo, sm ); | ||
107 | registerMonitor( "mem/physical/allocated", "integer", printAllocated, printAllocatedInfo, sm ); | ||||
100 | registerMonitor( "mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm ); | 108 | registerMonitor( "mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm ); | ||
101 | registerMonitor( "mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm ); | 109 | registerMonitor( "mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm ); | ||
102 | registerMonitor( "mem/cache/dirty", "integer", printCDirty, printCDirtyInfo, sm); | 110 | registerMonitor( "mem/cache/dirty", "integer", printCDirty, printCDirtyInfo, sm); | ||
103 | registerMonitor( "mem/cache/writeback", "integer", printCWriteback, printCWritebackInfo, sm); | 111 | registerMonitor( "mem/cache/writeback", "integer", printCWriteback, printCWritebackInfo, sm); | ||
104 | } | 112 | } | ||
105 | 113 | | |||
106 | void exitMemory( void ) | 114 | void exitMemory( void ) | ||
107 | { | 115 | { | ||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Line(s) | 119 | { | |||
156 | 164 | | |||
157 | close( fd ); | 165 | close( fd ); | ||
158 | MemInfoBuf[ n ] = '\0'; | 166 | MemInfoBuf[ n ] = '\0'; | ||
159 | Dirty = 1; | 167 | Dirty = 1; | ||
160 | 168 | | |||
161 | return 0; | 169 | return 0; | ||
162 | } | 170 | } | ||
163 | 171 | | |||
172 | void printTotal( const char* cmd ) | ||||
173 | { | ||||
174 | (void)cmd; | ||||
175 | | ||||
176 | if ( Dirty ) | ||||
177 | processMemInfo(); | ||||
178 | | ||||
179 | output( "%llu\n", Total ); | ||||
180 | } | ||||
181 | | ||||
182 | void printTotalInfo( const char* cmd ) | ||||
183 | { | ||||
184 | (void)cmd; | ||||
185 | | ||||
186 | if ( Dirty ) | ||||
187 | processMemInfo(); | ||||
188 | | ||||
189 | output( "Total Memory\t0\t%llu\tKB\n", Total ); | ||||
190 | } | ||||
191 | | ||||
164 | void printMFree( const char* cmd ) | 192 | void printMFree( const char* cmd ) | ||
165 | { | 193 | { | ||
166 | (void)cmd; | 194 | (void)cmd; | ||
167 | 195 | | |||
168 | if ( Dirty ) | 196 | if ( Dirty ) | ||
169 | processMemInfo(); | 197 | processMemInfo(); | ||
170 | 198 | | |||
171 | output( "%llu\n", MFree ); | 199 | output( "%llu\n", MFree ); | ||
172 | } | 200 | } | ||
173 | 201 | | |||
174 | void printMFreeInfo( const char* cmd ) | 202 | void printMFreeInfo( const char* cmd ) | ||
175 | { | 203 | { | ||
176 | (void)cmd; | 204 | (void)cmd; | ||
177 | 205 | | |||
178 | if ( Dirty ) | 206 | if ( Dirty ) | ||
179 | processMemInfo(); | 207 | processMemInfo(); | ||
180 | 208 | | |||
181 | output( "Free Memory\t0\t%llu\tKB\n", Total ); | 209 | output( "Free Memory\t0\t%llu\tKB\n", Total ); | ||
182 | } | 210 | } | ||
183 | 211 | | |||
212 | void printAvailable( const char* cmd ) | ||||
213 | { | ||||
214 | (void)cmd; | ||||
215 | | ||||
216 | if ( Dirty ) | ||||
217 | processMemInfo(); | ||||
218 | | ||||
219 | output( "%llu\n", Available ); | ||||
220 | } | ||||
221 | | ||||
222 | void printAvailableInfo( const char* cmd ) | ||||
223 | { | ||||
224 | (void)cmd; | ||||
225 | | ||||
226 | if ( Dirty ) | ||||
227 | processMemInfo(); | ||||
228 | | ||||
229 | output( "Available Memory\t0\t%llu\tKB\n", Total ); | ||||
230 | } | ||||
231 | | ||||
184 | void printUsed( const char* cmd ) | 232 | void printUsed( const char* cmd ) | ||
185 | { | 233 | { | ||
186 | (void)cmd; | 234 | (void)cmd; | ||
187 | 235 | | |||
188 | if ( Dirty ) | 236 | if ( Dirty ) | ||
189 | processMemInfo(); | 237 | processMemInfo(); | ||
190 | 238 | | |||
191 | output( "%llu\n", Used ); | 239 | output( "%llu\n", Used ); | ||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Line(s) | 303 | { | |||
256 | (void)cmd; | 304 | (void)cmd; | ||
257 | 305 | | |||
258 | if ( Dirty ) | 306 | if ( Dirty ) | ||
259 | processMemInfo(); | 307 | processMemInfo(); | ||
260 | 308 | | |||
261 | output( "Cached Memory\t0\t%llu\tKB\n", Total ); | 309 | output( "Cached Memory\t0\t%llu\tKB\n", Total ); | ||
262 | } | 310 | } | ||
263 | 311 | | |||
312 | void printAllocated( const char* cmd ) | ||||
313 | { | ||||
314 | (void)cmd; | ||||
315 | | ||||
316 | if ( Dirty ) | ||||
317 | processMemInfo(); | ||||
318 | | ||||
319 | output( "%llu\n", Allocated ); | ||||
320 | } | ||||
321 | | ||||
322 | void printAllocatedInfo( const char* cmd ) | ||||
323 | { | ||||
324 | (void)cmd; | ||||
325 | | ||||
326 | if ( Dirty ) | ||||
327 | processMemInfo(); | ||||
328 | | ||||
329 | output( "Allocated Memory\t0\t%llu\tKB\n", Total ); | ||||
330 | } | ||||
331 | | ||||
264 | void printSwapUsed( const char* cmd ) | 332 | void printSwapUsed( const char* cmd ) | ||
265 | { | 333 | { | ||
266 | (void)cmd; | 334 | (void)cmd; | ||
267 | 335 | | |||
268 | if ( Dirty ) | 336 | if ( Dirty ) | ||
269 | processMemInfo(); | 337 | processMemInfo(); | ||
270 | 338 | | |||
271 | output( "%llu\n", SUsed ); | 339 | output( "%llu\n", SUsed ); | ||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |