]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/concurrent.c
3 * Copyright © 2013 S.Çağlar Onur <caglar@10ur.org>
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.
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.
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.
29 #include <lxc/lxccontainer.h>
31 static int nthreads
= 5;
32 static int iterations
= 1;
36 static const char *template = "busybox";
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
, '?' },
50 static void usage(void) {
51 fprintf(stderr
, "Usage: lxc-test-concurrent [OPTION]...\n\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"
63 "Mandatory or optional arguments to long options are also mandatory or optional\n"
64 "for any corresponding short options.\n\n");
73 static void do_function(void *arguments
)
75 char name
[NAME_MAX
+ 1];
76 struct thread_args
*args
= arguments
;
77 struct lxc_container
*c
;
79 sprintf(name
, "lxc-test-concurrent-%d", args
->thread_id
);
81 args
->return_code
= 1;
83 c
= lxc_container_new(name
, NULL
);
85 fprintf(stderr
, "Unable to instantiate container (%s)\n", name
);
90 c
->set_config_item(c
, "lxc.log.level", "DEBUG");
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
);
99 } else if(strcmp(args
->mode
, "start") == 0) {
100 if (c
->is_defined(c
) && !c
->is_running(c
)) {
101 c
->want_daemonize(c
, true);
103 if (!c
->start(c
, false, NULL
)) {
104 fprintf(stderr
, "Starting the container (%s) failed...\n", name
);
108 if (!c
->wait(c
, "RUNNING", 15)) {
109 fprintf(stderr
, "Waiting the container (%s) to start failed...\n", name
);
115 } else if(strcmp(args
->mode
, "stop") == 0) {
116 if (c
->is_defined(c
) && c
->is_running(c
)) {
118 fprintf(stderr
, "Stopping the container (%s) failed...\n", name
);
122 if (!c
->wait(c
, "STOPPED", 15)) {
123 fprintf(stderr
, "Waiting the container (%s) to stop failed...\n", name
);
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
);
136 args
->return_code
= 0;
139 lxc_container_put(c
);
145 static void *concurrent(void *arguments
)
147 do_function(arguments
);
153 int main(int argc
, char *argv
[]) {
157 struct thread_args
*args
;
159 char *modes_default
[] = {"create", "start", "stop", "destroy", NULL
};
160 char **modes
= modes_default
;
162 pthread_attr_init(&attr
);
164 while ((opt
= getopt_long(argc
, argv
, "j:i:t:d:m:qD", options
, NULL
)) != -1) {
167 nthreads
= atoi(optarg
);
170 iterations
= atoi(optarg
);
176 delay
= atoi(optarg
);
185 char *mode_tok
, *tok
, *saveptr
= 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));
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
);
219 for (iter
= 1; iter
<= iterations
; iter
++) {
222 fd
= open("/", O_RDONLY
);
224 fprintf(stderr
, "Failed to open /\n");
229 printf("\nIteration %d/%d maxfd:%d\n", iter
, iterations
, fd
);
233 for (i
= 0; modes
[i
];i
++) {
235 printf("Executing (%s) for %d containers...\n", modes
[i
], nthreads
);
237 for (j
= 0; j
< nthreads
; j
++) {
238 args
[j
].thread_id
= j
;
239 args
[j
].mode
= modes
[i
];
242 if (pthread_create(&threads
[j
], &attr
, concurrent
, (void *) &args
[j
]) != 0) {
243 perror("pthread_create() error");
247 do_function(&args
[j
]);
251 for (j
= 0; j
< nthreads
; j
++) {
253 if (pthread_join(threads
[j
], NULL
) != 0) {
254 perror("pthread_join() error");
259 if (args
[j
].return_code
) {
260 fprintf(stderr
, "thread returned error %d\n", args
[j
].return_code
);
269 pthread_attr_destroy(&attr
);