summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/memstat.c
blob: a58b4c2ed3e80e6356956319ae5fea18804a2d4b (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (c) 2005-2007 Proofpoint, Inc. and its suppliers.
 *      All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: memstat.c,v 1.7 2013-11-22 20:51:43 ca Exp $")

#include <errno.h>
#include <sm/misc.h>

#if USESWAPCTL
#include <sys/stat.h>
#include <sys/swap.h>

static long sc_page_size;

/*
**  SM_MEMSTAT_OPEN -- open memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_open()
{
	sc_page_size = sysconf(_SC_PAGE_SIZE);
	if (sc_page_size == -1)
		return (errno != 0) ? errno : -1;
	return 0;
}

/*
**  SM_MEMSTAT_CLOSE -- close memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_close()
{
	return 0;
}

/*
**  SM_MEMSTAT_GET -- get memory statistics
**
**	Parameters:
**		resource -- resource to look up
**		pvalue -- (pointer to) memory statistics value (output)
**
**	Results:
**		0: success
**		!=0: error
*/

int
sm_memstat_get(resource, pvalue)
	char *resource;
	long *pvalue;
{
	int r;
	struct anoninfo ai;

	r = swapctl(SC_AINFO, &ai);
	if (r == -1)
		return (errno != 0) ? errno : -1;
	r = ai.ani_max - ai.ani_resv;
	r *= sc_page_size >> 10;
   	*pvalue = r;
	return 0;
}

#elif USEKSTAT

#include <kstat.h>
#include <sys/sysinfo.h>

static kstat_ctl_t *kc;
static kstat_t *kst;

/*
**  SM_MEMSTAT_OPEN -- open memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_open()
{
	kstat_named_t *kn;

	kc = kstat_open();
	if (kc == NULL)
		return (errno != 0) ? errno : -1;
	kst = kstat_lookup(kc, "unix", 0,
		(name != NULL) ? name : "system_pages");
	if (kst == 0)
		return (errno != 0) ? errno : -2;
	return 0;
}

/*
**  SM_MEMSTAT_CLOSE -- close memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_close()
{
	int r;

	if (kc == NULL)
		return 0;
	r = kstat_close(kc);
	if (r != 0)
		return (errno != 0) ? errno : -1;
	return 0;
}

/*
**  SM_MEMSTAT_GET -- get memory statistics
**
**	Parameters:
**		resource -- resource to look up
**		pvalue -- (pointer to) memory statistics value (output)
**
**	Results:
**		0: success
**		!=0: error
*/

int
sm_memstat_get(resource, pvalue)
	char *resource;
	long *pvalue;
{
	int r;
	kstat_named_t *kn;

	if (kc == NULL || kst == NULL)
		return -1;
	if (kstat_read(kc, kst, NULL) == -1)
		return (errno != 0) ? errno : -2;
	kn = kstat_data_lookup(kst,
			(resource != NULL) ? resource: "freemem");
	if (kn == NULL)
		return (errno != 0) ? errno : -3;
   	*pvalue = kn->value.ul;
	return 0;
}

#elif USEPROCMEMINFO

/*
/proc/meminfo?
        total:    used:    free:  shared: buffers:  cached:
Mem:  261468160 252149760  9318400        0  3854336 109813760
Swap: 1052794880 62185472 990609408
MemTotal:       255340 kB
MemFree:          9100 kB
MemShared:           0 kB
Buffers:          3764 kB
Cached:         107240 kB
Active:         104340 kB
Inact_dirty:      4220 kB
Inact_clean:      2444 kB
Inact_target:     4092 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       255340 kB
LowFree:          9100 kB
SwapTotal:     1028120 kB
SwapFree:       967392 kB
*/

#include <stdio.h>
#include <string.h>
static FILE *fp;

/*
**  SM_MEMSTAT_OPEN -- open memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_open()
{
	fp = fopen("/proc/meminfo", "r");
	return (fp != NULL) ? 0 : errno;
}

/*
**  SM_MEMSTAT_CLOSE -- close memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_close()
{
	if (fp != NULL)
	{
		fclose(fp);
		fp = NULL;
	}
	return 0;
}

/*
**  SM_MEMSTAT_GET -- get memory statistics
**
**	Parameters:
**		resource -- resource to look up
**		pvalue -- (pointer to) memory statistics value (output)
**
**	Results:
**		0: success
**		!=0: error
*/

int
sm_memstat_get(resource, pvalue)
	char *resource;
	long *pvalue;
{
	int r;
	size_t l;
	char buf[80];

	if (resource == NULL)
		return EINVAL;
	if (pvalue == NULL)
		return EINVAL;
	if (fp == NULL)
		return -1;	/* try to reopen? */
	rewind(fp);
	l = strlen(resource);
	if (l >= sizeof(buf))
		return EINVAL;
	while (fgets(buf, sizeof(buf), fp) != NULL)
	{
		if (strncmp(buf, resource, l) == 0 && buf[l] == ':')
		{
			r = sscanf(buf + l + 1, "%ld", pvalue);
			return (r > 0) ? 0 : -1;
		}
	}
	return 0;
}

#else /* USEPROCMEMINFO */

/*
**  SM_MEMSTAT_OPEN -- open memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_open()
{
	return -1;
}

/*
**  SM_MEMSTAT_CLOSE -- close memory statistics
**
**	Parameters:
**		none
**
**	Results:
**		errno as error code, 0: ok
*/

int
sm_memstat_close()
{
	return 0;
}

/*
**  SM_MEMSTAT_GET -- get memory statistics
**
**	Parameters:
**		resource -- resource to look up
**		pvalue -- (pointer to) memory statistics value (output)
**
**	Results:
**		0: success
**		!=0: error
*/

int
sm_memstat_get(resource, pvalue)
	char *resource;
	long *pvalue;
{
	return -1;
}

#endif /* USEKSTAT */
OpenPOWER on IntegriCloud