forked from jmrosinski/GPTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_memusage.c
64 lines (55 loc) · 1.89 KB
/
print_memusage.c
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
/*
** print_memusage.c
**
** Author: Jim Rosinski
**
** print_memusage:
**
** Prints info about memory usage of this process by calling get_memusage.
**
** Return value: 0 = success
** -1 = failure
*/
#include "gptl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int GPTLprint_memusage (const char *str)
{
int size; /* process size (returned from OS) */
int rss; /* resident set size (returned from OS) */
int share; /* shared data segment size (returned from OS) */
int text; /* text segment size (returned from OS) */
int datastack; /* data/stack size (returned from OS) */
static int pagesize = -1; /* convert to bytes (init to invalid) */
static double pagestomb = -1; /* convert pages to MB */
static const char *thisfunc = "GPTLprint_memusage";
if (GPTLget_memusage (&size, &rss, &share, &text, &datastack) < 0)
return -1;
#if (defined HAVE_SLASHPROC || defined __APPLE__)
if (pagesize == -1)
if ((pagesize = sysconf (_SC_PAGESIZE)) > 0) {
pagestomb = pagesize / (1024.*1024.);
printf ("%s: Using pagesize=%d\n", thisfunc, pagesize);
}
if (pagestomb > 0)
printf ("%s: %s size=%.1f MB rss=%.1f MB datastack=%.1f MB\n",
thisfunc, str, size*pagestomb, rss*pagestomb, datastack*pagestomb);
else
printf ("%s: %s size=%d rss=%d datastack=%d\n",
thisfunc, str, size, rss, datastack);
#else
/*
** Use max rss as returned by getrusage. If someone knows how to
** get the process size under AIX please tell me.
*/
pagesize = 1024;
pagestomb = pagesize / (1024.*1024.);
if (1) /* change to 0 if cannot convert to MB */
printf ("%s: %s max rss=%.1f MB\n", thisfunc, str, rss*pagestomb);
else
printf ("%s: %s max rss=%d\n", thisfunc, str, rss);
#endif
return 0;
}