]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/concurrent.c
Only include execinfo.h if MUTEX_DEBUGGING is set
[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 <limits.h>
20 #include <stdio.h>
21 #include <pthread.h>
22 #include <unistd.h>
23 #define _GNU_SOURCE
24 #include <getopt.h>
25
26 #include <lxc/lxccontainer.h>
27
28 static int nthreads = 5;
29 static int iterations = 1;
30 static int quiet = 0;
31 static int delay = 0;
32 static const char *template = "busybox";
33
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, '?' },
41 { 0, 0, 0, 0 },
42 };
43
44 static 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 }
58
59 struct thread_args {
60 int thread_id;
61 int return_code;
62 char *mode;
63 };
64
65 static void do_function(void *arguments)
66 {
67 char name[NAME_MAX+1];
68 struct thread_args *args = arguments;
69 struct lxc_container *c;
70
71 sprintf(name, "lxc-test-concurrent-%d", args->thread_id);
72
73 args->return_code = 1;
74 c = lxc_container_new(name, NULL);
75 if (!c) {
76 fprintf(stderr, "Unable to instantiate container (%s)\n", name);
77 return;
78 }
79
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);
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 }
94 if (!c->wait(c, "RUNNING", -1)) {
95 fprintf(stderr, "Waiting the container (%s) to start failed...\n", name);
96 goto out;
97 }
98 sleep(delay);
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 }
106 if (!c->wait(c, "STOPPED", -1)) {
107 fprintf(stderr, "Waiting the container (%s) to stop failed...\n", name);
108 goto out;
109 }
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;
120 out:
121 lxc_container_put(c);
122 }
123
124 static void *concurrent(void *arguments)
125 {
126 do_function(arguments);
127 pthread_exit(NULL);
128 }
129
130 int main(int argc, char *argv[]) {
131 int i, j, iter, opt;
132 pthread_attr_t attr;
133 pthread_t *threads;
134 struct thread_args *args;
135
136 char *modes[] = {"create", "start", "stop", "destroy", NULL};
137
138 pthread_attr_init(&attr);
139
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);
160 }
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 }
169
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 }
192 }
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 }
205 }
206 }
207 }
208
209 free(args);
210 free(threads);
211 pthread_attr_destroy(&attr);
212 exit(EXIT_SUCCESS);
213 }