summaryrefslogtreecommitdiff
path: root/src/pmdas/linux/proc_meminfo.c
blob: 471e2ce846426d1f5c674c598ec3f74232df3b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Linux /proc/meminfo metrics cluster
 *
 * Copyright (c) 2013-2014 Red Hat.
 * Copyright (c) 2002 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <ctype.h>
#include "pmapi.h"
#include "pmda.h"
#include "indom.h"
#include <sys/stat.h>
#include "proc_meminfo.h"

static proc_meminfo_t moff;
extern size_t _pm_system_pagesize;

static struct {
    char	*field;
    int64_t	*offset;
} meminfo_fields[] = {
    { "MemTotal",	&moff.MemTotal },
    { "MemFree",	&moff.MemFree },
    { "MemAvailable",	&moff.MemAvailable },
    { "MemShared",	&moff.MemShared },
    { "Buffers",	&moff.Buffers },
    { "Cached",		&moff.Cached },
    { "SwapCached",	&moff.SwapCached },
    { "Active",		&moff.Active },
    { "Inactive",	&moff.Inactive },
    { "Active(anon)",	&moff.Active_anon },
    { "Inactive(anon)", &moff.Inactive_anon },
    { "Active(file)",   &moff.Active_file },
    { "Inactive(file)", &moff.Inactive_file },
    { "Unevictable",	&moff.Unevictable },
    { "Mlocked",	&moff.Mlocked },
    { "HighTotal",	&moff.HighTotal },
    { "HighFree",	&moff.HighFree },
    { "LowTotal",	&moff.LowTotal },
    { "LowFree",	&moff.LowFree },
    { "MmapCopy",	&moff.MmapCopy },
    { "SwapTotal",	&moff.SwapTotal },
    { "SwapFree",	&moff.SwapFree },
    { "Dirty",		&moff.Dirty },
    { "Writeback",	&moff.Writeback },
    { "AnonPages",	&moff.AnonPages },
    { "Mapped",		&moff.Mapped },
    { "Shmem",		&moff.Shmem },
    { "Slab",		&moff.Slab },
    { "SReclaimable",	&moff.SlabReclaimable },
    { "SUnreclaim",	&moff.SlabUnreclaimable },
    { "KernelStack",	&moff.KernelStack },
    { "PageTables",	&moff.PageTables },
    { "Quicklists",	&moff.Quicklists },
    { "NFS_Unstable",	&moff.NFS_Unstable },
    { "Bounce",		&moff.Bounce },
    { "WritebackTmp",	&moff.WritebackTmp },
    { "CommitLimit",	&moff.CommitLimit },
    { "Committed_AS",	&moff.Committed_AS },
    { "VmallocTotal",	&moff.VmallocTotal },
    { "VmallocUsed",	&moff.VmallocUsed },
    { "VmallocChunk",	&moff.VmallocChunk },
    { "HardwareCorrupted", &moff.HardwareCorrupted },
    { "AnonHugePages",	&moff.AnonHugePages },
    /* vendor kernel patches, some outdated now */
    { "MemShared",	&moff.MemShared },
    { "ReverseMaps",	&moff.ReverseMaps },
    { "HugePages_Total", &moff.HugepagesTotal },
    { "HugePages_Free",	&moff.HugepagesFree },
    { "HugePages_Rsvd",	&moff.HugepagesRsvd },
    { "HugePages_Surp",	&moff.HugepagesSurp },
    { "DirectMap4k",	&moff.directMap4k },
    { "DirectMap2M",	&moff.directMap2M },
    { "DirectMap1G",	&moff.directMap1G },
    { NULL, NULL }
};

#define MOFFSET(ii, pp) (int64_t *)((char *)pp + \
    (__psint_t)meminfo_fields[ii].offset - (__psint_t)&moff)

int
refresh_proc_meminfo(proc_meminfo_t *proc_meminfo)
{
    char	buf[1024];
    char	*bufp;
    int64_t	*p;
    int		i;
    FILE	*fp;

    for (i = 0; meminfo_fields[i].field != NULL; i++) {
	p = MOFFSET(i, proc_meminfo);
	*p = -1; /* marked as "no value available" */
    }

    if ((fp = linux_statsfile("/proc/meminfo", buf, sizeof(buf))) == NULL)
	return -oserror();

    while (fgets(buf, sizeof(buf), fp) != NULL) {
	if ((bufp = strchr(buf, ':')) == NULL)
	    continue;
	*bufp = '\0';
	for (i=0; meminfo_fields[i].field != NULL; i++) {
	    if (strcmp(buf, meminfo_fields[i].field) != 0)
		continue;
	    p = MOFFSET(i, proc_meminfo);
	    for (bufp++; *bufp; bufp++) {
	    	if (isdigit((int)*bufp)) {
		    sscanf(bufp, "%llu", (unsigned long long *)p);
		    *p *= 1024; /* kbytes -> bytes */
		    break;
		}
	    }
	}
    }

    fclose(fp);

    /*
     * MemAvailable is only in 3.x or later kernels but we can calculate it
     * using other values, similar to upstream kernel commit 34e431b0ae.
     * The environment variable is for QA purposes.
     */
    if (!MEMINFO_VALID_VALUE(proc_meminfo->MemAvailable) ||
    	getenv("PCP_QA_ESTIMATE_MEMAVAILABLE") != NULL) {
	if (MEMINFO_VALID_VALUE(proc_meminfo->MemTotal) &&
	    MEMINFO_VALID_VALUE(proc_meminfo->MemFree) &&
	    MEMINFO_VALID_VALUE(proc_meminfo->Active_file) &&
	    MEMINFO_VALID_VALUE(proc_meminfo->Inactive_file) &&
	    MEMINFO_VALID_VALUE(proc_meminfo->SlabReclaimable)) {

	    int64_t pagecache;
	    int64_t wmark_low = 0;

	    /*
	     * sum for each zone->watermark[WMARK_LOW];
	     */
	    if ((fp = fopen("/proc/zoneinfo", "r")) != NULL) {
		while (fgets(buf, sizeof(buf), fp) != NULL) {
		    if ((bufp = strstr(buf, "low ")) != NULL) {
			int64_t low;
		    	if (sscanf(bufp+4, "%lld", (long long int *)&low) == 1)
			    wmark_low += low;
		    }
		}
		fclose(fp);
		wmark_low *= _pm_system_pagesize;
	    }

	    /*  
	     * Free memory cannot be taken below the low watermark, before the
	     * system starts swapping.
	     */
	    proc_meminfo->MemAvailable = proc_meminfo->MemFree - wmark_low;

	    /*
	     * Not all the page cache can be freed, otherwise the system will
	     * start swapping. Assume at least half of the page cache, or the
	     * low watermark worth of cache, needs to stay.
	     */
	    pagecache = proc_meminfo->Active_file + proc_meminfo->Inactive_file;
	    pagecache -= MIN(pagecache / 2, wmark_low);
	    proc_meminfo->MemAvailable += pagecache;

	    /*
	     * Part of the reclaimable slab consists of items that are in use,
	     * and cannot be freed. Cap this estimate at the low watermark.
	     */
	    proc_meminfo->MemAvailable += proc_meminfo->SlabReclaimable;
	    proc_meminfo->MemAvailable -= MIN(proc_meminfo->SlabReclaimable / 2, wmark_low);

	    if (proc_meminfo->MemAvailable < 0)
	    	proc_meminfo->MemAvailable = 0;
	}
    }

    /* success */
    return 0;
}