1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Author: Nathan Rutman <nathan@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * See how many local OSCs we can start whaling on a OST
24 * We're doing direct ioctls instead of going though a system() call to lctl
25 * to avoid the bash overhead.
26 * Adds an osc / echo client pair in each thread and starts echo transactions.
34 #include <sys/types.h>
44 #include <lnet/lnetctl.h>
48 static char cmdname[512];
49 static char target[64] = "";
51 static int live_threads = 0;
52 static int sig_received = 0;
53 static int o_verbose = 4; /* 0-5 */
54 static int my_oss = 0;
55 static int my_ecs = 0;
57 static int jt_quit(int argc, char **argv) {
58 Parser_quit(argc, argv);
62 static int loadgen_usage(int argc, char **argv)
66 "This is a test program used to simulate large numbers of\n"
67 "clients. The echo obds are used, so the obdecho module must\n"
69 "Typical usage would be:\n"
70 " loadgen> dev lustre-OST0000 set the target device\n"
71 " loadgen> start 20 start 20 echo clients\n"
72 " loadgen> wr 10 5 have 10 clients do the brw_write\n"
73 " test 5 times each\n"
76 return (Parser_help(argc, argv));
79 static int loadgen_verbose(int argc, char **argv);
80 static int loadgen_target(int argc, char **argv);
81 static int loadgen_start_echosrv(int argc, char **argv);
82 static int loadgen_start_clients(int argc, char **argv);
83 static int loadgen_wait(int argc, char **argv);
84 static int loadgen_write(int argc, char **argv);
86 command_t cmdlist[] = {
87 {"device", loadgen_target, 0,
88 "set target ost name (e.g. lustre-OST0000)\n"
89 "usage: device <name> [<nid>]"},
90 {"dl", jt_obd_list, 0, "show all devices\n"
92 {"echosrv", loadgen_start_echosrv, 0, "start an echo server\n"},
93 {"start", loadgen_start_clients, 0, "set up echo clients\n"
94 "usage: start_clients <num>"},
95 {"verbose", loadgen_verbose, 0, "set verbosity level 0-5\n"
96 "usage: verbose <level>"},
97 {"wait", loadgen_wait, 0,
98 "wait for all threads to finish\n"},
99 {"write", loadgen_write, 0,
100 "start a test_brw write test on X clients for Y iterations\n"
101 "usage: write <num_clients> <num_iter> [<delay>]"},
103 /* User interface commands */
104 {"help", loadgen_usage, 0, "help"},
105 {"exit", jt_quit, 0, "quit"},
106 {"quit", jt_quit, 0, "quit"},
112 #define C_STOP 0x0001
113 #define C_CREATE_EVERY 0x0002 /* destroy and recreate every time */
114 #define C_READ 0x0004
115 #define C_WRITE 0x0008
124 struct command_t k_cmd;
125 struct kid_t *k_next;
132 static struct kid_t *kid_list = NULL;
134 static struct kid_t *push_kid(int tnum)
137 kid = (struct kid_t *)calloc(1, sizeof(struct kid_t));
139 fprintf(stderr, "malloc failure\n");
142 kid->k_pthread = pthread_self();
143 kid->k_next = kid_list;
150 int trigger_count = 0;
151 int waiting_count = 0;
154 struct timeval trigger_start;
155 struct command_t trigger_cmd;
156 pthread_mutex_t m_trigger = PTHREAD_MUTEX_INITIALIZER;
157 pthread_cond_t cv_trigger = PTHREAD_COND_INITIALIZER;
159 unsigned long long write_bytes;
160 pthread_mutex_t m_count = PTHREAD_MUTEX_INITIALIZER;
162 static void trigger(int command, int threads, int repeat, int delay)
165 pthread_mutex_lock(&m_trigger);
166 trigger_cmd.c_flags = command;
167 trigger_cmd.c_rpt = repeat;
168 trigger_cmd.c_delay = delay;
169 trigger_count = threads;
171 printf("trigger %d cmd c=%d f=%x\n", trigger_count,
172 trigger_cmd.c_rpt, trigger_cmd.c_flags);
173 gettimeofday(&trigger_start, NULL);
175 pthread_mutex_lock(&m_count);
177 pthread_mutex_unlock(&m_count);
179 pthread_cond_broadcast(&cv_trigger);
180 pthread_mutex_unlock(&m_trigger);
183 static __inline__ void stop_all(int unused)
188 static void kill_kids(void)
190 struct kid_t *tmp = kid_list;
193 trigger(C_STOP, 0, 0, 0);
195 pthread_kill(tmp->k_pthread, SIGTERM);
200 static void sig_master(int unused)
206 static int wait_for_threads()
208 struct kid_t *tmp = kid_list;
212 printf("waiting for %d children\n", live_threads);
215 rc = pthread_join(tmp->k_pthread, &statusp);
216 status = (long)statusp;
218 printf("%d: joined, rc = %d, status = %d\n",
219 tmp->k_id, rc, status);
220 kid_list = tmp->k_next;
227 printf("%s done, rc = %d\n", cmdname, rc);
231 static int write_proc(char *proc_path, char *value)
235 fd = open(proc_path, O_WRONLY);
237 fprintf(stderr, "open('%s') failed: %s\n",
238 proc_path, strerror(errno));
241 rc = write(fd, value, strlen(value));
243 fprintf(stderr, "write('%s') failed: %s\n",
244 proc_path, strerror(errno));
251 static int read_proc(char *proc_path, long long *value)
256 fd = open(proc_path, O_RDONLY);
258 fprintf(stderr, "open('%s') failed: %s\n",
259 proc_path, strerror(errno));
263 rc = read(fd, buf, sizeof(buf));
265 if (errno == EOPNOTSUPP) {
266 /* probably an echo server */
270 fprintf(stderr, "read('%s') failed: %s (%d)\n",
271 proc_path, strerror(errno), errno);
274 *value = strtoull(buf, NULL, 10);
278 static int grant_estimate(int thread)
280 unsigned long long avail, grant;
285 /* I don't really care about protecting this with a mutex */
292 /* Divide /proc/fs/lustre/osc/o_0001/kbytesavail
293 by /proc/fs/lustre/osc/o_0001/cur_grant_bytes to find max clients */
294 sprintf(proc_path, "/proc/fs/lustre/osc/o%.5d/kbytesavail", thread);
295 rc = read_proc(proc_path, &avail);
298 sprintf(proc_path, "/proc/fs/lustre/osc/o%.5d/cur_grant_bytes", thread);
299 rc = read_proc(proc_path, &grant);
305 printf("Estimate %llu clients before we run out of grant space "
306 "(%lluK / %llu)\n", (avail << 10) / grant, avail, grant);
311 /* We hold a thread mutex around create/cleanup because cur_dev is not
312 shared-memory safe */
313 pthread_mutex_t m_config = PTHREAD_MUTEX_INITIALIZER;
315 static int cleanup(char *obdname, int quiet)
320 pthread_mutex_lock(&m_config);
324 rc = jt_lcfg_device(2, args);
326 fprintf(stderr, "%s: can't configure '%s' (%d)\n",
327 cmdname, obdname, rc);
329 rc = jt_obd_cleanup(2, args);
331 fprintf(stderr, "%s: can't cleanup '%s' (%d)\n",
332 cmdname, obdname, rc);
333 rc = jt_obd_detach(1, args);
335 fprintf(stderr, "%s: can't detach '%s' (%d)\n",
336 cmdname, obdname, rc);
338 pthread_mutex_unlock(&m_config);
342 static int echocli_setup(char *oname, char *ename, int *dev)
348 pthread_mutex_lock(&m_config);
353 /* attach "osc" oscname oscuuid */
355 args[2] = args[3] = oname;
356 rc = jt_lcfg_attach(4, args);
358 fprintf(stderr, "%s: can't attach osc '%s' (%d)\n",
360 /* Assume we want e.g. an old one cleaned anyhow. */
363 /* setup ostname "OSS_UUID" */
365 args[2] = "OSS_UUID";
366 rc = jt_lcfg_setup(3, args);
368 fprintf(stderr, "%s: can't setup osc '%s' (%d)\n",
373 /* Large grants cause ENOSPC to be reported, even though
374 there's space left. We can reduce the grant size by
376 sprintf(proc_path, "/proc/fs/lustre/osc/%s/max_dirty_mb", oname);
377 rc = write_proc(proc_path, "1");
378 sprintf(proc_path, "/proc/fs/lustre/osc/%s/max_rpcs_in_flight", oname);
379 rc = write_proc(proc_path, "1");
382 /* attach "echo_client" echoname echouuid */
383 args[1] = "echo_client";
384 args[2] = args[3] = ename;
385 rc = jt_lcfg_attach(4, args);
387 fprintf(stderr, "%s: can't attach '%s' (%d)\n",
390 fprintf(stderr, "%s: is the obdecho module loaded?\n",
396 rc = jt_lcfg_setup(2, args);
398 fprintf(stderr, "%s: can't setup '%s' (%d)\n",
404 rc = jt_obd_device(2, args);
406 fprintf(stderr, "%s: can't set device '%s' (%d)\n",
412 *dev = jt_obd_get_device();
413 pthread_mutex_unlock(&m_config);
417 pthread_mutex_unlock(&m_config);
423 /* We can't use the libptlctl library fns because they are not shared-memory
424 safe with respect to the ioctl device (cur_dev) */
425 static int obj_ioctl(int cmd, struct obd_ioctl_data *data, int unpack)
430 //IOC_PACK(cmdname, data);
431 if (obd_ioctl_pack(data, &buf, sizeof(*data))) {
432 fprintf(stderr, "dev %d invalid ioctl\n", data->ioc_dev);
437 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
440 //IOC_UNPACK(argv[0], data);
441 if (obd_ioctl_unpack(data, buf, sizeof(*data))) {
442 fprintf(stderr, "dev %d invalid reply\n", data->ioc_dev);
454 /* See jt_obd_create */
455 static int obj_create(struct kid_t *kid)
457 struct obd_ioctl_data data;
460 memset(&data, 0, sizeof(data));
461 data.ioc_dev = kid->k_dev;
462 data.ioc_obdo1.o_mode = 0100644;
463 data.ioc_obdo1.o_id = 0;
464 data.ioc_obdo1.o_uid = 0;
465 data.ioc_obdo1.o_gid = 0;
466 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
467 OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
469 rc = obj_ioctl(OBD_IOC_CREATE, &data, 1);
471 fprintf(stderr, "%d: create (%d) %s\n",
472 kid->k_id, rc, strerror(errno));
476 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
477 fprintf(stderr, "%d: create oid not valid "LPX64"\n",
478 kid->k_id, data.ioc_obdo1.o_valid);
482 kid->k_objid = data.ioc_obdo1.o_id;
485 printf("%d: cr "LPX64"\n", kid->k_id, kid->k_objid);
490 /* See jt_obd_destroy */
491 static int obj_delete(struct kid_t *kid)
493 struct obd_ioctl_data data;
497 printf("%d: del "LPX64"\n", kid->k_id, kid->k_objid);
499 memset(&data, 0, sizeof(data));
500 data.ioc_dev = kid->k_dev;
501 data.ioc_obdo1.o_id = kid->k_objid;
502 data.ioc_obdo1.o_mode = S_IFREG | 0644;
503 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
505 rc = obj_ioctl(OBD_IOC_DESTROY, &data, 1);
507 fprintf(stderr, "%s-%d: can't destroy obj "LPX64" (%d)\n",
508 cmdname, kid->k_id, kid->k_objid, rc);
514 #define difftime(a, b) \
515 ((a)->tv_sec - (b)->tv_sec + \
516 ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
518 /* See jt_obd_test_brw */
519 static int obj_write(struct kid_t *kid)
521 struct obd_ioctl_data data;
522 struct timeval start;
524 int rc = 0, i, pages = 0;
527 printf("%d: wr "LPX64"\n", kid->k_id, kid->k_objid);
531 len = pages * getpagesize();
533 memset(&data, 0, sizeof(data));
534 data.ioc_dev = kid->k_dev;
535 /* communicate the 'type' of brw test and batching to echo_client.
536 * don't start. we'd love to refactor this lctl->echo_client
538 data.ioc_pbuf1 = (void *)1;
541 data.ioc_obdo1.o_id = kid->k_objid;
542 data.ioc_obdo1.o_mode = S_IFREG;
543 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
545 data.ioc_obdo1.o_flags = OBD_FL_DEBUG_CHECK;
546 data.ioc_count = len;
549 gettimeofday(&start, NULL);
551 for (i = 1; i <= count; i++) {
552 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS|OBD_MD_FLGRANT);
553 rc = obj_ioctl(OBD_IOC_BRW_WRITE, &data, 0);
555 fprintf(stderr, "%d: write %s\n", kid->k_id,
556 strerror(rc = errno));
560 data.ioc_offset += len;
567 gettimeofday(&end, NULL);
568 diff = difftime(&end, &start);
572 pthread_mutex_lock(&m_count);
573 write_bytes += i * len;
574 pthread_mutex_unlock(&m_count);
577 printf("%d: wrote %dx%d pages in %.3fs (%.3f MB/s): %s",
578 kid->k_id, i, pages, diff,
579 ((double)i * len) / (diff * 1048576.0),
584 fprintf(stderr, "%s-%d: err test_brw obj "LPX64" (%d)\n",
585 cmdname, kid->k_id, kid->k_objid, rc);
589 static int do_work(struct kid_t *kid)
591 int rc = 0, err, iter = 0;
593 if (!(kid->k_cmd.c_flags & C_CREATE_EVERY))
594 rc = obj_create(kid);
596 for (iter = 0; iter < kid->k_cmd.c_rpt; iter++) {
597 if (rc || sig_received)
600 if (kid->k_cmd.c_flags & C_CREATE_EVERY) {
601 rc = obj_create(kid);
606 if (kid->k_cmd.c_flags & C_WRITE) {
608 grant_estimate(kid->k_id);
611 if (kid->k_cmd.c_flags & C_CREATE_EVERY) {
612 err = obj_delete(kid);
616 if ((o_verbose > 3) && (iter % 10 == 0))
617 printf("%d: i%d\n", kid->k_id, iter);
619 sleep(kid->k_cmd.c_delay);
622 if (!(kid->k_cmd.c_flags & C_CREATE_EVERY)) {
623 err = obj_delete(kid);
628 printf("%d: done (%d)\n", kid->k_id, rc);
633 static void report_perf()
638 gettimeofday(&end, NULL);
639 diff = difftime(&end, &trigger_start);
641 pthread_mutex_lock(&m_count);
642 printf("wrote %lluMB in %.3fs (%.3f MB/s)\n",
643 write_bytes >> 20, diff,
644 (write_bytes >> 20) / diff);
645 pthread_mutex_unlock(&m_count);
649 static void *run_one_child(void *threadvp)
652 char oname[10], ename[10];
653 int thread = (long)threadvp, dev;
657 printf("%s: running thread #%d\n", cmdname, thread);
659 sprintf(oname, "o%.5d", thread);
660 sprintf(ename, "e%.5d", thread);
661 rc = echocli_setup(oname, ename, &dev);
663 fprintf(stderr, "%s: can't setup '%s/%s' (%d)\n",
664 cmdname, oname, ename, rc);
665 pthread_exit((void *)(long)rc);
668 kid = push_kid(thread);
675 while(!(rc || sig_received)) {
676 pthread_mutex_lock(&m_trigger);
678 if ((waiting_count == live_threads) && timer_on) {
683 pthread_cond_wait(&cv_trigger, &m_trigger);
687 /* First trigger_count threads will do the work, the rest
691 printf("%d: trigger %d cmd %x\n",
692 kid->k_id, trigger_count,
693 trigger_cmd.c_flags);
695 memcpy(&kid->k_cmd, &trigger_cmd, sizeof(trigger_cmd));
696 pthread_mutex_unlock(&m_trigger);
699 pthread_mutex_unlock(&m_trigger);
704 printf("%s: thread #%d done (%d)\n", cmdname, thread, rc);
710 err = cleanup(ename, 0);
712 err = cleanup(oname, 0);
715 pthread_exit((void *)(long)rc);
718 static int loadgen_start_clients(int argc, char **argv)
721 struct timespec ts = {0, 1000*1000*100}; /* .1 sec */
727 numt = strtoul(argv[1], NULL, 0);
732 fprintf(stderr, "%s: target OST is not defined, use 'device' "
733 "command\n", cmdname);
737 rc = pthread_attr_init(&attr);
739 fprintf(stderr, "%s: pthread_attr_init:(%d) %s\n",
740 cmdname, rc, strerror(errno));
743 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
744 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
746 numt += live_threads;
748 printf("start %d to %d\n", i, numt);
749 while(!rc && !sig_received && (i < numt)) {
753 rc = pthread_create(&thread, &attr, run_one_child,
756 fprintf(stderr, "%s: pthread: #%d - (%d) %s\n",
757 cmdname, i, rc, strerror(rc));
761 /* give them slightly different start times */
762 nanosleep(&ts, NULL);
765 pthread_attr_destroy(&attr);
770 static int loadgen_target(int argc, char **argv)
776 if (argc < 2 || argc > 3)
782 nidx = libcfs_str2nid(argv[2]);
783 if (nidx == LNET_NID_ANY) {
784 fprintf(stderr, "%s: invalid nid '%s'\n",
789 /* Local device should be in list */
791 rc = jt_obd_device(2, args);
793 fprintf(stderr, "%s: local device '%s' doesn't "
794 "seem to exist. You must use obdfilter device "
795 "names like 'lustre-OST0000'. Use 'dl' to "
796 "list all devices.\n",
801 /* Use the first local nid */
802 args[1] = (char *)(&nidx);
803 args[1][0] = 1; /* hack to get back first nid */
804 rc = jt_ptl_list_nids(2, args);
806 fprintf(stderr, "%s: can't get local nid (%d)\n",
811 if (strcmp(nid, libcfs_nid2str(nidx)) != 0) {
812 /* if new, do an add_uuid */
813 sprintf(nid, "%s", libcfs_nid2str(nidx));
815 /* Fixme change the uuid for every new one */
816 args[1] = "OSS_UUID";
818 rc = jt_lcfg_add_uuid(3, args);
820 fprintf(stderr, "%s: can't add uuid '%s' (%d)\n",
821 cmdname, args[2], rc);
826 snprintf(target, sizeof(target), "%s", argv[1]);
827 printf("Target OST name is '%s'\n", target);
832 static int loadgen_verbose(int argc, char **argv)
836 o_verbose = atoi(argv[1]);
837 printf("verbosity set to %d\n", o_verbose);
841 static int loadgen_write(int argc, char **argv)
845 if (argc < 3 || argc > 4)
847 threads = atoi(argv[1]);
848 if (threads > live_threads) {
849 fprintf(stderr, "requested %d threads but only %d are running. "
850 "Use 'start' to start some more.\n",
851 threads, live_threads);
854 trigger(C_WRITE, threads, atoi(argv[2]),
855 (argc == 4) ? atoi(argv[3]) : 0);
859 char ecsname[] = "echosrv";
860 static int loadgen_stop_echosrv(int argc, char **argv)
862 int verbose = (argc != 9);
865 cleanup(name, verbose);
868 if (my_ecs || (argc == 9)) {
869 cleanup(ecsname, verbose);
875 static int loadgen_start_echosrv(int argc, char **argv)
880 pthread_mutex_lock(&m_config);
884 /* attach obdecho echosrv echosrv_UUID */
886 args[2] = args[3] = ecsname;
887 rc = jt_lcfg_attach(4, args);
889 fprintf(stderr, "%s: can't attach echo server (%d)\n",
891 /* Assume we want e.g. an old one cleaned anyhow. */
897 rc = jt_lcfg_setup(1, args);
899 fprintf(stderr, "%s: can't setup echo server (%d)\n",
904 /* Create an OSS to handle the communications */
905 /* attach ost OSS OSS_UUID */
907 args[2] = args[3] = "OSS";
909 rc = jt_lcfg_attach(4, args);
911 /* Already set up for somebody else, that's fine. */
912 printf("OSS already set up, no problem.\n");
913 pthread_mutex_unlock(&m_config);
917 fprintf(stderr, "%s: can't attach OSS (%d)\n",
924 rc = jt_lcfg_setup(1, args);
926 fprintf(stderr, "%s: can't setup OSS (%d)\n",
931 pthread_mutex_unlock(&m_config);
935 pthread_mutex_unlock(&m_config);
936 loadgen_stop_echosrv(9, argv);
940 static int loadgen_wait(int argc, char **argv)
942 /* Give scripts a chance to start some threads */
950 static int loadgen_init(int argc, char **argv)
955 sprintf(cmdname, "%s", argv[0]);
957 signal(SIGTERM, sig_master);
958 signal(SIGINT, sig_master);
960 /* Test to make sure obdecho module is loaded */
962 args[1] = "echo_client";
963 args[2] = args[3] = "ecc_test";
964 rc = jt_lcfg_attach(4, args);
966 fprintf(stderr, "%s: can't attach echo client (%d)\n",
969 fprintf(stderr, "%s: is the obdecho module loaded?\n",
973 jt_obd_detach(1, args);
979 static int loadgen_exit()
983 printf("stopping %d children\n", live_threads);
985 rc = wait_for_threads();
987 loadgen_stop_echosrv(0, NULL);
992 /* libptlctl interface */
993 static int loadgen_main(int argc, char **argv)
998 /* without this threaded errors cause segfault */
1001 if ((rc = ptl_initialize(argc, argv)) < 0)
1003 if ((rc = obd_initialize(argc, argv)) < 0)
1005 if ((rc = dbg_initialize(argc, argv)) < 0)
1008 Parser_init("loadgen> ", cmdlist);
1010 rc = loadgen_init(argc, argv);
1015 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
1017 rc = Parser_commands();
1020 rc = loadgen_exit();
1023 obd_finalize(argc, argv);
1027 #ifndef LIBLUSTRE_TEST
1028 int main (int argc, char **argv)
1031 rc = loadgen_main(argc, argv);
1032 pthread_exit((void *)(long)rc);