summaryrefslogtreecommitdiff
path: root/qa/src/grind_ctx.c
blob: da2832f15527a5ec99d341058196ecc4223a49f8 (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
/*
 * Repeatedly call pmNewContext ... then pmDestroyContext ...
 *
 * Looking for memory leaks, malloc botches etc, especially in the
 * presence of derived metrics.
 *
 * Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
 * (largely borrowed from chkctx2.c)
 */

#define SOURCE handle == 0 ? "host" : ( type == PM_CONTEXT_ARCHIVE ? "archive" : "host" )
#define HOST handle == 0 ? "localhost" : host

#include <pcp/pmapi.h>
#include <pcp/impl.h>

#define NUMCTX 10

static void
grind(int type, char *host)
{
    int		ctx[NUMCTX];
    int		i;
    int		sts;

    for (i = 0; i < NUMCTX; i++) {
	if (type == PM_CONTEXT_HOST) {
	    if ((sts = pmNewContext(PM_CONTEXT_HOST, host)) < 0) {
		fprintf(stderr, "pmNewContext(host=%s): %s\n", host, pmErrStr(sts));
		exit(1);
	    }
	}
	else if (type == PM_CONTEXT_LOCAL) {
	    if ((sts = pmNewContext(PM_CONTEXT_LOCAL, host)) < 0) {
		fprintf(stderr, "pmNewContext(local): %s\n", pmErrStr(sts));
		exit(1);
	    }
	}
	else {
	    if ((sts = pmNewContext(PM_CONTEXT_ARCHIVE, host)) < 0) {
		fprintf(stderr, "pmNewContext(archive=%s): %s\n", host, pmErrStr(sts));
		exit(1);
	    }
	}
	ctx[i] = sts;
    }

    for (i = NUMCTX-1; i >=0; i--) {
	if ((sts = pmDestroyContext(ctx[i])) < 0) {
	    fprintf(stderr, "pmDestroyContext(%d): %s\n", ctx[i], pmErrStr(sts));
	}
    }
}

int
main(int argc, char **argv)
{
    int		c;
    int		sts;
    int		errflag = 0;
    int		type = 0;
    int		iter = 5;
    char	*host = "localhost";
    char	*endnum;
#ifdef PCP_DEBUG
    static char	*debug = "[-D N] ";
#else
    static char	*debug = "";
#endif
    static char	*usage = "[-a archive] [-c dmfile] [-h hostname] [-L] [-n namespace] [-s iterations]";

    __pmSetProgname(argv[0]);

    while ((c = getopt(argc, argv, "a:c:D:h:Ln:s:")) != EOF) {
	switch (c) {

	case 'a':	/* archive name */
	    if (type != 0) {
		fprintf(stderr, "%s: at most one of -a, -h and -L allowed\n", pmProgname);
		errflag++;
	    }
	    type = PM_CONTEXT_ARCHIVE;
	    host = optarg;
	    break;

	case 'c':	/* derived metrics config file */
	    sts = pmLoadDerivedConfig(optarg);
	    if (sts < 0) {
		fprintf(stderr, "%s: -c error: %s\n", pmProgname, pmErrStr(sts));
		exit(1);
	    }
	    break;

#ifdef PCP_DEBUG
	case 'D':	/* debug flag */
	    sts = __pmParseDebug(optarg);
	    if (sts < 0) {
		fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n",
		    pmProgname, optarg);
		errflag++;
	    }
	    else
		pmDebug |= sts;
	    break;
#endif

	case 'h':	/* hostname for PMCD to contact */
	    if (type != 0) {
		fprintf(stderr, "%s: at most one of -a, -h and -L allowed\n", pmProgname);
		errflag++;
	    }
	    host = optarg;
	    type = PM_CONTEXT_HOST;
	    break;

	case 'L':	/* local mode, no PMCD */
	    if (type != 0) {
		fprintf(stderr, "%s: at most one of -a, -h and -L allowed\n", pmProgname);
		errflag++;
	    }
	    host = NULL;
	    type = PM_CONTEXT_LOCAL;
	    break;

	case 'n':	/* alternative name space file */
	    if ((sts = pmLoadNameSpace(optarg)) < 0) {
		fprintf(stderr, "%s: cannot load namespace from \"%s\": %s\n", pmProgname, optarg, pmErrStr(sts));
		exit(1);
	    }
	    break;

	case 's':	/* iterations */
	    iter = (int)strtol(optarg, &endnum, 10);
	    if (*endnum != '\0' || iter < 0) {
		fprintf(stderr, "%s: -s requires poisitive numeric argument\n", pmProgname);
		errflag++;
	    }
	    break;

	case '?':
	default:
	    errflag++;
	    break;
	}
    }

    if (errflag) {
	fprintf(stderr, "Usage: %s %s%s\n", pmProgname, debug, usage);
	exit(1);
    }

    if (type == 0)
	type = PM_CONTEXT_HOST;		/* default */

    while (iter-- > 0) {
	printf("Iteration %d\n", iter);
	grind(type, host);
    }

    exit(0);
}