]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/concurrent.c
Only include execinfo.h if MUTEX_DEBUGGING is set
[mirror_lxc.git] / src / tests / concurrent.c
CommitLineData
f209d63a
ÇO
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 */
beb6d93e
DE
18
19#include <limits.h>
f209d63a
ÇO
20#include <stdio.h>
21#include <pthread.h>
beb6d93e
DE
22#include <unistd.h>
23#define _GNU_SOURCE
24#include <getopt.h>
f209d63a 25
948955a2 26#include <lxc/lxccontainer.h>
f209d63a 27
beb6d93e
DE
28static int nthreads = 5;
29static int iterations = 1;
30static int quiet = 0;
31static int delay = 0;
32static const char *template = "busybox";
33
34static 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, '?' },
41 { 0, 0, 0, 0 },
42};
f209d63a 43
beb6d93e
DE
44static void usage(void) {
45 fprintf(stderr, "Usage: lxc-test-concurrent [OPTION]...\n\n"
46 "Common options :\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"
54 "\n"
55 "Mandatory or optional arguments to long options are also mandatory or optional\n"
56 "for any corresponding short options.\n\n");
57}
103a2fc0 58
f209d63a
ÇO
59struct thread_args {
60 int thread_id;
61 int return_code;
62 char *mode;
63};
64
beb6d93e
DE
65static void do_function(void *arguments)
66{
67 char name[NAME_MAX+1];
f209d63a
ÇO
68 struct thread_args *args = arguments;
69 struct lxc_container *c;
70
beb6d93e 71 sprintf(name, "lxc-test-concurrent-%d", args->thread_id);
f209d63a 72
beb6d93e 73 args->return_code = 1;
f209d63a 74 c = lxc_container_new(name, NULL);
beb6d93e
DE
75 if (!c) {
76 fprintf(stderr, "Unable to instantiate container (%s)\n", name);
77 return;
78 }
f209d63a 79
f209d63a
ÇO
80 if (strcmp(args->mode, "create") == 0) {
81 if (!c->is_defined(c)) {
103a2fc0 82 if (!c->create(c, template, NULL, NULL, 1, NULL)) {
f209d63a
ÇO
83 fprintf(stderr, "Creating the container (%s) failed...\n", name);
84 goto out;
85 }
86 }
87 } else if(strcmp(args->mode, "start") == 0) {
88 if (c->is_defined(c) && !c->is_running(c)) {
89 c->want_daemonize(c);
90 if (!c->start(c, false, NULL)) {
91 fprintf(stderr, "Starting the container (%s) failed...\n", name);
92 goto out;
93 }
b130964d
ÇO
94 if (!c->wait(c, "RUNNING", -1)) {
95 fprintf(stderr, "Waiting the container (%s) to start failed...\n", name);
96 goto out;
97 }
beb6d93e 98 sleep(delay);
f209d63a
ÇO
99 }
100 } else if(strcmp(args->mode, "stop") == 0) {
101 if (c->is_defined(c) && c->is_running(c)) {
102 if (!c->stop(c)) {
103 fprintf(stderr, "Stopping the container (%s) failed...\n", name);
104 goto out;
105 }
b130964d
ÇO
106 if (!c->wait(c, "STOPPED", -1)) {
107 fprintf(stderr, "Waiting the container (%s) to stop failed...\n", name);
108 goto out;
109 }
f209d63a
ÇO
110 }
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);
115 goto out;
116 }
117 }
118 }
119 args->return_code = 0;
120out:
121 lxc_container_put(c);
f209d63a
ÇO
122}
123
beb6d93e
DE
124static void *concurrent(void *arguments)
125{
126 do_function(arguments);
127 pthread_exit(NULL);
128}
f209d63a
ÇO
129
130int main(int argc, char *argv[]) {
beb6d93e 131 int i, j, iter, opt;
f209d63a 132 pthread_attr_t attr;
beb6d93e
DE
133 pthread_t *threads;
134 struct thread_args *args;
f209d63a
ÇO
135
136 char *modes[] = {"create", "start", "stop", "destroy", NULL};
137
138 pthread_attr_init(&attr);
f209d63a 139
beb6d93e
DE
140 while ((opt = getopt_long(argc, argv, "j:i:t:d:q", options, NULL)) != -1) {
141 switch(opt) {
142 case 'j':
143 nthreads = atoi(optarg);
144 break;
145 case 'i':
146 iterations = atoi(optarg);
147 break;
148 case 't':
149 template = optarg;
150 break;
151 case 'd':
152 delay = atoi(optarg);
153 break;
154 case 'q':
155 quiet = 1;
156 break;
157 default: /* '?' */
158 usage();
159 exit(EXIT_FAILURE);
f209d63a 160 }
beb6d93e
DE
161 }
162
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);
167 exit(EXIT_FAILURE);
168 }
f209d63a 169
beb6d93e
DE
170 for (iter = 1; iter <= iterations; iter++) {
171 int fd;
172 fd = open("/", O_RDONLY);
173 if (!quiet)
174 printf("\nIteration %d/%d maxfd:%d\n", iter, iterations, fd);
175 close(fd);
176
177 for (i = 0; modes[i];i++) {
178 if (!quiet)
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];
183
184 if (nthreads > 1) {
185 if (pthread_create(&threads[j], &attr, concurrent, (void *) &args[j]) != 0) {
186 perror("pthread_create() error");
187 exit(EXIT_FAILURE);
188 }
189 } else {
190 do_function(&args[j]);
191 }
f209d63a 192 }
beb6d93e
DE
193
194 for (j = 0; j < nthreads; j++) {
195 if (nthreads > 1) {
196 if (pthread_join(threads[j], NULL) != 0) {
197 perror("pthread_join() error");
198 exit(EXIT_FAILURE);
199 }
200 }
201 if (args[j].return_code) {
202 fprintf(stderr, "thread returned error %d", args[j].return_code);
203 exit(EXIT_FAILURE);
204 }
f209d63a
ÇO
205 }
206 }
f209d63a
ÇO
207 }
208
beb6d93e
DE
209 free(args);
210 free(threads);
f209d63a
ÇO
211 pthread_attr_destroy(&attr);
212 exit(EXIT_SUCCESS);
213}