aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xenstore/xs_stress.c
blob: 9c480b155314c9925a44c290e7f96e8723a87036 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Stress test for Xen Store: multiple people hammering transactions */
#include "xs.h"
#include "utils.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define NUM_HANDLES 2
#define DIR_FANOUT 3
#define DIR_DEPTH 3

/* How often to print progress */
static int print;

/* Layout looks like /<num>/<num>/count. */
static void work(unsigned int cycles, unsigned int childnum)
{
	unsigned int i;
	struct xs_handle *handles[NUM_HANDLES];
	char id;

	if (childnum < 10)
		id = '0' + childnum;
	else
		id = 'A' + childnum - 10;

	for (i = 0; i < NUM_HANDLES; i++) {
		handles[i] = xs_daemon_open();
		if (!handles[i])
			barf_perror("Opening handle %i", i);
	}

	srandom(childnum);
	for (i = 0; i < cycles; i++) {
		unsigned int lockdepth, j, len;
		char file[100] = "", lockdir[100];
		char *contents, tmp[100];
		struct xs_handle *h = handles[random() % NUM_HANDLES];

		lockdepth = random() % DIR_DEPTH;
		for (j = 0; j < DIR_DEPTH; j++) {
			if (j == lockdepth)
				strcpy(lockdir, file);
			sprintf(file + strlen(file), "/%li",
				random()%DIR_FANOUT);
		}
		if (streq(lockdir, ""))
			strcpy(lockdir, "/");
		
		if (!xs_transaction_start(h, lockdir))
			barf_perror("%i: starting transaction %i on %s",
				    childnum, i, lockdir);

		sprintf(file + strlen(file), "/count");
		contents = xs_read(h, file, &len);
		if (!contents)
			barf_perror("%i: can't read %s iter %i",
				    childnum, file, i);
		sprintf(tmp, "%i", atoi(contents) + 1);
		if (!xs_write(h, file, tmp, strlen(tmp)+1, 0))
			barf_perror("%i: can't write %s iter %i",
				    childnum, file, i);

		/* Abandon 1 in 10 */
		if (random() % 10 == 0) {
			if (!xs_transaction_end(h, true))
				barf_perror("%i: can't abort transact %s",
					    childnum, lockdir);
			i--;
		} else {
			if (!xs_transaction_end(h, false))
				barf_perror("%i: can't commit transact %s",
					    childnum, lockdir);

			/* Offset when we print . so kids don't all
			 * print at once. */
			if ((i + print/(childnum+1)) % print == 0)
				write(STDOUT_FILENO, &id, 1);
		}
	}
}

static void create_dirs(struct xs_handle *h, const char *base, int togo)
{
	unsigned int i;
	char filename[100];

	if (togo == 0) {
		sprintf(filename, "%s/count", base);
		if (!xs_write(h, filename, "0", 2, O_EXCL|O_CREAT))
			barf_perror("Writing to %s", filename);
		return;
	}

	for (i = 0; i < DIR_FANOUT; i++) {
		sprintf(filename, "%s/%i", base, i);
		if (!xs_mkdir(h, filename))
			barf_perror("xs_mkdir %s", filename);
		create_dirs(h, filename, togo-1);
	}
}

static unsigned int add_count(struct xs_handle *h, const char *base, int togo)
{
	unsigned int i, count;
	char filename[100];

	if (togo == 0) {
		char *answer;
		unsigned int len;

		sprintf(filename, "%s/count", base);
		answer = xs_read(h, filename, &len);
		if (!answer)
			barf_perror("Reading %s", filename);
		count = atoi(answer);
		free(answer);
		return count;
	}

	count = 0;
	for (i = 0; i < DIR_FANOUT; i++) {
		sprintf(filename, "%s/%i", base, i);
		count += add_count(h, filename, togo-1);
	}
	return count;
}

static void setup(void)
{
	struct xs_handle *h;

	/* Do setup. */
	h = xs_daemon_open();
	if (!h)
		barf_perror("Contacting daemon");
	create_dirs(h, "", DIR_DEPTH);
	xs_daemon_close(h);
}

static unsigned int tally_counts(void)
{
	struct xs_handle *h;
	unsigned int ret;
	
	h = xs_daemon_open();
	if (!h)
		barf_perror("Contacting daemon");

	ret = add_count(h, "", DIR_DEPTH);
	xs_daemon_close(h);
	return ret;
}	

int main(int argc, char *argv[])
{
	unsigned int i;
	bool failed = false;
	int kids[10];

	if (argc != 2)
		barf("Usage: xs_stress <iterations>");

	printf("Setting up directories...\n");
	setup();

	print = atoi(argv[1]) / 76;
	if (!print)
		print = 1;

	printf("Running %i children...\n", ARRAY_SIZE(kids));
	for (i = 0; i < ARRAY_SIZE(kids); i++) {
		kids[i] = fork();
		if (kids[i] == -1)
			barf_perror("fork");
		if (kids[i] == 0) {
			work(atoi(argv[1]) / ARRAY_SIZE(kids), i);
			exit(0);
		}
	}

	for (i = 0; i < ARRAY_SIZE(kids); i++) {
		int status;
		if (waitpid(kids[i], &status, 0) == -1)
			barf_perror("waitpid");
		if (!WIFEXITED(status))
			barf("Kid %i died via signal %i\n",
			     i, WTERMSIG(status));
		if (WEXITSTATUS(status) != 0) {
			printf("Child %i exited %i\n", i, WEXITSTATUS(status));
			failed = true;
		}
	}
	if (failed)
		exit(1);

	printf("\nCounting results...\n");
	i = tally_counts();
	if (i != (unsigned)atoi(argv[1]))
		barf("Total counts %i not %s", i, atoi(argv[1]));
	printf("Success!\n");
	exit(0);
}