Debian Codegraph @

tests/shm-fork.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/shm.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
28 #include <hugetlbfs.h>
29 #include "hugetests.h"
30
31 #define P "shm-fork"
32 #define DESC \
33 "* Test shared memory behavior when multiple threads are attached *\n"\
34 "* to a segment. A segment is created and then children are *\n"\
35 "* spawned which attach, write, read (verify), and detach from the *\n"\
36 "* shared memory segment. *"
37
38 extern int errno;
39
40 /* Global Configuration */
41 static int nr_hugepages;
42 static int numprocs;
43 static int shmid = -1;
44
45 #define MAX_PROCS 200
46 #define BUF_SZ 256
47
48 #define CHILD_FAIL(thread, fmt, ...) \
49 do { \
50 verbose_printf("Thread %d (pid=%d) FAIL: " fmt, \
51 thread, getpid(), __VA_ARGS__); \
52 exit(1); \
53 } while (0)
54
55 void cleanup(void)
56 {
57 remove_shmid(shmid);
58 }
59
60 static void do_child(int thread, unsigned long size)
61 {
62 volatile char *shmaddr;
63 int j;
64 unsigned long k;
65
66 verbose_printf(".");
67 for (j=0; j<5; j++) {
68 shmaddr = shmat(shmid, 0, SHM_RND);
69 if (shmaddr == MAP_FAILED)
70 CHILD_FAIL(thread, "shmat() failed: %s",
71 strerror(errno));
72
73 for (k=0;k<size;k++)
74 shmaddr[k] = (char) (k);
75 for (k=0;k<size;k++)
76 if (shmaddr[k] != (char)k)
77 CHILD_FAIL(thread, "Index %lu mismatch", k);
78
79 if (shmdt((const void *)shmaddr) != 0)
80 CHILD_FAIL(thread, "shmdt() failed: %s",
81 strerror(errno));
82 }
83 exit(0);
84 }
85
86 int main(int argc, char ** argv)
87 {
88 unsigned long size;
89 long hpage_size;
90 int pid, status;
91 int i;
92 int wait_list[MAX_PROCS];
93
94 test_init(argc, argv);
95
96 if (argc < 3)
97 CONFIG("Usage: %s <# procs> <# pages>", argv[0]);
98
99 numprocs = atoi(argv[1]);
100 nr_hugepages = atoi(argv[2]);
101
102 if (numprocs > MAX_PROCS)
103 CONFIG("Cannot spawn more than %d processes", MAX_PROCS);
104
105 check_hugetlb_shm_group();
106
107 hpage_size = check_hugepagesize();
108 size = hpage_size * nr_hugepages;
109 verbose_printf("Requesting %lu bytes\n", size);
110 if ((shmid = shmget(2, size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W )) < 0)
111 FAIL("shmget(): %s", strerror(errno));
112
113 verbose_printf("shmid: %d\n", shmid);
114
115 verbose_printf("Spawning children:\n");
116 for (i=0; i<numprocs; i++) {
117 if ((pid = fork()) < 0)
118 FAIL("fork(): %s", strerror(errno));
119
120 if (pid == 0)
121 do_child(i, size);
122
123 wait_list[i] = pid;
124 }
125
126 for (i=0; i<numprocs; i++) {
127 waitpid(wait_list[i], &status, 0);
128 if (WEXITSTATUS(status) != 0)
129 FAIL("Thread %d (pid=%d) failed", i, wait_list[i]);
130
131 if (WIFSIGNALED(status))
132 FAIL("Thread %d (pid=%d) received unhandled signal",
133 i, wait_list[i]);
134 }
135
136 PASS();
137 }