xref: /onnv-gate/usr/src/cmd/filebench/common/flowop_library.c (revision 6391:f317d2de8920)
15184Sek110237 /*
25184Sek110237  * CDDL HEADER START
35184Sek110237  *
45184Sek110237  * The contents of this file are subject to the terms of the
55184Sek110237  * Common Development and Distribution License (the "License").
65184Sek110237  * You may not use this file except in compliance with the License.
75184Sek110237  *
85184Sek110237  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95184Sek110237  * or http://www.opensolaris.org/os/licensing.
105184Sek110237  * See the License for the specific language governing permissions
115184Sek110237  * and limitations under the License.
125184Sek110237  *
135184Sek110237  * When distributing Covered Code, include this CDDL HEADER in each
145184Sek110237  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155184Sek110237  * If applicable, add the following below this CDDL HEADER, with the
165184Sek110237  * fields enclosed by brackets "[]" replaced with your own identifying
175184Sek110237  * information: Portions Copyright [yyyy] [name of copyright owner]
185184Sek110237  *
195184Sek110237  * CDDL HEADER END
205184Sek110237  */
215184Sek110237 /*
226084Saw148015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235184Sek110237  * Use is subject to license terms.
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>
406212Saw148015 #include <math.h>
415184Sek110237 
425184Sek110237 #ifdef HAVE_UTILITY_H
435184Sek110237 #include <utility.h>
445184Sek110237 #endif /* HAVE_UTILITY_H */
455184Sek110237 
465184Sek110237 #ifdef HAVE_AIO
475184Sek110237 #include <aio.h>
485184Sek110237 #endif /* HAVE_AIO */
495184Sek110237 
505184Sek110237 #ifdef HAVE_LIBAIO_H
515184Sek110237 #include <libaio.h>
525184Sek110237 #endif /* HAVE_LIBAIO_H */
535184Sek110237 
545184Sek110237 #ifdef HAVE_SYS_ASYNC_H
555184Sek110237 #include <sys/asynch.h>
565184Sek110237 #endif /* HAVE_SYS_ASYNC_H */
575184Sek110237 
585184Sek110237 #ifdef HAVE_AIO_H
595184Sek110237 #include <aio.h>
605184Sek110237 #endif /* HAVE_AIO_H */
615184Sek110237 
625184Sek110237 #ifndef HAVE_UINT_T
635184Sek110237 #define	uint_t unsigned int
645184Sek110237 #endif /* HAVE_UINT_T */
655184Sek110237 
665184Sek110237 #ifndef HAVE_AIOCB64_T
675184Sek110237 #define	aiocb64 aiocb
685184Sek110237 #endif /* HAVE_AIOCB64_T */
695184Sek110237 
705184Sek110237 #ifndef HAVE_SYSV_SEM
715184Sek110237 #include <semaphore.h>
725184Sek110237 #endif /* HAVE_SYSV_SEM */
735184Sek110237 
745184Sek110237 #include "filebench.h"
755184Sek110237 #include "flowop.h"
765184Sek110237 #include "fileset.h"
776212Saw148015 #include "fb_random.h"
785184Sek110237 
795184Sek110237 /*
805184Sek110237  * These routines implement the flowops from the f language. Each
815184Sek110237  * flowop has has a name such as "read", and a set of function pointers
825184Sek110237  * to call for initialization, execution and destruction of the flowop.
835184Sek110237  * The table flowoplib_funcs[] contains a flowoplib struct for each
845184Sek110237  * implemented flowop. Most flowops use a generic initialization function
855184Sek110237  * and all currently use a generic destruction function. All flowop
865184Sek110237  * functions referenced from the table are in this file, though, of
875184Sek110237  * course, they often call functions from other files.
885184Sek110237  *
895184Sek110237  * The flowop_init() routine uses the flowoplib_funcs[] table to
905184Sek110237  * create an initial set of "instance 0" flowops, one for each type of
915184Sek110237  * flowop, from which all other flowops are derived. These "instance 0"
925184Sek110237  * flowops are initialized with information from the table including
935184Sek110237  * pointers for their fo_init, fo_func and fo_destroy functions. When
945184Sek110237  * a flowop definition is encountered in an f language script, the
955184Sek110237  * "type" of flowop, such as "read" is used to search for the
965184Sek110237  * "instance 0" flowop named "read", then a new flowop is allocated
975184Sek110237  * which inherits its function pointers and other initial properties
985184Sek110237  * from the instance 0 flowop, and is given a new name as specified
995184Sek110237  * by the "name=" attribute.
1005184Sek110237  */
1015184Sek110237 
1025184Sek110237 static int flowoplib_init_generic(flowop_t *flowop);
1035184Sek110237 static void flowoplib_destruct_generic(flowop_t *flowop);
1046084Saw148015 static void flowoplib_destruct_noop(flowop_t *flowop);
1055184Sek110237 static int flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop);
1065184Sek110237 static int flowoplib_write(threadflow_t *threadflow, flowop_t *flowop);
1075184Sek110237 #ifdef HAVE_AIO
1085184Sek110237 static int flowoplib_aiowrite(threadflow_t *threadflow, flowop_t *flowop);
1095184Sek110237 static int flowoplib_aiowait(threadflow_t *threadflow, flowop_t *flowop);
1105184Sek110237 #endif
1115184Sek110237 static int flowoplib_read(threadflow_t *threadflow, flowop_t *flowop);
1125184Sek110237 static int flowoplib_block_init(flowop_t *flowop);
1135184Sek110237 static int flowoplib_block(threadflow_t *threadflow, flowop_t *flowop);
1145184Sek110237 static int flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop);
1155184Sek110237 static int flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop);
1165184Sek110237 static int flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop);
1175184Sek110237 static int flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop);
1185184Sek110237 static int flowoplib_sempost_init(flowop_t *flowop);
1195184Sek110237 static int flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop);
1205184Sek110237 static int flowoplib_semblock_init(flowop_t *flowop);
1215184Sek110237 static void flowoplib_semblock_destruct(flowop_t *flowop);
1225184Sek110237 static int flowoplib_eventlimit(threadflow_t *, flowop_t *flowop);
1235184Sek110237 static int flowoplib_bwlimit(threadflow_t *, flowop_t *flowop);
1245184Sek110237 static int flowoplib_iopslimit(threadflow_t *, flowop_t *flowop);
1255184Sek110237 static int flowoplib_opslimit(threadflow_t *, flowop_t *flowop);
1265184Sek110237 static int flowoplib_openfile(threadflow_t *, flowop_t *flowop);
1275184Sek110237 static int flowoplib_openfile_common(threadflow_t *, flowop_t *flowop, int fd);
1285184Sek110237 static int flowoplib_createfile(threadflow_t *, flowop_t *flowop);
1295184Sek110237 static int flowoplib_closefile(threadflow_t *, flowop_t *flowop);
1305184Sek110237 static int flowoplib_fsync(threadflow_t *, flowop_t *flowop);
1315184Sek110237 static int flowoplib_readwholefile(threadflow_t *, flowop_t *flowop);
1325184Sek110237 static int flowoplib_writewholefile(threadflow_t *, flowop_t *flowop);
1335184Sek110237 static int flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop);
1345184Sek110237 static int flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop);
1355184Sek110237 static int flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop);
1365184Sek110237 static int flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop);
1375184Sek110237 static int flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop);
1385184Sek110237 static int flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop);
1395184Sek110237 static int flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop);
1406212Saw148015 static int flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop);
1416212Saw148015 static int flowoplib_testrandvar_init(flowop_t *flowop);
1426212Saw148015 static void flowoplib_testrandvar_destruct(flowop_t *flowop);
1435184Sek110237 
1445184Sek110237 typedef struct flowoplib {
1455184Sek110237 	int	fl_type;
1465184Sek110237 	int	fl_attrs;
1475184Sek110237 	char	*fl_name;
1485184Sek110237 	int	(*fl_init)();
1495184Sek110237 	int	(*fl_func)();
1505184Sek110237 	void	(*fl_destruct)();
1515184Sek110237 } flowoplib_t;
1525184Sek110237 
1535184Sek110237 static flowoplib_t flowoplib_funcs[] = {
1545184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "write", flowoplib_init_generic,
1555184Sek110237 	flowoplib_write, flowoplib_destruct_generic,
1565184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_READ, "read", flowoplib_init_generic,
1575184Sek110237 	flowoplib_read, flowoplib_destruct_generic,
1585184Sek110237 #ifdef HAVE_AIO
1595184Sek110237 	FLOW_TYPE_AIO, FLOW_ATTR_WRITE, "aiowrite", flowoplib_init_generic,
1605184Sek110237 	flowoplib_aiowrite, flowoplib_destruct_generic,
1615184Sek110237 	FLOW_TYPE_AIO, 0, "aiowait", flowoplib_init_generic,
1625184Sek110237 	flowoplib_aiowait, flowoplib_destruct_generic,
1635184Sek110237 #endif
1645184Sek110237 	FLOW_TYPE_SYNC, 0, "block", flowoplib_block_init,
1655184Sek110237 	flowoplib_block, flowoplib_destruct_generic,
1665184Sek110237 	FLOW_TYPE_SYNC, 0, "wakeup", flowoplib_init_generic,
1675184Sek110237 	flowoplib_wakeup, flowoplib_destruct_generic,
1685184Sek110237 	FLOW_TYPE_SYNC, 0, "semblock", flowoplib_semblock_init,
1695184Sek110237 	flowoplib_semblock, flowoplib_semblock_destruct,
1705184Sek110237 	FLOW_TYPE_SYNC, 0, "sempost", flowoplib_sempost_init,
1716084Saw148015 	flowoplib_sempost, flowoplib_destruct_noop,
1725184Sek110237 	FLOW_TYPE_OTHER, 0, "hog", flowoplib_init_generic,
1735184Sek110237 	flowoplib_hog, flowoplib_destruct_generic,
1745184Sek110237 	FLOW_TYPE_OTHER, 0, "delay", flowoplib_init_generic,
1755184Sek110237 	flowoplib_delay, flowoplib_destruct_generic,
1765184Sek110237 	FLOW_TYPE_OTHER, 0, "eventlimit", flowoplib_init_generic,
1775184Sek110237 	flowoplib_eventlimit, flowoplib_destruct_generic,
1785184Sek110237 	FLOW_TYPE_OTHER, 0, "bwlimit", flowoplib_init_generic,
1795184Sek110237 	flowoplib_bwlimit, flowoplib_destruct_generic,
1805184Sek110237 	FLOW_TYPE_OTHER, 0, "iopslimit", flowoplib_init_generic,
1815184Sek110237 	flowoplib_iopslimit, flowoplib_destruct_generic,
1825184Sek110237 	FLOW_TYPE_OTHER, 0, "opslimit", flowoplib_init_generic,
1835184Sek110237 	flowoplib_opslimit, flowoplib_destruct_generic,
1845184Sek110237 	FLOW_TYPE_OTHER, 0, "finishoncount", flowoplib_init_generic,
1855184Sek110237 	flowoplib_finishoncount, flowoplib_destruct_generic,
1865184Sek110237 	FLOW_TYPE_OTHER, 0, "finishonbytes", flowoplib_init_generic,
1875184Sek110237 	flowoplib_finishonbytes, flowoplib_destruct_generic,
1885184Sek110237 	FLOW_TYPE_IO, 0, "openfile", flowoplib_init_generic,
1895184Sek110237 	flowoplib_openfile, flowoplib_destruct_generic,
1905184Sek110237 	FLOW_TYPE_IO, 0, "createfile", flowoplib_init_generic,
1915184Sek110237 	flowoplib_createfile, flowoplib_destruct_generic,
1925184Sek110237 	FLOW_TYPE_IO, 0, "closefile", flowoplib_init_generic,
1935184Sek110237 	flowoplib_closefile, flowoplib_destruct_generic,
1945184Sek110237 	FLOW_TYPE_IO, 0, "fsync", flowoplib_init_generic,
1955184Sek110237 	flowoplib_fsync, flowoplib_destruct_generic,
1965184Sek110237 	FLOW_TYPE_IO, 0, "fsyncset", flowoplib_init_generic,
1975184Sek110237 	flowoplib_fsyncset, flowoplib_destruct_generic,
1985184Sek110237 	FLOW_TYPE_IO, 0, "statfile", flowoplib_init_generic,
1995184Sek110237 	flowoplib_statfile, flowoplib_destruct_generic,
2005184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_READ, "readwholefile", flowoplib_init_generic,
2015184Sek110237 	flowoplib_readwholefile, flowoplib_destruct_generic,
2025184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfile", flowoplib_init_generic,
2035184Sek110237 	flowoplib_appendfile, flowoplib_destruct_generic,
2045184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfilerand", flowoplib_init_generic,
2055184Sek110237 	flowoplib_appendfilerand, flowoplib_destruct_generic,
2065184Sek110237 	FLOW_TYPE_IO, 0, "deletefile", flowoplib_init_generic,
2075184Sek110237 	flowoplib_deletefile, flowoplib_destruct_generic,
2085184Sek110237 	FLOW_TYPE_IO, FLOW_ATTR_WRITE, "writewholefile", flowoplib_init_generic,
2096212Saw148015 	flowoplib_writewholefile, flowoplib_destruct_generic,
2106212Saw148015 	/* routine to calculate mean and stddev for output from a randvar */
2116212Saw148015 	FLOW_TYPE_OTHER, 0, "testrandvar", flowoplib_testrandvar_init,
2126212Saw148015 	flowoplib_testrandvar, flowoplib_testrandvar_destruct
2135184Sek110237 };
2145184Sek110237 
2155184Sek110237 /*
2165184Sek110237  * Loops through the master list of flowops defined in this
2175184Sek110237  * module, and creates and initializes a flowop for each one
2185184Sek110237  * by calling flowop_define. As a side effect of calling
2195184Sek110237  * flowop define, the created flowops are placed on the
2205184Sek110237  * master flowop list. All created flowops are set to
2215184Sek110237  * instance "0".
2225184Sek110237  */
2235184Sek110237 void
2245184Sek110237 flowoplib_init()
2255184Sek110237 {
2265184Sek110237 	int nops = sizeof (flowoplib_funcs) / sizeof (flowoplib_t);
2275184Sek110237 	int i;
2285184Sek110237 
2295184Sek110237 	for (i = 0; i < nops; i++) {
2305184Sek110237 		flowop_t *flowop;
2315184Sek110237 		flowoplib_t *fl;
2325184Sek110237 
2335184Sek110237 		fl = &flowoplib_funcs[i];
2345184Sek110237 
2355184Sek110237 		if ((flowop = flowop_define(NULL,
2365184Sek110237 		    fl->fl_name, NULL, 0, fl->fl_type)) == 0) {
2375184Sek110237 			filebench_log(LOG_ERROR,
2385184Sek110237 			    "failed to create flowop %s\n",
2395184Sek110237 			    fl->fl_name);
2405184Sek110237 			filebench_shutdown(1);
2415184Sek110237 		}
2425184Sek110237 
2435184Sek110237 		flowop->fo_func = fl->fl_func;
2445184Sek110237 		flowop->fo_init = fl->fl_init;
2455184Sek110237 		flowop->fo_destruct = fl->fl_destruct;
2465184Sek110237 		flowop->fo_attrs = fl->fl_attrs;
2475184Sek110237 	}
2485184Sek110237 }
2495184Sek110237 
2505184Sek110237 static int
2515184Sek110237 flowoplib_init_generic(flowop_t *flowop)
2525184Sek110237 {
2535184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
2546084Saw148015 	return (FILEBENCH_OK);
2555184Sek110237 }
2565184Sek110237 
2575184Sek110237 static void
2585184Sek110237 flowoplib_destruct_generic(flowop_t *flowop)
2595184Sek110237 {
2606084Saw148015 	char *buf;
2616084Saw148015 
2626084Saw148015 	/* release any local resources held by the flowop */
2636084Saw148015 	(void) ipc_mutex_lock(&flowop->fo_lock);
2646084Saw148015 	buf = flowop->fo_buf;
2656084Saw148015 	flowop->fo_buf = NULL;
2666084Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
2676084Saw148015 
2686084Saw148015 	if (buf)
2696084Saw148015 		free(buf);
2706084Saw148015 }
2716084Saw148015 
2726084Saw148015 /*
2736084Saw148015  * Special total noop destruct
2746084Saw148015  */
2756084Saw148015 /* ARGSUSED */
2766084Saw148015 static void
2776084Saw148015 flowoplib_destruct_noop(flowop_t *flowop)
2786084Saw148015 {
2795184Sek110237 }
2805184Sek110237 
2815184Sek110237 /*
2825184Sek110237  * Generates a file attribute from flags in the supplied flowop.
2835184Sek110237  * Sets FLOW_ATTR_DIRECTIO and/or FLOW_ATTR_DSYNC as needed.
2845184Sek110237  */
2855184Sek110237 static int
2865184Sek110237 flowoplib_fileattrs(flowop_t *flowop)
2875184Sek110237 {
2885184Sek110237 	int attrs = 0;
2895184Sek110237 
2906212Saw148015 	if (avd_get_bool(flowop->fo_directio))
2915184Sek110237 		attrs |= FLOW_ATTR_DIRECTIO;
2925184Sek110237 
2936212Saw148015 	if (avd_get_bool(flowop->fo_dsync))
2945184Sek110237 		attrs |= FLOW_ATTR_DSYNC;
2955184Sek110237 
2965184Sek110237 	return (attrs);
2975184Sek110237 }
2985184Sek110237 
2995184Sek110237 /*
3005184Sek110237  * Searches for a file descriptor. Tries the flowop's
3015184Sek110237  * fo_fdnumber first and returns with it if it has been
3025184Sek110237  * explicitly set (greater than 0). It next checks to
3035184Sek110237  * see if a rotating file descriptor policy is in effect,
3045184Sek110237  * and if not returns the fdnumber regardless of what
3055184Sek110237  * it is. (note that if it is 0, it just selects to the
3065184Sek110237  * default file descriptor in the threadflow's tf_fd
3075184Sek110237  * array). If the rotating fd policy is in effect, it
3085184Sek110237  * cycles from the end of the tf_fd array to one location
3095184Sek110237  * beyond the maximum needed by the number of entries in
3105184Sek110237  * the associated fileset on each invocation, then starts
3115184Sek110237  * over from the end.
3125184Sek110237  *
3135184Sek110237  * The routine returns an index into the threadflow's
3145184Sek110237  * tf_fd table where the actual file descriptor will be
3155184Sek110237  * found. Note: the calling routine must not call this
3165184Sek110237  * routine if the flowop does not have a fileset, and the
3175184Sek110237  * flowop's fo_fdnumber is zero and fo_rotatefd is
3185184Sek110237  * asserted, or an addressing fault may occur.
3195184Sek110237  */
3205673Saw148015 static int
3215184Sek110237 flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop)
3225184Sek110237 {
3236212Saw148015 	fbint_t	entries;
324*6391Saw148015 	int fdnumber = flowop->fo_fdnumber;
3256212Saw148015 
3265184Sek110237 	/* If the script sets the fd explicitly */
327*6391Saw148015 	if (fdnumber > 0)
328*6391Saw148015 		return (fdnumber);
3295184Sek110237 
3305184Sek110237 	/* If the flowop defaults to persistent fd */
3316212Saw148015 	if (!avd_get_bool(flowop->fo_rotatefd))
332*6391Saw148015 		return (fdnumber);
333*6391Saw148015 
334*6391Saw148015 	if (flowop->fo_fileset == NULL) {
335*6391Saw148015 		filebench_log(LOG_ERROR, "flowop NULL file");
336*6391Saw148015 		return (FILEBENCH_ERROR);
337*6391Saw148015 	}
3385184Sek110237 
3396212Saw148015 	entries = flowop->fo_fileset->fs_constentries;
3406212Saw148015 
3415184Sek110237 	/* Rotate the fd on each flowop invocation */
3426212Saw148015 	if (entries > (THREADFLOW_MAXFD / 2)) {
3435184Sek110237 		filebench_log(LOG_ERROR, "Out of file descriptors in flowop %s"
3446286Saw148015 		    " (too many files : %llu",
3456286Saw148015 		    flowop->fo_name, (u_longlong_t)entries);
3466084Saw148015 		return (FILEBENCH_ERROR);
3475184Sek110237 	}
3485184Sek110237 
3495184Sek110237 	/* First time around */
3505184Sek110237 	if (threadflow->tf_fdrotor == 0)
3515184Sek110237 		threadflow->tf_fdrotor = THREADFLOW_MAXFD;
3525184Sek110237 
3535184Sek110237 	/* One fd for every file in the set */
3546212Saw148015 	if (entries == (THREADFLOW_MAXFD - threadflow->tf_fdrotor))
3555184Sek110237 		threadflow->tf_fdrotor = THREADFLOW_MAXFD;
3565184Sek110237 
3575184Sek110237 
3585184Sek110237 	threadflow->tf_fdrotor--;
3595184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "selected fd = %d",
3605184Sek110237 	    threadflow->tf_fdrotor);
3615184Sek110237 	return (threadflow->tf_fdrotor);
3625184Sek110237 }
3635184Sek110237 
3645184Sek110237 /*
3655673Saw148015  * Determines the file descriptor to use, and attempts to open
3665673Saw148015  * the file if it is not already open. Also determines the wss
3676084Saw148015  * value. Returns FILEBENCH_ERROR on errors, FILESET_NORSC if
3686084Saw148015  * if flowop_openfile_common couldn't obtain an appropriate file
3696084Saw148015  * from a the fileset, and FILEBENCH_OK otherwise.
3705673Saw148015  */
3715673Saw148015 static int
3725673Saw148015 flowoplib_filesetup(threadflow_t *threadflow, flowop_t *flowop,
3736212Saw148015     fbint_t *wssp, int *filedescp)
3745673Saw148015 {
3755673Saw148015 	int fd = flowoplib_fdnum(threadflow, flowop);
3765673Saw148015 
3775673Saw148015 	if (fd == -1)
3786084Saw148015 		return (FILEBENCH_ERROR);
3795673Saw148015 
3805673Saw148015 	if (threadflow->tf_fd[fd] == 0) {
3816084Saw148015 		int ret;
3826084Saw148015 
3836084Saw148015 		if ((ret = flowoplib_openfile_common(
3846084Saw148015 		    threadflow, flowop, fd)) != FILEBENCH_OK)
3856084Saw148015 			return (ret);
3865673Saw148015 
3875673Saw148015 		if (threadflow->tf_fse[fd]) {
3885673Saw148015 			filebench_log(LOG_DEBUG_IMPL, "opened file %s",
3895673Saw148015 			    threadflow->tf_fse[fd]->fse_path);
3905673Saw148015 		} else {
3915673Saw148015 			filebench_log(LOG_DEBUG_IMPL,
3925673Saw148015 			    "opened device %s/%s",
3936212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_path),
3946212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_name));
3955673Saw148015 		}
3965673Saw148015 	}
3975673Saw148015 
3985673Saw148015 	*filedescp = threadflow->tf_fd[fd];
3995673Saw148015 
4006212Saw148015 	if ((*wssp = flowop->fo_constwss) == 0) {
4015673Saw148015 		if (threadflow->tf_fse[fd])
4025673Saw148015 			*wssp = threadflow->tf_fse[fd]->fse_size;
4035673Saw148015 		else
4046212Saw148015 			*wssp = avd_get_int(flowop->fo_fileset->fs_size);
4055673Saw148015 	}
4065673Saw148015 
4076084Saw148015 	return (FILEBENCH_OK);
4085673Saw148015 }
4095673Saw148015 
4105673Saw148015 /*
4115673Saw148015  * Determines the io buffer or random offset into tf_mem for
4126084Saw148015  * the IO operation. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
4135673Saw148015  */
4145673Saw148015 static int
4155673Saw148015 flowoplib_iobufsetup(threadflow_t *threadflow, flowop_t *flowop,
4166212Saw148015     caddr_t *iobufp, fbint_t iosize)
4175673Saw148015 {
4185673Saw148015 	long memsize;
4195673Saw148015 	size_t memoffset;
4205673Saw148015 
4215673Saw148015 	if (iosize == 0) {
4225673Saw148015 		filebench_log(LOG_ERROR, "zero iosize for thread %s",
4235673Saw148015 		    flowop->fo_name);
4246084Saw148015 		return (FILEBENCH_ERROR);
4255673Saw148015 	}
4265673Saw148015 
4276212Saw148015 	if ((memsize = threadflow->tf_constmemsize) != 0) {
4285673Saw148015 
4295673Saw148015 		/* use tf_mem for I/O with random offset */
4306212Saw148015 		if (filebench_randomno(&memoffset,
4316212Saw148015 		    memsize, iosize, NULL) == -1) {
4325673Saw148015 			filebench_log(LOG_ERROR,
4335673Saw148015 			    "tf_memsize smaller than IO size for thread %s",
4345673Saw148015 			    flowop->fo_name);
4356084Saw148015 			return (FILEBENCH_ERROR);
4365673Saw148015 		}
4375673Saw148015 		*iobufp = threadflow->tf_mem + memoffset;
4385673Saw148015 
4395673Saw148015 	} else {
4405673Saw148015 		/* use private I/O buffer */
4415673Saw148015 		if ((flowop->fo_buf != NULL) &&
4425673Saw148015 		    (flowop->fo_buf_size < iosize)) {
4436212Saw148015 			/* too small, so free up and re-allocate */
4445673Saw148015 			free(flowop->fo_buf);
4455673Saw148015 			flowop->fo_buf = NULL;
4465673Saw148015 		}
4476212Saw148015 
4486212Saw148015 		/*
4496212Saw148015 		 * Allocate memory for the  buffer. The memory is freed
4506212Saw148015 		 * by flowop_destruct_generic() or by this routine if more
4516212Saw148015 		 * memory is needed for the buffer.
4526212Saw148015 		 */
4535673Saw148015 		if ((flowop->fo_buf == NULL) && ((flowop->fo_buf
4545673Saw148015 		    = (char *)malloc(iosize)) == NULL))
4556084Saw148015 			return (FILEBENCH_ERROR);
4565673Saw148015 
4575673Saw148015 		flowop->fo_buf_size = iosize;
4585673Saw148015 		*iobufp = flowop->fo_buf;
4595673Saw148015 	}
4606084Saw148015 	return (FILEBENCH_OK);
4615673Saw148015 }
4625673Saw148015 
4635673Saw148015 /*
4645673Saw148015  * Determines the file descriptor to use, opens it if necessary, the
4655673Saw148015  * io buffer or random offset into tf_mem for IO operation and the wss
4666084Saw148015  * value. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
4675673Saw148015  */
4685673Saw148015 static int
4695673Saw148015 flowoplib_iosetup(threadflow_t *threadflow, flowop_t *flowop,
4706212Saw148015     fbint_t *wssp, caddr_t *iobufp, int *filedescp, fbint_t iosize)
4715673Saw148015 {
4726084Saw148015 	int ret;
4736084Saw148015 
4746084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, wssp, filedescp)) !=
4756084Saw148015 	    FILEBENCH_OK)
4766084Saw148015 		return (ret);
4775673Saw148015 
4786084Saw148015 	if ((ret = flowoplib_iobufsetup(threadflow, flowop, iobufp, iosize)) !=
4796084Saw148015 	    FILEBENCH_OK)
4806084Saw148015 		return (ret);
4815673Saw148015 
4826084Saw148015 	return (FILEBENCH_OK);
4835673Saw148015 }
4845673Saw148015 
4855673Saw148015 /*
4865184Sek110237  * Emulate posix read / pread. If the flowop has a fileset,
4875184Sek110237  * a file descriptor number index is fetched, otherwise a
4885184Sek110237  * supplied fileobj file is used. In either case the specified
4895184Sek110237  * file will be opened if not already open. If the flowop has
4906084Saw148015  * neither a fileset or fileobj, an error is logged and FILEBENCH_ERROR
4915184Sek110237  * returned.
4925184Sek110237  *
4935184Sek110237  * The actual read is done to a random offset in the
4945184Sek110237  * threadflow's thread memory (tf_mem), with a size set by
4955184Sek110237  * fo_iosize and at either a random disk offset within the
4965184Sek110237  * working set size, or at the next sequential location. If
4976084Saw148015  * any errors are encountered, FILEBENCH_ERROR is returned,
4986084Saw148015  * if no appropriate file can be obtained from the fileset then
4996084Saw148015  * FILEBENCH_NORSC is returned, otherise FILEBENCH_OK is returned.
5005184Sek110237  */
5015184Sek110237 static int
5025184Sek110237 flowoplib_read(threadflow_t *threadflow, flowop_t *flowop)
5035184Sek110237 {
5045673Saw148015 	caddr_t iobuf;
5056212Saw148015 	fbint_t wss;
5066212Saw148015 	fbint_t iosize;
5075184Sek110237 	int filedesc;
5085184Sek110237 	int ret;
5095184Sek110237 
5106212Saw148015 
5116212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
5126084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
5136212Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
5146084Saw148015 		return (ret);
5155184Sek110237 
5166212Saw148015 	if (avd_get_bool(flowop->fo_random)) {
5175184Sek110237 		uint64_t fileoffset;
5185184Sek110237 
5196212Saw148015 		if (filebench_randomno64(&fileoffset,
5206212Saw148015 		    wss, iosize, NULL) == -1) {
5215184Sek110237 			filebench_log(LOG_ERROR,
5225184Sek110237 			    "file size smaller than IO size for thread %s",
5235184Sek110237 			    flowop->fo_name);
5246084Saw148015 			return (FILEBENCH_ERROR);
5255184Sek110237 		}
5265184Sek110237 
5275184Sek110237 		(void) flowop_beginop(threadflow, flowop);
5285673Saw148015 		if ((ret = pread64(filedesc, iobuf,
5296212Saw148015 		    iosize, (off64_t)fileoffset)) == -1) {
5305673Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5315184Sek110237 			filebench_log(LOG_ERROR,
5326286Saw148015 			    "read file %s failed, offset %llu "
5335673Saw148015 			    "io buffer %zd: %s",
5346212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_name),
5356286Saw148015 			    (u_longlong_t)fileoffset, iobuf, strerror(errno));
5365673Saw148015 			flowop_endop(threadflow, flowop, 0);
5376084Saw148015 			return (FILEBENCH_ERROR);
5385184Sek110237 		}
5395673Saw148015 		(void) flowop_endop(threadflow, flowop, ret);
5405184Sek110237 
5415184Sek110237 		if ((ret == 0))
5425184Sek110237 			(void) lseek64(filedesc, 0, SEEK_SET);
5435184Sek110237 
5445184Sek110237 	} else {
5455184Sek110237 		(void) flowop_beginop(threadflow, flowop);
5466212Saw148015 		if ((ret = read(filedesc, iobuf, iosize)) == -1) {
5476212Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5485184Sek110237 			filebench_log(LOG_ERROR,
5495673Saw148015 			    "read file %s failed, io buffer %zd: %s",
5506212Saw148015 			    avd_get_str(flowop->fo_fileset->fs_name),
5515673Saw148015 			    iobuf, strerror(errno));
5525673Saw148015 			(void) flowop_endop(threadflow, flowop, 0);
5536084Saw148015 			return (FILEBENCH_ERROR);
5545184Sek110237 		}
5555673Saw148015 		(void) flowop_endop(threadflow, flowop, ret);
5565184Sek110237 
5575184Sek110237 		if ((ret == 0))
5585184Sek110237 			(void) lseek64(filedesc, 0, SEEK_SET);
5595184Sek110237 	}
5605184Sek110237 
5616084Saw148015 	return (FILEBENCH_OK);
5625184Sek110237 }
5635184Sek110237 
5645184Sek110237 #ifdef HAVE_AIO
5655184Sek110237 
5665184Sek110237 /*
5675184Sek110237  * Asynchronous write section. An Asynchronous IO element
5685184Sek110237  * (aiolist_t) is used to associate the asynchronous write request with
5695184Sek110237  * its subsequent completion. This element includes a aiocb64 struct
5705184Sek110237  * that is used by posix aio_xxx calls to track the asynchronous writes.
5715184Sek110237  * The flowops aiowrite and aiowait result in calls to these posix
5725184Sek110237  * aio_xxx system routines to do the actual asynchronous write IO
5735184Sek110237  * operations.
5745184Sek110237  */
5755184Sek110237 
5765184Sek110237 
5775184Sek110237 /*
5785184Sek110237  * Allocates an asynchronous I/O list (aio, of type
5795184Sek110237  * aiolist_t) element. Adds it to the flowop thread's
5805184Sek110237  * threadflow aio list. Returns a pointer to the element.
5815184Sek110237  */
5825184Sek110237 static aiolist_t *
5835184Sek110237 aio_allocate(flowop_t *flowop)
5845184Sek110237 {
5855184Sek110237 	aiolist_t *aiolist;
5865184Sek110237 
5875184Sek110237 	if ((aiolist = malloc(sizeof (aiolist_t))) == NULL) {
5885184Sek110237 		filebench_log(LOG_ERROR, "malloc aiolist failed");
5895184Sek110237 		filebench_shutdown(1);
5905184Sek110237 	}
5915184Sek110237 
5925184Sek110237 	/* Add to list */
5935184Sek110237 	if (flowop->fo_thread->tf_aiolist == NULL) {
5945184Sek110237 		flowop->fo_thread->tf_aiolist = aiolist;
5955184Sek110237 		aiolist->al_next = NULL;
5965184Sek110237 	} else {
5975184Sek110237 		aiolist->al_next = flowop->fo_thread->tf_aiolist;
5985184Sek110237 		flowop->fo_thread->tf_aiolist = aiolist;
5995184Sek110237 	}
6005184Sek110237 	return (aiolist);
6015184Sek110237 }
6025184Sek110237 
6035184Sek110237 /*
6045184Sek110237  * Searches for the aiolist element that has a matching
6056084Saw148015  * completion block, aiocb. If none found returns FILEBENCH_ERROR. If
6065184Sek110237  * found, removes the aiolist element from flowop thread's
6076084Saw148015  * list and returns FILEBENCH_OK.
6085184Sek110237  */
6095184Sek110237 static int
6105184Sek110237 aio_deallocate(flowop_t *flowop, struct aiocb64 *aiocb)
6115184Sek110237 {
6125184Sek110237 	aiolist_t *aiolist = flowop->fo_thread->tf_aiolist;
6135184Sek110237 	aiolist_t *previous = NULL;
6145184Sek110237 	aiolist_t *match = NULL;
6155184Sek110237 
6165184Sek110237 	if (aiocb == NULL) {
6175184Sek110237 		filebench_log(LOG_ERROR, "null aiocb deallocate");
6186084Saw148015 		return (FILEBENCH_OK);
6195184Sek110237 	}
6205184Sek110237 
6215184Sek110237 	while (aiolist) {
6225184Sek110237 		if (aiocb == &(aiolist->al_aiocb)) {
6235184Sek110237 			match = aiolist;
6245184Sek110237 			break;
6255184Sek110237 		}
6265184Sek110237 		previous = aiolist;
6275184Sek110237 		aiolist = aiolist->al_next;
6285184Sek110237 	}
6295184Sek110237 
6305184Sek110237 	if (match == NULL)
6316084Saw148015 		return (FILEBENCH_ERROR);
6325184Sek110237 
6335184Sek110237 	/* Remove from the list */
6345184Sek110237 	if (previous)
6355184Sek110237 		previous->al_next = match->al_next;
6365184Sek110237 	else
6375184Sek110237 		flowop->fo_thread->tf_aiolist = match->al_next;
6385184Sek110237 
6396084Saw148015 	return (FILEBENCH_OK);
6405184Sek110237 }
6415184Sek110237 
6425184Sek110237 /*
6435184Sek110237  * Emulate posix aiowrite(). Determines which file to use,
6445184Sek110237  * either one file of a fileset, or the file associated
6455184Sek110237  * with a fileobj, allocates and fills an aiolist_t element
6465184Sek110237  * for the write, and issues the asynchronous write. This
6475184Sek110237  * operation is only valid for random IO, and returns an
6486084Saw148015  * error if the flowop is set for sequential IO. Returns
6496084Saw148015  * FILEBENCH_OK on success, FILEBENCH_NORSC if iosetup can't
6506084Saw148015  * obtain a file to open, and FILEBENCH_ERROR on any
6516084Saw148015  * encountered error.
6525184Sek110237  */
6535184Sek110237 static int
6545184Sek110237 flowoplib_aiowrite(threadflow_t *threadflow, flowop_t *flowop)
6555184Sek110237 {
6565673Saw148015 	caddr_t iobuf;
6576212Saw148015 	fbint_t wss;
6586212Saw148015 	fbint_t iosize;
6595184Sek110237 	int filedesc;
6606084Saw148015 	int ret;
6615184Sek110237 
6626212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
6636212Saw148015 
6646084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
6656212Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
6666084Saw148015 		return (ret);
6675184Sek110237 
6686212Saw148015 	if (avd_get_bool(flowop->fo_random)) {
6695184Sek110237 		uint64_t fileoffset;
6705184Sek110237 		struct aiocb64 *aiocb;
6715184Sek110237 		aiolist_t *aiolist;
6725184Sek110237 
6735184Sek110237 		if (filebench_randomno64(&fileoffset,
6746212Saw148015 		    wss, iosize, NULL) == -1) {
6755184Sek110237 			filebench_log(LOG_ERROR,
6765184Sek110237 			    "file size smaller than IO size for thread %s",
6775184Sek110237 			    flowop->fo_name);
6786084Saw148015 			return (FILEBENCH_ERROR);
6795184Sek110237 		}
6805184Sek110237 
6815184Sek110237 		aiolist = aio_allocate(flowop);
6825184Sek110237 		aiolist->al_type = AL_WRITE;
6835184Sek110237 		aiocb = &aiolist->al_aiocb;
6845184Sek110237 
6855184Sek110237 		aiocb->aio_fildes = filedesc;
6865673Saw148015 		aiocb->aio_buf = iobuf;
6876212Saw148015 		aiocb->aio_nbytes = (size_t)iosize;
6885184Sek110237 		aiocb->aio_offset = (off64_t)fileoffset;
6895184Sek110237 		aiocb->aio_reqprio = 0;
6905184Sek110237 
6915184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
6926286Saw148015 		    "aio fd=%d, bytes=%llu, offset=%llu",
6936286Saw148015 		    filedesc, (u_longlong_t)iosize, (u_longlong_t)fileoffset);
6945184Sek110237 
6955184Sek110237 		flowop_beginop(threadflow, flowop);
6965184Sek110237 		if (aio_write64(aiocb) < 0) {
6975184Sek110237 			filebench_log(LOG_ERROR, "aiowrite failed: %s",
6985184Sek110237 			    strerror(errno));
6995184Sek110237 			filebench_shutdown(1);
7005184Sek110237 		}
7016212Saw148015 		flowop_endop(threadflow, flowop, iosize);
7025184Sek110237 	} else {
7036084Saw148015 		return (FILEBENCH_ERROR);
7045184Sek110237 	}
7055184Sek110237 
7066084Saw148015 	return (FILEBENCH_OK);
7075184Sek110237 }
7085184Sek110237 
7095184Sek110237 
7105184Sek110237 
7115184Sek110237 #define	MAXREAP 4096
7125184Sek110237 
7135184Sek110237 /*
7145184Sek110237  * Emulate posix aiowait(). Waits for the completion of half the
7155184Sek110237  * outstanding asynchronous IOs, or a single IO, which ever is
7165184Sek110237  * larger. The routine will return after a sufficient number of
7175184Sek110237  * completed calls issued by any thread in the procflow have
7185184Sek110237  * completed, or a 1 second timout elapses. All completed
7195184Sek110237  * IO operations are deleted from the thread's aiolist.
7205184Sek110237  */
7215184Sek110237 static int
7225184Sek110237 flowoplib_aiowait(threadflow_t *threadflow, flowop_t *flowop)
7235184Sek110237 {
7245184Sek110237 	struct aiocb64 **worklist;
7255184Sek110237 	aiolist_t *aio = flowop->fo_thread->tf_aiolist;
7265184Sek110237 	int uncompleted = 0;
7275184Sek110237 
7285184Sek110237 	worklist = calloc(MAXREAP, sizeof (struct aiocb64 *));
7295184Sek110237 
7305184Sek110237 	/* Count the list of pending aios */
7315184Sek110237 	while (aio) {
7325184Sek110237 		uncompleted++;
7335184Sek110237 		aio = aio->al_next;
7345184Sek110237 	}
7355184Sek110237 
7365184Sek110237 	do {
7375184Sek110237 		uint_t ncompleted = 0;
7385184Sek110237 		uint_t todo;
7395184Sek110237 		struct timespec timeout;
7405184Sek110237 		int inprogress;
7415184Sek110237 		int i;
7425184Sek110237 
7435184Sek110237 		/* Wait for half of the outstanding requests */
7445184Sek110237 		timeout.tv_sec = 1;
7455184Sek110237 		timeout.tv_nsec = 0;
7465184Sek110237 
7475184Sek110237 		if (uncompleted > MAXREAP)
7485184Sek110237 			todo = MAXREAP;
7495184Sek110237 		else
7505184Sek110237 			todo = uncompleted / 2;
7515184Sek110237 
7525184Sek110237 		if (todo == 0)
7535184Sek110237 			todo = 1;
7545184Sek110237 
7555184Sek110237 		flowop_beginop(threadflow, flowop);
7565184Sek110237 
7575184Sek110237 #ifdef HAVE_AIOWAITN
7585184Sek110237 		if ((aio_waitn64((struct aiocb64 **)worklist,
7595184Sek110237 		    MAXREAP, &todo, &timeout) == -1) &&
7605184Sek110237 		    errno && (errno != ETIME)) {
7615184Sek110237 			filebench_log(LOG_ERROR,
7625184Sek110237 			    "aiowait failed: %s, outstanding = %d, "
7635184Sek110237 			    "ncompleted = %d ",
7645184Sek110237 			    strerror(errno), uncompleted, todo);
7655184Sek110237 		}
7665184Sek110237 
7675184Sek110237 		ncompleted = todo;
7685184Sek110237 		/* Take the  completed I/Os from the list */
7695184Sek110237 		inprogress = 0;
7705184Sek110237 		for (i = 0; i < ncompleted; i++) {
7715184Sek110237 			if ((aio_return64(worklist[i]) == -1) &&
7725184Sek110237 			    (errno == EINPROGRESS)) {
7735184Sek110237 				inprogress++;
7745184Sek110237 				continue;
7755184Sek110237 			}
7765184Sek110237 			if (aio_deallocate(flowop, worklist[i]) < 0) {
7775184Sek110237 				filebench_log(LOG_ERROR, "Could not remove "
7785184Sek110237 				    "aio from list ");
7795673Saw148015 				flowop_endop(threadflow, flowop, 0);
7806084Saw148015 				return (FILEBENCH_ERROR);
7815184Sek110237 			}
7825184Sek110237 		}
7835184Sek110237 
7845184Sek110237 		uncompleted -= ncompleted;
7855184Sek110237 		uncompleted += inprogress;
7865184Sek110237 
7875184Sek110237 #else
7885184Sek110237 
7895184Sek110237 		for (ncompleted = 0, inprogress = 0,
7905184Sek110237 		    aio = flowop->fo_thread->tf_aiolist;
7915184Sek110237 		    ncompleted < todo, aio != NULL; aio = aio->al_next) {
7925184Sek110237 
7935184Sek110237 			result = aio_error64(&aio->al_aiocb);
7945184Sek110237 
7955184Sek110237 			if (result == EINPROGRESS) {
7965184Sek110237 				inprogress++;
7975184Sek110237 				continue;
7985184Sek110237 			}
7995184Sek110237 
8005184Sek110237 			if ((aio_return64(&aio->al_aiocb) == -1) || result) {
8015184Sek110237 				filebench_log(LOG_ERROR, "aio failed: %s",
8025184Sek110237 				    strerror(result));
8035184Sek110237 				continue;
8045184Sek110237 			}
8055184Sek110237 
8065184Sek110237 			ncompleted++;
8075184Sek110237 
8085184Sek110237 			if (aio_deallocate(flowop, &aio->al_aiocb) < 0) {
8095184Sek110237 				filebench_log(LOG_ERROR, "Could not remove aio "
8105184Sek110237 				    "from list ");
8115673Saw148015 				flowop_endop(threadflow, flowop, 0);
8126084Saw148015 				return (FILEBENCH_ERROR);
8135184Sek110237 			}
8145184Sek110237 		}
8155184Sek110237 
8165184Sek110237 		uncompleted -= ncompleted;
8175184Sek110237 
8185184Sek110237 #endif
8195184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT,
8205184Sek110237 		    "aio2 completed %d ios, uncompleted = %d, inprogress = %d",
8215184Sek110237 		    ncompleted, uncompleted, inprogress);
8225184Sek110237 
8235184Sek110237 	} while (uncompleted > MAXREAP);
8245184Sek110237 
8255673Saw148015 	flowop_endop(threadflow, flowop, 0);
8265184Sek110237 
8275184Sek110237 	free(worklist);
8285184Sek110237 
8296084Saw148015 	return (FILEBENCH_OK);
8305184Sek110237 }
8315184Sek110237 
8325184Sek110237 #endif /* HAVE_AIO */
8335184Sek110237 
8345184Sek110237 /*
8355184Sek110237  * Initializes a "flowop_block" flowop. Specifically, it
8365184Sek110237  * initializes the flowop's fo_cv and unlocks the fo_lock.
8375184Sek110237  */
8385184Sek110237 static int
8395184Sek110237 flowoplib_block_init(flowop_t *flowop)
8405184Sek110237 {
8415184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d block init address %zx",
8425184Sek110237 	    flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
8435184Sek110237 	(void) pthread_cond_init(&flowop->fo_cv, ipc_condattr());
8445184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
8455184Sek110237 
8466084Saw148015 	return (FILEBENCH_OK);
8475184Sek110237 }
8485184Sek110237 
8495184Sek110237 /*
8505184Sek110237  * Blocks the threadflow until woken up by flowoplib_wakeup.
8515184Sek110237  * The routine blocks on the flowop's fo_cv condition variable.
8525184Sek110237  */
8535184Sek110237 static int
8545184Sek110237 flowoplib_block(threadflow_t *threadflow, flowop_t *flowop)
8555184Sek110237 {
8565184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d blocking at address %zx",
8575184Sek110237 	    flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
8585184Sek110237 	(void) ipc_mutex_lock(&flowop->fo_lock);
8595184Sek110237 
8605184Sek110237 	flowop_beginop(threadflow, flowop);
8615184Sek110237 	(void) pthread_cond_wait(&flowop->fo_cv, &flowop->fo_lock);
8625673Saw148015 	flowop_endop(threadflow, flowop, 0);
8635184Sek110237 
8645184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
8655184Sek110237 	    flowop->fo_name, flowop->fo_instance);
8665184Sek110237 
8675184Sek110237 	(void) ipc_mutex_unlock(&flowop->fo_lock);
8685184Sek110237 
8696084Saw148015 	return (FILEBENCH_OK);
8705184Sek110237 }
8715184Sek110237 
8725184Sek110237 /*
8735184Sek110237  * Wakes up one or more target blocking flowops.
8745184Sek110237  * Sends broadcasts on the fo_cv condition variables of all
8755184Sek110237  * flowops on the target list, except those that are
8765184Sek110237  * FLOW_MASTER flowops. The target list consists of all
8775184Sek110237  * flowops whose name matches this flowop's "fo_targetname"
8785184Sek110237  * attribute. The target list is generated on the first
8795184Sek110237  * invocation, and the run will be shutdown if no targets
8806084Saw148015  * are found. Otherwise the routine always returns FILEBENCH_OK.
8815184Sek110237  */
8825184Sek110237 static int
8835184Sek110237 flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop)
8845184Sek110237 {
8855184Sek110237 	flowop_t *target;
8865184Sek110237 
8875184Sek110237 	/* if this is the first wakeup, create the wakeup list */
8885184Sek110237 	if (flowop->fo_targets == NULL) {
8895184Sek110237 		flowop_t *result = flowop_find(flowop->fo_targetname);
8905184Sek110237 
8915184Sek110237 		flowop->fo_targets = result;
8925184Sek110237 		if (result == NULL) {
8935184Sek110237 			filebench_log(LOG_ERROR,
8945184Sek110237 			    "wakeup: could not find op %s for thread %s",
8955184Sek110237 			    flowop->fo_targetname,
8965184Sek110237 			    threadflow->tf_name);
8975184Sek110237 			filebench_shutdown(1);
8985184Sek110237 		}
8995184Sek110237 		while (result) {
9005184Sek110237 			result->fo_targetnext =
9015184Sek110237 			    result->fo_resultnext;
9025184Sek110237 			result = result->fo_resultnext;
9035184Sek110237 		}
9045184Sek110237 	}
9055184Sek110237 
9065184Sek110237 	target = flowop->fo_targets;
9075184Sek110237 
9085184Sek110237 	/* wakeup the targets */
9095184Sek110237 	while (target) {
9105184Sek110237 		if (target->fo_instance == FLOW_MASTER) {
9115184Sek110237 			target = target->fo_targetnext;
9125184Sek110237 			continue;
9135184Sek110237 		}
9145184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
9155184Sek110237 		    "wakeup flow %s-%d at address %zx",
9165184Sek110237 		    target->fo_name,
9175184Sek110237 		    target->fo_instance,
9185184Sek110237 		    &target->fo_cv);
9195184Sek110237 
9205184Sek110237 		flowop_beginop(threadflow, flowop);
9215184Sek110237 		(void) ipc_mutex_lock(&target->fo_lock);
9225184Sek110237 		(void) pthread_cond_broadcast(&target->fo_cv);
9235184Sek110237 		(void) ipc_mutex_unlock(&target->fo_lock);
9245673Saw148015 		flowop_endop(threadflow, flowop, 0);
9255184Sek110237 
9265184Sek110237 		target = target->fo_targetnext;
9275184Sek110237 	}
9285184Sek110237 
9296084Saw148015 	return (FILEBENCH_OK);
9305184Sek110237 }
9315184Sek110237 
9325184Sek110237 /*
9335184Sek110237  * "think time" routines. the "hog" routine consumes cpu cycles as
9345184Sek110237  * it "thinks", while the "delay" flowop simply calls sleep() to delay
9355184Sek110237  * for a given number of seconds without consuming cpu cycles.
9365184Sek110237  */
9375184Sek110237 
9385184Sek110237 
9395184Sek110237 /*
9405184Sek110237  * Consumes CPU cycles and memory bandwidth by looping for
9415184Sek110237  * flowop->fo_value times. With each loop sets memory location
9425184Sek110237  * threadflow->tf_mem to 1.
9435184Sek110237  */
9445184Sek110237 static int
9455184Sek110237 flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop)
9465184Sek110237 {
9476212Saw148015 	uint64_t value = avd_get_int(flowop->fo_value);
9485184Sek110237 	int i;
9495184Sek110237 
9505673Saw148015 	filebench_log(LOG_DEBUG_IMPL, "hog enter");
9515184Sek110237 	flowop_beginop(threadflow, flowop);
9525673Saw148015 	if (threadflow->tf_mem != NULL) {
9535673Saw148015 		for (i = 0; i < value; i++)
9545673Saw148015 			*(threadflow->tf_mem) = 1;
9555673Saw148015 	}
9565673Saw148015 	flowop_endop(threadflow, flowop, 0);
9575184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "hog exit");
9586084Saw148015 	return (FILEBENCH_OK);
9595184Sek110237 }
9605184Sek110237 
9615184Sek110237 
9625184Sek110237 /*
9635184Sek110237  * Delays for fo_value seconds.
9645184Sek110237  */
9655184Sek110237 static int
9665184Sek110237 flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop)
9675184Sek110237 {
9686212Saw148015 	int value = avd_get_int(flowop->fo_value);
9695184Sek110237 
9705184Sek110237 	flowop_beginop(threadflow, flowop);
9715184Sek110237 	(void) sleep(value);
9725673Saw148015 	flowop_endop(threadflow, flowop, 0);
9736084Saw148015 	return (FILEBENCH_OK);
9745184Sek110237 }
9755184Sek110237 
9765184Sek110237 /*
9775184Sek110237  * Rate limiting routines. This is the event consuming half of the
9785184Sek110237  * event system. Each of the four following routines will limit the rate
9795184Sek110237  * to one unit of either calls, issued I/O operations, issued filebench
9805184Sek110237  * operations, or I/O bandwidth. Since there is only one event generator,
9815184Sek110237  * the events will be divided amoung multiple instances of an event
9825184Sek110237  * consumer, and further divided among different consumers if more than
9835184Sek110237  * one has been defined. There is no mechanism to enforce equal sharing
9845184Sek110237  * of events.
9855184Sek110237  */
9865184Sek110237 
9875184Sek110237 /*
9885184Sek110237  * Completes one invocation per posted event. If eventgen_q
9895184Sek110237  * has an event count greater than zero, one will be removed
9905184Sek110237  * (count decremented), otherwise the calling thread will
9915184Sek110237  * block until another event has been posted. Always returns 0
9925184Sek110237  */
9935184Sek110237 static int
9945184Sek110237 flowoplib_eventlimit(threadflow_t *threadflow, flowop_t *flowop)
9955184Sek110237 {
9965184Sek110237 	/* Immediately bail if not set/enabled */
997*6391Saw148015 	if (filebench_shm->shm_eventgen_hz == 0)
9986084Saw148015 		return (FILEBENCH_OK);
9995184Sek110237 
10005184Sek110237 	if (flowop->fo_initted == 0) {
10015184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
10025184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
10035184Sek110237 		flowop->fo_initted = 1;
10045184Sek110237 	}
10055184Sek110237 
10065184Sek110237 	flowop_beginop(threadflow, flowop);
1007*6391Saw148015 	while (filebench_shm->shm_eventgen_hz) {
1008*6391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
1009*6391Saw148015 		if (filebench_shm->shm_eventgen_q > 0) {
1010*6391Saw148015 			filebench_shm->shm_eventgen_q--;
1011*6391Saw148015 			(void) ipc_mutex_unlock(
1012*6391Saw148015 			    &filebench_shm->shm_eventgen_lock);
10135184Sek110237 			break;
10145184Sek110237 		}
1015*6391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
1016*6391Saw148015 		    &filebench_shm->shm_eventgen_lock);
1017*6391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
10185184Sek110237 	}
10195673Saw148015 	flowop_endop(threadflow, flowop, 0);
10206084Saw148015 	return (FILEBENCH_OK);
10215184Sek110237 }
10225184Sek110237 
10235184Sek110237 /*
10245184Sek110237  * Blocks the calling thread if the number of issued I/O
10255184Sek110237  * operations exceeds the number of posted events, thus
10265184Sek110237  * limiting the average I/O operation rate to the rate
10276084Saw148015  * specified by eventgen_hz. Always returns FILEBENCH_OK.
10285184Sek110237  */
10295184Sek110237 static int
10305184Sek110237 flowoplib_iopslimit(threadflow_t *threadflow, flowop_t *flowop)
10315184Sek110237 {
10325184Sek110237 	uint64_t iops;
10335184Sek110237 	uint64_t delta;
10345673Saw148015 	uint64_t events;
10355184Sek110237 
10365184Sek110237 	/* Immediately bail if not set/enabled */
1037*6391Saw148015 	if (filebench_shm->shm_eventgen_hz == 0)
10386084Saw148015 		return (FILEBENCH_OK);
10395184Sek110237 
10405184Sek110237 	if (flowop->fo_initted == 0) {
10415184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
10425184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
10435184Sek110237 		flowop->fo_initted = 1;
10445184Sek110237 	}
10455184Sek110237 
10466212Saw148015 	(void) ipc_mutex_lock(&controlstats_lock);
10475184Sek110237 	iops = (controlstats.fs_rcount +
10485184Sek110237 	    controlstats.fs_wcount);
10496212Saw148015 	(void) ipc_mutex_unlock(&controlstats_lock);
10505184Sek110237 
10515184Sek110237 	/* Is this the first time around */
10525184Sek110237 	if (flowop->fo_tputlast == 0) {
10535184Sek110237 		flowop->fo_tputlast = iops;
10546084Saw148015 		return (FILEBENCH_OK);
10555184Sek110237 	}
10565184Sek110237 
10575184Sek110237 	delta = iops - flowop->fo_tputlast;
10585184Sek110237 	flowop->fo_tputbucket -= delta;
10595184Sek110237 	flowop->fo_tputlast = iops;
10605184Sek110237 
10615184Sek110237 	/* No need to block if the q isn't empty */
10625184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
10635673Saw148015 		flowop_endop(threadflow, flowop, 0);
10646084Saw148015 		return (FILEBENCH_OK);
10655184Sek110237 	}
10665184Sek110237 
10675184Sek110237 	iops = flowop->fo_tputbucket * -1;
10685184Sek110237 	events = iops;
10695184Sek110237 
10705184Sek110237 	flowop_beginop(threadflow, flowop);
1071*6391Saw148015 	while (filebench_shm->shm_eventgen_hz) {
10725184Sek110237 
1073*6391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
1074*6391Saw148015 		if (filebench_shm->shm_eventgen_q >= events) {
1075*6391Saw148015 			filebench_shm->shm_eventgen_q -= events;
1076*6391Saw148015 			(void) ipc_mutex_unlock(
1077*6391Saw148015 			    &filebench_shm->shm_eventgen_lock);
10785184Sek110237 			flowop->fo_tputbucket += events;
10795184Sek110237 			break;
10805184Sek110237 		}
1081*6391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
1082*6391Saw148015 		    &filebench_shm->shm_eventgen_lock);
1083*6391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
10845184Sek110237 	}
10855673Saw148015 	flowop_endop(threadflow, flowop, 0);
10865184Sek110237 
10876084Saw148015 	return (FILEBENCH_OK);
10885184Sek110237 }
10895184Sek110237 
10905184Sek110237 /*
10915184Sek110237  * Blocks the calling thread if the number of issued filebench
10925184Sek110237  * operations exceeds the number of posted events, thus limiting
10935184Sek110237  * the average filebench operation rate to the rate specified by
10946084Saw148015  * eventgen_hz. Always returns FILEBENCH_OK.
10955184Sek110237  */
10965184Sek110237 static int
10975184Sek110237 flowoplib_opslimit(threadflow_t *threadflow, flowop_t *flowop)
10985184Sek110237 {
10995184Sek110237 	uint64_t ops;
11005184Sek110237 	uint64_t delta;
11015673Saw148015 	uint64_t events;
11025184Sek110237 
11035184Sek110237 	/* Immediately bail if not set/enabled */
1104*6391Saw148015 	if (filebench_shm->shm_eventgen_hz == 0)
11056084Saw148015 		return (FILEBENCH_OK);
11065184Sek110237 
11075184Sek110237 	if (flowop->fo_initted == 0) {
11085184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
11095184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
11105184Sek110237 		flowop->fo_initted = 1;
11115184Sek110237 	}
11125184Sek110237 
11136212Saw148015 	(void) ipc_mutex_lock(&controlstats_lock);
11145184Sek110237 	ops = controlstats.fs_count;
11156212Saw148015 	(void) ipc_mutex_unlock(&controlstats_lock);
11165184Sek110237 
11175184Sek110237 	/* Is this the first time around */
11185184Sek110237 	if (flowop->fo_tputlast == 0) {
11195184Sek110237 		flowop->fo_tputlast = ops;
11206084Saw148015 		return (FILEBENCH_OK);
11215184Sek110237 	}
11225184Sek110237 
11235184Sek110237 	delta = ops - flowop->fo_tputlast;
11245184Sek110237 	flowop->fo_tputbucket -= delta;
11255184Sek110237 	flowop->fo_tputlast = ops;
11265184Sek110237 
11275184Sek110237 	/* No need to block if the q isn't empty */
11285184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
11295673Saw148015 		flowop_endop(threadflow, flowop, 0);
11306084Saw148015 		return (FILEBENCH_OK);
11315184Sek110237 	}
11325184Sek110237 
11335184Sek110237 	ops = flowop->fo_tputbucket * -1;
11345184Sek110237 	events = ops;
11355184Sek110237 
11365184Sek110237 	flowop_beginop(threadflow, flowop);
1137*6391Saw148015 	while (filebench_shm->shm_eventgen_hz) {
1138*6391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
1139*6391Saw148015 		if (filebench_shm->shm_eventgen_q >= events) {
1140*6391Saw148015 			filebench_shm->shm_eventgen_q -= events;
1141*6391Saw148015 			(void) ipc_mutex_unlock(
1142*6391Saw148015 			    &filebench_shm->shm_eventgen_lock);
11435184Sek110237 			flowop->fo_tputbucket += events;
11445184Sek110237 			break;
11455184Sek110237 		}
1146*6391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
1147*6391Saw148015 		    &filebench_shm->shm_eventgen_lock);
1148*6391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
11495184Sek110237 	}
11505673Saw148015 	flowop_endop(threadflow, flowop, 0);
11515184Sek110237 
11526084Saw148015 	return (FILEBENCH_OK);
11535184Sek110237 }
11545184Sek110237 
11555184Sek110237 
11565184Sek110237 /*
11575184Sek110237  * Blocks the calling thread if the number of bytes of I/O
11585184Sek110237  * issued exceeds one megabyte times the number of posted
11595184Sek110237  * events, thus limiting the average I/O byte rate to one
11605184Sek110237  * megabyte times the event rate as set by eventgen_hz.
11616084Saw148015  * Always retuns FILEBENCH_OK.
11625184Sek110237  */
11635184Sek110237 static int
11645184Sek110237 flowoplib_bwlimit(threadflow_t *threadflow, flowop_t *flowop)
11655184Sek110237 {
11665184Sek110237 	uint64_t bytes;
11675184Sek110237 	uint64_t delta;
11685673Saw148015 	uint64_t events;
11695184Sek110237 
11705184Sek110237 	/* Immediately bail if not set/enabled */
1171*6391Saw148015 	if (filebench_shm->shm_eventgen_hz == 0)
11726084Saw148015 		return (FILEBENCH_OK);
11735184Sek110237 
11745184Sek110237 	if (flowop->fo_initted == 0) {
11755184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
11765184Sek110237 		    flowop, threadflow->tf_name, threadflow->tf_instance);
11775184Sek110237 		flowop->fo_initted = 1;
11785184Sek110237 	}
11795184Sek110237 
11806212Saw148015 	(void) ipc_mutex_lock(&controlstats_lock);
11815184Sek110237 	bytes = (controlstats.fs_rbytes +
11825184Sek110237 	    controlstats.fs_wbytes);
11836212Saw148015 	(void) ipc_mutex_unlock(&controlstats_lock);
11845184Sek110237 
11855184Sek110237 	/* Is this the first time around */
11865184Sek110237 	if (flowop->fo_tputlast == 0) {
11875184Sek110237 		flowop->fo_tputlast = bytes;
11886084Saw148015 		return (FILEBENCH_OK);
11895184Sek110237 	}
11905184Sek110237 
11915184Sek110237 	delta = bytes - flowop->fo_tputlast;
11925184Sek110237 	flowop->fo_tputbucket -= delta;
11935184Sek110237 	flowop->fo_tputlast = bytes;
11945184Sek110237 
11955184Sek110237 	/* No need to block if the q isn't empty */
11965184Sek110237 	if (flowop->fo_tputbucket >= 0LL) {
11975673Saw148015 		flowop_endop(threadflow, flowop, 0);
11986084Saw148015 		return (FILEBENCH_OK);
11995184Sek110237 	}
12005184Sek110237 
12015184Sek110237 	bytes = flowop->fo_tputbucket * -1;
12025184Sek110237 	events = (bytes / MB) + 1;
12035184Sek110237 
12046286Saw148015 	filebench_log(LOG_DEBUG_IMPL, "%llu bytes, %llu events",
12056286Saw148015 	    (u_longlong_t)bytes, (u_longlong_t)events);
12065184Sek110237 
12075184Sek110237 	flowop_beginop(threadflow, flowop);
1208*6391Saw148015 	while (filebench_shm->shm_eventgen_hz) {
1209*6391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
1210*6391Saw148015 		if (filebench_shm->shm_eventgen_q >= events) {
1211*6391Saw148015 			filebench_shm->shm_eventgen_q -= events;
1212*6391Saw148015 			(void) ipc_mutex_unlock(
1213*6391Saw148015 			    &filebench_shm->shm_eventgen_lock);
12145184Sek110237 			flowop->fo_tputbucket += (events * MB);
12155184Sek110237 			break;
12165184Sek110237 		}
1217*6391Saw148015 		(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
1218*6391Saw148015 		    &filebench_shm->shm_eventgen_lock);
1219*6391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
12205184Sek110237 	}
12215673Saw148015 	flowop_endop(threadflow, flowop, 0);
12225184Sek110237 
12236084Saw148015 	return (FILEBENCH_OK);
12245184Sek110237 }
12255184Sek110237 
12265184Sek110237 /*
12275184Sek110237  * These flowops terminate a benchmark run when either the specified
12285184Sek110237  * number of bytes of I/O (flowoplib_finishonbytes) or the specified
12295184Sek110237  * number of I/O operations (flowoplib_finishoncount) have been generated.
12305184Sek110237  */
12315184Sek110237 
12325184Sek110237 
12335184Sek110237 /*
12345184Sek110237  * Stop filebench run when specified number of I/O bytes have been
12356212Saw148015  * transferred. Compares controlstats.fs_bytes with flowop->value,
12365184Sek110237  * and if greater returns 1, stopping the run, if not, returns 0
12375184Sek110237  * to continue running.
12385184Sek110237  */
12395184Sek110237 static int
12405184Sek110237 flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop)
12415184Sek110237 {
12425184Sek110237 	uint64_t b;
12436212Saw148015 	uint64_t bytes = flowop->fo_constvalue; /* use constant value */
12445184Sek110237 
12456212Saw148015 	(void) ipc_mutex_lock(&controlstats_lock);
12465184Sek110237 	b = controlstats.fs_bytes;
12476212Saw148015 	(void) ipc_mutex_unlock(&controlstats_lock);
12485184Sek110237 
12495184Sek110237 	flowop_beginop(threadflow, flowop);
12505184Sek110237 	if (b > bytes) {
12515673Saw148015 		flowop_endop(threadflow, flowop, 0);
12526084Saw148015 		return (FILEBENCH_DONE);
12535184Sek110237 	}
12545673Saw148015 	flowop_endop(threadflow, flowop, 0);
12555184Sek110237 
12566084Saw148015 	return (FILEBENCH_OK);
12575184Sek110237 }
12585184Sek110237 
12595184Sek110237 /*
12605184Sek110237  * Stop filebench run when specified number of I/O operations have
12615184Sek110237  * been performed. Compares controlstats.fs_count with *flowop->value,
12626084Saw148015  * and if greater returns 1, stopping the run, if not, returns FILEBENCH_OK
12636084Saw148015  * to continue running.
12645184Sek110237  */
12655184Sek110237 static int
12665184Sek110237 flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop)
12675184Sek110237 {
12685184Sek110237 	uint64_t ops;
12696212Saw148015 	uint64_t count = flowop->fo_constvalue; /* use constant value */
12705184Sek110237 
12716212Saw148015 	(void) ipc_mutex_lock(&controlstats_lock);
12725184Sek110237 	ops = controlstats.fs_count;
12736212Saw148015 	(void) ipc_mutex_unlock(&controlstats_lock);
12745184Sek110237 
12755184Sek110237 	flowop_beginop(threadflow, flowop);
12766084Saw148015 	if (ops >= count) {
12775673Saw148015 		flowop_endop(threadflow, flowop, 0);
12786084Saw148015 		return (FILEBENCH_DONE);
12795184Sek110237 	}
12805673Saw148015 	flowop_endop(threadflow, flowop, 0);
12815184Sek110237 
12826084Saw148015 	return (FILEBENCH_OK);
12835184Sek110237 }
12845184Sek110237 
12855184Sek110237 /*
12865184Sek110237  * Semaphore synchronization using either System V semaphores or
12875184Sek110237  * posix semaphores. If System V semaphores are available, they will be
12885184Sek110237  * used, otherwise posix semaphores will be used.
12895184Sek110237  */
12905184Sek110237 
12915184Sek110237 
12925184Sek110237 /*
12935184Sek110237  * Initializes the filebench "block on semaphore" flowop.
12945184Sek110237  * If System V semaphores are implemented, the routine
12955184Sek110237  * initializes the System V semaphore subsystem if it hasn't
12965184Sek110237  * already been initialized, also allocates a pair of semids
12975184Sek110237  * and initializes the highwater System V semaphore.
12985184Sek110237  * If no System V semaphores, then does nothing special.
12996084Saw148015  * Returns FILEBENCH_ERROR if it cannot acquire a set of System V semphores
13006084Saw148015  * or if the initial post to the semaphore set fails. Returns FILEBENCH_OK
13015184Sek110237  * on success.
13025184Sek110237  */
13035184Sek110237 static int
13045184Sek110237 flowoplib_semblock_init(flowop_t *flowop)
13055184Sek110237 {
13065184Sek110237 
13075184Sek110237 #ifdef HAVE_SYSV_SEM
1308*6391Saw148015 	int sys_semid;
13095184Sek110237 	struct sembuf sbuf[2];
13105184Sek110237 	int highwater;
13115184Sek110237 
13125184Sek110237 	ipc_seminit();
13135184Sek110237 
13145184Sek110237 	flowop->fo_semid_lw = ipc_semidalloc();
13155184Sek110237 	flowop->fo_semid_hw = ipc_semidalloc();
13165184Sek110237 
13175184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d semblock init semid=%x",
13185184Sek110237 	    flowop->fo_name, flowop->fo_instance, flowop->fo_semid_lw);
13195184Sek110237 
1320*6391Saw148015 	sys_semid = filebench_shm->shm_sys_semid;
13215184Sek110237 
13225184Sek110237 	if ((highwater = flowop->fo_semid_hw) == 0)
13236212Saw148015 		highwater = flowop->fo_constvalue; /* use constant value */
13245184Sek110237 
13255184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "setting highwater to : %d", highwater);
13265184Sek110237 
13275673Saw148015 	sbuf[0].sem_num = (short)highwater;
13286212Saw148015 	sbuf[0].sem_op = avd_get_int(flowop->fo_highwater);
13295184Sek110237 	sbuf[0].sem_flg = 0;
1330*6391Saw148015 	if ((semop(sys_semid, &sbuf[0], 1) == -1) && errno) {
13315184Sek110237 		filebench_log(LOG_ERROR, "semblock init post failed: %s (%d,"
13325184Sek110237 		    "%d)", strerror(errno), sbuf[0].sem_num, sbuf[0].sem_op);
13336084Saw148015 		return (FILEBENCH_ERROR);
13345184Sek110237 	}
13355184Sek110237 #else
13365184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
13375184Sek110237 	    "flow %s-%d semblock init with posix semaphore",
13385184Sek110237 	    flowop->fo_name, flowop->fo_instance);
13395184Sek110237 
13405184Sek110237 	sem_init(&flowop->fo_sem, 1, 0);
13415184Sek110237 #endif	/* HAVE_SYSV_SEM */
13425184Sek110237 
13436212Saw148015 	if (!(avd_get_bool(flowop->fo_blocking)))
13445184Sek110237 		(void) ipc_mutex_unlock(&flowop->fo_lock);
13455184Sek110237 
13466084Saw148015 	return (FILEBENCH_OK);
13475184Sek110237 }
13485184Sek110237 
13495184Sek110237 /*
13505184Sek110237  * Releases the semids for the System V semaphore allocated
13515184Sek110237  * to this flowop. If not using System V semaphores, then
13526084Saw148015  * it is effectively just a no-op.
13535184Sek110237  */
13545184Sek110237 static void
13555184Sek110237 flowoplib_semblock_destruct(flowop_t *flowop)
13565184Sek110237 {
13575184Sek110237 #ifdef HAVE_SYSV_SEM
13585184Sek110237 	ipc_semidfree(flowop->fo_semid_lw);
13595184Sek110237 	ipc_semidfree(flowop->fo_semid_hw);
1360*6391Saw148015 	(void) semctl(filebench_shm->shm_sys_semid, 0, IPC_RMID);
1361*6391Saw148015 	filebench_shm->shm_sys_semid = -1;
13625184Sek110237 #else
13635184Sek110237 	sem_destroy(&flowop->fo_sem);
13645184Sek110237 #endif /* HAVE_SYSV_SEM */
13655184Sek110237 }
13665184Sek110237 
13675184Sek110237 /*
13685184Sek110237  * Attempts to pass a System V or posix semaphore as appropriate,
13696084Saw148015  * and blocks if necessary. Returns FILEBENCH_ERROR if a set of System V
13705184Sek110237  * semphores is not available or cannot be acquired, or if the initial
13716084Saw148015  * post to the semaphore set fails. Returns FILEBENCH_OK on success.
13725184Sek110237  */
13735184Sek110237 static int
13745184Sek110237 flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop)
13755184Sek110237 {
13765184Sek110237 
13775184Sek110237 #ifdef HAVE_SYSV_SEM
13785184Sek110237 	struct sembuf sbuf[2];
13796212Saw148015 	int value = avd_get_int(flowop->fo_value);
1380*6391Saw148015 	int sys_semid;
13815184Sek110237 	struct timespec timeout;
13825184Sek110237 
1383*6391Saw148015 	sys_semid = filebench_shm->shm_sys_semid;
13845184Sek110237 
13855184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
13865184Sek110237 	    "flow %s-%d sem blocking on id %x num %x value %d",
1387*6391Saw148015 	    flowop->fo_name, flowop->fo_instance, sys_semid,
13885184Sek110237 	    flowop->fo_semid_hw, value);
13895184Sek110237 
13905184Sek110237 	/* Post, decrement the increment the hw queue */
13915184Sek110237 	sbuf[0].sem_num = flowop->fo_semid_hw;
13925673Saw148015 	sbuf[0].sem_op = (short)value;
13935184Sek110237 	sbuf[0].sem_flg = 0;
13945184Sek110237 	sbuf[1].sem_num = flowop->fo_semid_lw;
13955184Sek110237 	sbuf[1].sem_op = value * -1;
13965184Sek110237 	sbuf[1].sem_flg = 0;
13975184Sek110237 	timeout.tv_sec = 600;
13985184Sek110237 	timeout.tv_nsec = 0;
13995184Sek110237 
14006212Saw148015 	if (avd_get_bool(flowop->fo_blocking))
14015184Sek110237 		(void) ipc_mutex_unlock(&flowop->fo_lock);
14025184Sek110237 
14035184Sek110237 	flowop_beginop(threadflow, flowop);
14045184Sek110237 
14055184Sek110237 #ifdef HAVE_SEMTIMEDOP
1406*6391Saw148015 	(void) semtimedop(sys_semid, &sbuf[0], 1, &timeout);
1407*6391Saw148015 	(void) semtimedop(sys_semid, &sbuf[1], 1, &timeout);
14085184Sek110237 #else
1409*6391Saw148015 	(void) semop(sys_semid, &sbuf[0], 1);
1410*6391Saw148015 	(void) semop(sys_semid, &sbuf[1], 1);
14115184Sek110237 #endif /* HAVE_SEMTIMEDOP */
14125184Sek110237 
14136212Saw148015 	if (avd_get_bool(flowop->fo_blocking))
14145184Sek110237 		(void) ipc_mutex_lock(&flowop->fo_lock);
14155184Sek110237 
14165673Saw148015 	flowop_endop(threadflow, flowop, 0);
14175184Sek110237 
14185184Sek110237 #else
14196212Saw148015 	int value = avd_get_int(flowop->fo_value);
14205184Sek110237 	int i;
14215184Sek110237 
14225184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
14235184Sek110237 	    "flow %s-%d sem blocking on posix semaphore",
14245184Sek110237 	    flowop->fo_name, flowop->fo_instance);
14255184Sek110237 
14265184Sek110237 	/* Decrement sem by value */
14275184Sek110237 	for (i = 0; i < value; i++) {
14285184Sek110237 		if (sem_wait(&flowop->fo_sem) == -1) {
14295184Sek110237 			filebench_log(LOG_ERROR, "semop wait failed");
14306084Saw148015 			return (FILEBENCH_ERROR);
14315184Sek110237 		}
14325184Sek110237 	}
14335184Sek110237 
14345184Sek110237 	filebench_log(LOG_DEBUG_IMPL, "flow %s-%d sem unblocking",
14355184Sek110237 	    flowop->fo_name, flowop->fo_instance);
14365184Sek110237 #endif /* HAVE_SYSV_SEM */
14375184Sek110237 
14386084Saw148015 	return (FILEBENCH_OK);
14395184Sek110237 }
14405184Sek110237 
14415184Sek110237 /*
14426084Saw148015  * Calls ipc_seminit(). Always returns FILEBENCH_OK.
14435184Sek110237  */
14445184Sek110237 /* ARGSUSED */
14455184Sek110237 static int
14465184Sek110237 flowoplib_sempost_init(flowop_t *flowop)
14475184Sek110237 {
14485184Sek110237 #ifdef HAVE_SYSV_SEM
14495184Sek110237 	ipc_seminit();
14505184Sek110237 #endif /* HAVE_SYSV_SEM */
14516084Saw148015 	return (FILEBENCH_OK);
14525184Sek110237 }
14535184Sek110237 
14545184Sek110237 /*
14555184Sek110237  * Post to a System V or posix semaphore as appropriate.
14565184Sek110237  * On the first call for a given flowop instance, this routine
14575184Sek110237  * will use the fo_targetname attribute to locate all semblock
14585184Sek110237  * flowops that are expecting posts from this flowop. All
14595184Sek110237  * target flowops on this list will have a post operation done
14605184Sek110237  * to their semaphores on each call.
14615184Sek110237  */
14625184Sek110237 static int
14635184Sek110237 flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop)
14645184Sek110237 {
14655184Sek110237 	flowop_t *target;
14665184Sek110237 
14675184Sek110237 	filebench_log(LOG_DEBUG_IMPL,
14685184Sek110237 	    "sempost flow %s-%d",
14695184Sek110237 	    flowop->fo_name,
14705184Sek110237 	    flowop->fo_instance);
14715184Sek110237 
14725184Sek110237 	/* if this is the first post, create the post list */
14735184Sek110237 	if (flowop->fo_targets == NULL) {
14745184Sek110237 		flowop_t *result = flowop_find(flowop->fo_targetname);
14755184Sek110237 
14765184Sek110237 		flowop->fo_targets = result;
14775184Sek110237 
14785184Sek110237 		if (result == NULL) {
14795184Sek110237 			filebench_log(LOG_ERROR,
14805184Sek110237 			    "sempost: could not find op %s for thread %s",
14815184Sek110237 			    flowop->fo_targetname,
14825184Sek110237 			    threadflow->tf_name);
14835184Sek110237 			filebench_shutdown(1);
14845184Sek110237 		}
14855184Sek110237 
14865184Sek110237 		while (result) {
14875184Sek110237 			result->fo_targetnext =
14885184Sek110237 			    result->fo_resultnext;
14895184Sek110237 			result = result->fo_resultnext;
14905184Sek110237 		}
14915184Sek110237 	}
14925184Sek110237 
14935184Sek110237 	target = flowop->fo_targets;
14945184Sek110237 
14955184Sek110237 	flowop_beginop(threadflow, flowop);
14965184Sek110237 	/* post to the targets */
14975184Sek110237 	while (target) {
14985184Sek110237 #ifdef HAVE_SYSV_SEM
14995184Sek110237 		struct sembuf sbuf[2];
1500*6391Saw148015 		int sys_semid;
15015184Sek110237 		int blocking;
15025184Sek110237 #else
15035184Sek110237 		int i;
15045184Sek110237 #endif /* HAVE_SYSV_SEM */
15055184Sek110237 		struct timespec timeout;
15066212Saw148015 		int value = avd_get_int(flowop->fo_value);
15075184Sek110237 
15085184Sek110237 		if (target->fo_instance == FLOW_MASTER) {
15095184Sek110237 			target = target->fo_targetnext;
15105184Sek110237 			continue;
15115184Sek110237 		}
15125184Sek110237 
15135184Sek110237 #ifdef HAVE_SYSV_SEM
15145184Sek110237 
15155184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
15165184Sek110237 		    "sempost flow %s-%d num %x",
15175184Sek110237 		    target->fo_name,
15185184Sek110237 		    target->fo_instance,
15195184Sek110237 		    target->fo_semid_lw);
15205184Sek110237 
1521*6391Saw148015 		sys_semid = filebench_shm->shm_sys_semid;
15225184Sek110237 		sbuf[0].sem_num = target->fo_semid_lw;
15235673Saw148015 		sbuf[0].sem_op = (short)value;
15245184Sek110237 		sbuf[0].sem_flg = 0;
15255184Sek110237 		sbuf[1].sem_num = target->fo_semid_hw;
15265184Sek110237 		sbuf[1].sem_op = value * -1;
15275184Sek110237 		sbuf[1].sem_flg = 0;
15285184Sek110237 		timeout.tv_sec = 600;
15295184Sek110237 		timeout.tv_nsec = 0;
15305184Sek110237 
15316212Saw148015 		if (avd_get_bool(flowop->fo_blocking))
15325184Sek110237 			blocking = 1;
15335184Sek110237 		else
15345184Sek110237 			blocking = 0;
15355184Sek110237 
15365184Sek110237 #ifdef HAVE_SEMTIMEDOP
1537*6391Saw148015 		if ((semtimedop(sys_semid, &sbuf[0], blocking + 1,
15385184Sek110237 		    &timeout) == -1) && (errno && (errno != EAGAIN))) {
15395184Sek110237 #else
1540*6391Saw148015 		if ((semop(sys_semid, &sbuf[0], blocking + 1) == -1) &&
15415184Sek110237 		    (errno && (errno != EAGAIN))) {
15425184Sek110237 #endif /* HAVE_SEMTIMEDOP */
15435184Sek110237 			filebench_log(LOG_ERROR, "semop post failed: %s",
15445184Sek110237 			    strerror(errno));
15456084Saw148015 			return (FILEBENCH_ERROR);
15465184Sek110237 		}
15475184Sek110237 
15485184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
15495184Sek110237 		    "flow %s-%d finished posting",
15505184Sek110237 		    target->fo_name, target->fo_instance);
15515184Sek110237 #else
15525184Sek110237 		filebench_log(LOG_DEBUG_IMPL,
15535184Sek110237 		    "sempost flow %s-%d to posix semaphore",
15545184Sek110237 		    target->fo_name,
15555184Sek110237 		    target->fo_instance);
15565184Sek110237 
15575184Sek110237 		/* Increment sem by value */
15585184Sek110237 		for (i = 0; i < value; i++) {
15595184Sek110237 			if (sem_post(&target->fo_sem) == -1) {
15605184Sek110237 				filebench_log(LOG_ERROR, "semop post failed");
15616084Saw148015 				return (FILEBENCH_ERROR);
15625184Sek110237 			}
15635184Sek110237 		}
15645184Sek110237 
15655184Sek110237 		filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
15665184Sek110237 		    target->fo_name, target->fo_instance);
15675184Sek110237 #endif /* HAVE_SYSV_SEM */
15685184Sek110237 
15695184Sek110237 		target = target->fo_targetnext;
15705184Sek110237 	}
15715673Saw148015 	flowop_endop(threadflow, flowop, 0);
15725184Sek110237 
15736084Saw148015 	return (FILEBENCH_OK);
15745184Sek110237 }
15755184Sek110237 
15765184Sek110237 
15775184Sek110237 /*
15785184Sek110237  * Section for exercising create / open / close / delete operations
15795184Sek110237  * on files within a fileset. For proper operation, the flowop attribute
15805184Sek110237  * "fd", which sets the fo_fdnumber field in the flowop, must be used
15815184Sek110237  * so that the same file is opened and later closed. "fd" is an index
15825184Sek110237  * into a pair of arrays maintained by threadflows, one of which
15835184Sek110237  * contains the operating system assigned file descriptors and the other
15845184Sek110237  * a pointer to the filesetentry whose file the file descriptor
15855184Sek110237  * references. An openfile flowop defined without fd being set will use
15865184Sek110237  * the default (0) fd or, if specified, rotate through fd indices, but
15875184Sek110237  * createfile and closefile must use the default or a specified fd.
15885184Sek110237  * Meanwhile deletefile picks and arbitrary file to delete, regardless
15895184Sek110237  * of fd attribute.
15905184Sek110237  */
15915184Sek110237 
15925184Sek110237 /*
15935184Sek110237  * XXX Making file selection more consistent among the flowops might good
15945184Sek110237  */
15955184Sek110237 
15965184Sek110237 
15975184Sek110237 /*
15985184Sek110237  * Emulates (and actually does) file open. Obtains a file descriptor
15996084Saw148015  * index, then calls flowoplib_openfile_common() to open. Returns
16006084Saw148015  * FILEBENCH_ERROR if no file descriptor is found, and returns the
16016084Saw148015  * status from flowoplib_openfile_common otherwise (FILEBENCH_ERROR,
16026084Saw148015  * FILEBENCH_NORSC, FILEBENCH_OK).
16035184Sek110237  */
16045184Sek110237 static int
16055184Sek110237 flowoplib_openfile(threadflow_t *threadflow, flowop_t *flowop)
16065184Sek110237 {
16075184Sek110237 	int fd = flowoplib_fdnum(threadflow, flowop);
16085184Sek110237 
16095184Sek110237 	if (fd == -1)
16106084Saw148015 		return (FILEBENCH_ERROR);
16115184Sek110237 
16125184Sek110237 	return (flowoplib_openfile_common(threadflow, flowop, fd));
16135184Sek110237 }
16145184Sek110237 
16155184Sek110237 /*
16165184Sek110237  * Common file opening code for filesets. Uses the supplied
16175184Sek110237  * file descriptor index to determine the tf_fd entry to use.
16185184Sek110237  * If the entry is empty (0) and the fileset exists, fileset
16195184Sek110237  * pick is called to select a fileset entry to use. The file
16205184Sek110237  * specified in the filesetentry is opened, and the returned
16215184Sek110237  * operating system file descriptor and a pointer to the
16225184Sek110237  * filesetentry are stored in tf_fd[fd] and tf_fse[fd],
16236084Saw148015  * respectively. Returns FILEBENCH_ERROR on error,
16246084Saw148015  * FILEBENCH_NORSC if no suitable filesetentry can be found,
16256084Saw148015  * and FILEBENCH_OK on success.
16265184Sek110237  */
16275184Sek110237 static int
16285184Sek110237 flowoplib_openfile_common(threadflow_t *threadflow, flowop_t *flowop, int fd)
16295184Sek110237 {
16305184Sek110237 	filesetentry_t *file;
16316212Saw148015 	char *fileset_name;
16325184Sek110237 	int tid = 0;
16335184Sek110237 
1634*6391Saw148015 	if (flowop->fo_fileset == NULL) {
1635*6391Saw148015 		filebench_log(LOG_ERROR, "flowop NULL file");
1636*6391Saw148015 		return (FILEBENCH_ERROR);
1637*6391Saw148015 	}
1638*6391Saw148015 
16396212Saw148015 	if ((fileset_name =
16406212Saw148015 	    avd_get_str(flowop->fo_fileset->fs_name)) == NULL) {
16416212Saw148015 		filebench_log(LOG_ERROR,
16426212Saw148015 		    "flowop %s: fileset has no name", flowop->fo_name);
16436212Saw148015 		return (FILEBENCH_ERROR);
16446212Saw148015 	}
16456212Saw148015 
16465184Sek110237 	/*
16475184Sek110237 	 * If the flowop doesn't default to persistent fd
16485184Sek110237 	 * then get unique thread ID for use by fileset_pick
16495184Sek110237 	 */
16506212Saw148015 	if (avd_get_bool(flowop->fo_rotatefd))
16515184Sek110237 		tid = threadflow->tf_utid;
16525184Sek110237 
16535184Sek110237 	if (threadflow->tf_fd[fd] != 0) {
16545184Sek110237 		filebench_log(LOG_ERROR,
16555184Sek110237 		    "flowop %s attempted to open without closing on fd %d",
16565184Sek110237 		    flowop->fo_name, fd);
16576084Saw148015 		return (FILEBENCH_ERROR);
16585184Sek110237 	}
16595184Sek110237 
16605673Saw148015 #ifdef HAVE_RAW_SUPPORT
16615673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
16625673Saw148015 		int open_attrs = 0;
16635673Saw148015 		char name[MAXPATHLEN];
16645673Saw148015 
16656212Saw148015 		(void) strcpy(name,
16666212Saw148015 		    avd_get_str(flowop->fo_fileset->fs_path));
16675673Saw148015 		(void) strcat(name, "/");
16686212Saw148015 		(void) strcat(name, fileset_name);
16695673Saw148015 
16706212Saw148015 		if (avd_get_bool(flowop->fo_dsync)) {
16715673Saw148015 #ifdef sun
16725673Saw148015 			open_attrs |= O_DSYNC;
16735673Saw148015 #else
16745673Saw148015 			open_attrs |= O_FSYNC;
16755673Saw148015 #endif
16765673Saw148015 		}
16775673Saw148015 
16785673Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
16795673Saw148015 		    "open raw device %s flags %d = %d", name, open_attrs, fd);
16805673Saw148015 
16815673Saw148015 		threadflow->tf_fd[fd] = open64(name,
16825673Saw148015 		    O_RDWR | open_attrs, 0666);
16835673Saw148015 
16845673Saw148015 		if (threadflow->tf_fd[fd] < 0) {
16855673Saw148015 			filebench_log(LOG_ERROR,
16865673Saw148015 			    "Failed to open raw device %s: %s",
16875673Saw148015 			    name, strerror(errno));
16886084Saw148015 			return (FILEBENCH_ERROR);
16895673Saw148015 		}
16905673Saw148015 
16915673Saw148015 		/* if running on Solaris, use un-buffered io */
16925673Saw148015 #ifdef sun
16935673Saw148015 		(void) directio(threadflow->tf_fd[fd], DIRECTIO_ON);
16945673Saw148015 #endif
16955673Saw148015 
16965673Saw148015 		threadflow->tf_fse[fd] = NULL;
16975673Saw148015 
16986084Saw148015 		return (FILEBENCH_OK);
16995673Saw148015 	}
17005673Saw148015 #endif /* HAVE_RAW_SUPPORT */
17015673Saw148015 
17025184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
17035184Sek110237 	    FILESET_PICKEXISTS, tid)) == NULL) {
17046084Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
17055184Sek110237 		    "flowop %s failed to pick file from %s on fd %d",
17066212Saw148015 		    flowop->fo_name, fileset_name, fd);
17076084Saw148015 		return (FILEBENCH_NORSC);
17085184Sek110237 	}
17095184Sek110237 
17105184Sek110237 	threadflow->tf_fse[fd] = file;
17115184Sek110237 
17125184Sek110237 	flowop_beginop(threadflow, flowop);
17135184Sek110237 	threadflow->tf_fd[fd] = fileset_openfile(flowop->fo_fileset,
17145184Sek110237 	    file, O_RDWR, 0666, flowoplib_fileattrs(flowop));
17155673Saw148015 	flowop_endop(threadflow, flowop, 0);
17165184Sek110237 
17175184Sek110237 	if (threadflow->tf_fd[fd] < 0) {
17186212Saw148015 		filebench_log(LOG_ERROR, "flowop %s failed to open file %s",
17196212Saw148015 		    flowop->fo_name, file->fse_path);
17206084Saw148015 		return (FILEBENCH_ERROR);
17215184Sek110237 	}
17225184Sek110237 
17235184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT,
17245184Sek110237 	    "flowop %s: opened %s fd[%d] = %d",
17255184Sek110237 	    flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]);
17265184Sek110237 
17276084Saw148015 	return (FILEBENCH_OK);
17285184Sek110237 }
17295184Sek110237 
17305184Sek110237 /*
17315184Sek110237  * Emulate create of a file. Uses the flowop's fdnumber to select
17325184Sek110237  * tf_fd and tf_fse array locations to put the created file's file
17335184Sek110237  * descriptor and filesetentry respectively. Uses fileset_pick()
17345184Sek110237  * to select a specific filesetentry whose file does not currently
17355184Sek110237  * exist for the file create operation. Then calls
17365184Sek110237  * fileset_openfile() with the O_CREATE flag set to create the
17376084Saw148015  * file. Returns FILEBENCH_ERROR if the array index specified by fdnumber is
17385184Sek110237  * already in use, the flowop has no associated fileset, or
17395184Sek110237  * the create call fails. Returns 1 if a filesetentry with a
17406084Saw148015  * nonexistent file cannot be found. Returns FILEBENCH_OK on success.
17415184Sek110237  */
17425184Sek110237 static int
17435184Sek110237 flowoplib_createfile(threadflow_t *threadflow, flowop_t *flowop)
17445184Sek110237 {
17455184Sek110237 	filesetentry_t *file;
17465184Sek110237 	int fd = flowop->fo_fdnumber;
17475184Sek110237 
17485184Sek110237 	if (threadflow->tf_fd[fd] != 0) {
17495184Sek110237 		filebench_log(LOG_ERROR,
17505184Sek110237 		    "flowop %s attempted to create without closing on fd %d",
17515184Sek110237 		    flowop->fo_name, fd);
17526084Saw148015 		return (FILEBENCH_ERROR);
17535184Sek110237 	}
17545184Sek110237 
17555184Sek110237 	if (flowop->fo_fileset == NULL) {
17565184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
17576084Saw148015 		return (FILEBENCH_ERROR);
17585184Sek110237 	}
17595184Sek110237 
17605673Saw148015 #ifdef HAVE_RAW_SUPPORT
17615673Saw148015 	/* can't be used with raw devices */
17625673Saw148015 	if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) {
17635673Saw148015 		filebench_log(LOG_ERROR,
17645673Saw148015 		    "flowop %s attempted to a createfile on RAW device",
17655673Saw148015 		    flowop->fo_name);
17666084Saw148015 		return (FILEBENCH_ERROR);
17675673Saw148015 	}
17685673Saw148015 #endif /* HAVE_RAW_SUPPORT */
17695673Saw148015 
17705184Sek110237 	if ((file = fileset_pick(flowop->fo_fileset,
17715184Sek110237 	    FILESET_PICKNOEXIST, 0)) == NULL) {
17726084Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
17736084Saw148015 		    "flowop %s failed to pick file from fileset %s",
17746212Saw148015 		    flowop->fo_name,
17756212Saw148015 		    avd_get_str(flowop->fo_fileset->fs_name));
17766084Saw148015 		return (FILEBENCH_NORSC);
17775184Sek110237 	}
17785184Sek110237 
17795184Sek110237 	threadflow->tf_fse[fd] = file;
17805184Sek110237 
17815184Sek110237 	flowop_beginop(threadflow, flowop);
17825184Sek110237 	threadflow->tf_fd[fd] = fileset_openfile(flowop->fo_fileset,
17835184Sek110237 	    file, O_RDWR | O_CREAT, 0666, flowoplib_fileattrs(flowop));
17845673Saw148015 	flowop_endop(threadflow, flowop, 0);
17855184Sek110237 
17865184Sek110237 	if (threadflow->tf_fd[fd] < 0) {
17875184Sek110237 		filebench_log(LOG_ERROR, "failed to create file %s",
17885184Sek110237 		    flowop->fo_name);
17896084Saw148015 		return (FILEBENCH_ERROR);
17905184Sek110237 	}
17915184Sek110237 
17925184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT,
17935184Sek110237 	    "flowop %s: created %s fd[%d] = %d",
17945184Sek110237 	    flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]);
17955184Sek110237 
17966084Saw148015 	return (FILEBENCH_OK);
17975184Sek110237 }
17985184Sek110237 
17995184Sek110237 /*
1800*6391Saw148015  * Emulates delete of a file. If a valid fd is provided, it uses the
1801*6391Saw148015  * filesetentry stored at that fd location to select the file to be
1802*6391Saw148015  * deleted, otherwise it picks an arbitrary filesetentry
1803*6391Saw148015  * whose file exists. It then uses unlink() to delete it and Clears
18046084Saw148015  * the FSE_EXISTS flag for the filesetentry. Returns FILEBENCH_ERROR if the
18056084Saw148015  * flowop has no associated fileset. Returns FILEBENCH_NORSC if an appropriate
18066084Saw148015  * filesetentry cannot be found, and FILEBENCH_OK on success.
18075184Sek110237  */
18085184Sek110237 static int
18095184Sek110237 flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop)
18105184Sek110237 {
18115184Sek110237 	filesetentry_t *file;
18125184Sek110237 	fileset_t *fileset;
18135184Sek110237 	char path[MAXPATHLEN];
18145184Sek110237 	char *pathtmp;
1815*6391Saw148015 	int fd = flowop->fo_fdnumber;
18165184Sek110237 
1817*6391Saw148015 	/* if fd specified, use it to access file */
1818*6391Saw148015 	if ((fd > 0) && ((file = threadflow->tf_fse[fd]) != NULL)) {
1819*6391Saw148015 
1820*6391Saw148015 		/* check whether file still open */
1821*6391Saw148015 		if (threadflow->tf_fd[fd] > 0) {
1822*6391Saw148015 			filebench_log(LOG_DEBUG_SCRIPT,
1823*6391Saw148015 			    "flowop %s deleting still open file at fd = %d",
1824*6391Saw148015 			    flowop->fo_name, fd);
1825*6391Saw148015 		}
1826*6391Saw148015 
1827*6391Saw148015 		/* indicate that the file will be deleted */
1828*6391Saw148015 		threadflow->tf_fse[fd] = NULL;
1829*6391Saw148015 
1830*6391Saw148015 		/* if here, we still have a valid file pointer */
1831*6391Saw148015 		fileset = file->fse_fileset;
1832*6391Saw148015 	} else {
1833*6391Saw148015 		/* Otherwise, pick arbitrary file */
1834*6391Saw148015 		file = NULL;
1835*6391Saw148015 		fileset = flowop->fo_fileset;
1836*6391Saw148015 	}
1837*6391Saw148015 
1838*6391Saw148015 
1839*6391Saw148015 	if (fileset == NULL) {
18405184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
18416084Saw148015 		return (FILEBENCH_ERROR);
18425184Sek110237 	}
18435184Sek110237 
18445673Saw148015 #ifdef HAVE_RAW_SUPPORT
18455673Saw148015 	/* can't be used with raw devices */
1846*6391Saw148015 	if (fileset->fs_attrs & FILESET_IS_RAW_DEV) {
18475673Saw148015 		filebench_log(LOG_ERROR,
18485673Saw148015 		    "flowop %s attempted a deletefile on RAW device",
18495673Saw148015 		    flowop->fo_name);
18506084Saw148015 		return (FILEBENCH_ERROR);
18515673Saw148015 	}
18525673Saw148015 #endif /* HAVE_RAW_SUPPORT */
18535673Saw148015 
1854*6391Saw148015 	if (file == NULL) {
1855*6391Saw148015 		if ((file = fileset_pick(fileset, FILESET_PICKEXISTS, 0))
1856*6391Saw148015 		    == NULL) {
1857*6391Saw148015 			filebench_log(LOG_DEBUG_SCRIPT,
1858*6391Saw148015 			    "flowop %s failed to pick file", flowop->fo_name);
1859*6391Saw148015 			return (FILEBENCH_NORSC);
1860*6391Saw148015 		}
1861*6391Saw148015 	} else {
1862*6391Saw148015 		(void) ipc_mutex_lock(&file->fse_lock);
18635184Sek110237 	}
18645184Sek110237 
18655184Sek110237 	*path = 0;
18666212Saw148015 	(void) strcpy(path, avd_get_str(fileset->fs_path));
18675184Sek110237 	(void) strcat(path, "/");
18686212Saw148015 	(void) strcat(path, avd_get_str(fileset->fs_name));
18695184Sek110237 	pathtmp = fileset_resolvepath(file);
18705184Sek110237 	(void) strcat(path, pathtmp);
18715184Sek110237 	free(pathtmp);
18725184Sek110237 
18735184Sek110237 	flowop_beginop(threadflow, flowop);
18745184Sek110237 	(void) unlink(path);
18755673Saw148015 	flowop_endop(threadflow, flowop, 0);
18765184Sek110237 	file->fse_flags &= ~FSE_EXISTS;
18775184Sek110237 	(void) ipc_mutex_unlock(&file->fse_lock);
18785184Sek110237 
18795184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "deleted file %s", file->fse_path);
18805184Sek110237 
18816084Saw148015 	return (FILEBENCH_OK);
18825184Sek110237 }
18835184Sek110237 
18845184Sek110237 /*
18855184Sek110237  * Emulates fsync of a file. Obtains the file descriptor index
18865184Sek110237  * from the flowop, obtains the actual file descriptor from
18875184Sek110237  * the threadflow's table, checks to be sure it is still an
18886084Saw148015  * open file, then does an fsync operation on it. Returns FILEBENCH_ERROR
18896084Saw148015  * if the file no longer is open, FILEBENCH_OK otherwise.
18905184Sek110237  */
18915184Sek110237 static int
18925184Sek110237 flowoplib_fsync(threadflow_t *threadflow, flowop_t *flowop)
18935184Sek110237 {
18945184Sek110237 	filesetentry_t *file;
18955184Sek110237 	int fd = flowop->fo_fdnumber;
18965184Sek110237 
18975184Sek110237 	if (threadflow->tf_fd[fd] == 0) {
18985184Sek110237 		filebench_log(LOG_ERROR,
18995184Sek110237 		    "flowop %s attempted to fsync a closed fd %d",
19005184Sek110237 		    flowop->fo_name, fd);
19016084Saw148015 		return (FILEBENCH_ERROR);
19025184Sek110237 	}
19035184Sek110237 
19045673Saw148015 	file = threadflow->tf_fse[fd];
19055673Saw148015 
19065673Saw148015 	if ((file == NULL) ||
19075673Saw148015 	    (file->fse_fileset->fs_attrs & FILESET_IS_RAW_DEV)) {
19085673Saw148015 		filebench_log(LOG_ERROR,
19095673Saw148015 		    "flowop %s attempted to a fsync a RAW device",
19105673Saw148015 		    flowop->fo_name);
19116084Saw148015 		return (FILEBENCH_ERROR);
19125673Saw148015 	}
19135673Saw148015 
19145184Sek110237 	/* Measure time to fsync */
19155184Sek110237 	flowop_beginop(threadflow, flowop);
19165184Sek110237 	(void) fsync(threadflow->tf_fd[fd]);
19175673Saw148015 	flowop_endop(threadflow, flowop, 0);
19185184Sek110237 
19195184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s", file->fse_path);
19205184Sek110237 
19216084Saw148015 	return (FILEBENCH_OK);
19225184Sek110237 }
19235184Sek110237 
19245184Sek110237 /*
19255184Sek110237  * Emulate fsync of an entire fileset. Search through the
19265184Sek110237  * threadflow's file descriptor array, doing fsync() on each
19275184Sek110237  * open file that belongs to the flowop's fileset. Always
19286084Saw148015  * returns FILEBENCH_OK.
19295184Sek110237  */
19305184Sek110237 static int
19315184Sek110237 flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop)
19325184Sek110237 {
19335184Sek110237 	int fd;
19345184Sek110237 
19355184Sek110237 	for (fd = 0; fd < THREADFLOW_MAXFD; fd++) {
19365184Sek110237 		filesetentry_t *file;
19375184Sek110237 
19385184Sek110237 		/* Match the file set to fsync */
19395184Sek110237 		if ((threadflow->tf_fse[fd] == NULL) ||
19405184Sek110237 		    (flowop->fo_fileset != threadflow->tf_fse[fd]->fse_fileset))
19415184Sek110237 			continue;
19425184Sek110237 
19435184Sek110237 		/* Measure time to fsync */
19445184Sek110237 		flowop_beginop(threadflow, flowop);
19455184Sek110237 		(void) fsync(threadflow->tf_fd[fd]);
19465673Saw148015 		flowop_endop(threadflow, flowop, 0);
19475184Sek110237 
19485184Sek110237 		file = threadflow->tf_fse[fd];
19495184Sek110237 
19505184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s",
19515184Sek110237 		    file->fse_path);
19525184Sek110237 	}
19535184Sek110237 
19546084Saw148015 	return (FILEBENCH_OK);
19555184Sek110237 }
19565184Sek110237 
19575184Sek110237 /*
19585184Sek110237  * Emulate close of a file.  Obtains the file descriptor index
19595184Sek110237  * from the flowop, obtains the actual file descriptor from the
19605184Sek110237  * threadflow's table, checks to be sure it is still an open
19615184Sek110237  * file, then does a close operation on it. Then sets the
19625184Sek110237  * threadflow file descriptor table entry to 0, and the file set
19636084Saw148015  * entry pointer to NULL. Returns FILEBENCH_ERROR if the file was not open,
19646084Saw148015  * FILEBENCH_OK otherwise.
19655184Sek110237  */
19665184Sek110237 static int
19675184Sek110237 flowoplib_closefile(threadflow_t *threadflow, flowop_t *flowop)
19685184Sek110237 {
19695184Sek110237 	filesetentry_t *file;
19705184Sek110237 	int fd = flowop->fo_fdnumber;
19715184Sek110237 
19725184Sek110237 	if (threadflow->tf_fd[fd] == 0) {
19735184Sek110237 		filebench_log(LOG_ERROR,
19745184Sek110237 		    "flowop %s attempted to close an already closed fd %d",
19755184Sek110237 		    flowop->fo_name, fd);
19766084Saw148015 		return (FILEBENCH_ERROR);
19775184Sek110237 	}
19785184Sek110237 
19795184Sek110237 	/* Measure time to close */
19805184Sek110237 	flowop_beginop(threadflow, flowop);
19815184Sek110237 	(void) close(threadflow->tf_fd[fd]);
19825673Saw148015 	flowop_endop(threadflow, flowop, 0);
19835184Sek110237 
19845184Sek110237 	file = threadflow->tf_fse[fd];
19855184Sek110237 
19865184Sek110237 	threadflow->tf_fd[fd] = 0;
19875184Sek110237 
19885184Sek110237 	filebench_log(LOG_DEBUG_SCRIPT, "closed file %s", file->fse_path);
19895184Sek110237 
19906084Saw148015 	return (FILEBENCH_OK);
19915184Sek110237 }
19925184Sek110237 
19935184Sek110237 /*
19945184Sek110237  * Emulate stat of a file. Picks an arbitrary filesetentry with
19955184Sek110237  * an existing file from the flowop's fileset, then performs a
19966084Saw148015  * stat() operation on it. Returns FILEBENCH_ERROR if the flowop has no
19976084Saw148015  * associated fileset. Returns FILEBENCH_NORSC if an appropriate filesetentry
19986084Saw148015  * cannot be found, and FILEBENCH_OK on success.
19995184Sek110237  */
20005184Sek110237 static int
20015184Sek110237 flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop)
20025184Sek110237 {
20035184Sek110237 	filesetentry_t *file;
20045184Sek110237 	fileset_t *fileset;
20055184Sek110237 	char path[MAXPATHLEN];
20065184Sek110237 	char *pathtmp;
20075184Sek110237 
2008*6391Saw148015 	if ((fileset = flowop->fo_fileset) == NULL) {
20095184Sek110237 		filebench_log(LOG_ERROR, "flowop NULL file");
20106084Saw148015 		return (FILEBENCH_ERROR);
20115184Sek110237 	}
20125184Sek110237 
2013*6391Saw148015 	if ((file = fileset_pick(fileset, FILESET_PICKEXISTS, 0)) == NULL) {
2014*6391Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
2015*6391Saw148015 		    "flowop %s failed to pick file",
20165184Sek110237 		    flowop->fo_name);
20176084Saw148015 		return (FILEBENCH_NORSC);
20185184Sek110237 	}
20195184Sek110237 
20205184Sek110237 	*path = 0;
20216212Saw148015 	(void) strcpy(path, avd_get_str(fileset->fs_path));
20225184Sek110237 	(void) strcat(path, "/");
20236212Saw148015 	(void) strcat(path, avd_get_str(fileset->fs_name));
20245184Sek110237 	pathtmp = fileset_resolvepath(file);
20255184Sek110237 	(void) strcat(path, pathtmp);
20265184Sek110237 	free(pathtmp);
20275184Sek110237 
20285184Sek110237 	flowop_beginop(threadflow, flowop);
20295673Saw148015 	flowop_endop(threadflow, flowop, 0);
20305184Sek110237 
20315184Sek110237 	(void) ipc_mutex_unlock(&file->fse_lock);
20325184Sek110237 
20336084Saw148015 	return (FILEBENCH_OK);
20345184Sek110237 }
20355184Sek110237 
20365184Sek110237 
20375184Sek110237 /*
20385184Sek110237  * Additional reads and writes. Read and write whole files, write
20395184Sek110237  * and append to files. Some of these work with both fileobjs and
20405184Sek110237  * filesets, others only with filesets. The flowoplib_write routine
20415184Sek110237  * writes from thread memory, while the others read or write using
20425184Sek110237  * fo_buf memory. Note that both flowoplib_read() and
20435184Sek110237  * flowoplib_aiowrite() use thread memory as well.
20445184Sek110237  */
20455184Sek110237 
20465184Sek110237 
20475184Sek110237 /*
20485673Saw148015  * Emulate a read of a whole file. The file must be open with
20495673Saw148015  * file descriptor and filesetentry stored at the locations indexed
20505673Saw148015  * by the flowop's fdnumber. It then seeks to the beginning of the
20515673Saw148015  * associated file, and reads fs_iosize bytes at a time until the end
20526084Saw148015  * of the file. Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if
20536084Saw148015  * out of files, and FILEBENCH_OK on success.
20545184Sek110237  */
20555184Sek110237 static int
20565184Sek110237 flowoplib_readwholefile(threadflow_t *threadflow, flowop_t *flowop)
20575184Sek110237 {
20585673Saw148015 	caddr_t iobuf;
20595184Sek110237 	off64_t bytes = 0;
20605673Saw148015 	int filedesc;
20616212Saw148015 	uint64_t wss;
20626212Saw148015 	fbint_t iosize;
20635184Sek110237 	int ret;
20646212Saw148015 	char zerordbuf;
20655184Sek110237 
20665673Saw148015 	/* get the file to use */
20676084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, &wss,
20686084Saw148015 	    &filedesc)) != FILEBENCH_OK)
20696084Saw148015 		return (ret);
20705184Sek110237 
20715673Saw148015 	/* an I/O size of zero means read entire working set with one I/O */
20726212Saw148015 	if ((iosize = avd_get_int(flowop->fo_iosize)) == 0)
20735673Saw148015 		iosize = wss;
20745184Sek110237 
20756212Saw148015 	/*
20766212Saw148015 	 * The file may actually be 0 bytes long, in which case skip
20776212Saw148015 	 * the buffer set up call (which would fail) and substitute
20786212Saw148015 	 * a small buffer, which won't really be used.
20796212Saw148015 	 */
20806212Saw148015 	if (iosize == 0) {
20816212Saw148015 		iobuf = (caddr_t)&zerordbuf;
20826212Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
20836212Saw148015 		    "flowop %s read zero length file", flowop->fo_name);
20846212Saw148015 	} else {
20856212Saw148015 		if (flowoplib_iobufsetup(threadflow, flowop, &iobuf,
20866212Saw148015 		    iosize) != 0)
20876212Saw148015 			return (FILEBENCH_ERROR);
20886212Saw148015 	}
20895184Sek110237 
20905184Sek110237 	/* Measure time to read bytes */
20915184Sek110237 	flowop_beginop(threadflow, flowop);
20925673Saw148015 	(void) lseek64(filedesc, 0, SEEK_SET);
20935673Saw148015 	while ((ret = read(filedesc, iobuf, iosize)) > 0)
20945184Sek110237 		bytes += ret;
20955184Sek110237 
20965673Saw148015 	flowop_endop(threadflow, flowop, bytes);
20975184Sek110237 
20985184Sek110237 	if (ret < 0) {
20995184Sek110237 		filebench_log(LOG_ERROR,
2100*6391Saw148015 		    "readwhole fail Failed to read whole file: %s",
2101*6391Saw148015 		    strerror(errno));
21026084Saw148015 		return (FILEBENCH_ERROR);
21035184Sek110237 	}
21045184Sek110237 
21056084Saw148015 	return (FILEBENCH_OK);
21065184Sek110237 }
21075184Sek110237 
21085184Sek110237 /*
21095184Sek110237  * Emulate a write to a file of size fo_iosize.  Will write
21105184Sek110237  * to a file from a fileset if the flowop's fo_fileset field
21115184Sek110237  * specifies one or its fdnumber is non zero. Otherwise it
21125184Sek110237  * will write to a fileobj file, if one exists. If the file
21135184Sek110237  * is not currently open, the routine will attempt to open
21145184Sek110237  * it. The flowop's fo_wss parameter will be used to set the
21155184Sek110237  * maximum file size if it is non-zero, otherwise the
21165184Sek110237  * filesetentry's  fse_size will be used. A random memory
21175184Sek110237  * buffer offset is calculated, and, if fo_random is TRUE,
21185184Sek110237  * a random file offset is used for the write. Otherwise the
21196084Saw148015  * write is to the next sequential location. Returns
21206084Saw148015  * FILEBENCH_ERROR on errors, FILEBENCH_NORSC if iosetup can't
21216084Saw148015  * obtain a file, or FILEBENCH_OK on success.
21225184Sek110237  */
21235184Sek110237 static int
21245184Sek110237 flowoplib_write(threadflow_t *threadflow, flowop_t *flowop)
21255184Sek110237 {
21265673Saw148015 	caddr_t iobuf;
21276212Saw148015 	fbint_t wss;
21286212Saw148015 	fbint_t iosize;
21295184Sek110237 	int filedesc;
21306084Saw148015 	int ret;
21315184Sek110237 
21326212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
21336084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
21346212Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
21356084Saw148015 		return (ret);
21365184Sek110237 
21376212Saw148015 	if (avd_get_bool(flowop->fo_random)) {
21385184Sek110237 		uint64_t fileoffset;
21395184Sek110237 
21405184Sek110237 		if (filebench_randomno64(&fileoffset,
21416212Saw148015 		    wss, iosize, NULL) == -1) {
21425184Sek110237 			filebench_log(LOG_ERROR,
21435184Sek110237 			    "file size smaller than IO size for thread %s",
21445184Sek110237 			    flowop->fo_name);
21456084Saw148015 			return (FILEBENCH_ERROR);
21465184Sek110237 		}
21475184Sek110237 		flowop_beginop(threadflow, flowop);
21485673Saw148015 		if (pwrite64(filedesc, iobuf,
21496212Saw148015 		    iosize, (off64_t)fileoffset) == -1) {
21505184Sek110237 			filebench_log(LOG_ERROR, "write failed, "
21516286Saw148015 			    "offset %llu io buffer %zd: %s",
21526286Saw148015 			    (u_longlong_t)fileoffset, iobuf, strerror(errno));
21535673Saw148015 			flowop_endop(threadflow, flowop, 0);
21546084Saw148015 			return (FILEBENCH_ERROR);
21555184Sek110237 		}
21566212Saw148015 		flowop_endop(threadflow, flowop, iosize);
21575184Sek110237 	} else {
21585184Sek110237 		flowop_beginop(threadflow, flowop);
2159*6391Saw148015 		if (write(filedesc, iobuf, iosize) == -1) {
21605184Sek110237 			filebench_log(LOG_ERROR,
21615673Saw148015 			    "write failed, io buffer %zd: %s",
21625673Saw148015 			    iobuf, strerror(errno));
21635673Saw148015 			flowop_endop(threadflow, flowop, 0);
21646084Saw148015 			return (FILEBENCH_ERROR);
21655184Sek110237 		}
21666212Saw148015 		flowop_endop(threadflow, flowop, iosize);
21675184Sek110237 	}
21685184Sek110237 
21696084Saw148015 	return (FILEBENCH_OK);
21705184Sek110237 }
21715184Sek110237 
21725184Sek110237 /*
21735184Sek110237  * Emulate a write of a whole file.  The size of the file
21745673Saw148015  * is taken from a filesetentry identified by fo_srcfdnumber or
21755673Saw148015  * from the working set size, while the file descriptor used is
21765673Saw148015  * identified by fo_fdnumber. Does multiple writes of fo_iosize
21776084Saw148015  * length length until full file has been written. Returns FILEBENCH_ERROR on
21786084Saw148015  * error, FILEBENCH_NORSC if out of files, FILEBENCH_OK on success.
21795184Sek110237  */
21805184Sek110237 static int
21815184Sek110237 flowoplib_writewholefile(threadflow_t *threadflow, flowop_t *flowop)
21825184Sek110237 {
21835673Saw148015 	caddr_t iobuf;
21845184Sek110237 	filesetentry_t *file;
21855184Sek110237 	int wsize;
21865184Sek110237 	off64_t seek;
21875184Sek110237 	off64_t bytes = 0;
21885673Saw148015 	uint64_t wss;
21896212Saw148015 	fbint_t iosize;
21905673Saw148015 	int filedesc;
21915184Sek110237 	int srcfd = flowop->fo_srcfdnumber;
21925184Sek110237 	int ret;
21936212Saw148015 	char zerowrtbuf;
21945184Sek110237 
21955673Saw148015 	/* get the file to use */
21966084Saw148015 	if ((ret = flowoplib_filesetup(threadflow, flowop, &wss,
21976084Saw148015 	    &filedesc)) != FILEBENCH_OK)
21986084Saw148015 		return (ret);
21995184Sek110237 
22006212Saw148015 	/* an I/O size of zero means write entire working set with one I/O */
22016212Saw148015 	if ((iosize = avd_get_int(flowop->fo_iosize)) == 0)
22025673Saw148015 		iosize = wss;
22035184Sek110237 
22046212Saw148015 	/*
22056212Saw148015 	 * The file may actually be 0 bytes long, in which case skip
22066212Saw148015 	 * the buffer set up call (which would fail) and substitute
22076212Saw148015 	 * a small buffer, which won't really be used.
22086212Saw148015 	 */
22096212Saw148015 	if (iosize == 0) {
22106212Saw148015 		iobuf = (caddr_t)&zerowrtbuf;
22116212Saw148015 		filebench_log(LOG_DEBUG_SCRIPT,
22126212Saw148015 		    "flowop %s wrote zero length file", flowop->fo_name);
22136212Saw148015 	} else {
22146212Saw148015 		if (flowoplib_iobufsetup(threadflow, flowop, &iobuf,
22156212Saw148015 		    iosize) != 0)
22166212Saw148015 			return (FILEBENCH_ERROR);
22176212Saw148015 	}
22185184Sek110237 
22195184Sek110237 	file = threadflow->tf_fse[srcfd];
22205673Saw148015 	if ((srcfd != 0) && (file == NULL)) {
22215673Saw148015 		filebench_log(LOG_ERROR, "flowop %s: NULL src file",
22225184Sek110237 		    flowop->fo_name);
22236084Saw148015 		return (FILEBENCH_ERROR);
22245184Sek110237 	}
22255184Sek110237 
22265673Saw148015 	if (file)
22275673Saw148015 		wss = file->fse_size;
22285673Saw148015 
22295673Saw148015 	wsize = (int)MIN(wss, iosize);
22305184Sek110237 
22315184Sek110237 	/* Measure time to write bytes */
22325184Sek110237 	flowop_beginop(threadflow, flowop);
22335673Saw148015 	for (seek = 0; seek < wss; seek += wsize) {
22345673Saw148015 		ret = write(filedesc, iobuf, wsize);
22355184Sek110237 		if (ret != wsize) {
22365184Sek110237 			filebench_log(LOG_ERROR,
22375184Sek110237 			    "Failed to write %d bytes on fd %d: %s",
22385673Saw148015 			    wsize, filedesc, strerror(errno));
22395673Saw148015 			flowop_endop(threadflow, flowop, 0);
22406084Saw148015 			return (FILEBENCH_ERROR);
22415184Sek110237 		}
22425673Saw148015 		wsize = (int)MIN(wss - seek, iosize);
22435184Sek110237 		bytes += ret;
22445184Sek110237 	}
22455673Saw148015 	flowop_endop(threadflow, flowop, bytes);
22465184Sek110237 
22476084Saw148015 	return (FILEBENCH_OK);
22485184Sek110237 }
22495184Sek110237 
22505184Sek110237 
22515184Sek110237 /*
22525184Sek110237  * Emulate a fixed size append to a file. Will append data to
22535184Sek110237  * a file chosen from a fileset if the flowop's fo_fileset
22545184Sek110237  * field specifies one or if its fdnumber is non zero.
22555184Sek110237  * Otherwise it will write to a fileobj file, if one exists.
22565184Sek110237  * The flowop's fo_wss parameter will be used to set the
22575184Sek110237  * maximum file size if it is non-zero, otherwise the
22585184Sek110237  * filesetentry's fse_size will be used. A random memory
22595184Sek110237  * buffer offset is calculated, then a logical seek to the
22605184Sek110237  * end of file is done followed by a write of fo_iosize
22615184Sek110237  * bytes. Writes are actually done from fo_buf, rather than
22625184Sek110237  * tf_mem as is done with flowoplib_write(), and no check
22635184Sek110237  * is made to see if fo_iosize exceeds the size of fo_buf.
22646084Saw148015  * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of
22656084Saw148015  * files in the fileset, FILEBENCH_OK on success.
22665184Sek110237  */
22675184Sek110237 static int
22685184Sek110237 flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop)
22695184Sek110237 {
22705673Saw148015 	caddr_t iobuf;
22715673Saw148015 	int filedesc;
22726212Saw148015 	fbint_t wss;
22736212Saw148015 	fbint_t iosize;
22745184Sek110237 	int ret;
22755184Sek110237 
22766212Saw148015 	iosize = avd_get_int(flowop->fo_iosize);
22776084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
22786084Saw148015 	    &filedesc, iosize)) != FILEBENCH_OK)
22796084Saw148015 		return (ret);
22805184Sek110237 
22815184Sek110237 	/* XXX wss is not being used */
22825184Sek110237 
22835184Sek110237 	/* Measure time to write bytes */
22845184Sek110237 	flowop_beginop(threadflow, flowop);
22855184Sek110237 	(void) lseek64(filedesc, 0, SEEK_END);
22865673Saw148015 	ret = write(filedesc, iobuf, iosize);
22875673Saw148015 	if (ret != iosize) {
22885184Sek110237 		filebench_log(LOG_ERROR,
22896286Saw148015 		    "Failed to write %llu bytes on fd %d: %s",
22906286Saw148015 		    (u_longlong_t)iosize, filedesc, strerror(errno));
22916212Saw148015 		flowop_endop(threadflow, flowop, ret);
22926084Saw148015 		return (FILEBENCH_ERROR);
22935184Sek110237 	}
22946212Saw148015 	flowop_endop(threadflow, flowop, ret);
22955184Sek110237 
22966084Saw148015 	return (FILEBENCH_OK);
22975184Sek110237 }
22985184Sek110237 
22995184Sek110237 /*
23005184Sek110237  * Emulate a random size append to a file. Will append data
23015184Sek110237  * to a file chosen from a fileset if the flowop's fo_fileset
23025184Sek110237  * field specifies one or if its fdnumber is non zero. Otherwise
23035184Sek110237  * it will write to a fileobj file, if one exists. The flowop's
23045184Sek110237  * fo_wss parameter will be used to set the maximum file size
23055184Sek110237  * if it is non-zero, otherwise the filesetentry's fse_size
23065184Sek110237  * will be used.  A random transfer size (but at most fo_iosize
23075184Sek110237  * bytes) and a random memory offset are calculated. A logical
23085184Sek110237  * seek to the end of file is done, then writes of up to
23095184Sek110237  * FILE_ALLOC_BLOCK in size are done until the full transfer
23105184Sek110237  * size has been written. Writes are actually done from fo_buf,
23115184Sek110237  * rather than tf_mem as is done with flowoplib_write().
23126084Saw148015  * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of
23136084Saw148015  * files in the fileset, FILEBENCH_OK on success.
23145184Sek110237  */
23155184Sek110237 static int
23165184Sek110237 flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop)
23175184Sek110237 {
23185673Saw148015 	caddr_t iobuf;
23195184Sek110237 	uint64_t appendsize;
23205673Saw148015 	int filedesc;
23216212Saw148015 	fbint_t wss;
23226212Saw148015 	fbint_t iosize;
23236212Saw148015 	int ret = 0;
23245184Sek110237 
23256212Saw148015 	if ((iosize = avd_get_int(flowop->fo_iosize)) == 0) {
23266212Saw148015 		filebench_log(LOG_ERROR, "zero iosize for flowop %s",
23276212Saw148015 		    flowop->fo_name);
23286212Saw148015 		return (FILEBENCH_ERROR);
23296212Saw148015 	}
23306212Saw148015 
23316212Saw148015 	if (filebench_randomno64(&appendsize, iosize, 1LL, NULL) != 0)
23326084Saw148015 		return (FILEBENCH_ERROR);
23335184Sek110237 
23345673Saw148015 	/* skip if attempting zero length append */
23355673Saw148015 	if (appendsize == 0) {
23365673Saw148015 		flowop_beginop(threadflow, flowop);
23375673Saw148015 		flowop_endop(threadflow, flowop, 0LL);
23386084Saw148015 		return (FILEBENCH_OK);
23395673Saw148015 	}
23405184Sek110237 
23416084Saw148015 	if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
23426084Saw148015 	    &filedesc, appendsize)) != FILEBENCH_OK)
23436084Saw148015 		return (ret);
23445673Saw148015 
23455184Sek110237 	/* XXX wss is not being used */
23465184Sek110237 
23475673Saw148015 	/* Measure time to write bytes */
23485673Saw148015 	flowop_beginop(threadflow, flowop);
23495673Saw148015 
23505673Saw148015 	(void) lseek64(filedesc, 0, SEEK_END);
23515673Saw148015 	ret = write(filedesc, iobuf, appendsize);
23525673Saw148015 	if (ret != appendsize) {
23535673Saw148015 		filebench_log(LOG_ERROR,
23546286Saw148015 		    "Failed to write %llu bytes on fd %d: %s",
23556286Saw148015 		    (u_longlong_t)appendsize, filedesc, strerror(errno));
23565673Saw148015 		flowop_endop(threadflow, flowop, 0);
23576084Saw148015 		return (FILEBENCH_ERROR);
23585184Sek110237 	}
23595184Sek110237 
23605673Saw148015 	flowop_endop(threadflow, flowop, appendsize);
23615184Sek110237 
23626084Saw148015 	return (FILEBENCH_OK);
23635184Sek110237 }
23645184Sek110237 
23656212Saw148015 typedef struct testrandvar_priv {
23666212Saw148015 	uint64_t sample_count;
23676212Saw148015 	double val_sum;
23686212Saw148015 	double sqr_sum;
23696212Saw148015 } testrandvar_priv_t;
23706212Saw148015 
23716212Saw148015 /*
23726212Saw148015  * flowop to calculate various statistics from the number stream
23736212Saw148015  * produced by a random variable. This allows verification that the
23746212Saw148015  * random distribution used to define the random variable is producing
23756212Saw148015  * the expected distribution of random numbers.
23766212Saw148015  */
23776212Saw148015 /* ARGSUSED */
23786212Saw148015 static int
23796212Saw148015 flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop)
23806212Saw148015 {
23816212Saw148015 	testrandvar_priv_t	*mystats;
23826212Saw148015 	double			value;
23836212Saw148015 
23846212Saw148015 	if ((mystats = (testrandvar_priv_t *)flowop->fo_private) == NULL) {
23856212Saw148015 		filebench_log(LOG_ERROR, "testrandvar not initialized\n");
23866212Saw148015 		filebench_shutdown(1);
23876212Saw148015 		return (-1);
23886212Saw148015 	}
23896212Saw148015 
23906212Saw148015 	value = avd_get_dbl(flowop->fo_value);
23916212Saw148015 
23926212Saw148015 	mystats->sample_count++;
23936212Saw148015 	mystats->val_sum += value;
23946212Saw148015 	mystats->sqr_sum += (value * value);
23956212Saw148015 
23966212Saw148015 	return (0);
23976212Saw148015 }
23986212Saw148015 
23996212Saw148015 /*
24006212Saw148015  * Initialize the private data area used to accumulate the statistics
24016212Saw148015  */
24026212Saw148015 static int
24036212Saw148015 flowoplib_testrandvar_init(flowop_t *flowop)
24046212Saw148015 {
24056212Saw148015 	testrandvar_priv_t	*mystats;
24066212Saw148015 
24076212Saw148015 	if ((mystats = (testrandvar_priv_t *)
24086212Saw148015 	    malloc(sizeof (testrandvar_priv_t))) == NULL) {
24096212Saw148015 		filebench_log(LOG_ERROR, "could not initialize testrandvar");
24106212Saw148015 		filebench_shutdown(1);
24116212Saw148015 		return (-1);
24126212Saw148015 	}
24136212Saw148015 
24146212Saw148015 	mystats->sample_count = 0;
24156212Saw148015 	mystats->val_sum = 0;
24166212Saw148015 	mystats->sqr_sum = 0;
24176212Saw148015 	flowop->fo_private = (void *)mystats;
24186212Saw148015 
24196212Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
24206212Saw148015 	return (0);
24216212Saw148015 }
24226212Saw148015 
24236212Saw148015 /*
24246212Saw148015  * Print out the accumulated statistics, and free the private storage
24256212Saw148015  */
24266212Saw148015 static void
24276212Saw148015 flowoplib_testrandvar_destruct(flowop_t *flowop)
24286212Saw148015 {
24296212Saw148015 	testrandvar_priv_t	*mystats;
24306212Saw148015 	double mean, std_dev, dbl_count;
24316212Saw148015 
24326212Saw148015 	(void) ipc_mutex_lock(&flowop->fo_lock);
24336212Saw148015 	if ((mystats = (testrandvar_priv_t *)
24346212Saw148015 	    flowop->fo_private) == NULL) {
24356212Saw148015 		(void) ipc_mutex_unlock(&flowop->fo_lock);
24366212Saw148015 		return;
24376212Saw148015 	}
24386212Saw148015 
24396212Saw148015 	flowop->fo_private = NULL;
24406212Saw148015 	(void) ipc_mutex_unlock(&flowop->fo_lock);
24416212Saw148015 
24426212Saw148015 	dbl_count = (double)mystats->sample_count;
24436212Saw148015 	mean = mystats->val_sum / dbl_count;
24446212Saw148015 	std_dev = sqrt((mystats->sqr_sum / dbl_count) - (mean * mean)) / mean;
24456212Saw148015 
24466212Saw148015 	filebench_log(LOG_VERBOSE,
24476286Saw148015 	    "testrandvar: ops = %llu, mean = %8.2lf, stddev = %8.2lf",
24486286Saw148015 	    (u_longlong_t)mystats->sample_count, mean, std_dev);
24496212Saw148015 	free(mystats);
24506212Saw148015 }
24515184Sek110237 
24525184Sek110237 /*
24535184Sek110237  * Prints usage information for flowop operations.
24545184Sek110237  */
24555184Sek110237 void
24565184Sek110237 flowoplib_usage()
24575184Sek110237 {
24585184Sek110237 	(void) fprintf(stderr,
24595184Sek110237 	    "flowop [openfile|createfile] name=<name>,fileset=<fname>\n");
24605184Sek110237 	(void) fprintf(stderr,
24615184Sek110237 	    "                       [,fd=<file desc num>]\n");
24625184Sek110237 	(void) fprintf(stderr, "\n");
24635184Sek110237 	(void) fprintf(stderr,
24645184Sek110237 	    "flowop closefile name=<name>,fd=<file desc num>]\n");
24655184Sek110237 	(void) fprintf(stderr, "\n");
24665184Sek110237 	(void) fprintf(stderr, "flowop deletefile name=<name>\n");
24675184Sek110237 	(void) fprintf(stderr, "                       [,fileset=<fname>]\n");
24685184Sek110237 	(void) fprintf(stderr,
24695184Sek110237 	    "                       [,fd=<file desc num>]\n");
24705184Sek110237 	(void) fprintf(stderr, "\n");
24715184Sek110237 	(void) fprintf(stderr, "flowop statfile name=<name>\n");
24725184Sek110237 	(void) fprintf(stderr, "                       [,fileset=<fname>]\n");
24735184Sek110237 	(void) fprintf(stderr,
24745184Sek110237 	    "                       [,fd=<file desc num>]\n");
24755184Sek110237 	(void) fprintf(stderr, "\n");
24765184Sek110237 	(void) fprintf(stderr,
24775184Sek110237 	    "flowop fsync name=<name>,fd=<file desc num>]\n");
24785184Sek110237 	(void) fprintf(stderr, "\n");
24795184Sek110237 	(void) fprintf(stderr,
24805184Sek110237 	    "flowop fsyncset name=<name>,fileset=<fname>]\n");
24815184Sek110237 	(void) fprintf(stderr, "\n");
24825184Sek110237 	(void) fprintf(stderr, "flowop [write|read|aiowrite] name=<name>, \n");
24835184Sek110237 	(void) fprintf(stderr,
24845184Sek110237 	    "                       filename|fileset=<fname>,\n");
24855184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
24865184Sek110237 	(void) fprintf(stderr, "                       [,directio]\n");
24875184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
24885184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
24895184Sek110237 	(void) fprintf(stderr, "                       [,random]\n");
24905184Sek110237 	(void) fprintf(stderr, "                       [,opennext]\n");
24915184Sek110237 	(void) fprintf(stderr, "                       [,workingset=<size>]\n");
24925184Sek110237 	(void) fprintf(stderr,
24935184Sek110237 	    "flowop [appendfile|appendfilerand] name=<name>, \n");
24945184Sek110237 	(void) fprintf(stderr,
24955184Sek110237 	    "                       filename|fileset=<fname>,\n");
24965184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
24975184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
24985184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
24995184Sek110237 	(void) fprintf(stderr, "                       [,workingset=<size>]\n");
25005184Sek110237 	(void) fprintf(stderr,
25015184Sek110237 	    "flowop [readwholefile|writewholefile] name=<name>, \n");
25025184Sek110237 	(void) fprintf(stderr,
25035184Sek110237 	    "                       filename|fileset=<fname>,\n");
25045184Sek110237 	(void) fprintf(stderr, "                       iosize=<size>\n");
25055184Sek110237 	(void) fprintf(stderr, "                       [,dsync]\n");
25065184Sek110237 	(void) fprintf(stderr, "                       [,iters=<count>]\n");
25075184Sek110237 	(void) fprintf(stderr, "\n");
25085184Sek110237 	(void) fprintf(stderr, "flowop aiowait name=<name>,target="
25095184Sek110237 	    "<aiowrite-flowop>\n");
25105184Sek110237 	(void) fprintf(stderr, "\n");
25115184Sek110237 	(void) fprintf(stderr, "flowop sempost name=<name>,"
25125184Sek110237 	    "target=<semblock-flowop>,\n");
25135184Sek110237 	(void) fprintf(stderr,
25145184Sek110237 	    "                       value=<increment-to-post>\n");
25155184Sek110237 	(void) fprintf(stderr, "\n");
25165184Sek110237 	(void) fprintf(stderr, "flowop semblock name=<name>,value="
25175184Sek110237 	    "<decrement-to-receive>,\n");
25185184Sek110237 	(void) fprintf(stderr, "                       highwater="
25195184Sek110237 	    "<inbound-queue-max>\n");
25205184Sek110237 	(void) fprintf(stderr, "\n");
25215184Sek110237 	(void) fprintf(stderr, "flowop block name=<name>\n");
25225184Sek110237 	(void) fprintf(stderr, "\n");
25235184Sek110237 	(void) fprintf(stderr,
25245184Sek110237 	    "flowop wakeup name=<name>,target=<block-flowop>,\n");
25255184Sek110237 	(void) fprintf(stderr, "\n");
25265184Sek110237 	(void) fprintf(stderr,
25275184Sek110237 	    "flowop hog name=<name>,value=<number-of-mem-ops>\n");
25285184Sek110237 	(void) fprintf(stderr,
25295184Sek110237 	    "flowop delay name=<name>,value=<number-of-seconds>\n");
25305184Sek110237 	(void) fprintf(stderr, "\n");
25315184Sek110237 	(void) fprintf(stderr, "flowop eventlimit name=<name>\n");
25325184Sek110237 	(void) fprintf(stderr, "flowop bwlimit name=<name>,value=<mb/s>\n");
25335184Sek110237 	(void) fprintf(stderr, "flowop iopslimit name=<name>,value=<iop/s>\n");
25345184Sek110237 	(void) fprintf(stderr,
25355184Sek110237 	    "flowop finishoncount name=<name>,value=<ops/s>\n");
25365184Sek110237 	(void) fprintf(stderr,
25375184Sek110237 	    "flowop finishonbytes name=<name>,value=<bytes>\n");
25385184Sek110237 	(void) fprintf(stderr, "\n");
25395184Sek110237 	(void) fprintf(stderr, "\n");
25405184Sek110237 }
2541