concurrent.c revision 540f932aeb28274e8e7ea1e8f3a8e5889b88e1d6
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher * Copyright © 2013 S.Çağlar Onur <caglar@10ur.org>
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher * This program is free software; you can redistribute it and/or modify
c252d148fa8ab50aaaa8bbae7beb4d208025171dNikolai Kondrashov * it under the terms of the GNU General Public License version 2, as
9542512d7be40f2000298c86d3d2b728f4f0f65aStephen Gallagher * published by the Free Software Foundation.
9542512d7be40f2000298c86d3d2b728f4f0f65aStephen Gallagher * This program is distributed in the hope that it will be useful,
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov * but WITHOUT ANY WARRANTY; without even the implied warranty of
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov * GNU General Public License for more details.
c6e39e15178675d0779e0ae855245774a09b4eb5Nikolai Kondrashov * You should have received a copy of the GNU General Public License along
fd5a4eacd56700ffb08a73121aeacdc806cb0132Sumit Bose * with this program; if not, write to the Free Software Foundation, Inc.,
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic int quiet = 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic int delay = 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { "threads", required_argument, NULL, 'j' },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { "iterations", required_argument, NULL, 'i' },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { "template", required_argument, NULL, 't' },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { "delay", required_argument, NULL, 'd' },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { "modes", required_argument, NULL, 'm' },
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose { 0, 0, 0, 0 },
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bosestatic void usage(void) {
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke fprintf(stderr, "Usage: lxc-test-concurrent [OPTION]...\n\n"
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke "Common options :\n"
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke " -j, --threads=N Threads to run concurrently\n"
f3c85d900c4663854cc7bbae7d9f77867ed1f69bSumit Bose " (default: 5, use 1 for no threading)\n"
f3c85d900c4663854cc7bbae7d9f77867ed1f69bSumit Bose " -i, --iterations=N Number times to run the test (default: 1)\n"
f3c85d900c4663854cc7bbae7d9f77867ed1f69bSumit Bose " -t, --template=t Template to use (default: busybox)\n"
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher " -d, --delay=N Delay in seconds between start and stop\n"
2a552e43581c74f51205c7141ec9f6e9542509f8Stephen Gallagher " -m, --modes=<mode,mode,...> Modes to run (create, start, stop, destroy)\n"
2a552e43581c74f51205c7141ec9f6e9542509f8Stephen Gallagher " -q, --quiet Don't produce any output\n"
8214510f125879c3b1d247f2ce981ee20b5375d1Jakub Hrozek " -?, --help Give this help list\n"
a5bb518446d5ce565d7ba819590a009cabb0b0b4Jakub Hrozek "Mandatory or optional arguments to long options are also mandatory or optional\n"
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher "for any corresponding short options.\n\n");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher sprintf(name, "lxc-test-concurrent-%d", args->thread_id);
e124844907ed6973915e4d56f5442ecd07535a12Jakub Hrozek fprintf(stderr, "Unable to instantiate container (%s)\n", name);
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek if (!c->create(c, template, NULL, NULL, 1, NULL)) {
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek fprintf(stderr, "Creating the container (%s) failed...\n", name);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else if(strcmp(args->mode, "start") == 0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (c->is_defined(c) && !c->is_running(c)) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher fprintf(stderr, "Starting the container (%s) failed...\n", name);
a3d176d116ceccd6a7547c128fab5df5cdd2c2b6Michal Zidek fprintf(stderr, "Waiting the container (%s) to start failed...\n", name);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else if(strcmp(args->mode, "stop") == 0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (c->is_defined(c) && c->is_running(c)) {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta if (!c->stop(c)) {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta fprintf(stderr, "Stopping the container (%s) failed...\n", name);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta fprintf(stderr, "Waiting the container (%s) to stop failed...\n", name);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else if(strcmp(args->mode, "destroy") == 0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (c->is_defined(c) && !c->is_running(c)) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher fprintf(stderr, "Destroying the container (%s) failed...\n", name);
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta char *modes_default[] = {"create", "start", "stop", "destroy", NULL};
cb4d5b588e704114b7090678752d33512baa718eJakub Hrozek while ((opt = getopt_long(argc, argv, "j:i:t:d:m:q", options, NULL)) != -1) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher (tok = strtok_r(mode_tok, ",", &saveptr));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher modes = realloc(modes, sizeof(*modes) * (i+2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher default: /* '?' */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher threads = malloc(sizeof(*threads) * nthreads);
fe60346714a73ac3987f786731389320633dd245Pavel Březina fprintf(stderr, "Unable malloc enough memory for %d threads\n", nthreads);
71e7918be3ca5d38794a16a17f6b4f19a24d51fcPavel Březina printf("\nIteration %d/%d maxfd:%d\n", iter, iterations, fd);
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina for (i = 0; modes[i];i++) {
bbaba8b3ef9bc101863b8687f234f4ee956caacdPavel Březina printf("Executing (%s) for %d containers...\n", modes[i], nthreads);
bbaba8b3ef9bc101863b8687f234f4ee956caacdPavel Březina for (j = 0; j < nthreads; j++) {
4bd20c075f0f187db0181dc53d00ab6cd47fdb4dJakub Hrozek if (pthread_create(&threads[j], &attr, concurrent, (void *) &args[j]) != 0) {
2a9af1f71887f02935e2fb6ad5023afba5b6d43eSumit Bose for (j = 0; j < nthreads; j++) {
f69f3581658351003a6d9245045e41d0efb85022Sumit Bose fprintf(stderr, "thread returned error %d", args[j].return_code);