]> arthur.barton.de Git - netdata.git/blob - src/proc_meminfo.c
replace strcmp() with strsame() and procfile improvements
[netdata.git] / src / proc_meminfo.c
1 #include "common.h"
2
3 int do_proc_meminfo(int update_every, usec_t 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     size_t lines = procfile_lines(ff), l;
117
118     int hwcorrupted = 0;
119
120     unsigned long long MemTotal = 0,
121             MemFree = 0,
122             Buffers = 0,
123             Cached = 0,
124             //SwapCached = 0,
125             //Active = 0,
126             //Inactive = 0,
127             //ActiveAnon = 0,
128             //InactiveAnon = 0,
129             //ActiveFile = 0,
130             //InactiveFile = 0,
131             //Unevictable = 0,
132             //Mlocked = 0,
133             SwapTotal = 0,
134             SwapFree = 0,
135             Dirty = 0,
136             Writeback = 0,
137             //AnonPages = 0,
138             //Mapped = 0,
139             //Shmem = 0,
140             Slab = 0,
141             SReclaimable = 0,
142             SUnreclaim = 0,
143             KernelStack = 0,
144             PageTables = 0,
145             NFS_Unstable = 0,
146             Bounce = 0,
147             WritebackTmp = 0,
148             //CommitLimit = 0,
149             Committed_AS = 0,
150             //VmallocTotal = 0,
151             VmallocUsed = 0,
152             //VmallocChunk = 0,
153             //AnonHugePages = 0,
154             //HugePages_Total = 0,
155             //HugePages_Free = 0,
156             //HugePages_Rsvd = 0,
157             //HugePages_Surp = 0,
158             //Hugepagesize = 0,
159             //DirectMap4k = 0,
160             //DirectMap2M = 0,
161             HardwareCorrupted = 0;
162
163     unsigned long long *value = NULL;
164     for(l = 0; l < lines ;l++) {
165         size_t 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
171              if(hash == MemTotal_hash && strsame(name, "MemTotal") == 0) value = &MemTotal;
172         else if(hash == MemFree_hash && strsame(name, "MemFree") == 0) value = &MemFree;
173         else if(hash == Buffers_hash && strsame(name, "Buffers") == 0) value = &Buffers;
174         else if(hash == Cached_hash && strsame(name, "Cached") == 0) value = &Cached;
175         //else if(hash == SwapCached_hash && strsame(name, "SwapCached") == 0) value = &SwapCached;
176         //else if(hash == Active_hash && strsame(name, "Active") == 0) value = &Active;
177         //else if(hash == Inactive_hash && strsame(name, "Inactive") == 0) value = &Inactive;
178         //else if(hash == ActiveAnon_hash && strsame(name, "ActiveAnon") == 0) value = &ActiveAnon;
179         //else if(hash == InactiveAnon_hash && strsame(name, "InactiveAnon") == 0) value = &InactiveAnon;
180         //else if(hash == ActiveFile_hash && strsame(name, "ActiveFile") == 0) value = &ActiveFile;
181         //else if(hash == InactiveFile_hash && strsame(name, "InactiveFile") == 0) value = &InactiveFile;
182         //else if(hash == Unevictable_hash && strsame(name, "Unevictable") == 0) value = &Unevictable;
183         //else if(hash == Mlocked_hash && strsame(name, "Mlocked") == 0) value = &Mlocked;
184         else if(hash == SwapTotal_hash && strsame(name, "SwapTotal") == 0) value = &SwapTotal;
185         else if(hash == SwapFree_hash && strsame(name, "SwapFree") == 0) value = &SwapFree;
186         else if(hash == Dirty_hash && strsame(name, "Dirty") == 0) value = &Dirty;
187         else if(hash == Writeback_hash && strsame(name, "Writeback") == 0) value = &Writeback;
188         //else if(hash == AnonPages_hash && strsame(name, "AnonPages") == 0) value = &AnonPages;
189         //else if(hash == Mapped_hash && strsame(name, "Mapped") == 0) value = &Mapped;
190         //else if(hash == Shmem_hash && strsame(name, "Shmem") == 0) value = &Shmem;
191         else if(hash == Slab_hash && strsame(name, "Slab") == 0) value = &Slab;
192         else if(hash == SReclaimable_hash && strsame(name, "SReclaimable") == 0) value = &SReclaimable;
193         else if(hash == SUnreclaim_hash && strsame(name, "SUnreclaim") == 0) value = &SUnreclaim;
194         else if(hash == KernelStack_hash && strsame(name, "KernelStack") == 0) value = &KernelStack;
195         else if(hash == PageTables_hash && strsame(name, "PageTables") == 0) value = &PageTables;
196         else if(hash == NFS_Unstable_hash && strsame(name, "NFS_Unstable") == 0) value = &NFS_Unstable;
197         else if(hash == Bounce_hash && strsame(name, "Bounce") == 0) value = &Bounce;
198         else if(hash == WritebackTmp_hash && strsame(name, "WritebackTmp") == 0) value = &WritebackTmp;
199         //else if(hash == CommitLimit_hash && strsame(name, "CommitLimit") == 0) value = &CommitLimit;
200         else if(hash == Committed_AS_hash && strsame(name, "Committed_AS") == 0) value = &Committed_AS;
201         //else if(hash == VmallocTotal_hash && strsame(name, "VmallocTotal") == 0) value = &VmallocTotal;
202         else if(hash == VmallocUsed_hash && strsame(name, "VmallocUsed") == 0) value = &VmallocUsed;
203         //else if(hash == VmallocChunk_hash && strsame(name, "VmallocChunk") == 0) value = &VmallocChunk;
204         else if(hash == HardwareCorrupted_hash && strsame(name, "HardwareCorrupted") == 0) { value = &HardwareCorrupted; hwcorrupted = 1; }
205         //else if(hash == AnonHugePages_hash && strsame(name, "AnonHugePages") == 0) value = &AnonHugePages;
206         //else if(hash == HugePages_Total_hash && strsame(name, "HugePages_Total") == 0) value = &HugePages_Total;
207         //else if(hash == HugePages_Free_hash && strsame(name, "HugePages_Free") == 0) value = &HugePages_Free;
208         //else if(hash == HugePages_Rsvd_hash && strsame(name, "HugePages_Rsvd") == 0) value = &HugePages_Rsvd;
209         //else if(hash == HugePages_Surp_hash && strsame(name, "HugePages_Surp") == 0) value = &HugePages_Surp;
210         //else if(hash == Hugepagesize_hash && strsame(name, "Hugepagesize") == 0) value = &Hugepagesize;
211         //else if(hash == DirectMap4k_hash && strsame(name, "DirectMap4k") == 0) value = &DirectMap4k;
212         //else if(hash == DirectMap2M_hash && strsame(name, "DirectMap2M") == 0) value = &DirectMap2M;
213
214         if(value) {
215             *value = str2ull(procfile_lineword(ff, l, 1));
216             value = NULL;
217         }
218     }
219
220     RRDSET *st;
221
222     // --------------------------------------------------------------------
223
224     // http://stackoverflow.com/questions/3019748/how-to-reliably-measure-available-memory-in-linux
225     unsigned long long MemUsed = MemTotal - MemFree - Cached - Buffers;
226
227     if(do_ram) {
228         st = rrdset_find("system.ram");
229         if(!st) {
230             st = rrdset_create("system", "ram", NULL, "ram", NULL, "System RAM", "MB", 200, update_every, RRDSET_TYPE_STACKED);
231
232             rrddim_add(st, "free",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
233             rrddim_add(st, "used",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
234             rrddim_add(st, "cached",  NULL, 1, 1024, RRDDIM_ABSOLUTE);
235             rrddim_add(st, "buffers", NULL, 1, 1024, RRDDIM_ABSOLUTE);
236         }
237         else rrdset_next(st);
238
239         rrddim_set(st, "free", MemFree);
240         rrddim_set(st, "used", MemUsed);
241         rrddim_set(st, "cached", Cached);
242         rrddim_set(st, "buffers", Buffers);
243         rrdset_done(st);
244     }
245
246     // --------------------------------------------------------------------
247
248     unsigned long long SwapUsed = SwapTotal - SwapFree;
249
250     if(SwapTotal || SwapUsed || SwapFree || do_swap == CONFIG_ONDEMAND_YES) {
251         do_swap = CONFIG_ONDEMAND_YES;
252
253         st = rrdset_find("system.swap");
254         if(!st) {
255             st = rrdset_create("system", "swap", NULL, "swap", NULL, "System Swap", "MB", 201, update_every, RRDSET_TYPE_STACKED);
256             st->isdetail = 1;
257
258             rrddim_add(st, "free",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
259             rrddim_add(st, "used",    NULL, 1, 1024, RRDDIM_ABSOLUTE);
260         }
261         else rrdset_next(st);
262
263         rrddim_set(st, "used", SwapUsed);
264         rrddim_set(st, "free", SwapFree);
265         rrdset_done(st);
266     }
267
268     // --------------------------------------------------------------------
269
270     if(hwcorrupted && (do_hwcorrupt == CONFIG_ONDEMAND_YES || (do_hwcorrupt == CONFIG_ONDEMAND_ONDEMAND && HardwareCorrupted > 0))) {
271         do_hwcorrupt = CONFIG_ONDEMAND_YES;
272
273         st = rrdset_find("mem.hwcorrupt");
274         if(!st) {
275             st = rrdset_create("mem", "hwcorrupt", NULL, "ecc", NULL, "Hardware Corrupted ECC", "MB", 9000, update_every, RRDSET_TYPE_LINE);
276             st->isdetail = 1;
277
278             rrddim_add(st, "HardwareCorrupted", NULL, 1, 1024, RRDDIM_ABSOLUTE);
279         }
280         else rrdset_next(st);
281
282         rrddim_set(st, "HardwareCorrupted", HardwareCorrupted);
283         rrdset_done(st);
284     }
285
286     // --------------------------------------------------------------------
287
288     if(do_committed) {
289         st = rrdset_find("mem.committed");
290         if(!st) {
291             st = rrdset_create("mem", "committed", NULL, "system", NULL, "Committed (Allocated) Memory", "MB", 5000, update_every, RRDSET_TYPE_AREA);
292             st->isdetail = 1;
293
294             rrddim_add(st, "Committed_AS", NULL, 1, 1024, RRDDIM_ABSOLUTE);
295         }
296         else rrdset_next(st);
297
298         rrddim_set(st, "Committed_AS", Committed_AS);
299         rrdset_done(st);
300     }
301
302     // --------------------------------------------------------------------
303
304     if(do_writeback) {
305         st = rrdset_find("mem.writeback");
306         if(!st) {
307             st = rrdset_create("mem", "writeback", NULL, "kernel", NULL, "Writeback Memory", "MB", 4000, update_every, RRDSET_TYPE_LINE);
308             st->isdetail = 1;
309
310             rrddim_add(st, "Dirty", NULL, 1, 1024, RRDDIM_ABSOLUTE);
311             rrddim_add(st, "Writeback", NULL, 1, 1024, RRDDIM_ABSOLUTE);
312             rrddim_add(st, "FuseWriteback", NULL, 1, 1024, RRDDIM_ABSOLUTE);
313             rrddim_add(st, "NfsWriteback", NULL, 1, 1024, RRDDIM_ABSOLUTE);
314             rrddim_add(st, "Bounce", NULL, 1, 1024, RRDDIM_ABSOLUTE);
315         }
316         else rrdset_next(st);
317
318         rrddim_set(st, "Dirty", Dirty);
319         rrddim_set(st, "Writeback", Writeback);
320         rrddim_set(st, "FuseWriteback", WritebackTmp);
321         rrddim_set(st, "NfsWriteback", NFS_Unstable);
322         rrddim_set(st, "Bounce", Bounce);
323         rrdset_done(st);
324     }
325
326     // --------------------------------------------------------------------
327
328     if(do_kernel) {
329         st = rrdset_find("mem.kernel");
330         if(!st) {
331             st = rrdset_create("mem", "kernel", NULL, "kernel", NULL, "Memory Used by Kernel", "MB", 6000, update_every, RRDSET_TYPE_STACKED);
332             st->isdetail = 1;
333
334             rrddim_add(st, "Slab", NULL, 1, 1024, RRDDIM_ABSOLUTE);
335             rrddim_add(st, "KernelStack", NULL, 1, 1024, RRDDIM_ABSOLUTE);
336             rrddim_add(st, "PageTables", NULL, 1, 1024, RRDDIM_ABSOLUTE);
337             rrddim_add(st, "VmallocUsed", NULL, 1, 1024, RRDDIM_ABSOLUTE);
338         }
339         else rrdset_next(st);
340
341         rrddim_set(st, "KernelStack", KernelStack);
342         rrddim_set(st, "Slab", Slab);
343         rrddim_set(st, "PageTables", PageTables);
344         rrddim_set(st, "VmallocUsed", VmallocUsed);
345         rrdset_done(st);
346     }
347
348     // --------------------------------------------------------------------
349
350     if(do_slab) {
351         st = rrdset_find("mem.slab");
352         if(!st) {
353             st = rrdset_create("mem", "slab", NULL, "slab", NULL, "Reclaimable Kernel Memory", "MB", 6500, update_every, RRDSET_TYPE_STACKED);
354             st->isdetail = 1;
355
356             rrddim_add(st, "reclaimable", NULL, 1, 1024, RRDDIM_ABSOLUTE);
357             rrddim_add(st, "unreclaimable", NULL, 1, 1024, RRDDIM_ABSOLUTE);
358         }
359         else rrdset_next(st);
360
361         rrddim_set(st, "reclaimable", SReclaimable);
362         rrddim_set(st, "unreclaimable", SUnreclaim);
363         rrdset_done(st);
364     }
365
366     return 0;
367 }
368