]> arthur.barton.de Git - netdata.git/blob - src/proc_meminfo.c
several code optimizations
[netdata.git] / src / proc_meminfo.c
1 #include "common.h"
2
3 int do_proc_meminfo(int update_every, unsigned long long dt) {
4     (void)dt;
5
6     static procfile *ff = NULL;
7     static int do_ram = -1, do_swap = -1, do_hwcorrupt = -1, do_committed = -1, do_writeback = -1, do_kernel = -1, do_slab = -1;
8     static uint32_t MemTotal_hash = 0,
9             MemFree_hash = 0,
10             Buffers_hash = 0,
11             Cached_hash = 0,
12             //SwapCached_hash = 0,
13             //Active_hash = 0,
14             //Inactive_hash = 0,
15             //ActiveAnon_hash = 0,
16             //InactiveAnon_hash = 0,
17             //ActiveFile_hash = 0,
18             //InactiveFile_hash = 0,
19             //Unevictable_hash = 0,
20             //Mlocked_hash = 0,
21             SwapTotal_hash = 0,
22             SwapFree_hash = 0,
23             Dirty_hash = 0,
24             Writeback_hash = 0,
25             //AnonPages_hash = 0,
26             //Mapped_hash = 0,
27             //Shmem_hash = 0,
28             Slab_hash = 0,
29             SReclaimable_hash = 0,
30             SUnreclaim_hash = 0,
31             KernelStack_hash = 0,
32             PageTables_hash = 0,
33             NFS_Unstable_hash = 0,
34             Bounce_hash = 0,
35             WritebackTmp_hash = 0,
36             //CommitLimit_hash = 0,
37             Committed_AS_hash = 0,
38             //VmallocTotal_hash = 0,
39             VmallocUsed_hash = 0,
40             //VmallocChunk_hash = 0,
41             //AnonHugePages_hash = 0,
42             //HugePages_Total_hash = 0,
43             //HugePages_Free_hash = 0,
44             //HugePages_Rsvd_hash = 0,
45             //HugePages_Surp_hash = 0,
46             //Hugepagesize_hash = 0,
47             //DirectMap4k_hash = 0,
48             //DirectMap2M_hash = 0,
49             HardwareCorrupted_hash = 0;
50
51     if(unlikely(do_ram == -1)) {
52         do_ram          = config_get_boolean("plugin:proc:/proc/meminfo", "system ram", 1);
53         do_swap         = config_get_boolean_ondemand("plugin:proc:/proc/meminfo", "system swap", CONFIG_ONDEMAND_ONDEMAND);
54         do_hwcorrupt    = config_get_boolean_ondemand("plugin:proc:/proc/meminfo", "hardware corrupted ECC", CONFIG_ONDEMAND_ONDEMAND);
55         do_committed    = config_get_boolean("plugin:proc:/proc/meminfo", "committed memory", 1);
56         do_writeback    = config_get_boolean("plugin:proc:/proc/meminfo", "writeback memory", 1);
57         do_kernel       = config_get_boolean("plugin:proc:/proc/meminfo", "kernel memory", 1);
58         do_slab         = config_get_boolean("plugin:proc:/proc/meminfo", "slab memory", 1);
59
60         MemTotal_hash = simple_hash("MemTotal");
61         MemFree_hash = simple_hash("MemFree");
62         Buffers_hash = simple_hash("Buffers");
63         Cached_hash = simple_hash("Cached");
64         //SwapCached_hash = simple_hash("SwapCached");
65         //Active_hash = simple_hash("Active");
66         //Inactive_hash = simple_hash("Inactive");
67         //ActiveAnon_hash = simple_hash("ActiveAnon");
68         //InactiveAnon_hash = simple_hash("InactiveAnon");
69         //ActiveFile_hash = simple_hash("ActiveFile");
70         //InactiveFile_hash = simple_hash("InactiveFile");
71         //Unevictable_hash = simple_hash("Unevictable");
72         //Mlocked_hash = simple_hash("Mlocked");
73         SwapTotal_hash = simple_hash("SwapTotal");
74         SwapFree_hash = simple_hash("SwapFree");
75         Dirty_hash = simple_hash("Dirty");
76         Writeback_hash = simple_hash("Writeback");
77         //AnonPages_hash = simple_hash("AnonPages");
78         //Mapped_hash = simple_hash("Mapped");
79         //Shmem_hash = simple_hash("Shmem");
80         Slab_hash = simple_hash("Slab");
81         SReclaimable_hash = simple_hash("SReclaimable");
82         SUnreclaim_hash = simple_hash("SUnreclaim");
83         KernelStack_hash = simple_hash("KernelStack");
84         PageTables_hash = simple_hash("PageTables");
85         NFS_Unstable_hash = simple_hash("NFS_Unstable");
86         Bounce_hash = simple_hash("Bounce");
87         WritebackTmp_hash = simple_hash("WritebackTmp");
88         //CommitLimit_hash = simple_hash("CommitLimit");
89         Committed_AS_hash = simple_hash("Committed_AS");
90         //VmallocTotal_hash = simple_hash("VmallocTotal");
91         VmallocUsed_hash = simple_hash("VmallocUsed");
92         //VmallocChunk_hash = simple_hash("VmallocChunk");
93         HardwareCorrupted_hash = simple_hash("HardwareCorrupted");
94         //AnonHugePages_hash = simple_hash("AnonHugePages");
95         //HugePages_Total_hash = simple_hash("HugePages_Total");
96         //HugePages_Free_hash = simple_hash("HugePages_Free");
97         //HugePages_Rsvd_hash = simple_hash("HugePages_Rsvd");
98         //HugePages_Surp_hash = simple_hash("HugePages_Surp");
99         //Hugepagesize_hash = simple_hash("Hugepagesize");
100         //DirectMap4k_hash = simple_hash("DirectMap4k");
101         //DirectMap2M_hash = simple_hash("DirectMap2M");
102     }
103
104     if(unlikely(!ff)) {
105         char filename[FILENAME_MAX + 1];
106         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/meminfo");
107         ff = procfile_open(config_get("plugin:proc:/proc/meminfo", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
108         if(unlikely(!ff))
109             return 1;
110     }
111
112     ff = procfile_readall(ff);
113     if(unlikely(!ff))
114         return 0; // we return 0, so that we will retry to open it next time
115
116     uint32_t lines = procfile_lines(ff), l;
117     uint32_t words;
118
119     int hwcorrupted = 0;
120
121     unsigned long long MemTotal = 0,
122             MemFree = 0,
123             Buffers = 0,
124             Cached = 0,
125             //SwapCached = 0,
126             //Active = 0,
127             //Inactive = 0,
128             //ActiveAnon = 0,
129             //InactiveAnon = 0,
130             //ActiveFile = 0,
131             //InactiveFile = 0,
132             //Unevictable = 0,
133             //Mlocked = 0,
134             SwapTotal = 0,
135             SwapFree = 0,
136             Dirty = 0,
137             Writeback = 0,
138             //AnonPages = 0,
139             //Mapped = 0,
140             //Shmem = 0,
141             Slab = 0,
142             SReclaimable = 0,
143             SUnreclaim = 0,
144             KernelStack = 0,
145             PageTables = 0,
146             NFS_Unstable = 0,
147             Bounce = 0,
148             WritebackTmp = 0,
149             //CommitLimit = 0,
150             Committed_AS = 0,
151             //VmallocTotal = 0,
152             VmallocUsed = 0,
153             //VmallocChunk = 0,
154             //AnonHugePages = 0,
155             //HugePages_Total = 0,
156             //HugePages_Free = 0,
157             //HugePages_Rsvd = 0,
158             //HugePages_Surp = 0,
159             //Hugepagesize = 0,
160             //DirectMap4k = 0,
161             //DirectMap2M = 0,
162             HardwareCorrupted = 0;
163
164     for(l = 0; l < lines ;l++) {
165         words = procfile_linewords(ff, l);
166         if(unlikely(words < 2)) continue;
167
168         char *name = procfile_lineword(ff, l, 0);
169         uint32_t hash = simple_hash(name);
170         unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
171
172              if(hash == MemTotal_hash && strcmp(name, "MemTotal") == 0) MemTotal = value;
173         else if(hash == MemFree_hash && strcmp(name, "MemFree") == 0) MemFree = value;
174         else if(hash == Buffers_hash && strcmp(name, "Buffers") == 0) Buffers = value;
175         else if(hash == Cached_hash && strcmp(name, "Cached") == 0) Cached = value;
176         //else if(hash == SwapCached_hash && strcmp(name, "SwapCached") == 0) SwapCached = value;
177         //else if(hash == Active_hash && strcmp(name, "Active") == 0) Active = value;
178         //else if(hash == Inactive_hash && strcmp(name, "Inactive") == 0) Inactive = value;
179         //else if(hash == ActiveAnon_hash && strcmp(name, "ActiveAnon") == 0) ActiveAnon = value;
180         //else if(hash == InactiveAnon_hash && strcmp(name, "InactiveAnon") == 0) InactiveAnon = value;
181         //else if(hash == ActiveFile_hash && strcmp(name, "ActiveFile") == 0) ActiveFile = value;
182         //else if(hash == InactiveFile_hash && strcmp(name, "InactiveFile") == 0) InactiveFile = value;
183         //else if(hash == Unevictable_hash && strcmp(name, "Unevictable") == 0) Unevictable = value;
184         //else if(hash == Mlocked_hash && strcmp(name, "Mlocked") == 0) Mlocked = value;
185         else if(hash == SwapTotal_hash && strcmp(name, "SwapTotal") == 0) SwapTotal = value;
186         else if(hash == SwapFree_hash && strcmp(name, "SwapFree") == 0) SwapFree = value;
187         else if(hash == Dirty_hash && strcmp(name, "Dirty") == 0) Dirty = value;
188         else if(hash == Writeback_hash && strcmp(name, "Writeback") == 0) Writeback = value;
189         //else if(hash == AnonPages_hash && strcmp(name, "AnonPages") == 0) AnonPages = value;
190         //else if(hash == Mapped_hash && strcmp(name, "Mapped") == 0) Mapped = value;
191         //else if(hash == Shmem_hash && strcmp(name, "Shmem") == 0) Shmem = value;
192         else if(hash == Slab_hash && strcmp(name, "Slab") == 0) Slab = value;
193         else if(hash == SReclaimable_hash && strcmp(name, "SReclaimable") == 0) SReclaimable = value;
194         else if(hash == SUnreclaim_hash && strcmp(name, "SUnreclaim") == 0) SUnreclaim = value;
195         else if(hash == KernelStack_hash && strcmp(name, "KernelStack") == 0) KernelStack = value;
196         else if(hash == PageTables_hash && strcmp(name, "PageTables") == 0) PageTables = value;
197         else if(hash == NFS_Unstable_hash && strcmp(name, "NFS_Unstable") == 0) NFS_Unstable = value;
198         else if(hash == Bounce_hash && strcmp(name, "Bounce") == 0) Bounce = value;
199         else if(hash == WritebackTmp_hash && strcmp(name, "WritebackTmp") == 0) WritebackTmp = value;
200         //else if(hash == CommitLimit_hash && strcmp(name, "CommitLimit") == 0) CommitLimit = value;
201         else if(hash == Committed_AS_hash && strcmp(name, "Committed_AS") == 0) Committed_AS = value;
202         //else if(hash == VmallocTotal_hash && strcmp(name, "VmallocTotal") == 0) VmallocTotal = value;
203         else if(hash == VmallocUsed_hash && strcmp(name, "VmallocUsed") == 0) VmallocUsed = value;
204         //else if(hash == VmallocChunk_hash && strcmp(name, "VmallocChunk") == 0) VmallocChunk = value;
205         else if(hash == HardwareCorrupted_hash && strcmp(name, "HardwareCorrupted") == 0) { HardwareCorrupted = value; hwcorrupted = 1; }
206         //else if(hash == AnonHugePages_hash && strcmp(name, "AnonHugePages") == 0) AnonHugePages = value;
207         //else if(hash == HugePages_Total_hash && strcmp(name, "HugePages_Total") == 0) HugePages_Total = value;
208         //else if(hash == HugePages_Free_hash && strcmp(name, "HugePages_Free") == 0) HugePages_Free = value;
209         //else if(hash == HugePages_Rsvd_hash && strcmp(name, "HugePages_Rsvd") == 0) HugePages_Rsvd = value;
210         //else if(hash == HugePages_Surp_hash && strcmp(name, "HugePages_Surp") == 0) HugePages_Surp = value;
211         //else if(hash == Hugepagesize_hash && strcmp(name, "Hugepagesize") == 0) Hugepagesize = value;
212         //else if(hash == DirectMap4k_hash && strcmp(name, "DirectMap4k") == 0) DirectMap4k = value;
213         //else if(hash == DirectMap2M_hash && strcmp(name, "DirectMap2M") == 0) DirectMap2M = value;
214     }
215
216     RRDSET *st;
217
218     // --------------------------------------------------------------------
219
220     // http://stackoverflow.com/questions/3019748/how-to-reliably-measure-available-memory-in-linux
221     unsigned long long MemUsed = MemTotal - MemFree - Cached - Buffers;
222
223     if(do_ram) {
224         st = rrdset_find("system.ram");
225         if(!st) {
226             st = rrdset_create("system", "ram", NULL, "ram", NULL, "System RAM", "MB", 200, update_every, RRDSET_TYPE_STACKED);
227
228             rrddim_add(st, "free",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
229             rrddim_add(st, "used",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
230             rrddim_add(st, "cached",  NULL, 1, 1024, RRDDIM_ABSOLUTE);
231             rrddim_add(st, "buffers", NULL, 1, 1024, RRDDIM_ABSOLUTE);
232         }
233         else rrdset_next(st);
234
235         rrddim_set(st, "free", MemFree);
236         rrddim_set(st, "used", MemUsed);
237         rrddim_set(st, "cached", Cached);
238         rrddim_set(st, "buffers", Buffers);
239         rrdset_done(st);
240     }
241
242     // --------------------------------------------------------------------
243
244     unsigned long long SwapUsed = SwapTotal - SwapFree;
245
246     if(SwapTotal || SwapUsed || SwapFree || do_swap == CONFIG_ONDEMAND_YES) {
247         do_swap = CONFIG_ONDEMAND_YES;
248
249         st = rrdset_find("system.swap");
250         if(!st) {
251             st = rrdset_create("system", "swap", NULL, "swap", NULL, "System Swap", "MB", 201, update_every, RRDSET_TYPE_STACKED);
252             st->isdetail = 1;
253
254             rrddim_add(st, "free",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
255             rrddim_add(st, "used",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
256         }
257         else rrdset_next(st);
258
259         rrddim_set(st, "used", SwapUsed);
260         rrddim_set(st, "free", SwapFree);
261         rrdset_done(st);
262     }
263
264     // --------------------------------------------------------------------
265
266     if(hwcorrupted && do_hwcorrupt && HardwareCorrupted > 0) {
267         do_hwcorrupt = CONFIG_ONDEMAND_YES;
268
269         st = rrdset_find("mem.hwcorrupt");
270         if(!st) {
271             st = rrdset_create("mem", "hwcorrupt", NULL, "errors", NULL, "Hardware Corrupted ECC", "MB", 9000, update_every, RRDSET_TYPE_LINE);
272             st->isdetail = 1;
273
274             rrddim_add(st, "HardwareCorrupted", NULL, 1, 1024, RRDDIM_ABSOLUTE);
275         }
276         else rrdset_next(st);
277
278         rrddim_set(st, "HardwareCorrupted", HardwareCorrupted);
279         rrdset_done(st);
280     }
281
282     // --------------------------------------------------------------------
283
284     if(do_committed) {
285         st = rrdset_find("mem.committed");
286         if(!st) {
287             st = rrdset_create("mem", "committed", NULL, "system", NULL, "Committed (Allocated) Memory", "MB", 5000, update_every, RRDSET_TYPE_AREA);
288             st->isdetail = 1;
289
290             rrddim_add(st, "Committed_AS", NULL, 1, 1024, RRDDIM_ABSOLUTE);
291         }
292         else rrdset_next(st);
293
294         rrddim_set(st, "Committed_AS", Committed_AS);
295         rrdset_done(st);
296     }
297
298     // --------------------------------------------------------------------
299
300     if(do_writeback) {
301         st = rrdset_find("mem.writeback");
302         if(!st) {
303             st = rrdset_create("mem", "writeback", NULL, "kernel", NULL, "Writeback Memory", "MB", 4000, update_every, RRDSET_TYPE_LINE);
304             st->isdetail = 1;
305
306             rrddim_add(st, "Dirty", NULL, 1, 1024, RRDDIM_ABSOLUTE);
307             rrddim_add(st, "Writeback", NULL, 1, 1024, RRDDIM_ABSOLUTE);
308             rrddim_add(st, "FuseWriteback", NULL, 1, 1024, RRDDIM_ABSOLUTE);
309             rrddim_add(st, "NfsWriteback", NULL, 1, 1024, RRDDIM_ABSOLUTE);
310             rrddim_add(st, "Bounce", NULL, 1, 1024, RRDDIM_ABSOLUTE);
311         }
312         else rrdset_next(st);
313
314         rrddim_set(st, "Dirty", Dirty);
315         rrddim_set(st, "Writeback", Writeback);
316         rrddim_set(st, "FuseWriteback", WritebackTmp);
317         rrddim_set(st, "NfsWriteback", NFS_Unstable);
318         rrddim_set(st, "Bounce", Bounce);
319         rrdset_done(st);
320     }
321
322     // --------------------------------------------------------------------
323
324     if(do_kernel) {
325         st = rrdset_find("mem.kernel");
326         if(!st) {
327             st = rrdset_create("mem", "kernel", NULL, "kernel", NULL, "Memory Used by Kernel", "MB", 6000, update_every, RRDSET_TYPE_STACKED);
328             st->isdetail = 1;
329
330             rrddim_add(st, "Slab", NULL, 1, 1024, RRDDIM_ABSOLUTE);
331             rrddim_add(st, "KernelStack", NULL, 1, 1024, RRDDIM_ABSOLUTE);
332             rrddim_add(st, "PageTables", NULL, 1, 1024, RRDDIM_ABSOLUTE);
333             rrddim_add(st, "VmallocUsed", NULL, 1, 1024, RRDDIM_ABSOLUTE);
334         }
335         else rrdset_next(st);
336
337         rrddim_set(st, "KernelStack", KernelStack);
338         rrddim_set(st, "Slab", Slab);
339         rrddim_set(st, "PageTables", PageTables);
340         rrddim_set(st, "VmallocUsed", VmallocUsed);
341         rrdset_done(st);
342     }
343
344     // --------------------------------------------------------------------
345
346     if(do_slab) {
347         st = rrdset_find("mem.slab");
348         if(!st) {
349             st = rrdset_create("mem", "slab", NULL, "slab", NULL, "Reclaimable Kernel Memory", "MB", 6500, update_every, RRDSET_TYPE_STACKED);
350             st->isdetail = 1;
351
352             rrddim_add(st, "reclaimable", NULL, 1, 1024, RRDDIM_ABSOLUTE);
353             rrddim_add(st, "unreclaimable", NULL, 1, 1024, RRDDIM_ABSOLUTE);
354         }
355         else rrdset_next(st);
356
357         rrddim_set(st, "reclaimable", SReclaimable);
358         rrddim_set(st, "unreclaimable", SUnreclaim);
359         rrdset_done(st);
360     }
361
362     return 0;
363 }
364