summaryrefslogtreecommitdiff
path: root/qa/src/arch_maxfd.c
blob: 16bc2da7448ea0b535868d4c73bedc6be9b12d46 (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
/*
 * Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
 *
 * Exercise pmNewContext() for archives close to the NOFILE max fd limit.
 * For incident: 504616
 */

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <sys/resource.h>
#include <pcp/pmapi.h>
#include <pcp/impl.h>

#include "localconfig.h"

#if PCP_VER < 2200
#define PRINTF_P_PFX ""
#endif

static char	*sfx[] = { "0", "index", "meta" };

int
main(int argc, char **argv)
{
    int		c;
    int		sts;
    int		errflag = 0;
    int		i;
    int		j;
    int		max_ctx;
    int		max_nofile;
    int		numopen = 0;			/* pander to gcc */
    int		ctx = -1;			/* pander to gcc */
    int		last_ctx;
    char	buf[100];
    char	lbuf[100];
    struct rlimit	top;
    char	*start = NULL;
    char	*end;

    __pmSetProgname(pmProgname);

    while ((c = getopt(argc, argv, "D:?")) != EOF) {
	switch (c) {

	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;

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

    sts = getrlimit(RLIMIT_NOFILE, &top);

    for (max_nofile = 30; max_nofile > 20; max_nofile--) {
	top.rlim_cur = max_nofile;
	sts = setrlimit(RLIMIT_NOFILE, &top);
	if (sts < 0) {
	    fprintf(stderr, "setrlimit(NOFILE=%d) failed: %s\n", max_nofile, strerror(errno));
	    exit(1);
	}
	sts = dup(0);
	if (sts < 0) {
	    fprintf(stderr, "dup(0) failed: %s\n", strerror(errno));
	    exit(1);
	}
	max_ctx = (max_nofile + 2 - sts) / 3;
	close(sts);

	printf("max fd: %d max ctx#: %d\n", max_nofile, max_ctx);
	last_ctx = -1;

	for (i = 0; i <= max_ctx; i++) {

	    for (j = 0; j < 3; j++) {
		sprintf(lbuf, "qa-tmp-%d.%s", i, sfx[j]);
		sprintf(buf, "%s.%s", argv[optind], sfx[j]);
		sts = link(buf, lbuf);
		if (sts < 0) {
		    fprintf(stderr, "link %s -> %s failed: %s\n",
			lbuf, buf, strerror(errno));
		    break;
		}
	    }

	    sprintf(lbuf, "qa-tmp-%d", i);
	    ctx = pmNewContext(PM_CONTEXT_ARCHIVE, lbuf);

	    for (j = 0; j < 3; j++) {
		sprintf(lbuf, "qa-tmp-%d.%s", i, sfx[j]);
		sts = unlink(lbuf);
		if (sts < 0) {
		    fprintf(stderr, "unlink %s failed: %s\n",
			lbuf, strerror(errno));
		    break;
		}
	    }

	    if (ctx < 0) {
		printf("pmNewContext(): %s\n", pmErrStr(ctx));
		if (i != max_ctx && i != max_ctx-1)
		    printf("Error: failure after ctx# %d, expected after %d or %d\n", last_ctx, max_ctx, max_ctx-1);
		break;
	    }
	    else
		numopen++;
	    last_ctx = ctx;
	}

	if (ctx >= 0)
	    printf("Error: pmNewContext() did not fail?\n");

	for (i = 0; i <= last_ctx; i++)
	    pmDestroyContext(i);

	if (start == NULL) {
	    start = sbrk(0);
	    numopen = 0;
	}
    }

    end = sbrk(0);

    if (end - start > 16*1024) {
	printf("Memory leak? after first pass, %ld bytes per archive open-close\n",
	    (long)((end - start) / numopen));
	printf("start: " PRINTF_P_PFX "%p end: " PRINTF_P_PFX "%p diff: %ld numopen: %d\n", start, end,
		(long)(end - start), numopen);
    }
    
    return 0;
}