2 * Copyright (c) 2005-2007 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
10 #pragma ident "%Z%%M% %I% %E% SMI"
13 SM_RCSID("@(#)$Id: memstat.c,v 1.6 2007/03/20 23:26:12 ca Exp $")
22 static long sc_page_size
;
25 ** SM_MEMSTAT_OPEN -- open memory statistics
31 ** errno as error code, 0: ok
37 sc_page_size
= sysconf(_SC_PAGE_SIZE
);
38 if (sc_page_size
== -1)
39 return (errno
!= 0) ? errno
: -1;
44 ** SM_MEMSTAT_CLOSE -- close memory statistics
50 ** errno as error code, 0: ok
60 ** SM_MEMSTAT_GET -- get memory statistics
63 ** resource -- resource to look up
64 ** pvalue -- (pointer to) memory statistics value (output)
72 sm_memstat_get(resource
, pvalue
)
79 r
= swapctl(SC_AINFO
, &ai
);
81 return (errno
!= 0) ? errno
: -1;
82 r
= ai
.ani_max
- ai
.ani_resv
;
83 r
*= sc_page_size
>> 10;
91 #include <sys/sysinfo.h>
93 static kstat_ctl_t
*kc
;
97 ** SM_MEMSTAT_OPEN -- open memory statistics
103 ** errno as error code, 0: ok
113 return (errno
!= 0) ? errno
: -1;
114 kst
= kstat_lookup(kc
, "unix", 0,
115 (name
!= NULL
) ? name
: "system_pages");
117 return (errno
!= 0) ? errno
: -2;
122 ** SM_MEMSTAT_CLOSE -- close memory statistics
128 ** errno as error code, 0: ok
140 return (errno
!= 0) ? errno
: -1;
145 ** SM_MEMSTAT_GET -- get memory statistics
148 ** resource -- resource to look up
149 ** pvalue -- (pointer to) memory statistics value (output)
157 sm_memstat_get(resource
, pvalue
)
164 if (kc
== NULL
|| kst
== NULL
)
166 if (kstat_read(kc
, kst
, NULL
) == -1)
167 return (errno
!= 0) ? errno
: -2;
168 kn
= kstat_data_lookup(kst
,
169 (resource
!= NULL
) ? resource
: "freemem");
171 return (errno
!= 0) ? errno
: -3;
172 *pvalue
= kn
->value
.ul
;
180 total: used: free: shared: buffers: cached:
181 Mem: 261468160 252149760 9318400 0 3854336 109813760
182 Swap: 1052794880 62185472 990609408
191 Inact_target: 4092 kB
196 SwapTotal: 1028120 kB
205 ** SM_MEMSTAT_OPEN -- open memory statistics
211 ** errno as error code, 0: ok
217 fp
= fopen("/proc/meminfo", "r");
218 return (fp
!= NULL
) ? 0 : errno
;
222 ** SM_MEMSTAT_CLOSE -- close memory statistics
228 ** errno as error code, 0: ok
243 ** SM_MEMSTAT_GET -- get memory statistics
246 ** resource -- resource to look up
247 ** pvalue -- (pointer to) memory statistics value (output)
255 sm_memstat_get(resource
, pvalue
)
263 if (resource
== NULL
)
268 return -1; /* try to reopen? */
270 l
= strlen(resource
);
271 if (l
>= sizeof(buf
))
273 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
275 if (strncmp(buf
, resource
, l
) == 0 && buf
[l
] == ':')
277 r
= sscanf(buf
+ l
+ 1, "%ld", pvalue
);
278 return (r
> 0) ? 0 : -1;
284 #else /* USEPROCMEMINFO */
287 ** SM_MEMSTAT_OPEN -- open memory statistics
293 ** errno as error code, 0: ok
303 ** SM_MEMSTAT_CLOSE -- close memory statistics
309 ** errno as error code, 0: ok
319 ** SM_MEMSTAT_GET -- get memory statistics
322 ** resource -- resource to look up
323 ** pvalue -- (pointer to) memory statistics value (output)
331 sm_memstat_get(resource
, pvalue
)
338 #endif /* USEKSTAT */