]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/test_uuidd.c
dmesg: add --follow-new
[thirdparty/util-linux.git] / misc-utils / test_uuidd.c
1 /*
2 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
3 * Huschaam Hussain <Huschaam.Hussain@hp.com>
4 * TSG Solution Alliances Engineering
5 * SAP Technology Group
6 *
7 * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
8 *
9 *
10 * The test heavily uses shared memory, to enlarge maximal size of shared
11 * segment use:
12 *
13 * echo "4294967295" > /proc/sys/kernel/shmm
14 *
15 * The test is compiled against in-tree libuuid, if you want to test uuidd
16 * installed to the system then make sure that libuuid uses the same socket
17 * like the running uuidd. You can start the uuidd manually, for example:
18 *
19 * uuidd --debug --no-fork --no-pid --socket /run/uuidd/request
20 *
21 * if the $runstatedir (as defined by build-system) is /run. If you want
22 * to overwrite the built-in default then use:
23 *
24 * make uuidd uuidgen runstatedir=/var/run
25 */
26 #include <pthread.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/shm.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34
35 #include "uuid.h"
36 #include "c.h"
37 #include "xalloc.h"
38 #include "strutils.h"
39 #include "nls.h"
40
41 #define LOG(level,args) if (loglev >= level) { fprintf args; }
42
43 static size_t nprocesses = 4;
44 static size_t nthreads = 4;
45 static size_t nobjects = 4096;
46 static size_t loglev = 1;
47
48 struct processentry {
49 pid_t pid;
50 int status;
51 };
52 typedef struct processentry process_t;
53
54 struct threadentry {
55 process_t *proc;
56 pthread_t tid; /* pthread_self() / phtread_create() */
57 pthread_attr_t thread_attr;
58 size_t index; /* index in object[] */
59 int retval; /* pthread exit() */
60 };
61 typedef struct threadentry thread_t;
62
63 /* this is in shared memory, keep it as small as possible */
64 struct objectentry {
65 uuid_t uuid;
66 pthread_t tid;
67 pid_t pid;
68 size_t idx;
69 };
70 typedef struct objectentry object_t;
71
72 static int shmem_id;
73 static object_t *objects;
74
75
76 static void __attribute__((__noreturn__)) usage(void)
77 {
78 printf("\n %s [options]\n", program_invocation_short_name);
79
80 printf(" -p <num> number of nprocesses (default:%zu)\n", nprocesses);
81 printf(" -t <num> number of nthreads (default:%zu)\n", nthreads);
82 printf(" -o <num> number of nobjects (default:%zu)\n", nobjects);
83 printf(" -l <level> log level (default:%zu)\n", loglev);
84 printf(" -h display help\n");
85
86 exit(EXIT_SUCCESS);
87 }
88
89 static void allocate_segment(int *id, void **address, size_t number, size_t size)
90 {
91 *id = shmget(IPC_PRIVATE, number * size, IPC_CREAT | 0600);
92 if (*id == -1)
93 err(EXIT_FAILURE, "shmget failed to create %zu bytes shared memory", number * size);
94
95 *address = shmat(*id, NULL, 0);
96 if (*address == (void *)-1)
97 err(EXIT_FAILURE, "shmat failed");
98
99 LOG(2, (stderr,
100 "allocate shared memory segment [id=%d,address=0x%p]\n",
101 *id, *address));
102
103 memset(*address, 0, number * size);
104 }
105
106 static void remove_segment(int id, void *address)
107 {
108 if (shmdt(address) == -1)
109 err(EXIT_FAILURE, "shmdt failed");
110 if (shmctl(id, IPC_RMID, NULL) == -1)
111 err(EXIT_FAILURE, "shmctl failed");
112 LOG(2,
113 (stderr,
114 "remove shared memory segment [id=%d,address=0x%p]\n",
115 id, address));
116 }
117
118 static void object_uuid_create(object_t * object)
119 {
120 uuid_generate_time(object->uuid);
121 }
122
123 static void object_uuid_to_string(object_t * object, char **string_uuid)
124 {
125 uuid_unparse(object->uuid, *string_uuid);
126 }
127
128 static int object_uuid_compare(const void *object1, const void *object2)
129 {
130 uuid_t *uuid1 = &((object_t *) object1)->uuid,
131 *uuid2 = &((object_t *) object2)->uuid;
132
133 return uuid_compare(*uuid1, *uuid2);
134 }
135
136 static void *create_uuids(thread_t *th)
137 {
138 size_t i;
139
140 for (i = th->index; i < th->index + nobjects; i++) {
141 object_t *obj = &objects[i];
142
143 object_uuid_create(obj);
144 obj->tid = th->tid;
145 obj->pid = th->proc->pid;
146 obj->idx = th->index + i;
147 }
148 return NULL;
149 }
150
151 static void *thread_body(void *arg)
152 {
153 thread_t *th = (thread_t *) arg;
154
155 return create_uuids(th);
156 }
157
158 static void create_nthreads(process_t *proc, size_t index)
159 {
160 thread_t *threads;
161 size_t i, ncreated = 0;
162 int rc;
163
164 threads = xcalloc(nthreads, sizeof(thread_t));
165
166 for (i = 0; i < nthreads; i++) {
167 thread_t *th = &threads[i];
168
169 rc = pthread_attr_init(&th->thread_attr);
170 if (rc) {
171 errno = rc;
172 warn("%d: pthread_attr_init failed", proc->pid);
173 break;
174 }
175
176 th->index = index;
177 th->proc = proc;
178 rc = pthread_create(&th->tid, &th->thread_attr, &thread_body, th);
179
180 if (rc) {
181 errno = rc;
182 warn("%d: pthread_create failed", proc->pid);
183 break;
184 }
185
186 LOG(2, (stderr, "%d: started thread [tid=%jd,index=%zu]\n",
187 proc->pid, (intmax_t) th->tid, th->index));
188 index += nobjects;
189 ncreated++;
190 }
191
192 if (ncreated != nthreads)
193 fprintf(stderr, "%d: %zu threads not created and ~%zu objects will be ignored\n",
194 proc->pid, nthreads - ncreated,
195 (nthreads - ncreated) * nobjects);
196
197 for (i = 0; i < ncreated; i++) {
198 thread_t *th = &threads[i];
199
200 rc = pthread_join(th->tid, (void *) &th->retval);
201 if (rc) {
202 errno = rc;
203 err(EXIT_FAILURE, "pthread_join failed");
204 }
205
206 LOG(2, (stderr, "%d: thread exited [tid=%jd,return=%d]\n",
207 proc->pid, (intmax_t) th->tid, th->retval));
208 }
209 }
210
211 static void create_nprocesses(void)
212 {
213 process_t *process;
214 size_t i;
215
216 process = xcalloc(nprocesses, sizeof(process_t));
217
218 for (i = 0; i < nprocesses; i++) {
219 process_t *proc = &process[i];
220
221 proc->pid = fork();
222 switch (proc->pid) {
223 case -1: /* error */
224 err(EXIT_FAILURE, "fork failed");
225 break;
226 case 0: /* child */
227 proc->pid = getpid();
228 create_nthreads(proc, i * nthreads * nobjects);
229 exit(EXIT_SUCCESS);
230 break;
231 default: /* parent */
232 LOG(2, (stderr, "started process [pid=%d]\n", proc->pid));
233 break;
234 }
235 }
236
237 for (i = 0; i < nprocesses; i++) {
238 process_t *proc = &process[i];
239
240 if (waitpid(proc->pid, &proc->status, 0) == (pid_t) - 1)
241 err(EXIT_FAILURE, "waitpid failed");
242 LOG(2,
243 (stderr, "process exited [pid=%d,status=%d]\n",
244 proc->pid, proc->status));
245 }
246 }
247
248 static void object_dump(size_t idx, object_t *obj)
249 {
250 char uuid_string[UUID_STR_LEN], *p;
251
252 p = uuid_string;
253 object_uuid_to_string(obj, &p);
254
255 fprintf(stderr, "object[%zu]: {\n", idx);
256 fprintf(stderr, " uuid: <%s>\n", p);
257 fprintf(stderr, " idx: %zu\n", obj->idx);
258 fprintf(stderr, " process: %d\n", (int) obj->pid);
259 fprintf(stderr, " thread: %jd\n", (intmax_t) obj->tid);
260 fprintf(stderr, "}\n");
261 }
262
263 #define MSG_TRY_HELP "Try '-h' for help."
264
265 int main(int argc, char *argv[])
266 {
267 size_t i, nfailed = 0, nignored = 0;
268 int c;
269
270 while (((c = getopt(argc, argv, "p:t:o:l:h")) != -1)) {
271 switch (c) {
272 case 'p':
273 nprocesses = strtou32_or_err(optarg, "invalid nprocesses number argument");
274 break;
275 case 't':
276 nthreads = strtou32_or_err(optarg, "invalid nthreads number argument");
277 break;
278 case 'o':
279 nobjects = strtou32_or_err(optarg, "invalid nobjects number argument");
280 break;
281 case 'l':
282 loglev = strtou32_or_err(optarg, "invalid log level argument");
283 break;
284 case 'h':
285 usage();
286 break;
287 default:
288 fprintf(stderr, MSG_TRY_HELP);
289 exit(EXIT_FAILURE);
290 }
291 }
292
293 if (optind != argc)
294 errx(EXIT_FAILURE, "bad usage\n" MSG_TRY_HELP);
295
296 if (loglev == 1)
297 fprintf(stderr, "requested: %zu processes, %zu threads, %zu objects per thread (%zu objects = %zu bytes)\n",
298 nprocesses, nthreads, nobjects,
299 nprocesses * nthreads * nobjects,
300 nprocesses * nthreads * nobjects * sizeof(object_t));
301
302 allocate_segment(&shmem_id, (void **)&objects,
303 nprocesses * nthreads * nobjects, sizeof(object_t));
304
305 create_nprocesses();
306
307 if (loglev >= 3) {
308 for (i = 0; i < nprocesses * nthreads * nobjects; i++)
309 object_dump(i, &objects[i]);
310 }
311
312 qsort(objects, nprocesses * nthreads * nobjects, sizeof(object_t),
313 object_uuid_compare);
314
315 for (i = 0; i < nprocesses * nthreads * nobjects - 1; i++) {
316 object_t *obj1 = &objects[i],
317 *obj2 = &objects[i + 1];
318
319 if (!obj1->tid) {
320 LOG(3, (stderr, "ignore unused object #%zu\n", i));
321 nignored++;
322 continue;
323 }
324
325 if (object_uuid_compare(obj1, obj2) == 0) {
326 if (loglev >= 1)
327 fprintf(stderr, "nobjects #%zu and #%zu have duplicate UUIDs\n",
328 i, i + 1);
329 object_dump(i, obj1),
330 object_dump(i + 1, obj2);
331 nfailed++;
332 }
333 }
334
335 remove_segment(shmem_id, objects);
336 if (nignored)
337 printf("%zu objects ignored\n", nignored);
338 if (!nfailed)
339 printf("test successful (no duplicate UUIDs found)\n");
340 else
341 printf("test failed (found %zu duplicate UUIDs)\n", nfailed);
342
343 return nfailed ? EXIT_FAILURE : EXIT_SUCCESS;
344 }