]>
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.
26 #include <lxc/lxccontainer.h>
28 static int nthreads
= 5;
29 static int iterations
= 1;
32 static const char *template = "busybox";
34 static struct option options
[] = {
35 { "threads", required_argument
, NULL
, 'j' },
36 { "iterations", required_argument
, NULL
, 'i' },
37 { "template", required_argument
, NULL
, 't' },
38 { "delay", required_argument
, NULL
, 'd' },
39 { "quiet", no_argument
, NULL
, 'q' },
40 { "help", no_argument
, NULL
, '?' },
44 static void usage(void) {
45 fprintf(stderr
, "Usage: lxc-test-concurrent [OPTION]...\n\n"
47 " -j, --threads=N Threads to run concurrently\n"
48 " (default: 5, use 1 for no threading)\n"
49 " -i, --iterations=N Number times to run the test (default: 1)\n"
50 " -t, --template=t Template to use (default: busybox)\n"
51 " -d, --delay=N Delay in seconds between start and stop\n"
52 " -q, --quiet Don't produce any output\n"
53 " -?, --help Give this help list\n"
55 "Mandatory or optional arguments to long options are also mandatory or optional\n"
56 "for any corresponding short options.\n\n");
65 static void do_function(void *arguments
)
67 char name
[NAME_MAX
+1];
68 struct thread_args
*args
= arguments
;
69 struct lxc_container
*c
;
71 sprintf(name
, "lxc-test-concurrent-%d", args
->thread_id
);
73 args
->return_code
= 1;
74 c
= lxc_container_new(name
, NULL
);
76 fprintf(stderr
, "Unable to instantiate container (%s)\n", name
);
80 if (strcmp(args
->mode
, "create") == 0) {
81 if (!c
->is_defined(c
)) {
82 if (!c
->create(c
, template, NULL
, NULL
, 1, NULL
)) {
83 fprintf(stderr
, "Creating the container (%s) failed...\n", name
);
87 } else if(strcmp(args
->mode
, "start") == 0) {
88 if (c
->is_defined(c
) && !c
->is_running(c
)) {
90 if (!c
->start(c
, false, NULL
)) {
91 fprintf(stderr
, "Starting the container (%s) failed...\n", name
);
94 if (!c
->wait(c
, "RUNNING", -1)) {
95 fprintf(stderr
, "Waiting the container (%s) to start failed...\n", name
);
100 } else if(strcmp(args
->mode
, "stop") == 0) {
101 if (c
->is_defined(c
) && c
->is_running(c
)) {
103 fprintf(stderr
, "Stopping the container (%s) failed...\n", name
);
106 if (!c
->wait(c
, "STOPPED", -1)) {
107 fprintf(stderr
, "Waiting the container (%s) to stop failed...\n", name
);
111 } else if(strcmp(args
->mode
, "destroy") == 0) {
112 if (c
->is_defined(c
) && !c
->is_running(c
)) {
113 if (!c
->destroy(c
)) {
114 fprintf(stderr
, "Destroying the container (%s) failed...\n", name
);
119 args
->return_code
= 0;
121 lxc_container_put(c
);
124 static void *concurrent(void *arguments
)
126 do_function(arguments
);
130 int main(int argc
, char *argv
[]) {
134 struct thread_args
*args
;
136 char *modes
[] = {"create", "start", "stop", "destroy", NULL
};
138 pthread_attr_init(&attr
);
140 while ((opt
= getopt_long(argc
, argv
, "j:i:t:d:q", options
, NULL
)) != -1) {
143 nthreads
= atoi(optarg
);
146 iterations
= atoi(optarg
);
152 delay
= atoi(optarg
);
163 threads
= malloc(sizeof(*threads
) * nthreads
);
164 args
= malloc(sizeof(*args
) * nthreads
);
165 if (threads
== NULL
|| args
== NULL
) {
166 fprintf(stderr
, "Unable malloc enough memory for %d threads\n", nthreads
);
170 for (iter
= 1; iter
<= iterations
; iter
++) {
172 fd
= open("/", O_RDONLY
);
174 printf("\nIteration %d/%d maxfd:%d\n", iter
, iterations
, fd
);
177 for (i
= 0; modes
[i
];i
++) {
179 printf("Executing (%s) for %d containers...\n", modes
[i
], nthreads
);
180 for (j
= 0; j
< nthreads
; j
++) {
181 args
[j
].thread_id
= j
;
182 args
[j
].mode
= modes
[i
];
185 if (pthread_create(&threads
[j
], &attr
, concurrent
, (void *) &args
[j
]) != 0) {
186 perror("pthread_create() error");
190 do_function(&args
[j
]);
194 for (j
= 0; j
< nthreads
; j
++) {
196 if (pthread_join(threads
[j
], NULL
) != 0) {
197 perror("pthread_join() error");
201 if (args
[j
].return_code
) {
202 fprintf(stderr
, "thread returned error %d", args
[j
].return_code
);
211 pthread_attr_destroy(&attr
);