]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/concurrent.c
tests: include config.h
[mirror_lxc.git] / src / tests / concurrent.c
1 /* concurrent.c
2 *
3 * Copyright © 2013 S.Çağlar Onur <caglar@10ur.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "config.h"
20
21 #include <limits.h>
22 #include <stdio.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <getopt.h>
28
29 #include <lxc/lxccontainer.h>
30
31 static int nthreads = 5;
32 static int iterations = 1;
33 static int debug = 0;
34 static int quiet = 0;
35 static int delay = 0;
36 static const char *template = "busybox";
37
38 static const struct option options[] = {
39 { "threads", required_argument, NULL, 'j' },
40 { "iterations", required_argument, NULL, 'i' },
41 { "template", required_argument, NULL, 't' },
42 { "delay", required_argument, NULL, 'd' },
43 { "modes", required_argument, NULL, 'm' },
44 { "quiet", no_argument, NULL, 'q' },
45 { "debug", no_argument, NULL, 'D' },
46 { "help", no_argument, NULL, '?' },
47 { 0, 0, 0, 0 },
48 };
49
50 static void usage(void) {
51 fprintf(stderr, "Usage: lxc-test-concurrent [OPTION]...\n\n"
52 "Common options :\n"
53 " -j, --threads=N Threads to run concurrently\n"
54 " (default: 5, use 1 for no threading)\n"
55 " -i, --iterations=N Number times to run the test (default: 1)\n"
56 " -t, --template=t Template to use (default: busybox)\n"
57 " -d, --delay=N Delay in seconds between start and stop\n"
58 " -m, --modes=<mode,mode,...> Modes to run (create, start, stop, destroy)\n"
59 " -q, --quiet Don't produce any output\n"
60 " -D, --debug Create a debug log\n"
61 " -?, --help Give this help list\n"
62 "\n"
63 "Mandatory or optional arguments to long options are also mandatory or optional\n"
64 "for any corresponding short options.\n\n");
65 }
66
67 struct thread_args {
68 int thread_id;
69 int return_code;
70 const char *mode;
71 };
72
73 static void do_function(void *arguments)
74 {
75 char name[NAME_MAX + 1];
76 struct thread_args *args = arguments;
77 struct lxc_container *c;
78
79 sprintf(name, "lxc-test-concurrent-%d", args->thread_id);
80
81 args->return_code = 1;
82
83 c = lxc_container_new(name, NULL);
84 if (!c) {
85 fprintf(stderr, "Unable to instantiate container (%s)\n", name);
86 return;
87 }
88
89 if (debug)
90 c->set_config_item(c, "lxc.log.level", "DEBUG");
91
92 if (strcmp(args->mode, "create") == 0) {
93 if (!c->is_defined(c)) {
94 if (!c->create(c, template, NULL, NULL, 1, NULL)) {
95 fprintf(stderr, "Creating the container (%s) failed...\n", name);
96 goto out;
97 }
98 }
99 } else if(strcmp(args->mode, "start") == 0) {
100 if (c->is_defined(c) && !c->is_running(c)) {
101 c->want_daemonize(c, true);
102
103 if (!c->start(c, false, NULL)) {
104 fprintf(stderr, "Starting the container (%s) failed...\n", name);
105 goto out;
106 }
107
108 if (!c->wait(c, "RUNNING", 15)) {
109 fprintf(stderr, "Waiting the container (%s) to start failed...\n", name);
110 goto out;
111 }
112
113 sleep(delay);
114 }
115 } else if(strcmp(args->mode, "stop") == 0) {
116 if (c->is_defined(c) && c->is_running(c)) {
117 if (!c->stop(c)) {
118 fprintf(stderr, "Stopping the container (%s) failed...\n", name);
119 goto out;
120 }
121
122 if (!c->wait(c, "STOPPED", 15)) {
123 fprintf(stderr, "Waiting the container (%s) to stop failed...\n", name);
124 goto out;
125 }
126 }
127 } else if(strcmp(args->mode, "destroy") == 0) {
128 if (c->is_defined(c) && !c->is_running(c)) {
129 if (!c->destroy(c)) {
130 fprintf(stderr, "Destroying the container (%s) failed...\n", name);
131 goto out;
132 }
133 }
134 }
135
136 args->return_code = 0;
137
138 out:
139 lxc_container_put(c);
140
141 if (debug)
142 lxc_log_close();
143 }
144
145 static void *concurrent(void *arguments)
146 {
147 do_function(arguments);
148 pthread_exit(NULL);
149
150 return NULL;
151 }
152
153 int main(int argc, char *argv[]) {
154 int i, j, iter, opt;
155 pthread_attr_t attr;
156 pthread_t *threads;
157 struct thread_args *args;
158
159 char *modes_default[] = {"create", "start", "stop", "destroy", NULL};
160 char **modes = modes_default;
161
162 pthread_attr_init(&attr);
163
164 while ((opt = getopt_long(argc, argv, "j:i:t:d:m:qD", options, NULL)) != -1) {
165 switch(opt) {
166 case 'j':
167 nthreads = atoi(optarg);
168 break;
169 case 'i':
170 iterations = atoi(optarg);
171 break;
172 case 't':
173 template = optarg;
174 break;
175 case 'd':
176 delay = atoi(optarg);
177 break;
178 case 'q':
179 quiet = 1;
180 break;
181 case 'D':
182 debug = 1;
183 break;
184 case 'm': {
185 char *mode_tok, *tok, *saveptr = NULL;
186
187 if (!optarg)
188 continue;
189
190 modes = NULL;
191 for (i = 0, mode_tok = optarg;
192 (tok = strtok_r(mode_tok, ",", &saveptr));
193 i++, mode_tok = NULL) {
194 modes = realloc(modes, sizeof(*modes) * (i+2));
195 if (!modes) {
196 perror("realloc");
197 exit(EXIT_FAILURE);
198 }
199 modes[i] = tok;
200 }
201
202 if (modes)
203 modes[i] = NULL;
204 break;
205 }
206 default: /* '?' */
207 usage();
208 exit(EXIT_FAILURE);
209 }
210 }
211
212 threads = malloc(sizeof(*threads) * nthreads);
213 args = malloc(sizeof(*args) * nthreads);
214 if (threads == NULL || args == NULL) {
215 fprintf(stderr, "Unable malloc enough memory for %d threads\n", nthreads);
216 exit(EXIT_FAILURE);
217 }
218
219 for (iter = 1; iter <= iterations; iter++) {
220 int fd;
221
222 fd = open("/", O_RDONLY);
223 if (fd < 0) {
224 fprintf(stderr, "Failed to open /\n");
225 continue;
226 }
227
228 if (!quiet)
229 printf("\nIteration %d/%d maxfd:%d\n", iter, iterations, fd);
230
231 close(fd);
232
233 for (i = 0; modes[i];i++) {
234 if (!quiet)
235 printf("Executing (%s) for %d containers...\n", modes[i], nthreads);
236
237 for (j = 0; j < nthreads; j++) {
238 args[j].thread_id = j;
239 args[j].mode = modes[i];
240
241 if (nthreads > 1) {
242 if (pthread_create(&threads[j], &attr, concurrent, (void *) &args[j]) != 0) {
243 perror("pthread_create() error");
244 exit(EXIT_FAILURE);
245 }
246 } else {
247 do_function(&args[j]);
248 }
249 }
250
251 for (j = 0; j < nthreads; j++) {
252 if (nthreads > 1) {
253 if (pthread_join(threads[j], NULL) != 0) {
254 perror("pthread_join() error");
255 exit(EXIT_FAILURE);
256 }
257 }
258
259 if (args[j].return_code) {
260 fprintf(stderr, "thread returned error %d\n", args[j].return_code);
261 exit(EXIT_FAILURE);
262 }
263 }
264 }
265 }
266
267 free(args);
268 free(threads);
269 pthread_attr_destroy(&attr);
270 exit(EXIT_SUCCESS);
271 }