xref: /onnv-gate/usr/src/cmd/filebench/common/flowop_library.c (revision 6084:d5f45b4dae7e)
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 /*
22*6084Saw148015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235184Sek110237  * Use is subject to license terms.
245184Sek110237  */
255184Sek110237 
265184Sek110237 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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>
405184Sek110237 
415184Sek110237 #ifdef HAVE_UTILITY_H
425184Sek110237 #include <utility.h>
435184Sek110237 #endif /* HAVE_UTILITY_H */
445184Sek110237 
455184Sek110237 #ifdef HAVE_AIO
465184Sek110237 #include <aio.h>
475184Sek110237 #endif /* HAVE_AIO */
485184Sek110237 
495184Sek110237 #ifdef HAVE_LIBAIO_H
505184Sek110237 #include <libaio.h>
515184Sek110237 #endif /* HAVE_LIBAIO_H */
525184Sek110237 
535184Sek110237 #ifdef HAVE_SYS_ASYNC_H
545184Sek110237 #include <sys/asynch.h>
555184Sek110237 #endif /* HAVE_SYS_ASYNC_H */
565184Sek110237 
575184Sek110237 #ifdef HAVE_AIO_H
585184Sek110237 #include <aio.h>
595184Sek110237 #endif /* HAVE_AIO_H */
605184Sek110237 
615184Sek110237 #ifndef HAVE_UINT_T
625184Sek110237 #define	uint_t unsigned int
635184Sek110237 #endif /* HAVE_UINT_T */
645184Sek110237 
655184Sek110237 #ifndef HAVE_AIOCB64_T
665184Sek110237 #define	aiocb64 aiocb
675184Sek110237 #endif /* HAVE_AIOCB64_T */
685184Sek110237 
695184Sek110237 #ifndef HAVE_SYSV_SEM
705184Sek110237 #include <semaphore.h>
715184Sek110237 #endif /* HAVE_SYSV_SEM */
725184Sek110237 
735184Sek110237 #include "filebench.h"
745184Sek110237 #include "flowop.h"
755184Sek110237 #include "fileset.h"
765184Sek110237 
775184Sek110237 /*
785184Sek110237  * These routines implement the flowops from the f language. Each
795184Sek110237  * flowop has has a name such as "read", and a set of function pointers
805184Sek110237  * to call for initialization, execution and destruction of the flowop.
815184Sek110237  * The table flowoplib_funcs[] contains a flowoplib struct for each
825184Sek110237  * implemented flowop. Most flowops use a generic initialization function
835184Sek110237  * and all currently use a generic destruction function. All flowop
845184Sek110237  * functions referenced from the table are in this file, though, of
855184Sek110237  * course, they often call functions from other files.
865184Sek110237  *
875184Sek110237  * The flowop_init() routine uses the flowoplib_funcs[] table to
885184Sek110237  * create an initial set of "instance 0" flowops, one for each type of
895184Sek110237  * flowop, from which all other flowops are derived. These "instance 0"
905184Sek110237  * flowops are initialized with information from the table including
915184Sek110237  * pointers for their fo_init, fo_func and fo_destroy functions. When
925184Sek110237  * a flowop definition is encountered in an f language script, the
935184Sek110237  * "type" of flowop, such as "read" is used to search for the
945184Sek110237  * "instance 0" flowop named "read", then a new flowop is allocated
955184Sek110237  * which inherits its function pointers and other initial properties
965184Sek110237  * from the instance 0 flowop, and is given a new name as specified
975184Sek110237  * by the "name=" attribute.
985184Sek110237  */
995184Sek110237 
1005184Sek110237 static int flowoplib_init_generic(flowop_t *flowop);
1015184Sek110237 static void flowoplib_destruct_generic(flowop_t *flowop);
102*6084Saw148015 static void flowoplib_destruct_noop(flowop_t *flowop);
1035184Sek110237 static int flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop);
1045184Sek110237 static int flowoplib_write(threadflow_t *threadflow, flowop_t *flowop);
1055184Sek110237 #ifdef HAVE_AIO
1065184Sek110237 static int flowoplib_aiowrite(threadflow_t *threadflow, flowop_t *flowop);
1075184Sek110237 static int flowoplib_aiowait(threadflow_t *threadflow, flowop_t *flowop);
1085184Sek110237 #endif
1095184Sek110237 static int flowoplib_read(threadflow_t *threadflow, flowop_t *flowop);
1105184Sek110237 static int flowoplib_block_init(flowop_t *flowop);
1115184Sek110237 static int flowoplib_block(threadflow_t *threadflow, flowop_t *flowop);
1125184Sek110237 static int flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop);
1135184Sek110237 static int flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop);
1145184Sek110237 static int flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop);
1155184Sek110237 static int flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop);
1165184Sek110237 static int flowoplib_sempost_init(flowop_t *flowop);
1175184Sek110237 static int flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop);
1185184Sek110237 static int flowoplib_semblock_init(flowop_t *flowop);
1195184Sek110237 static void flowoplib_semblock_destruct(flowop_t *flowop);
1205184Sek110237 static int flowoplib_eventlimit(threadflow_t *, flowop_t *flowop);
1215184Sek110237 static int flowoplib_bwlimit(threadflow_t *, flowop_t *flowop);
1225184Sek110237 static int flowoplib_iopslimit(threadflow_t *, flowop_t *flowop);
1235184Sek110237 static int flowoplib_opslimit(threadflow_t *, flowop_t *flowop);
1245184Sek110237 static int flowoplib_openfile(threadflow_t *, flowop_t *flowop);
1255184Sek110237 static int flowoplib_openfile_common(threadflow_t *, flowop_t *flowop, int fd);
1265184Sek110237 static int flowoplib_createfile(threadflow_t *, flowop_t *flowop);
1275184Sek110237 static int flowoplib_closefile(threadflow_t *, flowop_t *flowop);
1285184Sek110237 static int flowoplib_fsync(threadflow_t *, flowop_t *flowop);
1295184Sek110237 static int flowoplib_readwholefile(threadflow_t *, flowop_t *flowop);
1305184Sek110237 static int flowoplib_writewholefile(threadflow_t *, flowop_t *flowop);
1315184Sek110237 static int flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop);
1325184Sek110237 static int flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop);
1335184Sek110237 static int flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop);
1345184Sek110237 static int flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop);
1355184Sek110237 static int flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop);
1365184Sek110237 static int flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop);
1375184Sek110237 static int flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop);
1385184Sek110237 
1395184Sek110237 typedef struct flowoplib {
1405184Sek110237 	int	fl_type;
1415184Sek110237 	int	fl_attrs;
1425184Sek110237 	char	*fl_name;
1435184Sek110237 	int	(*fl_init)();
1445184Sek110237 	int	(*fl_func)();
1455184Sek110237 	void	(*fl_destruct)();
1465184Sek110237 } flowoplib_t;
1475184Sek110237 
1485184Sek110237 static flowoplib_t flowoplib_funcs[] = {
1495184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "write", flowoplib_init_generic,
1505184Sek110237 	flowoplib_write, flowoplib_destruct_generic,
1515184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_READ, "read", flowoplib_init_generic,
1525184Sek110237 	flowoplib_read, flowoplib_destruct_generic,
1535184Sek110237 #ifdef HAVE_AIO
1545184Sek110237 	FLOW_TYPE_AIO, FLOW_ATTR_WRITE, "aiowrite", flowoplib_init_generic,
1555184Sek110237 	flowoplib_aiowrite, flowoplib_destruct_generic,
1565184Sek110237 	FLOW_TYPE_AIO, 0, "aiowait", flowoplib_init_generic,
1575184Sek110237 	flowoplib_aiowait, flowoplib_destruct_generic,
1585184Sek110237 #endif
1595184Sek110237 	FLOW_TYPE_SYNC, 0, "block", flowoplib_block_init,
1605184Sek110237 	flowoplib_block, flowoplib_destruct_generic,
1615184Sek110237 	FLOW_TYPE_SYNC, 0, "wakeup", flowoplib_init_generic,
1625184Sek110237 	flowoplib_wakeup, flowoplib_destruct_generic,
1635184Sek110237 	FLOW_TYPE_SYNC, 0, "semblock", flowoplib_semblock_init,
1645184Sek110237 	flowoplib_semblock, flowoplib_semblock_destruct,
1655184Sek110237 	FLOW_TYPE_SYNC, 0, "sempost", flowoplib_sempost_init,
166*6084Saw148015 	flowoplib_sempost, flowoplib_destruct_noop,
1675184Sek110237 	FLOW_TYPE_OTHER, 0, "hog", flowoplib_init_generic,
1685184Sek110237 	flowoplib_hog, flowoplib_destruct_generic,
1695184Sek110237 	FLOW_TYPE_OTHER, 0, "delay", flowoplib_init_generic,
1705184Sek110237 	flowoplib_delay, flowoplib_destruct_generic,
1715184Sek110237 	FLOW_TYPE_OTHER, 0, "eventlimit", flowoplib_init_generic,
1725184Sek110237 	flowoplib_eventlimit, flowoplib_destruct_generic,
1735184Sek110237 	FLOW_TYPE_OTHER, 0, "bwlimit", flowoplib_init_generic,
1745184Sek110237 	flowoplib_bwlimit, flowoplib_destruct_generic,
1755184Sek110237 	FLOW_TYPE_OTHER, 0, "iopslimit", flowoplib_init_generic,
1765184Sek110237 	flowoplib_iopslimit, flowoplib_destruct_generic,
1775184Sek110237 	FLOW_TYPE_OTHER, 0, "opslimit", flowoplib_init_generic,
1785184Sek110237 	flowoplib_opslimit, flowoplib_destruct_generic,
1795184Sek110237 	FLOW_TYPE_OTHER, 0, "finishoncount", flowoplib_init_generic,
1805184Sek110237 	flowoplib_finishoncount, flowoplib_destruct_generic,
1815184Sek110237 	FLOW_TYPE_OTHER, 0, "finishonbytes", flowoplib_init_generic,
1825184Sek110237 	flowoplib_finishonbytes, flowoplib_destruct_generic,
1835184Sek110237 	FLOW_TYPE_IO, 0, "openfile", flowoplib_init_generic,
1845184Sek110237 	flowoplib_openfile, flowoplib_destruct_generic,
1855184Sek110237 	FLOW_TYPE_IO, 0, "createfile", flowoplib_init_generic,
1865184Sek110237 	flowoplib_createfile, flowoplib_destruct_generic,
1875184Sek110237 	FLOW_TYPE_IO, 0, "closefile", flowoplib_init_generic,
1885184Sek110237 	flowoplib_closefile, flowoplib_destruct_generic,
1895184Sek110237 	FLOW_TYPE_IO, 0, "fsync", flowoplib_init_generic,
1905184Sek110237 	flowoplib_fsync, flowoplib_destruct_generic,
1915184Sek110237 	FLOW_TYPE_IO, 0, "fsyncset", flowoplib_init_generic,
1925184Sek110237 	flowoplib_fsyncset, flowoplib_destruct_generic,
1935184Sek110237 	FLOW_TYPE_IO, 0, "statfile", flowoplib_init_generic,
1945184Sek110237 	flowoplib_statfile, flowoplib_destruct_generic,
1955184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_READ, "readwholefile", flowoplib_init_generic,
1965184Sek110237 	flowoplib_readwholefile, flowoplib_destruct_generic,
1975184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfile", flowoplib_init_generic,
1985184Sek110237 	flowoplib_appendfile, flowoplib_destruct_generic,
1995184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfilerand", flowoplib_init_generic,
2005184Sek110237 	flowoplib_appendfilerand, flowoplib_destruct_generic,
2015184Sek110237 	FLOW_TYPE_IO, 0, "deletefile", flowoplib_init_generic,
2025184Sek110237 	flowoplib_deletefile, flowoplib_destruct_generic,
2035184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "writewholefile", flowoplib_init_generic,
2045184Sek110237 	flowoplib_writewholefile, flowoplib_destruct_generic
2055184Sek110237 };
2065184Sek110237 
2075184Sek110237 /*
2085184Sek110237  * Loops through the master list of flowops defined in this
2095184Sek110237  * module, and creates and initializes a flowop for each one
2105184Sek110237  * by calling flowop_define. As a side effect of calling
2115184Sek110237  * flowop define, the created flowops are placed on the
2125184Sek110237  * master flowop list. All created flowops are set to
2135184Sek110237  * instance "0".
2145184Sek110237  */
2155184Sek110237 void
2165184Sek110237 flowoplib_init()
2175184Sek110237 {
2185184Sek110237 	int nops = sizeof (flowoplib_funcs) / sizeof (flowoplib_t);
2195184Sek110237 	int i;
2205184Sek110237 
2215184Sek110237 	for (i = 0; i < nops; i++) {
2225184Sek110237 		flowop_t *flowop;
2235184Sek110237 		flowoplib_t *fl;
2245184Sek110237 
2255184Sek110237 		fl = &flowoplib_funcs[i];
2265184Sek110237 
2275184Sek110237 		if ((flowop = flowop_define(NULL,
2285184Sek110237 		    fl->fl_name, NULL, 0, fl->fl_type)) == 0) {
2295184Sek110237 			filebench_log(LOG_ERROR,
2305184Sek110237 			    "failed to create flowop %s\n",
2315184Sek110237 			    fl->fl_name);
2325184Sek110237 			filebench_shutdown(1);
2335184Sek110237 		}
2345184Sek110237 
2355184Sek110237 		flowop->fo_func = fl->fl_func;
2365184Sek110237 		flowop->fo_init = fl->fl_init;
2375184Sek110237 		flowop->fo_destruct = fl->fl_destruct;
2385184Sek110237 		flowop->fo_attrs = fl->fl_attrs;
2395184Sek110237 	}
2405184Sek110237 }
2415184Sek110237 
2425184Sek110237 static int
2435184Sek110237 flowoplib_init_generic(flowop_t *flowop)
2445184Sek110237 {
2455184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
246*6084Saw148015 	return (FILEBENCH_OK);
2475184Sek110237 }
2485184Sek110237 
2495184Sek110237 static void
2505184Sek110237 flowoplib_destruct_generic(flowop_t *flowop)
2515184Sek110237 {
252*6084Saw148015 	char *buf;
253*6084Saw148015 
254*6084Saw148015 	/* release any local resources held by the flowop */
255*6084Saw148015 	(void) ipc_mutex_lock(&flowop->fo_lock);
256*6084Saw148015 	buf = flowop->fo_buf;
257*6084Saw148015 	flowop->fo_buf = NULL;
258*6084Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
259*6084Saw148015 
260*6084Saw148015 	if (buf)
261*6084Saw148015 		free(buf);
262*6084Saw148015 }
263*6084Saw148015 
264*6084Saw148015 /*
265*6084Saw148015  * Special total noop destruct
266*6084Saw148015  */
267*6084Saw148015 /* ARGSUSED */
268*6084Saw148015 static void
269*6084Saw148015 flowoplib_destruct_noop(flowop_t *flowop)
270*6084Saw148015 {
2715184Sek110237 }
2725184Sek110237 
2735184Sek110237 /*
2745184Sek110237  * Generates a file attribute from flags in the supplied flowop.
2755184Sek110237  * Sets FLOW_ATTR_DIRECTIO and/or FLOW_ATTR_DSYNC as needed.
2765184Sek110237  */
2775184Sek110237 static int
2785184Sek110237 flowoplib_fileattrs(flowop_t *flowop)
2795184Sek110237 {
2805184Sek110237 	int attrs = 0;
2815184Sek110237 
2825184Sek110237 	if (*flowop->fo_directio)
2835184Sek110237 		attrs |= FLOW_ATTR_DIRECTIO;
2845184Sek110237 
2855184Sek110237 	if (*flowop->fo_dsync)
2865184Sek110237 		attrs |= FLOW_ATTR_DSYNC;
2875184Sek110237 
2885184Sek110237 	return (attrs);
2895184Sek110237 }
2905184Sek110237 
2915184Sek110237 /*
2925184Sek110237  * Searches for a file descriptor. Tries the flowop's
2935184Sek110237  * fo_fdnumber first and returns with it if it has been
2945184Sek110237  * explicitly set (greater than 0). It next checks to
2955184Sek110237  * see if a rotating file descriptor policy is in effect,
2965184Sek110237  * and if not returns the fdnumber regardless of what
2975184Sek110237  * it is. (note that if it is 0, it just selects to the
2985184Sek110237  * default file descriptor in the threadflow's tf_fd
2995184Sek110237  * array). If the rotating fd policy is in effect, it
3005184Sek110237  * cycles from the end of the tf_fd array to one location
3015184Sek110237  * beyond the maximum needed by the number of entries in
3025184Sek110237  * the associated fileset on each invocation, then starts
3035184Sek110237  * over from the end.
3045184Sek110237  *
3055184Sek110237  * The routine returns an index into the threadflow's
3065184Sek110237  * tf_fd table where the actual file descriptor will be
3075184Sek110237  * found. Note: the calling routine must not call this
3085184Sek110237  * routine if the flowop does not have a fileset, and the
3095184Sek110237  * flowop's fo_fdnumber is zero and fo_rotatefd is
3105184Sek110237  * asserted, or an addressing fault may occur.
3115184Sek110237  */
3125673Saw148015 static int
3135184Sek110237 flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop)
3145184Sek110237 {
3155184Sek110237 	/* If the script sets the fd explicitly */
3165184Sek110237 	if (flowop->fo_fdnumber > 0)
3175184Sek110237 		return (flowop->fo_fdnumber);
3185184Sek110237 
3195184Sek110237 	/* If the flowop defaults to persistent fd */
3205184Sek110237 	if (!integer_isset(flowop->fo_rotatefd))
3215184Sek110237 		return (flowop->fo_fdnumber);
3225184Sek110237 
3235184Sek110237 	/* Rotate the fd on each flowop invocation */
3245184Sek110237 	if (*(flowop->fo_fileset->fs_entries) > (THREADFLOW_MAXFD / 2)) {
3255184Sek110237 		filebench_log(LOG_ERROR, "Out of file descriptors in flowop %s"
3265184Sek110237 		    " (too many files : %d", flowop->fo_name,
3275184Sek110237 		    *(flowop->fo_fileset->fs_entries));
328*6084Saw148015 		return (FILEBENCH_ERROR);
3295184Sek110237 	}
3305184Sek110237 
3315184Sek110237 	/* First time around */
3325184Sek110237 	if (threadflow->tf_fdrotor == 0)
3335184Sek110237 		threadflow->tf_fdrotor = THREADFLOW_MAXFD;
3345184Sek110237 
3355184Sek110237 	/* One fd for every file in the set */
3365184Sek110237 	if (*(flowop->fo_fileset->fs_entries) ==
3375184Sek110237 	    (THREADFLOW_MAXFD - threadflow->tf_fdrotor))
3385184Sek110237 		threadflow->tf_fdrotor = THREADFLOW_MAXFD;
3395184Sek110237 
3405184Sek110237 
3415184Sek110237 	threadflow->tf_fdrotor--;
3425184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "selected fd = %d",
3435184Sek110237 	    threadflow->tf_fdrotor);
3445184Sek110237 	return (threadflow->tf_fdrotor);
3455184Sek110237 }
3465184Sek110237 
3475184Sek110237 /*
3485673Saw148015  * Determines the file descriptor to use, and attempts to open
3495673Saw148015  * the file if it is not already open. Also determines the wss
350*6084Saw148015  * value. Returns FILEBENCH_ERROR on errors, FILESET_NORSC if
351*6084Saw148015  * if flowop_openfile_common couldn't obtain an appropriate file
352*6084Saw148015  * from a the fileset, and FILEBENCH_OK otherwise.
3535673Saw148015  */
3545673Saw148015 static int
3555673Saw148015 flowoplib_filesetup(threadflow_t *threadflow, flowop_t *flowop,
3565673Saw148015     vinteger_t *wssp, int *filedescp)
3575673Saw148015 {
3585673Saw148015 	int fd = flowoplib_fdnum(threadflow, flowop);
3595673Saw148015 
3605673Saw148015 	if (fd == -1)
361*6084Saw148015 		return (FILEBENCH_ERROR);
3625673Saw148015 
3635673Saw148015 	if (threadflow->tf_fd[fd] == 0) {
364*6084Saw148015 		int ret;
365*6084Saw148015 
366*6084Saw148015 		if ((ret = flowoplib_openfile_common(
367*6084Saw148015 		    threadflow, flowop, fd)) != FILEBENCH_OK)
368*6084Saw148015 			return (ret);
3695673Saw148015 
3705673Saw148015 		if (threadflow->tf_fse[fd]) {
3715673Saw148015 			filebench_log(LOG_DEBUG_IMPL, "opened file %s",
3725673Saw148015 			    threadflow->tf_fse[fd]->fse_path);
3735673Saw148015 		} else {
3745673Saw148015 			filebench_log(LOG_DEBUG_IMPL,
3755673Saw148015 			    "opened device %s/%s",
3765673Saw148015 			    flowop->fo_fileset->fs_path,
3775673Saw148015 			    flowop->fo_fileset->fs_name);
3785673Saw148015 		}
3795673Saw148015 	}
3805673Saw148015 
3815673Saw148015 	*filedescp = threadflow->tf_fd[fd];
3825673Saw148015 
3835673Saw148015 	if (*flowop->fo_wss == 0) {
3845673Saw148015 		if (threadflow->tf_fse[fd])
3855673Saw148015 			*wssp = threadflow->tf_fse[fd]->fse_size;
3865673Saw148015 		else
3875673Saw148015 			*wssp = *flowop->fo_fileset->fs_size;
3885673Saw148015 	} else {
3895673Saw148015 		*wssp = *flowop->fo_wss;
3905673Saw148015 	}
3915673Saw148015 
392*6084Saw148015 	return (FILEBENCH_OK);
3935673Saw148015 }
3945673Saw148015 
3955673Saw148015 /*
3965673Saw148015  * Determines the io buffer or random offset into tf_mem for
397*6084Saw148015  * the IO operation. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
3985673Saw148015  */
3995673Saw148015 static int
4005673Saw148015 flowoplib_iobufsetup(threadflow_t *threadflow, flowop_t *flowop,
4015673Saw148015     caddr_t *iobufp, vinteger_t iosize)
4025673Saw148015 {
4035673Saw148015 	long memsize;
4045673Saw148015 	size_t memoffset;
4055673Saw148015 
4065673Saw148015 	if (iosize == 0) {
4075673Saw148015 		filebench_log(LOG_ERROR, "zero iosize for thread %s",
4085673Saw148015 		    flowop->fo_name);
409*6084Saw148015 		return (FILEBENCH_ERROR);
4105673Saw148015 	}
4115673Saw148015 
4125673Saw148015 	if ((memsize = *threadflow->tf_memsize) != 0) {
4135673Saw148015 
4145673Saw148015 		/* use tf_mem for I/O with random offset */
4155673Saw148015 		if (filebench_randomno(&memoffset, memsize, iosize) == -1) {
4165673Saw148015 			filebench_log(LOG_ERROR,
4175673Saw148015 			    "tf_memsize smaller than IO size for thread %s",
4185673Saw148015 			    flowop->fo_name);
419*6084Saw148015 			return (FILEBENCH_ERROR);
4205673Saw148015 		}
4215673Saw148015 		*iobufp = threadflow->tf_mem + memoffset;
4225673Saw148015 
4235673Saw148015 	} else {
4245673Saw148015 		/* use private I/O buffer */
4255673Saw148015 		if ((flowop->fo_buf != NULL) &&
4265673Saw148015 		    (flowop->fo_buf_size < iosize)) {
4275673Saw148015 			free(flowop->fo_buf);
4285673Saw148015 			flowop->fo_buf = NULL;
4295673Saw148015 		}
4305673Saw148015 		if ((flowop->fo_buf == NULL) && ((flowop->fo_buf
4315673Saw148015 		    = (char *)malloc(iosize)) == NULL))
432*6084Saw148015 			return (FILEBENCH_ERROR);
4335673Saw148015 
4345673Saw148015 		flowop->fo_buf_size = iosize;
4355673Saw148015 		*iobufp = flowop->fo_buf;
4365673Saw148015 	}
437*6084Saw148015 	return (FILEBENCH_OK);
4385673Saw148015 }
4395673Saw148015 
4405673Saw148015 /*
4415673Saw148015  * Determines the file descriptor to use, opens it if necessary, the
4425673Saw148015  * io buffer or random offset into tf_mem for IO operation and the wss
443*6084Saw148015  * value. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
4445673Saw148015  */
4455673Saw148015 static int
4465673Saw148015 flowoplib_iosetup(threadflow_t *threadflow, flowop_t *flowop,
4475673Saw148015     vinteger_t *wssp, caddr_t *iobufp, int *filedescp, vinteger_t iosize)
4485673Saw148015 {
449*6084Saw148015 	int ret;
450*6084Saw148015 
451*6084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, wssp, filedescp)) !=
452*6084Saw148015 	    FILEBENCH_OK)
453*6084Saw148015 		return (ret);
4545673Saw148015 
455*6084Saw148015 	if ((ret = flowoplib_iobufsetup(threadflow, flowop, iobufp, iosize)) !=
456*6084Saw148015 	    FILEBENCH_OK)
457*6084Saw148015 		return (ret);
4585673Saw148015 
459*6084Saw148015 	return (FILEBENCH_OK);
4605673Saw148015 }
4615673Saw148015 
4625673Saw148015 /*
4635184Sek110237  * Emulate posix read / pread. If the flowop has a fileset,
4645184Sek110237  * a file descriptor number index is fetched, otherwise a
4655184Sek110237  * supplied fileobj file is used. In either case the specified
4665184Sek110237  * file will be opened if not already open. If the flowop has
467*6084Saw148015  * neither a fileset or fileobj, an error is logged and FILEBENCH_ERROR
4685184Sek110237  * returned.
4695184Sek110237  *
4705184Sek110237  * The actual read is done to a random offset in the
4715184Sek110237  * threadflow's thread memory (tf_mem), with a size set by
4725184Sek110237  * fo_iosize and at either a random disk offset within the
4735184Sek110237  * working set size, or at the next sequential location. If
474*6084Saw148015  * any errors are encountered, FILEBENCH_ERROR is returned,
475*6084Saw148015  * if no appropriate file can be obtained from the fileset then
476*6084Saw148015  * FILEBENCH_NORSC is returned, otherise FILEBENCH_OK is returned.
4775184Sek110237  */
4785184Sek110237 static int
4795184Sek110237 flowoplib_read(threadflow_t *threadflow, flowop_t *flowop)
4805184Sek110237 {
4815673Saw148015 	caddr_t iobuf;
4825184Sek110237 	vinteger_t wss;
4835184Sek110237 	int filedesc;
4845184Sek110237 	int ret;
4855184Sek110237 
486*6084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
487*6084Saw148015 	    &filedesc, *flowop->fo_iosize)) != FILEBENCH_OK)
488*6084Saw148015 		return (ret);
4895184Sek110237 
4905184Sek110237 	if (*flowop->fo_random) {
4915184Sek110237 		uint64_t fileoffset;
4925184Sek110237 
4935184Sek110237 		if (filebench_randomno64(&fileoffset, wss,
4945184Sek110237 		    *flowop->fo_iosize) == -1) {
4955184Sek110237 			filebench_log(LOG_ERROR,
4965184Sek110237 			    "file size smaller than IO size for thread %s",
4975184Sek110237 			    flowop->fo_name);
498*6084Saw148015 			return (FILEBENCH_ERROR);
4995184Sek110237 		}
5005184Sek110237 
5015184Sek110237 		(void) flowop_beginop(threadflow, flowop);
5025673Saw148015 		if ((ret = pread64(filedesc, iobuf,
5035184Sek110237 		    *flowop->fo_iosize, (off64_t)fileoffset)) == -1) {
5045673Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5055184Sek110237 			filebench_log(LOG_ERROR,
5065184Sek110237 			    "read file %s failed, offset %lld "
5075673Saw148015 			    "io buffer %zd: %s",
5085673Saw148015 			    flowop->fo_fileset->fs_name,
5095673Saw148015 			    fileoffset, iobuf, strerror(errno));
5105673Saw148015 			flowop_endop(threadflow, flowop, 0);
511*6084Saw148015 			return (FILEBENCH_ERROR);
5125184Sek110237 		}
5135673Saw148015 		(void) flowop_endop(threadflow, flowop, ret);
5145184Sek110237 
5155184Sek110237 		if ((ret == 0))
5165184Sek110237 			(void) lseek64(filedesc, 0, SEEK_SET);
5175184Sek110237 
5185184Sek110237 	} else {
5195184Sek110237 		(void) flowop_beginop(threadflow, flowop);
5205673Saw148015 		if ((ret = read(filedesc, iobuf,
5215184Sek110237 		    *flowop->fo_iosize)) == -1) {
5225184Sek110237 			filebench_log(LOG_ERROR,
5235673Saw148015 			    "read file %s failed, io buffer %zd: %s",
5245673Saw148015 			    flowop->fo_fileset->fs_name,
5255673Saw148015 			    iobuf, strerror(errno));
5265673Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
527*6084Saw148015 			return (FILEBENCH_ERROR);
5285184Sek110237 		}
5295673Saw148015 		(void) flowop_endop(threadflow, flowop, ret);
5305184Sek110237 
5315184Sek110237 		if ((ret == 0))
5325184Sek110237 			(void) lseek64(filedesc, 0, SEEK_SET);
5335184Sek110237 	}
5345184Sek110237 
535*6084Saw148015 	return (FILEBENCH_OK);
5365184Sek110237 }
5375184Sek110237 
5385184Sek110237 #ifdef HAVE_AIO
5395184Sek110237 
5405184Sek110237 /*
5415184Sek110237  * Asynchronous write section. An Asynchronous IO element
5425184Sek110237  * (aiolist_t) is used to associate the asynchronous write request with
5435184Sek110237  * its subsequent completion. This element includes a aiocb64 struct
5445184Sek110237  * that is used by posix aio_xxx calls to track the asynchronous writes.
5455184Sek110237  * The flowops aiowrite and aiowait result in calls to these posix
5465184Sek110237  * aio_xxx system routines to do the actual asynchronous write IO
5475184Sek110237  * operations.
5485184Sek110237  */
5495184Sek110237 
5505184Sek110237 
5515184Sek110237 /*
5525184Sek110237  * Allocates an asynchronous I/O list (aio, of type
5535184Sek110237  * aiolist_t) element. Adds it to the flowop thread's
5545184Sek110237  * threadflow aio list. Returns a pointer to the element.
5555184Sek110237  */
5565184Sek110237 static aiolist_t *
5575184Sek110237 aio_allocate(flowop_t *flowop)
5585184Sek110237 {
5595184Sek110237 	aiolist_t *aiolist;
5605184Sek110237 
5615184Sek110237 	if ((aiolist = malloc(sizeof (aiolist_t))) == NULL) {
5625184Sek110237 		filebench_log(LOG_ERROR, "malloc aiolist failed");
5635184Sek110237 		filebench_shutdown(1);
5645184Sek110237 	}
5655184Sek110237 
5665184Sek110237 	/* Add to list */
5675184Sek110237 	if (flowop->fo_thread->tf_aiolist == NULL) {
5685184Sek110237 		flowop->fo_thread->tf_aiolist = aiolist;
5695184Sek110237 		aiolist->al_next = NULL;
5705184Sek110237 	} else {
5715184Sek110237 		aiolist->al_next = flowop->fo_thread->tf_aiolist;
5725184Sek110237 		flowop->fo_thread->tf_aiolist = aiolist;
5735184Sek110237 	}
5745184Sek110237 	return (aiolist);
5755184Sek110237 }
5765184Sek110237 
5775184Sek110237 /*
5785184Sek110237  * Searches for the aiolist element that has a matching
579*6084Saw148015  * completion block, aiocb. If none found returns FILEBENCH_ERROR. If
5805184Sek110237  * found, removes the aiolist element from flowop thread's
581*6084Saw148015  * list and returns FILEBENCH_OK.
5825184Sek110237  */
5835184Sek110237 static int
5845184Sek110237 aio_deallocate(flowop_t *flowop, struct aiocb64 *aiocb)
5855184Sek110237 {
5865184Sek110237 	aiolist_t *aiolist = flowop->fo_thread->tf_aiolist;
5875184Sek110237 	aiolist_t *previous = NULL;
5885184Sek110237 	aiolist_t *match = NULL;
5895184Sek110237 
5905184Sek110237 	if (aiocb == NULL) {
5915184Sek110237 		filebench_log(LOG_ERROR, "null aiocb deallocate");
592*6084Saw148015 		return (FILEBENCH_OK);
5935184Sek110237 	}
5945184Sek110237 
5955184Sek110237 	while (aiolist) {
5965184Sek110237 		if (aiocb == &(aiolist->al_aiocb)) {
5975184Sek110237 			match = aiolist;
5985184Sek110237 			break;
5995184Sek110237 		}
6005184Sek110237 		previous = aiolist;
6015184Sek110237 		aiolist = aiolist->al_next;
6025184Sek110237 	}
6035184Sek110237 
6045184Sek110237 	if (match == NULL)
605*6084Saw148015 		return (FILEBENCH_ERROR);
6065184Sek110237 
6075184Sek110237 	/* Remove from the list */
6085184Sek110237 	if (previous)
6095184Sek110237 		previous->al_next = match->al_next;
6105184Sek110237 	else
6115184Sek110237 		flowop->fo_thread->tf_aiolist = match->al_next;
6125184Sek110237 
613*6084Saw148015 	return (FILEBENCH_OK);
6145184Sek110237 }
6155184Sek110237 
6165184Sek110237 /*
6175184Sek110237  * Emulate posix aiowrite(). Determines which file to use,
6185184Sek110237  * either one file of a fileset, or the file associated
6195184Sek110237  * with a fileobj, allocates and fills an aiolist_t element
6205184Sek110237  * for the write, and issues the asynchronous write. This
6215184Sek110237  * operation is only valid for random IO, and returns an
622*6084Saw148015  * error if the flowop is set for sequential IO. Returns
623*6084Saw148015  * FILEBENCH_OK on success, FILEBENCH_NORSC if iosetup can't
624*6084Saw148015  * obtain a file to open, and FILEBENCH_ERROR on any
625*6084Saw148015  * encountered error.
6265184Sek110237  */
6275184Sek110237 static int
6285184Sek110237 flowoplib_aiowrite(threadflow_t *threadflow, flowop_t *flowop)
6295184Sek110237 {
6305673Saw148015 	caddr_t iobuf;
6315184Sek110237 	vinteger_t wss;
6325184Sek110237 	int filedesc;
633*6084Saw148015 	int ret;
6345184Sek110237 
635*6084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
636*6084Saw148015 	    &filedesc, *flowop->fo_iosize)) != FILEBENCH_OK)
637*6084Saw148015 		return (ret);
6385184Sek110237 
6395184Sek110237 	if (*flowop->fo_random) {
6405184Sek110237 		uint64_t fileoffset;
6415184Sek110237 		struct aiocb64 *aiocb;
6425184Sek110237 		aiolist_t *aiolist;
6435184Sek110237 
6445184Sek110237 		if (filebench_randomno64(&fileoffset,
6455184Sek110237 		    wss, *flowop->fo_iosize) == -1) {
6465184Sek110237 			filebench_log(LOG_ERROR,
6475184Sek110237 			    "file size smaller than IO size for thread %s",
6485184Sek110237 			    flowop->fo_name);
649*6084Saw148015 			return (FILEBENCH_ERROR);
6505184Sek110237 		}
6515184Sek110237 
6525184Sek110237 		aiolist = aio_allocate(flowop);
6535184Sek110237 		aiolist->al_type = AL_WRITE;
6545184Sek110237 		aiocb = &aiolist->al_aiocb;
6555184Sek110237 
6565184Sek110237 		aiocb->aio_fildes = filedesc;
6575673Saw148015 		aiocb->aio_buf = iobuf;
6585184Sek110237 		aiocb->aio_nbytes = *flowop->fo_iosize;
6595184Sek110237 		aiocb->aio_offset = (off64_t)fileoffset;
6605184Sek110237 		aiocb->aio_reqprio = 0;
6615184Sek110237 
6625184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
6635184Sek110237 		    "aio fd=%d, bytes=%lld, offset=%lld",
6645184Sek110237 		    filedesc, *flowop->fo_iosize, fileoffset);
6655184Sek110237 
6665184Sek110237 		flowop_beginop(threadflow, flowop);
6675184Sek110237 		if (aio_write64(aiocb) < 0) {
6685184Sek110237 			filebench_log(LOG_ERROR, "aiowrite failed: %s",
6695184Sek110237 			    strerror(errno));
6705184Sek110237 			filebench_shutdown(1);
6715184Sek110237 		}
6725673Saw148015 		flowop_endop(threadflow, flowop, *flowop->fo_iosize);
6735184Sek110237 	} else {
674*6084Saw148015 		return (FILEBENCH_ERROR);
6755184Sek110237 	}
6765184Sek110237 
677*6084Saw148015 	return (FILEBENCH_OK);
6785184Sek110237 }
6795184Sek110237 
6805184Sek110237 
6815184Sek110237 
6825184Sek110237 #define	MAXREAP 4096
6835184Sek110237 
6845184Sek110237 /*
6855184Sek110237  * Emulate posix aiowait(). Waits for the completion of half the
6865184Sek110237  * outstanding asynchronous IOs, or a single IO, which ever is
6875184Sek110237  * larger. The routine will return after a sufficient number of
6885184Sek110237  * completed calls issued by any thread in the procflow have
6895184Sek110237  * completed, or a 1 second timout elapses. All completed
6905184Sek110237  * IO operations are deleted from the thread's aiolist.
6915184Sek110237  */
6925184Sek110237 static int
6935184Sek110237 flowoplib_aiowait(threadflow_t *threadflow, flowop_t *flowop)
6945184Sek110237 {
6955184Sek110237 	struct aiocb64 **worklist;
6965184Sek110237 	aiolist_t *aio = flowop->fo_thread->tf_aiolist;
6975184Sek110237 	int uncompleted = 0;
6985184Sek110237 
6995184Sek110237 	worklist = calloc(MAXREAP, sizeof (struct aiocb64 *));
7005184Sek110237 
7015184Sek110237 	/* Count the list of pending aios */
7025184Sek110237 	while (aio) {
7035184Sek110237 		uncompleted++;
7045184Sek110237 		aio = aio->al_next;
7055184Sek110237 	}
7065184Sek110237 
7075184Sek110237 	do {
7085184Sek110237 		uint_t ncompleted = 0;
7095184Sek110237 		uint_t todo;
7105184Sek110237 		struct timespec timeout;
7115184Sek110237 		int inprogress;
7125184Sek110237 		int i;
7135184Sek110237 
7145184Sek110237 		/* Wait for half of the outstanding requests */
7155184Sek110237 		timeout.tv_sec = 1;
7165184Sek110237 		timeout.tv_nsec = 0;
7175184Sek110237 
7185184Sek110237 		if (uncompleted > MAXREAP)
7195184Sek110237 			todo = MAXREAP;
7205184Sek110237 		else
7215184Sek110237 			todo = uncompleted / 2;
7225184Sek110237 
7235184Sek110237 		if (todo == 0)
7245184Sek110237 			todo = 1;
7255184Sek110237 
7265184Sek110237 		flowop_beginop(threadflow, flowop);
7275184Sek110237 
7285184Sek110237 #ifdef HAVE_AIOWAITN
7295184Sek110237 		if ((aio_waitn64((struct aiocb64 **)worklist,
7305184Sek110237 		    MAXREAP, &todo, &timeout) == -1) &&
7315184Sek110237 		    errno && (errno != ETIME)) {
7325184Sek110237 			filebench_log(LOG_ERROR,
7335184Sek110237 			    "aiowait failed: %s, outstanding = %d, "
7345184Sek110237 			    "ncompleted = %d ",
7355184Sek110237 			    strerror(errno), uncompleted, todo);
7365184Sek110237 		}
7375184Sek110237 
7385184Sek110237 		ncompleted = todo;
7395184Sek110237 		/* Take the  completed I/Os from the list */
7405184Sek110237 		inprogress = 0;
7415184Sek110237 		for (i = 0; i < ncompleted; i++) {
7425184Sek110237 			if ((aio_return64(worklist[i]) == -1) &&
7435184Sek110237 			    (errno == EINPROGRESS)) {
7445184Sek110237 				inprogress++;
7455184Sek110237 				continue;
7465184Sek110237 			}
7475184Sek110237 			if (aio_deallocate(flowop, worklist[i]) < 0) {
7485184Sek110237 				filebench_log(LOG_ERROR, "Could not remove "
7495184Sek110237 				    "aio from list ");
7505673Saw148015 				flowop_endop(threadflow, flowop, 0);
751*6084Saw148015 				return (FILEBENCH_ERROR);
7525184Sek110237 			}
7535184Sek110237 		}
7545184Sek110237 
7555184Sek110237 		uncompleted -= ncompleted;
7565184Sek110237 		uncompleted += inprogress;
7575184Sek110237 
7585184Sek110237 #else
7595184Sek110237 
7605184Sek110237 		for (ncompleted = 0, inprogress = 0,
7615184Sek110237 		    aio = flowop->fo_thread->tf_aiolist;
7625184Sek110237 		    ncompleted < todo, aio != NULL; aio = aio->al_next) {
7635184Sek110237 
7645184Sek110237 			result = aio_error64(&aio->al_aiocb);
7655184Sek110237 
7665184Sek110237 			if (result == EINPROGRESS) {
7675184Sek110237 				inprogress++;
7685184Sek110237 				continue;
7695184Sek110237 			}
7705184Sek110237 
7715184Sek110237 			if ((aio_return64(&aio->al_aiocb) == -1) || result) {
7725184Sek110237 				filebench_log(LOG_ERROR, "aio failed: %s",
7735184Sek110237 				    strerror(result));
7745184Sek110237 				continue;
7755184Sek110237 			}
7765184Sek110237 
7775184Sek110237 			ncompleted++;
7785184Sek110237 
7795184Sek110237 			if (aio_deallocate(flowop, &aio->al_aiocb) < 0) {
7805184Sek110237 				filebench_log(LOG_ERROR, "Could not remove aio "
7815184Sek110237 				    "from list ");
7825673Saw148015 				flowop_endop(threadflow, flowop, 0);
783*6084Saw148015 				return (FILEBENCH_ERROR);
7845184Sek110237 			}
7855184Sek110237 		}
7865184Sek110237 
7875184Sek110237 		uncompleted -= ncompleted;
7885184Sek110237 
7895184Sek110237 #endif
7905184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT,
7915184Sek110237 		    "aio2 completed %d ios, uncompleted = %d, inprogress = %d",
7925184Sek110237 		    ncompleted, uncompleted, inprogress);
7935184Sek110237 
7945184Sek110237 	} while (uncompleted > MAXREAP);
7955184Sek110237 
7965673Saw148015 	flowop_endop(threadflow, flowop, 0);
7975184Sek110237 
7985184Sek110237 	free(worklist);
7995184Sek110237 
800*6084Saw148015 	return (FILEBENCH_OK);
8015184Sek110237 }
8025184Sek110237 
8035184Sek110237 #endif /* HAVE_AIO */
8045184Sek110237 
8055184Sek110237 /*
8065184Sek110237  * Initializes a "flowop_block" flowop. Specifically, it
8075184Sek110237  * initializes the flowop's fo_cv and unlocks the fo_lock.
8085184Sek110237  */
8095184Sek110237 static int
8105184Sek110237 flowoplib_block_init(flowop_t *flowop)
8115184Sek110237 {
8125184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d block init address %zx",
8135184Sek110237 	    flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
8145184Sek110237 	(void) pthread_cond_init(&flowop->fo_cv, ipc_condattr());
8155184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
8165184Sek110237 
817*6084Saw148015 	return (FILEBENCH_OK);
8185184Sek110237 }
8195184Sek110237 
8205184Sek110237 /*
8215184Sek110237  * Blocks the threadflow until woken up by flowoplib_wakeup.
8225184Sek110237  * The routine blocks on the flowop's fo_cv condition variable.
8235184Sek110237  */
8245184Sek110237 static int
8255184Sek110237 flowoplib_block(threadflow_t *threadflow, flowop_t *flowop)
8265184Sek110237 {
8275184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d blocking at address %zx",
8285184Sek110237 	    flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
8295184Sek110237 	(void) ipc_mutex_lock(&flowop->fo_lock);
8305184Sek110237 
8315184Sek110237 	flowop_beginop(threadflow, flowop);
8325184Sek110237 	(void) pthread_cond_wait(&flowop->fo_cv, &flowop->fo_lock);
8335673Saw148015 	flowop_endop(threadflow, flowop, 0);
8345184Sek110237 
8355184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
8365184Sek110237 	    flowop->fo_name, flowop->fo_instance);
8375184Sek110237 
8385184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
8395184Sek110237 
840*6084Saw148015 	return (FILEBENCH_OK);
8415184Sek110237 }
8425184Sek110237 
8435184Sek110237 /*
8445184Sek110237  * Wakes up one or more target blocking flowops.
8455184Sek110237  * Sends broadcasts on the fo_cv condition variables of all
8465184Sek110237  * flowops on the target list, except those that are
8475184Sek110237  * FLOW_MASTER flowops. The target list consists of all
8485184Sek110237  * flowops whose name matches this flowop's "fo_targetname"
8495184Sek110237  * attribute. The target list is generated on the first
8505184Sek110237  * invocation, and the run will be shutdown if no targets
851*6084Saw148015  * are found. Otherwise the routine always returns FILEBENCH_OK.
8525184Sek110237  */
8535184Sek110237 static int
8545184Sek110237 flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop)
8555184Sek110237 {
8565184Sek110237 	flowop_t *target;
8575184Sek110237 
8585184Sek110237 	/* if this is the first wakeup, create the wakeup list */
8595184Sek110237 	if (flowop->fo_targets == NULL) {
8605184Sek110237 		flowop_t *result = flowop_find(flowop->fo_targetname);
8615184Sek110237 
8625184Sek110237 		flowop->fo_targets = result;
8635184Sek110237 		if (result == NULL) {
8645184Sek110237 			filebench_log(LOG_ERROR,
8655184Sek110237 			    "wakeup: could not find op %s for thread %s",
8665184Sek110237 			    flowop->fo_targetname,
8675184Sek110237 			    threadflow->tf_name);
8685184Sek110237 			filebench_shutdown(1);
8695184Sek110237 		}
8705184Sek110237 		while (result) {
8715184Sek110237 			result->fo_targetnext =
8725184Sek110237 			    result->fo_resultnext;
8735184Sek110237 			result = result->fo_resultnext;
8745184Sek110237 		}
8755184Sek110237 	}
8765184Sek110237 
8775184Sek110237 	target = flowop->fo_targets;
8785184Sek110237 
8795184Sek110237 	/* wakeup the targets */
8805184Sek110237 	while (target) {
8815184Sek110237 		if (target->fo_instance == FLOW_MASTER) {
8825184Sek110237 			target = target->fo_targetnext;
8835184Sek110237 			continue;
8845184Sek110237 		}
8855184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
8865184Sek110237 		    "wakeup flow %s-%d at address %zx",
8875184Sek110237 		    target->fo_name,
8885184Sek110237 		    target->fo_instance,
8895184Sek110237 		    &target->fo_cv);
8905184Sek110237 
8915184Sek110237 		flowop_beginop(threadflow, flowop);
8925184Sek110237 		(void) ipc_mutex_lock(&target->fo_lock);
8935184Sek110237 		(void) pthread_cond_broadcast(&target->fo_cv);
8945184Sek110237 		(void) ipc_mutex_unlock(&target->fo_lock);
8955673Saw148015 		flowop_endop(threadflow, flowop, 0);
8965184Sek110237 
8975184Sek110237 		target = target->fo_targetnext;
8985184Sek110237 	}
8995184Sek110237 
900*6084Saw148015 	return (FILEBENCH_OK);
9015184Sek110237 }
9025184Sek110237 
9035184Sek110237 /*
9045184Sek110237  * "think time" routines. the "hog" routine consumes cpu cycles as
9055184Sek110237  * it "thinks", while the "delay" flowop simply calls sleep() to delay
9065184Sek110237  * for a given number of seconds without consuming cpu cycles.
9075184Sek110237  */
9085184Sek110237 
9095184Sek110237 
9105184Sek110237 /*
9115184Sek110237  * Consumes CPU cycles and memory bandwidth by looping for
9125184Sek110237  * flowop->fo_value times. With each loop sets memory location
9135184Sek110237  * threadflow->tf_mem to 1.
9145184Sek110237  */
9155184Sek110237 static int
9165184Sek110237 flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop)
9175184Sek110237 {
9185184Sek110237 	uint64_t value = *flowop->fo_value;
9195184Sek110237 	int i;
9205184Sek110237 
9215673Saw148015 	filebench_log(LOG_DEBUG_IMPL, "hog enter");
9225184Sek110237 	flowop_beginop(threadflow, flowop);
9235673Saw148015 	if (threadflow->tf_mem != NULL) {
9245673Saw148015 		for (i = 0; i < value; i++)
9255673Saw148015 			*(threadflow->tf_mem) = 1;
9265673Saw148015 	}
9275673Saw148015 	flowop_endop(threadflow, flowop, 0);
9285184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "hog exit");
929*6084Saw148015 	return (FILEBENCH_OK);
9305184Sek110237 }
9315184Sek110237 
9325184Sek110237 
9335184Sek110237 /*
9345184Sek110237  * Delays for fo_value seconds.
9355184Sek110237  */
9365184Sek110237 static int
9375184Sek110237 flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop)
9385184Sek110237 {
9395184Sek110237 	int value = *flowop->fo_value;
9405184Sek110237 
9415184Sek110237 	flowop_beginop(threadflow, flowop);
9425184Sek110237 	(void) sleep(value);
9435673Saw148015 	flowop_endop(threadflow, flowop, 0);
944*6084Saw148015 	return (FILEBENCH_OK);
9455184Sek110237 }
9465184Sek110237 
9475184Sek110237 /*
9485184Sek110237  * Rate limiting routines. This is the event consuming half of the
9495184Sek110237  * event system. Each of the four following routines will limit the rate
9505184Sek110237  * to one unit of either calls, issued I/O operations, issued filebench
9515184Sek110237  * operations, or I/O bandwidth. Since there is only one event generator,
9525184Sek110237  * the events will be divided amoung multiple instances of an event
9535184Sek110237  * consumer, and further divided among different consumers if more than
9545184Sek110237  * one has been defined. There is no mechanism to enforce equal sharing
9555184Sek110237  * of events.
9565184Sek110237  */
9575184Sek110237 
9585184Sek110237 /*
9595184Sek110237  * Completes one invocation per posted event. If eventgen_q
9605184Sek110237  * has an event count greater than zero, one will be removed
9615184Sek110237  * (count decremented), otherwise the calling thread will
9625184Sek110237  * block until another event has been posted. Always returns 0
9635184Sek110237  */
9645184Sek110237 static int
9655184Sek110237 flowoplib_eventlimit(threadflow_t *threadflow, flowop_t *flowop)
9665184Sek110237 {
9675184Sek110237 	/* Immediately bail if not set/enabled */
9685184Sek110237 	if (filebench_shm->eventgen_hz == 0)
969*6084Saw148015 		return (FILEBENCH_OK);
9705184Sek110237 
9715184Sek110237 	if (flowop->fo_initted == 0) {
9725184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
9735184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
9745184Sek110237 		flowop->fo_initted = 1;
9755184Sek110237 	}
9765184Sek110237 
9775184Sek110237 	flowop_beginop(threadflow, flowop);
9785184Sek110237 	while (filebench_shm->eventgen_hz) {
9795184Sek110237 		(void) ipc_mutex_lock(&filebench_shm->eventgen_lock);
9805184Sek110237 		if (filebench_shm->eventgen_q > 0) {
9815184Sek110237 			filebench_shm->eventgen_q--;
9825184Sek110237 			(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
9835184Sek110237 			break;
9845184Sek110237 		}
9855184Sek110237 		(void) pthread_cond_wait(&filebench_shm->eventgen_cv,
9865184Sek110237 		    &filebench_shm->eventgen_lock);
9875184Sek110237 		(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
9885184Sek110237 	}
9895673Saw148015 	flowop_endop(threadflow, flowop, 0);
990*6084Saw148015 	return (FILEBENCH_OK);
9915184Sek110237 }
9925184Sek110237 
9935184Sek110237 /*
9945184Sek110237  * Blocks the calling thread if the number of issued I/O
9955184Sek110237  * operations exceeds the number of posted events, thus
9965184Sek110237  * limiting the average I/O operation rate to the rate
997*6084Saw148015  * specified by eventgen_hz. Always returns FILEBENCH_OK.
9985184Sek110237  */
9995184Sek110237 static int
10005184Sek110237 flowoplib_iopslimit(threadflow_t *threadflow, flowop_t *flowop)
10015184Sek110237 {
10025184Sek110237 	uint64_t iops;
10035184Sek110237 	uint64_t delta;
10045673Saw148015 	uint64_t events;
10055184Sek110237 
10065184Sek110237 	/* Immediately bail if not set/enabled */
10075184Sek110237 	if (filebench_shm->eventgen_hz == 0)
1008*6084Saw148015 		return (FILEBENCH_OK);
10095184Sek110237 
10105184Sek110237 	if (flowop->fo_initted == 0) {
10115184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
10125184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
10135184Sek110237 		flowop->fo_initted = 1;
10145184Sek110237 	}
10155184Sek110237 
10165184Sek110237 	iops = (controlstats.fs_rcount +
10175184Sek110237 	    controlstats.fs_wcount);
10185184Sek110237 
10195184Sek110237 	/* Is this the first time around */
10205184Sek110237 	if (flowop->fo_tputlast == 0) {
10215184Sek110237 		flowop->fo_tputlast = iops;
1022*6084Saw148015 		return (FILEBENCH_OK);
10235184Sek110237 	}
10245184Sek110237 
10255184Sek110237 	delta = iops - flowop->fo_tputlast;
10265184Sek110237 	flowop->fo_tputbucket -= delta;
10275184Sek110237 	flowop->fo_tputlast = iops;
10285184Sek110237 
10295184Sek110237 	/* No need to block if the q isn't empty */
10305184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
10315673Saw148015 		flowop_endop(threadflow, flowop, 0);
1032*6084Saw148015 		return (FILEBENCH_OK);
10335184Sek110237 	}
10345184Sek110237 
10355184Sek110237 	iops = flowop->fo_tputbucket * -1;
10365184Sek110237 	events = iops;
10375184Sek110237 
10385184Sek110237 	flowop_beginop(threadflow, flowop);
10395184Sek110237 	while (filebench_shm->eventgen_hz) {
10405184Sek110237 
10415184Sek110237 		(void) ipc_mutex_lock(&filebench_shm->eventgen_lock);
10425184Sek110237 		if (filebench_shm->eventgen_q >= events) {
10435184Sek110237 			filebench_shm->eventgen_q -= events;
10445184Sek110237 			(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
10455184Sek110237 			flowop->fo_tputbucket += events;
10465184Sek110237 			break;
10475184Sek110237 		}
10485184Sek110237 		(void) pthread_cond_wait(&filebench_shm->eventgen_cv,
10495184Sek110237 		    &filebench_shm->eventgen_lock);
10505184Sek110237 		(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
10515184Sek110237 	}
10525673Saw148015 	flowop_endop(threadflow, flowop, 0);
10535184Sek110237 
1054*6084Saw148015 	return (FILEBENCH_OK);
10555184Sek110237 }
10565184Sek110237 
10575184Sek110237 /*
10585184Sek110237  * Blocks the calling thread if the number of issued filebench
10595184Sek110237  * operations exceeds the number of posted events, thus limiting
10605184Sek110237  * the average filebench operation rate to the rate specified by
1061*6084Saw148015  * eventgen_hz. Always returns FILEBENCH_OK.
10625184Sek110237  */
10635184Sek110237 static int
10645184Sek110237 flowoplib_opslimit(threadflow_t *threadflow, flowop_t *flowop)
10655184Sek110237 {
10665184Sek110237 	uint64_t ops;
10675184Sek110237 	uint64_t delta;
10685673Saw148015 	uint64_t events;
10695184Sek110237 
10705184Sek110237 	/* Immediately bail if not set/enabled */
10715184Sek110237 	if (filebench_shm->eventgen_hz == 0)
1072*6084Saw148015 		return (FILEBENCH_OK);
10735184Sek110237 
10745184Sek110237 	if (flowop->fo_initted == 0) {
10755184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
10765184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
10775184Sek110237 		flowop->fo_initted = 1;
10785184Sek110237 	}
10795184Sek110237 
10805184Sek110237 	ops = controlstats.fs_count;
10815184Sek110237 
10825184Sek110237 	/* Is this the first time around */
10835184Sek110237 	if (flowop->fo_tputlast == 0) {
10845184Sek110237 		flowop->fo_tputlast = ops;
1085*6084Saw148015 		return (FILEBENCH_OK);
10865184Sek110237 	}
10875184Sek110237 
10885184Sek110237 	delta = ops - flowop->fo_tputlast;
10895184Sek110237 	flowop->fo_tputbucket -= delta;
10905184Sek110237 	flowop->fo_tputlast = ops;
10915184Sek110237 
10925184Sek110237 	/* No need to block if the q isn't empty */
10935184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
10945673Saw148015 		flowop_endop(threadflow, flowop, 0);
1095*6084Saw148015 		return (FILEBENCH_OK);
10965184Sek110237 	}
10975184Sek110237 
10985184Sek110237 	ops = flowop->fo_tputbucket * -1;
10995184Sek110237 	events = ops;
11005184Sek110237 
11015184Sek110237 	flowop_beginop(threadflow, flowop);
11025184Sek110237 	while (filebench_shm->eventgen_hz) {
11035184Sek110237 		(void) ipc_mutex_lock(&filebench_shm->eventgen_lock);
11045184Sek110237 		if (filebench_shm->eventgen_q >= events) {
11055184Sek110237 			filebench_shm->eventgen_q -= events;
11065184Sek110237 			(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
11075184Sek110237 			flowop->fo_tputbucket += events;
11085184Sek110237 			break;
11095184Sek110237 		}
11105184Sek110237 		(void) pthread_cond_wait(&filebench_shm->eventgen_cv,
11115184Sek110237 		    &filebench_shm->eventgen_lock);
11125184Sek110237 		(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
11135184Sek110237 	}
11145673Saw148015 	flowop_endop(threadflow, flowop, 0);
11155184Sek110237 
1116*6084Saw148015 	return (FILEBENCH_OK);
11175184Sek110237 }
11185184Sek110237 
11195184Sek110237 
11205184Sek110237 /*
11215184Sek110237  * Blocks the calling thread if the number of bytes of I/O
11225184Sek110237  * issued exceeds one megabyte times the number of posted
11235184Sek110237  * events, thus limiting the average I/O byte rate to one
11245184Sek110237  * megabyte times the event rate as set by eventgen_hz.
1125*6084Saw148015  * Always retuns FILEBENCH_OK.
11265184Sek110237  */
11275184Sek110237 static int
11285184Sek110237 flowoplib_bwlimit(threadflow_t *threadflow, flowop_t *flowop)
11295184Sek110237 {
11305184Sek110237 	uint64_t bytes;
11315184Sek110237 	uint64_t delta;
11325673Saw148015 	uint64_t events;
11335184Sek110237 
11345184Sek110237 	/* Immediately bail if not set/enabled */
11355184Sek110237 	if (filebench_shm->eventgen_hz == 0)
1136*6084Saw148015 		return (FILEBENCH_OK);
11375184Sek110237 
11385184Sek110237 	if (flowop->fo_initted == 0) {
11395184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
11405184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
11415184Sek110237 		flowop->fo_initted = 1;
11425184Sek110237 	}
11435184Sek110237 
11445184Sek110237 	bytes = (controlstats.fs_rbytes +
11455184Sek110237 	    controlstats.fs_wbytes);
11465184Sek110237 
11475184Sek110237 	/* Is this the first time around */
11485184Sek110237 	if (flowop->fo_tputlast == 0) {
11495184Sek110237 		flowop->fo_tputlast = bytes;
1150*6084Saw148015 		return (FILEBENCH_OK);
11515184Sek110237 	}
11525184Sek110237 
11535184Sek110237 	delta = bytes - flowop->fo_tputlast;
11545184Sek110237 	flowop->fo_tputbucket -= delta;
11555184Sek110237 	flowop->fo_tputlast = bytes;
11565184Sek110237 
11575184Sek110237 	/* No need to block if the q isn't empty */
11585184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
11595673Saw148015 		flowop_endop(threadflow, flowop, 0);
1160*6084Saw148015 		return (FILEBENCH_OK);
11615184Sek110237 	}
11625184Sek110237 
11635184Sek110237 	bytes = flowop->fo_tputbucket * -1;
11645184Sek110237 	events = (bytes / MB) + 1;
11655184Sek110237 
11665184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "%lld bytes, %lld events",
11675184Sek110237 	    bytes, events);
11685184Sek110237 
11695184Sek110237 	flowop_beginop(threadflow, flowop);
11705184Sek110237 	while (filebench_shm->eventgen_hz) {
11715184Sek110237 		(void) ipc_mutex_lock(&filebench_shm->eventgen_lock);
11725184Sek110237 		if (filebench_shm->eventgen_q >= events) {
11735184Sek110237 			filebench_shm->eventgen_q -= events;
11745184Sek110237 			(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
11755184Sek110237 			flowop->fo_tputbucket += (events * MB);
11765184Sek110237 			break;
11775184Sek110237 		}
11785184Sek110237 		(void) pthread_cond_wait(&filebench_shm->eventgen_cv,
11795184Sek110237 		    &filebench_shm->eventgen_lock);
11805184Sek110237 		(void) ipc_mutex_unlock(&filebench_shm->eventgen_lock);
11815184Sek110237 	}
11825673Saw148015 	flowop_endop(threadflow, flowop, 0);
11835184Sek110237 
1184*6084Saw148015 	return (FILEBENCH_OK);
11855184Sek110237 }
11865184Sek110237 
11875184Sek110237 /*
11885184Sek110237  * These flowops terminate a benchmark run when either the specified
11895184Sek110237  * number of bytes of I/O (flowoplib_finishonbytes) or the specified
11905184Sek110237  * number of I/O operations (flowoplib_finishoncount) have been generated.
11915184Sek110237  */
11925184Sek110237 
11935184Sek110237 
11945184Sek110237 /*
11955184Sek110237  * Stop filebench run when specified number of I/O bytes have been
11965184Sek110237  * transferred. Compares controlstats.fs_bytes with *flowop->value,
11975184Sek110237  * and if greater returns 1, stopping the run, if not, returns 0
11985184Sek110237  * to continue running.
11995184Sek110237  */
12005184Sek110237 static int
12015184Sek110237 flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop)
12025184Sek110237 {
12035184Sek110237 	uint64_t b;
12045184Sek110237 	uint64_t bytes = *flowop->fo_value;
12055184Sek110237 
12065184Sek110237 	b = controlstats.fs_bytes;
12075184Sek110237 
12085184Sek110237 	flowop_beginop(threadflow, flowop);
12095184Sek110237 	if (b > bytes) {
12105673Saw148015 		flowop_endop(threadflow, flowop, 0);
1211*6084Saw148015 		return (FILEBENCH_DONE);
12125184Sek110237 	}
12135673Saw148015 	flowop_endop(threadflow, flowop, 0);
12145184Sek110237 
1215*6084Saw148015 	return (FILEBENCH_OK);
12165184Sek110237 }
12175184Sek110237 
12185184Sek110237 /*
12195184Sek110237  * Stop filebench run when specified number of I/O operations have
12205184Sek110237  * been performed. Compares controlstats.fs_count with *flowop->value,
1221*6084Saw148015  * and if greater returns 1, stopping the run, if not, returns FILEBENCH_OK
1222*6084Saw148015  * to continue running.
12235184Sek110237  */
12245184Sek110237 static int
12255184Sek110237 flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop)
12265184Sek110237 {
12275184Sek110237 	uint64_t ops;
12285184Sek110237 	uint64_t count = *flowop->fo_value;
12295184Sek110237 
12305184Sek110237 	ops = controlstats.fs_count;
12315184Sek110237 
12325184Sek110237 	flowop_beginop(threadflow, flowop);
1233*6084Saw148015 	if (ops >= count) {
12345673Saw148015 		flowop_endop(threadflow, flowop, 0);
1235*6084Saw148015 		return (FILEBENCH_DONE);
12365184Sek110237 	}
12375673Saw148015 	flowop_endop(threadflow, flowop, 0);
12385184Sek110237 
1239*6084Saw148015 	return (FILEBENCH_OK);
12405184Sek110237 }
12415184Sek110237 
12425184Sek110237 /*
12435184Sek110237  * Semaphore synchronization using either System V semaphores or
12445184Sek110237  * posix semaphores. If System V semaphores are available, they will be
12455184Sek110237  * used, otherwise posix semaphores will be used.
12465184Sek110237  */
12475184Sek110237 
12485184Sek110237 
12495184Sek110237 /*
12505184Sek110237  * Initializes the filebench "block on semaphore" flowop.
12515184Sek110237  * If System V semaphores are implemented, the routine
12525184Sek110237  * initializes the System V semaphore subsystem if it hasn't
12535184Sek110237  * already been initialized, also allocates a pair of semids
12545184Sek110237  * and initializes the highwater System V semaphore.
12555184Sek110237  * If no System V semaphores, then does nothing special.
1256*6084Saw148015  * Returns FILEBENCH_ERROR if it cannot acquire a set of System V semphores
1257*6084Saw148015  * or if the initial post to the semaphore set fails. Returns FILEBENCH_OK
12585184Sek110237  * on success.
12595184Sek110237  */
12605184Sek110237 static int
12615184Sek110237 flowoplib_semblock_init(flowop_t *flowop)
12625184Sek110237 {
12635184Sek110237 
12645184Sek110237 #ifdef HAVE_SYSV_SEM
12655184Sek110237 	int semid;
12665184Sek110237 	struct sembuf sbuf[2];
12675184Sek110237 	int highwater;
12685184Sek110237 
12695184Sek110237 	ipc_seminit();
12705184Sek110237 
12715184Sek110237 	flowop->fo_semid_lw = ipc_semidalloc();
12725184Sek110237 	flowop->fo_semid_hw = ipc_semidalloc();
12735184Sek110237 
12745184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d semblock init semid=%x",
12755184Sek110237 	    flowop->fo_name, flowop->fo_instance, flowop->fo_semid_lw);
12765184Sek110237 
12775184Sek110237 	/*
12785184Sek110237 	 * Raise the number of the hw queue, causing the posting side to
12795184Sek110237 	 * block if queue is > 2 x blocking value
12805184Sek110237 	 */
12815184Sek110237 	if ((semid = semget(filebench_shm->semkey, FILEBENCH_NSEMS, 0)) == -1) {
12825184Sek110237 		filebench_log(LOG_ERROR, "semblock init lookup %x failed: %s",
12835184Sek110237 		    filebench_shm->semkey,
12845184Sek110237 		    strerror(errno));
1285*6084Saw148015 		return (FILEBENCH_ERROR);
12865184Sek110237 	}
12875184Sek110237 
12885184Sek110237 	if ((highwater = flowop->fo_semid_hw) == 0)
12895184Sek110237 		highwater = *flowop->fo_value;
12905184Sek110237 
12915184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "setting highwater to : %d", highwater);
12925184Sek110237 
12935673Saw148015 	sbuf[0].sem_num = (short)highwater;
12945184Sek110237 	sbuf[0].sem_op = *flowop->fo_highwater;
12955184Sek110237 	sbuf[0].sem_flg = 0;
12965184Sek110237 	if ((semop(semid, &sbuf[0], 1) == -1) && errno) {
12975184Sek110237 		filebench_log(LOG_ERROR, "semblock init post failed: %s (%d,"
12985184Sek110237 		    "%d)", strerror(errno), sbuf[0].sem_num, sbuf[0].sem_op);
1299*6084Saw148015 		return (FILEBENCH_ERROR);
13005184Sek110237 	}
13015184Sek110237 #else
13025184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
13035184Sek110237 	    "flow %s-%d semblock init with posix semaphore",
13045184Sek110237 	    flowop->fo_name, flowop->fo_instance);
13055184Sek110237 
13065184Sek110237 	sem_init(&flowop->fo_sem, 1, 0);
13075184Sek110237 #endif	/* HAVE_SYSV_SEM */
13085184Sek110237 
13095184Sek110237 	if (!(*flowop->fo_blocking))
13105184Sek110237 		(void) ipc_mutex_unlock(&flowop->fo_lock);
13115184Sek110237 
1312*6084Saw148015 	return (FILEBENCH_OK);
13135184Sek110237 }
13145184Sek110237 
13155184Sek110237 /*
13165184Sek110237  * Releases the semids for the System V semaphore allocated
13175184Sek110237  * to this flowop. If not using System V semaphores, then
1318*6084Saw148015  * it is effectively just a no-op.
13195184Sek110237  */
13205184Sek110237 static void
13215184Sek110237 flowoplib_semblock_destruct(flowop_t *flowop)
13225184Sek110237 {
13235184Sek110237 #ifdef HAVE_SYSV_SEM
13245184Sek110237 	ipc_semidfree(flowop->fo_semid_lw);
13255184Sek110237 	ipc_semidfree(flowop->fo_semid_hw);
13265184Sek110237 #else
13275184Sek110237 	sem_destroy(&flowop->fo_sem);
13285184Sek110237 #endif /* HAVE_SYSV_SEM */
13295184Sek110237 }
13305184Sek110237 
13315184Sek110237 /*
13325184Sek110237  * Attempts to pass a System V or posix semaphore as appropriate,
1333*6084Saw148015  * and blocks if necessary. Returns FILEBENCH_ERROR if a set of System V
13345184Sek110237  * semphores is not available or cannot be acquired, or if the initial
1335*6084Saw148015  * post to the semaphore set fails. Returns FILEBENCH_OK on success.
13365184Sek110237  */
13375184Sek110237 static int
13385184Sek110237 flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop)
13395184Sek110237 {
13405184Sek110237 
13415184Sek110237 #ifdef HAVE_SYSV_SEM
13425184Sek110237 	struct sembuf sbuf[2];
13435184Sek110237 	int value = *flowop->fo_value;
13445184Sek110237 	int semid;
13455184Sek110237 	struct timespec timeout;
13465184Sek110237 
13475184Sek110237 	if ((semid = semget(filebench_shm->semkey, FILEBENCH_NSEMS, 0)) == -1) {
13485184Sek110237 		filebench_log(LOG_ERROR, "lookup semop %x failed: %s",
13495184Sek110237 		    filebench_shm->semkey,
13505184Sek110237 		    strerror(errno));
1351*6084Saw148015 		return (FILEBENCH_ERROR);
13525184Sek110237 	}
13535184Sek110237 
13545184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
13555184Sek110237 	    "flow %s-%d sem blocking on id %x num %x value %d",
13565184Sek110237 	    flowop->fo_name, flowop->fo_instance, semid,
13575184Sek110237 	    flowop->fo_semid_hw, value);
13585184Sek110237 
13595184Sek110237 	/* Post, decrement the increment the hw queue */
13605184Sek110237 	sbuf[0].sem_num = flowop->fo_semid_hw;
13615673Saw148015 	sbuf[0].sem_op = (short)value;
13625184Sek110237 	sbuf[0].sem_flg = 0;
13635184Sek110237 	sbuf[1].sem_num = flowop->fo_semid_lw;
13645184Sek110237 	sbuf[1].sem_op = value * -1;
13655184Sek110237 	sbuf[1].sem_flg = 0;
13665184Sek110237 	timeout.tv_sec = 600;
13675184Sek110237 	timeout.tv_nsec = 0;
13685184Sek110237 
13695184Sek110237 	if (*flowop->fo_blocking)
13705184Sek110237 		(void) ipc_mutex_unlock(&flowop->fo_lock);
13715184Sek110237 
13725184Sek110237 	flowop_beginop(threadflow, flowop);
13735184Sek110237 
13745184Sek110237 #ifdef HAVE_SEMTIMEDOP
13755184Sek110237 	(void) semtimedop(semid, &sbuf[0], 1, &timeout);
13765184Sek110237 	(void) semtimedop(semid, &sbuf[1], 1, &timeout);
13775184Sek110237 #else
13785184Sek110237 	(void) semop(semid, &sbuf[0], 1);
13795184Sek110237 	(void) semop(semid, &sbuf[1], 1);
13805184Sek110237 #endif /* HAVE_SEMTIMEDOP */
13815184Sek110237 
13825184Sek110237 	if (*flowop->fo_blocking)
13835184Sek110237 		(void) ipc_mutex_lock(&flowop->fo_lock);
13845184Sek110237 
13855673Saw148015 	flowop_endop(threadflow, flowop, 0);
13865184Sek110237 
13875184Sek110237 #else
13885184Sek110237 	int value = *flowop->fo_value;
13895184Sek110237 	int i;
13905184Sek110237 
13915184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
13925184Sek110237 	    "flow %s-%d sem blocking on posix semaphore",
13935184Sek110237 	    flowop->fo_name, flowop->fo_instance);
13945184Sek110237 
13955184Sek110237 	/* Decrement sem by value */
13965184Sek110237 	for (i = 0; i < value; i++) {
13975184Sek110237 		if (sem_wait(&flowop->fo_sem) == -1) {
13985184Sek110237 			filebench_log(LOG_ERROR, "semop wait failed");
1399*6084Saw148015 			return (FILEBENCH_ERROR);
14005184Sek110237 		}
14015184Sek110237 	}
14025184Sek110237 
14035184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d sem unblocking",
14045184Sek110237 	    flowop->fo_name, flowop->fo_instance);
14055184Sek110237 #endif /* HAVE_SYSV_SEM */
14065184Sek110237 
1407*6084Saw148015 	return (FILEBENCH_OK);
14085184Sek110237 }
14095184Sek110237 
14105184Sek110237 /*
1411*6084Saw148015  * Calls ipc_seminit(). Always returns FILEBENCH_OK.
14125184Sek110237  */
14135184Sek110237 /* ARGSUSED */
14145184Sek110237 static int
14155184Sek110237 flowoplib_sempost_init(flowop_t *flowop)
14165184Sek110237 {
14175184Sek110237 #ifdef HAVE_SYSV_SEM
14185184Sek110237 	ipc_seminit();
14195184Sek110237 #endif /* HAVE_SYSV_SEM */
1420*6084Saw148015 	return (FILEBENCH_OK);
14215184Sek110237 }
14225184Sek110237 
14235184Sek110237 /*
14245184Sek110237  * Post to a System V or posix semaphore as appropriate.
14255184Sek110237  * On the first call for a given flowop instance, this routine
14265184Sek110237  * will use the fo_targetname attribute to locate all semblock
14275184Sek110237  * flowops that are expecting posts from this flowop. All
14285184Sek110237  * target flowops on this list will have a post operation done
14295184Sek110237  * to their semaphores on each call.
14305184Sek110237  */
14315184Sek110237 static int
14325184Sek110237 flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop)
14335184Sek110237 {
14345184Sek110237 	flowop_t *target;
14355184Sek110237 
14365184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
14375184Sek110237 	    "sempost flow %s-%d",
14385184Sek110237 	    flowop->fo_name,
14395184Sek110237 	    flowop->fo_instance);
14405184Sek110237 
14415184Sek110237 	/* if this is the first post, create the post list */
14425184Sek110237 	if (flowop->fo_targets == NULL) {
14435184Sek110237 		flowop_t *result = flowop_find(flowop->fo_targetname);
14445184Sek110237 
14455184Sek110237 		flowop->fo_targets = result;
14465184Sek110237 
14475184Sek110237 		if (result == NULL) {
14485184Sek110237 			filebench_log(LOG_ERROR,
14495184Sek110237 			    "sempost: could not find op %s for thread %s",
14505184Sek110237 			    flowop->fo_targetname,
14515184Sek110237 			    threadflow->tf_name);
14525184Sek110237 			filebench_shutdown(1);
14535184Sek110237 		}
14545184Sek110237 
14555184Sek110237 		while (result) {
14565184Sek110237 			result->fo_targetnext =
14575184Sek110237 			    result->fo_resultnext;
14585184Sek110237 			result = result->fo_resultnext;
14595184Sek110237 		}
14605184Sek110237 	}
14615184Sek110237 
14625184Sek110237 	target = flowop->fo_targets;
14635184Sek110237 
14645184Sek110237 	flowop_beginop(threadflow, flowop);
14655184Sek110237 	/* post to the targets */
14665184Sek110237 	while (target) {
14675184Sek110237 #ifdef HAVE_SYSV_SEM
14685184Sek110237 		struct sembuf sbuf[2];
14695184Sek110237 		int semid;
14705184Sek110237 		int blocking;
14715184Sek110237 #else
14725184Sek110237 		int i;
14735184Sek110237 #endif /* HAVE_SYSV_SEM */
14745184Sek110237 		int value = *flowop->fo_value;
14755184Sek110237 		struct timespec timeout;
14765184Sek110237 
14775184Sek110237 		if (target->fo_instance == FLOW_MASTER) {
14785184Sek110237 			target = target->fo_targetnext;
14795184Sek110237 			continue;
14805184Sek110237 		}
14815184Sek110237 
14825184Sek110237 #ifdef HAVE_SYSV_SEM
14835184Sek110237 
14845184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
14855184Sek110237 		    "sempost flow %s-%d num %x",
14865184Sek110237 		    target->fo_name,
14875184Sek110237 		    target->fo_instance,
14885184Sek110237 		    target->fo_semid_lw);
14895184Sek110237 
14905184Sek110237 		if ((semid = semget(filebench_shm->semkey,
14915184Sek110237 		    FILEBENCH_NSEMS, 0)) == -1) {
14925184Sek110237 			filebench_log(LOG_ERROR,
14935184Sek110237 			    "lookup semop %x failed: %s",
14945184Sek110237 			    filebench_shm->semkey,
14955184Sek110237 			    strerror(errno));
1496*6084Saw148015 			return (FILEBENCH_ERROR);
14975184Sek110237 		}
14985184Sek110237 
14995184Sek110237 		sbuf[0].sem_num = target->fo_semid_lw;
15005673Saw148015 		sbuf[0].sem_op = (short)value;
15015184Sek110237 		sbuf[0].sem_flg = 0;
15025184Sek110237 		sbuf[1].sem_num = target->fo_semid_hw;
15035184Sek110237 		sbuf[1].sem_op = value * -1;
15045184Sek110237 		sbuf[1].sem_flg = 0;
15055184Sek110237 		timeout.tv_sec = 600;
15065184Sek110237 		timeout.tv_nsec = 0;
15075184Sek110237 
15085184Sek110237 		if (*flowop->fo_blocking)
15095184Sek110237 			blocking = 1;
15105184Sek110237 		else
15115184Sek110237 			blocking = 0;
15125184Sek110237 
15135184Sek110237 #ifdef HAVE_SEMTIMEDOP
15145184Sek110237 		if ((semtimedop(semid, &sbuf[0], blocking + 1,
15155184Sek110237 		    &timeout) == -1) && (errno && (errno != EAGAIN))) {
15165184Sek110237 #else
15175184Sek110237 		if ((semop(semid, &sbuf[0], blocking + 1) == -1) &&
15185184Sek110237 		    (errno && (errno != EAGAIN))) {
15195184Sek110237 #endif /* HAVE_SEMTIMEDOP */
15205184Sek110237 			filebench_log(LOG_ERROR, "semop post failed: %s",
15215184Sek110237 			    strerror(errno));
1522*6084Saw148015 			return (FILEBENCH_ERROR);
15235184Sek110237 		}
15245184Sek110237 
15255184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
15265184Sek110237 		    "flow %s-%d finished posting",
15275184Sek110237 		    target->fo_name, target->fo_instance);
15285184Sek110237 #else
15295184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
15305184Sek110237 		    "sempost flow %s-%d to posix semaphore",
15315184Sek110237 		    target->fo_name,
15325184Sek110237 		    target->fo_instance);
15335184Sek110237 
15345184Sek110237 		/* Increment sem by value */
15355184Sek110237 		for (i = 0; i < value; i++) {
15365184Sek110237 			if (sem_post(&target->fo_sem) == -1) {
15375184Sek110237 				filebench_log(LOG_ERROR, "semop post failed");
1538*6084Saw148015 				return (FILEBENCH_ERROR);
15395184Sek110237 			}
15405184Sek110237 		}
15415184Sek110237 
15425184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
15435184Sek110237 		    target->fo_name, target->fo_instance);
15445184Sek110237 #endif /* HAVE_SYSV_SEM */
15455184Sek110237 
15465184Sek110237 		target = target->fo_targetnext;
15475184Sek110237 	}
15485673Saw148015 	flowop_endop(threadflow, flowop, 0);
15495184Sek110237 
1550*6084Saw148015 	return (FILEBENCH_OK);
15515184Sek110237 }
15525184Sek110237 
15535184Sek110237 
15545184Sek110237 /*
15555184Sek110237  * Section for exercising create / open / close / delete operations
15565184Sek110237  * on files within a fileset. For proper operation, the flowop attribute
15575184Sek110237  * "fd", which sets the fo_fdnumber field in the flowop, must be used
15585184Sek110237  * so that the same file is opened and later closed. "fd" is an index
15595184Sek110237  * into a pair of arrays maintained by threadflows, one of which
15605184Sek110237  * contains the operating system assigned file descriptors and the other
15615184Sek110237  * a pointer to the filesetentry whose file the file descriptor
15625184Sek110237  * references. An openfile flowop defined without fd being set will use
15635184Sek110237  * the default (0) fd or, if specified, rotate through fd indices, but
15645184Sek110237  * createfile and closefile must use the default or a specified fd.
15655184Sek110237  * Meanwhile deletefile picks and arbitrary file to delete, regardless
15665184Sek110237  * of fd attribute.
15675184Sek110237  */
15685184Sek110237 
15695184Sek110237 /*
15705184Sek110237  * XXX Making file selection more consistent among the flowops might good
15715184Sek110237  */
15725184Sek110237 
15735184Sek110237 
15745184Sek110237 /*
15755184Sek110237  * Emulates (and actually does) file open. Obtains a file descriptor
1576*6084Saw148015  * index, then calls flowoplib_openfile_common() to open. Returns
1577*6084Saw148015  * FILEBENCH_ERROR if no file descriptor is found, and returns the
1578*6084Saw148015  * status from flowoplib_openfile_common otherwise (FILEBENCH_ERROR,
1579*6084Saw148015  * FILEBENCH_NORSC, FILEBENCH_OK).
15805184Sek110237  */
15815184Sek110237 static int
15825184Sek110237 flowoplib_openfile(threadflow_t *threadflow, flowop_t *flowop)
15835184Sek110237 {
15845184Sek110237 	int fd = flowoplib_fdnum(threadflow, flowop);
15855184Sek110237 
15865184Sek110237 	if (fd == -1)
1587*6084Saw148015 		return (FILEBENCH_ERROR);
15885184Sek110237 
15895184Sek110237 	return (flowoplib_openfile_common(threadflow, flowop, fd));
15905184Sek110237 }
15915184Sek110237 
15925184Sek110237 /*
15935184Sek110237  * Common file opening code for filesets. Uses the supplied
15945184Sek110237  * file descriptor index to determine the tf_fd entry to use.
15955184Sek110237  * If the entry is empty (0) and the fileset exists, fileset
15965184Sek110237  * pick is called to select a fileset entry to use. The file
15975184Sek110237  * specified in the filesetentry is opened, and the returned
15985184Sek110237  * operating system file descriptor and a pointer to the
15995184Sek110237  * filesetentry are stored in tf_fd[fd] and tf_fse[fd],
1600*6084Saw148015  * respectively. Returns FILEBENCH_ERROR on error,
1601*6084Saw148015  * FILEBENCH_NORSC if no suitable filesetentry can be found,
1602*6084Saw148015  * and FILEBENCH_OK on success.
16035184Sek110237  */
16045184Sek110237 static int
16055184Sek110237 flowoplib_openfile_common(threadflow_t *threadflow, flowop_t *flowop, int fd)
16065184Sek110237 {
16075184Sek110237 	filesetentry_t *file;
16085184Sek110237 	int tid = 0;
16095184Sek110237 
16105184Sek110237 	/*
16115184Sek110237 	 * If the flowop doesn't default to persistent fd
16125184Sek110237 	 * then get unique thread ID for use by fileset_pick
16135184Sek110237 	 */
16145184Sek110237 	if (integer_isset(flowop->fo_rotatefd))
16155184Sek110237 		tid = threadflow->tf_utid;
16165184Sek110237 
16175184Sek110237 	if (threadflow->tf_fd[fd] != 0) {
16185184Sek110237 		filebench_log(LOG_ERROR,
16195184Sek110237 		    "flowop %s attempted to open without closing on fd %d",
16205184Sek110237 		    flowop->fo_name, fd);
1621*6084Saw148015 		return (FILEBENCH_ERROR);
16225184Sek110237 	}
16235184Sek110237 
16245184Sek110237 	if (flowop->fo_fileset == NULL) {
16255184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
1626*6084Saw148015 		return (FILEBENCH_ERROR);
16275184Sek110237 	}
16285184Sek110237 
16295673Saw148015 #ifdef HAVE_RAW_SUPPORT
16305673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
16315673Saw148015 		int open_attrs = 0;
16325673Saw148015 		char name[MAXPATHLEN];
16335673Saw148015 
16345673Saw148015 		(void) strcpy(name, *flowop->fo_fileset->fs_path);
16355673Saw148015 		(void) strcat(name, "/");
16365673Saw148015 		(void) strcat(name, flowop->fo_fileset->fs_name);
16375673Saw148015 
16385673Saw148015 		if (*flowop->fo_dsync) {
16395673Saw148015 #ifdef sun
16405673Saw148015 			open_attrs |= O_DSYNC;
16415673Saw148015 #else
16425673Saw148015 			open_attrs |= O_FSYNC;
16435673Saw148015 #endif
16445673Saw148015 		}
16455673Saw148015 
16465673Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
16475673Saw148015 		    "open raw device %s flags %d = %d", name, open_attrs, fd);
16485673Saw148015 
16495673Saw148015 		threadflow->tf_fd[fd] = open64(name,
16505673Saw148015 		    O_RDWR | open_attrs, 0666);
16515673Saw148015 
16525673Saw148015 		if (threadflow->tf_fd[fd] < 0) {
16535673Saw148015 			filebench_log(LOG_ERROR,
16545673Saw148015 			    "Failed to open raw device %s: %s",
16555673Saw148015 			    name, strerror(errno));
1656*6084Saw148015 			return (FILEBENCH_ERROR);
16575673Saw148015 		}
16585673Saw148015 
16595673Saw148015 		/* if running on Solaris, use un-buffered io */
16605673Saw148015 #ifdef sun
16615673Saw148015 		(void) directio(threadflow->tf_fd[fd], DIRECTIO_ON);
16625673Saw148015 #endif
16635673Saw148015 
16645673Saw148015 		threadflow->tf_fse[fd] = NULL;
16655673Saw148015 
1666*6084Saw148015 		return (FILEBENCH_OK);
16675673Saw148015 	}
16685673Saw148015 #endif /* HAVE_RAW_SUPPORT */
16695673Saw148015 
16705184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
16715184Sek110237 	    FILESET_PICKEXISTS, tid)) == NULL) {
1672*6084Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
16735184Sek110237 		    "flowop %s failed to pick file from %s on fd %d",
16745184Sek110237 		    flowop->fo_name,
16755184Sek110237 		    flowop->fo_fileset->fs_name, fd);
1676*6084Saw148015 		return (FILEBENCH_NORSC);
16775184Sek110237 	}
16785184Sek110237 
16795184Sek110237 	threadflow->tf_fse[fd] = file;
16805184Sek110237 
16815184Sek110237 	flowop_beginop(threadflow, flowop);
16825184Sek110237 	threadflow->tf_fd[fd] = fileset_openfile(flowop->fo_fileset,
16835184Sek110237 	    file, O_RDWR, 0666, flowoplib_fileattrs(flowop));
16845673Saw148015 	flowop_endop(threadflow, flowop, 0);
16855184Sek110237 
16865184Sek110237 	if (threadflow->tf_fd[fd] < 0) {
16875184Sek110237 		filebench_log(LOG_ERROR, "failed to open file %s",
16885184Sek110237 		    flowop->fo_name);
1689*6084Saw148015 		return (FILEBENCH_ERROR);
16905184Sek110237 	}
16915184Sek110237 
16925184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT,
16935184Sek110237 	    "flowop %s: opened %s fd[%d] = %d",
16945184Sek110237 	    flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]);
16955184Sek110237 
1696*6084Saw148015 	return (FILEBENCH_OK);
16975184Sek110237 }
16985184Sek110237 
16995184Sek110237 /*
17005184Sek110237  * Emulate create of a file. Uses the flowop's fdnumber to select
17015184Sek110237  * tf_fd and tf_fse array locations to put the created file's file
17025184Sek110237  * descriptor and filesetentry respectively. Uses fileset_pick()
17035184Sek110237  * to select a specific filesetentry whose file does not currently
17045184Sek110237  * exist for the file create operation. Then calls
17055184Sek110237  * fileset_openfile() with the O_CREATE flag set to create the
1706*6084Saw148015  * file. Returns FILEBENCH_ERROR if the array index specified by fdnumber is
17075184Sek110237  * already in use, the flowop has no associated fileset, or
17085184Sek110237  * the create call fails. Returns 1 if a filesetentry with a
1709*6084Saw148015  * nonexistent file cannot be found. Returns FILEBENCH_OK on success.
17105184Sek110237  */
17115184Sek110237 static int
17125184Sek110237 flowoplib_createfile(threadflow_t *threadflow, flowop_t *flowop)
17135184Sek110237 {
17145184Sek110237 	filesetentry_t *file;
17155184Sek110237 	int fd = flowop->fo_fdnumber;
17165184Sek110237 
17175184Sek110237 	if (threadflow->tf_fd[fd] != 0) {
17185184Sek110237 		filebench_log(LOG_ERROR,
17195184Sek110237 		    "flowop %s attempted to create without closing on fd %d",
17205184Sek110237 		    flowop->fo_name, fd);
1721*6084Saw148015 		return (FILEBENCH_ERROR);
17225184Sek110237 	}
17235184Sek110237 
17245184Sek110237 	if (flowop->fo_fileset == NULL) {
17255184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
1726*6084Saw148015 		return (FILEBENCH_ERROR);
17275184Sek110237 	}
17285184Sek110237 
17295673Saw148015 #ifdef HAVE_RAW_SUPPORT
17305673Saw148015 	/* can't be used with raw devices */
17315673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
17325673Saw148015 		filebench_log(LOG_ERROR,
17335673Saw148015 		    "flowop %s attempted to a createfile on RAW device",
17345673Saw148015 		    flowop->fo_name);
1735*6084Saw148015 		return (FILEBENCH_ERROR);
17365673Saw148015 	}
17375673Saw148015 #endif /* HAVE_RAW_SUPPORT */
17385673Saw148015 
17395184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
17405184Sek110237 	    FILESET_PICKNOEXIST, 0)) == NULL) {
1741*6084Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
1742*6084Saw148015 		    "flowop %s failed to pick file from fileset %s",
1743*6084Saw148015 		    flowop->fo_name, flowop->fo_fileset->fs_name);
1744*6084Saw148015 		return (FILEBENCH_NORSC);
17455184Sek110237 	}
17465184Sek110237 
17475184Sek110237 	threadflow->tf_fse[fd] = file;
17485184Sek110237 
17495184Sek110237 	flowop_beginop(threadflow, flowop);
17505184Sek110237 	threadflow->tf_fd[fd] = fileset_openfile(flowop->fo_fileset,
17515184Sek110237 	    file, O_RDWR | O_CREAT, 0666, flowoplib_fileattrs(flowop));
17525673Saw148015 	flowop_endop(threadflow, flowop, 0);
17535184Sek110237 
17545184Sek110237 	if (threadflow->tf_fd[fd] < 0) {
17555184Sek110237 		filebench_log(LOG_ERROR, "failed to create file %s",
17565184Sek110237 		    flowop->fo_name);
1757*6084Saw148015 		return (FILEBENCH_ERROR);
17585184Sek110237 	}
17595184Sek110237 
17605184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT,
17615184Sek110237 	    "flowop %s: created %s fd[%d] = %d",
17625184Sek110237 	    flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]);
17635184Sek110237 
1764*6084Saw148015 	return (FILEBENCH_OK);
17655184Sek110237 }
17665184Sek110237 
17675184Sek110237 /*
17685184Sek110237  * Emulates delete of a file. Picks an arbitrary filesetentry
17695184Sek110237  * whose file exists and uses unlink() to delete it. Clears
1770*6084Saw148015  * the FSE_EXISTS flag for the filesetentry. Returns FILEBENCH_ERROR if the
1771*6084Saw148015  * flowop has no associated fileset. Returns FILEBENCH_NORSC if an appropriate
1772*6084Saw148015  * filesetentry cannot be found, and FILEBENCH_OK on success.
17735184Sek110237  */
17745184Sek110237 static int
17755184Sek110237 flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop)
17765184Sek110237 {
17775184Sek110237 	filesetentry_t *file;
17785184Sek110237 	fileset_t *fileset;
17795184Sek110237 	char path[MAXPATHLEN];
17805184Sek110237 	char *pathtmp;
17815184Sek110237 
17825184Sek110237 	if (flowop->fo_fileset == NULL) {
17835184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
1784*6084Saw148015 		return (FILEBENCH_ERROR);
17855184Sek110237 	}
17865184Sek110237 
17875184Sek110237 	fileset = flowop->fo_fileset;
17885184Sek110237 
17895673Saw148015 #ifdef HAVE_RAW_SUPPORT
17905673Saw148015 	/* can't be used with raw devices */
17915673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
17925673Saw148015 		filebench_log(LOG_ERROR,
17935673Saw148015 		    "flowop %s attempted a deletefile on RAW device",
17945673Saw148015 		    flowop->fo_name);
1795*6084Saw148015 		return (FILEBENCH_ERROR);
17965673Saw148015 	}
17975673Saw148015 #endif /* HAVE_RAW_SUPPORT */
17985673Saw148015 
17995184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
18005184Sek110237 	    FILESET_PICKEXISTS, 0)) == NULL) {
18015184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT, "flowop %s failed to pick file",
18025184Sek110237 		    flowop->fo_name);
1803*6084Saw148015 		return (FILEBENCH_NORSC);
18045184Sek110237 	}
18055184Sek110237 
18065184Sek110237 	*path = 0;
18075184Sek110237 	(void) strcpy(path, *fileset->fs_path);
18085184Sek110237 	(void) strcat(path, "/");
18095184Sek110237 	(void) strcat(path, fileset->fs_name);
18105184Sek110237 	pathtmp = fileset_resolvepath(file);
18115184Sek110237 	(void) strcat(path, pathtmp);
18125184Sek110237 	free(pathtmp);
18135184Sek110237 
18145184Sek110237 	flowop_beginop(threadflow, flowop);
18155184Sek110237 	(void) unlink(path);
18165673Saw148015 	flowop_endop(threadflow, flowop, 0);
18175184Sek110237 	file->fse_flags &= ~FSE_EXISTS;
18185184Sek110237 	(void) ipc_mutex_unlock(&file->fse_lock);
18195184Sek110237 
18205184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "deleted file %s", file->fse_path);
18215184Sek110237 
1822*6084Saw148015 	return (FILEBENCH_OK);
18235184Sek110237 }
18245184Sek110237 
18255184Sek110237 /*
18265184Sek110237  * Emulates fsync of a file. Obtains the file descriptor index
18275184Sek110237  * from the flowop, obtains the actual file descriptor from
18285184Sek110237  * the threadflow's table, checks to be sure it is still an
1829*6084Saw148015  * open file, then does an fsync operation on it. Returns FILEBENCH_ERROR
1830*6084Saw148015  * if the file no longer is open, FILEBENCH_OK otherwise.
18315184Sek110237  */
18325184Sek110237 static int
18335184Sek110237 flowoplib_fsync(threadflow_t *threadflow, flowop_t *flowop)
18345184Sek110237 {
18355184Sek110237 	filesetentry_t *file;
18365184Sek110237 	int fd = flowop->fo_fdnumber;
18375184Sek110237 
18385184Sek110237 	if (threadflow->tf_fd[fd] == 0) {
18395184Sek110237 		filebench_log(LOG_ERROR,
18405184Sek110237 		    "flowop %s attempted to fsync a closed fd %d",
18415184Sek110237 		    flowop->fo_name, fd);
1842*6084Saw148015 		return (FILEBENCH_ERROR);
18435184Sek110237 	}
18445184Sek110237 
18455673Saw148015 	file = threadflow->tf_fse[fd];
18465673Saw148015 
18475673Saw148015 	if ((file == NULL) ||
18485673Saw148015 	    (file->fse_fileset->fs_attrs & FILESET_IS_RAW_DEV)) {
18495673Saw148015 		filebench_log(LOG_ERROR,
18505673Saw148015 		    "flowop %s attempted to a fsync a RAW device",
18515673Saw148015 		    flowop->fo_name);
1852*6084Saw148015 		return (FILEBENCH_ERROR);
18535673Saw148015 	}
18545673Saw148015 
18555184Sek110237 	/* Measure time to fsync */
18565184Sek110237 	flowop_beginop(threadflow, flowop);
18575184Sek110237 	(void) fsync(threadflow->tf_fd[fd]);
18585673Saw148015 	flowop_endop(threadflow, flowop, 0);
18595184Sek110237 
18605184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s", file->fse_path);
18615184Sek110237 
1862*6084Saw148015 	return (FILEBENCH_OK);
18635184Sek110237 }
18645184Sek110237 
18655184Sek110237 /*
18665184Sek110237  * Emulate fsync of an entire fileset. Search through the
18675184Sek110237  * threadflow's file descriptor array, doing fsync() on each
18685184Sek110237  * open file that belongs to the flowop's fileset. Always
1869*6084Saw148015  * returns FILEBENCH_OK.
18705184Sek110237  */
18715184Sek110237 static int
18725184Sek110237 flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop)
18735184Sek110237 {
18745184Sek110237 	int fd;
18755184Sek110237 
18765184Sek110237 	for (fd = 0; fd < THREADFLOW_MAXFD; fd++) {
18775184Sek110237 		filesetentry_t *file;
18785184Sek110237 
18795184Sek110237 		/* Match the file set to fsync */
18805184Sek110237 		if ((threadflow->tf_fse[fd] == NULL) ||
18815184Sek110237 		    (flowop->fo_fileset != threadflow->tf_fse[fd]->fse_fileset))
18825184Sek110237 			continue;
18835184Sek110237 
18845184Sek110237 		/* Measure time to fsync */
18855184Sek110237 		flowop_beginop(threadflow, flowop);
18865184Sek110237 		(void) fsync(threadflow->tf_fd[fd]);
18875673Saw148015 		flowop_endop(threadflow, flowop, 0);
18885184Sek110237 
18895184Sek110237 		file = threadflow->tf_fse[fd];
18905184Sek110237 
18915184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s",
18925184Sek110237 		    file->fse_path);
18935184Sek110237 	}
18945184Sek110237 
1895*6084Saw148015 	return (FILEBENCH_OK);
18965184Sek110237 }
18975184Sek110237 
18985184Sek110237 /*
18995184Sek110237  * Emulate close of a file.  Obtains the file descriptor index
19005184Sek110237  * from the flowop, obtains the actual file descriptor from the
19015184Sek110237  * threadflow's table, checks to be sure it is still an open
19025184Sek110237  * file, then does a close operation on it. Then sets the
19035184Sek110237  * threadflow file descriptor table entry to 0, and the file set
1904*6084Saw148015  * entry pointer to NULL. Returns FILEBENCH_ERROR if the file was not open,
1905*6084Saw148015  * FILEBENCH_OK otherwise.
19065184Sek110237  */
19075184Sek110237 static int
19085184Sek110237 flowoplib_closefile(threadflow_t *threadflow, flowop_t *flowop)
19095184Sek110237 {
19105184Sek110237 	filesetentry_t *file;
19115184Sek110237 	int fd = flowop->fo_fdnumber;
19125184Sek110237 
19135184Sek110237 	if (threadflow->tf_fd[fd] == 0) {
19145184Sek110237 		filebench_log(LOG_ERROR,
19155184Sek110237 		    "flowop %s attempted to close an already closed fd %d",
19165184Sek110237 		    flowop->fo_name, fd);
1917*6084Saw148015 		return (FILEBENCH_ERROR);
19185184Sek110237 	}
19195184Sek110237 
19205184Sek110237 	/* Measure time to close */
19215184Sek110237 	flowop_beginop(threadflow, flowop);
19225184Sek110237 	(void) close(threadflow->tf_fd[fd]);
19235673Saw148015 	flowop_endop(threadflow, flowop, 0);
19245184Sek110237 
19255184Sek110237 	file = threadflow->tf_fse[fd];
19265184Sek110237 
19275184Sek110237 	threadflow->tf_fd[fd] = 0;
19285184Sek110237 	threadflow->tf_fse[fd] = NULL;
19295184Sek110237 
19305184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "closed file %s", file->fse_path);
19315184Sek110237 
1932*6084Saw148015 	return (FILEBENCH_OK);
19335184Sek110237 }
19345184Sek110237 
19355184Sek110237 /*
19365184Sek110237  * Emulate stat of a file. Picks an arbitrary filesetentry with
19375184Sek110237  * an existing file from the flowop's fileset, then performs a
1938*6084Saw148015  * stat() operation on it. Returns FILEBENCH_ERROR if the flowop has no
1939*6084Saw148015  * associated fileset. Returns FILEBENCH_NORSC if an appropriate filesetentry
1940*6084Saw148015  * cannot be found, and FILEBENCH_OK on success.
19415184Sek110237  */
19425184Sek110237 static int
19435184Sek110237 flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop)
19445184Sek110237 {
19455184Sek110237 	filesetentry_t *file;
19465184Sek110237 	fileset_t *fileset;
19475184Sek110237 	char path[MAXPATHLEN];
19485184Sek110237 	char *pathtmp;
19495184Sek110237 
19505184Sek110237 	if (flowop->fo_fileset == NULL) {
19515184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
1952*6084Saw148015 		return (FILEBENCH_ERROR);
19535184Sek110237 	}
19545184Sek110237 
19555184Sek110237 	fileset = flowop->fo_fileset;
19565184Sek110237 
19575184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
19585184Sek110237 	    FILESET_PICKEXISTS, 0)) == NULL) {
19595184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT, "flowop %s failed to pick file",
19605184Sek110237 		    flowop->fo_name);
1961*6084Saw148015 		return (FILEBENCH_NORSC);
19625184Sek110237 	}
19635184Sek110237 
19645184Sek110237 	*path = 0;
19655184Sek110237 	(void) strcpy(path, *fileset->fs_path);
19665184Sek110237 	(void) strcat(path, "/");
19675184Sek110237 	(void) strcat(path, fileset->fs_name);
19685184Sek110237 	pathtmp = fileset_resolvepath(file);
19695184Sek110237 	(void) strcat(path, pathtmp);
19705184Sek110237 	free(pathtmp);
19715184Sek110237 
19725184Sek110237 	flowop_beginop(threadflow, flowop);
19735673Saw148015 	flowop_endop(threadflow, flowop, 0);
19745184Sek110237 
19755184Sek110237 	(void) ipc_mutex_unlock(&file->fse_lock);
19765184Sek110237 
1977*6084Saw148015 	return (FILEBENCH_OK);
19785184Sek110237 }
19795184Sek110237 
19805184Sek110237 
19815184Sek110237 /*
19825184Sek110237  * Additional reads and writes. Read and write whole files, write
19835184Sek110237  * and append to files. Some of these work with both fileobjs and
19845184Sek110237  * filesets, others only with filesets. The flowoplib_write routine
19855184Sek110237  * writes from thread memory, while the others read or write using
19865184Sek110237  * fo_buf memory. Note that both flowoplib_read() and
19875184Sek110237  * flowoplib_aiowrite() use thread memory as well.
19885184Sek110237  */
19895184Sek110237 
19905184Sek110237 
19915184Sek110237 /*
19925673Saw148015  * Emulate a read of a whole file. The file must be open with
19935673Saw148015  * file descriptor and filesetentry stored at the locations indexed
19945673Saw148015  * by the flowop's fdnumber. It then seeks to the beginning of the
19955673Saw148015  * associated file, and reads fs_iosize bytes at a time until the end
1996*6084Saw148015  * of the file. Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if
1997*6084Saw148015  * out of files, and FILEBENCH_OK on success.
19985184Sek110237  */
19995184Sek110237 static int
20005184Sek110237 flowoplib_readwholefile(threadflow_t *threadflow, flowop_t *flowop)
20015184Sek110237 {
20025673Saw148015 	caddr_t iobuf;
20035184Sek110237 	off64_t bytes = 0;
20045184Sek110237 	int fd = flowop->fo_fdnumber;
20055673Saw148015 	int filedesc;
20065184Sek110237 	int ret;
20075673Saw148015 	uint64_t wss;
20085673Saw148015 	vinteger_t iosize = *flowop->fo_iosize;
20095184Sek110237 
20105673Saw148015 	/* get the file to use */
2011*6084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, &wss,
2012*6084Saw148015 	    &filedesc)) != FILEBENCH_OK)
2013*6084Saw148015 		return (ret);
20145184Sek110237 
20155673Saw148015 	/* an I/O size of zero means read entire working set with one I/O */
20165673Saw148015 	if (iosize == 0)
20175673Saw148015 		iosize = wss;
20185184Sek110237 
20195673Saw148015 	if (flowoplib_iobufsetup(threadflow, flowop, &iobuf, iosize) != 0)
2020*6084Saw148015 		return (FILEBENCH_ERROR);
20215184Sek110237 
20225184Sek110237 	/* Measure time to read bytes */
20235184Sek110237 	flowop_beginop(threadflow, flowop);
20245673Saw148015 	(void) lseek64(filedesc, 0, SEEK_SET);
20255673Saw148015 	while ((ret = read(filedesc, iobuf, iosize)) > 0)
20265184Sek110237 		bytes += ret;
20275184Sek110237 
20285673Saw148015 	flowop_endop(threadflow, flowop, bytes);
20295184Sek110237 
20305184Sek110237 	if (ret < 0) {
20315184Sek110237 		filebench_log(LOG_ERROR,
20325184Sek110237 		    "Failed to read fd %d: %s",
20335184Sek110237 		    fd, strerror(errno));
2034*6084Saw148015 		return (FILEBENCH_ERROR);
20355184Sek110237 	}
20365184Sek110237 
2037*6084Saw148015 	return (FILEBENCH_OK);
20385184Sek110237 }
20395184Sek110237 
20405184Sek110237 /*
20415184Sek110237  * Emulate a write to a file of size fo_iosize.  Will write
20425184Sek110237  * to a file from a fileset if the flowop's fo_fileset field
20435184Sek110237  * specifies one or its fdnumber is non zero. Otherwise it
20445184Sek110237  * will write to a fileobj file, if one exists. If the file
20455184Sek110237  * is not currently open, the routine will attempt to open
20465184Sek110237  * it. The flowop's fo_wss parameter will be used to set the
20475184Sek110237  * maximum file size if it is non-zero, otherwise the
20485184Sek110237  * filesetentry's  fse_size will be used. A random memory
20495184Sek110237  * buffer offset is calculated, and, if fo_random is TRUE,
20505184Sek110237  * a random file offset is used for the write. Otherwise the
2051*6084Saw148015  * write is to the next sequential location. Returns
2052*6084Saw148015  * FILEBENCH_ERROR on errors, FILEBENCH_NORSC if iosetup can't
2053*6084Saw148015  * obtain a file, or FILEBENCH_OK on success.
20545184Sek110237  */
20555184Sek110237 static int
20565184Sek110237 flowoplib_write(threadflow_t *threadflow, flowop_t *flowop)
20575184Sek110237 {
20585673Saw148015 	caddr_t iobuf;
20595184Sek110237 	vinteger_t wss;
20605184Sek110237 	int filedesc;
2061*6084Saw148015 	int ret;
20625184Sek110237 
2063*6084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
2064*6084Saw148015 	    &filedesc, *flowop->fo_iosize)) != FILEBENCH_OK)
2065*6084Saw148015 		return (ret);
20665184Sek110237 
20675184Sek110237 	if (*flowop->fo_random) {
20685184Sek110237 		uint64_t fileoffset;
20695184Sek110237 
20705184Sek110237 		if (filebench_randomno64(&fileoffset,
20715184Sek110237 		    wss, *flowop->fo_iosize) == -1) {
20725184Sek110237 			filebench_log(LOG_ERROR,
20735184Sek110237 			    "file size smaller than IO size for thread %s",
20745184Sek110237 			    flowop->fo_name);
2075*6084Saw148015 			return (FILEBENCH_ERROR);
20765184Sek110237 		}
20775184Sek110237 		flowop_beginop(threadflow, flowop);
20785673Saw148015 		if (pwrite64(filedesc, iobuf,
20795184Sek110237 		    *flowop->fo_iosize, (off64_t)fileoffset) == -1) {
20805184Sek110237 			filebench_log(LOG_ERROR, "write failed, "
20815673Saw148015 			    "offset %lld io buffer %zd: %s",
20825673Saw148015 			    fileoffset, iobuf, strerror(errno));
20835673Saw148015 			flowop_endop(threadflow, flowop, 0);
2084*6084Saw148015 			return (FILEBENCH_ERROR);
20855184Sek110237 		}
20865673Saw148015 		flowop_endop(threadflow, flowop, *flowop->fo_iosize);
20875184Sek110237 	} else {
20885184Sek110237 		flowop_beginop(threadflow, flowop);
20895673Saw148015 		if (write(filedesc, iobuf,
20905184Sek110237 		    *flowop->fo_iosize) == -1) {
20915184Sek110237 			filebench_log(LOG_ERROR,
20925673Saw148015 			    "write failed, io buffer %zd: %s",
20935673Saw148015 			    iobuf, strerror(errno));
20945673Saw148015 			flowop_endop(threadflow, flowop, 0);
2095*6084Saw148015 			return (FILEBENCH_ERROR);
20965184Sek110237 		}
20975673Saw148015 		flowop_endop(threadflow, flowop, *flowop->fo_iosize);
20985184Sek110237 	}
20995184Sek110237 
2100*6084Saw148015 	return (FILEBENCH_OK);
21015184Sek110237 }
21025184Sek110237 
21035184Sek110237 /*
21045184Sek110237  * Emulate a write of a whole file.  The size of the file
21055673Saw148015  * is taken from a filesetentry identified by fo_srcfdnumber or
21065673Saw148015  * from the working set size, while the file descriptor used is
21075673Saw148015  * identified by fo_fdnumber. Does multiple writes of fo_iosize
2108*6084Saw148015  * length length until full file has been written. Returns FILEBENCH_ERROR on
2109*6084Saw148015  * error, FILEBENCH_NORSC if out of files, FILEBENCH_OK on success.
21105184Sek110237  */
21115184Sek110237 static int
21125184Sek110237 flowoplib_writewholefile(threadflow_t *threadflow, flowop_t *flowop)
21135184Sek110237 {
21145673Saw148015 	caddr_t iobuf;
21155184Sek110237 	filesetentry_t *file;
21165184Sek110237 	int wsize;
21175184Sek110237 	off64_t seek;
21185184Sek110237 	off64_t bytes = 0;
21195673Saw148015 	uint64_t wss;
21205673Saw148015 	int filedesc;
21215184Sek110237 	int srcfd = flowop->fo_srcfdnumber;
21225184Sek110237 	int ret;
21235673Saw148015 	vinteger_t iosize = *flowop->fo_iosize;
21245184Sek110237 
21255673Saw148015 	/* get the file to use */
2126*6084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, &wss,
2127*6084Saw148015 	    &filedesc)) != FILEBENCH_OK)
2128*6084Saw148015 		return (ret);
21295184Sek110237 
21305673Saw148015 	/* an I/O size of zero means read entire working set with one I/O */
21315673Saw148015 	if (iosize == 0)
21325673Saw148015 		iosize = wss;
21335184Sek110237 
21345673Saw148015 	if (flowoplib_iobufsetup(threadflow, flowop, &iobuf, iosize) != 0)
2135*6084Saw148015 		return (FILEBENCH_ERROR);
21365184Sek110237 
21375184Sek110237 	file = threadflow->tf_fse[srcfd];
21385673Saw148015 	if ((srcfd != 0) && (file == NULL)) {
21395673Saw148015 		filebench_log(LOG_ERROR, "flowop %s: NULL src file",
21405184Sek110237 		    flowop->fo_name);
2141*6084Saw148015 		return (FILEBENCH_ERROR);
21425184Sek110237 	}
21435184Sek110237 
21445673Saw148015 	if (file)
21455673Saw148015 		wss = file->fse_size;
21465673Saw148015 
21475673Saw148015 	wsize = (int)MIN(wss, iosize);
21485184Sek110237 
21495184Sek110237 	/* Measure time to write bytes */
21505184Sek110237 	flowop_beginop(threadflow, flowop);
21515673Saw148015 	for (seek = 0; seek < wss; seek += wsize) {
21525673Saw148015 		ret = write(filedesc, iobuf, wsize);
21535184Sek110237 		if (ret != wsize) {
21545184Sek110237 			filebench_log(LOG_ERROR,
21555184Sek110237 			    "Failed to write %d bytes on fd %d: %s",
21565673Saw148015 			    wsize, filedesc, strerror(errno));
21575673Saw148015 			flowop_endop(threadflow, flowop, 0);
2158*6084Saw148015 			return (FILEBENCH_ERROR);
21595184Sek110237 		}
21605673Saw148015 		wsize = (int)MIN(wss - seek, iosize);
21615184Sek110237 		bytes += ret;
21625184Sek110237 	}
21635673Saw148015 	flowop_endop(threadflow, flowop, bytes);
21645184Sek110237 
2165*6084Saw148015 	return (FILEBENCH_OK);
21665184Sek110237 }
21675184Sek110237 
21685184Sek110237 
21695184Sek110237 /*
21705184Sek110237  * Emulate a fixed size append to a file. Will append data to
21715184Sek110237  * a file chosen from a fileset if the flowop's fo_fileset
21725184Sek110237  * field specifies one or if its fdnumber is non zero.
21735184Sek110237  * Otherwise it will write to a fileobj file, if one exists.
21745184Sek110237  * The flowop's fo_wss parameter will be used to set the
21755184Sek110237  * maximum file size if it is non-zero, otherwise the
21765184Sek110237  * filesetentry's fse_size will be used. A random memory
21775184Sek110237  * buffer offset is calculated, then a logical seek to the
21785184Sek110237  * end of file is done followed by a write of fo_iosize
21795184Sek110237  * bytes. Writes are actually done from fo_buf, rather than
21805184Sek110237  * tf_mem as is done with flowoplib_write(), and no check
21815184Sek110237  * is made to see if fo_iosize exceeds the size of fo_buf.
2182*6084Saw148015  * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of
2183*6084Saw148015  * files in the fileset, FILEBENCH_OK on success.
21845184Sek110237  */
21855184Sek110237 static int
21865184Sek110237 flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop)
21875184Sek110237 {
21885673Saw148015 	caddr_t iobuf;
21895673Saw148015 	int filedesc;
21905184Sek110237 	vinteger_t wss;
21915673Saw148015 	vinteger_t iosize = *flowop->fo_iosize;
21925184Sek110237 	int ret;
21935184Sek110237 
2194*6084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
2195*6084Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
2196*6084Saw148015 		return (ret);
21975184Sek110237 
21985184Sek110237 	/* XXX wss is not being used */
21995184Sek110237 
22005184Sek110237 	/* Measure time to write bytes */
22015184Sek110237 	flowop_beginop(threadflow, flowop);
22025184Sek110237 	(void) lseek64(filedesc, 0, SEEK_END);
22035673Saw148015 	ret = write(filedesc, iobuf, iosize);
22045673Saw148015 	if (ret != iosize) {
22055184Sek110237 		filebench_log(LOG_ERROR,
22065184Sek110237 		    "Failed to write %d bytes on fd %d: %s",
22075673Saw148015 		    iosize, filedesc, strerror(errno));
22085673Saw148015 		flowop_endop(threadflow, flowop, 0);
2209*6084Saw148015 		return (FILEBENCH_ERROR);
22105184Sek110237 	}
22115673Saw148015 	flowop_endop(threadflow, flowop, iosize);
22125184Sek110237 
2213*6084Saw148015 	return (FILEBENCH_OK);
22145184Sek110237 }
22155184Sek110237 
22165184Sek110237 /*
22175184Sek110237  * Emulate a random size append to a file. Will append data
22185184Sek110237  * to a file chosen from a fileset if the flowop's fo_fileset
22195184Sek110237  * field specifies one or if its fdnumber is non zero. Otherwise
22205184Sek110237  * it will write to a fileobj file, if one exists. The flowop's
22215184Sek110237  * fo_wss parameter will be used to set the maximum file size
22225184Sek110237  * if it is non-zero, otherwise the filesetentry's fse_size
22235184Sek110237  * will be used.  A random transfer size (but at most fo_iosize
22245184Sek110237  * bytes) and a random memory offset are calculated. A logical
22255184Sek110237  * seek to the end of file is done, then writes of up to
22265184Sek110237  * FILE_ALLOC_BLOCK in size are done until the full transfer
22275184Sek110237  * size has been written. Writes are actually done from fo_buf,
22285184Sek110237  * rather than tf_mem as is done with flowoplib_write().
2229*6084Saw148015  * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of
2230*6084Saw148015  * files in the fileset, FILEBENCH_OK on success.
22315184Sek110237  */
22325184Sek110237 static int
22335184Sek110237 flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop)
22345184Sek110237 {
22355673Saw148015 	caddr_t iobuf;
22365184Sek110237 	uint64_t appendsize;
22375673Saw148015 	int filedesc;
22385184Sek110237 	vinteger_t wss;
2239*6084Saw148015 	int ret;
22405184Sek110237 
22415673Saw148015 	if (filebench_randomno64(&appendsize, *flowop->fo_iosize, 1LL) != 0)
2242*6084Saw148015 		return (FILEBENCH_ERROR);
22435184Sek110237 
22445673Saw148015 	/* skip if attempting zero length append */
22455673Saw148015 	if (appendsize == 0) {
22465673Saw148015 		flowop_beginop(threadflow, flowop);
22475673Saw148015 		flowop_endop(threadflow, flowop, 0LL);
2248*6084Saw148015 		return (FILEBENCH_OK);
22495673Saw148015 	}
22505184Sek110237 
2251*6084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
2252*6084Saw148015 	    &filedesc, appendsize)) != FILEBENCH_OK)
2253*6084Saw148015 		return (ret);
22545673Saw148015 
22555184Sek110237 	/* XXX wss is not being used */
22565184Sek110237 
22575673Saw148015 	/* Measure time to write bytes */
22585673Saw148015 	flowop_beginop(threadflow, flowop);
22595673Saw148015 
22605673Saw148015 	(void) lseek64(filedesc, 0, SEEK_END);
22615673Saw148015 	ret = write(filedesc, iobuf, appendsize);
22625673Saw148015 	if (ret != appendsize) {
22635673Saw148015 		filebench_log(LOG_ERROR,
22645673Saw148015 		    "Failed to write %d bytes on fd %d: %s",
22655673Saw148015 		    appendsize, filedesc, strerror(errno));
22665673Saw148015 		flowop_endop(threadflow, flowop, 0);
2267*6084Saw148015 		return (FILEBENCH_ERROR);
22685184Sek110237 	}
22695184Sek110237 
22705673Saw148015 	flowop_endop(threadflow, flowop, appendsize);
22715184Sek110237 
2272*6084Saw148015 	return (FILEBENCH_OK);
22735184Sek110237 }
22745184Sek110237 
22755184Sek110237 
22765184Sek110237 /*
22775184Sek110237  * Prints usage information for flowop operations.
22785184Sek110237  */
22795184Sek110237 void
22805184Sek110237 flowoplib_usage()
22815184Sek110237 {
22825184Sek110237 	(void) fprintf(stderr,
22835184Sek110237 	    "flowop [openfile|createfile] name=<name>,fileset=<fname>\n");
22845184Sek110237 	(void) fprintf(stderr,
22855184Sek110237 	    "                       [,fd=<file desc num>]\n");
22865184Sek110237 	(void) fprintf(stderr, "\n");
22875184Sek110237 	(void) fprintf(stderr,
22885184Sek110237 	    "flowop closefile name=<name>,fd=<file desc num>]\n");
22895184Sek110237 	(void) fprintf(stderr, "\n");
22905184Sek110237 	(void) fprintf(stderr, "flowop deletefile name=<name>\n");
22915184Sek110237 	(void) fprintf(stderr, "                       [,fileset=<fname>]\n");
22925184Sek110237 	(void) fprintf(stderr,
22935184Sek110237 	    "                       [,fd=<file desc num>]\n");
22945184Sek110237 	(void) fprintf(stderr, "\n");
22955184Sek110237 	(void) fprintf(stderr, "flowop statfile name=<name>\n");
22965184Sek110237 	(void) fprintf(stderr, "                       [,fileset=<fname>]\n");
22975184Sek110237 	(void) fprintf(stderr,
22985184Sek110237 	    "                       [,fd=<file desc num>]\n");
22995184Sek110237 	(void) fprintf(stderr, "\n");
23005184Sek110237 	(void) fprintf(stderr,
23015184Sek110237 	    "flowop fsync name=<name>,fd=<file desc num>]\n");
23025184Sek110237 	(void) fprintf(stderr, "\n");
23035184Sek110237 	(void) fprintf(stderr,
23045184Sek110237 	    "flowop fsyncset name=<name>,fileset=<fname>]\n");
23055184Sek110237 	(void) fprintf(stderr, "\n");
23065184Sek110237 	(void) fprintf(stderr, "flowop [write|read|aiowrite] name=<name>, \n");
23075184Sek110237 	(void) fprintf(stderr,
23085184Sek110237 	    "                       filename|fileset=<fname>,\n");
23095184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
23105184Sek110237 	(void) fprintf(stderr, "                       [,directio]\n");
23115184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
23125184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
23135184Sek110237 	(void) fprintf(stderr, "                       [,random]\n");
23145184Sek110237 	(void) fprintf(stderr, "                       [,opennext]\n");
23155184Sek110237 	(void) fprintf(stderr, "                       [,workingset=<size>]\n");
23165184Sek110237 	(void) fprintf(stderr,
23175184Sek110237 	    "flowop [appendfile|appendfilerand] name=<name>, \n");
23185184Sek110237 	(void) fprintf(stderr,
23195184Sek110237 	    "                       filename|fileset=<fname>,\n");
23205184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
23215184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
23225184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
23235184Sek110237 	(void) fprintf(stderr, "                       [,workingset=<size>]\n");
23245184Sek110237 	(void) fprintf(stderr,
23255184Sek110237 	    "flowop [readwholefile|writewholefile] name=<name>, \n");
23265184Sek110237 	(void) fprintf(stderr,
23275184Sek110237 	    "                       filename|fileset=<fname>,\n");
23285184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
23295184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
23305184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
23315184Sek110237 	(void) fprintf(stderr, "\n");
23325184Sek110237 	(void) fprintf(stderr, "flowop aiowait name=<name>,target="
23335184Sek110237 	    "<aiowrite-flowop>\n");
23345184Sek110237 	(void) fprintf(stderr, "\n");
23355184Sek110237 	(void) fprintf(stderr, "flowop sempost name=<name>,"
23365184Sek110237 	    "target=<semblock-flowop>,\n");
23375184Sek110237 	(void) fprintf(stderr,
23385184Sek110237 	    "                       value=<increment-to-post>\n");
23395184Sek110237 	(void) fprintf(stderr, "\n");
23405184Sek110237 	(void) fprintf(stderr, "flowop semblock name=<name>,value="
23415184Sek110237 	    "<decrement-to-receive>,\n");
23425184Sek110237 	(void) fprintf(stderr, "                       highwater="
23435184Sek110237 	    "<inbound-queue-max>\n");
23445184Sek110237 	(void) fprintf(stderr, "\n");
23455184Sek110237 	(void) fprintf(stderr, "flowop block name=<name>\n");
23465184Sek110237 	(void) fprintf(stderr, "\n");
23475184Sek110237 	(void) fprintf(stderr,
23485184Sek110237 	    "flowop wakeup name=<name>,target=<block-flowop>,\n");
23495184Sek110237 	(void) fprintf(stderr, "\n");
23505184Sek110237 	(void) fprintf(stderr,
23515184Sek110237 	    "flowop hog name=<name>,value=<number-of-mem-ops>\n");
23525184Sek110237 	(void) fprintf(stderr,
23535184Sek110237 	    "flowop delay name=<name>,value=<number-of-seconds>\n");
23545184Sek110237 	(void) fprintf(stderr, "\n");
23555184Sek110237 	(void) fprintf(stderr, "flowop eventlimit name=<name>\n");
23565184Sek110237 	(void) fprintf(stderr, "flowop bwlimit name=<name>,value=<mb/s>\n");
23575184Sek110237 	(void) fprintf(stderr, "flowop iopslimit name=<name>,value=<iop/s>\n");
23585184Sek110237 	(void) fprintf(stderr,
23595184Sek110237 	    "flowop finishoncount name=<name>,value=<ops/s>\n");
23605184Sek110237 	(void) fprintf(stderr,
23615184Sek110237 	    "flowop finishonbytes name=<name>,value=<bytes>\n");
23625184Sek110237 	(void) fprintf(stderr, "\n");
23635184Sek110237 	(void) fprintf(stderr, "\n");
23645184Sek110237 }
2365