summaryrefslogtreecommitdiff
path: root/src/procmemstat/procmemstat.c
blob: f542455ee73dd0ac797e367de79dc92336fc46d2 (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
/*
 * procmemstat - sample, simple PMAPI client to report your own memory
 * usage
 *
 * Copyright (c) 2013 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 "pmapi.h"
#include "impl.h"
#include "pmnsmap.h"

static void
get_sample(void)
{
    int			first = 1;
    static pmResult	*rp;
    static int		numpmid;
    static pmID		*pmidlist;
    static pmDesc	*desclist;
    pmUnits		kbyte_scale;
    int			pid;
    pmAtomValue		tmp;
    pmAtomValue		atom;
    int			sts;
    int			i;

    if (first) {
	memset(&kbyte_scale, 0, sizeof(kbyte_scale));
	kbyte_scale.dimSpace = 1;
	kbyte_scale.scaleSpace = PM_SPACE_KBYTE;

	numpmid = sizeof(metrics) / sizeof(char *);
	if ((pmidlist = (pmID *)malloc(numpmid * sizeof(pmidlist[0]))) == NULL) {
	    fprintf(stderr, "%s: get_sample: malloc: %s\n", pmProgname, osstrerror());
	    exit(1);
	}
	if ((desclist = (pmDesc *)malloc(numpmid * sizeof(desclist[0]))) == NULL) {
	    fprintf(stderr, "%s: get_sample: malloc: %s\n", pmProgname, osstrerror());
	    exit(1);
	}
	if ((sts = pmLookupName(numpmid, metrics, pmidlist)) < 0) {
	    printf("%s: pmLookupName: %s\n", pmProgname, pmErrStr(sts));
	    for (i = 0; i < numpmid; i++) {
		if (pmidlist[i] == PM_ID_NULL)
		    fprintf(stderr, "%s: metric \"%s\" not in name space\n", pmProgname, metrics[i]);
	    }
	    exit(1);
	}
	for (i = 0; i < numpmid; i++) {
	    if ((sts = pmLookupDesc(pmidlist[i], &desclist[i])) < 0) {
		fprintf(stderr, "%s: cannot retrieve description for metric \"%s\" (PMID: %s)\nReason: %s\n",
		    pmProgname, metrics[i], pmIDStr(pmidlist[i]), pmErrStr(sts));
		exit(1);
	    }
	}
	/*
	 * All metrics we care about share the same instance domain,
	 * and the instance of interest is _my_ PID
	 */
	pmDelProfile(desclist[0].indom, 0, NULL);	/* all off */
	pid = (int)getpid();
	pmAddProfile(desclist[0].indom, 1, &pid);

	first = 0;
    }

    /* fetch the current metrics */
    if ((sts = pmFetch(numpmid, pmidlist, &rp)) < 0) {
	fprintf(stderr, "%s: pmFetch: %s\n", pmProgname, pmErrStr(sts));
	exit(1);
    }

    printf("memory metrics for pid %" FMT_PID " (sizes in Kbytes)\n", pid);
    for (i = 0; i < numpmid; i++) {
	/* process metrics in turn */
	pmExtractValue(rp->vset[i]->valfmt, rp->vset[i]->vlist,
		       desclist[i].type, &tmp, PM_TYPE_U32);
	pmConvScale(PM_TYPE_U32, &tmp, &desclist[i].units,
		    &atom, &kbyte_scale);
	printf("%8d %s\n", atom.l, metrics[i]);
    }
}

pmLongOptions longopts[] = {
    PMAPI_OPTIONS_HEADER("Options"),
    PMOPT_DEBUG,
    PMOPT_HELP,
    PMAPI_OPTIONS_END
};

pmOptions opts = {
    .short_options = "D:?",
    .long_options = longopts,
};

int
main(int argc, char **argv)
{
    int			sts;
    char		*p;
    char		*q;

    setlinebuf(stdout);
    pmGetOptions(argc, argv, &opts);

    if (opts.errors || opts.optind < argc - 1) {
	pmUsageMessage(&opts);
	exit(1);
    }

    if ((sts = pmNewContext(PM_CONTEXT_HOST, "local:")) < 0) {
	fprintf(stderr, "%s: Cannot connect to PMCD on host \"local:\": %s\n",
		pmProgname, pmErrStr(sts));
	exit(1);
    }

    get_sample();

#define ARRAY 1*1024*1024
    p = (char *)malloc(ARRAY);
    for (q = p; q < &p[ARRAY]; q += 1024)
	*q = '\0';
    printf("\nAfter malloc ...\n");
    get_sample();

    exit(0);
}