Whamcloud - gitweb
LU-4629 lov: fix sscanf format specification
[fs/lustre-release.git] / lustre / utils / loadgen.c
index e83a298..fbc145e 100644 (file)
@@ -1,30 +1,46 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- *   Copyright (C) 2006 Cluster File Systems, Inc.
- *   Author: Nathan Rutman <nathan@clusterfs.com>
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2013, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/utils/loadgen.c
  *
- * loadgen.c
  * See how many local OSCs we can start whaling on a OST
  * We're doing direct ioctls instead of going though a system() call to lctl
  * to avoid the bash overhead.
  * Adds an osc / echo client pair in each thread and starts echo transactions.
  *
+ * Author: Nathan Rutman <nathan@clusterfs.com>
  */
 
 #include <pthread.h>
 #include <sys/time.h>
 
 #include <lnet/lnetctl.h>
-#include "parser.h"
+#include <libcfs/libcfsutil.h>
+#include <lustre_ioctl.h>
 #include "obdctl.h"
 
 static char cmdname[512];
 static char target[64] = "";
 char nid[64] = "";
-static int live_threads = 0;
 static int sig_received = 0;
 static int o_verbose = 4; /* 0-5 */
 static int my_oss = 0;
@@ -62,7 +78,7 @@ static int jt_quit(int argc, char **argv) {
 static int loadgen_usage(int argc, char **argv)
 {
         if (argc == 1) {
-                fprintf(stderr, 
+                fprintf(stderr,
         "This is a test program used to simulate large numbers of\n"
         "clients.  The echo obds are used, so the obdecho module must\n"
         "be loaded.\n"
@@ -80,6 +96,7 @@ static int loadgen_verbose(int argc, char **argv);
 static int loadgen_target(int argc, char **argv);
 static int loadgen_start_echosrv(int argc, char **argv);
 static int loadgen_start_clients(int argc, char **argv);
+static int loadgen_wait(int argc, char **argv);
 static int loadgen_write(int argc, char **argv);
 
 command_t cmdlist[] = {
@@ -93,6 +110,8 @@ command_t cmdlist[] = {
          "usage: start_clients <num>"},
         {"verbose", loadgen_verbose, 0, "set verbosity level 0-5\n"
          "usage: verbose <level>"},
+        {"wait", loadgen_wait, 0,
+         "wait for all threads to finish\n"},
         {"write", loadgen_write, 0,
          "start a test_brw write test on X clients for Y iterations\n"
          "usage: write <num_clients> <num_iter> [<delay>]"},
@@ -113,7 +132,7 @@ command_t cmdlist[] = {
 
 struct command_t {
         int           c_flags;
-        int           c_count;
+        int           c_rpt;
         int           c_delay;
 };
 
@@ -126,7 +145,9 @@ struct kid_t {
         int              k_dev;
 };
 
+static int live_threads = 0;
 static struct kid_t *kid_list = NULL;
+pthread_mutex_t m_list = PTHREAD_MUTEX_INITIALIZER;
 
 static struct kid_t *push_kid(int tnum)
 {
@@ -137,30 +158,38 @@ static struct kid_t *push_kid(int tnum)
                 return NULL;
         }
         kid->k_pthread = pthread_self();
+        pthread_mutex_lock(&m_list);
         kid->k_next = kid_list;
         kid->k_id = tnum;
         kid_list = kid;
         live_threads++;
+        pthread_mutex_unlock(&m_list);
         return kid;
 }
 
 int trigger_count = 0;
 int waiting_count = 0;
 int timer_on = 0;
+int all_done = 1;
 struct timeval trigger_start;
-struct command_t *trigger_cmd = NULL;
+struct command_t trigger_cmd;
 pthread_mutex_t m_trigger = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t cv_trigger = PTHREAD_COND_INITIALIZER;
 
 unsigned long long write_bytes;
 pthread_mutex_t m_count = PTHREAD_MUTEX_INITIALIZER;
 
-static void trigger(struct command_t *cmd, int count)
+static void trigger(int command, int threads, int repeat, int delay)
 {
 
         pthread_mutex_lock(&m_trigger);
-        trigger_cmd = cmd;
-        trigger_count = count;
+        trigger_cmd.c_flags = command;
+        trigger_cmd.c_rpt = repeat;
+        trigger_cmd.c_delay = delay;
+        trigger_count = threads;
+        if (o_verbose > 4)
+                printf("trigger %d cmd c=%d f=%x\n", trigger_count,
+                       trigger_cmd.c_rpt, trigger_cmd.c_flags);
         gettimeofday(&trigger_start, NULL);
         timer_on = 1;
         pthread_mutex_lock(&m_count);
@@ -178,12 +207,10 @@ static __inline__ void stop_all(int unused)
 
 static void kill_kids(void)
 {
-        struct command_t cmd;
         struct kid_t *tmp = kid_list;
 
         stop_all(SIGINT);
-        cmd.c_flags = C_STOP;
-        trigger(&cmd, 0);
+        trigger(C_STOP, 0, 0, 0);
         while(tmp) {
                 pthread_kill(tmp->k_pthread, SIGTERM);
                 tmp = tmp->k_next;
@@ -241,7 +268,7 @@ static int write_proc(char *proc_path, char *value)
         return rc;
 }
 
-static int read_proc(char *proc_path, long long *value)
+static int read_proc(char *proc_path,  unsigned long long *value)
 {
         int fd, rc;
         char buf[50];
@@ -447,17 +474,18 @@ out:
 /* See jt_obd_create */
 static int obj_create(struct kid_t *kid)
 {
-        struct obd_ioctl_data data;
-        int rc;
-
-        memset(&data, 0, sizeof(data));
-        data.ioc_dev = kid->k_dev;
-        data.ioc_obdo1.o_mode = 0100644;
-        data.ioc_obdo1.o_id = 0;
-        data.ioc_obdo1.o_uid = 0;
-        data.ioc_obdo1.o_gid = 0;
-        data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
-                OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
+       struct obd_ioctl_data data;
+       int rc;
+
+       memset(&data, 0, sizeof(data));
+       data.ioc_dev = kid->k_dev;
+       data.ioc_obdo1.o_mode = 0100644;
+       ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
+       ostid_set_id(&data.ioc_obdo1.o_oi, 1);
+       data.ioc_obdo1.o_uid = 0;
+       data.ioc_obdo1.o_gid = 0;
+       data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
+                       OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
 
         rc = obj_ioctl(OBD_IOC_CREATE, &data, 1);
         if (rc) {
@@ -472,7 +500,7 @@ static int obj_create(struct kid_t *kid)
                 return rc;
         }
 
-        kid->k_objid = data.ioc_obdo1.o_id;
+       kid->k_objid = ostid_id(&data.ioc_obdo1.o_oi);
 
         if (o_verbose > 4)
                 printf("%d: cr "LPX64"\n", kid->k_id, kid->k_objid);
@@ -487,17 +515,17 @@ static int obj_delete(struct kid_t *kid)
         int rc;
 
         if (o_verbose > 4)
-                printf("%d: del "LPU64"\n", kid->k_id, kid->k_objid);
+                printf("%d: del "LPX64"\n", kid->k_id, kid->k_objid);
 
         memset(&data, 0, sizeof(data));
         data.ioc_dev = kid->k_dev;
-        data.ioc_obdo1.o_id = kid->k_objid;
+       ostid_set_id(&data.ioc_obdo1.o_oi, kid->k_objid);
         data.ioc_obdo1.o_mode = S_IFREG | 0644;
         data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
 
         rc = obj_ioctl(OBD_IOC_DESTROY, &data, 1);
         if (rc)
-                fprintf(stderr, "%s-%d: can't destroy obj "LPU64" (%d)\n",
+                fprintf(stderr, "%s-%d: can't destroy obj "LPX64" (%d)\n",
                         cmdname, kid->k_id, kid->k_objid, rc);
 
         kid->k_objid = 0;
@@ -531,13 +559,14 @@ static int obj_write(struct kid_t *kid)
         data.ioc_pbuf1 = (void *)1;
         data.ioc_plen1 = 1;
 
-        data.ioc_obdo1.o_id = kid->k_objid;
-        data.ioc_obdo1.o_mode = S_IFREG;
-        data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
-                OBD_MD_FLFLAGS;
-        data.ioc_obdo1.o_flags = OBD_FL_DEBUG_CHECK;
-        data.ioc_count = len;
-        data.ioc_offset = 0;
+       ostid_set_seq_echo(&data.ioc_obdo1.o_oi);
+       ostid_set_id(&data.ioc_obdo1.o_oi, kid->k_objid);
+       data.ioc_obdo1.o_mode = S_IFREG;
+       data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
+                                OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
+       data.ioc_obdo1.o_flags = OBD_FL_DEBUG_CHECK;
+       data.ioc_count = len;
+       data.ioc_offset = 0;
 
         gettimeofday(&start, NULL);
 
@@ -586,7 +615,7 @@ static int do_work(struct kid_t *kid)
         if (!(kid->k_cmd.c_flags & C_CREATE_EVERY))
                 rc = obj_create(kid);
 
-        for (iter = 0; iter < kid->k_cmd.c_count; iter++) {
+        for (iter = 0; iter < kid->k_cmd.c_rpt; iter++) {
                 if (rc || sig_received)
                         break;
 
@@ -637,26 +666,35 @@ static void report_perf()
                        (write_bytes >> 20) / diff);
                 pthread_mutex_unlock(&m_count);
         }
-        timer_on = 0;
 }
 
 static void *run_one_child(void *threadvp)
 {
-        struct kid_t *kid;
-        char oname[10], ename[10];
-        int thread = (long)threadvp, dev;
-        int rc = 0, err;
-
-        if (o_verbose > 2)
-                printf("%s: running thread #%d\n", cmdname, thread);
-
-        sprintf(oname, "o%.5d", thread);
-        sprintf(ename, "e%.5d", thread);
+       struct kid_t *kid;
+       char oname[16], ename[16];
+       int thread = (long)threadvp;
+       int dev = 0;
+       int err;
+       int rc;
+
+       if (o_verbose > 2)
+               printf("%s: running thread #%d\n", cmdname, thread);
+
+       rc = snprintf(oname, sizeof(oname), "o%.5d", thread);
+       if (rc != 1) {
+               rc = -EFAULT;
+               goto out_exit;
+       }
+       rc = snprintf(ename, sizeof(ename), "e%.5d", thread);
+       if (rc != 1) {
+               rc = -EFAULT;
+               goto out_exit;
+       }
         rc = echocli_setup(oname, ename, &dev);
         if (rc) {
                 fprintf(stderr, "%s: can't setup '%s/%s' (%d)\n",
                         cmdname, oname, ename, rc);
-                pthread_exit((void *)(long)rc);
+               goto out_exit;
         }
 
         kid = push_kid(thread);
@@ -668,12 +706,17 @@ static void *run_one_child(void *threadvp)
 
         while(!(rc || sig_received)) {
                 pthread_mutex_lock(&m_trigger);
+               pthread_mutex_lock(&m_list);
                 waiting_count++;
-                if ((waiting_count == live_threads) && timer_on)
+                if ((waiting_count == live_threads) && timer_on) {
                         report_perf();
-
+                        timer_on = 0;
+                        all_done = 1;
+                }
+               pthread_mutex_unlock(&m_list);
                 pthread_cond_wait(&cv_trigger, &m_trigger);
                 waiting_count--;
+                all_done = 0;
 
                 /* First trigger_count threads will do the work, the rest
                    will block again */
@@ -681,9 +724,9 @@ static void *run_one_child(void *threadvp)
                         if (o_verbose > 4)
                                 printf("%d: trigger %d cmd %x\n",
                                        kid->k_id, trigger_count,
-                                       trigger_cmd->c_flags);
+                                       trigger_cmd.c_flags);
                         trigger_count--;
-                        kid->k_cmd = *trigger_cmd;
+                        memcpy(&kid->k_cmd, &trigger_cmd, sizeof(trigger_cmd));
                         pthread_mutex_unlock(&m_trigger);
                         rc = do_work(kid);
                 } else {
@@ -703,9 +746,23 @@ out:
         err = cleanup(oname, 0);
         if (!rc) rc = err;
 
-        pthread_exit((void *)(long)rc);
+out_exit:
+       pthread_exit((void *)(long)rc);
 }
 
+/* 
+ * PTHREAD_STACK_MIN is 16K minimal stack for threads. This
+ * is stack consumed by one thread, which executes NULL procedure.
+ * We need some more here and 20k stack for one client thread
+ * is enough to not overflow. In same time it does not consume
+ * a lot of memory for large number of threads.
+ *
+ * 20K virtual clients will only consume 320M + 400M. Still to
+ * create this number of virtual clients we need to fix 8192
+ * OBDs limit.
+ */
+#define CLIENT_THREAD_STACK_SIZE (PTHREAD_STACK_MIN + (20 * 1024))
+
 static int loadgen_start_clients(int argc, char **argv)
 {
         int rc = 0, i, numt;
@@ -731,7 +788,7 @@ static int loadgen_start_clients(int argc, char **argv)
                         cmdname, rc, strerror(errno));
                 return -errno;
         }
-        pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
+        pthread_attr_setstacksize (&attr, CLIENT_THREAD_STACK_SIZE);
         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
         numt += live_threads;
@@ -831,23 +888,23 @@ static int loadgen_verbose(int argc, char **argv)
 
 static int loadgen_write(int argc, char **argv)
 {
-        struct command_t cmd;
         int threads;
 
         if (argc < 3 || argc > 4)
                 return CMD_HELP;
         threads = atoi(argv[1]);
+        pthread_mutex_lock(&m_list);
         if (threads > live_threads) {
+               pthread_mutex_unlock(&m_list);
                 fprintf(stderr, "requested %d threads but only %d are running. "
                         "Use 'start' to start some more.\n",
                         threads, live_threads);
                 return -EOVERFLOW;
+        } else {
+               pthread_mutex_unlock(&m_list);
         }
-        cmd.c_flags = C_WRITE;
-        cmd.c_count = atoi(argv[2]);
-        if (argc == 4)
-                cmd.c_delay = atoi(argv[3]);
-        trigger(&cmd, threads);
+        trigger(C_WRITE, threads, atoi(argv[2]),
+                (argc == 4) ? atoi(argv[3]) : 0);
         return 0;
 }
 
@@ -895,7 +952,7 @@ static int loadgen_start_echosrv(int argc, char **argv)
                         cmdname, rc);
                 goto clean;
         }
-  
+
         /* Create an OSS to handle the communications */
         /* attach ost OSS OSS_UUID */
         args[1] = "ost";
@@ -932,9 +989,19 @@ clean:
         return rc;
 }
 
+static int loadgen_wait(int argc, char **argv)
+{
+        /* Give scripts a chance to start some threads */
+        sleep(1);
+        while (!all_done) {
+                sleep(1);
+        }
+        return 0;
+}
+
 static int loadgen_init(int argc, char **argv)
 {
-        char *args[3];
+        char *args[4];
         int rc;
 
         sprintf(cmdname, "%s", argv[0]);
@@ -964,7 +1031,7 @@ static int loadgen_init(int argc, char **argv)
 static int loadgen_exit()
 {
         int rc;
-        
+
         printf("stopping %d children\n", live_threads);
         kill_kids();
         rc = wait_for_threads();
@@ -1006,7 +1073,7 @@ static int loadgen_main(int argc, char **argv)
 
 out:
         obd_finalize(argc, argv);
-        return rc;
+        return rc < 0 ? -rc : rc;
 }
 
 #ifndef LIBLUSTRE_TEST
@@ -1019,4 +1086,3 @@ int main (int argc, char **argv)
         return rc;
 }
 #endif
-