summaryrefslogtreecommitdiffstats
path: root/misc-utils/test_uuidd.c
blob: f012a2cf7be66dca0a756b6befbe295021ac4e95 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
 * Huschaam Hussain <Huschaam.Hussain@hp.com>
 *	TSG Solution Alliances Engineering
 *	SAP Technology Group
 *
 * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
 *
 *
 * The test heavily uses shared memory, to enlarge maximal size of shared
 * segment use:
 *
 *	echo "4294967295" > /proc/sys/kernel/shmm
 *
 * The test is compiled against in-tree libuuid, if you want to test uuidd
 * installed to the system then make sure that libuuid uses the same socket
 * like the running uuidd. You can start the uuidd manually, for example:
 *
 *	uuidd --debug --no-fork --no-pid --socket /run/uuidd/request
 *
 * if the $runstatedir (as defined by build-system) is /run. If you want
 * to overwrite the built-in default then use:
 *
 *	make uuidd uuidgen runstatedir=/var/run
 */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "uuid.h"
#include "c.h"
#include "xalloc.h"
#include "strutils.h"
#include "nls.h"

#define LOG(level,args) if (loglev >= level) { fprintf args; }

static size_t nprocesses = 4;
static size_t nthreads = 4;
static size_t nobjects = 4096;
static size_t loglev = 1;

struct processentry {
	pid_t		pid;
	int		status;
};
typedef struct processentry process_t;

struct threadentry {
	process_t	*proc;
	pthread_t	tid;		/* pthread_self() / phtread_create() */
	pthread_attr_t	thread_attr;
	size_t		index;		/* index in object[] */
	int		retval;		/* pthread exit() */
};
typedef struct threadentry thread_t;

/* this is in shared memory, keep it as small as possible */
struct objectentry {
	uuid_t		uuid;
	pthread_t	tid;
	pid_t		pid;
	size_t		idx;
};
typedef struct objectentry object_t;

static int shmem_id;
static object_t *objects;


static void __attribute__((__noreturn__)) usage(void)
{
	printf("\n %s [options]\n", program_invocation_short_name);

	printf("  -p <num>     number of nprocesses (default:%zu)\n", nprocesses);
	printf("  -t <num>     number of nthreads (default:%zu)\n", nthreads);
	printf("  -o <num>     number of nobjects (default:%zu)\n", nobjects);
	printf("  -l <level>   log level (default:%zu)\n", loglev);
	printf("  -h           display help\n");

	exit(EXIT_SUCCESS);
}

static void allocate_segment(int *id, void **address, size_t number, size_t size)
{
	*id = shmget(IPC_PRIVATE, number * size, IPC_CREAT | 0600);
	if (*id == -1)
		err(EXIT_FAILURE, "shmget failed to create %zu bytes shared memory", number * size);

	*address = shmat(*id, NULL, 0);
	if (*address == (void *)-1)
		err(EXIT_FAILURE, "shmat failed");

	LOG(2, (stderr,
	     "allocate shared memory segment [id=%d,address=0x%p]\n",
	     *id, *address));

	memset(*address, 0, number * size);
}

static void remove_segment(int id, void *address)
{
	if (shmdt(address) == -1)
		err(EXIT_FAILURE, "shmdt failed");
	if (shmctl(id, IPC_RMID, NULL) == -1)
		err(EXIT_FAILURE, "shmctl failed");
	LOG(2,
	    (stderr,
	     "remove shared memory segment [id=%d,address=0x%p]\n",
	     id, address));
}

static void object_uuid_create(object_t * object)
{
	uuid_generate_time(object->uuid);
}

static void object_uuid_to_string(object_t * object, char **string_uuid)
{
	uuid_unparse(object->uuid, *string_uuid);
}

static int object_uuid_compare(const void *object1, const void *object2)
{
	uuid_t *uuid1 = &((object_t *) object1)->uuid,
	       *uuid2 = &((object_t *) object2)->uuid;

	return uuid_compare(*uuid1, *uuid2);
}

static void *create_uuids(thread_t *th)
{
	size_t i;

	for (i = th->index; i < th->index + nobjects; i++) {
		object_t *obj = &objects[i];

		object_uuid_create(obj);
		obj->tid = th->tid;
		obj->pid = th->proc->pid;
		obj->idx = th->index + i;
	}
	return NULL;
}

static void *thread_body(void *arg)
{
	thread_t *th = (thread_t *) arg;

	return create_uuids(th);
}

static void create_nthreads(process_t *proc, size_t index)
{
	thread_t *threads;
	size_t i, ncreated = 0;
	int rc;

	threads = xcalloc(nthreads, sizeof(thread_t));

	for (i = 0; i < nthreads; i++) {
		thread_t *th = &threads[i];

		rc = pthread_attr_init(&th->thread_attr);
		if (rc) {
			errno = rc;
			warn("%d: pthread_attr_init failed", proc->pid);
			break;
		}

		th->index = index;
		th->proc = proc;
		rc = pthread_create(&th->tid, &th->thread_attr, &thread_body, th);

		if (rc) {
			errno = rc;
			warn("%d: pthread_create failed", proc->pid);
			break;
		}

		LOG(2, (stderr, "%d: started thread [tid=%jd,index=%zu]\n",
		     proc->pid, (intmax_t) (intptr_t) th->tid, th->index));
		index += nobjects;
		ncreated++;
	}

	if (ncreated != nthreads)
		fprintf(stderr, "%d: %zu threads not created and ~%zu objects will be ignored\n",
				proc->pid, nthreads - ncreated,
				(nthreads - ncreated) * nobjects);

	for (i = 0; i < ncreated; i++) {
		thread_t *th = &threads[i];

		rc = pthread_join(th->tid, (void *) &th->retval);
		if (rc) {
			errno = rc;
			err(EXIT_FAILURE, "pthread_join failed");
		}

		LOG(2, (stderr, "%d: thread exited [tid=%jd,return=%d]\n",
		     proc->pid, (intmax_t) (intptr_t) th->tid, th->retval));
	}

	free(threads);
}

static void create_nprocesses(void)
{
	process_t *process;
	size_t i;

	process = xcalloc(nprocesses, sizeof(process_t));

	for (i = 0; i < nprocesses; i++) {
		process_t *proc = &process[i];

		proc->pid = fork();
		switch (proc->pid) {
		case -1: /* error */
			err(EXIT_FAILURE, "fork failed");
			break;
		case 0: /* child */
			proc->pid = getpid();
			create_nthreads(proc, i * nthreads * nobjects);
			exit(EXIT_SUCCESS);
			break;
		default: /* parent */
			LOG(2, (stderr, "started process [pid=%d]\n", proc->pid));
			break;
		}
	}

	for (i = 0; i < nprocesses; i++) {
		process_t *proc = &process[i];

		if (waitpid(proc->pid, &proc->status, 0) == (pid_t) - 1)
			err(EXIT_FAILURE, "waitpid failed");
		LOG(2,
		    (stderr, "process exited [pid=%d,status=%d]\n",
		     proc->pid, proc->status));
	}

	free(process);
}

static void object_dump(size_t idx, object_t *obj)
{
	char uuid_string[UUID_STR_LEN], *p;

	p = uuid_string;
	object_uuid_to_string(obj, &p);

	fprintf(stderr, "object[%zu]: {\n", idx);
	fprintf(stderr, "  uuid:    <%s>\n", p);
	fprintf(stderr, "  idx:     %zu\n", obj->idx);
	fprintf(stderr, "  process: %d\n", (int) obj->pid);
	fprintf(stderr, "  thread:  %jd\n", (intmax_t) (intptr_t) obj->tid);
	fprintf(stderr, "}\n");
}

#define MSG_TRY_HELP "Try '-h' for help."

int main(int argc, char *argv[])
{
	size_t i, nfailed = 0, nignored = 0;
	int c;

	while (((c = getopt(argc, argv, "p:t:o:l:h")) != -1)) {
		switch (c) {
		case 'p':
			nprocesses = strtou32_or_err(optarg, "invalid nprocesses number argument");
			break;
		case 't':
			nthreads = strtou32_or_err(optarg, "invalid nthreads number argument");
			break;
		case 'o':
			nobjects = strtou32_or_err(optarg, "invalid nobjects number argument");
			break;
		case 'l':
			loglev = strtou32_or_err(optarg, "invalid log level argument");
			break;
		case 'h':
			usage();
			break;
		default:
			fprintf(stderr, MSG_TRY_HELP);
			exit(EXIT_FAILURE);
		}
	}

	if (optind != argc)
		errx(EXIT_FAILURE, "bad usage\n" MSG_TRY_HELP);

	if (loglev == 1)
		fprintf(stderr, "requested: %zu processes, %zu threads, %zu objects per thread (%zu objects = %zu bytes)\n",
				nprocesses, nthreads, nobjects,
				nprocesses * nthreads * nobjects,
				nprocesses * nthreads * nobjects * sizeof(object_t));

	allocate_segment(&shmem_id, (void **)&objects,
			 nprocesses * nthreads * nobjects, sizeof(object_t));

	create_nprocesses();

	if (loglev >= 3) {
		for (i = 0; i < nprocesses * nthreads * nobjects; i++)
			object_dump(i, &objects[i]);
	}

	qsort(objects, nprocesses * nthreads * nobjects, sizeof(object_t),
	      object_uuid_compare);

	for (i = 0; i < nprocesses * nthreads * nobjects - 1; i++) {
		object_t *obj1 = &objects[i],
			 *obj2 = &objects[i + 1];

		if (!obj1->tid) {
			LOG(3, (stderr, "ignore unused object #%zu\n", i));
			nignored++;
			continue;
		}

		if (object_uuid_compare(obj1, obj2) == 0) {
			if (loglev >= 1)
				fprintf(stderr, "nobjects #%zu and #%zu have duplicate UUIDs\n",
					i, i + 1);
					object_dump(i, obj1),
					object_dump(i + 1, obj2);
			nfailed++;
		}
	}

	remove_segment(shmem_id, objects);
	if (nignored)
		printf("%zu objects ignored\n", nignored);
	if (!nfailed)
		printf("test successful (no duplicate UUIDs found)\n");
	else
		printf("test failed (found %zu duplicate UUIDs)\n", nfailed);

	return nfailed ? EXIT_FAILURE : EXIT_SUCCESS;
}