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 */
55 static int jt_quit(int argc, char **argv) {
56 Parser_quit(argc, argv);
60 static int loadgen_usage(int argc, char **argv)
64 "This is a test program used to simulate large numbers of\n"
65 "clients. The echo obds are used, so the obdecho module must\n"
67 "Typical usage would be:\n"
68 " loadgen> dev lustre-OST0000 set the target device\n"
69 " loadgen> start 20 start 20 echo clients\n"
70 " loadgen> wr 10 5 have 10 clients do the brw_write\n"
71 " test 5 times each\n"
74 return (Parser_help(argc, argv));
77 static int loadgen_verbose(int argc, char **argv);
78 static int loadgen_target(int argc, char **argv);
79 static int loadgen_start_clients(int argc, char **argv);
80 static int loadgen_write(int argc, char **argv);
82 command_t cmdlist[] = {
83 {"device", loadgen_target, 0,
84 "set target ost name (e.g. lustre-OST0000)\n"
85 "usage: device <name> [<nid>]"},
86 {"dl", jt_obd_list, 0, "show all devices\n"
88 {"start", loadgen_start_clients, 0,
89 "set up echo clients\n"
90 "usage: start_clients <num>"},
91 {"verbose", loadgen_verbose, 0,
92 "set verbosity level 0-5\n"
93 "usage: verbose <level>"},
94 {"write", loadgen_write, 0,
95 "start a test_brw write test on X clients for Y iterations\n"
96 "usage: write <num_clients> <num_iter> [<delay>]"},
98 /* User interface commands */
99 {"help", loadgen_usage, 0, "help"},
100 {"exit", jt_quit, 0, "quit"},
101 {"quit", jt_quit, 0, "quit"},
107 #define C_STOP 0x0001
108 #define C_CREATE_EVERY 0x0002 /* destroy and recreate every time */
109 #define C_READ 0x0004
110 #define C_WRITE 0x0008
119 struct command_t k_cmd;
120 struct kid_t *k_next;
127 static struct kid_t *kid_list = NULL;
129 static struct kid_t *push_kid(int tnum)
132 kid = (struct kid_t *)calloc(1, sizeof(struct kid_t));
134 fprintf(stderr, "malloc failure\n");
137 kid->k_pthread = pthread_self();
138 kid->k_next = kid_list;
145 int trigger_count = 0;
146 int waiting_count = 0;
148 struct timeval trigger_start;
149 struct command_t *trigger_cmd = NULL;
150 pthread_mutex_t m_trigger = PTHREAD_MUTEX_INITIALIZER;
151 pthread_cond_t cv_trigger = PTHREAD_COND_INITIALIZER;
154 pthread_mutex_t m_count = PTHREAD_MUTEX_INITIALIZER;
156 static void trigger(struct command_t *cmd, int count)
159 pthread_mutex_lock(&m_trigger);
161 trigger_count = count;
162 gettimeofday(&trigger_start, NULL);
164 pthread_mutex_lock(&m_count);
166 pthread_mutex_unlock(&m_count);
168 pthread_cond_broadcast(&cv_trigger);
169 pthread_mutex_unlock(&m_trigger);
172 static __inline__ void stop_all(int unused)
177 static void kill_kids(void)
179 struct command_t cmd;
180 struct kid_t *tmp = kid_list;
183 cmd.c_flags = C_STOP;
186 pthread_kill(tmp->k_pthread, SIGTERM);
191 static void sig_master(int unused)
197 static int wait_for_threads()
199 struct kid_t *tmp = kid_list;
203 printf("waiting for %d children\n", live_threads);
206 rc = pthread_join(tmp->k_pthread, &statusp);
207 status = (long)statusp;
209 printf("%d: joined, rc = %d, status = %d\n",
210 tmp->k_id, rc, status);
211 kid_list = tmp->k_next;
218 printf("%s done, rc = %d\n", cmdname, rc);
222 static int write_proc(char *proc_path, char *value)
226 fd = open(proc_path, O_WRONLY);
228 fprintf(stderr, "open('%s') failed: %s\n",
229 proc_path, strerror(errno));
232 rc = write(fd, value, strlen(value));
234 fprintf(stderr, "write('%s') failed: %s\n",
235 proc_path, strerror(errno));
242 static int read_proc(char *proc_path, long long *value)
247 fd = open(proc_path, O_RDONLY);
249 fprintf(stderr, "open('%s') failed: %s\n",
250 proc_path, strerror(errno));
254 rc = read(fd, buf, sizeof(buf));
257 fprintf(stderr, "read('%s') failed: %s (%d)\n",
258 proc_path, strerror(errno), rc);
261 *value = strtoull(buf, NULL, 10);
265 static int grant_estimate(int thread)
267 unsigned long long avail, grant;
272 /* I don't really care about protecting this with a mutex */
279 /* Divide /proc/fs/lustre/osc/o_0001/kbytesavail
280 by /proc/fs/lustre/osc/o_0001/cur_grant_bytes to find max clients */
281 sprintf(proc_path, "/proc/fs/lustre/osc/o%.5d/kbytesavail", thread);
282 rc = read_proc(proc_path, &avail);
285 sprintf(proc_path, "/proc/fs/lustre/osc/o%.5d/cur_grant_bytes", thread);
286 rc = read_proc(proc_path, &grant);
292 printf("Estimate %llu clients before we run out of grant space "
293 "(%lluK / %llu)\n", (avail << 10) / grant, avail, grant);
298 /* We hold a thread mutex around create/cleanup because cur_dev is not
299 shared-memory safe */
300 pthread_mutex_t m_config = PTHREAD_MUTEX_INITIALIZER;
302 static int cleanup(char *obdname, int quiet)
307 pthread_mutex_lock(&m_config);
311 rc = jt_lcfg_device(2, args);
313 fprintf(stderr, "%s: can't configure '%s' (%d)\n",
314 cmdname, obdname, rc);
316 rc = jt_obd_cleanup(2, args);
318 fprintf(stderr, "%s: can't cleanup '%s' (%d)\n",
319 cmdname, obdname, rc);
320 rc = jt_obd_detach(1, args);
322 fprintf(stderr, "%s: can't detach '%s' (%d)\n",
323 cmdname, obdname, rc);
325 pthread_mutex_unlock(&m_config);
329 static int setup(char *oname, char *ename, int *dev)
335 pthread_mutex_lock(&m_config);
340 /* attach "osc" oscname oscuuid */
342 args[2] = args[3] = oname;
343 rc = jt_lcfg_attach(4, args);
345 fprintf(stderr, "%s: can't attach osc '%s' (%d)\n",
347 /* Assume we want e.g. an old one cleaned anyhow. */
350 /* setup ostname "OSS_UUID" */
352 args[2] = "OSS_UUID";
353 rc = jt_lcfg_setup(3, args);
355 fprintf(stderr, "%s: can't setup osc '%s' (%d)\n",
360 /* Large grants cause ENOSPC to be reported, even though
361 there's space left. We can reduce the grant size by
363 sprintf(proc_path, "/proc/fs/lustre/osc/%s/max_dirty_mb", oname);
364 rc = write_proc(proc_path, "1");
365 sprintf(proc_path, "/proc/fs/lustre/osc/%s/max_rpcs_in_flight", oname);
366 rc = write_proc(proc_path, "1");
369 /* attach "echo_client" echoname echouuid */
370 args[1] = "echo_client";
371 args[2] = args[3] = ename;
372 rc = jt_lcfg_attach(4, args);
374 fprintf(stderr, "%s: can't attach '%s' (%d)\n",
377 fprintf(stderr, "%s: is the obdecho module loaded?\n",
383 rc = jt_lcfg_setup(2, args);
385 fprintf(stderr, "%s: can't setup '%s' (%d)\n",
391 rc = jt_obd_device(2, args);
393 fprintf(stderr, "%s: can't set device '%s' (%d)\n",
399 *dev = jt_obd_get_device();
400 pthread_mutex_unlock(&m_config);
404 pthread_mutex_unlock(&m_config);
410 /* We can't use the libptlctl library fns because they are not shared-memory
411 safe with respect to the ioctl device (cur_dev) */
412 static int obj_ioctl(int cmd, struct obd_ioctl_data *data, int unpack)
417 //IOC_PACK(cmdname, data);
418 if (obd_ioctl_pack(data, &buf, sizeof(*data))) {
419 fprintf(stderr, "dev %d invalid ioctl\n", data->ioc_dev);
424 rc = l_ioctl(OBD_DEV_ID, cmd, buf);
427 //IOC_UNPACK(argv[0], data);
428 if (obd_ioctl_unpack(data, buf, sizeof(*data))) {
429 fprintf(stderr, "dev %d invalid reply\n", data->ioc_dev);
441 /* See jt_obd_create */
442 static int obj_create(struct kid_t *kid)
444 struct obd_ioctl_data data;
447 memset(&data, 0, sizeof(data));
448 data.ioc_dev = kid->k_dev;
449 data.ioc_obdo1.o_mode = 0100644;
450 data.ioc_obdo1.o_id = 0;
451 data.ioc_obdo1.o_uid = 0;
452 data.ioc_obdo1.o_gid = 0;
453 data.ioc_obdo1.o_valid = OBD_MD_FLTYPE | OBD_MD_FLMODE |
454 OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID;
456 rc = obj_ioctl(OBD_IOC_CREATE, &data, 1);
458 fprintf(stderr, "%d: create (%d) %s\n",
459 kid->k_id, rc, strerror(errno));
463 if (!(data.ioc_obdo1.o_valid & OBD_MD_FLID)) {
464 fprintf(stderr,"%d: create oid not valid "LPX64"\n",
465 kid->k_id, data.ioc_obdo1.o_valid);
469 kid->k_objid = data.ioc_obdo1.o_id;
472 printf("%d: cr "LPX64"\n", kid->k_id, kid->k_objid);
477 /* See jt_obd_destroy */
478 static int obj_delete(struct kid_t *kid)
480 struct obd_ioctl_data data;
484 printf("%d: del "LPU64"\n", kid->k_id, kid->k_objid);
486 memset(&data, 0, sizeof(data));
487 data.ioc_dev = kid->k_dev;
488 data.ioc_obdo1.o_id = kid->k_objid;
489 data.ioc_obdo1.o_mode = S_IFREG | 0644;
490 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLMODE;
492 rc = obj_ioctl(OBD_IOC_DESTROY, &data, 1);
494 fprintf(stderr, "%s-%d: can't destroy obj "LPU64" (%d)\n",
495 cmdname, kid->k_id, kid->k_objid, rc);
501 #define difftime(a, b) \
502 ((a)->tv_sec - (b)->tv_sec + \
503 ((a)->tv_usec - (b)->tv_usec) / 1000000.0)
505 /* See jt_obd_test_brw */
506 static int obj_write(struct kid_t *kid)
508 struct obd_ioctl_data data;
509 struct timeval start;
511 int rc = 0, i, pages = 0;
514 printf("%d: wr "LPX64"\n", kid->k_id, kid->k_objid);
518 len = pages * getpagesize();
520 memset(&data, 0, sizeof(data));
521 data.ioc_dev = kid->k_dev;
522 /* communicate the 'type' of brw test and batching to echo_client.
523 * don't start. we'd love to refactor this lctl->echo_client
525 data.ioc_pbuf1 = (void *)1;
528 data.ioc_obdo1.o_id = kid->k_objid;
529 data.ioc_obdo1.o_mode = S_IFREG;
530 data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
532 data.ioc_obdo1.o_flags = OBD_FL_DEBUG_CHECK;
533 data.ioc_count = len;
536 gettimeofday(&start, NULL);
538 for (i = 1; i <= count; i++) {
539 data.ioc_obdo1.o_valid &= ~(OBD_MD_FLBLOCKS|OBD_MD_FLGRANT);
540 rc = obj_ioctl(OBD_IOC_BRW_WRITE, &data, 0);
542 fprintf(stderr, "%d: write %s\n", kid->k_id,
543 strerror(rc = errno));
547 data.ioc_offset += len;
554 gettimeofday(&end, NULL);
555 diff = difftime(&end, &start);
559 pthread_mutex_lock(&m_count);
560 write_bytes += i * len;
561 pthread_mutex_unlock(&m_count);
564 printf("%d: wrote %dx%d pages in %.3fs (%.3f MB/s): %s",
565 kid->k_id, i, pages, diff,
566 ((double)i * len) / (diff * 1048576.0),
571 fprintf(stderr, "%s-%d: err test_brw obj "LPX64" (%d)\n",
572 cmdname, kid->k_id, kid->k_objid, rc);
576 static int do_work(struct kid_t *kid)
578 int rc = 0, err, iter = 0;
580 if (!(kid->k_cmd.c_flags & C_CREATE_EVERY))
581 rc = obj_create(kid);
583 for (iter = 0; iter < kid->k_cmd.c_count; iter++) {
584 if (rc || sig_received)
587 if (kid->k_cmd.c_flags & C_CREATE_EVERY) {
588 rc = obj_create(kid);
593 if (kid->k_cmd.c_flags & C_WRITE) {
595 grant_estimate(kid->k_id);
598 if (kid->k_cmd.c_flags & C_CREATE_EVERY) {
599 err = obj_delete(kid);
603 if ((o_verbose > 3) && (iter % 10 == 0))
604 printf("%d: i%d\n", kid->k_id, iter);
606 sleep(kid->k_cmd.c_delay);
609 if (!(kid->k_cmd.c_flags & C_CREATE_EVERY)) {
610 err = obj_delete(kid);
615 printf("%d: done (%d)\n", kid->k_id, rc);
620 static void report_perf()
625 gettimeofday(&end, NULL);
626 diff = difftime(&end, &trigger_start);
628 pthread_mutex_lock(&m_count);
629 printf("wrote %lluMB in %.3fs (%.3f MB/s)\n",
630 write_bytes >> 20, diff,
631 (write_bytes >> 20) / diff);
632 pthread_mutex_unlock(&m_count);
637 static void *run_one_child(void *threadvp)
640 char oname[10], ename[10];
641 int thread = (long)threadvp, dev;
645 printf("%s: running thread #%d\n", cmdname, thread);
647 sprintf(oname, "o%.5d", thread);
648 sprintf(ename, "e%.5d", thread);
649 rc = setup(oname, ename, &dev);
651 fprintf(stderr, "%s: can't setup '%s/%s' (%d)\n",
652 cmdname, oname, ename, rc);
653 pthread_exit((void *)(long)rc);
656 kid = push_kid(thread);
663 while(!(rc || sig_received)) {
664 pthread_mutex_lock(&m_trigger);
666 if ((waiting_count == live_threads) && timer_on)
669 pthread_cond_wait(&cv_trigger, &m_trigger);
672 /* First trigger_count threads will do the work, the rest
676 printf("%d: trigger %d cmd %x\n",
677 kid->k_id, trigger_count,
678 trigger_cmd->c_flags);
680 kid->k_cmd = *trigger_cmd;
681 pthread_mutex_unlock(&m_trigger);
684 pthread_mutex_unlock(&m_trigger);
689 printf("%s: thread #%d done (%d)\n", cmdname, thread, rc);
695 err = cleanup(ename, 0);
697 err = cleanup(oname, 0);
700 pthread_exit((void *)(long)rc);
703 static int loadgen_start_clients(int argc, char **argv)
706 struct timespec ts = {0, 1000*1000*100}; /* .1 sec */
712 numt = strtoul(argv[1], NULL, 0);
717 fprintf(stderr,"%s: target OST is not defined, use 'device' "
718 "command\n", cmdname);
722 rc = pthread_attr_init(&attr);
724 fprintf(stderr,"%s: pthread_attr_init:(%d) %s\n",
725 cmdname, rc, strerror(errno));
728 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
729 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
731 numt += live_threads;
733 printf("start %d to %d\n", i, numt);
734 while(!rc && !sig_received && (i < numt)) {
738 rc = pthread_create(&thread, &attr, run_one_child,
741 fprintf(stderr, "%s: pthread: #%d - (%d) %s\n",
742 cmdname, i, rc, strerror(rc));
746 /* give them slightly different start times */
747 nanosleep(&ts, NULL);
750 pthread_attr_destroy(&attr);
755 static int loadgen_target(int argc, char **argv)
761 if (argc < 2 || argc > 3)
767 nidx = libcfs_str2nid(argv[2]);
768 if (nidx == LNET_NID_ANY) {
769 fprintf(stderr, "%s: invalid nid '%s'\n",
774 /* Local device should be in list */
776 rc = jt_obd_device(2, args);
778 fprintf(stderr, "%s: local device '%s' doesn't "
779 "seem to exist. You must use obdfilter device "
780 "names like 'lustre-OST0000'. Use 'dl' to "
781 "list all devices.\n",
786 /* Use the first local nid */
787 args[1] = (char *)(&nidx);
788 args[1][0] = 1; /* hack to get back first nid */
789 rc = jt_ptl_list_nids(2, args);
791 fprintf(stderr, "%s: can't get local nid (%d)\n",
796 if (strcmp(nid, libcfs_nid2str(nidx)) != 0) {
797 /* if new, do an add_uuid */
798 sprintf(nid, "%s", libcfs_nid2str(nidx));
800 /* Fixme change the uuid for every new one */
801 args[1] = "OSS_UUID";
803 rc = jt_lcfg_add_uuid(3, args);
805 fprintf(stderr, "%s: can't add uuid '%s' (%d)\n",
806 cmdname, args[2], rc);
811 snprintf(target, sizeof(target), "%s", argv[1]);
812 printf("Target OST name is '%s'\n", target);
817 static int loadgen_verbose(int argc, char **argv)
821 o_verbose = atoi(argv[1]);
822 printf("verbosity set to %d\n", o_verbose);
826 static int loadgen_write(int argc, char **argv)
828 struct command_t cmd;
831 if (argc < 3 || argc > 4)
833 threads = atoi(argv[1]);
834 if (threads > live_threads) {
835 fprintf(stderr, "requested %d threads but only %d are running. "
836 "Use 'start' to start some more.\n",
837 threads, live_threads);
840 cmd.c_flags = C_WRITE;
841 cmd.c_count = atoi(argv[2]);
843 cmd.c_delay = atoi(argv[3]);
844 trigger(&cmd, threads);
848 static int loadgen_init(int argc, char **argv)
853 sprintf(cmdname, "%s", argv[0]);
855 signal(SIGTERM, sig_master);
856 signal(SIGINT, sig_master);
858 /* Test to make sure obdecho module is loaded */
860 args[1] = "echo_client";
861 args[2] = args[3] = "ecc_test";
862 rc = jt_lcfg_attach(4, args);
864 fprintf(stderr, "%s: can't attach echo client (%d)\n",
867 fprintf(stderr, "%s: is the obdecho module loaded?\n",
871 jt_obd_detach(1, args);
877 static int loadgen_exit()
880 printf("stopping %d children\n", live_threads);
882 rc = wait_for_threads();
886 /* libptlctl interface */
887 static int loadgen_main(int argc, char **argv)
893 if ((rc = ptl_initialize(argc, argv)) < 0)
895 if ((rc = obd_initialize(argc, argv)) < 0)
897 if ((rc = dbg_initialize(argc, argv)) < 0)
900 Parser_init("loadgen> ", cmdlist);
902 rc = loadgen_init(argc, argv);
907 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
909 rc = Parser_commands();
915 obd_finalize(argc, argv);
919 #ifndef LIBLUSTRE_TEST
920 int main (int argc, char **argv)
923 rc = loadgen_main(argc, argv);
924 pthread_exit((void *)(long)rc);