xref: /onnv-gate/usr/src/cmd/filebench/common/flowop_library.c (revision 7946:36d7e7afa9fa)
15184Sek110237 /*
25184Sek110237  * CDDL HEADER START
35184Sek110237  *
45184Sek110237  * The contents of this file are subject to the terms of the
55184Sek110237  * Common Development and Distribution License (the "License").
65184Sek110237  * You may not use this file except in compliance with the License.
75184Sek110237  *
85184Sek110237  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95184Sek110237  * or http://www.opensolaris.org/os/licensing.
105184Sek110237  * See the License for the specific language governing permissions
115184Sek110237  * and limitations under the License.
125184Sek110237  *
135184Sek110237  * When distributing Covered Code, include this CDDL HEADER in each
145184Sek110237  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155184Sek110237  * If applicable, add the following below this CDDL HEADER, with the
165184Sek110237  * fields enclosed by brackets "[]" replaced with your own identifying
175184Sek110237  * information: Portions Copyright [yyyy] [name of copyright owner]
185184Sek110237  *
195184Sek110237  * CDDL HEADER END
205184Sek110237  */
215184Sek110237 /*
226084Saw148015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235184Sek110237  * Use is subject to license terms.
246613Sek110237  *
256613Sek110237  * Portions Copyright 2008 Denis Cheng
265184Sek110237  */
275184Sek110237 
285184Sek110237 #include "config.h"
295184Sek110237 
305184Sek110237 #include <sys/types.h>
315184Sek110237 #ifdef HAVE_SYS_ASYNCH_H
325184Sek110237 #include <sys/asynch.h>
335184Sek110237 #endif
345184Sek110237 #include <sys/ipc.h>
355184Sek110237 #include <sys/sem.h>
365184Sek110237 #include <sys/errno.h>
375184Sek110237 #include <sys/time.h>
385184Sek110237 #include <inttypes.h>
395184Sek110237 #include <fcntl.h>
406212Saw148015 #include <math.h>
41*7946SAndrew.W.Wilson@sun.com #include <dirent.h>
425184Sek110237 
435184Sek110237 #ifdef HAVE_UTILITY_H
445184Sek110237 #include <utility.h>
455184Sek110237 #endif /* HAVE_UTILITY_H */
465184Sek110237 
475184Sek110237 #ifdef HAVE_AIO
485184Sek110237 #include <aio.h>
495184Sek110237 #endif /* HAVE_AIO */
505184Sek110237 
515184Sek110237 #ifdef HAVE_LIBAIO_H
525184Sek110237 #include <libaio.h>
535184Sek110237 #endif /* HAVE_LIBAIO_H */
545184Sek110237 
555184Sek110237 #ifdef HAVE_SYS_ASYNC_H
565184Sek110237 #include <sys/asynch.h>
575184Sek110237 #endif /* HAVE_SYS_ASYNC_H */
585184Sek110237 
595184Sek110237 #ifdef HAVE_AIO_H
605184Sek110237 #include <aio.h>
615184Sek110237 #endif /* HAVE_AIO_H */
625184Sek110237 
635184Sek110237 #ifndef HAVE_UINT_T
645184Sek110237 #define	uint_t unsigned int
655184Sek110237 #endif /* HAVE_UINT_T */
665184Sek110237 
675184Sek110237 #ifndef HAVE_AIOCB64_T
685184Sek110237 #define	aiocb64 aiocb
695184Sek110237 #endif /* HAVE_AIOCB64_T */
705184Sek110237 
715184Sek110237 #ifndef HAVE_SYSV_SEM
725184Sek110237 #include <semaphore.h>
735184Sek110237 #endif /* HAVE_SYSV_SEM */
745184Sek110237 
755184Sek110237 #include "filebench.h"
765184Sek110237 #include "flowop.h"
775184Sek110237 #include "fileset.h"
786212Saw148015 #include "fb_random.h"
79*7946SAndrew.W.Wilson@sun.com #include "utils.h"
805184Sek110237 /*
815184Sek110237  * These routines implement the flowops from the f language. Each
825184Sek110237  * flowop has has a name such as "read", and a set of function pointers
835184Sek110237  * to call for initialization, execution and destruction of the flowop.
845184Sek110237  * The table flowoplib_funcs[] contains a flowoplib struct for each
855184Sek110237  * implemented flowop. Most flowops use a generic initialization function
865184Sek110237  * and all currently use a generic destruction function. All flowop
875184Sek110237  * functions referenced from the table are in this file, though, of
885184Sek110237  * course, they often call functions from other files.
895184Sek110237  *
905184Sek110237  * The flowop_init() routine uses the flowoplib_funcs[] table to
915184Sek110237  * create an initial set of "instance 0" flowops, one for each type of
925184Sek110237  * flowop, from which all other flowops are derived. These "instance 0"
935184Sek110237  * flowops are initialized with information from the table including
945184Sek110237  * pointers for their fo_init, fo_func and fo_destroy functions. When
955184Sek110237  * a flowop definition is encountered in an f language script, the
965184Sek110237  * "type" of flowop, such as "read" is used to search for the
975184Sek110237  * "instance 0" flowop named "read", then a new flowop is allocated
985184Sek110237  * which inherits its function pointers and other initial properties
995184Sek110237  * from the instance 0 flowop, and is given a new name as specified
1005184Sek110237  * by the "name=" attribute.
1015184Sek110237  */
1025184Sek110237 
1035184Sek110237 static int flowoplib_init_generic(flowop_t *flowop);
1045184Sek110237 static void flowoplib_destruct_generic(flowop_t *flowop);
1056084Saw148015 static void flowoplib_destruct_noop(flowop_t *flowop);
1065184Sek110237 static int flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop);
1077556SAndrew.W.Wilson@sun.com static int flowoplib_print(threadflow_t *threadflow, flowop_t *flowop);
1085184Sek110237 static int flowoplib_write(threadflow_t *threadflow, flowop_t *flowop);
1095184Sek110237 #ifdef HAVE_AIO
1105184Sek110237 static int flowoplib_aiowrite(threadflow_t *threadflow, flowop_t *flowop);
1115184Sek110237 static int flowoplib_aiowait(threadflow_t *threadflow, flowop_t *flowop);
1125184Sek110237 #endif
1135184Sek110237 static int flowoplib_read(threadflow_t *threadflow, flowop_t *flowop);
1145184Sek110237 static int flowoplib_block_init(flowop_t *flowop);
1155184Sek110237 static int flowoplib_block(threadflow_t *threadflow, flowop_t *flowop);
1165184Sek110237 static int flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop);
1175184Sek110237 static int flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop);
1185184Sek110237 static int flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop);
1195184Sek110237 static int flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop);
1205184Sek110237 static int flowoplib_sempost_init(flowop_t *flowop);
1215184Sek110237 static int flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop);
1225184Sek110237 static int flowoplib_semblock_init(flowop_t *flowop);
1235184Sek110237 static void flowoplib_semblock_destruct(flowop_t *flowop);
1245184Sek110237 static int flowoplib_eventlimit(threadflow_t *, flowop_t *flowop);
1255184Sek110237 static int flowoplib_bwlimit(threadflow_t *, flowop_t *flowop);
1265184Sek110237 static int flowoplib_iopslimit(threadflow_t *, flowop_t *flowop);
1275184Sek110237 static int flowoplib_opslimit(threadflow_t *, flowop_t *flowop);
1285184Sek110237 static int flowoplib_openfile(threadflow_t *, flowop_t *flowop);
1295184Sek110237 static int flowoplib_openfile_common(threadflow_t *, flowop_t *flowop, int fd);
1305184Sek110237 static int flowoplib_createfile(threadflow_t *, flowop_t *flowop);
1315184Sek110237 static int flowoplib_closefile(threadflow_t *, flowop_t *flowop);
132*7946SAndrew.W.Wilson@sun.com static int flowoplib_makedir(threadflow_t *, flowop_t *flowop);
133*7946SAndrew.W.Wilson@sun.com static int flowoplib_removedir(threadflow_t *, flowop_t *flowop);
134*7946SAndrew.W.Wilson@sun.com static int flowoplib_listdir(threadflow_t *, flowop_t *flowop);
1355184Sek110237 static int flowoplib_fsync(threadflow_t *, flowop_t *flowop);
1365184Sek110237 static int flowoplib_readwholefile(threadflow_t *, flowop_t *flowop);
1375184Sek110237 static int flowoplib_writewholefile(threadflow_t *, flowop_t *flowop);
1385184Sek110237 static int flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop);
1395184Sek110237 static int flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop);
1405184Sek110237 static int flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop);
1415184Sek110237 static int flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop);
1425184Sek110237 static int flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop);
1435184Sek110237 static int flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop);
1445184Sek110237 static int flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop);
1456212Saw148015 static int flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop);
1466212Saw148015 static int flowoplib_testrandvar_init(flowop_t *flowop);
1476212Saw148015 static void flowoplib_testrandvar_destruct(flowop_t *flowop);
1485184Sek110237 
1495184Sek110237 typedef struct flowoplib {
1505184Sek110237 	int	fl_type;
1515184Sek110237 	int	fl_attrs;
1525184Sek110237 	char	*fl_name;
1535184Sek110237 	int	(*fl_init)();
1545184Sek110237 	int	(*fl_func)();
1555184Sek110237 	void	(*fl_destruct)();
1565184Sek110237 } flowoplib_t;
1575184Sek110237 
1585184Sek110237 static flowoplib_t flowoplib_funcs[] = {
1595184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "write", flowoplib_init_generic,
1605184Sek110237 	flowoplib_write, flowoplib_destruct_generic,
1615184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_READ, "read", flowoplib_init_generic,
1625184Sek110237 	flowoplib_read, flowoplib_destruct_generic,
1635184Sek110237 #ifdef HAVE_AIO
1645184Sek110237 	FLOW_TYPE_AIO, FLOW_ATTR_WRITE, "aiowrite", flowoplib_init_generic,
1655184Sek110237 	flowoplib_aiowrite, flowoplib_destruct_generic,
1665184Sek110237 	FLOW_TYPE_AIO, 0, "aiowait", flowoplib_init_generic,
1675184Sek110237 	flowoplib_aiowait, flowoplib_destruct_generic,
1685184Sek110237 #endif
1695184Sek110237 	FLOW_TYPE_SYNC, 0, "block", flowoplib_block_init,
1705184Sek110237 	flowoplib_block, flowoplib_destruct_generic,
1715184Sek110237 	FLOW_TYPE_SYNC, 0, "wakeup", flowoplib_init_generic,
1725184Sek110237 	flowoplib_wakeup, flowoplib_destruct_generic,
1735184Sek110237 	FLOW_TYPE_SYNC, 0, "semblock", flowoplib_semblock_init,
1745184Sek110237 	flowoplib_semblock, flowoplib_semblock_destruct,
1755184Sek110237 	FLOW_TYPE_SYNC, 0, "sempost", flowoplib_sempost_init,
1766084Saw148015 	flowoplib_sempost, flowoplib_destruct_noop,
1775184Sek110237 	FLOW_TYPE_OTHER, 0, "hog", flowoplib_init_generic,
1785184Sek110237 	flowoplib_hog, flowoplib_destruct_generic,
1795184Sek110237 	FLOW_TYPE_OTHER, 0, "delay", flowoplib_init_generic,
1805184Sek110237 	flowoplib_delay, flowoplib_destruct_generic,
1815184Sek110237 	FLOW_TYPE_OTHER, 0, "eventlimit", flowoplib_init_generic,
1825184Sek110237 	flowoplib_eventlimit, flowoplib_destruct_generic,
1835184Sek110237 	FLOW_TYPE_OTHER, 0, "bwlimit", flowoplib_init_generic,
1845184Sek110237 	flowoplib_bwlimit, flowoplib_destruct_generic,
1855184Sek110237 	FLOW_TYPE_OTHER, 0, "iopslimit", flowoplib_init_generic,
1865184Sek110237 	flowoplib_iopslimit, flowoplib_destruct_generic,
1875184Sek110237 	FLOW_TYPE_OTHER, 0, "opslimit", flowoplib_init_generic,
1885184Sek110237 	flowoplib_opslimit, flowoplib_destruct_generic,
1895184Sek110237 	FLOW_TYPE_OTHER, 0, "finishoncount", flowoplib_init_generic,
1905184Sek110237 	flowoplib_finishoncount, flowoplib_destruct_generic,
1915184Sek110237 	FLOW_TYPE_OTHER, 0, "finishonbytes", flowoplib_init_generic,
1925184Sek110237 	flowoplib_finishonbytes, flowoplib_destruct_generic,
1935184Sek110237 	FLOW_TYPE_IO, 0, "openfile", flowoplib_init_generic,
1945184Sek110237 	flowoplib_openfile, flowoplib_destruct_generic,
1955184Sek110237 	FLOW_TYPE_IO, 0, "createfile", flowoplib_init_generic,
1965184Sek110237 	flowoplib_createfile, flowoplib_destruct_generic,
1975184Sek110237 	FLOW_TYPE_IO, 0, "closefile", flowoplib_init_generic,
1985184Sek110237 	flowoplib_closefile, flowoplib_destruct_generic,
199*7946SAndrew.W.Wilson@sun.com 	FLOW_TYPE_IO, 0, "makedir", flowoplib_init_generic,
200*7946SAndrew.W.Wilson@sun.com 	flowoplib_makedir, flowoplib_destruct_generic,
201*7946SAndrew.W.Wilson@sun.com 	FLOW_TYPE_IO, 0, "removedir", flowoplib_init_generic,
202*7946SAndrew.W.Wilson@sun.com 	flowoplib_removedir, flowoplib_destruct_generic,
203*7946SAndrew.W.Wilson@sun.com 	FLOW_TYPE_IO, 0, "listdir", flowoplib_init_generic,
204*7946SAndrew.W.Wilson@sun.com 	flowoplib_listdir, flowoplib_destruct_generic,
2055184Sek110237 	FLOW_TYPE_IO, 0, "fsync", flowoplib_init_generic,
2065184Sek110237 	flowoplib_fsync, flowoplib_destruct_generic,
2075184Sek110237 	FLOW_TYPE_IO, 0, "fsyncset", flowoplib_init_generic,
2085184Sek110237 	flowoplib_fsyncset, flowoplib_destruct_generic,
2095184Sek110237 	FLOW_TYPE_IO, 0, "statfile", flowoplib_init_generic,
2105184Sek110237 	flowoplib_statfile, flowoplib_destruct_generic,
2115184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_READ, "readwholefile", flowoplib_init_generic,
2125184Sek110237 	flowoplib_readwholefile, flowoplib_destruct_generic,
2135184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfile", flowoplib_init_generic,
2145184Sek110237 	flowoplib_appendfile, flowoplib_destruct_generic,
2155184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfilerand", flowoplib_init_generic,
2165184Sek110237 	flowoplib_appendfilerand, flowoplib_destruct_generic,
2175184Sek110237 	FLOW_TYPE_IO, 0, "deletefile", flowoplib_init_generic,
2185184Sek110237 	flowoplib_deletefile, flowoplib_destruct_generic,
2195184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "writewholefile", flowoplib_init_generic,
2206212Saw148015 	flowoplib_writewholefile, flowoplib_destruct_generic,
2217556SAndrew.W.Wilson@sun.com 	FLOW_TYPE_OTHER, 0, "print", flowoplib_init_generic,
2227556SAndrew.W.Wilson@sun.com 	flowoplib_print, flowoplib_destruct_generic,
2236212Saw148015 	/* routine to calculate mean and stddev for output from a randvar */
2246212Saw148015 	FLOW_TYPE_OTHER, 0, "testrandvar", flowoplib_testrandvar_init,
2256212Saw148015 	flowoplib_testrandvar, flowoplib_testrandvar_destruct
2265184Sek110237 };
2275184Sek110237 
2285184Sek110237 /*
2295184Sek110237  * Loops through the master list of flowops defined in this
2305184Sek110237  * module, and creates and initializes a flowop for each one
2315184Sek110237  * by calling flowop_define. As a side effect of calling
2325184Sek110237  * flowop define, the created flowops are placed on the
2335184Sek110237  * master flowop list. All created flowops are set to
2345184Sek110237  * instance "0".
2355184Sek110237  */
2365184Sek110237 void
2375184Sek110237 flowoplib_init()
2385184Sek110237 {
2395184Sek110237 	int nops = sizeof (flowoplib_funcs) / sizeof (flowoplib_t);
2405184Sek110237 	int i;
2415184Sek110237 
2425184Sek110237 	for (i = 0; i < nops; i++) {
2435184Sek110237 		flowop_t *flowop;
2445184Sek110237 		flowoplib_t *fl;
2455184Sek110237 
2465184Sek110237 		fl = &flowoplib_funcs[i];
2475184Sek110237 
2485184Sek110237 		if ((flowop = flowop_define(NULL,
2496550Saw148015 		    fl->fl_name, NULL, NULL, 0, fl->fl_type)) == 0) {
2505184Sek110237 			filebench_log(LOG_ERROR,
2515184Sek110237 			    "failed to create flowop %s\n",
2525184Sek110237 			    fl->fl_name);
2535184Sek110237 			filebench_shutdown(1);
2545184Sek110237 		}
2555184Sek110237 
2565184Sek110237 		flowop->fo_func = fl->fl_func;
2575184Sek110237 		flowop->fo_init = fl->fl_init;
2585184Sek110237 		flowop->fo_destruct = fl->fl_destruct;
2595184Sek110237 		flowop->fo_attrs = fl->fl_attrs;
2605184Sek110237 	}
2615184Sek110237 }
2625184Sek110237 
2635184Sek110237 static int
2645184Sek110237 flowoplib_init_generic(flowop_t *flowop)
2655184Sek110237 {
2665184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
2676084Saw148015 	return (FILEBENCH_OK);
2685184Sek110237 }
2695184Sek110237 
2705184Sek110237 static void
2715184Sek110237 flowoplib_destruct_generic(flowop_t *flowop)
2725184Sek110237 {
2736084Saw148015 	char *buf;
2746084Saw148015 
2756084Saw148015 	/* release any local resources held by the flowop */
2766084Saw148015 	(void) ipc_mutex_lock(&flowop->fo_lock);
2776084Saw148015 	buf = flowop->fo_buf;
2786084Saw148015 	flowop->fo_buf = NULL;
2796084Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
2806084Saw148015 
2816084Saw148015 	if (buf)
2826084Saw148015 		free(buf);
2836084Saw148015 }
2846084Saw148015 
2856084Saw148015 /*
2866084Saw148015  * Special total noop destruct
2876084Saw148015  */
2886084Saw148015 /* ARGSUSED */
2896084Saw148015 static void
2906084Saw148015 flowoplib_destruct_noop(flowop_t *flowop)
2916084Saw148015 {
2925184Sek110237 }
2935184Sek110237 
2945184Sek110237 /*
2955184Sek110237  * Generates a file attribute from flags in the supplied flowop.
2965184Sek110237  * Sets FLOW_ATTR_DIRECTIO and/or FLOW_ATTR_DSYNC as needed.
2975184Sek110237  */
2985184Sek110237 static int
2995184Sek110237 flowoplib_fileattrs(flowop_t *flowop)
3005184Sek110237 {
3015184Sek110237 	int attrs = 0;
3025184Sek110237 
3036212Saw148015 	if (avd_get_bool(flowop->fo_directio))
3045184Sek110237 		attrs |= FLOW_ATTR_DIRECTIO;
3055184Sek110237 
3066212Saw148015 	if (avd_get_bool(flowop->fo_dsync))
3075184Sek110237 		attrs |= FLOW_ATTR_DSYNC;
3085184Sek110237 
3095184Sek110237 	return (attrs);
3105184Sek110237 }
3115184Sek110237 
3125184Sek110237 /*
3135184Sek110237  * Searches for a file descriptor. Tries the flowop's
3145184Sek110237  * fo_fdnumber first and returns with it if it has been
3155184Sek110237  * explicitly set (greater than 0). It next checks to
3165184Sek110237  * see if a rotating file descriptor policy is in effect,
3175184Sek110237  * and if not returns the fdnumber regardless of what
3185184Sek110237  * it is. (note that if it is 0, it just selects to the
3195184Sek110237  * default file descriptor in the threadflow's tf_fd
3205184Sek110237  * array). If the rotating fd policy is in effect, it
3215184Sek110237  * cycles from the end of the tf_fd array to one location
3225184Sek110237  * beyond the maximum needed by the number of entries in
3235184Sek110237  * the associated fileset on each invocation, then starts
3245184Sek110237  * over from the end.
3255184Sek110237  *
3265184Sek110237  * The routine returns an index into the threadflow's
3275184Sek110237  * tf_fd table where the actual file descriptor will be
3285184Sek110237  * found. Note: the calling routine must not call this
3295184Sek110237  * routine if the flowop does not have a fileset, and the
3305184Sek110237  * flowop's fo_fdnumber is zero and fo_rotatefd is
3315184Sek110237  * asserted, or an addressing fault may occur.
3325184Sek110237  */
3335673Saw148015 static int
3345184Sek110237 flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop)
3355184Sek110237 {
3366212Saw148015 	fbint_t	entries;
3376391Saw148015 	int fdnumber = flowop->fo_fdnumber;
3386212Saw148015 
3395184Sek110237 	/* If the script sets the fd explicitly */
3406391Saw148015 	if (fdnumber > 0)
3416391Saw148015 		return (fdnumber);
3425184Sek110237 
3435184Sek110237 	/* If the flowop defaults to persistent fd */
3446212Saw148015 	if (!avd_get_bool(flowop->fo_rotatefd))
3456391Saw148015 		return (fdnumber);
3466391Saw148015 
3476391Saw148015 	if (flowop->fo_fileset == NULL) {
3486391Saw148015 		filebench_log(LOG_ERROR, "flowop NULL file");
3496391Saw148015 		return (FILEBENCH_ERROR);
3506391Saw148015 	}
3515184Sek110237 
3526212Saw148015 	entries = flowop->fo_fileset->fs_constentries;
3536212Saw148015 
3545184Sek110237 	/* Rotate the fd on each flowop invocation */
3556212Saw148015 	if (entries > (THREADFLOW_MAXFD / 2)) {
3565184Sek110237 		filebench_log(LOG_ERROR, "Out of file descriptors in flowop %s"
3576286Saw148015 		    " (too many files : %llu",
3586286Saw148015 		    flowop->fo_name, (u_longlong_t)entries);
3596084Saw148015 		return (FILEBENCH_ERROR);
3605184Sek110237 	}
3615184Sek110237 
3625184Sek110237 	/* First time around */
3635184Sek110237 	if (threadflow->tf_fdrotor == 0)
3645184Sek110237 		threadflow->tf_fdrotor = THREADFLOW_MAXFD;
3655184Sek110237 
3665184Sek110237 	/* One fd for every file in the set */
3676212Saw148015 	if (entries == (THREADFLOW_MAXFD - threadflow->tf_fdrotor))
3685184Sek110237 		threadflow->tf_fdrotor = THREADFLOW_MAXFD;
3695184Sek110237 
3705184Sek110237 
3715184Sek110237 	threadflow->tf_fdrotor--;
3725184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "selected fd = %d",
3735184Sek110237 	    threadflow->tf_fdrotor);
3745184Sek110237 	return (threadflow->tf_fdrotor);
3755184Sek110237 }
3765184Sek110237 
3775184Sek110237 /*
3785673Saw148015  * Determines the file descriptor to use, and attempts to open
3795673Saw148015  * the file if it is not already open. Also determines the wss
3806084Saw148015  * value. Returns FILEBENCH_ERROR on errors, FILESET_NORSC if
3816084Saw148015  * if flowop_openfile_common couldn't obtain an appropriate file
3826084Saw148015  * from a the fileset, and FILEBENCH_OK otherwise.
3835673Saw148015  */
3845673Saw148015 static int
3855673Saw148015 flowoplib_filesetup(threadflow_t *threadflow, flowop_t *flowop,
3866212Saw148015     fbint_t *wssp, int *filedescp)
3875673Saw148015 {
3885673Saw148015 	int fd = flowoplib_fdnum(threadflow, flowop);
3895673Saw148015 
3905673Saw148015 	if (fd == -1)
3916084Saw148015 		return (FILEBENCH_ERROR);
3925673Saw148015 
3935673Saw148015 	if (threadflow->tf_fd[fd] == 0) {
3946084Saw148015 		int ret;
3956084Saw148015 
3966084Saw148015 		if ((ret = flowoplib_openfile_common(
3976084Saw148015 		    threadflow, flowop, fd)) != FILEBENCH_OK)
3986084Saw148015 			return (ret);
3995673Saw148015 
4005673Saw148015 		if (threadflow->tf_fse[fd]) {
4015673Saw148015 			filebench_log(LOG_DEBUG_IMPL, "opened file %s",
4025673Saw148015 			    threadflow->tf_fse[fd]->fse_path);
4035673Saw148015 		} else {
4045673Saw148015 			filebench_log(LOG_DEBUG_IMPL,
4055673Saw148015 			    "opened device %s/%s",
4066212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_path),
4076212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_name));
4085673Saw148015 		}
4095673Saw148015 	}
4105673Saw148015 
4115673Saw148015 	*filedescp = threadflow->tf_fd[fd];
4125673Saw148015 
4136212Saw148015 	if ((*wssp = flowop->fo_constwss) == 0) {
4145673Saw148015 		if (threadflow->tf_fse[fd])
4155673Saw148015 			*wssp = threadflow->tf_fse[fd]->fse_size;
4165673Saw148015 		else
4176212Saw148015 			*wssp = avd_get_int(flowop->fo_fileset->fs_size);
4185673Saw148015 	}
4195673Saw148015 
4206084Saw148015 	return (FILEBENCH_OK);
4215673Saw148015 }
4225673Saw148015 
4235673Saw148015 /*
4245673Saw148015  * Determines the io buffer or random offset into tf_mem for
4256084Saw148015  * the IO operation. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
4265673Saw148015  */
4275673Saw148015 static int
4285673Saw148015 flowoplib_iobufsetup(threadflow_t *threadflow, flowop_t *flowop,
4296212Saw148015     caddr_t *iobufp, fbint_t iosize)
4305673Saw148015 {
4315673Saw148015 	long memsize;
4325673Saw148015 	size_t memoffset;
4335673Saw148015 
4345673Saw148015 	if (iosize == 0) {
4355673Saw148015 		filebench_log(LOG_ERROR, "zero iosize for thread %s",
4365673Saw148015 		    flowop->fo_name);
4376084Saw148015 		return (FILEBENCH_ERROR);
4385673Saw148015 	}
4395673Saw148015 
4406212Saw148015 	if ((memsize = threadflow->tf_constmemsize) != 0) {
4415673Saw148015 
4425673Saw148015 		/* use tf_mem for I/O with random offset */
4436212Saw148015 		if (filebench_randomno(&memoffset,
4446212Saw148015 		    memsize, iosize, NULL) == -1) {
4455673Saw148015 			filebench_log(LOG_ERROR,
4465673Saw148015 			    "tf_memsize smaller than IO size for thread %s",
4475673Saw148015 			    flowop->fo_name);
4486084Saw148015 			return (FILEBENCH_ERROR);
4495673Saw148015 		}
4505673Saw148015 		*iobufp = threadflow->tf_mem + memoffset;
4515673Saw148015 
4525673Saw148015 	} else {
4535673Saw148015 		/* use private I/O buffer */
4545673Saw148015 		if ((flowop->fo_buf != NULL) &&
4555673Saw148015 		    (flowop->fo_buf_size < iosize)) {
4566212Saw148015 			/* too small, so free up and re-allocate */
4575673Saw148015 			free(flowop->fo_buf);
4585673Saw148015 			flowop->fo_buf = NULL;
4595673Saw148015 		}
4606212Saw148015 
4616212Saw148015 		/*
4626212Saw148015 		 * Allocate memory for the  buffer. The memory is freed
4636212Saw148015 		 * by flowop_destruct_generic() or by this routine if more
4646212Saw148015 		 * memory is needed for the buffer.
4656212Saw148015 		 */
4665673Saw148015 		if ((flowop->fo_buf == NULL) && ((flowop->fo_buf
4675673Saw148015 		    = (char *)malloc(iosize)) == NULL))
4686084Saw148015 			return (FILEBENCH_ERROR);
4695673Saw148015 
4705673Saw148015 		flowop->fo_buf_size = iosize;
4715673Saw148015 		*iobufp = flowop->fo_buf;
4725673Saw148015 	}
4736084Saw148015 	return (FILEBENCH_OK);
4745673Saw148015 }
4755673Saw148015 
4765673Saw148015 /*
4775673Saw148015  * Determines the file descriptor to use, opens it if necessary, the
4785673Saw148015  * io buffer or random offset into tf_mem for IO operation and the wss
4796084Saw148015  * value. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
4805673Saw148015  */
4815673Saw148015 static int
4825673Saw148015 flowoplib_iosetup(threadflow_t *threadflow, flowop_t *flowop,
4836212Saw148015     fbint_t *wssp, caddr_t *iobufp, int *filedescp, fbint_t iosize)
4845673Saw148015 {
4856084Saw148015 	int ret;
4866084Saw148015 
4876084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, wssp, filedescp)) !=
4886084Saw148015 	    FILEBENCH_OK)
4896084Saw148015 		return (ret);
4905673Saw148015 
4916084Saw148015 	if ((ret = flowoplib_iobufsetup(threadflow, flowop, iobufp, iosize)) !=
4926084Saw148015 	    FILEBENCH_OK)
4936084Saw148015 		return (ret);
4945673Saw148015 
4956084Saw148015 	return (FILEBENCH_OK);
4965673Saw148015 }
4975673Saw148015 
4985673Saw148015 /*
4995184Sek110237  * Emulate posix read / pread. If the flowop has a fileset,
5005184Sek110237  * a file descriptor number index is fetched, otherwise a
5015184Sek110237  * supplied fileobj file is used. In either case the specified
5025184Sek110237  * file will be opened if not already open. If the flowop has
5036084Saw148015  * neither a fileset or fileobj, an error is logged and FILEBENCH_ERROR
5045184Sek110237  * returned.
5055184Sek110237  *
5065184Sek110237  * The actual read is done to a random offset in the
5075184Sek110237  * threadflow's thread memory (tf_mem), with a size set by
5085184Sek110237  * fo_iosize and at either a random disk offset within the
5095184Sek110237  * working set size, or at the next sequential location. If
5106084Saw148015  * any errors are encountered, FILEBENCH_ERROR is returned,
5116084Saw148015  * if no appropriate file can be obtained from the fileset then
5126084Saw148015  * FILEBENCH_NORSC is returned, otherise FILEBENCH_OK is returned.
5135184Sek110237  */
5145184Sek110237 static int
5155184Sek110237 flowoplib_read(threadflow_t *threadflow, flowop_t *flowop)
5165184Sek110237 {
5175673Saw148015 	caddr_t iobuf;
5186212Saw148015 	fbint_t wss;
5196212Saw148015 	fbint_t iosize;
5205184Sek110237 	int filedesc;
5215184Sek110237 	int ret;
5225184Sek110237 
5236212Saw148015 
5246212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
5256084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
5266212Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
5276084Saw148015 		return (ret);
5285184Sek110237 
5296212Saw148015 	if (avd_get_bool(flowop->fo_random)) {
5305184Sek110237 		uint64_t fileoffset;
5315184Sek110237 
5326212Saw148015 		if (filebench_randomno64(&fileoffset,
5336212Saw148015 		    wss, iosize, NULL) == -1) {
5345184Sek110237 			filebench_log(LOG_ERROR,
5355184Sek110237 			    "file size smaller than IO size for thread %s",
5365184Sek110237 			    flowop->fo_name);
5376084Saw148015 			return (FILEBENCH_ERROR);
5385184Sek110237 		}
5395184Sek110237 
5405184Sek110237 		(void) flowop_beginop(threadflow, flowop);
5415673Saw148015 		if ((ret = pread64(filedesc, iobuf,
5426212Saw148015 		    iosize, (off64_t)fileoffset)) == -1) {
5435673Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5445184Sek110237 			filebench_log(LOG_ERROR,
5456286Saw148015 			    "read file %s failed, offset %llu "
5465673Saw148015 			    "io buffer %zd: %s",
5476212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_name),
5486286Saw148015 			    (u_longlong_t)fileoffset, iobuf, strerror(errno));
5495673Saw148015 			flowop_endop(threadflow, flowop, 0);
5506084Saw148015 			return (FILEBENCH_ERROR);
5515184Sek110237 		}
5525673Saw148015 		(void) flowop_endop(threadflow, flowop, ret);
5535184Sek110237 
5545184Sek110237 		if ((ret == 0))
5555184Sek110237 			(void) lseek64(filedesc, 0, SEEK_SET);
5565184Sek110237 
5575184Sek110237 	} else {
5585184Sek110237 		(void) flowop_beginop(threadflow, flowop);
5596212Saw148015 		if ((ret = read(filedesc, iobuf, iosize)) == -1) {
5606212Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5615184Sek110237 			filebench_log(LOG_ERROR,
5625673Saw148015 			    "read file %s failed, io buffer %zd: %s",
5636212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_name),
5645673Saw148015 			    iobuf, strerror(errno));
5655673Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5666084Saw148015 			return (FILEBENCH_ERROR);
5675184Sek110237 		}
5685673Saw148015 		(void) flowop_endop(threadflow, flowop, ret);
5695184Sek110237 
5705184Sek110237 		if ((ret == 0))
5715184Sek110237 			(void) lseek64(filedesc, 0, SEEK_SET);
5725184Sek110237 	}
5735184Sek110237 
5746084Saw148015 	return (FILEBENCH_OK);
5755184Sek110237 }
5765184Sek110237 
5775184Sek110237 #ifdef HAVE_AIO
5785184Sek110237 
5795184Sek110237 /*
5805184Sek110237  * Asynchronous write section. An Asynchronous IO element
5815184Sek110237  * (aiolist_t) is used to associate the asynchronous write request with
5825184Sek110237  * its subsequent completion. This element includes a aiocb64 struct
5835184Sek110237  * that is used by posix aio_xxx calls to track the asynchronous writes.
5845184Sek110237  * The flowops aiowrite and aiowait result in calls to these posix
5855184Sek110237  * aio_xxx system routines to do the actual asynchronous write IO
5865184Sek110237  * operations.
5875184Sek110237  */
5885184Sek110237 
5895184Sek110237 
5905184Sek110237 /*
5915184Sek110237  * Allocates an asynchronous I/O list (aio, of type
5925184Sek110237  * aiolist_t) element. Adds it to the flowop thread's
5935184Sek110237  * threadflow aio list. Returns a pointer to the element.
5945184Sek110237  */
5955184Sek110237 static aiolist_t *
5965184Sek110237 aio_allocate(flowop_t *flowop)
5975184Sek110237 {
5985184Sek110237 	aiolist_t *aiolist;
5995184Sek110237 
6005184Sek110237 	if ((aiolist = malloc(sizeof (aiolist_t))) == NULL) {
6015184Sek110237 		filebench_log(LOG_ERROR, "malloc aiolist failed");
6025184Sek110237 		filebench_shutdown(1);
6035184Sek110237 	}
6045184Sek110237 
6055184Sek110237 	/* Add to list */
6065184Sek110237 	if (flowop->fo_thread->tf_aiolist == NULL) {
6075184Sek110237 		flowop->fo_thread->tf_aiolist = aiolist;
6085184Sek110237 		aiolist->al_next = NULL;
6095184Sek110237 	} else {
6105184Sek110237 		aiolist->al_next = flowop->fo_thread->tf_aiolist;
6115184Sek110237 		flowop->fo_thread->tf_aiolist = aiolist;
6125184Sek110237 	}
6135184Sek110237 	return (aiolist);
6145184Sek110237 }
6155184Sek110237 
6165184Sek110237 /*
6175184Sek110237  * Searches for the aiolist element that has a matching
6186084Saw148015  * completion block, aiocb. If none found returns FILEBENCH_ERROR. If
6195184Sek110237  * found, removes the aiolist element from flowop thread's
6206084Saw148015  * list and returns FILEBENCH_OK.
6215184Sek110237  */
6225184Sek110237 static int
6235184Sek110237 aio_deallocate(flowop_t *flowop, struct aiocb64 *aiocb)
6245184Sek110237 {
6255184Sek110237 	aiolist_t *aiolist = flowop->fo_thread->tf_aiolist;
6265184Sek110237 	aiolist_t *previous = NULL;
6275184Sek110237 	aiolist_t *match = NULL;
6285184Sek110237 
6295184Sek110237 	if (aiocb == NULL) {
6305184Sek110237 		filebench_log(LOG_ERROR, "null aiocb deallocate");
6316084Saw148015 		return (FILEBENCH_OK);
6325184Sek110237 	}
6335184Sek110237 
6345184Sek110237 	while (aiolist) {
6355184Sek110237 		if (aiocb == &(aiolist->al_aiocb)) {
6365184Sek110237 			match = aiolist;
6375184Sek110237 			break;
6385184Sek110237 		}
6395184Sek110237 		previous = aiolist;
6405184Sek110237 		aiolist = aiolist->al_next;
6415184Sek110237 	}
6425184Sek110237 
6435184Sek110237 	if (match == NULL)
6446084Saw148015 		return (FILEBENCH_ERROR);
6455184Sek110237 
6465184Sek110237 	/* Remove from the list */
6475184Sek110237 	if (previous)
6485184Sek110237 		previous->al_next = match->al_next;
6495184Sek110237 	else
6505184Sek110237 		flowop->fo_thread->tf_aiolist = match->al_next;
6515184Sek110237 
6526084Saw148015 	return (FILEBENCH_OK);
6535184Sek110237 }
6545184Sek110237 
6555184Sek110237 /*
6565184Sek110237  * Emulate posix aiowrite(). Determines which file to use,
6575184Sek110237  * either one file of a fileset, or the file associated
6585184Sek110237  * with a fileobj, allocates and fills an aiolist_t element
6595184Sek110237  * for the write, and issues the asynchronous write. This
6605184Sek110237  * operation is only valid for random IO, and returns an
6616084Saw148015  * error if the flowop is set for sequential IO. Returns
6626084Saw148015  * FILEBENCH_OK on success, FILEBENCH_NORSC if iosetup can't
6636084Saw148015  * obtain a file to open, and FILEBENCH_ERROR on any
6646084Saw148015  * encountered error.
6655184Sek110237  */
6665184Sek110237 static int
6675184Sek110237 flowoplib_aiowrite(threadflow_t *threadflow, flowop_t *flowop)
6685184Sek110237 {
6695673Saw148015 	caddr_t iobuf;
6706212Saw148015 	fbint_t wss;
6716212Saw148015 	fbint_t iosize;
6725184Sek110237 	int filedesc;
6736084Saw148015 	int ret;
6745184Sek110237 
6756212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
6766212Saw148015 
6776084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
6786212Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
6796084Saw148015 		return (ret);
6805184Sek110237 
6816212Saw148015 	if (avd_get_bool(flowop->fo_random)) {
6825184Sek110237 		uint64_t fileoffset;
6835184Sek110237 		struct aiocb64 *aiocb;
6845184Sek110237 		aiolist_t *aiolist;
6855184Sek110237 
6865184Sek110237 		if (filebench_randomno64(&fileoffset,
6876212Saw148015 		    wss, iosize, NULL) == -1) {
6885184Sek110237 			filebench_log(LOG_ERROR,
6895184Sek110237 			    "file size smaller than IO size for thread %s",
6905184Sek110237 			    flowop->fo_name);
6916084Saw148015 			return (FILEBENCH_ERROR);
6925184Sek110237 		}
6935184Sek110237 
6945184Sek110237 		aiolist = aio_allocate(flowop);
6955184Sek110237 		aiolist->al_type = AL_WRITE;
6965184Sek110237 		aiocb = &aiolist->al_aiocb;
6975184Sek110237 
6985184Sek110237 		aiocb->aio_fildes = filedesc;
6995673Saw148015 		aiocb->aio_buf = iobuf;
7006212Saw148015 		aiocb->aio_nbytes = (size_t)iosize;
7015184Sek110237 		aiocb->aio_offset = (off64_t)fileoffset;
7025184Sek110237 		aiocb->aio_reqprio = 0;
7035184Sek110237 
7045184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
7056286Saw148015 		    "aio fd=%d, bytes=%llu, offset=%llu",
7066286Saw148015 		    filedesc, (u_longlong_t)iosize, (u_longlong_t)fileoffset);
7075184Sek110237 
7085184Sek110237 		flowop_beginop(threadflow, flowop);
7095184Sek110237 		if (aio_write64(aiocb) < 0) {
7105184Sek110237 			filebench_log(LOG_ERROR, "aiowrite failed: %s",
7115184Sek110237 			    strerror(errno));
7125184Sek110237 			filebench_shutdown(1);
7135184Sek110237 		}
7146212Saw148015 		flowop_endop(threadflow, flowop, iosize);
7155184Sek110237 	} else {
7166084Saw148015 		return (FILEBENCH_ERROR);
7175184Sek110237 	}
7185184Sek110237 
7196084Saw148015 	return (FILEBENCH_OK);
7205184Sek110237 }
7215184Sek110237 
7225184Sek110237 
7235184Sek110237 
7245184Sek110237 #define	MAXREAP 4096
7255184Sek110237 
7265184Sek110237 /*
7275184Sek110237  * Emulate posix aiowait(). Waits for the completion of half the
7285184Sek110237  * outstanding asynchronous IOs, or a single IO, which ever is
7295184Sek110237  * larger. The routine will return after a sufficient number of
7305184Sek110237  * completed calls issued by any thread in the procflow have
7315184Sek110237  * completed, or a 1 second timout elapses. All completed
7325184Sek110237  * IO operations are deleted from the thread's aiolist.
7335184Sek110237  */
7345184Sek110237 static int
7355184Sek110237 flowoplib_aiowait(threadflow_t *threadflow, flowop_t *flowop)
7365184Sek110237 {
7375184Sek110237 	struct aiocb64 **worklist;
7385184Sek110237 	aiolist_t *aio = flowop->fo_thread->tf_aiolist;
7395184Sek110237 	int uncompleted = 0;
7405184Sek110237 
7415184Sek110237 	worklist = calloc(MAXREAP, sizeof (struct aiocb64 *));
7425184Sek110237 
7435184Sek110237 	/* Count the list of pending aios */
7445184Sek110237 	while (aio) {
7455184Sek110237 		uncompleted++;
7465184Sek110237 		aio = aio->al_next;
7475184Sek110237 	}
7485184Sek110237 
7495184Sek110237 	do {
7505184Sek110237 		uint_t ncompleted = 0;
7515184Sek110237 		uint_t todo;
7525184Sek110237 		struct timespec timeout;
7535184Sek110237 		int inprogress;
7545184Sek110237 		int i;
7555184Sek110237 
7565184Sek110237 		/* Wait for half of the outstanding requests */
7575184Sek110237 		timeout.tv_sec = 1;
7585184Sek110237 		timeout.tv_nsec = 0;
7595184Sek110237 
7605184Sek110237 		if (uncompleted > MAXREAP)
7615184Sek110237 			todo = MAXREAP;
7625184Sek110237 		else
7635184Sek110237 			todo = uncompleted / 2;
7645184Sek110237 
7655184Sek110237 		if (todo == 0)
7665184Sek110237 			todo = 1;
7675184Sek110237 
7685184Sek110237 		flowop_beginop(threadflow, flowop);
7695184Sek110237 
7705184Sek110237 #ifdef HAVE_AIOWAITN
7715184Sek110237 		if ((aio_waitn64((struct aiocb64 **)worklist,
7725184Sek110237 		    MAXREAP, &todo, &timeout) == -1) &&
7735184Sek110237 		    errno && (errno != ETIME)) {
7745184Sek110237 			filebench_log(LOG_ERROR,
7755184Sek110237 			    "aiowait failed: %s, outstanding = %d, "
7765184Sek110237 			    "ncompleted = %d ",
7775184Sek110237 			    strerror(errno), uncompleted, todo);
7785184Sek110237 		}
7795184Sek110237 
7805184Sek110237 		ncompleted = todo;
7815184Sek110237 		/* Take the  completed I/Os from the list */
7825184Sek110237 		inprogress = 0;
7835184Sek110237 		for (i = 0; i < ncompleted; i++) {
7845184Sek110237 			if ((aio_return64(worklist[i]) == -1) &&
7855184Sek110237 			    (errno == EINPROGRESS)) {
7865184Sek110237 				inprogress++;
7875184Sek110237 				continue;
7885184Sek110237 			}
7895184Sek110237 			if (aio_deallocate(flowop, worklist[i]) < 0) {
7905184Sek110237 				filebench_log(LOG_ERROR, "Could not remove "
7915184Sek110237 				    "aio from list ");
7925673Saw148015 				flowop_endop(threadflow, flowop, 0);
7936084Saw148015 				return (FILEBENCH_ERROR);
7945184Sek110237 			}
7955184Sek110237 		}
7965184Sek110237 
7975184Sek110237 		uncompleted -= ncompleted;
7985184Sek110237 		uncompleted += inprogress;
7995184Sek110237 
8005184Sek110237 #else
8015184Sek110237 
8025184Sek110237 		for (ncompleted = 0, inprogress = 0,
8035184Sek110237 		    aio = flowop->fo_thread->tf_aiolist;
8045184Sek110237 		    ncompleted < todo, aio != NULL; aio = aio->al_next) {
8056613Sek110237 			int result = aio_error64(&aio->al_aiocb);
8065184Sek110237 
8075184Sek110237 			if (result == EINPROGRESS) {
8085184Sek110237 				inprogress++;
8095184Sek110237 				continue;
8105184Sek110237 			}
8115184Sek110237 
8125184Sek110237 			if ((aio_return64(&aio->al_aiocb) == -1) || result) {
8135184Sek110237 				filebench_log(LOG_ERROR, "aio failed: %s",
8145184Sek110237 				    strerror(result));
8155184Sek110237 				continue;
8165184Sek110237 			}
8175184Sek110237 
8185184Sek110237 			ncompleted++;
8195184Sek110237 
8205184Sek110237 			if (aio_deallocate(flowop, &aio->al_aiocb) < 0) {
8215184Sek110237 				filebench_log(LOG_ERROR, "Could not remove aio "
8225184Sek110237 				    "from list ");
8235673Saw148015 				flowop_endop(threadflow, flowop, 0);
8246084Saw148015 				return (FILEBENCH_ERROR);
8255184Sek110237 			}
8265184Sek110237 		}
8275184Sek110237 
8285184Sek110237 		uncompleted -= ncompleted;
8295184Sek110237 
8305184Sek110237 #endif
8315184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT,
8325184Sek110237 		    "aio2 completed %d ios, uncompleted = %d, inprogress = %d",
8335184Sek110237 		    ncompleted, uncompleted, inprogress);
8345184Sek110237 
8355184Sek110237 	} while (uncompleted > MAXREAP);
8365184Sek110237 
8375673Saw148015 	flowop_endop(threadflow, flowop, 0);
8385184Sek110237 
8395184Sek110237 	free(worklist);
8405184Sek110237 
8416084Saw148015 	return (FILEBENCH_OK);
8425184Sek110237 }
8435184Sek110237 
8445184Sek110237 #endif /* HAVE_AIO */
8455184Sek110237 
8465184Sek110237 /*
8475184Sek110237  * Initializes a "flowop_block" flowop. Specifically, it
8485184Sek110237  * initializes the flowop's fo_cv and unlocks the fo_lock.
8495184Sek110237  */
8505184Sek110237 static int
8515184Sek110237 flowoplib_block_init(flowop_t *flowop)
8525184Sek110237 {
8535184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d block init address %zx",
8545184Sek110237 	    flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
8555184Sek110237 	(void) pthread_cond_init(&flowop->fo_cv, ipc_condattr());
8565184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
8575184Sek110237 
8586084Saw148015 	return (FILEBENCH_OK);
8595184Sek110237 }
8605184Sek110237 
8615184Sek110237 /*
8625184Sek110237  * Blocks the threadflow until woken up by flowoplib_wakeup.
8635184Sek110237  * The routine blocks on the flowop's fo_cv condition variable.
8645184Sek110237  */
8655184Sek110237 static int
8665184Sek110237 flowoplib_block(threadflow_t *threadflow, flowop_t *flowop)
8675184Sek110237 {
8685184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d blocking at address %zx",
8695184Sek110237 	    flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
8705184Sek110237 	(void) ipc_mutex_lock(&flowop->fo_lock);
8715184Sek110237 
8725184Sek110237 	flowop_beginop(threadflow, flowop);
8735184Sek110237 	(void) pthread_cond_wait(&flowop->fo_cv, &flowop->fo_lock);
8745673Saw148015 	flowop_endop(threadflow, flowop, 0);
8755184Sek110237 
8765184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
8775184Sek110237 	    flowop->fo_name, flowop->fo_instance);
8785184Sek110237 
8795184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
8805184Sek110237 
8816084Saw148015 	return (FILEBENCH_OK);
8825184Sek110237 }
8835184Sek110237 
8845184Sek110237 /*
8855184Sek110237  * Wakes up one or more target blocking flowops.
8865184Sek110237  * Sends broadcasts on the fo_cv condition variables of all
8875184Sek110237  * flowops on the target list, except those that are
8885184Sek110237  * FLOW_MASTER flowops. The target list consists of all
8895184Sek110237  * flowops whose name matches this flowop's "fo_targetname"
8905184Sek110237  * attribute. The target list is generated on the first
8915184Sek110237  * invocation, and the run will be shutdown if no targets
8926084Saw148015  * are found. Otherwise the routine always returns FILEBENCH_OK.
8935184Sek110237  */
8945184Sek110237 static int
8955184Sek110237 flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop)
8965184Sek110237 {
8975184Sek110237 	flowop_t *target;
8985184Sek110237 
8995184Sek110237 	/* if this is the first wakeup, create the wakeup list */
9005184Sek110237 	if (flowop->fo_targets == NULL) {
9015184Sek110237 		flowop_t *result = flowop_find(flowop->fo_targetname);
9025184Sek110237 
9035184Sek110237 		flowop->fo_targets = result;
9045184Sek110237 		if (result == NULL) {
9055184Sek110237 			filebench_log(LOG_ERROR,
9065184Sek110237 			    "wakeup: could not find op %s for thread %s",
9075184Sek110237 			    flowop->fo_targetname,
9085184Sek110237 			    threadflow->tf_name);
9095184Sek110237 			filebench_shutdown(1);
9105184Sek110237 		}
9115184Sek110237 		while (result) {
9125184Sek110237 			result->fo_targetnext =
9135184Sek110237 			    result->fo_resultnext;
9145184Sek110237 			result = result->fo_resultnext;
9155184Sek110237 		}
9165184Sek110237 	}
9175184Sek110237 
9185184Sek110237 	target = flowop->fo_targets;
9195184Sek110237 
9205184Sek110237 	/* wakeup the targets */
9215184Sek110237 	while (target) {
9225184Sek110237 		if (target->fo_instance == FLOW_MASTER) {
9235184Sek110237 			target = target->fo_targetnext;
9245184Sek110237 			continue;
9255184Sek110237 		}
9265184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
9275184Sek110237 		    "wakeup flow %s-%d at address %zx",
9285184Sek110237 		    target->fo_name,
9295184Sek110237 		    target->fo_instance,
9305184Sek110237 		    &target->fo_cv);
9315184Sek110237 
9325184Sek110237 		flowop_beginop(threadflow, flowop);
9335184Sek110237 		(void) ipc_mutex_lock(&target->fo_lock);
9345184Sek110237 		(void) pthread_cond_broadcast(&target->fo_cv);
9355184Sek110237 		(void) ipc_mutex_unlock(&target->fo_lock);
9365673Saw148015 		flowop_endop(threadflow, flowop, 0);
9375184Sek110237 
9385184Sek110237 		target = target->fo_targetnext;
9395184Sek110237 	}
9405184Sek110237 
9416084Saw148015 	return (FILEBENCH_OK);
9425184Sek110237 }
9435184Sek110237 
9445184Sek110237 /*
9455184Sek110237  * "think time" routines. the "hog" routine consumes cpu cycles as
9465184Sek110237  * it "thinks", while the "delay" flowop simply calls sleep() to delay
9475184Sek110237  * for a given number of seconds without consuming cpu cycles.
9485184Sek110237  */
9495184Sek110237 
9505184Sek110237 
9515184Sek110237 /*
9525184Sek110237  * Consumes CPU cycles and memory bandwidth by looping for
9535184Sek110237  * flowop->fo_value times. With each loop sets memory location
9545184Sek110237  * threadflow->tf_mem to 1.
9555184Sek110237  */
9565184Sek110237 static int
9575184Sek110237 flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop)
9585184Sek110237 {
9596212Saw148015 	uint64_t value = avd_get_int(flowop->fo_value);
9605184Sek110237 	int i;
9615184Sek110237 
9625673Saw148015 	filebench_log(LOG_DEBUG_IMPL, "hog enter");
9635184Sek110237 	flowop_beginop(threadflow, flowop);
9645673Saw148015 	if (threadflow->tf_mem != NULL) {
9655673Saw148015 		for (i = 0; i < value; i++)
9665673Saw148015 			*(threadflow->tf_mem) = 1;
9675673Saw148015 	}
9685673Saw148015 	flowop_endop(threadflow, flowop, 0);
9695184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "hog exit");
9706084Saw148015 	return (FILEBENCH_OK);
9715184Sek110237 }
9725184Sek110237 
9735184Sek110237 
9745184Sek110237 /*
9755184Sek110237  * Delays for fo_value seconds.
9765184Sek110237  */
9775184Sek110237 static int
9785184Sek110237 flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop)
9795184Sek110237 {
9806212Saw148015 	int value = avd_get_int(flowop->fo_value);
9815184Sek110237 
9825184Sek110237 	flowop_beginop(threadflow, flowop);
9835184Sek110237 	(void) sleep(value);
9845673Saw148015 	flowop_endop(threadflow, flowop, 0);
9856084Saw148015 	return (FILEBENCH_OK);
9865184Sek110237 }
9875184Sek110237 
9885184Sek110237 /*
9895184Sek110237  * Rate limiting routines. This is the event consuming half of the
9905184Sek110237  * event system. Each of the four following routines will limit the rate
9915184Sek110237  * to one unit of either calls, issued I/O operations, issued filebench
9925184Sek110237  * operations, or I/O bandwidth. Since there is only one event generator,
9935184Sek110237  * the events will be divided amoung multiple instances of an event
9945184Sek110237  * consumer, and further divided among different consumers if more than
9955184Sek110237  * one has been defined. There is no mechanism to enforce equal sharing
9965184Sek110237  * of events.
9975184Sek110237  */
9985184Sek110237 
9995184Sek110237 /*
10005184Sek110237  * Completes one invocation per posted event. If eventgen_q
10015184Sek110237  * has an event count greater than zero, one will be removed
10025184Sek110237  * (count decremented), otherwise the calling thread will
10035184Sek110237  * block until another event has been posted. Always returns 0
10045184Sek110237  */
10055184Sek110237 static int
10065184Sek110237 flowoplib_eventlimit(threadflow_t *threadflow, flowop_t *flowop)
10075184Sek110237 {
10085184Sek110237 	/* Immediately bail if not set/enabled */
1009*7946SAndrew.W.Wilson@sun.com 	if (filebench_shm->shm_eventgen_hz == NULL)
10106084Saw148015 		return (FILEBENCH_OK);
10115184Sek110237 
10125184Sek110237 	if (flowop->fo_initted == 0) {
10135184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
10145184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
10155184Sek110237 		flowop->fo_initted = 1;
10165184Sek110237 	}
10175184Sek110237 
10185184Sek110237 	flowop_beginop(threadflow, flowop);
1019*7946SAndrew.W.Wilson@sun.com 	while (filebench_shm->shm_eventgen_hz != NULL) {
10206391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
10216391Saw148015 		if (filebench_shm->shm_eventgen_q > 0) {
10226391Saw148015 			filebench_shm->shm_eventgen_q--;
10236391Saw148015 			(void) ipc_mutex_unlock(
10246391Saw148015 			    &filebench_shm->shm_eventgen_lock);
10255184Sek110237 			break;
10265184Sek110237 		}
10276391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
10286391Saw148015 		    &filebench_shm->shm_eventgen_lock);
10296391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
10305184Sek110237 	}
10315673Saw148015 	flowop_endop(threadflow, flowop, 0);
10326084Saw148015 	return (FILEBENCH_OK);
10335184Sek110237 }
10345184Sek110237 
10356701Saw148015 static int
10366701Saw148015 flowoplib_event_find_target(threadflow_t *threadflow, flowop_t *flowop)
10376701Saw148015 {
10386701Saw148015 	if (flowop->fo_targetname[0] != '\0') {
10396701Saw148015 
10406701Saw148015 		/* Try to use statistics from specific flowop */
10416701Saw148015 		flowop->fo_targets =
10426701Saw148015 		    flowop_find_from_list(flowop->fo_targetname,
10436701Saw148015 		    threadflow->tf_thrd_fops);
10446701Saw148015 		if (flowop->fo_targets == NULL) {
10456701Saw148015 			filebench_log(LOG_ERROR,
10466701Saw148015 			    "limit target: could not find flowop %s",
10476701Saw148015 			    flowop->fo_targetname);
10486701Saw148015 			filebench_shutdown(1);
10496701Saw148015 			return (FILEBENCH_ERROR);
10506701Saw148015 		}
10516701Saw148015 	} else {
10526701Saw148015 		/* use total workload statistics */
10536701Saw148015 		flowop->fo_targets = NULL;
10546701Saw148015 	}
10556701Saw148015 	return (FILEBENCH_OK);
10566701Saw148015 }
10576701Saw148015 
10585184Sek110237 /*
10595184Sek110237  * Blocks the calling thread if the number of issued I/O
10605184Sek110237  * operations exceeds the number of posted events, thus
10615184Sek110237  * limiting the average I/O operation rate to the rate
10626084Saw148015  * specified by eventgen_hz. Always returns FILEBENCH_OK.
10635184Sek110237  */
10645184Sek110237 static int
10655184Sek110237 flowoplib_iopslimit(threadflow_t *threadflow, flowop_t *flowop)
10665184Sek110237 {
10675184Sek110237 	uint64_t iops;
10685184Sek110237 	uint64_t delta;
10695673Saw148015 	uint64_t events;
10705184Sek110237 
10715184Sek110237 	/* Immediately bail if not set/enabled */
1072*7946SAndrew.W.Wilson@sun.com 	if (filebench_shm->shm_eventgen_hz == NULL)
10736084Saw148015 		return (FILEBENCH_OK);
10745184Sek110237 
10755184Sek110237 	if (flowop->fo_initted == 0) {
10765184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
10775184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
10785184Sek110237 		flowop->fo_initted = 1;
10796701Saw148015 
10806701Saw148015 		if (flowoplib_event_find_target(threadflow, flowop)
10816701Saw148015 		    == FILEBENCH_ERROR)
10826701Saw148015 			return (FILEBENCH_ERROR);
10836701Saw148015 
10846701Saw148015 		if (flowop->fo_targets && ((flowop->fo_targets->fo_attrs &
10856701Saw148015 		    (FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) {
10866701Saw148015 			filebench_log(LOG_ERROR,
10876701Saw148015 			    "WARNING: Flowop %s does no IO",
10886701Saw148015 			    flowop->fo_targets->fo_name);
10896701Saw148015 			filebench_shutdown(1);
10906701Saw148015 			return (FILEBENCH_ERROR);
10916701Saw148015 		}
10925184Sek110237 	}
10935184Sek110237 
10946701Saw148015 	if (flowop->fo_targets) {
10956701Saw148015 		/*
10966701Saw148015 		 * Note that fs_count is already the sum of fs_rcount
10976701Saw148015 		 * and fs_wcount if looking at a single flowop.
10986701Saw148015 		 */
10996701Saw148015 		iops = flowop->fo_targets->fo_stats.fs_count;
11006701Saw148015 	} else {
11016701Saw148015 		(void) ipc_mutex_lock(&controlstats_lock);
11026701Saw148015 		iops = (controlstats.fs_rcount +
11036701Saw148015 		    controlstats.fs_wcount);
11046701Saw148015 		(void) ipc_mutex_unlock(&controlstats_lock);
11056701Saw148015 	}
11065184Sek110237 
11075184Sek110237 	/* Is this the first time around */
11085184Sek110237 	if (flowop->fo_tputlast == 0) {
11095184Sek110237 		flowop->fo_tputlast = iops;
11106084Saw148015 		return (FILEBENCH_OK);
11115184Sek110237 	}
11125184Sek110237 
11135184Sek110237 	delta = iops - flowop->fo_tputlast;
11145184Sek110237 	flowop->fo_tputbucket -= delta;
11155184Sek110237 	flowop->fo_tputlast = iops;
11165184Sek110237 
11175184Sek110237 	/* No need to block if the q isn't empty */
11185184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
11195673Saw148015 		flowop_endop(threadflow, flowop, 0);
11206084Saw148015 		return (FILEBENCH_OK);
11215184Sek110237 	}
11225184Sek110237 
11235184Sek110237 	iops = flowop->fo_tputbucket * -1;
11245184Sek110237 	events = iops;
11255184Sek110237 
11265184Sek110237 	flowop_beginop(threadflow, flowop);
1127*7946SAndrew.W.Wilson@sun.com 	while (filebench_shm->shm_eventgen_hz != NULL) {
11285184Sek110237 
11296391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
11306391Saw148015 		if (filebench_shm->shm_eventgen_q >= events) {
11316391Saw148015 			filebench_shm->shm_eventgen_q -= events;
11326391Saw148015 			(void) ipc_mutex_unlock(
11336391Saw148015 			    &filebench_shm->shm_eventgen_lock);
11345184Sek110237 			flowop->fo_tputbucket += events;
11355184Sek110237 			break;
11365184Sek110237 		}
11376391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
11386391Saw148015 		    &filebench_shm->shm_eventgen_lock);
11396391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
11405184Sek110237 	}
11415673Saw148015 	flowop_endop(threadflow, flowop, 0);
11425184Sek110237 
11436084Saw148015 	return (FILEBENCH_OK);
11445184Sek110237 }
11455184Sek110237 
11465184Sek110237 /*
11475184Sek110237  * Blocks the calling thread if the number of issued filebench
11485184Sek110237  * operations exceeds the number of posted events, thus limiting
11495184Sek110237  * the average filebench operation rate to the rate specified by
11506084Saw148015  * eventgen_hz. Always returns FILEBENCH_OK.
11515184Sek110237  */
11525184Sek110237 static int
11535184Sek110237 flowoplib_opslimit(threadflow_t *threadflow, flowop_t *flowop)
11545184Sek110237 {
11555184Sek110237 	uint64_t ops;
11565184Sek110237 	uint64_t delta;
11575673Saw148015 	uint64_t events;
11585184Sek110237 
11595184Sek110237 	/* Immediately bail if not set/enabled */
1160*7946SAndrew.W.Wilson@sun.com 	if (filebench_shm->shm_eventgen_hz == NULL)
11616084Saw148015 		return (FILEBENCH_OK);
11625184Sek110237 
11635184Sek110237 	if (flowop->fo_initted == 0) {
11645184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
11655184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
11665184Sek110237 		flowop->fo_initted = 1;
11676701Saw148015 
11686701Saw148015 		if (flowoplib_event_find_target(threadflow, flowop)
11696701Saw148015 		    == FILEBENCH_ERROR)
11706701Saw148015 			return (FILEBENCH_ERROR);
11715184Sek110237 	}
11725184Sek110237 
11736701Saw148015 	if (flowop->fo_targets) {
11746701Saw148015 		ops = flowop->fo_targets->fo_stats.fs_count;
11756701Saw148015 	} else {
11766701Saw148015 		(void) ipc_mutex_lock(&controlstats_lock);
11776701Saw148015 		ops = controlstats.fs_count;
11786701Saw148015 		(void) ipc_mutex_unlock(&controlstats_lock);
11796701Saw148015 	}
11805184Sek110237 
11815184Sek110237 	/* Is this the first time around */
11825184Sek110237 	if (flowop->fo_tputlast == 0) {
11835184Sek110237 		flowop->fo_tputlast = ops;
11846084Saw148015 		return (FILEBENCH_OK);
11855184Sek110237 	}
11865184Sek110237 
11875184Sek110237 	delta = ops - flowop->fo_tputlast;
11885184Sek110237 	flowop->fo_tputbucket -= delta;
11895184Sek110237 	flowop->fo_tputlast = ops;
11905184Sek110237 
11915184Sek110237 	/* No need to block if the q isn't empty */
11925184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
11935673Saw148015 		flowop_endop(threadflow, flowop, 0);
11946084Saw148015 		return (FILEBENCH_OK);
11955184Sek110237 	}
11965184Sek110237 
11975184Sek110237 	ops = flowop->fo_tputbucket * -1;
11985184Sek110237 	events = ops;
11995184Sek110237 
12005184Sek110237 	flowop_beginop(threadflow, flowop);
1201*7946SAndrew.W.Wilson@sun.com 	while (filebench_shm->shm_eventgen_hz != NULL) {
12026391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
12036391Saw148015 		if (filebench_shm->shm_eventgen_q >= events) {
12046391Saw148015 			filebench_shm->shm_eventgen_q -= events;
12056391Saw148015 			(void) ipc_mutex_unlock(
12066391Saw148015 			    &filebench_shm->shm_eventgen_lock);
12075184Sek110237 			flowop->fo_tputbucket += events;
12085184Sek110237 			break;
12095184Sek110237 		}
12106391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
12116391Saw148015 		    &filebench_shm->shm_eventgen_lock);
12126391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
12135184Sek110237 	}
12145673Saw148015 	flowop_endop(threadflow, flowop, 0);
12155184Sek110237 
12166084Saw148015 	return (FILEBENCH_OK);
12175184Sek110237 }
12185184Sek110237 
12195184Sek110237 
12205184Sek110237 /*
12215184Sek110237  * Blocks the calling thread if the number of bytes of I/O
12225184Sek110237  * issued exceeds one megabyte times the number of posted
12235184Sek110237  * events, thus limiting the average I/O byte rate to one
12245184Sek110237  * megabyte times the event rate as set by eventgen_hz.
12256084Saw148015  * Always retuns FILEBENCH_OK.
12265184Sek110237  */
12275184Sek110237 static int
12285184Sek110237 flowoplib_bwlimit(threadflow_t *threadflow, flowop_t *flowop)
12295184Sek110237 {
12305184Sek110237 	uint64_t bytes;
12315184Sek110237 	uint64_t delta;
12325673Saw148015 	uint64_t events;
12335184Sek110237 
12345184Sek110237 	/* Immediately bail if not set/enabled */
1235*7946SAndrew.W.Wilson@sun.com 	if (filebench_shm->shm_eventgen_hz == NULL)
12366084Saw148015 		return (FILEBENCH_OK);
12375184Sek110237 
12385184Sek110237 	if (flowop->fo_initted == 0) {
12395184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
12405184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
12415184Sek110237 		flowop->fo_initted = 1;
12426701Saw148015 
12436701Saw148015 		if (flowoplib_event_find_target(threadflow, flowop)
12446701Saw148015 		    == FILEBENCH_ERROR)
12456701Saw148015 			return (FILEBENCH_ERROR);
12466701Saw148015 
12476701Saw148015 		if ((flowop->fo_targets) &&
12486701Saw148015 		    ((flowop->fo_targets->fo_attrs &
12496701Saw148015 		    (FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) {
12506701Saw148015 			filebench_log(LOG_ERROR,
12516701Saw148015 			    "WARNING: Flowop %s does no Reads or Writes",
12526701Saw148015 			    flowop->fo_targets->fo_name);
12536701Saw148015 			filebench_shutdown(1);
12546701Saw148015 			return (FILEBENCH_ERROR);
12556701Saw148015 		}
12565184Sek110237 	}
12575184Sek110237 
12586701Saw148015 	if (flowop->fo_targets) {
12596701Saw148015 		/*
12606701Saw148015 		 * Note that fs_bytes is already the sum of fs_rbytes
12616701Saw148015 		 * and fs_wbytes if looking at a single flowop.
12626701Saw148015 		 */
12636701Saw148015 		bytes = flowop->fo_targets->fo_stats.fs_bytes;
12646701Saw148015 	} else {
12656701Saw148015 		(void) ipc_mutex_lock(&controlstats_lock);
12666701Saw148015 		bytes = (controlstats.fs_rbytes +
12676701Saw148015 		    controlstats.fs_wbytes);
12686701Saw148015 		(void) ipc_mutex_unlock(&controlstats_lock);
12696701Saw148015 	}
12706701Saw148015 
12716701Saw148015 	/* Is this the first time around? */
12725184Sek110237 	if (flowop->fo_tputlast == 0) {
12735184Sek110237 		flowop->fo_tputlast = bytes;
12746084Saw148015 		return (FILEBENCH_OK);
12755184Sek110237 	}
12765184Sek110237 
12775184Sek110237 	delta = bytes - flowop->fo_tputlast;
12785184Sek110237 	flowop->fo_tputbucket -= delta;
12795184Sek110237 	flowop->fo_tputlast = bytes;
12805184Sek110237 
12815184Sek110237 	/* No need to block if the q isn't empty */
12825184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
12835673Saw148015 		flowop_endop(threadflow, flowop, 0);
12846084Saw148015 		return (FILEBENCH_OK);
12855184Sek110237 	}
12865184Sek110237 
12875184Sek110237 	bytes = flowop->fo_tputbucket * -1;
12885184Sek110237 	events = (bytes / MB) + 1;
12895184Sek110237 
12906286Saw148015 	filebench_log(LOG_DEBUG_IMPL, "%llu bytes, %llu events",
12916286Saw148015 	    (u_longlong_t)bytes, (u_longlong_t)events);
12925184Sek110237 
12935184Sek110237 	flowop_beginop(threadflow, flowop);
1294*7946SAndrew.W.Wilson@sun.com 	while (filebench_shm->shm_eventgen_hz != NULL) {
12956391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
12966391Saw148015 		if (filebench_shm->shm_eventgen_q >= events) {
12976391Saw148015 			filebench_shm->shm_eventgen_q -= events;
12986391Saw148015 			(void) ipc_mutex_unlock(
12996391Saw148015 			    &filebench_shm->shm_eventgen_lock);
13005184Sek110237 			flowop->fo_tputbucket += (events * MB);
13015184Sek110237 			break;
13025184Sek110237 		}
13036391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
13046391Saw148015 		    &filebench_shm->shm_eventgen_lock);
13056391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
13065184Sek110237 	}
13075673Saw148015 	flowop_endop(threadflow, flowop, 0);
13085184Sek110237 
13096084Saw148015 	return (FILEBENCH_OK);
13105184Sek110237 }
13115184Sek110237 
13125184Sek110237 /*
13135184Sek110237  * These flowops terminate a benchmark run when either the specified
13145184Sek110237  * number of bytes of I/O (flowoplib_finishonbytes) or the specified
13155184Sek110237  * number of I/O operations (flowoplib_finishoncount) have been generated.
13165184Sek110237  */
13175184Sek110237 
13185184Sek110237 
13195184Sek110237 /*
13205184Sek110237  * Stop filebench run when specified number of I/O bytes have been
13216212Saw148015  * transferred. Compares controlstats.fs_bytes with flowop->value,
13225184Sek110237  * and if greater returns 1, stopping the run, if not, returns 0
13235184Sek110237  * to continue running.
13245184Sek110237  */
13255184Sek110237 static int
13265184Sek110237 flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop)
13275184Sek110237 {
13286701Saw148015 	uint64_t bytes_io;		/* Bytes of I/O delivered so far */
13296701Saw148015 	uint64_t byte_lim = flowop->fo_constvalue;  /* Total Bytes desired */
13306701Saw148015 						    /* Uses constant value */
13316701Saw148015 
13326701Saw148015 	if (flowop->fo_initted == 0) {
13336701Saw148015 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
13346701Saw148015 		    flowop, threadflow->tf_name, threadflow->tf_instance);
13356701Saw148015 		flowop->fo_initted = 1;
13366701Saw148015 
13376701Saw148015 		if (flowoplib_event_find_target(threadflow, flowop)
13386701Saw148015 		    == FILEBENCH_ERROR)
13396701Saw148015 			return (FILEBENCH_ERROR);
13406701Saw148015 
13416701Saw148015 		if ((flowop->fo_targets) &&
13426701Saw148015 		    ((flowop->fo_targets->fo_attrs &
13436701Saw148015 		    (FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) {
13446701Saw148015 			filebench_log(LOG_ERROR,
13456701Saw148015 			    "WARNING: Flowop %s does no Reads or Writes",
13466701Saw148015 			    flowop->fo_targets->fo_name);
13476701Saw148015 			filebench_shutdown(1);
13486701Saw148015 			return (FILEBENCH_ERROR);
13496701Saw148015 		}
13506701Saw148015 	}
13516701Saw148015 
13526701Saw148015 	if (flowop->fo_targets) {
13536701Saw148015 		bytes_io = flowop->fo_targets->fo_stats.fs_bytes;
13546701Saw148015 	} else {
13556701Saw148015 		(void) ipc_mutex_lock(&controlstats_lock);
13566701Saw148015 		bytes_io = controlstats.fs_bytes;
13576701Saw148015 		(void) ipc_mutex_unlock(&controlstats_lock);
13586701Saw148015 	}
13595184Sek110237 
13605184Sek110237 	flowop_beginop(threadflow, flowop);
13616701Saw148015 	if (bytes_io > byte_lim) {
13625673Saw148015 		flowop_endop(threadflow, flowop, 0);
13636084Saw148015 		return (FILEBENCH_DONE);
13645184Sek110237 	}
13655673Saw148015 	flowop_endop(threadflow, flowop, 0);
13665184Sek110237 
13676084Saw148015 	return (FILEBENCH_OK);
13685184Sek110237 }
13695184Sek110237 
13705184Sek110237 /*
13715184Sek110237  * Stop filebench run when specified number of I/O operations have
13725184Sek110237  * been performed. Compares controlstats.fs_count with *flowop->value,
13736084Saw148015  * and if greater returns 1, stopping the run, if not, returns FILEBENCH_OK
13746084Saw148015  * to continue running.
13755184Sek110237  */
13765184Sek110237 static int
13775184Sek110237 flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop)
13785184Sek110237 {
13795184Sek110237 	uint64_t ops;
13806212Saw148015 	uint64_t count = flowop->fo_constvalue; /* use constant value */
13815184Sek110237 
13826701Saw148015 	if (flowop->fo_initted == 0) {
13836701Saw148015 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
13846701Saw148015 		    flowop, threadflow->tf_name, threadflow->tf_instance);
13856701Saw148015 		flowop->fo_initted = 1;
13866701Saw148015 
13876701Saw148015 		if (flowoplib_event_find_target(threadflow, flowop)
13886701Saw148015 		    == FILEBENCH_ERROR)
13896701Saw148015 			return (FILEBENCH_ERROR);
13906701Saw148015 	}
13916701Saw148015 
13926701Saw148015 	if (flowop->fo_targets) {
13936701Saw148015 		ops = flowop->fo_targets->fo_stats.fs_count;
13946701Saw148015 	} else {
13956701Saw148015 		(void) ipc_mutex_lock(&controlstats_lock);
13966701Saw148015 		ops = controlstats.fs_count;
13976701Saw148015 		(void) ipc_mutex_unlock(&controlstats_lock);
13986701Saw148015 	}
13995184Sek110237 
14005184Sek110237 	flowop_beginop(threadflow, flowop);
14016084Saw148015 	if (ops >= count) {
14025673Saw148015 		flowop_endop(threadflow, flowop, 0);
14036084Saw148015 		return (FILEBENCH_DONE);
14045184Sek110237 	}
14055673Saw148015 	flowop_endop(threadflow, flowop, 0);
14065184Sek110237 
14076084Saw148015 	return (FILEBENCH_OK);
14085184Sek110237 }
14095184Sek110237 
14105184Sek110237 /*
14115184Sek110237  * Semaphore synchronization using either System V semaphores or
14125184Sek110237  * posix semaphores. If System V semaphores are available, they will be
14135184Sek110237  * used, otherwise posix semaphores will be used.
14145184Sek110237  */
14155184Sek110237 
14165184Sek110237 
14175184Sek110237 /*
14185184Sek110237  * Initializes the filebench "block on semaphore" flowop.
14195184Sek110237  * If System V semaphores are implemented, the routine
14205184Sek110237  * initializes the System V semaphore subsystem if it hasn't
14215184Sek110237  * already been initialized, also allocates a pair of semids
14225184Sek110237  * and initializes the highwater System V semaphore.
14235184Sek110237  * If no System V semaphores, then does nothing special.
14246084Saw148015  * Returns FILEBENCH_ERROR if it cannot acquire a set of System V semphores
14256084Saw148015  * or if the initial post to the semaphore set fails. Returns FILEBENCH_OK
14265184Sek110237  * on success.
14275184Sek110237  */
14285184Sek110237 static int
14295184Sek110237 flowoplib_semblock_init(flowop_t *flowop)
14305184Sek110237 {
14315184Sek110237 
14325184Sek110237 #ifdef HAVE_SYSV_SEM
14336391Saw148015 	int sys_semid;
14345184Sek110237 	struct sembuf sbuf[2];
14355184Sek110237 	int highwater;
14365184Sek110237 
14375184Sek110237 	ipc_seminit();
14385184Sek110237 
14395184Sek110237 	flowop->fo_semid_lw = ipc_semidalloc();
14405184Sek110237 	flowop->fo_semid_hw = ipc_semidalloc();
14415184Sek110237 
14425184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d semblock init semid=%x",
14435184Sek110237 	    flowop->fo_name, flowop->fo_instance, flowop->fo_semid_lw);
14445184Sek110237 
14456391Saw148015 	sys_semid = filebench_shm->shm_sys_semid;
14465184Sek110237 
14475184Sek110237 	if ((highwater = flowop->fo_semid_hw) == 0)
14486212Saw148015 		highwater = flowop->fo_constvalue; /* use constant value */
14495184Sek110237 
14505184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "setting highwater to : %d", highwater);
14515184Sek110237 
14525673Saw148015 	sbuf[0].sem_num = (short)highwater;
14536212Saw148015 	sbuf[0].sem_op = avd_get_int(flowop->fo_highwater);
14545184Sek110237 	sbuf[0].sem_flg = 0;
14556391Saw148015 	if ((semop(sys_semid, &sbuf[0], 1) == -1) && errno) {
14565184Sek110237 		filebench_log(LOG_ERROR, "semblock init post failed: %s (%d,"
14575184Sek110237 		    "%d)", strerror(errno), sbuf[0].sem_num, sbuf[0].sem_op);
14586084Saw148015 		return (FILEBENCH_ERROR);
14595184Sek110237 	}
14605184Sek110237 #else
14615184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
14625184Sek110237 	    "flow %s-%d semblock init with posix semaphore",
14635184Sek110237 	    flowop->fo_name, flowop->fo_instance);
14645184Sek110237 
14655184Sek110237 	sem_init(&flowop->fo_sem, 1, 0);
14665184Sek110237 #endif	/* HAVE_SYSV_SEM */
14675184Sek110237 
14686212Saw148015 	if (!(avd_get_bool(flowop->fo_blocking)))
14695184Sek110237 		(void) ipc_mutex_unlock(&flowop->fo_lock);
14705184Sek110237 
14716084Saw148015 	return (FILEBENCH_OK);
14725184Sek110237 }
14735184Sek110237 
14745184Sek110237 /*
14755184Sek110237  * Releases the semids for the System V semaphore allocated
14765184Sek110237  * to this flowop. If not using System V semaphores, then
14776084Saw148015  * it is effectively just a no-op.
14785184Sek110237  */
14795184Sek110237 static void
14805184Sek110237 flowoplib_semblock_destruct(flowop_t *flowop)
14815184Sek110237 {
14825184Sek110237 #ifdef HAVE_SYSV_SEM
14835184Sek110237 	ipc_semidfree(flowop->fo_semid_lw);
14845184Sek110237 	ipc_semidfree(flowop->fo_semid_hw);
14855184Sek110237 #else
14865184Sek110237 	sem_destroy(&flowop->fo_sem);
14875184Sek110237 #endif /* HAVE_SYSV_SEM */
14885184Sek110237 }
14895184Sek110237 
14905184Sek110237 /*
14915184Sek110237  * Attempts to pass a System V or posix semaphore as appropriate,
14926084Saw148015  * and blocks if necessary. Returns FILEBENCH_ERROR if a set of System V
14935184Sek110237  * semphores is not available or cannot be acquired, or if the initial
14946084Saw148015  * post to the semaphore set fails. Returns FILEBENCH_OK on success.
14955184Sek110237  */
14965184Sek110237 static int
14975184Sek110237 flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop)
14985184Sek110237 {
14995184Sek110237 
15005184Sek110237 #ifdef HAVE_SYSV_SEM
15015184Sek110237 	struct sembuf sbuf[2];
15026212Saw148015 	int value = avd_get_int(flowop->fo_value);
15036391Saw148015 	int sys_semid;
15045184Sek110237 	struct timespec timeout;
15055184Sek110237 
15066391Saw148015 	sys_semid = filebench_shm->shm_sys_semid;
15075184Sek110237 
15085184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
15095184Sek110237 	    "flow %s-%d sem blocking on id %x num %x value %d",
15106391Saw148015 	    flowop->fo_name, flowop->fo_instance, sys_semid,
15115184Sek110237 	    flowop->fo_semid_hw, value);
15125184Sek110237 
15135184Sek110237 	/* Post, decrement the increment the hw queue */
15145184Sek110237 	sbuf[0].sem_num = flowop->fo_semid_hw;
15155673Saw148015 	sbuf[0].sem_op = (short)value;
15165184Sek110237 	sbuf[0].sem_flg = 0;
15175184Sek110237 	sbuf[1].sem_num = flowop->fo_semid_lw;
15185184Sek110237 	sbuf[1].sem_op = value * -1;
15195184Sek110237 	sbuf[1].sem_flg = 0;
15205184Sek110237 	timeout.tv_sec = 600;
15215184Sek110237 	timeout.tv_nsec = 0;
15225184Sek110237 
15236212Saw148015 	if (avd_get_bool(flowop->fo_blocking))
15245184Sek110237 		(void) ipc_mutex_unlock(&flowop->fo_lock);
15255184Sek110237 
15265184Sek110237 	flowop_beginop(threadflow, flowop);
15275184Sek110237 
15285184Sek110237 #ifdef HAVE_SEMTIMEDOP
15296391Saw148015 	(void) semtimedop(sys_semid, &sbuf[0], 1, &timeout);
15306391Saw148015 	(void) semtimedop(sys_semid, &sbuf[1], 1, &timeout);
15315184Sek110237 #else
15326391Saw148015 	(void) semop(sys_semid, &sbuf[0], 1);
15336391Saw148015 	(void) semop(sys_semid, &sbuf[1], 1);
15345184Sek110237 #endif /* HAVE_SEMTIMEDOP */
15355184Sek110237 
15366212Saw148015 	if (avd_get_bool(flowop->fo_blocking))
15375184Sek110237 		(void) ipc_mutex_lock(&flowop->fo_lock);
15385184Sek110237 
15395673Saw148015 	flowop_endop(threadflow, flowop, 0);
15405184Sek110237 
15415184Sek110237 #else
15426212Saw148015 	int value = avd_get_int(flowop->fo_value);
15435184Sek110237 	int i;
15445184Sek110237 
15455184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
15465184Sek110237 	    "flow %s-%d sem blocking on posix semaphore",
15475184Sek110237 	    flowop->fo_name, flowop->fo_instance);
15485184Sek110237 
15495184Sek110237 	/* Decrement sem by value */
15505184Sek110237 	for (i = 0; i < value; i++) {
15515184Sek110237 		if (sem_wait(&flowop->fo_sem) == -1) {
15525184Sek110237 			filebench_log(LOG_ERROR, "semop wait failed");
15536084Saw148015 			return (FILEBENCH_ERROR);
15545184Sek110237 		}
15555184Sek110237 	}
15565184Sek110237 
15575184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d sem unblocking",
15585184Sek110237 	    flowop->fo_name, flowop->fo_instance);
15595184Sek110237 #endif /* HAVE_SYSV_SEM */
15605184Sek110237 
15616084Saw148015 	return (FILEBENCH_OK);
15625184Sek110237 }
15635184Sek110237 
15645184Sek110237 /*
15656084Saw148015  * Calls ipc_seminit(). Always returns FILEBENCH_OK.
15665184Sek110237  */
15675184Sek110237 /* ARGSUSED */
15685184Sek110237 static int
15695184Sek110237 flowoplib_sempost_init(flowop_t *flowop)
15705184Sek110237 {
15715184Sek110237 #ifdef HAVE_SYSV_SEM
15725184Sek110237 	ipc_seminit();
15735184Sek110237 #endif /* HAVE_SYSV_SEM */
15746084Saw148015 	return (FILEBENCH_OK);
15755184Sek110237 }
15765184Sek110237 
15775184Sek110237 /*
15785184Sek110237  * Post to a System V or posix semaphore as appropriate.
15795184Sek110237  * On the first call for a given flowop instance, this routine
15805184Sek110237  * will use the fo_targetname attribute to locate all semblock
15815184Sek110237  * flowops that are expecting posts from this flowop. All
15825184Sek110237  * target flowops on this list will have a post operation done
15835184Sek110237  * to their semaphores on each call.
15845184Sek110237  */
15855184Sek110237 static int
15865184Sek110237 flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop)
15875184Sek110237 {
15885184Sek110237 	flowop_t *target;
15895184Sek110237 
15905184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
15915184Sek110237 	    "sempost flow %s-%d",
15925184Sek110237 	    flowop->fo_name,
15935184Sek110237 	    flowop->fo_instance);
15945184Sek110237 
15955184Sek110237 	/* if this is the first post, create the post list */
15965184Sek110237 	if (flowop->fo_targets == NULL) {
15975184Sek110237 		flowop_t *result = flowop_find(flowop->fo_targetname);
15985184Sek110237 
15995184Sek110237 		flowop->fo_targets = result;
16005184Sek110237 
16015184Sek110237 		if (result == NULL) {
16025184Sek110237 			filebench_log(LOG_ERROR,
16035184Sek110237 			    "sempost: could not find op %s for thread %s",
16045184Sek110237 			    flowop->fo_targetname,
16055184Sek110237 			    threadflow->tf_name);
16065184Sek110237 			filebench_shutdown(1);
16075184Sek110237 		}
16085184Sek110237 
16095184Sek110237 		while (result) {
16105184Sek110237 			result->fo_targetnext =
16115184Sek110237 			    result->fo_resultnext;
16125184Sek110237 			result = result->fo_resultnext;
16135184Sek110237 		}
16145184Sek110237 	}
16155184Sek110237 
16165184Sek110237 	target = flowop->fo_targets;
16175184Sek110237 
16185184Sek110237 	flowop_beginop(threadflow, flowop);
16195184Sek110237 	/* post to the targets */
16205184Sek110237 	while (target) {
16215184Sek110237 #ifdef HAVE_SYSV_SEM
16225184Sek110237 		struct sembuf sbuf[2];
16236391Saw148015 		int sys_semid;
16245184Sek110237 		int blocking;
16255184Sek110237 #else
16265184Sek110237 		int i;
16275184Sek110237 #endif /* HAVE_SYSV_SEM */
16285184Sek110237 		struct timespec timeout;
16296550Saw148015 		int value = (int)avd_get_int(flowop->fo_value);
16305184Sek110237 
16315184Sek110237 		if (target->fo_instance == FLOW_MASTER) {
16325184Sek110237 			target = target->fo_targetnext;
16335184Sek110237 			continue;
16345184Sek110237 		}
16355184Sek110237 
16365184Sek110237 #ifdef HAVE_SYSV_SEM
16375184Sek110237 
16385184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
16395184Sek110237 		    "sempost flow %s-%d num %x",
16405184Sek110237 		    target->fo_name,
16415184Sek110237 		    target->fo_instance,
16425184Sek110237 		    target->fo_semid_lw);
16435184Sek110237 
16446391Saw148015 		sys_semid = filebench_shm->shm_sys_semid;
16455184Sek110237 		sbuf[0].sem_num = target->fo_semid_lw;
16465673Saw148015 		sbuf[0].sem_op = (short)value;
16475184Sek110237 		sbuf[0].sem_flg = 0;
16485184Sek110237 		sbuf[1].sem_num = target->fo_semid_hw;
16495184Sek110237 		sbuf[1].sem_op = value * -1;
16505184Sek110237 		sbuf[1].sem_flg = 0;
16515184Sek110237 		timeout.tv_sec = 600;
16525184Sek110237 		timeout.tv_nsec = 0;
16535184Sek110237 
16546212Saw148015 		if (avd_get_bool(flowop->fo_blocking))
16555184Sek110237 			blocking = 1;
16565184Sek110237 		else
16575184Sek110237 			blocking = 0;
16585184Sek110237 
16595184Sek110237 #ifdef HAVE_SEMTIMEDOP
16606391Saw148015 		if ((semtimedop(sys_semid, &sbuf[0], blocking + 1,
16615184Sek110237 		    &timeout) == -1) && (errno && (errno != EAGAIN))) {
16625184Sek110237 #else
16636391Saw148015 		if ((semop(sys_semid, &sbuf[0], blocking + 1) == -1) &&
16645184Sek110237 		    (errno && (errno != EAGAIN))) {
16655184Sek110237 #endif /* HAVE_SEMTIMEDOP */
16665184Sek110237 			filebench_log(LOG_ERROR, "semop post failed: %s",
16675184Sek110237 			    strerror(errno));
16686084Saw148015 			return (FILEBENCH_ERROR);
16695184Sek110237 		}
16705184Sek110237 
16715184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
16725184Sek110237 		    "flow %s-%d finished posting",
16735184Sek110237 		    target->fo_name, target->fo_instance);
16745184Sek110237 #else
16755184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
16765184Sek110237 		    "sempost flow %s-%d to posix semaphore",
16775184Sek110237 		    target->fo_name,
16785184Sek110237 		    target->fo_instance);
16795184Sek110237 
16805184Sek110237 		/* Increment sem by value */
16815184Sek110237 		for (i = 0; i < value; i++) {
16825184Sek110237 			if (sem_post(&target->fo_sem) == -1) {
16835184Sek110237 				filebench_log(LOG_ERROR, "semop post failed");
16846084Saw148015 				return (FILEBENCH_ERROR);
16855184Sek110237 			}
16865184Sek110237 		}
16875184Sek110237 
16885184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
16895184Sek110237 		    target->fo_name, target->fo_instance);
16905184Sek110237 #endif /* HAVE_SYSV_SEM */
16915184Sek110237 
16925184Sek110237 		target = target->fo_targetnext;
16935184Sek110237 	}
16945673Saw148015 	flowop_endop(threadflow, flowop, 0);
16955184Sek110237 
16966084Saw148015 	return (FILEBENCH_OK);
16975184Sek110237 }
16985184Sek110237 
16995184Sek110237 
17005184Sek110237 /*
17015184Sek110237  * Section for exercising create / open / close / delete operations
17025184Sek110237  * on files within a fileset. For proper operation, the flowop attribute
17035184Sek110237  * "fd", which sets the fo_fdnumber field in the flowop, must be used
17045184Sek110237  * so that the same file is opened and later closed. "fd" is an index
17055184Sek110237  * into a pair of arrays maintained by threadflows, one of which
17065184Sek110237  * contains the operating system assigned file descriptors and the other
17075184Sek110237  * a pointer to the filesetentry whose file the file descriptor
17085184Sek110237  * references. An openfile flowop defined without fd being set will use
17095184Sek110237  * the default (0) fd or, if specified, rotate through fd indices, but
17105184Sek110237  * createfile and closefile must use the default or a specified fd.
17115184Sek110237  * Meanwhile deletefile picks and arbitrary file to delete, regardless
17125184Sek110237  * of fd attribute.
17135184Sek110237  */
17145184Sek110237 
17155184Sek110237 /*
17165184Sek110237  * XXX Making file selection more consistent among the flowops might good
17175184Sek110237  */
17185184Sek110237 
17195184Sek110237 
17205184Sek110237 /*
17215184Sek110237  * Emulates (and actually does) file open. Obtains a file descriptor
17226084Saw148015  * index, then calls flowoplib_openfile_common() to open. Returns
17236084Saw148015  * FILEBENCH_ERROR if no file descriptor is found, and returns the
17246084Saw148015  * status from flowoplib_openfile_common otherwise (FILEBENCH_ERROR,
17256084Saw148015  * FILEBENCH_NORSC, FILEBENCH_OK).
17265184Sek110237  */
17275184Sek110237 static int
17285184Sek110237 flowoplib_openfile(threadflow_t *threadflow, flowop_t *flowop)
17295184Sek110237 {
17305184Sek110237 	int fd = flowoplib_fdnum(threadflow, flowop);
17315184Sek110237 
17325184Sek110237 	if (fd == -1)
17336084Saw148015 		return (FILEBENCH_ERROR);
17345184Sek110237 
17355184Sek110237 	return (flowoplib_openfile_common(threadflow, flowop, fd));
17365184Sek110237 }
17375184Sek110237 
17385184Sek110237 /*
17395184Sek110237  * Common file opening code for filesets. Uses the supplied
17405184Sek110237  * file descriptor index to determine the tf_fd entry to use.
17415184Sek110237  * If the entry is empty (0) and the fileset exists, fileset
17425184Sek110237  * pick is called to select a fileset entry to use. The file
17435184Sek110237  * specified in the filesetentry is opened, and the returned
17445184Sek110237  * operating system file descriptor and a pointer to the
17455184Sek110237  * filesetentry are stored in tf_fd[fd] and tf_fse[fd],
17466084Saw148015  * respectively. Returns FILEBENCH_ERROR on error,
17476084Saw148015  * FILEBENCH_NORSC if no suitable filesetentry can be found,
17486084Saw148015  * and FILEBENCH_OK on success.
17495184Sek110237  */
17505184Sek110237 static int
17515184Sek110237 flowoplib_openfile_common(threadflow_t *threadflow, flowop_t *flowop, int fd)
17525184Sek110237 {
17535184Sek110237 	filesetentry_t *file;
17546212Saw148015 	char *fileset_name;
17555184Sek110237 	int tid = 0;
17565184Sek110237 
17576391Saw148015 	if (flowop->fo_fileset == NULL) {
17586391Saw148015 		filebench_log(LOG_ERROR, "flowop NULL file");
17596391Saw148015 		return (FILEBENCH_ERROR);
17606391Saw148015 	}
17616391Saw148015 
17626212Saw148015 	if ((fileset_name =
17636212Saw148015 	    avd_get_str(flowop->fo_fileset->fs_name)) == NULL) {
17646212Saw148015 		filebench_log(LOG_ERROR,
17656212Saw148015 		    "flowop %s: fileset has no name", flowop->fo_name);
17666212Saw148015 		return (FILEBENCH_ERROR);
17676212Saw148015 	}
17686212Saw148015 
17695184Sek110237 	/*
17705184Sek110237 	 * If the flowop doesn't default to persistent fd
17715184Sek110237 	 * then get unique thread ID for use by fileset_pick
17725184Sek110237 	 */
17736212Saw148015 	if (avd_get_bool(flowop->fo_rotatefd))
17745184Sek110237 		tid = threadflow->tf_utid;
17755184Sek110237 
17765184Sek110237 	if (threadflow->tf_fd[fd] != 0) {
17775184Sek110237 		filebench_log(LOG_ERROR,
17785184Sek110237 		    "flowop %s attempted to open without closing on fd %d",
17795184Sek110237 		    flowop->fo_name, fd);
17806084Saw148015 		return (FILEBENCH_ERROR);
17815184Sek110237 	}
17825184Sek110237 
17835673Saw148015 #ifdef HAVE_RAW_SUPPORT
17845673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
17855673Saw148015 		int open_attrs = 0;
17865673Saw148015 		char name[MAXPATHLEN];
17875673Saw148015 
1788*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcpy(name,
1789*7946SAndrew.W.Wilson@sun.com 		    avd_get_str(flowop->fo_fileset->fs_path), MAXPATHLEN);
1790*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(name, "/", MAXPATHLEN);
1791*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(name, fileset_name, MAXPATHLEN);
17925673Saw148015 
17936212Saw148015 		if (avd_get_bool(flowop->fo_dsync)) {
17945673Saw148015 #ifdef sun
17955673Saw148015 			open_attrs |= O_DSYNC;
17965673Saw148015 #else
17975673Saw148015 			open_attrs |= O_FSYNC;
17985673Saw148015 #endif
17995673Saw148015 		}
18005673Saw148015 
18015673Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
18025673Saw148015 		    "open raw device %s flags %d = %d", name, open_attrs, fd);
18035673Saw148015 
18045673Saw148015 		threadflow->tf_fd[fd] = open64(name,
18055673Saw148015 		    O_RDWR | open_attrs, 0666);
18065673Saw148015 
18075673Saw148015 		if (threadflow->tf_fd[fd] < 0) {
18085673Saw148015 			filebench_log(LOG_ERROR,
18095673Saw148015 			    "Failed to open raw device %s: %s",
18105673Saw148015 			    name, strerror(errno));
18116084Saw148015 			return (FILEBENCH_ERROR);
18125673Saw148015 		}
18135673Saw148015 
18145673Saw148015 		/* if running on Solaris, use un-buffered io */
18155673Saw148015 #ifdef sun
18165673Saw148015 		(void) directio(threadflow->tf_fd[fd], DIRECTIO_ON);
18175673Saw148015 #endif
18185673Saw148015 
18195673Saw148015 		threadflow->tf_fse[fd] = NULL;
18205673Saw148015 
18216084Saw148015 		return (FILEBENCH_OK);
18225673Saw148015 	}
18235673Saw148015 #endif /* HAVE_RAW_SUPPORT */
18245673Saw148015 
18255184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
18265184Sek110237 	    FILESET_PICKEXISTS, tid)) == NULL) {
18276084Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
18285184Sek110237 		    "flowop %s failed to pick file from %s on fd %d",
18296212Saw148015 		    flowop->fo_name, fileset_name, fd);
18306084Saw148015 		return (FILEBENCH_NORSC);
18315184Sek110237 	}
18325184Sek110237 
18335184Sek110237 	threadflow->tf_fse[fd] = file;
18345184Sek110237 
18355184Sek110237 	flowop_beginop(threadflow, flowop);
18365184Sek110237 	threadflow->tf_fd[fd] = fileset_openfile(flowop->fo_fileset,
18375184Sek110237 	    file, O_RDWR, 0666, flowoplib_fileattrs(flowop));
18385673Saw148015 	flowop_endop(threadflow, flowop, 0);
18395184Sek110237 
18405184Sek110237 	if (threadflow->tf_fd[fd] < 0) {
18416212Saw148015 		filebench_log(LOG_ERROR, "flowop %s failed to open file %s",
18426212Saw148015 		    flowop->fo_name, file->fse_path);
18436084Saw148015 		return (FILEBENCH_ERROR);
18445184Sek110237 	}
18455184Sek110237 
18465184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT,
18475184Sek110237 	    "flowop %s: opened %s fd[%d] = %d",
18485184Sek110237 	    flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]);
18495184Sek110237 
18506084Saw148015 	return (FILEBENCH_OK);
18515184Sek110237 }
18525184Sek110237 
18535184Sek110237 /*
18545184Sek110237  * Emulate create of a file. Uses the flowop's fdnumber to select
18555184Sek110237  * tf_fd and tf_fse array locations to put the created file's file
18565184Sek110237  * descriptor and filesetentry respectively. Uses fileset_pick()
18575184Sek110237  * to select a specific filesetentry whose file does not currently
18585184Sek110237  * exist for the file create operation. Then calls
18595184Sek110237  * fileset_openfile() with the O_CREATE flag set to create the
18606084Saw148015  * file. Returns FILEBENCH_ERROR if the array index specified by fdnumber is
18615184Sek110237  * already in use, the flowop has no associated fileset, or
18625184Sek110237  * the create call fails. Returns 1 if a filesetentry with a
18636084Saw148015  * nonexistent file cannot be found. Returns FILEBENCH_OK on success.
18645184Sek110237  */
18655184Sek110237 static int
18665184Sek110237 flowoplib_createfile(threadflow_t *threadflow, flowop_t *flowop)
18675184Sek110237 {
18685184Sek110237 	filesetentry_t *file;
18695184Sek110237 	int fd = flowop->fo_fdnumber;
18705184Sek110237 
18715184Sek110237 	if (threadflow->tf_fd[fd] != 0) {
18725184Sek110237 		filebench_log(LOG_ERROR,
18735184Sek110237 		    "flowop %s attempted to create without closing on fd %d",
18745184Sek110237 		    flowop->fo_name, fd);
18756084Saw148015 		return (FILEBENCH_ERROR);
18765184Sek110237 	}
18775184Sek110237 
18785184Sek110237 	if (flowop->fo_fileset == NULL) {
18795184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
18806084Saw148015 		return (FILEBENCH_ERROR);
18815184Sek110237 	}
18825184Sek110237 
18835673Saw148015 #ifdef HAVE_RAW_SUPPORT
18845673Saw148015 	/* can't be used with raw devices */
18855673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
18865673Saw148015 		filebench_log(LOG_ERROR,
18875673Saw148015 		    "flowop %s attempted to a createfile on RAW device",
18885673Saw148015 		    flowop->fo_name);
18896084Saw148015 		return (FILEBENCH_ERROR);
18905673Saw148015 	}
18915673Saw148015 #endif /* HAVE_RAW_SUPPORT */
18925673Saw148015 
18935184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
18945184Sek110237 	    FILESET_PICKNOEXIST, 0)) == NULL) {
18956084Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
18966084Saw148015 		    "flowop %s failed to pick file from fileset %s",
18976212Saw148015 		    flowop->fo_name,
18986212Saw148015 		    avd_get_str(flowop->fo_fileset->fs_name));
18996084Saw148015 		return (FILEBENCH_NORSC);
19005184Sek110237 	}
19015184Sek110237 
19025184Sek110237 	threadflow->tf_fse[fd] = file;
19035184Sek110237 
19045184Sek110237 	flowop_beginop(threadflow, flowop);
19055184Sek110237 	threadflow->tf_fd[fd] = fileset_openfile(flowop->fo_fileset,
19065184Sek110237 	    file, O_RDWR | O_CREAT, 0666, flowoplib_fileattrs(flowop));
19075673Saw148015 	flowop_endop(threadflow, flowop, 0);
19085184Sek110237 
19095184Sek110237 	if (threadflow->tf_fd[fd] < 0) {
19105184Sek110237 		filebench_log(LOG_ERROR, "failed to create file %s",
19115184Sek110237 		    flowop->fo_name);
19126084Saw148015 		return (FILEBENCH_ERROR);
19135184Sek110237 	}
19145184Sek110237 
19155184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT,
19165184Sek110237 	    "flowop %s: created %s fd[%d] = %d",
19175184Sek110237 	    flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]);
19185184Sek110237 
19196084Saw148015 	return (FILEBENCH_OK);
19205184Sek110237 }
19215184Sek110237 
19225184Sek110237 /*
19236391Saw148015  * Emulates delete of a file. If a valid fd is provided, it uses the
19246391Saw148015  * filesetentry stored at that fd location to select the file to be
19256391Saw148015  * deleted, otherwise it picks an arbitrary filesetentry
19266391Saw148015  * whose file exists. It then uses unlink() to delete it and Clears
19276084Saw148015  * the FSE_EXISTS flag for the filesetentry. Returns FILEBENCH_ERROR if the
19286084Saw148015  * flowop has no associated fileset. Returns FILEBENCH_NORSC if an appropriate
19296084Saw148015  * filesetentry cannot be found, and FILEBENCH_OK on success.
19305184Sek110237  */
19315184Sek110237 static int
19325184Sek110237 flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop)
19335184Sek110237 {
19345184Sek110237 	filesetentry_t *file;
19355184Sek110237 	fileset_t *fileset;
19365184Sek110237 	char path[MAXPATHLEN];
19375184Sek110237 	char *pathtmp;
19386391Saw148015 	int fd = flowop->fo_fdnumber;
19395184Sek110237 
19406391Saw148015 	/* if fd specified, use it to access file */
19416391Saw148015 	if ((fd > 0) && ((file = threadflow->tf_fse[fd]) != NULL)) {
19426391Saw148015 
19436391Saw148015 		/* check whether file still open */
19446391Saw148015 		if (threadflow->tf_fd[fd] > 0) {
19456391Saw148015 			filebench_log(LOG_DEBUG_SCRIPT,
19466391Saw148015 			    "flowop %s deleting still open file at fd = %d",
19476391Saw148015 			    flowop->fo_name, fd);
19486391Saw148015 		}
19496391Saw148015 
19506391Saw148015 		/* indicate that the file will be deleted */
19516391Saw148015 		threadflow->tf_fse[fd] = NULL;
19526391Saw148015 
19536391Saw148015 		/* if here, we still have a valid file pointer */
19546391Saw148015 		fileset = file->fse_fileset;
19556391Saw148015 	} else {
19566391Saw148015 		/* Otherwise, pick arbitrary file */
19576391Saw148015 		file = NULL;
19586391Saw148015 		fileset = flowop->fo_fileset;
19596391Saw148015 	}
19606391Saw148015 
19616391Saw148015 
19626391Saw148015 	if (fileset == NULL) {
19635184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
19646084Saw148015 		return (FILEBENCH_ERROR);
19655184Sek110237 	}
19665184Sek110237 
19675673Saw148015 #ifdef HAVE_RAW_SUPPORT
19685673Saw148015 	/* can't be used with raw devices */
19696391Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV) {
19705673Saw148015 		filebench_log(LOG_ERROR,
19715673Saw148015 		    "flowop %s attempted a deletefile on RAW device",
19725673Saw148015 		    flowop->fo_name);
19736084Saw148015 		return (FILEBENCH_ERROR);
19745673Saw148015 	}
19755673Saw148015 #endif /* HAVE_RAW_SUPPORT */
19765673Saw148015 
19776391Saw148015 	if (file == NULL) {
19787556SAndrew.W.Wilson@sun.com 		/* pick arbitrary, existing (allocated) file */
19796391Saw148015 		if ((file = fileset_pick(fileset, FILESET_PICKEXISTS, 0))
19806391Saw148015 		    == NULL) {
19816391Saw148015 			filebench_log(LOG_DEBUG_SCRIPT,
19826391Saw148015 			    "flowop %s failed to pick file", flowop->fo_name);
19836391Saw148015 			return (FILEBENCH_NORSC);
19846391Saw148015 		}
19856391Saw148015 	} else {
19867556SAndrew.W.Wilson@sun.com 		/* delete specific file. wait for it to be non-busy */
19877556SAndrew.W.Wilson@sun.com 		(void) ipc_mutex_lock(&fileset->fs_pick_lock);
19887556SAndrew.W.Wilson@sun.com 		while (file->fse_flags & FSE_BUSY) {
19897556SAndrew.W.Wilson@sun.com 			file->fse_flags |= FSE_THRD_WAITNG;
19907556SAndrew.W.Wilson@sun.com 			(void) pthread_cond_wait(&fileset->fs_thrd_wait_cv,
19917556SAndrew.W.Wilson@sun.com 			    &fileset->fs_pick_lock);
19927556SAndrew.W.Wilson@sun.com 		}
19937556SAndrew.W.Wilson@sun.com 
19947556SAndrew.W.Wilson@sun.com 		/* File now available, grab it for deletion */
19957556SAndrew.W.Wilson@sun.com 		file->fse_flags |= FSE_BUSY;
19967556SAndrew.W.Wilson@sun.com 		fileset->fs_idle_files--;
19977556SAndrew.W.Wilson@sun.com 		(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
19985184Sek110237 	}
19995184Sek110237 
2000*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
2001*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
2002*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
20035184Sek110237 	pathtmp = fileset_resolvepath(file);
2004*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
20055184Sek110237 	free(pathtmp);
20065184Sek110237 
20077556SAndrew.W.Wilson@sun.com 	/* delete the selected file */
20085184Sek110237 	flowop_beginop(threadflow, flowop);
20095184Sek110237 	(void) unlink(path);
20105673Saw148015 	flowop_endop(threadflow, flowop, 0);
20117556SAndrew.W.Wilson@sun.com 
20127556SAndrew.W.Wilson@sun.com 	/* indicate that it is no longer busy and no longer exists */
20137556SAndrew.W.Wilson@sun.com 	fileset_unbusy(file, TRUE, FALSE);
20145184Sek110237 
20155184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "deleted file %s", file->fse_path);
20165184Sek110237 
20176084Saw148015 	return (FILEBENCH_OK);
20185184Sek110237 }
20195184Sek110237 
20205184Sek110237 /*
20215184Sek110237  * Emulates fsync of a file. Obtains the file descriptor index
20225184Sek110237  * from the flowop, obtains the actual file descriptor from
20235184Sek110237  * the threadflow's table, checks to be sure it is still an
20246084Saw148015  * open file, then does an fsync operation on it. Returns FILEBENCH_ERROR
20256084Saw148015  * if the file no longer is open, FILEBENCH_OK otherwise.
20265184Sek110237  */
20275184Sek110237 static int
20285184Sek110237 flowoplib_fsync(threadflow_t *threadflow, flowop_t *flowop)
20295184Sek110237 {
20305184Sek110237 	filesetentry_t *file;
20315184Sek110237 	int fd = flowop->fo_fdnumber;
20325184Sek110237 
20335184Sek110237 	if (threadflow->tf_fd[fd] == 0) {
20345184Sek110237 		filebench_log(LOG_ERROR,
20355184Sek110237 		    "flowop %s attempted to fsync a closed fd %d",
20365184Sek110237 		    flowop->fo_name, fd);
20376084Saw148015 		return (FILEBENCH_ERROR);
20385184Sek110237 	}
20395184Sek110237 
20405673Saw148015 	file = threadflow->tf_fse[fd];
20415673Saw148015 
20425673Saw148015 	if ((file == NULL) ||
20435673Saw148015 	    (file->fse_fileset->fs_attrs & FILESET_IS_RAW_DEV)) {
20445673Saw148015 		filebench_log(LOG_ERROR,
20455673Saw148015 		    "flowop %s attempted to a fsync a RAW device",
20465673Saw148015 		    flowop->fo_name);
20476084Saw148015 		return (FILEBENCH_ERROR);
20485673Saw148015 	}
20495673Saw148015 
20505184Sek110237 	/* Measure time to fsync */
20515184Sek110237 	flowop_beginop(threadflow, flowop);
20525184Sek110237 	(void) fsync(threadflow->tf_fd[fd]);
20535673Saw148015 	flowop_endop(threadflow, flowop, 0);
20545184Sek110237 
20555184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s", file->fse_path);
20565184Sek110237 
20576084Saw148015 	return (FILEBENCH_OK);
20585184Sek110237 }
20595184Sek110237 
20605184Sek110237 /*
20615184Sek110237  * Emulate fsync of an entire fileset. Search through the
20625184Sek110237  * threadflow's file descriptor array, doing fsync() on each
20635184Sek110237  * open file that belongs to the flowop's fileset. Always
20646084Saw148015  * returns FILEBENCH_OK.
20655184Sek110237  */
20665184Sek110237 static int
20675184Sek110237 flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop)
20685184Sek110237 {
20695184Sek110237 	int fd;
20705184Sek110237 
20715184Sek110237 	for (fd = 0; fd < THREADFLOW_MAXFD; fd++) {
20725184Sek110237 		filesetentry_t *file;
20735184Sek110237 
20745184Sek110237 		/* Match the file set to fsync */
20755184Sek110237 		if ((threadflow->tf_fse[fd] == NULL) ||
20765184Sek110237 		    (flowop->fo_fileset != threadflow->tf_fse[fd]->fse_fileset))
20775184Sek110237 			continue;
20785184Sek110237 
20795184Sek110237 		/* Measure time to fsync */
20805184Sek110237 		flowop_beginop(threadflow, flowop);
20815184Sek110237 		(void) fsync(threadflow->tf_fd[fd]);
20825673Saw148015 		flowop_endop(threadflow, flowop, 0);
20835184Sek110237 
20845184Sek110237 		file = threadflow->tf_fse[fd];
20855184Sek110237 
20865184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s",
20875184Sek110237 		    file->fse_path);
20885184Sek110237 	}
20895184Sek110237 
20906084Saw148015 	return (FILEBENCH_OK);
20915184Sek110237 }
20925184Sek110237 
20935184Sek110237 /*
20945184Sek110237  * Emulate close of a file.  Obtains the file descriptor index
20955184Sek110237  * from the flowop, obtains the actual file descriptor from the
20965184Sek110237  * threadflow's table, checks to be sure it is still an open
20975184Sek110237  * file, then does a close operation on it. Then sets the
20985184Sek110237  * threadflow file descriptor table entry to 0, and the file set
20996084Saw148015  * entry pointer to NULL. Returns FILEBENCH_ERROR if the file was not open,
21006084Saw148015  * FILEBENCH_OK otherwise.
21015184Sek110237  */
21025184Sek110237 static int
21035184Sek110237 flowoplib_closefile(threadflow_t *threadflow, flowop_t *flowop)
21045184Sek110237 {
21055184Sek110237 	filesetentry_t *file;
21065184Sek110237 	int fd = flowop->fo_fdnumber;
21075184Sek110237 
21085184Sek110237 	if (threadflow->tf_fd[fd] == 0) {
21095184Sek110237 		filebench_log(LOG_ERROR,
21105184Sek110237 		    "flowop %s attempted to close an already closed fd %d",
21115184Sek110237 		    flowop->fo_name, fd);
21126084Saw148015 		return (FILEBENCH_ERROR);
21135184Sek110237 	}
21145184Sek110237 
21155184Sek110237 	/* Measure time to close */
21165184Sek110237 	flowop_beginop(threadflow, flowop);
21175184Sek110237 	(void) close(threadflow->tf_fd[fd]);
21185673Saw148015 	flowop_endop(threadflow, flowop, 0);
21195184Sek110237 
21205184Sek110237 	file = threadflow->tf_fse[fd];
21215184Sek110237 
21225184Sek110237 	threadflow->tf_fd[fd] = 0;
21235184Sek110237 
21245184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "closed file %s", file->fse_path);
21255184Sek110237 
21266084Saw148015 	return (FILEBENCH_OK);
21275184Sek110237 }
21285184Sek110237 
21295184Sek110237 /*
2130*7946SAndrew.W.Wilson@sun.com  * Obtain a filesetentry for a leaf directory. Result placed where dirp
2131*7946SAndrew.W.Wilson@sun.com  * points. Supply with flowop and a flag to indicate whether an existent
2132*7946SAndrew.W.Wilson@sun.com  * or non-existent leaf directory is required. Returns FILEBENCH_NORSC
2133*7946SAndrew.W.Wilson@sun.com  * if all out of the appropriate type of directories, FILEBENCH_ERROR
2134*7946SAndrew.W.Wilson@sun.com  * if the flowop does not point to a fileset, and FILEBENCH_OK otherwise.
2135*7946SAndrew.W.Wilson@sun.com  */
2136*7946SAndrew.W.Wilson@sun.com static int
2137*7946SAndrew.W.Wilson@sun.com flowoplib_pickleafdir(filesetentry_t **dirp, flowop_t *flowop, int flags)
2138*7946SAndrew.W.Wilson@sun.com {
2139*7946SAndrew.W.Wilson@sun.com 	fileset_t	*fileset;
2140*7946SAndrew.W.Wilson@sun.com 
2141*7946SAndrew.W.Wilson@sun.com 	if ((fileset = flowop->fo_fileset) == NULL) {
2142*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "flowop NO fileset");
2143*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
2144*7946SAndrew.W.Wilson@sun.com 	}
2145*7946SAndrew.W.Wilson@sun.com 
2146*7946SAndrew.W.Wilson@sun.com 	if ((*dirp = fileset_pick(fileset,
2147*7946SAndrew.W.Wilson@sun.com 	    FILESET_PICKLEAFDIR | flags, 0)) == NULL) {
2148*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_DEBUG_SCRIPT,
2149*7946SAndrew.W.Wilson@sun.com 		    "flowop %s failed to pick directory from fileset %s",
2150*7946SAndrew.W.Wilson@sun.com 		    flowop->fo_name,
2151*7946SAndrew.W.Wilson@sun.com 		    avd_get_str(fileset->fs_name));
2152*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_NORSC);
2153*7946SAndrew.W.Wilson@sun.com 	}
2154*7946SAndrew.W.Wilson@sun.com 
2155*7946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2156*7946SAndrew.W.Wilson@sun.com }
2157*7946SAndrew.W.Wilson@sun.com 
2158*7946SAndrew.W.Wilson@sun.com /*
2159*7946SAndrew.W.Wilson@sun.com  * Obtain the full pathname of the directory described by the filesetentry
2160*7946SAndrew.W.Wilson@sun.com  * indicated by "dir", and copy it into the character array pointed to by
2161*7946SAndrew.W.Wilson@sun.com  * path. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
2162*7946SAndrew.W.Wilson@sun.com  */
2163*7946SAndrew.W.Wilson@sun.com static int
2164*7946SAndrew.W.Wilson@sun.com flowoplib_getdirpath(filesetentry_t *dir, char *path)
2165*7946SAndrew.W.Wilson@sun.com {
2166*7946SAndrew.W.Wilson@sun.com 	char		*fileset_path;
2167*7946SAndrew.W.Wilson@sun.com 	char		*fileset_name;
2168*7946SAndrew.W.Wilson@sun.com 	char		*part_path;
2169*7946SAndrew.W.Wilson@sun.com 
2170*7946SAndrew.W.Wilson@sun.com 	if ((fileset_path = avd_get_str(dir->fse_fileset->fs_path)) == NULL) {
2171*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "Fileset path not set");
2172*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
2173*7946SAndrew.W.Wilson@sun.com 	}
2174*7946SAndrew.W.Wilson@sun.com 
2175*7946SAndrew.W.Wilson@sun.com 	if ((fileset_name = avd_get_str(dir->fse_fileset->fs_name)) == NULL) {
2176*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "Fileset name not set");
2177*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
2178*7946SAndrew.W.Wilson@sun.com 	}
2179*7946SAndrew.W.Wilson@sun.com 
2180*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcpy(path, fileset_path, MAXPATHLEN);
2181*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, "/", MAXPATHLEN);
2182*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, fileset_name, MAXPATHLEN);
2183*7946SAndrew.W.Wilson@sun.com 
2184*7946SAndrew.W.Wilson@sun.com 	if ((part_path = fileset_resolvepath(dir)) == NULL)
2185*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
2186*7946SAndrew.W.Wilson@sun.com 
2187*7946SAndrew.W.Wilson@sun.com 	(void) fb_strlcat(path, part_path, MAXPATHLEN);
2188*7946SAndrew.W.Wilson@sun.com 	free(part_path);
2189*7946SAndrew.W.Wilson@sun.com 
2190*7946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2191*7946SAndrew.W.Wilson@sun.com }
2192*7946SAndrew.W.Wilson@sun.com 
2193*7946SAndrew.W.Wilson@sun.com /*
2194*7946SAndrew.W.Wilson@sun.com  * Use mkdir to create a directory.  Obtains the fileset name from the
2195*7946SAndrew.W.Wilson@sun.com  * flowop, selects a non-existent leaf directory and obtains its full
2196*7946SAndrew.W.Wilson@sun.com  * path, then uses mkdir to create it on the storage subsystem (make it
2197*7946SAndrew.W.Wilson@sun.com  * existent). Returns FILEBENCH_NORSC is there are no more non-existent
2198*7946SAndrew.W.Wilson@sun.com  * directories in the fileset, FILEBENCH_ERROR on other errors, and
2199*7946SAndrew.W.Wilson@sun.com  * FILEBENCH_OK on success.
2200*7946SAndrew.W.Wilson@sun.com  */
2201*7946SAndrew.W.Wilson@sun.com static int
2202*7946SAndrew.W.Wilson@sun.com flowoplib_makedir(threadflow_t *threadflow, flowop_t *flowop)
2203*7946SAndrew.W.Wilson@sun.com {
2204*7946SAndrew.W.Wilson@sun.com 	filesetentry_t	*dir;
2205*7946SAndrew.W.Wilson@sun.com 	int		ret;
2206*7946SAndrew.W.Wilson@sun.com 	char		full_path[MAXPATHLEN];
2207*7946SAndrew.W.Wilson@sun.com 
2208*7946SAndrew.W.Wilson@sun.com 	if ((ret = flowoplib_pickleafdir(&dir, flowop,
2209*7946SAndrew.W.Wilson@sun.com 	    FILESET_PICKNOEXIST)) != FILEBENCH_OK)
2210*7946SAndrew.W.Wilson@sun.com 		return (ret);
2211*7946SAndrew.W.Wilson@sun.com 
2212*7946SAndrew.W.Wilson@sun.com 	if ((ret = flowoplib_getdirpath(dir, full_path)) != FILEBENCH_OK)
2213*7946SAndrew.W.Wilson@sun.com 		return (ret);
2214*7946SAndrew.W.Wilson@sun.com 
2215*7946SAndrew.W.Wilson@sun.com 	flowop_beginop(threadflow, flowop);
2216*7946SAndrew.W.Wilson@sun.com 	(void) mkdir(full_path, 0755);
2217*7946SAndrew.W.Wilson@sun.com 	flowop_endop(threadflow, flowop, 0);
2218*7946SAndrew.W.Wilson@sun.com 
2219*7946SAndrew.W.Wilson@sun.com 	/* indicate that it is no longer busy and now exists */
2220*7946SAndrew.W.Wilson@sun.com 	fileset_unbusy(dir, TRUE, TRUE);
2221*7946SAndrew.W.Wilson@sun.com 
2222*7946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2223*7946SAndrew.W.Wilson@sun.com }
2224*7946SAndrew.W.Wilson@sun.com 
2225*7946SAndrew.W.Wilson@sun.com /*
2226*7946SAndrew.W.Wilson@sun.com  * Use rmdir to delete a directory.  Obtains the fileset name from the
2227*7946SAndrew.W.Wilson@sun.com  * flowop, selects an existent leaf directory and obtains its full path,
2228*7946SAndrew.W.Wilson@sun.com  * then uses rmdir to remove it from the storage subsystem (make it
2229*7946SAndrew.W.Wilson@sun.com  * non-existent). Returns FILEBENCH_NORSC is there are no more existent
2230*7946SAndrew.W.Wilson@sun.com  * directories in the fileset, FILEBENCH_ERROR on other errors, and
2231*7946SAndrew.W.Wilson@sun.com  * FILEBENCH_OK on success.
2232*7946SAndrew.W.Wilson@sun.com  */
2233*7946SAndrew.W.Wilson@sun.com static int
2234*7946SAndrew.W.Wilson@sun.com flowoplib_removedir(threadflow_t *threadflow, flowop_t *flowop)
2235*7946SAndrew.W.Wilson@sun.com {
2236*7946SAndrew.W.Wilson@sun.com 	filesetentry_t *dir;
2237*7946SAndrew.W.Wilson@sun.com 	int		ret;
2238*7946SAndrew.W.Wilson@sun.com 	char		full_path[MAXPATHLEN];
2239*7946SAndrew.W.Wilson@sun.com 
2240*7946SAndrew.W.Wilson@sun.com 	if ((ret = flowoplib_pickleafdir(&dir, flowop,
2241*7946SAndrew.W.Wilson@sun.com 	    FILESET_PICKEXISTS)) != FILEBENCH_OK)
2242*7946SAndrew.W.Wilson@sun.com 		return (ret);
2243*7946SAndrew.W.Wilson@sun.com 
2244*7946SAndrew.W.Wilson@sun.com 	if ((ret = flowoplib_getdirpath(dir, full_path)) != FILEBENCH_OK)
2245*7946SAndrew.W.Wilson@sun.com 		return (ret);
2246*7946SAndrew.W.Wilson@sun.com 
2247*7946SAndrew.W.Wilson@sun.com 	flowop_beginop(threadflow, flowop);
2248*7946SAndrew.W.Wilson@sun.com 	(void) rmdir(full_path);
2249*7946SAndrew.W.Wilson@sun.com 	flowop_endop(threadflow, flowop, 0);
2250*7946SAndrew.W.Wilson@sun.com 
2251*7946SAndrew.W.Wilson@sun.com 	/* indicate that it is no longer busy and no longer exists */
2252*7946SAndrew.W.Wilson@sun.com 	fileset_unbusy(dir, TRUE, FALSE);
2253*7946SAndrew.W.Wilson@sun.com 
2254*7946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2255*7946SAndrew.W.Wilson@sun.com }
2256*7946SAndrew.W.Wilson@sun.com 
2257*7946SAndrew.W.Wilson@sun.com /*
2258*7946SAndrew.W.Wilson@sun.com  * Use opendir(), multiple readdir() calls, and closedir() to list the
2259*7946SAndrew.W.Wilson@sun.com  * contents of a directory.  Obtains the fileset name from the
2260*7946SAndrew.W.Wilson@sun.com  * flowop, selects a normal subdirectory (which always exist) and obtains
2261*7946SAndrew.W.Wilson@sun.com  * its full path, then uses opendir() to get a DIR handle to it from the
2262*7946SAndrew.W.Wilson@sun.com  * file system, a readdir() loop to access each directory entry, and
2263*7946SAndrew.W.Wilson@sun.com  * finally cleans up with a closedir(). The latency reported is the total
2264*7946SAndrew.W.Wilson@sun.com  * for all this activity, and it also reports the total number of bytes
2265*7946SAndrew.W.Wilson@sun.com  * in the entries as the amount "read". Returns FILEBENCH_ERROR on errors,
2266*7946SAndrew.W.Wilson@sun.com  * and FILEBENCH_OK on success.
2267*7946SAndrew.W.Wilson@sun.com  */
2268*7946SAndrew.W.Wilson@sun.com static int
2269*7946SAndrew.W.Wilson@sun.com flowoplib_listdir(threadflow_t *threadflow, flowop_t *flowop)
2270*7946SAndrew.W.Wilson@sun.com {
2271*7946SAndrew.W.Wilson@sun.com 	fileset_t	*fileset;
2272*7946SAndrew.W.Wilson@sun.com 	filesetentry_t	*dir;
2273*7946SAndrew.W.Wilson@sun.com 	DIR		*dir_handlep;
2274*7946SAndrew.W.Wilson@sun.com 	struct dirent	*direntp;
2275*7946SAndrew.W.Wilson@sun.com 	int		dir_bytes = 0;
2276*7946SAndrew.W.Wilson@sun.com 	int		ret;
2277*7946SAndrew.W.Wilson@sun.com 	char		full_path[MAXPATHLEN];
2278*7946SAndrew.W.Wilson@sun.com 
2279*7946SAndrew.W.Wilson@sun.com 	if ((fileset = flowop->fo_fileset) == NULL) {
2280*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR, "flowop NO fileset");
2281*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
2282*7946SAndrew.W.Wilson@sun.com 	}
2283*7946SAndrew.W.Wilson@sun.com 
2284*7946SAndrew.W.Wilson@sun.com 	if ((dir = fileset_pick(fileset,
2285*7946SAndrew.W.Wilson@sun.com 	    FILESET_PICKDIR, 0)) == NULL) {
2286*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_DEBUG_SCRIPT,
2287*7946SAndrew.W.Wilson@sun.com 		    "flowop %s failed to pick directory from fileset %s",
2288*7946SAndrew.W.Wilson@sun.com 		    flowop->fo_name,
2289*7946SAndrew.W.Wilson@sun.com 		    avd_get_str(fileset->fs_name));
2290*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_NORSC);
2291*7946SAndrew.W.Wilson@sun.com 	}
2292*7946SAndrew.W.Wilson@sun.com 
2293*7946SAndrew.W.Wilson@sun.com 	if ((ret = flowoplib_getdirpath(dir, full_path)) != FILEBENCH_OK)
2294*7946SAndrew.W.Wilson@sun.com 		return (ret);
2295*7946SAndrew.W.Wilson@sun.com 
2296*7946SAndrew.W.Wilson@sun.com 	flowop_beginop(threadflow, flowop);
2297*7946SAndrew.W.Wilson@sun.com 
2298*7946SAndrew.W.Wilson@sun.com 	/* open the directory */
2299*7946SAndrew.W.Wilson@sun.com 	if ((dir_handlep = opendir(full_path)) == NULL) {
2300*7946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
2301*7946SAndrew.W.Wilson@sun.com 		    "flowop %s failed to open directory in fileset %s\n",
2302*7946SAndrew.W.Wilson@sun.com 		    flowop->fo_name, avd_get_str(fileset->fs_name));
2303*7946SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
2304*7946SAndrew.W.Wilson@sun.com 	}
2305*7946SAndrew.W.Wilson@sun.com 
2306*7946SAndrew.W.Wilson@sun.com 	/* read through the directory entries */
2307*7946SAndrew.W.Wilson@sun.com 	while ((direntp = readdir(dir_handlep)) != NULL) {
2308*7946SAndrew.W.Wilson@sun.com 		dir_bytes += (strlen(direntp->d_name) +
2309*7946SAndrew.W.Wilson@sun.com 		    sizeof (struct dirent) - 1);
2310*7946SAndrew.W.Wilson@sun.com 	}
2311*7946SAndrew.W.Wilson@sun.com 
2312*7946SAndrew.W.Wilson@sun.com 	/* close the directory */
2313*7946SAndrew.W.Wilson@sun.com 	(void) closedir(dir_handlep);
2314*7946SAndrew.W.Wilson@sun.com 
2315*7946SAndrew.W.Wilson@sun.com 	flowop_endop(threadflow, flowop, dir_bytes);
2316*7946SAndrew.W.Wilson@sun.com 
2317*7946SAndrew.W.Wilson@sun.com 	/* indicate that it is no longer busy */
2318*7946SAndrew.W.Wilson@sun.com 	fileset_unbusy(dir, FALSE, FALSE);
2319*7946SAndrew.W.Wilson@sun.com 
2320*7946SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
2321*7946SAndrew.W.Wilson@sun.com }
2322*7946SAndrew.W.Wilson@sun.com 
2323*7946SAndrew.W.Wilson@sun.com /*
23245184Sek110237  * Emulate stat of a file. Picks an arbitrary filesetentry with
23255184Sek110237  * an existing file from the flowop's fileset, then performs a
23266084Saw148015  * stat() operation on it. Returns FILEBENCH_ERROR if the flowop has no
23276084Saw148015  * associated fileset. Returns FILEBENCH_NORSC if an appropriate filesetentry
23286084Saw148015  * cannot be found, and FILEBENCH_OK on success.
23295184Sek110237  */
23305184Sek110237 static int
23315184Sek110237 flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop)
23325184Sek110237 {
23335184Sek110237 	filesetentry_t *file;
23345184Sek110237 	fileset_t *fileset;
23357556SAndrew.W.Wilson@sun.com 	struct stat statbuf;
23367556SAndrew.W.Wilson@sun.com 	int fd = flowop->fo_fdnumber;
23377556SAndrew.W.Wilson@sun.com 
23387556SAndrew.W.Wilson@sun.com 	/* if fd specified and the file is open, use it to access file */
23397556SAndrew.W.Wilson@sun.com 	if ((fd > 0) && ((threadflow->tf_fd[fd]) > 0)) {
23407556SAndrew.W.Wilson@sun.com 
23417556SAndrew.W.Wilson@sun.com 		/* check whether file handle still valid */
23427556SAndrew.W.Wilson@sun.com 		if ((file = threadflow->tf_fse[fd]) == NULL) {
23437556SAndrew.W.Wilson@sun.com 			filebench_log(LOG_DEBUG_SCRIPT,
23447556SAndrew.W.Wilson@sun.com 			    "flowop %s trying to stat NULL file at fd = %d",
23457556SAndrew.W.Wilson@sun.com 			    flowop->fo_name, fd);
23467556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_ERROR);
23477556SAndrew.W.Wilson@sun.com 		}
23487556SAndrew.W.Wilson@sun.com 
23497556SAndrew.W.Wilson@sun.com 		/* if here, we still have a valid file pointer */
23507556SAndrew.W.Wilson@sun.com 		fileset = file->fse_fileset;
23517556SAndrew.W.Wilson@sun.com 	} else {
23527556SAndrew.W.Wilson@sun.com 		/* Otherwise, pick arbitrary file */
23537556SAndrew.W.Wilson@sun.com 		file = NULL;
23547556SAndrew.W.Wilson@sun.com 		fileset = flowop->fo_fileset;
23557556SAndrew.W.Wilson@sun.com 	}
23567556SAndrew.W.Wilson@sun.com 
23577556SAndrew.W.Wilson@sun.com 	if (fileset == NULL) {
23587556SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
23597556SAndrew.W.Wilson@sun.com 		    "statfile with no fileset specified");
23607556SAndrew.W.Wilson@sun.com 		return (FILEBENCH_ERROR);
23617556SAndrew.W.Wilson@sun.com 	}
23627556SAndrew.W.Wilson@sun.com 
23637556SAndrew.W.Wilson@sun.com #ifdef HAVE_RAW_SUPPORT
23647556SAndrew.W.Wilson@sun.com 	/* can't be used with raw devices */
23657556SAndrew.W.Wilson@sun.com 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV) {
23667556SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
23677556SAndrew.W.Wilson@sun.com 		    "flowop %s attempted do a statfile on a RAW device",
23687556SAndrew.W.Wilson@sun.com 		    flowop->fo_name);
23696084Saw148015 		return (FILEBENCH_ERROR);
23705184Sek110237 	}
23717556SAndrew.W.Wilson@sun.com #endif /* HAVE_RAW_SUPPORT */
23727556SAndrew.W.Wilson@sun.com 
23737556SAndrew.W.Wilson@sun.com 	if (file == NULL) {
23747556SAndrew.W.Wilson@sun.com 		char path[MAXPATHLEN];
23757556SAndrew.W.Wilson@sun.com 		char *pathtmp;
23767556SAndrew.W.Wilson@sun.com 
23777556SAndrew.W.Wilson@sun.com 		/* pick arbitrary, existing (allocated) file */
23787556SAndrew.W.Wilson@sun.com 		if ((file = fileset_pick(fileset, FILESET_PICKEXISTS, 0))
23797556SAndrew.W.Wilson@sun.com 		    == NULL) {
23807556SAndrew.W.Wilson@sun.com 			filebench_log(LOG_DEBUG_SCRIPT,
23817556SAndrew.W.Wilson@sun.com 			    "Statfile flowop %s failed to pick file",
23827556SAndrew.W.Wilson@sun.com 			    flowop->fo_name);
23837556SAndrew.W.Wilson@sun.com 			return (FILEBENCH_NORSC);
23847556SAndrew.W.Wilson@sun.com 		}
23857556SAndrew.W.Wilson@sun.com 
23867556SAndrew.W.Wilson@sun.com 		/* resolve path and do a stat on file */
2387*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcpy(path, avd_get_str(fileset->fs_path),
2388*7946SAndrew.W.Wilson@sun.com 		    MAXPATHLEN);
2389*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(path, "/", MAXPATHLEN);
2390*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(path, avd_get_str(fileset->fs_name),
2391*7946SAndrew.W.Wilson@sun.com 		    MAXPATHLEN);
23927556SAndrew.W.Wilson@sun.com 		pathtmp = fileset_resolvepath(file);
2393*7946SAndrew.W.Wilson@sun.com 		(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
23947556SAndrew.W.Wilson@sun.com 		free(pathtmp);
23957556SAndrew.W.Wilson@sun.com 
23967556SAndrew.W.Wilson@sun.com 		/* stat the file */
23977556SAndrew.W.Wilson@sun.com 		flowop_beginop(threadflow, flowop);
23987556SAndrew.W.Wilson@sun.com 		if (stat(path, &statbuf) == -1)
23997556SAndrew.W.Wilson@sun.com 			filebench_log(LOG_ERROR,
24007556SAndrew.W.Wilson@sun.com 			    "statfile flowop %s failed", flowop->fo_name);
24017556SAndrew.W.Wilson@sun.com 		flowop_endop(threadflow, flowop, 0);
24027556SAndrew.W.Wilson@sun.com 
24037556SAndrew.W.Wilson@sun.com 		fileset_unbusy(file, FALSE, FALSE);
24047556SAndrew.W.Wilson@sun.com 	} else {
24057556SAndrew.W.Wilson@sun.com 		/* stat specific file */
24067556SAndrew.W.Wilson@sun.com 		flowop_beginop(threadflow, flowop);
24077556SAndrew.W.Wilson@sun.com 		if (fstat(threadflow->tf_fd[fd], &statbuf) == -1)
24087556SAndrew.W.Wilson@sun.com 			filebench_log(LOG_ERROR,
24097556SAndrew.W.Wilson@sun.com 			    "statfile flowop %s failed", flowop->fo_name);
24107556SAndrew.W.Wilson@sun.com 		flowop_endop(threadflow, flowop, 0);
24117556SAndrew.W.Wilson@sun.com 
24125184Sek110237 	}
24135184Sek110237 
24146084Saw148015 	return (FILEBENCH_OK);
24155184Sek110237 }
24165184Sek110237 
24175184Sek110237 
24185184Sek110237 /*
24195184Sek110237  * Additional reads and writes. Read and write whole files, write
24205184Sek110237  * and append to files. Some of these work with both fileobjs and
24215184Sek110237  * filesets, others only with filesets. The flowoplib_write routine
24225184Sek110237  * writes from thread memory, while the others read or write using
24235184Sek110237  * fo_buf memory. Note that both flowoplib_read() and
24245184Sek110237  * flowoplib_aiowrite() use thread memory as well.
24255184Sek110237  */
24265184Sek110237 
24275184Sek110237 
24285184Sek110237 /*
24295673Saw148015  * Emulate a read of a whole file. The file must be open with
24305673Saw148015  * file descriptor and filesetentry stored at the locations indexed
24315673Saw148015  * by the flowop's fdnumber. It then seeks to the beginning of the
24325673Saw148015  * associated file, and reads fs_iosize bytes at a time until the end
24336084Saw148015  * of the file. Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if
24346084Saw148015  * out of files, and FILEBENCH_OK on success.
24355184Sek110237  */
24365184Sek110237 static int
24375184Sek110237 flowoplib_readwholefile(threadflow_t *threadflow, flowop_t *flowop)
24385184Sek110237 {
24395673Saw148015 	caddr_t iobuf;
24405184Sek110237 	off64_t bytes = 0;
24415673Saw148015 	int filedesc;
24426212Saw148015 	uint64_t wss;
24436212Saw148015 	fbint_t iosize;
24445184Sek110237 	int ret;
24456212Saw148015 	char zerordbuf;
24465184Sek110237 
24475673Saw148015 	/* get the file to use */
24486084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, &wss,
24496084Saw148015 	    &filedesc)) != FILEBENCH_OK)
24506084Saw148015 		return (ret);
24515184Sek110237 
24525673Saw148015 	/* an I/O size of zero means read entire working set with one I/O */
24536212Saw148015 	if ((iosize = avd_get_int(flowop->fo_iosize)) == 0)
24545673Saw148015 		iosize = wss;
24555184Sek110237 
24566212Saw148015 	/*
24576212Saw148015 	 * The file may actually be 0 bytes long, in which case skip
24586212Saw148015 	 * the buffer set up call (which would fail) and substitute
24596212Saw148015 	 * a small buffer, which won't really be used.
24606212Saw148015 	 */
24616212Saw148015 	if (iosize == 0) {
24626212Saw148015 		iobuf = (caddr_t)&zerordbuf;
24636212Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
24646212Saw148015 		    "flowop %s read zero length file", flowop->fo_name);
24656212Saw148015 	} else {
24666212Saw148015 		if (flowoplib_iobufsetup(threadflow, flowop, &iobuf,
24676212Saw148015 		    iosize) != 0)
24686212Saw148015 			return (FILEBENCH_ERROR);
24696212Saw148015 	}
24705184Sek110237 
24715184Sek110237 	/* Measure time to read bytes */
24725184Sek110237 	flowop_beginop(threadflow, flowop);
24735673Saw148015 	(void) lseek64(filedesc, 0, SEEK_SET);
24745673Saw148015 	while ((ret = read(filedesc, iobuf, iosize)) > 0)
24755184Sek110237 		bytes += ret;
24765184Sek110237 
24775673Saw148015 	flowop_endop(threadflow, flowop, bytes);
24785184Sek110237 
24795184Sek110237 	if (ret < 0) {
24805184Sek110237 		filebench_log(LOG_ERROR,
24816391Saw148015 		    "readwhole fail Failed to read whole file: %s",
24826391Saw148015 		    strerror(errno));
24836084Saw148015 		return (FILEBENCH_ERROR);
24845184Sek110237 	}
24855184Sek110237 
24866084Saw148015 	return (FILEBENCH_OK);
24875184Sek110237 }
24885184Sek110237 
24895184Sek110237 /*
24905184Sek110237  * Emulate a write to a file of size fo_iosize.  Will write
24915184Sek110237  * to a file from a fileset if the flowop's fo_fileset field
24925184Sek110237  * specifies one or its fdnumber is non zero. Otherwise it
24935184Sek110237  * will write to a fileobj file, if one exists. If the file
24945184Sek110237  * is not currently open, the routine will attempt to open
24955184Sek110237  * it. The flowop's fo_wss parameter will be used to set the
24965184Sek110237  * maximum file size if it is non-zero, otherwise the
24975184Sek110237  * filesetentry's  fse_size will be used. A random memory
24985184Sek110237  * buffer offset is calculated, and, if fo_random is TRUE,
24995184Sek110237  * a random file offset is used for the write. Otherwise the
25006084Saw148015  * write is to the next sequential location. Returns
25016084Saw148015  * FILEBENCH_ERROR on errors, FILEBENCH_NORSC if iosetup can't
25026084Saw148015  * obtain a file, or FILEBENCH_OK on success.
25035184Sek110237  */
25045184Sek110237 static int
25055184Sek110237 flowoplib_write(threadflow_t *threadflow, flowop_t *flowop)
25065184Sek110237 {
25075673Saw148015 	caddr_t iobuf;
25086212Saw148015 	fbint_t wss;
25096212Saw148015 	fbint_t iosize;
25105184Sek110237 	int filedesc;
25116084Saw148015 	int ret;
25125184Sek110237 
25136212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
25146084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
25156212Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
25166084Saw148015 		return (ret);
25175184Sek110237 
25186212Saw148015 	if (avd_get_bool(flowop->fo_random)) {
25195184Sek110237 		uint64_t fileoffset;
25205184Sek110237 
25215184Sek110237 		if (filebench_randomno64(&fileoffset,
25226212Saw148015 		    wss, iosize, NULL) == -1) {
25235184Sek110237 			filebench_log(LOG_ERROR,
25245184Sek110237 			    "file size smaller than IO size for thread %s",
25255184Sek110237 			    flowop->fo_name);
25266084Saw148015 			return (FILEBENCH_ERROR);
25275184Sek110237 		}
25285184Sek110237 		flowop_beginop(threadflow, flowop);
25295673Saw148015 		if (pwrite64(filedesc, iobuf,
25306212Saw148015 		    iosize, (off64_t)fileoffset) == -1) {
25315184Sek110237 			filebench_log(LOG_ERROR, "write failed, "
25326286Saw148015 			    "offset %llu io buffer %zd: %s",
25336286Saw148015 			    (u_longlong_t)fileoffset, iobuf, strerror(errno));
25345673Saw148015 			flowop_endop(threadflow, flowop, 0);
25356084Saw148015 			return (FILEBENCH_ERROR);
25365184Sek110237 		}
25376212Saw148015 		flowop_endop(threadflow, flowop, iosize);
25385184Sek110237 	} else {
25395184Sek110237 		flowop_beginop(threadflow, flowop);
25406391Saw148015 		if (write(filedesc, iobuf, iosize) == -1) {
25415184Sek110237 			filebench_log(LOG_ERROR,
25425673Saw148015 			    "write failed, io buffer %zd: %s",
25435673Saw148015 			    iobuf, strerror(errno));
25445673Saw148015 			flowop_endop(threadflow, flowop, 0);
25456084Saw148015 			return (FILEBENCH_ERROR);
25465184Sek110237 		}
25476212Saw148015 		flowop_endop(threadflow, flowop, iosize);
25485184Sek110237 	}
25495184Sek110237 
25506084Saw148015 	return (FILEBENCH_OK);
25515184Sek110237 }
25525184Sek110237 
25535184Sek110237 /*
25545184Sek110237  * Emulate a write of a whole file.  The size of the file
25555673Saw148015  * is taken from a filesetentry identified by fo_srcfdnumber or
25565673Saw148015  * from the working set size, while the file descriptor used is
25575673Saw148015  * identified by fo_fdnumber. Does multiple writes of fo_iosize
25586084Saw148015  * length length until full file has been written. Returns FILEBENCH_ERROR on
25596084Saw148015  * error, FILEBENCH_NORSC if out of files, FILEBENCH_OK on success.
25605184Sek110237  */
25615184Sek110237 static int
25625184Sek110237 flowoplib_writewholefile(threadflow_t *threadflow, flowop_t *flowop)
25635184Sek110237 {
25645673Saw148015 	caddr_t iobuf;
25655184Sek110237 	filesetentry_t *file;
25665184Sek110237 	int wsize;
25675184Sek110237 	off64_t seek;
25685184Sek110237 	off64_t bytes = 0;
25695673Saw148015 	uint64_t wss;
25706212Saw148015 	fbint_t iosize;
25715673Saw148015 	int filedesc;
25725184Sek110237 	int srcfd = flowop->fo_srcfdnumber;
25735184Sek110237 	int ret;
25746212Saw148015 	char zerowrtbuf;
25755184Sek110237 
25765673Saw148015 	/* get the file to use */
25776084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, &wss,
25786084Saw148015 	    &filedesc)) != FILEBENCH_OK)
25796084Saw148015 		return (ret);
25805184Sek110237 
25816212Saw148015 	/* an I/O size of zero means write entire working set with one I/O */
25826212Saw148015 	if ((iosize = avd_get_int(flowop->fo_iosize)) == 0)
25835673Saw148015 		iosize = wss;
25845184Sek110237 
25856212Saw148015 	/*
25866212Saw148015 	 * The file may actually be 0 bytes long, in which case skip
25876212Saw148015 	 * the buffer set up call (which would fail) and substitute
25886212Saw148015 	 * a small buffer, which won't really be used.
25896212Saw148015 	 */
25906212Saw148015 	if (iosize == 0) {
25916212Saw148015 		iobuf = (caddr_t)&zerowrtbuf;
25926212Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
25936212Saw148015 		    "flowop %s wrote zero length file", flowop->fo_name);
25946212Saw148015 	} else {
25956212Saw148015 		if (flowoplib_iobufsetup(threadflow, flowop, &iobuf,
25966212Saw148015 		    iosize) != 0)
25976212Saw148015 			return (FILEBENCH_ERROR);
25986212Saw148015 	}
25995184Sek110237 
26005184Sek110237 	file = threadflow->tf_fse[srcfd];
26015673Saw148015 	if ((srcfd != 0) && (file == NULL)) {
26025673Saw148015 		filebench_log(LOG_ERROR, "flowop %s: NULL src file",
26035184Sek110237 		    flowop->fo_name);
26046084Saw148015 		return (FILEBENCH_ERROR);
26055184Sek110237 	}
26065184Sek110237 
26075673Saw148015 	if (file)
26085673Saw148015 		wss = file->fse_size;
26095673Saw148015 
26105673Saw148015 	wsize = (int)MIN(wss, iosize);
26115184Sek110237 
26125184Sek110237 	/* Measure time to write bytes */
26135184Sek110237 	flowop_beginop(threadflow, flowop);
26145673Saw148015 	for (seek = 0; seek < wss; seek += wsize) {
26155673Saw148015 		ret = write(filedesc, iobuf, wsize);
26165184Sek110237 		if (ret != wsize) {
26175184Sek110237 			filebench_log(LOG_ERROR,
26185184Sek110237 			    "Failed to write %d bytes on fd %d: %s",
26195673Saw148015 			    wsize, filedesc, strerror(errno));
26205673Saw148015 			flowop_endop(threadflow, flowop, 0);
26216084Saw148015 			return (FILEBENCH_ERROR);
26225184Sek110237 		}
26235673Saw148015 		wsize = (int)MIN(wss - seek, iosize);
26245184Sek110237 		bytes += ret;
26255184Sek110237 	}
26265673Saw148015 	flowop_endop(threadflow, flowop, bytes);
26275184Sek110237 
26286084Saw148015 	return (FILEBENCH_OK);
26295184Sek110237 }
26305184Sek110237 
26315184Sek110237 
26325184Sek110237 /*
26335184Sek110237  * Emulate a fixed size append to a file. Will append data to
26345184Sek110237  * a file chosen from a fileset if the flowop's fo_fileset
26355184Sek110237  * field specifies one or if its fdnumber is non zero.
26365184Sek110237  * Otherwise it will write to a fileobj file, if one exists.
26375184Sek110237  * The flowop's fo_wss parameter will be used to set the
26385184Sek110237  * maximum file size if it is non-zero, otherwise the
26395184Sek110237  * filesetentry's fse_size will be used. A random memory
26405184Sek110237  * buffer offset is calculated, then a logical seek to the
26415184Sek110237  * end of file is done followed by a write of fo_iosize
26425184Sek110237  * bytes. Writes are actually done from fo_buf, rather than
26435184Sek110237  * tf_mem as is done with flowoplib_write(), and no check
26445184Sek110237  * is made to see if fo_iosize exceeds the size of fo_buf.
26456084Saw148015  * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of
26466084Saw148015  * files in the fileset, FILEBENCH_OK on success.
26475184Sek110237  */
26485184Sek110237 static int
26495184Sek110237 flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop)
26505184Sek110237 {
26515673Saw148015 	caddr_t iobuf;
26525673Saw148015 	int filedesc;
26536212Saw148015 	fbint_t wss;
26546212Saw148015 	fbint_t iosize;
26555184Sek110237 	int ret;
26565184Sek110237 
26576212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
26586084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
26596084Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
26606084Saw148015 		return (ret);
26615184Sek110237 
26625184Sek110237 	/* XXX wss is not being used */
26635184Sek110237 
26645184Sek110237 	/* Measure time to write bytes */
26655184Sek110237 	flowop_beginop(threadflow, flowop);
26665184Sek110237 	(void) lseek64(filedesc, 0, SEEK_END);
26675673Saw148015 	ret = write(filedesc, iobuf, iosize);
26685673Saw148015 	if (ret != iosize) {
26695184Sek110237 		filebench_log(LOG_ERROR,
26706286Saw148015 		    "Failed to write %llu bytes on fd %d: %s",
26716286Saw148015 		    (u_longlong_t)iosize, filedesc, strerror(errno));
26726212Saw148015 		flowop_endop(threadflow, flowop, ret);
26736084Saw148015 		return (FILEBENCH_ERROR);
26745184Sek110237 	}
26756212Saw148015 	flowop_endop(threadflow, flowop, ret);
26765184Sek110237 
26776084Saw148015 	return (FILEBENCH_OK);
26785184Sek110237 }
26795184Sek110237 
26805184Sek110237 /*
26815184Sek110237  * Emulate a random size append to a file. Will append data
26825184Sek110237  * to a file chosen from a fileset if the flowop's fo_fileset
26835184Sek110237  * field specifies one or if its fdnumber is non zero. Otherwise
26845184Sek110237  * it will write to a fileobj file, if one exists. The flowop's
26855184Sek110237  * fo_wss parameter will be used to set the maximum file size
26865184Sek110237  * if it is non-zero, otherwise the filesetentry's fse_size
26875184Sek110237  * will be used.  A random transfer size (but at most fo_iosize
26885184Sek110237  * bytes) and a random memory offset are calculated. A logical
26895184Sek110237  * seek to the end of file is done, then writes of up to
26905184Sek110237  * FILE_ALLOC_BLOCK in size are done until the full transfer
26915184Sek110237  * size has been written. Writes are actually done from fo_buf,
26925184Sek110237  * rather than tf_mem as is done with flowoplib_write().
26936084Saw148015  * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of
26946084Saw148015  * files in the fileset, FILEBENCH_OK on success.
26955184Sek110237  */
26965184Sek110237 static int
26975184Sek110237 flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop)
26985184Sek110237 {
26995673Saw148015 	caddr_t iobuf;
27005184Sek110237 	uint64_t appendsize;
27015673Saw148015 	int filedesc;
27026212Saw148015 	fbint_t wss;
27036212Saw148015 	fbint_t iosize;
27046212Saw148015 	int ret = 0;
27055184Sek110237 
27066212Saw148015 	if ((iosize = avd_get_int(flowop->fo_iosize)) == 0) {
27076212Saw148015 		filebench_log(LOG_ERROR, "zero iosize for flowop %s",
27086212Saw148015 		    flowop->fo_name);
27096212Saw148015 		return (FILEBENCH_ERROR);
27106212Saw148015 	}
27116212Saw148015 
27126212Saw148015 	if (filebench_randomno64(&appendsize, iosize, 1LL, NULL) != 0)
27136084Saw148015 		return (FILEBENCH_ERROR);
27145184Sek110237 
27155673Saw148015 	/* skip if attempting zero length append */
27165673Saw148015 	if (appendsize == 0) {
27175673Saw148015 		flowop_beginop(threadflow, flowop);
27185673Saw148015 		flowop_endop(threadflow, flowop, 0LL);
27196084Saw148015 		return (FILEBENCH_OK);
27205673Saw148015 	}
27215184Sek110237 
27226084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
27236084Saw148015 	    &filedesc, appendsize)) != FILEBENCH_OK)
27246084Saw148015 		return (ret);
27255673Saw148015 
27265184Sek110237 	/* XXX wss is not being used */
27275184Sek110237 
27285673Saw148015 	/* Measure time to write bytes */
27295673Saw148015 	flowop_beginop(threadflow, flowop);
27305673Saw148015 
27315673Saw148015 	(void) lseek64(filedesc, 0, SEEK_END);
27325673Saw148015 	ret = write(filedesc, iobuf, appendsize);
27335673Saw148015 	if (ret != appendsize) {
27345673Saw148015 		filebench_log(LOG_ERROR,
27356286Saw148015 		    "Failed to write %llu bytes on fd %d: %s",
27366286Saw148015 		    (u_longlong_t)appendsize, filedesc, strerror(errno));
27375673Saw148015 		flowop_endop(threadflow, flowop, 0);
27386084Saw148015 		return (FILEBENCH_ERROR);
27395184Sek110237 	}
27405184Sek110237 
27415673Saw148015 	flowop_endop(threadflow, flowop, appendsize);
27425184Sek110237 
27436084Saw148015 	return (FILEBENCH_OK);
27445184Sek110237 }
27455184Sek110237 
27466212Saw148015 typedef struct testrandvar_priv {
27476212Saw148015 	uint64_t sample_count;
27486212Saw148015 	double val_sum;
27496212Saw148015 	double sqr_sum;
27506212Saw148015 } testrandvar_priv_t;
27516212Saw148015 
27526212Saw148015 /*
27536212Saw148015  * flowop to calculate various statistics from the number stream
27546212Saw148015  * produced by a random variable. This allows verification that the
27556212Saw148015  * random distribution used to define the random variable is producing
27566212Saw148015  * the expected distribution of random numbers.
27576212Saw148015  */
27586212Saw148015 /* ARGSUSED */
27596212Saw148015 static int
27606212Saw148015 flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop)
27616212Saw148015 {
27626212Saw148015 	testrandvar_priv_t	*mystats;
27636212Saw148015 	double			value;
27646212Saw148015 
27656212Saw148015 	if ((mystats = (testrandvar_priv_t *)flowop->fo_private) == NULL) {
27666212Saw148015 		filebench_log(LOG_ERROR, "testrandvar not initialized\n");
27676212Saw148015 		filebench_shutdown(1);
27686212Saw148015 		return (-1);
27696212Saw148015 	}
27706212Saw148015 
27716212Saw148015 	value = avd_get_dbl(flowop->fo_value);
27726212Saw148015 
27736212Saw148015 	mystats->sample_count++;
27746212Saw148015 	mystats->val_sum += value;
27756212Saw148015 	mystats->sqr_sum += (value * value);
27766212Saw148015 
27776212Saw148015 	return (0);
27786212Saw148015 }
27796212Saw148015 
27806212Saw148015 /*
27816212Saw148015  * Initialize the private data area used to accumulate the statistics
27826212Saw148015  */
27836212Saw148015 static int
27846212Saw148015 flowoplib_testrandvar_init(flowop_t *flowop)
27856212Saw148015 {
27866212Saw148015 	testrandvar_priv_t	*mystats;
27876212Saw148015 
27886212Saw148015 	if ((mystats = (testrandvar_priv_t *)
27896212Saw148015 	    malloc(sizeof (testrandvar_priv_t))) == NULL) {
27906212Saw148015 		filebench_log(LOG_ERROR, "could not initialize testrandvar");
27916212Saw148015 		filebench_shutdown(1);
27926212Saw148015 		return (-1);
27936212Saw148015 	}
27946212Saw148015 
27956212Saw148015 	mystats->sample_count = 0;
27966212Saw148015 	mystats->val_sum = 0;
27976212Saw148015 	mystats->sqr_sum = 0;
27986212Saw148015 	flowop->fo_private = (void *)mystats;
27996212Saw148015 
28006212Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
28016212Saw148015 	return (0);
28026212Saw148015 }
28036212Saw148015 
28046212Saw148015 /*
28056212Saw148015  * Print out the accumulated statistics, and free the private storage
28066212Saw148015  */
28076212Saw148015 static void
28086212Saw148015 flowoplib_testrandvar_destruct(flowop_t *flowop)
28096212Saw148015 {
28106212Saw148015 	testrandvar_priv_t	*mystats;
28116212Saw148015 	double mean, std_dev, dbl_count;
28126212Saw148015 
28136212Saw148015 	(void) ipc_mutex_lock(&flowop->fo_lock);
28146212Saw148015 	if ((mystats = (testrandvar_priv_t *)
28156212Saw148015 	    flowop->fo_private) == NULL) {
28166212Saw148015 		(void) ipc_mutex_unlock(&flowop->fo_lock);
28176212Saw148015 		return;
28186212Saw148015 	}
28196212Saw148015 
28206212Saw148015 	flowop->fo_private = NULL;
28216212Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
28226212Saw148015 
28236212Saw148015 	dbl_count = (double)mystats->sample_count;
28246212Saw148015 	mean = mystats->val_sum / dbl_count;
28256212Saw148015 	std_dev = sqrt((mystats->sqr_sum / dbl_count) - (mean * mean)) / mean;
28266212Saw148015 
28276212Saw148015 	filebench_log(LOG_VERBOSE,
28286286Saw148015 	    "testrandvar: ops = %llu, mean = %8.2lf, stddev = %8.2lf",
28296286Saw148015 	    (u_longlong_t)mystats->sample_count, mean, std_dev);
28306212Saw148015 	free(mystats);
28316212Saw148015 }
28325184Sek110237 
28335184Sek110237 /*
28347556SAndrew.W.Wilson@sun.com  * prints message to the console from within a thread
28357556SAndrew.W.Wilson@sun.com  */
28367556SAndrew.W.Wilson@sun.com static int
28377556SAndrew.W.Wilson@sun.com flowoplib_print(threadflow_t *threadflow, flowop_t *flowop)
28387556SAndrew.W.Wilson@sun.com {
28397556SAndrew.W.Wilson@sun.com 	procflow_t *procflow;
28407556SAndrew.W.Wilson@sun.com 
28417556SAndrew.W.Wilson@sun.com 	procflow = threadflow->tf_process;
28427556SAndrew.W.Wilson@sun.com 	filebench_log(LOG_INFO,
28437556SAndrew.W.Wilson@sun.com 	    "Message from process (%s,%d), thread (%s,%d): %s",
28447556SAndrew.W.Wilson@sun.com 	    procflow->pf_name, procflow->pf_instance,
28457556SAndrew.W.Wilson@sun.com 	    threadflow->tf_name, threadflow->tf_instance,
28467556SAndrew.W.Wilson@sun.com 	    avd_get_str(flowop->fo_value));
28477556SAndrew.W.Wilson@sun.com 
28487556SAndrew.W.Wilson@sun.com 	return (FILEBENCH_OK);
28497556SAndrew.W.Wilson@sun.com }
28507556SAndrew.W.Wilson@sun.com 
28517556SAndrew.W.Wilson@sun.com /*
28525184Sek110237  * Prints usage information for flowop operations.
28535184Sek110237  */
28545184Sek110237 void
28555184Sek110237 flowoplib_usage()
28565184Sek110237 {
28575184Sek110237 	(void) fprintf(stderr,
28585184Sek110237 	    "flowop [openfile|createfile] name=<name>,fileset=<fname>\n");
28595184Sek110237 	(void) fprintf(stderr,
28605184Sek110237 	    "                       [,fd=<file desc num>]\n");
28615184Sek110237 	(void) fprintf(stderr, "\n");
28625184Sek110237 	(void) fprintf(stderr,
28635184Sek110237 	    "flowop closefile name=<name>,fd=<file desc num>]\n");
28645184Sek110237 	(void) fprintf(stderr, "\n");
28655184Sek110237 	(void) fprintf(stderr, "flowop deletefile name=<name>\n");
28665184Sek110237 	(void) fprintf(stderr, "                       [,fileset=<fname>]\n");
28675184Sek110237 	(void) fprintf(stderr,
28685184Sek110237 	    "                       [,fd=<file desc num>]\n");
28695184Sek110237 	(void) fprintf(stderr, "\n");
28705184Sek110237 	(void) fprintf(stderr, "flowop statfile name=<name>\n");
28715184Sek110237 	(void) fprintf(stderr, "                       [,fileset=<fname>]\n");
28725184Sek110237 	(void) fprintf(stderr,
28735184Sek110237 	    "                       [,fd=<file desc num>]\n");
28745184Sek110237 	(void) fprintf(stderr, "\n");
28755184Sek110237 	(void) fprintf(stderr,
28765184Sek110237 	    "flowop fsync name=<name>,fd=<file desc num>]\n");
28775184Sek110237 	(void) fprintf(stderr, "\n");
28785184Sek110237 	(void) fprintf(stderr,
28795184Sek110237 	    "flowop fsyncset name=<name>,fileset=<fname>]\n");
28805184Sek110237 	(void) fprintf(stderr, "\n");
28815184Sek110237 	(void) fprintf(stderr, "flowop [write|read|aiowrite] name=<name>, \n");
28825184Sek110237 	(void) fprintf(stderr,
28835184Sek110237 	    "                       filename|fileset=<fname>,\n");
28845184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
28855184Sek110237 	(void) fprintf(stderr, "                       [,directio]\n");
28865184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
28875184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
28885184Sek110237 	(void) fprintf(stderr, "                       [,random]\n");
28895184Sek110237 	(void) fprintf(stderr, "                       [,opennext]\n");
28905184Sek110237 	(void) fprintf(stderr, "                       [,workingset=<size>]\n");
28915184Sek110237 	(void) fprintf(stderr,
28925184Sek110237 	    "flowop [appendfile|appendfilerand] name=<name>, \n");
28935184Sek110237 	(void) fprintf(stderr,
28945184Sek110237 	    "                       filename|fileset=<fname>,\n");
28955184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
28965184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
28975184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
28985184Sek110237 	(void) fprintf(stderr, "                       [,workingset=<size>]\n");
28995184Sek110237 	(void) fprintf(stderr,
29005184Sek110237 	    "flowop [readwholefile|writewholefile] name=<name>, \n");
29015184Sek110237 	(void) fprintf(stderr,
29025184Sek110237 	    "                       filename|fileset=<fname>,\n");
29035184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
29045184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
29055184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
29065184Sek110237 	(void) fprintf(stderr, "\n");
29075184Sek110237 	(void) fprintf(stderr, "flowop aiowait name=<name>,target="
29085184Sek110237 	    "<aiowrite-flowop>\n");
29095184Sek110237 	(void) fprintf(stderr, "\n");
29105184Sek110237 	(void) fprintf(stderr, "flowop sempost name=<name>,"
29115184Sek110237 	    "target=<semblock-flowop>,\n");
29125184Sek110237 	(void) fprintf(stderr,
29135184Sek110237 	    "                       value=<increment-to-post>\n");
29145184Sek110237 	(void) fprintf(stderr, "\n");
29155184Sek110237 	(void) fprintf(stderr, "flowop semblock name=<name>,value="
29165184Sek110237 	    "<decrement-to-receive>,\n");
29175184Sek110237 	(void) fprintf(stderr, "                       highwater="
29185184Sek110237 	    "<inbound-queue-max>\n");
29195184Sek110237 	(void) fprintf(stderr, "\n");
29205184Sek110237 	(void) fprintf(stderr, "flowop block name=<name>\n");
29215184Sek110237 	(void) fprintf(stderr, "\n");
29225184Sek110237 	(void) fprintf(stderr,
29235184Sek110237 	    "flowop wakeup name=<name>,target=<block-flowop>,\n");
29245184Sek110237 	(void) fprintf(stderr, "\n");
29255184Sek110237 	(void) fprintf(stderr,
29265184Sek110237 	    "flowop hog name=<name>,value=<number-of-mem-ops>\n");
29275184Sek110237 	(void) fprintf(stderr,
29285184Sek110237 	    "flowop delay name=<name>,value=<number-of-seconds>\n");
29295184Sek110237 	(void) fprintf(stderr, "\n");
29305184Sek110237 	(void) fprintf(stderr, "flowop eventlimit name=<name>\n");
29315184Sek110237 	(void) fprintf(stderr, "flowop bwlimit name=<name>,value=<mb/s>\n");
29325184Sek110237 	(void) fprintf(stderr, "flowop iopslimit name=<name>,value=<iop/s>\n");
29335184Sek110237 	(void) fprintf(stderr,
29345184Sek110237 	    "flowop finishoncount name=<name>,value=<ops/s>\n");
29355184Sek110237 	(void) fprintf(stderr,
29365184Sek110237 	    "flowop finishonbytes name=<name>,value=<bytes>\n");
29375184Sek110237 	(void) fprintf(stderr, "\n");
29385184Sek110237 	(void) fprintf(stderr, "\n");
29395184Sek110237 }
2940