15184Sek110237 /* 25184Sek110237 * CDDL HEADER START 35184Sek110237 * 45184Sek110237 * The contents of this file are subject to the terms of the 55184Sek110237 * Common Development and Distribution License (the "License"). 65184Sek110237 * You may not use this file except in compliance with the License. 75184Sek110237 * 85184Sek110237 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95184Sek110237 * or http://www.opensolaris.org/os/licensing. 105184Sek110237 * See the License for the specific language governing permissions 115184Sek110237 * and limitations under the License. 125184Sek110237 * 135184Sek110237 * When distributing Covered Code, include this CDDL HEADER in each 145184Sek110237 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155184Sek110237 * If applicable, add the following below this CDDL HEADER, with the 165184Sek110237 * fields enclosed by brackets "[]" replaced with your own identifying 175184Sek110237 * information: Portions Copyright [yyyy] [name of copyright owner] 185184Sek110237 * 195184Sek110237 * CDDL HEADER END 205184Sek110237 */ 215184Sek110237 /* 22*8615SAndrew.W.Wilson@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 235184Sek110237 * Use is subject to license terms. 246613Sek110237 * 256613Sek110237 * Portions Copyright 2008 Denis Cheng 265184Sek110237 */ 275184Sek110237 285184Sek110237 #include "config.h" 295184Sek110237 305184Sek110237 #include <sys/types.h> 315184Sek110237 #ifdef HAVE_SYS_ASYNCH_H 325184Sek110237 #include <sys/asynch.h> 335184Sek110237 #endif 34*8615SAndrew.W.Wilson@sun.com #include <stddef.h> 355184Sek110237 #include <sys/ipc.h> 365184Sek110237 #include <sys/sem.h> 375184Sek110237 #include <sys/errno.h> 385184Sek110237 #include <sys/time.h> 395184Sek110237 #include <inttypes.h> 405184Sek110237 #include <fcntl.h> 416212Saw148015 #include <math.h> 427946SAndrew.W.Wilson@sun.com #include <dirent.h> 435184Sek110237 445184Sek110237 #ifdef HAVE_UTILITY_H 455184Sek110237 #include <utility.h> 465184Sek110237 #endif /* HAVE_UTILITY_H */ 475184Sek110237 485184Sek110237 #ifdef HAVE_SYS_ASYNC_H 495184Sek110237 #include <sys/asynch.h> 505184Sek110237 #endif /* HAVE_SYS_ASYNC_H */ 515184Sek110237 525184Sek110237 #ifndef HAVE_UINT_T 535184Sek110237 #define uint_t unsigned int 545184Sek110237 #endif /* HAVE_UINT_T */ 555184Sek110237 565184Sek110237 #ifndef HAVE_SYSV_SEM 575184Sek110237 #include <semaphore.h> 585184Sek110237 #endif /* HAVE_SYSV_SEM */ 595184Sek110237 605184Sek110237 #include "filebench.h" 615184Sek110237 #include "flowop.h" 625184Sek110237 #include "fileset.h" 636212Saw148015 #include "fb_random.h" 647946SAndrew.W.Wilson@sun.com #include "utils.h" 65*8615SAndrew.W.Wilson@sun.com #include "fsplug.h" 66*8615SAndrew.W.Wilson@sun.com 675184Sek110237 /* 685184Sek110237 * These routines implement the flowops from the f language. Each 695184Sek110237 * flowop has has a name such as "read", and a set of function pointers 705184Sek110237 * to call for initialization, execution and destruction of the flowop. 715184Sek110237 * The table flowoplib_funcs[] contains a flowoplib struct for each 725184Sek110237 * implemented flowop. Most flowops use a generic initialization function 735184Sek110237 * and all currently use a generic destruction function. All flowop 745184Sek110237 * functions referenced from the table are in this file, though, of 755184Sek110237 * course, they often call functions from other files. 765184Sek110237 * 775184Sek110237 * The flowop_init() routine uses the flowoplib_funcs[] table to 785184Sek110237 * create an initial set of "instance 0" flowops, one for each type of 795184Sek110237 * flowop, from which all other flowops are derived. These "instance 0" 805184Sek110237 * flowops are initialized with information from the table including 815184Sek110237 * pointers for their fo_init, fo_func and fo_destroy functions. When 825184Sek110237 * a flowop definition is encountered in an f language script, the 835184Sek110237 * "type" of flowop, such as "read" is used to search for the 845184Sek110237 * "instance 0" flowop named "read", then a new flowop is allocated 855184Sek110237 * which inherits its function pointers and other initial properties 865184Sek110237 * from the instance 0 flowop, and is given a new name as specified 875184Sek110237 * by the "name=" attribute. 885184Sek110237 */ 895184Sek110237 906084Saw148015 static void flowoplib_destruct_noop(flowop_t *flowop); 915184Sek110237 static int flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop); 927556SAndrew.W.Wilson@sun.com static int flowoplib_print(threadflow_t *threadflow, flowop_t *flowop); 935184Sek110237 static int flowoplib_write(threadflow_t *threadflow, flowop_t *flowop); 945184Sek110237 static int flowoplib_read(threadflow_t *threadflow, flowop_t *flowop); 955184Sek110237 static int flowoplib_block_init(flowop_t *flowop); 965184Sek110237 static int flowoplib_block(threadflow_t *threadflow, flowop_t *flowop); 975184Sek110237 static int flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop); 985184Sek110237 static int flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop); 995184Sek110237 static int flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop); 1005184Sek110237 static int flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop); 1015184Sek110237 static int flowoplib_sempost_init(flowop_t *flowop); 1025184Sek110237 static int flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop); 1035184Sek110237 static int flowoplib_semblock_init(flowop_t *flowop); 1045184Sek110237 static void flowoplib_semblock_destruct(flowop_t *flowop); 1055184Sek110237 static int flowoplib_eventlimit(threadflow_t *, flowop_t *flowop); 1065184Sek110237 static int flowoplib_bwlimit(threadflow_t *, flowop_t *flowop); 1075184Sek110237 static int flowoplib_iopslimit(threadflow_t *, flowop_t *flowop); 1085184Sek110237 static int flowoplib_opslimit(threadflow_t *, flowop_t *flowop); 1095184Sek110237 static int flowoplib_openfile(threadflow_t *, flowop_t *flowop); 1105184Sek110237 static int flowoplib_openfile_common(threadflow_t *, flowop_t *flowop, int fd); 1115184Sek110237 static int flowoplib_createfile(threadflow_t *, flowop_t *flowop); 1125184Sek110237 static int flowoplib_closefile(threadflow_t *, flowop_t *flowop); 1137946SAndrew.W.Wilson@sun.com static int flowoplib_makedir(threadflow_t *, flowop_t *flowop); 1147946SAndrew.W.Wilson@sun.com static int flowoplib_removedir(threadflow_t *, flowop_t *flowop); 1157946SAndrew.W.Wilson@sun.com static int flowoplib_listdir(threadflow_t *, flowop_t *flowop); 1165184Sek110237 static int flowoplib_fsync(threadflow_t *, flowop_t *flowop); 1175184Sek110237 static int flowoplib_readwholefile(threadflow_t *, flowop_t *flowop); 1185184Sek110237 static int flowoplib_writewholefile(threadflow_t *, flowop_t *flowop); 1195184Sek110237 static int flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop); 1205184Sek110237 static int flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop); 1215184Sek110237 static int flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop); 1225184Sek110237 static int flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop); 1235184Sek110237 static int flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop); 1245184Sek110237 static int flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop); 1255184Sek110237 static int flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop); 1266212Saw148015 static int flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop); 1276212Saw148015 static int flowoplib_testrandvar_init(flowop_t *flowop); 1286212Saw148015 static void flowoplib_testrandvar_destruct(flowop_t *flowop); 1295184Sek110237 130*8615SAndrew.W.Wilson@sun.com static flowop_proto_t flowoplib_funcs[] = { 131*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, FLOW_ATTR_WRITE, "write", flowop_init_generic, 132*8615SAndrew.W.Wilson@sun.com flowoplib_write, flowop_destruct_generic, 133*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, FLOW_ATTR_READ, "read", flowop_init_generic, 134*8615SAndrew.W.Wilson@sun.com flowoplib_read, flowop_destruct_generic, 1355184Sek110237 FLOW_TYPE_SYNC, 0, "block", flowoplib_block_init, 136*8615SAndrew.W.Wilson@sun.com flowoplib_block, flowop_destruct_generic, 137*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_SYNC, 0, "wakeup", flowop_init_generic, 138*8615SAndrew.W.Wilson@sun.com flowoplib_wakeup, flowop_destruct_generic, 1395184Sek110237 FLOW_TYPE_SYNC, 0, "semblock", flowoplib_semblock_init, 1405184Sek110237 flowoplib_semblock, flowoplib_semblock_destruct, 1415184Sek110237 FLOW_TYPE_SYNC, 0, "sempost", flowoplib_sempost_init, 1426084Saw148015 flowoplib_sempost, flowoplib_destruct_noop, 143*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "hog", flowop_init_generic, 144*8615SAndrew.W.Wilson@sun.com flowoplib_hog, flowop_destruct_generic, 145*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "delay", flowop_init_generic, 146*8615SAndrew.W.Wilson@sun.com flowoplib_delay, flowop_destruct_generic, 147*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "eventlimit", flowop_init_generic, 148*8615SAndrew.W.Wilson@sun.com flowoplib_eventlimit, flowop_destruct_generic, 149*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "bwlimit", flowop_init_generic, 150*8615SAndrew.W.Wilson@sun.com flowoplib_bwlimit, flowop_destruct_generic, 151*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "iopslimit", flowop_init_generic, 152*8615SAndrew.W.Wilson@sun.com flowoplib_iopslimit, flowop_destruct_generic, 153*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "opslimit", flowop_init_generic, 154*8615SAndrew.W.Wilson@sun.com flowoplib_opslimit, flowop_destruct_generic, 155*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "finishoncount", flowop_init_generic, 156*8615SAndrew.W.Wilson@sun.com flowoplib_finishoncount, flowop_destruct_generic, 157*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "finishonbytes", flowop_init_generic, 158*8615SAndrew.W.Wilson@sun.com flowoplib_finishonbytes, flowop_destruct_generic, 159*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "openfile", flowop_init_generic, 160*8615SAndrew.W.Wilson@sun.com flowoplib_openfile, flowop_destruct_generic, 161*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "createfile", flowop_init_generic, 162*8615SAndrew.W.Wilson@sun.com flowoplib_createfile, flowop_destruct_generic, 163*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "closefile", flowop_init_generic, 164*8615SAndrew.W.Wilson@sun.com flowoplib_closefile, flowop_destruct_generic, 165*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "makedir", flowop_init_generic, 166*8615SAndrew.W.Wilson@sun.com flowoplib_makedir, flowop_destruct_generic, 167*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "removedir", flowop_init_generic, 168*8615SAndrew.W.Wilson@sun.com flowoplib_removedir, flowop_destruct_generic, 169*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "listdir", flowop_init_generic, 170*8615SAndrew.W.Wilson@sun.com flowoplib_listdir, flowop_destruct_generic, 171*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "fsync", flowop_init_generic, 172*8615SAndrew.W.Wilson@sun.com flowoplib_fsync, flowop_destruct_generic, 173*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "fsyncset", flowop_init_generic, 174*8615SAndrew.W.Wilson@sun.com flowoplib_fsyncset, flowop_destruct_generic, 175*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "statfile", flowop_init_generic, 176*8615SAndrew.W.Wilson@sun.com flowoplib_statfile, flowop_destruct_generic, 177*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, FLOW_ATTR_READ, "readwholefile", flowop_init_generic, 178*8615SAndrew.W.Wilson@sun.com flowoplib_readwholefile, flowop_destruct_generic, 179*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfile", flowop_init_generic, 180*8615SAndrew.W.Wilson@sun.com flowoplib_appendfile, flowop_destruct_generic, 181*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfilerand", flowop_init_generic, 182*8615SAndrew.W.Wilson@sun.com flowoplib_appendfilerand, flowop_destruct_generic, 183*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, 0, "deletefile", flowop_init_generic, 184*8615SAndrew.W.Wilson@sun.com flowoplib_deletefile, flowop_destruct_generic, 185*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_IO, FLOW_ATTR_WRITE, "writewholefile", flowop_init_generic, 186*8615SAndrew.W.Wilson@sun.com flowoplib_writewholefile, flowop_destruct_generic, 187*8615SAndrew.W.Wilson@sun.com FLOW_TYPE_OTHER, 0, "print", flowop_init_generic, 188*8615SAndrew.W.Wilson@sun.com flowoplib_print, flowop_destruct_generic, 1896212Saw148015 /* routine to calculate mean and stddev for output from a randvar */ 1906212Saw148015 FLOW_TYPE_OTHER, 0, "testrandvar", flowoplib_testrandvar_init, 1916212Saw148015 flowoplib_testrandvar, flowoplib_testrandvar_destruct 1925184Sek110237 }; 1935184Sek110237 1945184Sek110237 /* 195*8615SAndrew.W.Wilson@sun.com * Loops through the list of flowops defined in this 1965184Sek110237 * module, and creates and initializes a flowop for each one 197*8615SAndrew.W.Wilson@sun.com * by calling flowop_flow_init. As a side effect of calling 198*8615SAndrew.W.Wilson@sun.com * flowop_flow_init, the created flowops are placed on the 1995184Sek110237 * master flowop list. All created flowops are set to 2005184Sek110237 * instance "0". 2015184Sek110237 */ 2025184Sek110237 void 203*8615SAndrew.W.Wilson@sun.com flowoplib_flowinit() 2045184Sek110237 { 205*8615SAndrew.W.Wilson@sun.com int nops = sizeof (flowoplib_funcs) / sizeof (flowop_proto_t); 206*8615SAndrew.W.Wilson@sun.com 207*8615SAndrew.W.Wilson@sun.com flowop_flow_init(flowoplib_funcs, nops); 2086084Saw148015 } 2096084Saw148015 2106084Saw148015 /* 2116084Saw148015 * Special total noop destruct 2126084Saw148015 */ 2136084Saw148015 /* ARGSUSED */ 2146084Saw148015 static void 2156084Saw148015 flowoplib_destruct_noop(flowop_t *flowop) 2166084Saw148015 { 2175184Sek110237 } 2185184Sek110237 2195184Sek110237 /* 2205184Sek110237 * Generates a file attribute from flags in the supplied flowop. 2215184Sek110237 * Sets FLOW_ATTR_DIRECTIO and/or FLOW_ATTR_DSYNC as needed. 2225184Sek110237 */ 2235184Sek110237 static int 2245184Sek110237 flowoplib_fileattrs(flowop_t *flowop) 2255184Sek110237 { 2265184Sek110237 int attrs = 0; 2275184Sek110237 2286212Saw148015 if (avd_get_bool(flowop->fo_directio)) 2295184Sek110237 attrs |= FLOW_ATTR_DIRECTIO; 2305184Sek110237 2316212Saw148015 if (avd_get_bool(flowop->fo_dsync)) 2325184Sek110237 attrs |= FLOW_ATTR_DSYNC; 2335184Sek110237 2345184Sek110237 return (attrs); 2355184Sek110237 } 2365184Sek110237 2375184Sek110237 /* 2388404SAndrew.W.Wilson@sun.com * Obtain a filesetentry for a file. Result placed where filep points. 2398404SAndrew.W.Wilson@sun.com * Supply with a flowop and a flag to indicate whether an existent or 2408404SAndrew.W.Wilson@sun.com * non-existent file is required. Returns FILEBENCH_NORSC if all out 2418404SAndrew.W.Wilson@sun.com * of the appropriate type of directories, FILEBENCH_ERROR if the 2428404SAndrew.W.Wilson@sun.com * flowop does not point to a fileset, and FILEBENCH_OK otherwise. 2438404SAndrew.W.Wilson@sun.com */ 2448404SAndrew.W.Wilson@sun.com static int 2458404SAndrew.W.Wilson@sun.com flowoplib_pickfile(filesetentry_t **filep, flowop_t *flowop, int flags, int tid) 2468404SAndrew.W.Wilson@sun.com { 2478404SAndrew.W.Wilson@sun.com fileset_t *fileset; 2488404SAndrew.W.Wilson@sun.com int fileindex; 2498404SAndrew.W.Wilson@sun.com 2508404SAndrew.W.Wilson@sun.com if ((fileset = flowop->fo_fileset) == NULL) { 2518404SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, "flowop NO fileset"); 2528404SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 2538404SAndrew.W.Wilson@sun.com } 2548404SAndrew.W.Wilson@sun.com 2558404SAndrew.W.Wilson@sun.com if (flowop->fo_fileindex) { 2568404SAndrew.W.Wilson@sun.com fileindex = (int)(avd_get_dbl(flowop->fo_fileindex) * 2578404SAndrew.W.Wilson@sun.com ((double)(fileset->fs_constentries / 2))); 2588404SAndrew.W.Wilson@sun.com fileindex = fileindex % fileset->fs_constentries; 2598404SAndrew.W.Wilson@sun.com flags |= FILESET_PICKBYINDEX; 2608404SAndrew.W.Wilson@sun.com } else { 2618404SAndrew.W.Wilson@sun.com fileindex = 0; 2628404SAndrew.W.Wilson@sun.com } 2638404SAndrew.W.Wilson@sun.com 2648404SAndrew.W.Wilson@sun.com if ((*filep = fileset_pick(fileset, FILESET_PICKFILE | flags, 2658404SAndrew.W.Wilson@sun.com tid, fileindex)) == NULL) { 2668404SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 2678404SAndrew.W.Wilson@sun.com "flowop %s failed to pick file from fileset %s", 2688404SAndrew.W.Wilson@sun.com flowop->fo_name, 2698404SAndrew.W.Wilson@sun.com avd_get_str(fileset->fs_name)); 2708404SAndrew.W.Wilson@sun.com return (FILEBENCH_NORSC); 2718404SAndrew.W.Wilson@sun.com } 2728404SAndrew.W.Wilson@sun.com 2738404SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 2748404SAndrew.W.Wilson@sun.com } 2758404SAndrew.W.Wilson@sun.com 2768404SAndrew.W.Wilson@sun.com /* 2778404SAndrew.W.Wilson@sun.com * Obtain a filesetentry for a leaf directory. Result placed where dirp 2788404SAndrew.W.Wilson@sun.com * points. Supply with flowop and a flag to indicate whether an existent 2798404SAndrew.W.Wilson@sun.com * or non-existent leaf directory is required. Returns FILEBENCH_NORSC 2808404SAndrew.W.Wilson@sun.com * if all out of the appropriate type of directories, FILEBENCH_ERROR 2818404SAndrew.W.Wilson@sun.com * if the flowop does not point to a fileset, and FILEBENCH_OK otherwise. 2828404SAndrew.W.Wilson@sun.com */ 2838404SAndrew.W.Wilson@sun.com static int 2848404SAndrew.W.Wilson@sun.com flowoplib_pickleafdir(filesetentry_t **dirp, flowop_t *flowop, int flags) 2858404SAndrew.W.Wilson@sun.com { 2868404SAndrew.W.Wilson@sun.com fileset_t *fileset; 2878404SAndrew.W.Wilson@sun.com int dirindex; 2888404SAndrew.W.Wilson@sun.com 2898404SAndrew.W.Wilson@sun.com if ((fileset = flowop->fo_fileset) == NULL) { 2908404SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, "flowop NO fileset"); 2918404SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 2928404SAndrew.W.Wilson@sun.com } 2938404SAndrew.W.Wilson@sun.com 2948404SAndrew.W.Wilson@sun.com if (flowop->fo_fileindex) { 2958404SAndrew.W.Wilson@sun.com dirindex = (int)(avd_get_dbl(flowop->fo_fileindex) * 2968404SAndrew.W.Wilson@sun.com ((double)(fileset->fs_constleafdirs / 2))); 2978404SAndrew.W.Wilson@sun.com dirindex = dirindex % fileset->fs_constleafdirs; 2988404SAndrew.W.Wilson@sun.com flags |= FILESET_PICKBYINDEX; 2998404SAndrew.W.Wilson@sun.com } else { 3008404SAndrew.W.Wilson@sun.com dirindex = 0; 3018404SAndrew.W.Wilson@sun.com } 3028404SAndrew.W.Wilson@sun.com 3038404SAndrew.W.Wilson@sun.com if ((*dirp = fileset_pick(fileset, 3048404SAndrew.W.Wilson@sun.com FILESET_PICKLEAFDIR | flags, 0, dirindex)) == NULL) { 3058404SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 3068404SAndrew.W.Wilson@sun.com "flowop %s failed to pick directory from fileset %s", 3078404SAndrew.W.Wilson@sun.com flowop->fo_name, 3088404SAndrew.W.Wilson@sun.com avd_get_str(fileset->fs_name)); 3098404SAndrew.W.Wilson@sun.com return (FILEBENCH_NORSC); 3108404SAndrew.W.Wilson@sun.com } 3118404SAndrew.W.Wilson@sun.com 3128404SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 3138404SAndrew.W.Wilson@sun.com } 3148404SAndrew.W.Wilson@sun.com 3158404SAndrew.W.Wilson@sun.com /* 3165184Sek110237 * Searches for a file descriptor. Tries the flowop's 3175184Sek110237 * fo_fdnumber first and returns with it if it has been 3185184Sek110237 * explicitly set (greater than 0). It next checks to 3195184Sek110237 * see if a rotating file descriptor policy is in effect, 3205184Sek110237 * and if not returns the fdnumber regardless of what 3215184Sek110237 * it is. (note that if it is 0, it just selects to the 3225184Sek110237 * default file descriptor in the threadflow's tf_fd 3235184Sek110237 * array). If the rotating fd policy is in effect, it 3245184Sek110237 * cycles from the end of the tf_fd array to one location 3255184Sek110237 * beyond the maximum needed by the number of entries in 3265184Sek110237 * the associated fileset on each invocation, then starts 3275184Sek110237 * over from the end. 3285184Sek110237 * 3295184Sek110237 * The routine returns an index into the threadflow's 3305184Sek110237 * tf_fd table where the actual file descriptor will be 3315184Sek110237 * found. Note: the calling routine must not call this 3325184Sek110237 * routine if the flowop does not have a fileset, and the 3335184Sek110237 * flowop's fo_fdnumber is zero and fo_rotatefd is 3345184Sek110237 * asserted, or an addressing fault may occur. 3355184Sek110237 */ 3365673Saw148015 static int 3375184Sek110237 flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop) 3385184Sek110237 { 3396212Saw148015 fbint_t entries; 3406391Saw148015 int fdnumber = flowop->fo_fdnumber; 3416212Saw148015 3425184Sek110237 /* If the script sets the fd explicitly */ 3436391Saw148015 if (fdnumber > 0) 3446391Saw148015 return (fdnumber); 3455184Sek110237 3465184Sek110237 /* If the flowop defaults to persistent fd */ 3476212Saw148015 if (!avd_get_bool(flowop->fo_rotatefd)) 3486391Saw148015 return (fdnumber); 3496391Saw148015 3506391Saw148015 if (flowop->fo_fileset == NULL) { 3516391Saw148015 filebench_log(LOG_ERROR, "flowop NULL file"); 3526391Saw148015 return (FILEBENCH_ERROR); 3536391Saw148015 } 3545184Sek110237 3556212Saw148015 entries = flowop->fo_fileset->fs_constentries; 3566212Saw148015 3575184Sek110237 /* Rotate the fd on each flowop invocation */ 3586212Saw148015 if (entries > (THREADFLOW_MAXFD / 2)) { 3595184Sek110237 filebench_log(LOG_ERROR, "Out of file descriptors in flowop %s" 3606286Saw148015 " (too many files : %llu", 3616286Saw148015 flowop->fo_name, (u_longlong_t)entries); 3626084Saw148015 return (FILEBENCH_ERROR); 3635184Sek110237 } 3645184Sek110237 3655184Sek110237 /* First time around */ 3665184Sek110237 if (threadflow->tf_fdrotor == 0) 3675184Sek110237 threadflow->tf_fdrotor = THREADFLOW_MAXFD; 3685184Sek110237 3695184Sek110237 /* One fd for every file in the set */ 3706212Saw148015 if (entries == (THREADFLOW_MAXFD - threadflow->tf_fdrotor)) 3715184Sek110237 threadflow->tf_fdrotor = THREADFLOW_MAXFD; 3725184Sek110237 3735184Sek110237 3745184Sek110237 threadflow->tf_fdrotor--; 3755184Sek110237 filebench_log(LOG_DEBUG_IMPL, "selected fd = %d", 3765184Sek110237 threadflow->tf_fdrotor); 3775184Sek110237 return (threadflow->tf_fdrotor); 3785184Sek110237 } 3795184Sek110237 3805184Sek110237 /* 3815673Saw148015 * Determines the file descriptor to use, and attempts to open 3825673Saw148015 * the file if it is not already open. Also determines the wss 3836084Saw148015 * value. Returns FILEBENCH_ERROR on errors, FILESET_NORSC if 3846084Saw148015 * if flowop_openfile_common couldn't obtain an appropriate file 3856084Saw148015 * from a the fileset, and FILEBENCH_OK otherwise. 3865673Saw148015 */ 3875673Saw148015 static int 3885673Saw148015 flowoplib_filesetup(threadflow_t *threadflow, flowop_t *flowop, 389*8615SAndrew.W.Wilson@sun.com fbint_t *wssp, fb_fdesc_t **fdescp) 3905673Saw148015 { 3915673Saw148015 int fd = flowoplib_fdnum(threadflow, flowop); 3925673Saw148015 3935673Saw148015 if (fd == -1) 3946084Saw148015 return (FILEBENCH_ERROR); 3955673Saw148015 396*8615SAndrew.W.Wilson@sun.com if (threadflow->tf_fd[fd].fd_ptr == NULL) { 3976084Saw148015 int ret; 3986084Saw148015 3996084Saw148015 if ((ret = flowoplib_openfile_common( 4006084Saw148015 threadflow, flowop, fd)) != FILEBENCH_OK) 4016084Saw148015 return (ret); 4025673Saw148015 4035673Saw148015 if (threadflow->tf_fse[fd]) { 4045673Saw148015 filebench_log(LOG_DEBUG_IMPL, "opened file %s", 4055673Saw148015 threadflow->tf_fse[fd]->fse_path); 4065673Saw148015 } else { 4075673Saw148015 filebench_log(LOG_DEBUG_IMPL, 4085673Saw148015 "opened device %s/%s", 4096212Saw148015 avd_get_str(flowop->fo_fileset->fs_path), 4106212Saw148015 avd_get_str(flowop->fo_fileset->fs_name)); 4115673Saw148015 } 4125673Saw148015 } 4135673Saw148015 414*8615SAndrew.W.Wilson@sun.com *fdescp = &(threadflow->tf_fd[fd]); 4155673Saw148015 4166212Saw148015 if ((*wssp = flowop->fo_constwss) == 0) { 4175673Saw148015 if (threadflow->tf_fse[fd]) 4185673Saw148015 *wssp = threadflow->tf_fse[fd]->fse_size; 4195673Saw148015 else 4206212Saw148015 *wssp = avd_get_int(flowop->fo_fileset->fs_size); 4215673Saw148015 } 4225673Saw148015 4236084Saw148015 return (FILEBENCH_OK); 4245673Saw148015 } 4255673Saw148015 4265673Saw148015 /* 4275673Saw148015 * Determines the io buffer or random offset into tf_mem for 4286084Saw148015 * the IO operation. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise. 4295673Saw148015 */ 4305673Saw148015 static int 4315673Saw148015 flowoplib_iobufsetup(threadflow_t *threadflow, flowop_t *flowop, 4326212Saw148015 caddr_t *iobufp, fbint_t iosize) 4335673Saw148015 { 4345673Saw148015 long memsize; 4355673Saw148015 size_t memoffset; 4365673Saw148015 4375673Saw148015 if (iosize == 0) { 4385673Saw148015 filebench_log(LOG_ERROR, "zero iosize for thread %s", 4395673Saw148015 flowop->fo_name); 4406084Saw148015 return (FILEBENCH_ERROR); 4415673Saw148015 } 4425673Saw148015 4436212Saw148015 if ((memsize = threadflow->tf_constmemsize) != 0) { 4445673Saw148015 4455673Saw148015 /* use tf_mem for I/O with random offset */ 4466212Saw148015 if (filebench_randomno(&memoffset, 4476212Saw148015 memsize, iosize, NULL) == -1) { 4485673Saw148015 filebench_log(LOG_ERROR, 4495673Saw148015 "tf_memsize smaller than IO size for thread %s", 4505673Saw148015 flowop->fo_name); 4516084Saw148015 return (FILEBENCH_ERROR); 4525673Saw148015 } 4535673Saw148015 *iobufp = threadflow->tf_mem + memoffset; 4545673Saw148015 4555673Saw148015 } else { 4565673Saw148015 /* use private I/O buffer */ 4575673Saw148015 if ((flowop->fo_buf != NULL) && 4585673Saw148015 (flowop->fo_buf_size < iosize)) { 4596212Saw148015 /* too small, so free up and re-allocate */ 4605673Saw148015 free(flowop->fo_buf); 4615673Saw148015 flowop->fo_buf = NULL; 4625673Saw148015 } 4636212Saw148015 4646212Saw148015 /* 4656212Saw148015 * Allocate memory for the buffer. The memory is freed 4666212Saw148015 * by flowop_destruct_generic() or by this routine if more 4676212Saw148015 * memory is needed for the buffer. 4686212Saw148015 */ 4695673Saw148015 if ((flowop->fo_buf == NULL) && ((flowop->fo_buf 4705673Saw148015 = (char *)malloc(iosize)) == NULL)) 4716084Saw148015 return (FILEBENCH_ERROR); 4725673Saw148015 4735673Saw148015 flowop->fo_buf_size = iosize; 4745673Saw148015 *iobufp = flowop->fo_buf; 4755673Saw148015 } 4766084Saw148015 return (FILEBENCH_OK); 4775673Saw148015 } 4785673Saw148015 4795673Saw148015 /* 4805673Saw148015 * Determines the file descriptor to use, opens it if necessary, the 4815673Saw148015 * io buffer or random offset into tf_mem for IO operation and the wss 4826084Saw148015 * value. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise. 4835673Saw148015 */ 484*8615SAndrew.W.Wilson@sun.com int 4855673Saw148015 flowoplib_iosetup(threadflow_t *threadflow, flowop_t *flowop, 486*8615SAndrew.W.Wilson@sun.com fbint_t *wssp, caddr_t *iobufp, fb_fdesc_t **filedescp, fbint_t iosize) 4875673Saw148015 { 4886084Saw148015 int ret; 4896084Saw148015 4906084Saw148015 if ((ret = flowoplib_filesetup(threadflow, flowop, wssp, filedescp)) != 4916084Saw148015 FILEBENCH_OK) 4926084Saw148015 return (ret); 4935673Saw148015 4946084Saw148015 if ((ret = flowoplib_iobufsetup(threadflow, flowop, iobufp, iosize)) != 4956084Saw148015 FILEBENCH_OK) 4966084Saw148015 return (ret); 4975673Saw148015 4986084Saw148015 return (FILEBENCH_OK); 4995673Saw148015 } 5005673Saw148015 5015673Saw148015 /* 5025184Sek110237 * Emulate posix read / pread. If the flowop has a fileset, 5035184Sek110237 * a file descriptor number index is fetched, otherwise a 5045184Sek110237 * supplied fileobj file is used. In either case the specified 5055184Sek110237 * file will be opened if not already open. If the flowop has 5066084Saw148015 * neither a fileset or fileobj, an error is logged and FILEBENCH_ERROR 5075184Sek110237 * returned. 5085184Sek110237 * 5095184Sek110237 * The actual read is done to a random offset in the 5105184Sek110237 * threadflow's thread memory (tf_mem), with a size set by 5115184Sek110237 * fo_iosize and at either a random disk offset within the 5125184Sek110237 * working set size, or at the next sequential location. If 5136084Saw148015 * any errors are encountered, FILEBENCH_ERROR is returned, 5146084Saw148015 * if no appropriate file can be obtained from the fileset then 5156084Saw148015 * FILEBENCH_NORSC is returned, otherise FILEBENCH_OK is returned. 5165184Sek110237 */ 5175184Sek110237 static int 5185184Sek110237 flowoplib_read(threadflow_t *threadflow, flowop_t *flowop) 5195184Sek110237 { 5205673Saw148015 caddr_t iobuf; 5216212Saw148015 fbint_t wss; 5226212Saw148015 fbint_t iosize; 523*8615SAndrew.W.Wilson@sun.com fb_fdesc_t *fdesc; 5245184Sek110237 int ret; 5255184Sek110237 5266212Saw148015 5276212Saw148015 iosize = avd_get_int(flowop->fo_iosize); 5286084Saw148015 if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf, 529*8615SAndrew.W.Wilson@sun.com &fdesc, iosize)) != FILEBENCH_OK) 5306084Saw148015 return (ret); 5315184Sek110237 5326212Saw148015 if (avd_get_bool(flowop->fo_random)) { 5335184Sek110237 uint64_t fileoffset; 5345184Sek110237 5356212Saw148015 if (filebench_randomno64(&fileoffset, 5366212Saw148015 wss, iosize, NULL) == -1) { 5375184Sek110237 filebench_log(LOG_ERROR, 5385184Sek110237 "file size smaller than IO size for thread %s", 5395184Sek110237 flowop->fo_name); 5406084Saw148015 return (FILEBENCH_ERROR); 5415184Sek110237 } 5425184Sek110237 5435184Sek110237 (void) flowop_beginop(threadflow, flowop); 544*8615SAndrew.W.Wilson@sun.com if ((ret = FB_PREAD(fdesc, iobuf, 5456212Saw148015 iosize, (off64_t)fileoffset)) == -1) { 5465673Saw148015 (void) flowop_endop(threadflow, flowop, 0); 5475184Sek110237 filebench_log(LOG_ERROR, 5486286Saw148015 "read file %s failed, offset %llu " 5495673Saw148015 "io buffer %zd: %s", 5506212Saw148015 avd_get_str(flowop->fo_fileset->fs_name), 5516286Saw148015 (u_longlong_t)fileoffset, iobuf, strerror(errno)); 5525673Saw148015 flowop_endop(threadflow, flowop, 0); 5536084Saw148015 return (FILEBENCH_ERROR); 5545184Sek110237 } 5555673Saw148015 (void) flowop_endop(threadflow, flowop, ret); 5565184Sek110237 5575184Sek110237 if ((ret == 0)) 558*8615SAndrew.W.Wilson@sun.com (void) FB_LSEEK(fdesc, 0, SEEK_SET); 5595184Sek110237 5605184Sek110237 } else { 5615184Sek110237 (void) flowop_beginop(threadflow, flowop); 562*8615SAndrew.W.Wilson@sun.com if ((ret = FB_READ(fdesc, iobuf, iosize)) == -1) { 5636212Saw148015 (void) flowop_endop(threadflow, flowop, 0); 5645184Sek110237 filebench_log(LOG_ERROR, 5655673Saw148015 "read file %s failed, io buffer %zd: %s", 5666212Saw148015 avd_get_str(flowop->fo_fileset->fs_name), 5675673Saw148015 iobuf, strerror(errno)); 5685673Saw148015 (void) flowop_endop(threadflow, flowop, 0); 5696084Saw148015 return (FILEBENCH_ERROR); 5705184Sek110237 } 5715673Saw148015 (void) flowop_endop(threadflow, flowop, ret); 5725184Sek110237 5735184Sek110237 if ((ret == 0)) 574*8615SAndrew.W.Wilson@sun.com (void) FB_LSEEK(fdesc, 0, SEEK_SET); 5755184Sek110237 } 5765184Sek110237 5776084Saw148015 return (FILEBENCH_OK); 5785184Sek110237 } 5795184Sek110237 5805184Sek110237 /* 5815184Sek110237 * Initializes a "flowop_block" flowop. Specifically, it 5825184Sek110237 * initializes the flowop's fo_cv and unlocks the fo_lock. 5835184Sek110237 */ 5845184Sek110237 static int 5855184Sek110237 flowoplib_block_init(flowop_t *flowop) 5865184Sek110237 { 5875184Sek110237 filebench_log(LOG_DEBUG_IMPL, "flow %s-%d block init address %zx", 5885184Sek110237 flowop->fo_name, flowop->fo_instance, &flowop->fo_cv); 5895184Sek110237 (void) pthread_cond_init(&flowop->fo_cv, ipc_condattr()); 5905184Sek110237 (void) ipc_mutex_unlock(&flowop->fo_lock); 5915184Sek110237 5926084Saw148015 return (FILEBENCH_OK); 5935184Sek110237 } 5945184Sek110237 5955184Sek110237 /* 5965184Sek110237 * Blocks the threadflow until woken up by flowoplib_wakeup. 5975184Sek110237 * The routine blocks on the flowop's fo_cv condition variable. 5985184Sek110237 */ 5995184Sek110237 static int 6005184Sek110237 flowoplib_block(threadflow_t *threadflow, flowop_t *flowop) 6015184Sek110237 { 6025184Sek110237 filebench_log(LOG_DEBUG_IMPL, "flow %s-%d blocking at address %zx", 6035184Sek110237 flowop->fo_name, flowop->fo_instance, &flowop->fo_cv); 6045184Sek110237 (void) ipc_mutex_lock(&flowop->fo_lock); 6055184Sek110237 6065184Sek110237 flowop_beginop(threadflow, flowop); 6075184Sek110237 (void) pthread_cond_wait(&flowop->fo_cv, &flowop->fo_lock); 6085673Saw148015 flowop_endop(threadflow, flowop, 0); 6095184Sek110237 6105184Sek110237 filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking", 6115184Sek110237 flowop->fo_name, flowop->fo_instance); 6125184Sek110237 6135184Sek110237 (void) ipc_mutex_unlock(&flowop->fo_lock); 6145184Sek110237 6156084Saw148015 return (FILEBENCH_OK); 6165184Sek110237 } 6175184Sek110237 6185184Sek110237 /* 6195184Sek110237 * Wakes up one or more target blocking flowops. 6205184Sek110237 * Sends broadcasts on the fo_cv condition variables of all 6215184Sek110237 * flowops on the target list, except those that are 6225184Sek110237 * FLOW_MASTER flowops. The target list consists of all 6235184Sek110237 * flowops whose name matches this flowop's "fo_targetname" 6245184Sek110237 * attribute. The target list is generated on the first 6255184Sek110237 * invocation, and the run will be shutdown if no targets 6266084Saw148015 * are found. Otherwise the routine always returns FILEBENCH_OK. 6275184Sek110237 */ 6285184Sek110237 static int 6295184Sek110237 flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop) 6305184Sek110237 { 6315184Sek110237 flowop_t *target; 6325184Sek110237 6335184Sek110237 /* if this is the first wakeup, create the wakeup list */ 6345184Sek110237 if (flowop->fo_targets == NULL) { 6355184Sek110237 flowop_t *result = flowop_find(flowop->fo_targetname); 6365184Sek110237 6375184Sek110237 flowop->fo_targets = result; 6385184Sek110237 if (result == NULL) { 6395184Sek110237 filebench_log(LOG_ERROR, 6405184Sek110237 "wakeup: could not find op %s for thread %s", 6415184Sek110237 flowop->fo_targetname, 6425184Sek110237 threadflow->tf_name); 6435184Sek110237 filebench_shutdown(1); 6445184Sek110237 } 6455184Sek110237 while (result) { 6465184Sek110237 result->fo_targetnext = 6475184Sek110237 result->fo_resultnext; 6485184Sek110237 result = result->fo_resultnext; 6495184Sek110237 } 6505184Sek110237 } 6515184Sek110237 6525184Sek110237 target = flowop->fo_targets; 6535184Sek110237 6545184Sek110237 /* wakeup the targets */ 6555184Sek110237 while (target) { 6565184Sek110237 if (target->fo_instance == FLOW_MASTER) { 6575184Sek110237 target = target->fo_targetnext; 6585184Sek110237 continue; 6595184Sek110237 } 6605184Sek110237 filebench_log(LOG_DEBUG_IMPL, 6615184Sek110237 "wakeup flow %s-%d at address %zx", 6625184Sek110237 target->fo_name, 6635184Sek110237 target->fo_instance, 6645184Sek110237 &target->fo_cv); 6655184Sek110237 6665184Sek110237 flowop_beginop(threadflow, flowop); 6675184Sek110237 (void) ipc_mutex_lock(&target->fo_lock); 6685184Sek110237 (void) pthread_cond_broadcast(&target->fo_cv); 6695184Sek110237 (void) ipc_mutex_unlock(&target->fo_lock); 6705673Saw148015 flowop_endop(threadflow, flowop, 0); 6715184Sek110237 6725184Sek110237 target = target->fo_targetnext; 6735184Sek110237 } 6745184Sek110237 6756084Saw148015 return (FILEBENCH_OK); 6765184Sek110237 } 6775184Sek110237 6785184Sek110237 /* 6795184Sek110237 * "think time" routines. the "hog" routine consumes cpu cycles as 6805184Sek110237 * it "thinks", while the "delay" flowop simply calls sleep() to delay 6815184Sek110237 * for a given number of seconds without consuming cpu cycles. 6825184Sek110237 */ 6835184Sek110237 6845184Sek110237 6855184Sek110237 /* 6865184Sek110237 * Consumes CPU cycles and memory bandwidth by looping for 6875184Sek110237 * flowop->fo_value times. With each loop sets memory location 6885184Sek110237 * threadflow->tf_mem to 1. 6895184Sek110237 */ 6905184Sek110237 static int 6915184Sek110237 flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop) 6925184Sek110237 { 6936212Saw148015 uint64_t value = avd_get_int(flowop->fo_value); 6945184Sek110237 int i; 6955184Sek110237 6965673Saw148015 filebench_log(LOG_DEBUG_IMPL, "hog enter"); 6975184Sek110237 flowop_beginop(threadflow, flowop); 6985673Saw148015 if (threadflow->tf_mem != NULL) { 6995673Saw148015 for (i = 0; i < value; i++) 7005673Saw148015 *(threadflow->tf_mem) = 1; 7015673Saw148015 } 7025673Saw148015 flowop_endop(threadflow, flowop, 0); 7035184Sek110237 filebench_log(LOG_DEBUG_IMPL, "hog exit"); 7046084Saw148015 return (FILEBENCH_OK); 7055184Sek110237 } 7065184Sek110237 7075184Sek110237 7085184Sek110237 /* 7095184Sek110237 * Delays for fo_value seconds. 7105184Sek110237 */ 7115184Sek110237 static int 7125184Sek110237 flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop) 7135184Sek110237 { 7146212Saw148015 int value = avd_get_int(flowop->fo_value); 7155184Sek110237 7165184Sek110237 flowop_beginop(threadflow, flowop); 7175184Sek110237 (void) sleep(value); 7185673Saw148015 flowop_endop(threadflow, flowop, 0); 7196084Saw148015 return (FILEBENCH_OK); 7205184Sek110237 } 7215184Sek110237 7225184Sek110237 /* 7235184Sek110237 * Rate limiting routines. This is the event consuming half of the 7245184Sek110237 * event system. Each of the four following routines will limit the rate 7255184Sek110237 * to one unit of either calls, issued I/O operations, issued filebench 7265184Sek110237 * operations, or I/O bandwidth. Since there is only one event generator, 7275184Sek110237 * the events will be divided amoung multiple instances of an event 7285184Sek110237 * consumer, and further divided among different consumers if more than 7295184Sek110237 * one has been defined. There is no mechanism to enforce equal sharing 7305184Sek110237 * of events. 7315184Sek110237 */ 7325184Sek110237 7335184Sek110237 /* 7345184Sek110237 * Completes one invocation per posted event. If eventgen_q 7355184Sek110237 * has an event count greater than zero, one will be removed 7365184Sek110237 * (count decremented), otherwise the calling thread will 7375184Sek110237 * block until another event has been posted. Always returns 0 7385184Sek110237 */ 7395184Sek110237 static int 7405184Sek110237 flowoplib_eventlimit(threadflow_t *threadflow, flowop_t *flowop) 7415184Sek110237 { 7425184Sek110237 /* Immediately bail if not set/enabled */ 7437946SAndrew.W.Wilson@sun.com if (filebench_shm->shm_eventgen_hz == NULL) 7446084Saw148015 return (FILEBENCH_OK); 7455184Sek110237 7465184Sek110237 if (flowop->fo_initted == 0) { 7475184Sek110237 filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking", 7485184Sek110237 flowop, threadflow->tf_name, threadflow->tf_instance); 7495184Sek110237 flowop->fo_initted = 1; 7505184Sek110237 } 7515184Sek110237 7525184Sek110237 flowop_beginop(threadflow, flowop); 7537946SAndrew.W.Wilson@sun.com while (filebench_shm->shm_eventgen_hz != NULL) { 7546391Saw148015 (void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock); 7556391Saw148015 if (filebench_shm->shm_eventgen_q > 0) { 7566391Saw148015 filebench_shm->shm_eventgen_q--; 7576391Saw148015 (void) ipc_mutex_unlock( 7586391Saw148015 &filebench_shm->shm_eventgen_lock); 7595184Sek110237 break; 7605184Sek110237 } 7616391Saw148015 (void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv, 7626391Saw148015 &filebench_shm->shm_eventgen_lock); 7636391Saw148015 (void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock); 7645184Sek110237 } 7655673Saw148015 flowop_endop(threadflow, flowop, 0); 7666084Saw148015 return (FILEBENCH_OK); 7675184Sek110237 } 7685184Sek110237 7696701Saw148015 static int 7706701Saw148015 flowoplib_event_find_target(threadflow_t *threadflow, flowop_t *flowop) 7716701Saw148015 { 7726701Saw148015 if (flowop->fo_targetname[0] != '\0') { 7736701Saw148015 7746701Saw148015 /* Try to use statistics from specific flowop */ 7756701Saw148015 flowop->fo_targets = 7766701Saw148015 flowop_find_from_list(flowop->fo_targetname, 7776701Saw148015 threadflow->tf_thrd_fops); 7786701Saw148015 if (flowop->fo_targets == NULL) { 7796701Saw148015 filebench_log(LOG_ERROR, 7806701Saw148015 "limit target: could not find flowop %s", 7816701Saw148015 flowop->fo_targetname); 7826701Saw148015 filebench_shutdown(1); 7836701Saw148015 return (FILEBENCH_ERROR); 7846701Saw148015 } 7856701Saw148015 } else { 7866701Saw148015 /* use total workload statistics */ 7876701Saw148015 flowop->fo_targets = NULL; 7886701Saw148015 } 7896701Saw148015 return (FILEBENCH_OK); 7906701Saw148015 } 7916701Saw148015 7925184Sek110237 /* 7935184Sek110237 * Blocks the calling thread if the number of issued I/O 7945184Sek110237 * operations exceeds the number of posted events, thus 7955184Sek110237 * limiting the average I/O operation rate to the rate 7966084Saw148015 * specified by eventgen_hz. Always returns FILEBENCH_OK. 7975184Sek110237 */ 7985184Sek110237 static int 7995184Sek110237 flowoplib_iopslimit(threadflow_t *threadflow, flowop_t *flowop) 8005184Sek110237 { 8015184Sek110237 uint64_t iops; 8025184Sek110237 uint64_t delta; 8035673Saw148015 uint64_t events; 8045184Sek110237 8055184Sek110237 /* Immediately bail if not set/enabled */ 8067946SAndrew.W.Wilson@sun.com if (filebench_shm->shm_eventgen_hz == NULL) 8076084Saw148015 return (FILEBENCH_OK); 8085184Sek110237 8095184Sek110237 if (flowop->fo_initted == 0) { 8105184Sek110237 filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking", 8115184Sek110237 flowop, threadflow->tf_name, threadflow->tf_instance); 8125184Sek110237 flowop->fo_initted = 1; 8136701Saw148015 8146701Saw148015 if (flowoplib_event_find_target(threadflow, flowop) 8156701Saw148015 == FILEBENCH_ERROR) 8166701Saw148015 return (FILEBENCH_ERROR); 8176701Saw148015 8186701Saw148015 if (flowop->fo_targets && ((flowop->fo_targets->fo_attrs & 8196701Saw148015 (FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) { 8206701Saw148015 filebench_log(LOG_ERROR, 8216701Saw148015 "WARNING: Flowop %s does no IO", 8226701Saw148015 flowop->fo_targets->fo_name); 8236701Saw148015 filebench_shutdown(1); 8246701Saw148015 return (FILEBENCH_ERROR); 8256701Saw148015 } 8265184Sek110237 } 8275184Sek110237 8286701Saw148015 if (flowop->fo_targets) { 8296701Saw148015 /* 8306701Saw148015 * Note that fs_count is already the sum of fs_rcount 8316701Saw148015 * and fs_wcount if looking at a single flowop. 8326701Saw148015 */ 8336701Saw148015 iops = flowop->fo_targets->fo_stats.fs_count; 8346701Saw148015 } else { 8356701Saw148015 (void) ipc_mutex_lock(&controlstats_lock); 8366701Saw148015 iops = (controlstats.fs_rcount + 8376701Saw148015 controlstats.fs_wcount); 8386701Saw148015 (void) ipc_mutex_unlock(&controlstats_lock); 8396701Saw148015 } 8405184Sek110237 8415184Sek110237 /* Is this the first time around */ 8425184Sek110237 if (flowop->fo_tputlast == 0) { 8435184Sek110237 flowop->fo_tputlast = iops; 8446084Saw148015 return (FILEBENCH_OK); 8455184Sek110237 } 8465184Sek110237 8475184Sek110237 delta = iops - flowop->fo_tputlast; 8485184Sek110237 flowop->fo_tputbucket -= delta; 8495184Sek110237 flowop->fo_tputlast = iops; 8505184Sek110237 8515184Sek110237 /* No need to block if the q isn't empty */ 8525184Sek110237 if (flowop->fo_tputbucket >= 0LL) { 8535673Saw148015 flowop_endop(threadflow, flowop, 0); 8546084Saw148015 return (FILEBENCH_OK); 8555184Sek110237 } 8565184Sek110237 8575184Sek110237 iops = flowop->fo_tputbucket * -1; 8585184Sek110237 events = iops; 8595184Sek110237 8605184Sek110237 flowop_beginop(threadflow, flowop); 8617946SAndrew.W.Wilson@sun.com while (filebench_shm->shm_eventgen_hz != NULL) { 8625184Sek110237 8636391Saw148015 (void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock); 8646391Saw148015 if (filebench_shm->shm_eventgen_q >= events) { 8656391Saw148015 filebench_shm->shm_eventgen_q -= events; 8666391Saw148015 (void) ipc_mutex_unlock( 8676391Saw148015 &filebench_shm->shm_eventgen_lock); 8685184Sek110237 flowop->fo_tputbucket += events; 8695184Sek110237 break; 8705184Sek110237 } 8716391Saw148015 (void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv, 8726391Saw148015 &filebench_shm->shm_eventgen_lock); 8736391Saw148015 (void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock); 8745184Sek110237 } 8755673Saw148015 flowop_endop(threadflow, flowop, 0); 8765184Sek110237 8776084Saw148015 return (FILEBENCH_OK); 8785184Sek110237 } 8795184Sek110237 8805184Sek110237 /* 8815184Sek110237 * Blocks the calling thread if the number of issued filebench 8825184Sek110237 * operations exceeds the number of posted events, thus limiting 8835184Sek110237 * the average filebench operation rate to the rate specified by 8846084Saw148015 * eventgen_hz. Always returns FILEBENCH_OK. 8855184Sek110237 */ 8865184Sek110237 static int 8875184Sek110237 flowoplib_opslimit(threadflow_t *threadflow, flowop_t *flowop) 8885184Sek110237 { 8895184Sek110237 uint64_t ops; 8905184Sek110237 uint64_t delta; 8915673Saw148015 uint64_t events; 8925184Sek110237 8935184Sek110237 /* Immediately bail if not set/enabled */ 8947946SAndrew.W.Wilson@sun.com if (filebench_shm->shm_eventgen_hz == NULL) 8956084Saw148015 return (FILEBENCH_OK); 8965184Sek110237 8975184Sek110237 if (flowop->fo_initted == 0) { 8985184Sek110237 filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking", 8995184Sek110237 flowop, threadflow->tf_name, threadflow->tf_instance); 9005184Sek110237 flowop->fo_initted = 1; 9016701Saw148015 9026701Saw148015 if (flowoplib_event_find_target(threadflow, flowop) 9036701Saw148015 == FILEBENCH_ERROR) 9046701Saw148015 return (FILEBENCH_ERROR); 9055184Sek110237 } 9065184Sek110237 9076701Saw148015 if (flowop->fo_targets) { 9086701Saw148015 ops = flowop->fo_targets->fo_stats.fs_count; 9096701Saw148015 } else { 9106701Saw148015 (void) ipc_mutex_lock(&controlstats_lock); 9116701Saw148015 ops = controlstats.fs_count; 9126701Saw148015 (void) ipc_mutex_unlock(&controlstats_lock); 9136701Saw148015 } 9145184Sek110237 9155184Sek110237 /* Is this the first time around */ 9165184Sek110237 if (flowop->fo_tputlast == 0) { 9175184Sek110237 flowop->fo_tputlast = ops; 9186084Saw148015 return (FILEBENCH_OK); 9195184Sek110237 } 9205184Sek110237 9215184Sek110237 delta = ops - flowop->fo_tputlast; 9225184Sek110237 flowop->fo_tputbucket -= delta; 9235184Sek110237 flowop->fo_tputlast = ops; 9245184Sek110237 9255184Sek110237 /* No need to block if the q isn't empty */ 9265184Sek110237 if (flowop->fo_tputbucket >= 0LL) { 9275673Saw148015 flowop_endop(threadflow, flowop, 0); 9286084Saw148015 return (FILEBENCH_OK); 9295184Sek110237 } 9305184Sek110237 9315184Sek110237 ops = flowop->fo_tputbucket * -1; 9325184Sek110237 events = ops; 9335184Sek110237 9345184Sek110237 flowop_beginop(threadflow, flowop); 9357946SAndrew.W.Wilson@sun.com while (filebench_shm->shm_eventgen_hz != NULL) { 9366391Saw148015 (void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock); 9376391Saw148015 if (filebench_shm->shm_eventgen_q >= events) { 9386391Saw148015 filebench_shm->shm_eventgen_q -= events; 9396391Saw148015 (void) ipc_mutex_unlock( 9406391Saw148015 &filebench_shm->shm_eventgen_lock); 9415184Sek110237 flowop->fo_tputbucket += events; 9425184Sek110237 break; 9435184Sek110237 } 9446391Saw148015 (void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv, 9456391Saw148015 &filebench_shm->shm_eventgen_lock); 9466391Saw148015 (void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock); 9475184Sek110237 } 9485673Saw148015 flowop_endop(threadflow, flowop, 0); 9495184Sek110237 9506084Saw148015 return (FILEBENCH_OK); 9515184Sek110237 } 9525184Sek110237 9535184Sek110237 9545184Sek110237 /* 9555184Sek110237 * Blocks the calling thread if the number of bytes of I/O 9565184Sek110237 * issued exceeds one megabyte times the number of posted 9575184Sek110237 * events, thus limiting the average I/O byte rate to one 9585184Sek110237 * megabyte times the event rate as set by eventgen_hz. 9596084Saw148015 * Always retuns FILEBENCH_OK. 9605184Sek110237 */ 9615184Sek110237 static int 9625184Sek110237 flowoplib_bwlimit(threadflow_t *threadflow, flowop_t *flowop) 9635184Sek110237 { 9645184Sek110237 uint64_t bytes; 9655184Sek110237 uint64_t delta; 9665673Saw148015 uint64_t events; 9675184Sek110237 9685184Sek110237 /* Immediately bail if not set/enabled */ 9697946SAndrew.W.Wilson@sun.com if (filebench_shm->shm_eventgen_hz == NULL) 9706084Saw148015 return (FILEBENCH_OK); 9715184Sek110237 9725184Sek110237 if (flowop->fo_initted == 0) { 9735184Sek110237 filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking", 9745184Sek110237 flowop, threadflow->tf_name, threadflow->tf_instance); 9755184Sek110237 flowop->fo_initted = 1; 9766701Saw148015 9776701Saw148015 if (flowoplib_event_find_target(threadflow, flowop) 9786701Saw148015 == FILEBENCH_ERROR) 9796701Saw148015 return (FILEBENCH_ERROR); 9806701Saw148015 9816701Saw148015 if ((flowop->fo_targets) && 9826701Saw148015 ((flowop->fo_targets->fo_attrs & 9836701Saw148015 (FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) { 9846701Saw148015 filebench_log(LOG_ERROR, 9856701Saw148015 "WARNING: Flowop %s does no Reads or Writes", 9866701Saw148015 flowop->fo_targets->fo_name); 9876701Saw148015 filebench_shutdown(1); 9886701Saw148015 return (FILEBENCH_ERROR); 9896701Saw148015 } 9905184Sek110237 } 9915184Sek110237 9926701Saw148015 if (flowop->fo_targets) { 9936701Saw148015 /* 9946701Saw148015 * Note that fs_bytes is already the sum of fs_rbytes 9956701Saw148015 * and fs_wbytes if looking at a single flowop. 9966701Saw148015 */ 9976701Saw148015 bytes = flowop->fo_targets->fo_stats.fs_bytes; 9986701Saw148015 } else { 9996701Saw148015 (void) ipc_mutex_lock(&controlstats_lock); 10006701Saw148015 bytes = (controlstats.fs_rbytes + 10016701Saw148015 controlstats.fs_wbytes); 10026701Saw148015 (void) ipc_mutex_unlock(&controlstats_lock); 10036701Saw148015 } 10046701Saw148015 10056701Saw148015 /* Is this the first time around? */ 10065184Sek110237 if (flowop->fo_tputlast == 0) { 10075184Sek110237 flowop->fo_tputlast = bytes; 10086084Saw148015 return (FILEBENCH_OK); 10095184Sek110237 } 10105184Sek110237 10115184Sek110237 delta = bytes - flowop->fo_tputlast; 10125184Sek110237 flowop->fo_tputbucket -= delta; 10135184Sek110237 flowop->fo_tputlast = bytes; 10145184Sek110237 10155184Sek110237 /* No need to block if the q isn't empty */ 10165184Sek110237 if (flowop->fo_tputbucket >= 0LL) { 10175673Saw148015 flowop_endop(threadflow, flowop, 0); 10186084Saw148015 return (FILEBENCH_OK); 10195184Sek110237 } 10205184Sek110237 10215184Sek110237 bytes = flowop->fo_tputbucket * -1; 10225184Sek110237 events = (bytes / MB) + 1; 10235184Sek110237 10246286Saw148015 filebench_log(LOG_DEBUG_IMPL, "%llu bytes, %llu events", 10256286Saw148015 (u_longlong_t)bytes, (u_longlong_t)events); 10265184Sek110237 10275184Sek110237 flowop_beginop(threadflow, flowop); 10287946SAndrew.W.Wilson@sun.com while (filebench_shm->shm_eventgen_hz != NULL) { 10296391Saw148015 (void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock); 10306391Saw148015 if (filebench_shm->shm_eventgen_q >= events) { 10316391Saw148015 filebench_shm->shm_eventgen_q -= events; 10326391Saw148015 (void) ipc_mutex_unlock( 10336391Saw148015 &filebench_shm->shm_eventgen_lock); 10345184Sek110237 flowop->fo_tputbucket += (events * MB); 10355184Sek110237 break; 10365184Sek110237 } 10376391Saw148015 (void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv, 10386391Saw148015 &filebench_shm->shm_eventgen_lock); 10396391Saw148015 (void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock); 10405184Sek110237 } 10415673Saw148015 flowop_endop(threadflow, flowop, 0); 10425184Sek110237 10436084Saw148015 return (FILEBENCH_OK); 10445184Sek110237 } 10455184Sek110237 10465184Sek110237 /* 10475184Sek110237 * These flowops terminate a benchmark run when either the specified 10485184Sek110237 * number of bytes of I/O (flowoplib_finishonbytes) or the specified 10495184Sek110237 * number of I/O operations (flowoplib_finishoncount) have been generated. 10505184Sek110237 */ 10515184Sek110237 10525184Sek110237 10535184Sek110237 /* 10545184Sek110237 * Stop filebench run when specified number of I/O bytes have been 10556212Saw148015 * transferred. Compares controlstats.fs_bytes with flowop->value, 10565184Sek110237 * and if greater returns 1, stopping the run, if not, returns 0 10575184Sek110237 * to continue running. 10585184Sek110237 */ 10595184Sek110237 static int 10605184Sek110237 flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop) 10615184Sek110237 { 10626701Saw148015 uint64_t bytes_io; /* Bytes of I/O delivered so far */ 10636701Saw148015 uint64_t byte_lim = flowop->fo_constvalue; /* Total Bytes desired */ 10646701Saw148015 /* Uses constant value */ 10656701Saw148015 10666701Saw148015 if (flowop->fo_initted == 0) { 10676701Saw148015 filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking", 10686701Saw148015 flowop, threadflow->tf_name, threadflow->tf_instance); 10696701Saw148015 flowop->fo_initted = 1; 10706701Saw148015 10716701Saw148015 if (flowoplib_event_find_target(threadflow, flowop) 10726701Saw148015 == FILEBENCH_ERROR) 10736701Saw148015 return (FILEBENCH_ERROR); 10746701Saw148015 10756701Saw148015 if ((flowop->fo_targets) && 10766701Saw148015 ((flowop->fo_targets->fo_attrs & 10776701Saw148015 (FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) { 10786701Saw148015 filebench_log(LOG_ERROR, 10796701Saw148015 "WARNING: Flowop %s does no Reads or Writes", 10806701Saw148015 flowop->fo_targets->fo_name); 10816701Saw148015 filebench_shutdown(1); 10826701Saw148015 return (FILEBENCH_ERROR); 10836701Saw148015 } 10846701Saw148015 } 10856701Saw148015 10866701Saw148015 if (flowop->fo_targets) { 10876701Saw148015 bytes_io = flowop->fo_targets->fo_stats.fs_bytes; 10886701Saw148015 } else { 10896701Saw148015 (void) ipc_mutex_lock(&controlstats_lock); 10906701Saw148015 bytes_io = controlstats.fs_bytes; 10916701Saw148015 (void) ipc_mutex_unlock(&controlstats_lock); 10926701Saw148015 } 10935184Sek110237 10945184Sek110237 flowop_beginop(threadflow, flowop); 10956701Saw148015 if (bytes_io > byte_lim) { 10965673Saw148015 flowop_endop(threadflow, flowop, 0); 10976084Saw148015 return (FILEBENCH_DONE); 10985184Sek110237 } 10995673Saw148015 flowop_endop(threadflow, flowop, 0); 11005184Sek110237 11016084Saw148015 return (FILEBENCH_OK); 11025184Sek110237 } 11035184Sek110237 11045184Sek110237 /* 11055184Sek110237 * Stop filebench run when specified number of I/O operations have 11065184Sek110237 * been performed. Compares controlstats.fs_count with *flowop->value, 11076084Saw148015 * and if greater returns 1, stopping the run, if not, returns FILEBENCH_OK 11086084Saw148015 * to continue running. 11095184Sek110237 */ 11105184Sek110237 static int 11115184Sek110237 flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop) 11125184Sek110237 { 11135184Sek110237 uint64_t ops; 11146212Saw148015 uint64_t count = flowop->fo_constvalue; /* use constant value */ 11155184Sek110237 11166701Saw148015 if (flowop->fo_initted == 0) { 11176701Saw148015 filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking", 11186701Saw148015 flowop, threadflow->tf_name, threadflow->tf_instance); 11196701Saw148015 flowop->fo_initted = 1; 11206701Saw148015 11216701Saw148015 if (flowoplib_event_find_target(threadflow, flowop) 11226701Saw148015 == FILEBENCH_ERROR) 11236701Saw148015 return (FILEBENCH_ERROR); 11246701Saw148015 } 11256701Saw148015 11266701Saw148015 if (flowop->fo_targets) { 11276701Saw148015 ops = flowop->fo_targets->fo_stats.fs_count; 11286701Saw148015 } else { 11296701Saw148015 (void) ipc_mutex_lock(&controlstats_lock); 11306701Saw148015 ops = controlstats.fs_count; 11316701Saw148015 (void) ipc_mutex_unlock(&controlstats_lock); 11326701Saw148015 } 11335184Sek110237 11345184Sek110237 flowop_beginop(threadflow, flowop); 11356084Saw148015 if (ops >= count) { 11365673Saw148015 flowop_endop(threadflow, flowop, 0); 11376084Saw148015 return (FILEBENCH_DONE); 11385184Sek110237 } 11395673Saw148015 flowop_endop(threadflow, flowop, 0); 11405184Sek110237 11416084Saw148015 return (FILEBENCH_OK); 11425184Sek110237 } 11435184Sek110237 11445184Sek110237 /* 11455184Sek110237 * Semaphore synchronization using either System V semaphores or 11465184Sek110237 * posix semaphores. If System V semaphores are available, they will be 11475184Sek110237 * used, otherwise posix semaphores will be used. 11485184Sek110237 */ 11495184Sek110237 11505184Sek110237 11515184Sek110237 /* 11525184Sek110237 * Initializes the filebench "block on semaphore" flowop. 11535184Sek110237 * If System V semaphores are implemented, the routine 11545184Sek110237 * initializes the System V semaphore subsystem if it hasn't 11555184Sek110237 * already been initialized, also allocates a pair of semids 11565184Sek110237 * and initializes the highwater System V semaphore. 11575184Sek110237 * If no System V semaphores, then does nothing special. 11586084Saw148015 * Returns FILEBENCH_ERROR if it cannot acquire a set of System V semphores 11596084Saw148015 * or if the initial post to the semaphore set fails. Returns FILEBENCH_OK 11605184Sek110237 * on success. 11615184Sek110237 */ 11625184Sek110237 static int 11635184Sek110237 flowoplib_semblock_init(flowop_t *flowop) 11645184Sek110237 { 11655184Sek110237 11665184Sek110237 #ifdef HAVE_SYSV_SEM 11676391Saw148015 int sys_semid; 11685184Sek110237 struct sembuf sbuf[2]; 11695184Sek110237 int highwater; 11705184Sek110237 11715184Sek110237 ipc_seminit(); 11725184Sek110237 11735184Sek110237 flowop->fo_semid_lw = ipc_semidalloc(); 11745184Sek110237 flowop->fo_semid_hw = ipc_semidalloc(); 11755184Sek110237 11765184Sek110237 filebench_log(LOG_DEBUG_IMPL, "flow %s-%d semblock init semid=%x", 11775184Sek110237 flowop->fo_name, flowop->fo_instance, flowop->fo_semid_lw); 11785184Sek110237 11796391Saw148015 sys_semid = filebench_shm->shm_sys_semid; 11805184Sek110237 11815184Sek110237 if ((highwater = flowop->fo_semid_hw) == 0) 11826212Saw148015 highwater = flowop->fo_constvalue; /* use constant value */ 11835184Sek110237 11845184Sek110237 filebench_log(LOG_DEBUG_IMPL, "setting highwater to : %d", highwater); 11855184Sek110237 11865673Saw148015 sbuf[0].sem_num = (short)highwater; 11876212Saw148015 sbuf[0].sem_op = avd_get_int(flowop->fo_highwater); 11885184Sek110237 sbuf[0].sem_flg = 0; 11896391Saw148015 if ((semop(sys_semid, &sbuf[0], 1) == -1) && errno) { 11905184Sek110237 filebench_log(LOG_ERROR, "semblock init post failed: %s (%d," 11915184Sek110237 "%d)", strerror(errno), sbuf[0].sem_num, sbuf[0].sem_op); 11926084Saw148015 return (FILEBENCH_ERROR); 11935184Sek110237 } 11945184Sek110237 #else 11955184Sek110237 filebench_log(LOG_DEBUG_IMPL, 11965184Sek110237 "flow %s-%d semblock init with posix semaphore", 11975184Sek110237 flowop->fo_name, flowop->fo_instance); 11985184Sek110237 11995184Sek110237 sem_init(&flowop->fo_sem, 1, 0); 12005184Sek110237 #endif /* HAVE_SYSV_SEM */ 12015184Sek110237 12026212Saw148015 if (!(avd_get_bool(flowop->fo_blocking))) 12035184Sek110237 (void) ipc_mutex_unlock(&flowop->fo_lock); 12045184Sek110237 12056084Saw148015 return (FILEBENCH_OK); 12065184Sek110237 } 12075184Sek110237 12085184Sek110237 /* 12095184Sek110237 * Releases the semids for the System V semaphore allocated 12105184Sek110237 * to this flowop. If not using System V semaphores, then 12116084Saw148015 * it is effectively just a no-op. 12125184Sek110237 */ 12135184Sek110237 static void 12145184Sek110237 flowoplib_semblock_destruct(flowop_t *flowop) 12155184Sek110237 { 12165184Sek110237 #ifdef HAVE_SYSV_SEM 12175184Sek110237 ipc_semidfree(flowop->fo_semid_lw); 12185184Sek110237 ipc_semidfree(flowop->fo_semid_hw); 12195184Sek110237 #else 12205184Sek110237 sem_destroy(&flowop->fo_sem); 12215184Sek110237 #endif /* HAVE_SYSV_SEM */ 12225184Sek110237 } 12235184Sek110237 12245184Sek110237 /* 12255184Sek110237 * Attempts to pass a System V or posix semaphore as appropriate, 12266084Saw148015 * and blocks if necessary. Returns FILEBENCH_ERROR if a set of System V 12275184Sek110237 * semphores is not available or cannot be acquired, or if the initial 12286084Saw148015 * post to the semaphore set fails. Returns FILEBENCH_OK on success. 12295184Sek110237 */ 12305184Sek110237 static int 12315184Sek110237 flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop) 12325184Sek110237 { 12335184Sek110237 12345184Sek110237 #ifdef HAVE_SYSV_SEM 12355184Sek110237 struct sembuf sbuf[2]; 12366212Saw148015 int value = avd_get_int(flowop->fo_value); 12376391Saw148015 int sys_semid; 12385184Sek110237 struct timespec timeout; 12395184Sek110237 12406391Saw148015 sys_semid = filebench_shm->shm_sys_semid; 12415184Sek110237 12425184Sek110237 filebench_log(LOG_DEBUG_IMPL, 12435184Sek110237 "flow %s-%d sem blocking on id %x num %x value %d", 12446391Saw148015 flowop->fo_name, flowop->fo_instance, sys_semid, 12455184Sek110237 flowop->fo_semid_hw, value); 12465184Sek110237 12475184Sek110237 /* Post, decrement the increment the hw queue */ 12485184Sek110237 sbuf[0].sem_num = flowop->fo_semid_hw; 12495673Saw148015 sbuf[0].sem_op = (short)value; 12505184Sek110237 sbuf[0].sem_flg = 0; 12515184Sek110237 sbuf[1].sem_num = flowop->fo_semid_lw; 12525184Sek110237 sbuf[1].sem_op = value * -1; 12535184Sek110237 sbuf[1].sem_flg = 0; 12545184Sek110237 timeout.tv_sec = 600; 12555184Sek110237 timeout.tv_nsec = 0; 12565184Sek110237 12576212Saw148015 if (avd_get_bool(flowop->fo_blocking)) 12585184Sek110237 (void) ipc_mutex_unlock(&flowop->fo_lock); 12595184Sek110237 12605184Sek110237 flowop_beginop(threadflow, flowop); 12615184Sek110237 12625184Sek110237 #ifdef HAVE_SEMTIMEDOP 12636391Saw148015 (void) semtimedop(sys_semid, &sbuf[0], 1, &timeout); 12646391Saw148015 (void) semtimedop(sys_semid, &sbuf[1], 1, &timeout); 12655184Sek110237 #else 12666391Saw148015 (void) semop(sys_semid, &sbuf[0], 1); 12676391Saw148015 (void) semop(sys_semid, &sbuf[1], 1); 12685184Sek110237 #endif /* HAVE_SEMTIMEDOP */ 12695184Sek110237 12706212Saw148015 if (avd_get_bool(flowop->fo_blocking)) 12715184Sek110237 (void) ipc_mutex_lock(&flowop->fo_lock); 12725184Sek110237 12735673Saw148015 flowop_endop(threadflow, flowop, 0); 12745184Sek110237 12755184Sek110237 #else 12766212Saw148015 int value = avd_get_int(flowop->fo_value); 12775184Sek110237 int i; 12785184Sek110237 12795184Sek110237 filebench_log(LOG_DEBUG_IMPL, 12805184Sek110237 "flow %s-%d sem blocking on posix semaphore", 12815184Sek110237 flowop->fo_name, flowop->fo_instance); 12825184Sek110237 12835184Sek110237 /* Decrement sem by value */ 12845184Sek110237 for (i = 0; i < value; i++) { 12855184Sek110237 if (sem_wait(&flowop->fo_sem) == -1) { 12865184Sek110237 filebench_log(LOG_ERROR, "semop wait failed"); 12876084Saw148015 return (FILEBENCH_ERROR); 12885184Sek110237 } 12895184Sek110237 } 12905184Sek110237 12915184Sek110237 filebench_log(LOG_DEBUG_IMPL, "flow %s-%d sem unblocking", 12925184Sek110237 flowop->fo_name, flowop->fo_instance); 12935184Sek110237 #endif /* HAVE_SYSV_SEM */ 12945184Sek110237 12956084Saw148015 return (FILEBENCH_OK); 12965184Sek110237 } 12975184Sek110237 12985184Sek110237 /* 12996084Saw148015 * Calls ipc_seminit(). Always returns FILEBENCH_OK. 13005184Sek110237 */ 13015184Sek110237 /* ARGSUSED */ 13025184Sek110237 static int 13035184Sek110237 flowoplib_sempost_init(flowop_t *flowop) 13045184Sek110237 { 13055184Sek110237 #ifdef HAVE_SYSV_SEM 13065184Sek110237 ipc_seminit(); 13075184Sek110237 #endif /* HAVE_SYSV_SEM */ 13086084Saw148015 return (FILEBENCH_OK); 13095184Sek110237 } 13105184Sek110237 13115184Sek110237 /* 13125184Sek110237 * Post to a System V or posix semaphore as appropriate. 13135184Sek110237 * On the first call for a given flowop instance, this routine 13145184Sek110237 * will use the fo_targetname attribute to locate all semblock 13155184Sek110237 * flowops that are expecting posts from this flowop. All 13165184Sek110237 * target flowops on this list will have a post operation done 13175184Sek110237 * to their semaphores on each call. 13185184Sek110237 */ 13195184Sek110237 static int 13205184Sek110237 flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop) 13215184Sek110237 { 13225184Sek110237 flowop_t *target; 13235184Sek110237 13245184Sek110237 filebench_log(LOG_DEBUG_IMPL, 13255184Sek110237 "sempost flow %s-%d", 13265184Sek110237 flowop->fo_name, 13275184Sek110237 flowop->fo_instance); 13285184Sek110237 13295184Sek110237 /* if this is the first post, create the post list */ 13305184Sek110237 if (flowop->fo_targets == NULL) { 13315184Sek110237 flowop_t *result = flowop_find(flowop->fo_targetname); 13325184Sek110237 13335184Sek110237 flowop->fo_targets = result; 13345184Sek110237 13355184Sek110237 if (result == NULL) { 13365184Sek110237 filebench_log(LOG_ERROR, 13375184Sek110237 "sempost: could not find op %s for thread %s", 13385184Sek110237 flowop->fo_targetname, 13395184Sek110237 threadflow->tf_name); 13405184Sek110237 filebench_shutdown(1); 13415184Sek110237 } 13425184Sek110237 13435184Sek110237 while (result) { 13445184Sek110237 result->fo_targetnext = 13455184Sek110237 result->fo_resultnext; 13465184Sek110237 result = result->fo_resultnext; 13475184Sek110237 } 13485184Sek110237 } 13495184Sek110237 13505184Sek110237 target = flowop->fo_targets; 13515184Sek110237 13525184Sek110237 flowop_beginop(threadflow, flowop); 13535184Sek110237 /* post to the targets */ 13545184Sek110237 while (target) { 13555184Sek110237 #ifdef HAVE_SYSV_SEM 13565184Sek110237 struct sembuf sbuf[2]; 13576391Saw148015 int sys_semid; 13585184Sek110237 int blocking; 13595184Sek110237 #else 13605184Sek110237 int i; 13615184Sek110237 #endif /* HAVE_SYSV_SEM */ 13625184Sek110237 struct timespec timeout; 13636550Saw148015 int value = (int)avd_get_int(flowop->fo_value); 13645184Sek110237 13655184Sek110237 if (target->fo_instance == FLOW_MASTER) { 13665184Sek110237 target = target->fo_targetnext; 13675184Sek110237 continue; 13685184Sek110237 } 13695184Sek110237 13705184Sek110237 #ifdef HAVE_SYSV_SEM 13715184Sek110237 13725184Sek110237 filebench_log(LOG_DEBUG_IMPL, 13735184Sek110237 "sempost flow %s-%d num %x", 13745184Sek110237 target->fo_name, 13755184Sek110237 target->fo_instance, 13765184Sek110237 target->fo_semid_lw); 13775184Sek110237 13786391Saw148015 sys_semid = filebench_shm->shm_sys_semid; 13795184Sek110237 sbuf[0].sem_num = target->fo_semid_lw; 13805673Saw148015 sbuf[0].sem_op = (short)value; 13815184Sek110237 sbuf[0].sem_flg = 0; 13825184Sek110237 sbuf[1].sem_num = target->fo_semid_hw; 13835184Sek110237 sbuf[1].sem_op = value * -1; 13845184Sek110237 sbuf[1].sem_flg = 0; 13855184Sek110237 timeout.tv_sec = 600; 13865184Sek110237 timeout.tv_nsec = 0; 13875184Sek110237 13886212Saw148015 if (avd_get_bool(flowop->fo_blocking)) 13895184Sek110237 blocking = 1; 13905184Sek110237 else 13915184Sek110237 blocking = 0; 13925184Sek110237 13935184Sek110237 #ifdef HAVE_SEMTIMEDOP 13946391Saw148015 if ((semtimedop(sys_semid, &sbuf[0], blocking + 1, 13955184Sek110237 &timeout) == -1) && (errno && (errno != EAGAIN))) { 13965184Sek110237 #else 13976391Saw148015 if ((semop(sys_semid, &sbuf[0], blocking + 1) == -1) && 13985184Sek110237 (errno && (errno != EAGAIN))) { 13995184Sek110237 #endif /* HAVE_SEMTIMEDOP */ 14005184Sek110237 filebench_log(LOG_ERROR, "semop post failed: %s", 14015184Sek110237 strerror(errno)); 14026084Saw148015 return (FILEBENCH_ERROR); 14035184Sek110237 } 14045184Sek110237 14055184Sek110237 filebench_log(LOG_DEBUG_IMPL, 14065184Sek110237 "flow %s-%d finished posting", 14075184Sek110237 target->fo_name, target->fo_instance); 14085184Sek110237 #else 14095184Sek110237 filebench_log(LOG_DEBUG_IMPL, 14105184Sek110237 "sempost flow %s-%d to posix semaphore", 14115184Sek110237 target->fo_name, 14125184Sek110237 target->fo_instance); 14135184Sek110237 14145184Sek110237 /* Increment sem by value */ 14155184Sek110237 for (i = 0; i < value; i++) { 14165184Sek110237 if (sem_post(&target->fo_sem) == -1) { 14175184Sek110237 filebench_log(LOG_ERROR, "semop post failed"); 14186084Saw148015 return (FILEBENCH_ERROR); 14195184Sek110237 } 14205184Sek110237 } 14215184Sek110237 14225184Sek110237 filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking", 14235184Sek110237 target->fo_name, target->fo_instance); 14245184Sek110237 #endif /* HAVE_SYSV_SEM */ 14255184Sek110237 14265184Sek110237 target = target->fo_targetnext; 14275184Sek110237 } 14285673Saw148015 flowop_endop(threadflow, flowop, 0); 14295184Sek110237 14306084Saw148015 return (FILEBENCH_OK); 14315184Sek110237 } 14325184Sek110237 14335184Sek110237 14345184Sek110237 /* 14355184Sek110237 * Section for exercising create / open / close / delete operations 14365184Sek110237 * on files within a fileset. For proper operation, the flowop attribute 14375184Sek110237 * "fd", which sets the fo_fdnumber field in the flowop, must be used 14385184Sek110237 * so that the same file is opened and later closed. "fd" is an index 14395184Sek110237 * into a pair of arrays maintained by threadflows, one of which 14405184Sek110237 * contains the operating system assigned file descriptors and the other 14415184Sek110237 * a pointer to the filesetentry whose file the file descriptor 14425184Sek110237 * references. An openfile flowop defined without fd being set will use 14435184Sek110237 * the default (0) fd or, if specified, rotate through fd indices, but 14445184Sek110237 * createfile and closefile must use the default or a specified fd. 14455184Sek110237 * Meanwhile deletefile picks and arbitrary file to delete, regardless 14465184Sek110237 * of fd attribute. 14475184Sek110237 */ 14485184Sek110237 14495184Sek110237 /* 14505184Sek110237 * Emulates (and actually does) file open. Obtains a file descriptor 14516084Saw148015 * index, then calls flowoplib_openfile_common() to open. Returns 14526084Saw148015 * FILEBENCH_ERROR if no file descriptor is found, and returns the 14536084Saw148015 * status from flowoplib_openfile_common otherwise (FILEBENCH_ERROR, 14546084Saw148015 * FILEBENCH_NORSC, FILEBENCH_OK). 14555184Sek110237 */ 14565184Sek110237 static int 14575184Sek110237 flowoplib_openfile(threadflow_t *threadflow, flowop_t *flowop) 14585184Sek110237 { 14595184Sek110237 int fd = flowoplib_fdnum(threadflow, flowop); 14605184Sek110237 14615184Sek110237 if (fd == -1) 14626084Saw148015 return (FILEBENCH_ERROR); 14635184Sek110237 14645184Sek110237 return (flowoplib_openfile_common(threadflow, flowop, fd)); 14655184Sek110237 } 14665184Sek110237 14675184Sek110237 /* 14685184Sek110237 * Common file opening code for filesets. Uses the supplied 14695184Sek110237 * file descriptor index to determine the tf_fd entry to use. 14705184Sek110237 * If the entry is empty (0) and the fileset exists, fileset 14715184Sek110237 * pick is called to select a fileset entry to use. The file 14725184Sek110237 * specified in the filesetentry is opened, and the returned 14735184Sek110237 * operating system file descriptor and a pointer to the 14745184Sek110237 * filesetentry are stored in tf_fd[fd] and tf_fse[fd], 14756084Saw148015 * respectively. Returns FILEBENCH_ERROR on error, 14766084Saw148015 * FILEBENCH_NORSC if no suitable filesetentry can be found, 14776084Saw148015 * and FILEBENCH_OK on success. 14785184Sek110237 */ 14795184Sek110237 static int 14805184Sek110237 flowoplib_openfile_common(threadflow_t *threadflow, flowop_t *flowop, int fd) 14815184Sek110237 { 14825184Sek110237 filesetentry_t *file; 14836212Saw148015 char *fileset_name; 14845184Sek110237 int tid = 0; 14858404SAndrew.W.Wilson@sun.com int err; 14865184Sek110237 14876391Saw148015 if (flowop->fo_fileset == NULL) { 14886391Saw148015 filebench_log(LOG_ERROR, "flowop NULL file"); 14896391Saw148015 return (FILEBENCH_ERROR); 14906391Saw148015 } 14916391Saw148015 14926212Saw148015 if ((fileset_name = 14936212Saw148015 avd_get_str(flowop->fo_fileset->fs_name)) == NULL) { 14946212Saw148015 filebench_log(LOG_ERROR, 14956212Saw148015 "flowop %s: fileset has no name", flowop->fo_name); 14966212Saw148015 return (FILEBENCH_ERROR); 14976212Saw148015 } 14986212Saw148015 14995184Sek110237 /* 15005184Sek110237 * If the flowop doesn't default to persistent fd 15015184Sek110237 * then get unique thread ID for use by fileset_pick 15025184Sek110237 */ 15036212Saw148015 if (avd_get_bool(flowop->fo_rotatefd)) 15045184Sek110237 tid = threadflow->tf_utid; 15055184Sek110237 1506*8615SAndrew.W.Wilson@sun.com if (threadflow->tf_fd[fd].fd_ptr != NULL) { 15075184Sek110237 filebench_log(LOG_ERROR, 15085184Sek110237 "flowop %s attempted to open without closing on fd %d", 15095184Sek110237 flowop->fo_name, fd); 15106084Saw148015 return (FILEBENCH_ERROR); 15115184Sek110237 } 15125184Sek110237 15135673Saw148015 #ifdef HAVE_RAW_SUPPORT 15145673Saw148015 if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) { 15155673Saw148015 int open_attrs = 0; 15165673Saw148015 char name[MAXPATHLEN]; 15175673Saw148015 15187946SAndrew.W.Wilson@sun.com (void) fb_strlcpy(name, 15197946SAndrew.W.Wilson@sun.com avd_get_str(flowop->fo_fileset->fs_path), MAXPATHLEN); 15207946SAndrew.W.Wilson@sun.com (void) fb_strlcat(name, "/", MAXPATHLEN); 15217946SAndrew.W.Wilson@sun.com (void) fb_strlcat(name, fileset_name, MAXPATHLEN); 15225673Saw148015 15236212Saw148015 if (avd_get_bool(flowop->fo_dsync)) { 15245673Saw148015 #ifdef sun 15255673Saw148015 open_attrs |= O_DSYNC; 15265673Saw148015 #else 15275673Saw148015 open_attrs |= O_FSYNC; 15285673Saw148015 #endif 15295673Saw148015 } 15305673Saw148015 15315673Saw148015 filebench_log(LOG_DEBUG_SCRIPT, 15325673Saw148015 "open raw device %s flags %d = %d", name, open_attrs, fd); 15335673Saw148015 1534*8615SAndrew.W.Wilson@sun.com if (FB_OPEN(&(threadflow->tf_fd[fd]), name, 1535*8615SAndrew.W.Wilson@sun.com O_RDWR | open_attrs, 0666) == FILEBENCH_ERROR) { 15365673Saw148015 filebench_log(LOG_ERROR, 15375673Saw148015 "Failed to open raw device %s: %s", 15385673Saw148015 name, strerror(errno)); 15396084Saw148015 return (FILEBENCH_ERROR); 15405673Saw148015 } 15415673Saw148015 15425673Saw148015 /* if running on Solaris, use un-buffered io */ 15435673Saw148015 #ifdef sun 1544*8615SAndrew.W.Wilson@sun.com (void) directio(threadflow->tf_fd[fd].fd_num, DIRECTIO_ON); 15455673Saw148015 #endif 15465673Saw148015 15475673Saw148015 threadflow->tf_fse[fd] = NULL; 15485673Saw148015 15496084Saw148015 return (FILEBENCH_OK); 15505673Saw148015 } 15515673Saw148015 #endif /* HAVE_RAW_SUPPORT */ 15525673Saw148015 15538404SAndrew.W.Wilson@sun.com if ((err = flowoplib_pickfile(&file, flowop, 15548404SAndrew.W.Wilson@sun.com FILESET_PICKEXISTS, tid)) != FILEBENCH_OK) { 15556084Saw148015 filebench_log(LOG_DEBUG_SCRIPT, 15565184Sek110237 "flowop %s failed to pick file from %s on fd %d", 15576212Saw148015 flowop->fo_name, fileset_name, fd); 15588404SAndrew.W.Wilson@sun.com return (err); 15595184Sek110237 } 15605184Sek110237 15615184Sek110237 threadflow->tf_fse[fd] = file; 15625184Sek110237 15635184Sek110237 flowop_beginop(threadflow, flowop); 1564*8615SAndrew.W.Wilson@sun.com err = fileset_openfile(&threadflow->tf_fd[fd], flowop->fo_fileset, 15655184Sek110237 file, O_RDWR, 0666, flowoplib_fileattrs(flowop)); 15665673Saw148015 flowop_endop(threadflow, flowop, 0); 15675184Sek110237 1568*8615SAndrew.W.Wilson@sun.com if (err == FILEBENCH_ERROR) { 15696212Saw148015 filebench_log(LOG_ERROR, "flowop %s failed to open file %s", 15706212Saw148015 flowop->fo_name, file->fse_path); 15716084Saw148015 return (FILEBENCH_ERROR); 15725184Sek110237 } 15735184Sek110237 15745184Sek110237 filebench_log(LOG_DEBUG_SCRIPT, 15755184Sek110237 "flowop %s: opened %s fd[%d] = %d", 15765184Sek110237 flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]); 15775184Sek110237 15786084Saw148015 return (FILEBENCH_OK); 15795184Sek110237 } 15805184Sek110237 15815184Sek110237 /* 15825184Sek110237 * Emulate create of a file. Uses the flowop's fdnumber to select 15835184Sek110237 * tf_fd and tf_fse array locations to put the created file's file 15848404SAndrew.W.Wilson@sun.com * descriptor and filesetentry respectively. Uses flowoplib_pickfile() 15855184Sek110237 * to select a specific filesetentry whose file does not currently 15865184Sek110237 * exist for the file create operation. Then calls 15875184Sek110237 * fileset_openfile() with the O_CREATE flag set to create the 15886084Saw148015 * file. Returns FILEBENCH_ERROR if the array index specified by fdnumber is 15895184Sek110237 * already in use, the flowop has no associated fileset, or 15905184Sek110237 * the create call fails. Returns 1 if a filesetentry with a 15916084Saw148015 * nonexistent file cannot be found. Returns FILEBENCH_OK on success. 15925184Sek110237 */ 15935184Sek110237 static int 15945184Sek110237 flowoplib_createfile(threadflow_t *threadflow, flowop_t *flowop) 15955184Sek110237 { 15965184Sek110237 filesetentry_t *file; 15975184Sek110237 int fd = flowop->fo_fdnumber; 15988404SAndrew.W.Wilson@sun.com int err; 15995184Sek110237 1600*8615SAndrew.W.Wilson@sun.com if (threadflow->tf_fd[fd].fd_ptr != NULL) { 16015184Sek110237 filebench_log(LOG_ERROR, 16025184Sek110237 "flowop %s attempted to create without closing on fd %d", 16035184Sek110237 flowop->fo_name, fd); 16046084Saw148015 return (FILEBENCH_ERROR); 16055184Sek110237 } 16065184Sek110237 16075184Sek110237 if (flowop->fo_fileset == NULL) { 16085184Sek110237 filebench_log(LOG_ERROR, "flowop NULL file"); 16096084Saw148015 return (FILEBENCH_ERROR); 16105184Sek110237 } 16115184Sek110237 16125673Saw148015 #ifdef HAVE_RAW_SUPPORT 16135673Saw148015 /* can't be used with raw devices */ 16145673Saw148015 if (flowop->fo_fileset->fs_attrs & FILESET_IS_RAW_DEV) { 16155673Saw148015 filebench_log(LOG_ERROR, 16165673Saw148015 "flowop %s attempted to a createfile on RAW device", 16175673Saw148015 flowop->fo_name); 16186084Saw148015 return (FILEBENCH_ERROR); 16195673Saw148015 } 16205673Saw148015 #endif /* HAVE_RAW_SUPPORT */ 16215673Saw148015 16228404SAndrew.W.Wilson@sun.com if ((err = flowoplib_pickfile(&file, flowop, 16238404SAndrew.W.Wilson@sun.com FILESET_PICKNOEXIST, 0)) != FILEBENCH_OK) { 16246084Saw148015 filebench_log(LOG_DEBUG_SCRIPT, 16256084Saw148015 "flowop %s failed to pick file from fileset %s", 16266212Saw148015 flowop->fo_name, 16276212Saw148015 avd_get_str(flowop->fo_fileset->fs_name)); 16288404SAndrew.W.Wilson@sun.com return (err); 16295184Sek110237 } 16305184Sek110237 16315184Sek110237 threadflow->tf_fse[fd] = file; 16325184Sek110237 16335184Sek110237 flowop_beginop(threadflow, flowop); 1634*8615SAndrew.W.Wilson@sun.com err = fileset_openfile(&threadflow->tf_fd[fd], flowop->fo_fileset, 16355184Sek110237 file, O_RDWR | O_CREAT, 0666, flowoplib_fileattrs(flowop)); 16365673Saw148015 flowop_endop(threadflow, flowop, 0); 16375184Sek110237 1638*8615SAndrew.W.Wilson@sun.com if (err == FILEBENCH_ERROR) { 16395184Sek110237 filebench_log(LOG_ERROR, "failed to create file %s", 16405184Sek110237 flowop->fo_name); 16416084Saw148015 return (FILEBENCH_ERROR); 16425184Sek110237 } 16435184Sek110237 16445184Sek110237 filebench_log(LOG_DEBUG_SCRIPT, 16455184Sek110237 "flowop %s: created %s fd[%d] = %d", 16465184Sek110237 flowop->fo_name, file->fse_path, fd, threadflow->tf_fd[fd]); 16475184Sek110237 16486084Saw148015 return (FILEBENCH_OK); 16495184Sek110237 } 16505184Sek110237 16515184Sek110237 /* 16526391Saw148015 * Emulates delete of a file. If a valid fd is provided, it uses the 16536391Saw148015 * filesetentry stored at that fd location to select the file to be 16546391Saw148015 * deleted, otherwise it picks an arbitrary filesetentry 16556391Saw148015 * whose file exists. It then uses unlink() to delete it and Clears 16566084Saw148015 * the FSE_EXISTS flag for the filesetentry. Returns FILEBENCH_ERROR if the 16576084Saw148015 * flowop has no associated fileset. Returns FILEBENCH_NORSC if an appropriate 16586084Saw148015 * filesetentry cannot be found, and FILEBENCH_OK on success. 16595184Sek110237 */ 16605184Sek110237 static int 16615184Sek110237 flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop) 16625184Sek110237 { 16635184Sek110237 filesetentry_t *file; 16645184Sek110237 fileset_t *fileset; 16655184Sek110237 char path[MAXPATHLEN]; 16665184Sek110237 char *pathtmp; 16676391Saw148015 int fd = flowop->fo_fdnumber; 16685184Sek110237 16696391Saw148015 /* if fd specified, use it to access file */ 16706391Saw148015 if ((fd > 0) && ((file = threadflow->tf_fse[fd]) != NULL)) { 16716391Saw148015 16726391Saw148015 /* indicate that the file will be deleted */ 16736391Saw148015 threadflow->tf_fse[fd] = NULL; 16746391Saw148015 16756391Saw148015 /* if here, we still have a valid file pointer */ 16766391Saw148015 fileset = file->fse_fileset; 16776391Saw148015 } else { 16788404SAndrew.W.Wilson@sun.com 16796391Saw148015 /* Otherwise, pick arbitrary file */ 16806391Saw148015 file = NULL; 16816391Saw148015 fileset = flowop->fo_fileset; 16826391Saw148015 } 16836391Saw148015 16846391Saw148015 16856391Saw148015 if (fileset == NULL) { 16865184Sek110237 filebench_log(LOG_ERROR, "flowop NULL file"); 16876084Saw148015 return (FILEBENCH_ERROR); 16885184Sek110237 } 16895184Sek110237 16905673Saw148015 #ifdef HAVE_RAW_SUPPORT 16915673Saw148015 /* can't be used with raw devices */ 16926391Saw148015 if (fileset->fs_attrs & FILESET_IS_RAW_DEV) { 16935673Saw148015 filebench_log(LOG_ERROR, 16945673Saw148015 "flowop %s attempted a deletefile on RAW device", 16955673Saw148015 flowop->fo_name); 16966084Saw148015 return (FILEBENCH_ERROR); 16975673Saw148015 } 16985673Saw148015 #endif /* HAVE_RAW_SUPPORT */ 16995673Saw148015 17006391Saw148015 if (file == NULL) { 17018404SAndrew.W.Wilson@sun.com int err; 17028404SAndrew.W.Wilson@sun.com 17037556SAndrew.W.Wilson@sun.com /* pick arbitrary, existing (allocated) file */ 17048404SAndrew.W.Wilson@sun.com if ((err = flowoplib_pickfile(&file, flowop, 17058404SAndrew.W.Wilson@sun.com FILESET_PICKEXISTS, 0)) != FILEBENCH_OK) { 17066391Saw148015 filebench_log(LOG_DEBUG_SCRIPT, 17076391Saw148015 "flowop %s failed to pick file", flowop->fo_name); 17088404SAndrew.W.Wilson@sun.com return (err); 17096391Saw148015 } 17106391Saw148015 } else { 17117556SAndrew.W.Wilson@sun.com /* delete specific file. wait for it to be non-busy */ 17127556SAndrew.W.Wilson@sun.com (void) ipc_mutex_lock(&fileset->fs_pick_lock); 17137556SAndrew.W.Wilson@sun.com while (file->fse_flags & FSE_BUSY) { 17147556SAndrew.W.Wilson@sun.com file->fse_flags |= FSE_THRD_WAITNG; 17157556SAndrew.W.Wilson@sun.com (void) pthread_cond_wait(&fileset->fs_thrd_wait_cv, 17167556SAndrew.W.Wilson@sun.com &fileset->fs_pick_lock); 17177556SAndrew.W.Wilson@sun.com } 17187556SAndrew.W.Wilson@sun.com 17197556SAndrew.W.Wilson@sun.com /* File now available, grab it for deletion */ 17207556SAndrew.W.Wilson@sun.com file->fse_flags |= FSE_BUSY; 17217556SAndrew.W.Wilson@sun.com fileset->fs_idle_files--; 17227556SAndrew.W.Wilson@sun.com (void) ipc_mutex_unlock(&fileset->fs_pick_lock); 17235184Sek110237 } 17245184Sek110237 17258404SAndrew.W.Wilson@sun.com /* don't delete if anyone (other than me) has file open */ 1726*8615SAndrew.W.Wilson@sun.com if ((fd > 0) && (threadflow->tf_fd[fd].fd_num > 0)) { 17278404SAndrew.W.Wilson@sun.com if (file->fse_open_cnt > 1) { 17288404SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 17298404SAndrew.W.Wilson@sun.com "flowop %s can't delete file opened by other" 17308404SAndrew.W.Wilson@sun.com " threads at fd = %d", flowop->fo_name, fd); 17318404SAndrew.W.Wilson@sun.com fileset_unbusy(file, FALSE, FALSE, 0); 17328404SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 17338404SAndrew.W.Wilson@sun.com } else { 17348404SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 17358404SAndrew.W.Wilson@sun.com "flowop %s deleting still open file at fd = %d", 17368404SAndrew.W.Wilson@sun.com flowop->fo_name, fd); 17378404SAndrew.W.Wilson@sun.com } 17388404SAndrew.W.Wilson@sun.com } else if (file->fse_open_cnt > 0) { 17398404SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 17408404SAndrew.W.Wilson@sun.com "flowop %s can't delete file opened by other" 17418404SAndrew.W.Wilson@sun.com " threads at fd = %d, open count = %d", 17428404SAndrew.W.Wilson@sun.com flowop->fo_name, fd, file->fse_open_cnt); 17438404SAndrew.W.Wilson@sun.com fileset_unbusy(file, FALSE, FALSE, 0); 17448404SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 17458404SAndrew.W.Wilson@sun.com } 17468404SAndrew.W.Wilson@sun.com 17477946SAndrew.W.Wilson@sun.com (void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN); 17487946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, "/", MAXPATHLEN); 17497946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN); 17505184Sek110237 pathtmp = fileset_resolvepath(file); 17517946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, pathtmp, MAXPATHLEN); 17525184Sek110237 free(pathtmp); 17535184Sek110237 17547556SAndrew.W.Wilson@sun.com /* delete the selected file */ 17555184Sek110237 flowop_beginop(threadflow, flowop); 1756*8615SAndrew.W.Wilson@sun.com (void) FB_UNLINK(path); 17575673Saw148015 flowop_endop(threadflow, flowop, 0); 17587556SAndrew.W.Wilson@sun.com 17597556SAndrew.W.Wilson@sun.com /* indicate that it is no longer busy and no longer exists */ 17608404SAndrew.W.Wilson@sun.com fileset_unbusy(file, TRUE, FALSE, -file->fse_open_cnt); 17615184Sek110237 17625184Sek110237 filebench_log(LOG_DEBUG_SCRIPT, "deleted file %s", file->fse_path); 17635184Sek110237 17646084Saw148015 return (FILEBENCH_OK); 17655184Sek110237 } 17665184Sek110237 17675184Sek110237 /* 17685184Sek110237 * Emulates fsync of a file. Obtains the file descriptor index 17695184Sek110237 * from the flowop, obtains the actual file descriptor from 17705184Sek110237 * the threadflow's table, checks to be sure it is still an 17716084Saw148015 * open file, then does an fsync operation on it. Returns FILEBENCH_ERROR 17726084Saw148015 * if the file no longer is open, FILEBENCH_OK otherwise. 17735184Sek110237 */ 17745184Sek110237 static int 17755184Sek110237 flowoplib_fsync(threadflow_t *threadflow, flowop_t *flowop) 17765184Sek110237 { 17775184Sek110237 filesetentry_t *file; 17785184Sek110237 int fd = flowop->fo_fdnumber; 17795184Sek110237 1780*8615SAndrew.W.Wilson@sun.com if (threadflow->tf_fd[fd].fd_ptr == NULL) { 17815184Sek110237 filebench_log(LOG_ERROR, 17825184Sek110237 "flowop %s attempted to fsync a closed fd %d", 17835184Sek110237 flowop->fo_name, fd); 17846084Saw148015 return (FILEBENCH_ERROR); 17855184Sek110237 } 17865184Sek110237 17875673Saw148015 file = threadflow->tf_fse[fd]; 17885673Saw148015 17895673Saw148015 if ((file == NULL) || 17905673Saw148015 (file->fse_fileset->fs_attrs & FILESET_IS_RAW_DEV)) { 17915673Saw148015 filebench_log(LOG_ERROR, 17925673Saw148015 "flowop %s attempted to a fsync a RAW device", 17935673Saw148015 flowop->fo_name); 17946084Saw148015 return (FILEBENCH_ERROR); 17955673Saw148015 } 17965673Saw148015 17975184Sek110237 /* Measure time to fsync */ 17985184Sek110237 flowop_beginop(threadflow, flowop); 1799*8615SAndrew.W.Wilson@sun.com (void) FB_FSYNC(&threadflow->tf_fd[fd]); 18005673Saw148015 flowop_endop(threadflow, flowop, 0); 18015184Sek110237 18025184Sek110237 filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s", file->fse_path); 18035184Sek110237 18046084Saw148015 return (FILEBENCH_OK); 18055184Sek110237 } 18065184Sek110237 18075184Sek110237 /* 18085184Sek110237 * Emulate fsync of an entire fileset. Search through the 18095184Sek110237 * threadflow's file descriptor array, doing fsync() on each 18105184Sek110237 * open file that belongs to the flowop's fileset. Always 18116084Saw148015 * returns FILEBENCH_OK. 18125184Sek110237 */ 18135184Sek110237 static int 18145184Sek110237 flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop) 18155184Sek110237 { 18165184Sek110237 int fd; 18175184Sek110237 18185184Sek110237 for (fd = 0; fd < THREADFLOW_MAXFD; fd++) { 18195184Sek110237 filesetentry_t *file; 18205184Sek110237 18215184Sek110237 /* Match the file set to fsync */ 18225184Sek110237 if ((threadflow->tf_fse[fd] == NULL) || 18235184Sek110237 (flowop->fo_fileset != threadflow->tf_fse[fd]->fse_fileset)) 18245184Sek110237 continue; 18255184Sek110237 18265184Sek110237 /* Measure time to fsync */ 18275184Sek110237 flowop_beginop(threadflow, flowop); 1828*8615SAndrew.W.Wilson@sun.com (void) FB_FSYNC(&threadflow->tf_fd[fd]); 18295673Saw148015 flowop_endop(threadflow, flowop, 0); 18305184Sek110237 18315184Sek110237 file = threadflow->tf_fse[fd]; 18325184Sek110237 18335184Sek110237 filebench_log(LOG_DEBUG_SCRIPT, "fsync file %s", 18345184Sek110237 file->fse_path); 18355184Sek110237 } 18365184Sek110237 18376084Saw148015 return (FILEBENCH_OK); 18385184Sek110237 } 18395184Sek110237 18405184Sek110237 /* 18415184Sek110237 * Emulate close of a file. Obtains the file descriptor index 18425184Sek110237 * from the flowop, obtains the actual file descriptor from the 18435184Sek110237 * threadflow's table, checks to be sure it is still an open 18445184Sek110237 * file, then does a close operation on it. Then sets the 18455184Sek110237 * threadflow file descriptor table entry to 0, and the file set 18466084Saw148015 * entry pointer to NULL. Returns FILEBENCH_ERROR if the file was not open, 18476084Saw148015 * FILEBENCH_OK otherwise. 18485184Sek110237 */ 18495184Sek110237 static int 18505184Sek110237 flowoplib_closefile(threadflow_t *threadflow, flowop_t *flowop) 18515184Sek110237 { 18525184Sek110237 filesetentry_t *file; 18538404SAndrew.W.Wilson@sun.com fileset_t *fileset; 18545184Sek110237 int fd = flowop->fo_fdnumber; 18555184Sek110237 1856*8615SAndrew.W.Wilson@sun.com if (threadflow->tf_fd[fd].fd_ptr == NULL) { 18575184Sek110237 filebench_log(LOG_ERROR, 18585184Sek110237 "flowop %s attempted to close an already closed fd %d", 18595184Sek110237 flowop->fo_name, fd); 18606084Saw148015 return (FILEBENCH_ERROR); 18615184Sek110237 } 18625184Sek110237 18638404SAndrew.W.Wilson@sun.com file = threadflow->tf_fse[fd]; 18648404SAndrew.W.Wilson@sun.com fileset = file->fse_fileset; 18658404SAndrew.W.Wilson@sun.com 18668404SAndrew.W.Wilson@sun.com /* Wait for it to be non-busy */ 18678404SAndrew.W.Wilson@sun.com (void) ipc_mutex_lock(&fileset->fs_pick_lock); 18688404SAndrew.W.Wilson@sun.com while (file->fse_flags & FSE_BUSY) { 18698404SAndrew.W.Wilson@sun.com file->fse_flags |= FSE_THRD_WAITNG; 18708404SAndrew.W.Wilson@sun.com (void) pthread_cond_wait(&fileset->fs_thrd_wait_cv, 18718404SAndrew.W.Wilson@sun.com &fileset->fs_pick_lock); 18728404SAndrew.W.Wilson@sun.com } 18738404SAndrew.W.Wilson@sun.com 18748404SAndrew.W.Wilson@sun.com /* File now available, grab it for closing */ 18758404SAndrew.W.Wilson@sun.com file->fse_flags |= FSE_BUSY; 18768404SAndrew.W.Wilson@sun.com 18778404SAndrew.W.Wilson@sun.com /* if last open, set declare idle */ 18788404SAndrew.W.Wilson@sun.com if (file->fse_open_cnt == 1) 18798404SAndrew.W.Wilson@sun.com fileset->fs_idle_files--; 18808404SAndrew.W.Wilson@sun.com 18818404SAndrew.W.Wilson@sun.com (void) ipc_mutex_unlock(&fileset->fs_pick_lock); 18828404SAndrew.W.Wilson@sun.com 18835184Sek110237 /* Measure time to close */ 18845184Sek110237 flowop_beginop(threadflow, flowop); 1885*8615SAndrew.W.Wilson@sun.com (void) FB_CLOSE(&threadflow->tf_fd[fd]); 18865673Saw148015 flowop_endop(threadflow, flowop, 0); 18875184Sek110237 18888404SAndrew.W.Wilson@sun.com fileset_unbusy(file, FALSE, FALSE, -1); 18895184Sek110237 1890*8615SAndrew.W.Wilson@sun.com threadflow->tf_fd[fd].fd_ptr = NULL; 18915184Sek110237 18925184Sek110237 filebench_log(LOG_DEBUG_SCRIPT, "closed file %s", file->fse_path); 18935184Sek110237 18946084Saw148015 return (FILEBENCH_OK); 18955184Sek110237 } 18965184Sek110237 18975184Sek110237 /* 18987946SAndrew.W.Wilson@sun.com * Obtain the full pathname of the directory described by the filesetentry 18997946SAndrew.W.Wilson@sun.com * indicated by "dir", and copy it into the character array pointed to by 19007946SAndrew.W.Wilson@sun.com * path. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise. 19017946SAndrew.W.Wilson@sun.com */ 19027946SAndrew.W.Wilson@sun.com static int 19037946SAndrew.W.Wilson@sun.com flowoplib_getdirpath(filesetentry_t *dir, char *path) 19047946SAndrew.W.Wilson@sun.com { 19057946SAndrew.W.Wilson@sun.com char *fileset_path; 19067946SAndrew.W.Wilson@sun.com char *fileset_name; 19077946SAndrew.W.Wilson@sun.com char *part_path; 19087946SAndrew.W.Wilson@sun.com 19097946SAndrew.W.Wilson@sun.com if ((fileset_path = avd_get_str(dir->fse_fileset->fs_path)) == NULL) { 19107946SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, "Fileset path not set"); 19117946SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 19127946SAndrew.W.Wilson@sun.com } 19137946SAndrew.W.Wilson@sun.com 19147946SAndrew.W.Wilson@sun.com if ((fileset_name = avd_get_str(dir->fse_fileset->fs_name)) == NULL) { 19157946SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, "Fileset name not set"); 19167946SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 19177946SAndrew.W.Wilson@sun.com } 19187946SAndrew.W.Wilson@sun.com 19197946SAndrew.W.Wilson@sun.com (void) fb_strlcpy(path, fileset_path, MAXPATHLEN); 19207946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, "/", MAXPATHLEN); 19217946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, fileset_name, MAXPATHLEN); 19227946SAndrew.W.Wilson@sun.com 19237946SAndrew.W.Wilson@sun.com if ((part_path = fileset_resolvepath(dir)) == NULL) 19247946SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 19257946SAndrew.W.Wilson@sun.com 19267946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, part_path, MAXPATHLEN); 19277946SAndrew.W.Wilson@sun.com free(part_path); 19287946SAndrew.W.Wilson@sun.com 19297946SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 19307946SAndrew.W.Wilson@sun.com } 19317946SAndrew.W.Wilson@sun.com 19327946SAndrew.W.Wilson@sun.com /* 19337946SAndrew.W.Wilson@sun.com * Use mkdir to create a directory. Obtains the fileset name from the 19347946SAndrew.W.Wilson@sun.com * flowop, selects a non-existent leaf directory and obtains its full 19357946SAndrew.W.Wilson@sun.com * path, then uses mkdir to create it on the storage subsystem (make it 19367946SAndrew.W.Wilson@sun.com * existent). Returns FILEBENCH_NORSC is there are no more non-existent 19377946SAndrew.W.Wilson@sun.com * directories in the fileset, FILEBENCH_ERROR on other errors, and 19387946SAndrew.W.Wilson@sun.com * FILEBENCH_OK on success. 19397946SAndrew.W.Wilson@sun.com */ 19407946SAndrew.W.Wilson@sun.com static int 19417946SAndrew.W.Wilson@sun.com flowoplib_makedir(threadflow_t *threadflow, flowop_t *flowop) 19427946SAndrew.W.Wilson@sun.com { 19437946SAndrew.W.Wilson@sun.com filesetentry_t *dir; 19447946SAndrew.W.Wilson@sun.com int ret; 19457946SAndrew.W.Wilson@sun.com char full_path[MAXPATHLEN]; 19467946SAndrew.W.Wilson@sun.com 19477946SAndrew.W.Wilson@sun.com if ((ret = flowoplib_pickleafdir(&dir, flowop, 19487946SAndrew.W.Wilson@sun.com FILESET_PICKNOEXIST)) != FILEBENCH_OK) 19497946SAndrew.W.Wilson@sun.com return (ret); 19507946SAndrew.W.Wilson@sun.com 19517946SAndrew.W.Wilson@sun.com if ((ret = flowoplib_getdirpath(dir, full_path)) != FILEBENCH_OK) 19527946SAndrew.W.Wilson@sun.com return (ret); 19537946SAndrew.W.Wilson@sun.com 19547946SAndrew.W.Wilson@sun.com flowop_beginop(threadflow, flowop); 1955*8615SAndrew.W.Wilson@sun.com (void) FB_MKDIR(full_path, 0755); 19567946SAndrew.W.Wilson@sun.com flowop_endop(threadflow, flowop, 0); 19577946SAndrew.W.Wilson@sun.com 19587946SAndrew.W.Wilson@sun.com /* indicate that it is no longer busy and now exists */ 19598404SAndrew.W.Wilson@sun.com fileset_unbusy(dir, TRUE, TRUE, 0); 19607946SAndrew.W.Wilson@sun.com 19617946SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 19627946SAndrew.W.Wilson@sun.com } 19637946SAndrew.W.Wilson@sun.com 19647946SAndrew.W.Wilson@sun.com /* 19657946SAndrew.W.Wilson@sun.com * Use rmdir to delete a directory. Obtains the fileset name from the 19667946SAndrew.W.Wilson@sun.com * flowop, selects an existent leaf directory and obtains its full path, 19677946SAndrew.W.Wilson@sun.com * then uses rmdir to remove it from the storage subsystem (make it 19687946SAndrew.W.Wilson@sun.com * non-existent). Returns FILEBENCH_NORSC is there are no more existent 19697946SAndrew.W.Wilson@sun.com * directories in the fileset, FILEBENCH_ERROR on other errors, and 19707946SAndrew.W.Wilson@sun.com * FILEBENCH_OK on success. 19717946SAndrew.W.Wilson@sun.com */ 19727946SAndrew.W.Wilson@sun.com static int 19737946SAndrew.W.Wilson@sun.com flowoplib_removedir(threadflow_t *threadflow, flowop_t *flowop) 19747946SAndrew.W.Wilson@sun.com { 19757946SAndrew.W.Wilson@sun.com filesetentry_t *dir; 19767946SAndrew.W.Wilson@sun.com int ret; 19777946SAndrew.W.Wilson@sun.com char full_path[MAXPATHLEN]; 19787946SAndrew.W.Wilson@sun.com 19797946SAndrew.W.Wilson@sun.com if ((ret = flowoplib_pickleafdir(&dir, flowop, 19807946SAndrew.W.Wilson@sun.com FILESET_PICKEXISTS)) != FILEBENCH_OK) 19817946SAndrew.W.Wilson@sun.com return (ret); 19827946SAndrew.W.Wilson@sun.com 19837946SAndrew.W.Wilson@sun.com if ((ret = flowoplib_getdirpath(dir, full_path)) != FILEBENCH_OK) 19847946SAndrew.W.Wilson@sun.com return (ret); 19857946SAndrew.W.Wilson@sun.com 19867946SAndrew.W.Wilson@sun.com flowop_beginop(threadflow, flowop); 1987*8615SAndrew.W.Wilson@sun.com (void) FB_RMDIR(full_path); 19887946SAndrew.W.Wilson@sun.com flowop_endop(threadflow, flowop, 0); 19897946SAndrew.W.Wilson@sun.com 19907946SAndrew.W.Wilson@sun.com /* indicate that it is no longer busy and no longer exists */ 19918404SAndrew.W.Wilson@sun.com fileset_unbusy(dir, TRUE, FALSE, 0); 19927946SAndrew.W.Wilson@sun.com 19937946SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 19947946SAndrew.W.Wilson@sun.com } 19957946SAndrew.W.Wilson@sun.com 19967946SAndrew.W.Wilson@sun.com /* 19977946SAndrew.W.Wilson@sun.com * Use opendir(), multiple readdir() calls, and closedir() to list the 19987946SAndrew.W.Wilson@sun.com * contents of a directory. Obtains the fileset name from the 19997946SAndrew.W.Wilson@sun.com * flowop, selects a normal subdirectory (which always exist) and obtains 20007946SAndrew.W.Wilson@sun.com * its full path, then uses opendir() to get a DIR handle to it from the 20017946SAndrew.W.Wilson@sun.com * file system, a readdir() loop to access each directory entry, and 20027946SAndrew.W.Wilson@sun.com * finally cleans up with a closedir(). The latency reported is the total 20037946SAndrew.W.Wilson@sun.com * for all this activity, and it also reports the total number of bytes 20047946SAndrew.W.Wilson@sun.com * in the entries as the amount "read". Returns FILEBENCH_ERROR on errors, 20057946SAndrew.W.Wilson@sun.com * and FILEBENCH_OK on success. 20067946SAndrew.W.Wilson@sun.com */ 20077946SAndrew.W.Wilson@sun.com static int 20087946SAndrew.W.Wilson@sun.com flowoplib_listdir(threadflow_t *threadflow, flowop_t *flowop) 20097946SAndrew.W.Wilson@sun.com { 20107946SAndrew.W.Wilson@sun.com fileset_t *fileset; 20117946SAndrew.W.Wilson@sun.com filesetentry_t *dir; 2012*8615SAndrew.W.Wilson@sun.com DIR *dir_handle; 20137946SAndrew.W.Wilson@sun.com struct dirent *direntp; 20147946SAndrew.W.Wilson@sun.com int dir_bytes = 0; 20157946SAndrew.W.Wilson@sun.com int ret; 20167946SAndrew.W.Wilson@sun.com char full_path[MAXPATHLEN]; 20177946SAndrew.W.Wilson@sun.com 20187946SAndrew.W.Wilson@sun.com if ((fileset = flowop->fo_fileset) == NULL) { 20197946SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, "flowop NO fileset"); 20207946SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 20217946SAndrew.W.Wilson@sun.com } 20227946SAndrew.W.Wilson@sun.com 20238404SAndrew.W.Wilson@sun.com if ((dir = fileset_pick(fileset, FILESET_PICKDIR, 0, 0)) == NULL) { 20247946SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 20257946SAndrew.W.Wilson@sun.com "flowop %s failed to pick directory from fileset %s", 20267946SAndrew.W.Wilson@sun.com flowop->fo_name, 20277946SAndrew.W.Wilson@sun.com avd_get_str(fileset->fs_name)); 20288404SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 20297946SAndrew.W.Wilson@sun.com } 20307946SAndrew.W.Wilson@sun.com 20317946SAndrew.W.Wilson@sun.com if ((ret = flowoplib_getdirpath(dir, full_path)) != FILEBENCH_OK) 20327946SAndrew.W.Wilson@sun.com return (ret); 20337946SAndrew.W.Wilson@sun.com 20347946SAndrew.W.Wilson@sun.com flowop_beginop(threadflow, flowop); 20357946SAndrew.W.Wilson@sun.com 20367946SAndrew.W.Wilson@sun.com /* open the directory */ 2037*8615SAndrew.W.Wilson@sun.com if ((dir_handle = FB_OPENDIR(full_path)) == NULL) { 20387946SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, 20397946SAndrew.W.Wilson@sun.com "flowop %s failed to open directory in fileset %s\n", 20407946SAndrew.W.Wilson@sun.com flowop->fo_name, avd_get_str(fileset->fs_name)); 20417946SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 20427946SAndrew.W.Wilson@sun.com } 20437946SAndrew.W.Wilson@sun.com 20447946SAndrew.W.Wilson@sun.com /* read through the directory entries */ 2045*8615SAndrew.W.Wilson@sun.com while ((direntp = FB_READDIR(dir_handle)) != NULL) { 20467946SAndrew.W.Wilson@sun.com dir_bytes += (strlen(direntp->d_name) + 20477946SAndrew.W.Wilson@sun.com sizeof (struct dirent) - 1); 20487946SAndrew.W.Wilson@sun.com } 20497946SAndrew.W.Wilson@sun.com 20507946SAndrew.W.Wilson@sun.com /* close the directory */ 2051*8615SAndrew.W.Wilson@sun.com (void) FB_CLOSEDIR(dir_handle); 20527946SAndrew.W.Wilson@sun.com 20537946SAndrew.W.Wilson@sun.com flowop_endop(threadflow, flowop, dir_bytes); 20547946SAndrew.W.Wilson@sun.com 20557946SAndrew.W.Wilson@sun.com /* indicate that it is no longer busy */ 20568404SAndrew.W.Wilson@sun.com fileset_unbusy(dir, FALSE, FALSE, 0); 20577946SAndrew.W.Wilson@sun.com 20587946SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 20597946SAndrew.W.Wilson@sun.com } 20607946SAndrew.W.Wilson@sun.com 20617946SAndrew.W.Wilson@sun.com /* 20625184Sek110237 * Emulate stat of a file. Picks an arbitrary filesetentry with 20635184Sek110237 * an existing file from the flowop's fileset, then performs a 20646084Saw148015 * stat() operation on it. Returns FILEBENCH_ERROR if the flowop has no 20656084Saw148015 * associated fileset. Returns FILEBENCH_NORSC if an appropriate filesetentry 20666084Saw148015 * cannot be found, and FILEBENCH_OK on success. 20675184Sek110237 */ 20685184Sek110237 static int 20695184Sek110237 flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop) 20705184Sek110237 { 20715184Sek110237 filesetentry_t *file; 20725184Sek110237 fileset_t *fileset; 2073*8615SAndrew.W.Wilson@sun.com struct stat64 statbuf; 20747556SAndrew.W.Wilson@sun.com int fd = flowop->fo_fdnumber; 20757556SAndrew.W.Wilson@sun.com 20767556SAndrew.W.Wilson@sun.com /* if fd specified and the file is open, use it to access file */ 2077*8615SAndrew.W.Wilson@sun.com if ((fd > 0) && (threadflow->tf_fd[fd].fd_num > 0)) { 20787556SAndrew.W.Wilson@sun.com 20797556SAndrew.W.Wilson@sun.com /* check whether file handle still valid */ 20807556SAndrew.W.Wilson@sun.com if ((file = threadflow->tf_fse[fd]) == NULL) { 20817556SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 20827556SAndrew.W.Wilson@sun.com "flowop %s trying to stat NULL file at fd = %d", 20837556SAndrew.W.Wilson@sun.com flowop->fo_name, fd); 20847556SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 20857556SAndrew.W.Wilson@sun.com } 20867556SAndrew.W.Wilson@sun.com 20877556SAndrew.W.Wilson@sun.com /* if here, we still have a valid file pointer */ 20887556SAndrew.W.Wilson@sun.com fileset = file->fse_fileset; 20897556SAndrew.W.Wilson@sun.com } else { 20907556SAndrew.W.Wilson@sun.com /* Otherwise, pick arbitrary file */ 20917556SAndrew.W.Wilson@sun.com file = NULL; 20927556SAndrew.W.Wilson@sun.com fileset = flowop->fo_fileset; 20937556SAndrew.W.Wilson@sun.com } 20947556SAndrew.W.Wilson@sun.com 20957556SAndrew.W.Wilson@sun.com if (fileset == NULL) { 20967556SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, 20977556SAndrew.W.Wilson@sun.com "statfile with no fileset specified"); 20987556SAndrew.W.Wilson@sun.com return (FILEBENCH_ERROR); 20997556SAndrew.W.Wilson@sun.com } 21007556SAndrew.W.Wilson@sun.com 21017556SAndrew.W.Wilson@sun.com #ifdef HAVE_RAW_SUPPORT 21027556SAndrew.W.Wilson@sun.com /* can't be used with raw devices */ 21037556SAndrew.W.Wilson@sun.com if (fileset->fs_attrs & FILESET_IS_RAW_DEV) { 21047556SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, 21057556SAndrew.W.Wilson@sun.com "flowop %s attempted do a statfile on a RAW device", 21067556SAndrew.W.Wilson@sun.com flowop->fo_name); 21076084Saw148015 return (FILEBENCH_ERROR); 21085184Sek110237 } 21097556SAndrew.W.Wilson@sun.com #endif /* HAVE_RAW_SUPPORT */ 21107556SAndrew.W.Wilson@sun.com 21117556SAndrew.W.Wilson@sun.com if (file == NULL) { 21127556SAndrew.W.Wilson@sun.com char path[MAXPATHLEN]; 21137556SAndrew.W.Wilson@sun.com char *pathtmp; 21148404SAndrew.W.Wilson@sun.com int err; 21157556SAndrew.W.Wilson@sun.com 21167556SAndrew.W.Wilson@sun.com /* pick arbitrary, existing (allocated) file */ 21178404SAndrew.W.Wilson@sun.com if ((err = flowoplib_pickfile(&file, flowop, 21188404SAndrew.W.Wilson@sun.com FILESET_PICKEXISTS, 0)) != FILEBENCH_OK) { 21197556SAndrew.W.Wilson@sun.com filebench_log(LOG_DEBUG_SCRIPT, 21207556SAndrew.W.Wilson@sun.com "Statfile flowop %s failed to pick file", 21217556SAndrew.W.Wilson@sun.com flowop->fo_name); 21228404SAndrew.W.Wilson@sun.com return (err); 21237556SAndrew.W.Wilson@sun.com } 21247556SAndrew.W.Wilson@sun.com 21257556SAndrew.W.Wilson@sun.com /* resolve path and do a stat on file */ 21267946SAndrew.W.Wilson@sun.com (void) fb_strlcpy(path, avd_get_str(fileset->fs_path), 21277946SAndrew.W.Wilson@sun.com MAXPATHLEN); 21287946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, "/", MAXPATHLEN); 21297946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, avd_get_str(fileset->fs_name), 21307946SAndrew.W.Wilson@sun.com MAXPATHLEN); 21317556SAndrew.W.Wilson@sun.com pathtmp = fileset_resolvepath(file); 21327946SAndrew.W.Wilson@sun.com (void) fb_strlcat(path, pathtmp, MAXPATHLEN); 21337556SAndrew.W.Wilson@sun.com free(pathtmp); 21347556SAndrew.W.Wilson@sun.com 21357556SAndrew.W.Wilson@sun.com /* stat the file */ 21367556SAndrew.W.Wilson@sun.com flowop_beginop(threadflow, flowop); 2137*8615SAndrew.W.Wilson@sun.com if (FB_STAT(path, &statbuf) == -1) 21387556SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, 21397556SAndrew.W.Wilson@sun.com "statfile flowop %s failed", flowop->fo_name); 21407556SAndrew.W.Wilson@sun.com flowop_endop(threadflow, flowop, 0); 21417556SAndrew.W.Wilson@sun.com 21428404SAndrew.W.Wilson@sun.com fileset_unbusy(file, FALSE, FALSE, 0); 21437556SAndrew.W.Wilson@sun.com } else { 21447556SAndrew.W.Wilson@sun.com /* stat specific file */ 21457556SAndrew.W.Wilson@sun.com flowop_beginop(threadflow, flowop); 2146*8615SAndrew.W.Wilson@sun.com if (FB_FSTAT(&threadflow->tf_fd[fd], &statbuf) == -1) 21477556SAndrew.W.Wilson@sun.com filebench_log(LOG_ERROR, 21487556SAndrew.W.Wilson@sun.com "statfile flowop %s failed", flowop->fo_name); 21497556SAndrew.W.Wilson@sun.com flowop_endop(threadflow, flowop, 0); 21507556SAndrew.W.Wilson@sun.com 21515184Sek110237 } 21525184Sek110237 21536084Saw148015 return (FILEBENCH_OK); 21545184Sek110237 } 21555184Sek110237 21565184Sek110237 21575184Sek110237 /* 21585184Sek110237 * Additional reads and writes. Read and write whole files, write 21595184Sek110237 * and append to files. Some of these work with both fileobjs and 21605184Sek110237 * filesets, others only with filesets. The flowoplib_write routine 21615184Sek110237 * writes from thread memory, while the others read or write using 21625184Sek110237 * fo_buf memory. Note that both flowoplib_read() and 21635184Sek110237 * flowoplib_aiowrite() use thread memory as well. 21645184Sek110237 */ 21655184Sek110237 21665184Sek110237 21675184Sek110237 /* 21685673Saw148015 * Emulate a read of a whole file. The file must be open with 21695673Saw148015 * file descriptor and filesetentry stored at the locations indexed 21705673Saw148015 * by the flowop's fdnumber. It then seeks to the beginning of the 21715673Saw148015 * associated file, and reads fs_iosize bytes at a time until the end 21726084Saw148015 * of the file. Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if 21736084Saw148015 * out of files, and FILEBENCH_OK on success. 21745184Sek110237 */ 21755184Sek110237 static int 21765184Sek110237 flowoplib_readwholefile(threadflow_t *threadflow, flowop_t *flowop) 21775184Sek110237 { 21785673Saw148015 caddr_t iobuf; 21795184Sek110237 off64_t bytes = 0; 2180*8615SAndrew.W.Wilson@sun.com fb_fdesc_t *fdesc; 21816212Saw148015 uint64_t wss; 21826212Saw148015 fbint_t iosize; 21835184Sek110237 int ret; 21846212Saw148015 char zerordbuf; 21855184Sek110237 21865673Saw148015 /* get the file to use */ 21876084Saw148015 if ((ret = flowoplib_filesetup(threadflow, flowop, &wss, 2188*8615SAndrew.W.Wilson@sun.com &fdesc)) != FILEBENCH_OK) 21896084Saw148015 return (ret); 21905184Sek110237 21915673Saw148015 /* an I/O size of zero means read entire working set with one I/O */ 21926212Saw148015 if ((iosize = avd_get_int(flowop->fo_iosize)) == 0) 21935673Saw148015 iosize = wss; 21945184Sek110237 21956212Saw148015 /* 21966212Saw148015 * The file may actually be 0 bytes long, in which case skip 21976212Saw148015 * the buffer set up call (which would fail) and substitute 21986212Saw148015 * a small buffer, which won't really be used. 21996212Saw148015 */ 22006212Saw148015 if (iosize == 0) { 22016212Saw148015 iobuf = (caddr_t)&zerordbuf; 22026212Saw148015 filebench_log(LOG_DEBUG_SCRIPT, 22036212Saw148015 "flowop %s read zero length file", flowop->fo_name); 22046212Saw148015 } else { 22056212Saw148015 if (flowoplib_iobufsetup(threadflow, flowop, &iobuf, 22066212Saw148015 iosize) != 0) 22076212Saw148015 return (FILEBENCH_ERROR); 22086212Saw148015 } 22095184Sek110237 22105184Sek110237 /* Measure time to read bytes */ 22115184Sek110237 flowop_beginop(threadflow, flowop); 2212*8615SAndrew.W.Wilson@sun.com (void) FB_LSEEK(fdesc, 0, SEEK_SET); 2213*8615SAndrew.W.Wilson@sun.com while ((ret = FB_READ(fdesc, iobuf, iosize)) > 0) 22145184Sek110237 bytes += ret; 22155184Sek110237 22165673Saw148015 flowop_endop(threadflow, flowop, bytes); 22175184Sek110237 22185184Sek110237 if (ret < 0) { 22195184Sek110237 filebench_log(LOG_ERROR, 22206391Saw148015 "readwhole fail Failed to read whole file: %s", 22216391Saw148015 strerror(errno)); 22226084Saw148015 return (FILEBENCH_ERROR); 22235184Sek110237 } 22245184Sek110237 22256084Saw148015 return (FILEBENCH_OK); 22265184Sek110237 } 22275184Sek110237 22285184Sek110237 /* 22295184Sek110237 * Emulate a write to a file of size fo_iosize. Will write 22305184Sek110237 * to a file from a fileset if the flowop's fo_fileset field 22315184Sek110237 * specifies one or its fdnumber is non zero. Otherwise it 22325184Sek110237 * will write to a fileobj file, if one exists. If the file 22335184Sek110237 * is not currently open, the routine will attempt to open 22345184Sek110237 * it. The flowop's fo_wss parameter will be used to set the 22355184Sek110237 * maximum file size if it is non-zero, otherwise the 22365184Sek110237 * filesetentry's fse_size will be used. A random memory 22375184Sek110237 * buffer offset is calculated, and, if fo_random is TRUE, 22385184Sek110237 * a random file offset is used for the write. Otherwise the 22396084Saw148015 * write is to the next sequential location. Returns 22406084Saw148015 * FILEBENCH_ERROR on errors, FILEBENCH_NORSC if iosetup can't 22416084Saw148015 * obtain a file, or FILEBENCH_OK on success. 22425184Sek110237 */ 22435184Sek110237 static int 22445184Sek110237 flowoplib_write(threadflow_t *threadflow, flowop_t *flowop) 22455184Sek110237 { 22465673Saw148015 caddr_t iobuf; 22476212Saw148015 fbint_t wss; 22486212Saw148015 fbint_t iosize; 2249*8615SAndrew.W.Wilson@sun.com fb_fdesc_t *fdesc; 22506084Saw148015 int ret; 22515184Sek110237 22526212Saw148015 iosize = avd_get_int(flowop->fo_iosize); 22536084Saw148015 if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf, 2254*8615SAndrew.W.Wilson@sun.com &fdesc, iosize)) != FILEBENCH_OK) 22556084Saw148015 return (ret); 22565184Sek110237 22576212Saw148015 if (avd_get_bool(flowop->fo_random)) { 22585184Sek110237 uint64_t fileoffset; 22595184Sek110237 22605184Sek110237 if (filebench_randomno64(&fileoffset, 22616212Saw148015 wss, iosize, NULL) == -1) { 22625184Sek110237 filebench_log(LOG_ERROR, 22635184Sek110237 "file size smaller than IO size for thread %s", 22645184Sek110237 flowop->fo_name); 22656084Saw148015 return (FILEBENCH_ERROR); 22665184Sek110237 } 22675184Sek110237 flowop_beginop(threadflow, flowop); 2268*8615SAndrew.W.Wilson@sun.com if (FB_PWRITE(fdesc, iobuf, 22696212Saw148015 iosize, (off64_t)fileoffset) == -1) { 22705184Sek110237 filebench_log(LOG_ERROR, "write failed, " 22716286Saw148015 "offset %llu io buffer %zd: %s", 22726286Saw148015 (u_longlong_t)fileoffset, iobuf, strerror(errno)); 22735673Saw148015 flowop_endop(threadflow, flowop, 0); 22746084Saw148015 return (FILEBENCH_ERROR); 22755184Sek110237 } 22766212Saw148015 flowop_endop(threadflow, flowop, iosize); 22775184Sek110237 } else { 22785184Sek110237 flowop_beginop(threadflow, flowop); 2279*8615SAndrew.W.Wilson@sun.com if (FB_WRITE(fdesc, iobuf, iosize) == -1) { 22805184Sek110237 filebench_log(LOG_ERROR, 22815673Saw148015 "write failed, io buffer %zd: %s", 22825673Saw148015 iobuf, strerror(errno)); 22835673Saw148015 flowop_endop(threadflow, flowop, 0); 22846084Saw148015 return (FILEBENCH_ERROR); 22855184Sek110237 } 22866212Saw148015 flowop_endop(threadflow, flowop, iosize); 22875184Sek110237 } 22885184Sek110237 22896084Saw148015 return (FILEBENCH_OK); 22905184Sek110237 } 22915184Sek110237 22925184Sek110237 /* 22935184Sek110237 * Emulate a write of a whole file. The size of the file 22945673Saw148015 * is taken from a filesetentry identified by fo_srcfdnumber or 22955673Saw148015 * from the working set size, while the file descriptor used is 22965673Saw148015 * identified by fo_fdnumber. Does multiple writes of fo_iosize 22976084Saw148015 * length length until full file has been written. Returns FILEBENCH_ERROR on 22986084Saw148015 * error, FILEBENCH_NORSC if out of files, FILEBENCH_OK on success. 22995184Sek110237 */ 23005184Sek110237 static int 23015184Sek110237 flowoplib_writewholefile(threadflow_t *threadflow, flowop_t *flowop) 23025184Sek110237 { 23035673Saw148015 caddr_t iobuf; 23045184Sek110237 filesetentry_t *file; 23055184Sek110237 int wsize; 23065184Sek110237 off64_t seek; 23075184Sek110237 off64_t bytes = 0; 23085673Saw148015 uint64_t wss; 23096212Saw148015 fbint_t iosize; 2310*8615SAndrew.W.Wilson@sun.com fb_fdesc_t *fdesc; 23115184Sek110237 int srcfd = flowop->fo_srcfdnumber; 23125184Sek110237 int ret; 23136212Saw148015 char zerowrtbuf; 23145184Sek110237 23155673Saw148015 /* get the file to use */ 23166084Saw148015 if ((ret = flowoplib_filesetup(threadflow, flowop, &wss, 2317*8615SAndrew.W.Wilson@sun.com &fdesc)) != FILEBENCH_OK) 23186084Saw148015 return (ret); 23195184Sek110237 23206212Saw148015 /* an I/O size of zero means write entire working set with one I/O */ 23216212Saw148015 if ((iosize = avd_get_int(flowop->fo_iosize)) == 0) 23225673Saw148015 iosize = wss; 23235184Sek110237 23246212Saw148015 /* 23256212Saw148015 * The file may actually be 0 bytes long, in which case skip 23266212Saw148015 * the buffer set up call (which would fail) and substitute 23276212Saw148015 * a small buffer, which won't really be used. 23286212Saw148015 */ 23296212Saw148015 if (iosize == 0) { 23306212Saw148015 iobuf = (caddr_t)&zerowrtbuf; 23316212Saw148015 filebench_log(LOG_DEBUG_SCRIPT, 23326212Saw148015 "flowop %s wrote zero length file", flowop->fo_name); 23336212Saw148015 } else { 23346212Saw148015 if (flowoplib_iobufsetup(threadflow, flowop, &iobuf, 23356212Saw148015 iosize) != 0) 23366212Saw148015 return (FILEBENCH_ERROR); 23376212Saw148015 } 23385184Sek110237 23395184Sek110237 file = threadflow->tf_fse[srcfd]; 23405673Saw148015 if ((srcfd != 0) && (file == NULL)) { 23415673Saw148015 filebench_log(LOG_ERROR, "flowop %s: NULL src file", 23425184Sek110237 flowop->fo_name); 23436084Saw148015 return (FILEBENCH_ERROR); 23445184Sek110237 } 23455184Sek110237 23465673Saw148015 if (file) 23475673Saw148015 wss = file->fse_size; 23485673Saw148015 23495673Saw148015 wsize = (int)MIN(wss, iosize); 23505184Sek110237 23515184Sek110237 /* Measure time to write bytes */ 23525184Sek110237 flowop_beginop(threadflow, flowop); 23535673Saw148015 for (seek = 0; seek < wss; seek += wsize) { 2354*8615SAndrew.W.Wilson@sun.com ret = FB_WRITE(fdesc, iobuf, wsize); 23555184Sek110237 if (ret != wsize) { 23565184Sek110237 filebench_log(LOG_ERROR, 23575184Sek110237 "Failed to write %d bytes on fd %d: %s", 2358*8615SAndrew.W.Wilson@sun.com wsize, fdesc->fd_num, strerror(errno)); 23595673Saw148015 flowop_endop(threadflow, flowop, 0); 23606084Saw148015 return (FILEBENCH_ERROR); 23615184Sek110237 } 23625673Saw148015 wsize = (int)MIN(wss - seek, iosize); 23635184Sek110237 bytes += ret; 23645184Sek110237 } 23655673Saw148015 flowop_endop(threadflow, flowop, bytes); 23665184Sek110237 23676084Saw148015 return (FILEBENCH_OK); 23685184Sek110237 } 23695184Sek110237 23705184Sek110237 23715184Sek110237 /* 23725184Sek110237 * Emulate a fixed size append to a file. Will append data to 23735184Sek110237 * a file chosen from a fileset if the flowop's fo_fileset 23745184Sek110237 * field specifies one or if its fdnumber is non zero. 23755184Sek110237 * Otherwise it will write to a fileobj file, if one exists. 23765184Sek110237 * The flowop's fo_wss parameter will be used to set the 23775184Sek110237 * maximum file size if it is non-zero, otherwise the 23785184Sek110237 * filesetentry's fse_size will be used. A random memory 23795184Sek110237 * buffer offset is calculated, then a logical seek to the 23805184Sek110237 * end of file is done followed by a write of fo_iosize 23815184Sek110237 * bytes. Writes are actually done from fo_buf, rather than 23825184Sek110237 * tf_mem as is done with flowoplib_write(), and no check 23835184Sek110237 * is made to see if fo_iosize exceeds the size of fo_buf. 23846084Saw148015 * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of 23856084Saw148015 * files in the fileset, FILEBENCH_OK on success. 23865184Sek110237 */ 23875184Sek110237 static int 23885184Sek110237 flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop) 23895184Sek110237 { 23905673Saw148015 caddr_t iobuf; 2391*8615SAndrew.W.Wilson@sun.com fb_fdesc_t *fdesc; 23926212Saw148015 fbint_t wss; 23936212Saw148015 fbint_t iosize; 23945184Sek110237 int ret; 23955184Sek110237 23966212Saw148015 iosize = avd_get_int(flowop->fo_iosize); 23976084Saw148015 if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf, 2398*8615SAndrew.W.Wilson@sun.com &fdesc, iosize)) != FILEBENCH_OK) 23996084Saw148015 return (ret); 24005184Sek110237 24015184Sek110237 /* XXX wss is not being used */ 24025184Sek110237 24035184Sek110237 /* Measure time to write bytes */ 24045184Sek110237 flowop_beginop(threadflow, flowop); 2405*8615SAndrew.W.Wilson@sun.com (void) FB_LSEEK(fdesc, 0, SEEK_END); 2406*8615SAndrew.W.Wilson@sun.com ret = FB_WRITE(fdesc, iobuf, iosize); 24075673Saw148015 if (ret != iosize) { 24085184Sek110237 filebench_log(LOG_ERROR, 24096286Saw148015 "Failed to write %llu bytes on fd %d: %s", 2410*8615SAndrew.W.Wilson@sun.com (u_longlong_t)iosize, fdesc->fd_num, strerror(errno)); 24116212Saw148015 flowop_endop(threadflow, flowop, ret); 24126084Saw148015 return (FILEBENCH_ERROR); 24135184Sek110237 } 24146212Saw148015 flowop_endop(threadflow, flowop, ret); 24155184Sek110237 24166084Saw148015 return (FILEBENCH_OK); 24175184Sek110237 } 24185184Sek110237 24195184Sek110237 /* 24205184Sek110237 * Emulate a random size append to a file. Will append data 24215184Sek110237 * to a file chosen from a fileset if the flowop's fo_fileset 24225184Sek110237 * field specifies one or if its fdnumber is non zero. Otherwise 24235184Sek110237 * it will write to a fileobj file, if one exists. The flowop's 24245184Sek110237 * fo_wss parameter will be used to set the maximum file size 24255184Sek110237 * if it is non-zero, otherwise the filesetentry's fse_size 24265184Sek110237 * will be used. A random transfer size (but at most fo_iosize 24275184Sek110237 * bytes) and a random memory offset are calculated. A logical 24285184Sek110237 * seek to the end of file is done, then writes of up to 24295184Sek110237 * FILE_ALLOC_BLOCK in size are done until the full transfer 24305184Sek110237 * size has been written. Writes are actually done from fo_buf, 24315184Sek110237 * rather than tf_mem as is done with flowoplib_write(). 24326084Saw148015 * Returns FILEBENCH_ERROR on error, FILEBENCH_NORSC if out of 24336084Saw148015 * files in the fileset, FILEBENCH_OK on success. 24345184Sek110237 */ 24355184Sek110237 static int 24365184Sek110237 flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop) 24375184Sek110237 { 24385673Saw148015 caddr_t iobuf; 24395184Sek110237 uint64_t appendsize; 2440*8615SAndrew.W.Wilson@sun.com fb_fdesc_t *fdesc; 24416212Saw148015 fbint_t wss; 24426212Saw148015 fbint_t iosize; 24436212Saw148015 int ret = 0; 24445184Sek110237 24456212Saw148015 if ((iosize = avd_get_int(flowop->fo_iosize)) == 0) { 24466212Saw148015 filebench_log(LOG_ERROR, "zero iosize for flowop %s", 24476212Saw148015 flowop->fo_name); 24486212Saw148015 return (FILEBENCH_ERROR); 24496212Saw148015 } 24506212Saw148015 24516212Saw148015 if (filebench_randomno64(&appendsize, iosize, 1LL, NULL) != 0) 24526084Saw148015 return (FILEBENCH_ERROR); 24535184Sek110237 24545673Saw148015 /* skip if attempting zero length append */ 24555673Saw148015 if (appendsize == 0) { 24565673Saw148015 flowop_beginop(threadflow, flowop); 24575673Saw148015 flowop_endop(threadflow, flowop, 0LL); 24586084Saw148015 return (FILEBENCH_OK); 24595673Saw148015 } 24605184Sek110237 24616084Saw148015 if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf, 2462*8615SAndrew.W.Wilson@sun.com &fdesc, appendsize)) != FILEBENCH_OK) 24636084Saw148015 return (ret); 24645673Saw148015 24655184Sek110237 /* XXX wss is not being used */ 24665184Sek110237 24675673Saw148015 /* Measure time to write bytes */ 24685673Saw148015 flowop_beginop(threadflow, flowop); 24695673Saw148015 2470*8615SAndrew.W.Wilson@sun.com (void) FB_LSEEK(fdesc, 0, SEEK_END); 2471*8615SAndrew.W.Wilson@sun.com ret = FB_WRITE(fdesc, iobuf, appendsize); 24725673Saw148015 if (ret != appendsize) { 24735673Saw148015 filebench_log(LOG_ERROR, 24746286Saw148015 "Failed to write %llu bytes on fd %d: %s", 2475*8615SAndrew.W.Wilson@sun.com (u_longlong_t)appendsize, fdesc->fd_num, strerror(errno)); 24765673Saw148015 flowop_endop(threadflow, flowop, 0); 24776084Saw148015 return (FILEBENCH_ERROR); 24785184Sek110237 } 24795184Sek110237 24805673Saw148015 flowop_endop(threadflow, flowop, appendsize); 24815184Sek110237 24826084Saw148015 return (FILEBENCH_OK); 24835184Sek110237 } 24845184Sek110237 24856212Saw148015 typedef struct testrandvar_priv { 24866212Saw148015 uint64_t sample_count; 24876212Saw148015 double val_sum; 24886212Saw148015 double sqr_sum; 24896212Saw148015 } testrandvar_priv_t; 24906212Saw148015 24916212Saw148015 /* 24926212Saw148015 * flowop to calculate various statistics from the number stream 24936212Saw148015 * produced by a random variable. This allows verification that the 24946212Saw148015 * random distribution used to define the random variable is producing 24956212Saw148015 * the expected distribution of random numbers. 24966212Saw148015 */ 24976212Saw148015 /* ARGSUSED */ 24986212Saw148015 static int 24996212Saw148015 flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop) 25006212Saw148015 { 25016212Saw148015 testrandvar_priv_t *mystats; 25026212Saw148015 double value; 25036212Saw148015 25046212Saw148015 if ((mystats = (testrandvar_priv_t *)flowop->fo_private) == NULL) { 25056212Saw148015 filebench_log(LOG_ERROR, "testrandvar not initialized\n"); 25066212Saw148015 filebench_shutdown(1); 25076212Saw148015 return (-1); 25086212Saw148015 } 25096212Saw148015 25106212Saw148015 value = avd_get_dbl(flowop->fo_value); 25116212Saw148015 25126212Saw148015 mystats->sample_count++; 25136212Saw148015 mystats->val_sum += value; 25146212Saw148015 mystats->sqr_sum += (value * value); 25156212Saw148015 25166212Saw148015 return (0); 25176212Saw148015 } 25186212Saw148015 25196212Saw148015 /* 25206212Saw148015 * Initialize the private data area used to accumulate the statistics 25216212Saw148015 */ 25226212Saw148015 static int 25236212Saw148015 flowoplib_testrandvar_init(flowop_t *flowop) 25246212Saw148015 { 25256212Saw148015 testrandvar_priv_t *mystats; 25266212Saw148015 25276212Saw148015 if ((mystats = (testrandvar_priv_t *) 25286212Saw148015 malloc(sizeof (testrandvar_priv_t))) == NULL) { 25296212Saw148015 filebench_log(LOG_ERROR, "could not initialize testrandvar"); 25306212Saw148015 filebench_shutdown(1); 25316212Saw148015 return (-1); 25326212Saw148015 } 25336212Saw148015 25346212Saw148015 mystats->sample_count = 0; 25356212Saw148015 mystats->val_sum = 0; 25366212Saw148015 mystats->sqr_sum = 0; 25376212Saw148015 flowop->fo_private = (void *)mystats; 25386212Saw148015 25396212Saw148015 (void) ipc_mutex_unlock(&flowop->fo_lock); 25406212Saw148015 return (0); 25416212Saw148015 } 25426212Saw148015 25436212Saw148015 /* 25446212Saw148015 * Print out the accumulated statistics, and free the private storage 25456212Saw148015 */ 25466212Saw148015 static void 25476212Saw148015 flowoplib_testrandvar_destruct(flowop_t *flowop) 25486212Saw148015 { 25496212Saw148015 testrandvar_priv_t *mystats; 25506212Saw148015 double mean, std_dev, dbl_count; 25516212Saw148015 25526212Saw148015 (void) ipc_mutex_lock(&flowop->fo_lock); 25536212Saw148015 if ((mystats = (testrandvar_priv_t *) 25546212Saw148015 flowop->fo_private) == NULL) { 25556212Saw148015 (void) ipc_mutex_unlock(&flowop->fo_lock); 25566212Saw148015 return; 25576212Saw148015 } 25586212Saw148015 25596212Saw148015 flowop->fo_private = NULL; 25606212Saw148015 (void) ipc_mutex_unlock(&flowop->fo_lock); 25616212Saw148015 25626212Saw148015 dbl_count = (double)mystats->sample_count; 25636212Saw148015 mean = mystats->val_sum / dbl_count; 25646212Saw148015 std_dev = sqrt((mystats->sqr_sum / dbl_count) - (mean * mean)) / mean; 25656212Saw148015 25666212Saw148015 filebench_log(LOG_VERBOSE, 25676286Saw148015 "testrandvar: ops = %llu, mean = %8.2lf, stddev = %8.2lf", 25686286Saw148015 (u_longlong_t)mystats->sample_count, mean, std_dev); 25696212Saw148015 free(mystats); 25706212Saw148015 } 25715184Sek110237 25725184Sek110237 /* 25737556SAndrew.W.Wilson@sun.com * prints message to the console from within a thread 25747556SAndrew.W.Wilson@sun.com */ 25757556SAndrew.W.Wilson@sun.com static int 25767556SAndrew.W.Wilson@sun.com flowoplib_print(threadflow_t *threadflow, flowop_t *flowop) 25777556SAndrew.W.Wilson@sun.com { 25787556SAndrew.W.Wilson@sun.com procflow_t *procflow; 25797556SAndrew.W.Wilson@sun.com 25807556SAndrew.W.Wilson@sun.com procflow = threadflow->tf_process; 25817556SAndrew.W.Wilson@sun.com filebench_log(LOG_INFO, 25827556SAndrew.W.Wilson@sun.com "Message from process (%s,%d), thread (%s,%d): %s", 25837556SAndrew.W.Wilson@sun.com procflow->pf_name, procflow->pf_instance, 25847556SAndrew.W.Wilson@sun.com threadflow->tf_name, threadflow->tf_instance, 25857556SAndrew.W.Wilson@sun.com avd_get_str(flowop->fo_value)); 25867556SAndrew.W.Wilson@sun.com 25877556SAndrew.W.Wilson@sun.com return (FILEBENCH_OK); 25887556SAndrew.W.Wilson@sun.com } 25897556SAndrew.W.Wilson@sun.com 25907556SAndrew.W.Wilson@sun.com /* 25915184Sek110237 * Prints usage information for flowop operations. 25925184Sek110237 */ 25935184Sek110237 void 25945184Sek110237 flowoplib_usage() 25955184Sek110237 { 25965184Sek110237 (void) fprintf(stderr, 25975184Sek110237 "flowop [openfile|createfile] name=<name>,fileset=<fname>\n"); 25985184Sek110237 (void) fprintf(stderr, 25995184Sek110237 " [,fd=<file desc num>]\n"); 26005184Sek110237 (void) fprintf(stderr, "\n"); 26015184Sek110237 (void) fprintf(stderr, 26025184Sek110237 "flowop closefile name=<name>,fd=<file desc num>]\n"); 26035184Sek110237 (void) fprintf(stderr, "\n"); 26045184Sek110237 (void) fprintf(stderr, "flowop deletefile name=<name>\n"); 26055184Sek110237 (void) fprintf(stderr, " [,fileset=<fname>]\n"); 26065184Sek110237 (void) fprintf(stderr, 26075184Sek110237 " [,fd=<file desc num>]\n"); 26085184Sek110237 (void) fprintf(stderr, "\n"); 26095184Sek110237 (void) fprintf(stderr, "flowop statfile name=<name>\n"); 26105184Sek110237 (void) fprintf(stderr, " [,fileset=<fname>]\n"); 26115184Sek110237 (void) fprintf(stderr, 26125184Sek110237 " [,fd=<file desc num>]\n"); 26135184Sek110237 (void) fprintf(stderr, "\n"); 26145184Sek110237 (void) fprintf(stderr, 26155184Sek110237 "flowop fsync name=<name>,fd=<file desc num>]\n"); 26165184Sek110237 (void) fprintf(stderr, "\n"); 26175184Sek110237 (void) fprintf(stderr, 26185184Sek110237 "flowop fsyncset name=<name>,fileset=<fname>]\n"); 26195184Sek110237 (void) fprintf(stderr, "\n"); 26205184Sek110237 (void) fprintf(stderr, "flowop [write|read|aiowrite] name=<name>, \n"); 26215184Sek110237 (void) fprintf(stderr, 26225184Sek110237 " filename|fileset=<fname>,\n"); 26235184Sek110237 (void) fprintf(stderr, " iosize=<size>\n"); 26245184Sek110237 (void) fprintf(stderr, " [,directio]\n"); 26255184Sek110237 (void) fprintf(stderr, " [,dsync]\n"); 26265184Sek110237 (void) fprintf(stderr, " [,iters=<count>]\n"); 26275184Sek110237 (void) fprintf(stderr, " [,random]\n"); 26285184Sek110237 (void) fprintf(stderr, " [,opennext]\n"); 26295184Sek110237 (void) fprintf(stderr, " [,workingset=<size>]\n"); 26305184Sek110237 (void) fprintf(stderr, 26315184Sek110237 "flowop [appendfile|appendfilerand] name=<name>, \n"); 26325184Sek110237 (void) fprintf(stderr, 26335184Sek110237 " filename|fileset=<fname>,\n"); 26345184Sek110237 (void) fprintf(stderr, " iosize=<size>\n"); 26355184Sek110237 (void) fprintf(stderr, " [,dsync]\n"); 26365184Sek110237 (void) fprintf(stderr, " [,iters=<count>]\n"); 26375184Sek110237 (void) fprintf(stderr, " [,workingset=<size>]\n"); 26385184Sek110237 (void) fprintf(stderr, 26395184Sek110237 "flowop [readwholefile|writewholefile] name=<name>, \n"); 26405184Sek110237 (void) fprintf(stderr, 26415184Sek110237 " filename|fileset=<fname>,\n"); 26425184Sek110237 (void) fprintf(stderr, " iosize=<size>\n"); 26435184Sek110237 (void) fprintf(stderr, " [,dsync]\n"); 26445184Sek110237 (void) fprintf(stderr, " [,iters=<count>]\n"); 26455184Sek110237 (void) fprintf(stderr, "\n"); 26465184Sek110237 (void) fprintf(stderr, "flowop aiowait name=<name>,target=" 26475184Sek110237 "<aiowrite-flowop>\n"); 26485184Sek110237 (void) fprintf(stderr, "\n"); 26495184Sek110237 (void) fprintf(stderr, "flowop sempost name=<name>," 26505184Sek110237 "target=<semblock-flowop>,\n"); 26515184Sek110237 (void) fprintf(stderr, 26525184Sek110237 " value=<increment-to-post>\n"); 26535184Sek110237 (void) fprintf(stderr, "\n"); 26545184Sek110237 (void) fprintf(stderr, "flowop semblock name=<name>,value=" 26555184Sek110237 "<decrement-to-receive>,\n"); 26565184Sek110237 (void) fprintf(stderr, " highwater=" 26575184Sek110237 "<inbound-queue-max>\n"); 26585184Sek110237 (void) fprintf(stderr, "\n"); 26595184Sek110237 (void) fprintf(stderr, "flowop block name=<name>\n"); 26605184Sek110237 (void) fprintf(stderr, "\n"); 26615184Sek110237 (void) fprintf(stderr, 26625184Sek110237 "flowop wakeup name=<name>,target=<block-flowop>,\n"); 26635184Sek110237 (void) fprintf(stderr, "\n"); 26645184Sek110237 (void) fprintf(stderr, 26655184Sek110237 "flowop hog name=<name>,value=<number-of-mem-ops>\n"); 26665184Sek110237 (void) fprintf(stderr, 26675184Sek110237 "flowop delay name=<name>,value=<number-of-seconds>\n"); 26685184Sek110237 (void) fprintf(stderr, "\n"); 26695184Sek110237 (void) fprintf(stderr, "flowop eventlimit name=<name>\n"); 26705184Sek110237 (void) fprintf(stderr, "flowop bwlimit name=<name>,value=<mb/s>\n"); 26715184Sek110237 (void) fprintf(stderr, "flowop iopslimit name=<name>,value=<iop/s>\n"); 26725184Sek110237 (void) fprintf(stderr, 26735184Sek110237 "flowop finishoncount name=<name>,value=<ops/s>\n"); 26745184Sek110237 (void) fprintf(stderr, 26755184Sek110237 "flowop finishonbytes name=<name>,value=<bytes>\n"); 26765184Sek110237 (void) fprintf(stderr, "\n"); 26775184Sek110237 (void) fprintf(stderr, "\n"); 26785184Sek110237 } 2679