xref: /onnv-gate/usr/src/cmd/filebench/common/eventgen.c (revision 9801:4a9784073e11)
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*9801SAndrew.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 /*
295184Sek110237  * The event generator in this module is the producer half of a
305184Sek110237  * metering system which blocks flows using consumer routines in the
315184Sek110237  * flowop_library.c module. Four routines in that module can limit rates
325184Sek110237  * by event rate (flowoplib_eventlimit), by I/O operations rate
335184Sek110237  * (flowoplib_iopslimit()), by operations rate (flowoplib_opslimit),
345184Sek110237  * or by I/O bandwidth limit (flowoplib_bwlimit). By setting appropriate
355184Sek110237  * event generation rates, required calls per second, I/O ops per second,
365184Sek110237  * file system ops per second, or I/O bandwidth per second limits can
375184Sek110237  * be set. Note, the generated events are shared with all consumer
385184Sek110237  * flowops, of which their will be one for each process / thread
395184Sek110237  * instance which has a consumer flowop defined in it.
405184Sek110237  */
415184Sek110237 
425184Sek110237 #include <sys/time.h>
436613Sek110237 
446613Sek110237 #include "filebench.h"
455184Sek110237 #include "vars.h"
465184Sek110237 #include "eventgen.h"
475184Sek110237 #include "flowop.h"
485184Sek110237 #include "ipc.h"
495184Sek110237 
505184Sek110237 /*
515184Sek110237  * Prints "how to use" information for the eventgen module
525184Sek110237  */
535184Sek110237 void
eventgen_usage(void)545184Sek110237 eventgen_usage(void)
555184Sek110237 {
565184Sek110237 	(void) fprintf(stderr, "eventgen rate=<rate>\n");
575184Sek110237 	(void) fprintf(stderr, "\n");
585184Sek110237 }
595184Sek110237 
605184Sek110237 /*
615184Sek110237  * The producer side of the event system.
625184Sek110237  * Once eventgen_hz has been set by eventgen_setrate(),
635184Sek110237  * the routine sends eventgen_hz events per second until
645184Sek110237  * the program terminates. Events are posted by incrementing
656391Saw148015  * filebench_shm->shm_eventgen_q by the number of generated
665184Sek110237  * events then signalling the condition variable
676391Saw148015  * filebench_shm->shm_eventgen_cv to indicate to event consumers
685184Sek110237  * that more events are available.
695184Sek110237  *
705184Sek110237  * Eventgen_thread attempts to sleep for 10 event periods,
715184Sek110237  * then, once awakened, determines how many periods actually
725184Sek110237  * passed since sleeping, and issues a set of events equal
735184Sek110237  * to the number of periods that it slept, thus keeping the
745184Sek110237  * average rate at the requested rate.
755184Sek110237  */
765184Sek110237 static void
eventgen_thread(void)775184Sek110237 eventgen_thread(void)
785184Sek110237 {
795184Sek110237 	hrtime_t last;
805184Sek110237 
815184Sek110237 	last = gethrtime();
82*9801SAndrew.W.Wilson@sun.com 	filebench_shm->shm_eventgen_enabled = FALSE;
835184Sek110237 
845184Sek110237 	/* CONSTCOND */
855184Sek110237 	while (1) {
865184Sek110237 		struct timespec sleeptime;
875184Sek110237 		hrtime_t delta;
887946SAndrew.W.Wilson@sun.com 		int count, rate;
895184Sek110237 
907946SAndrew.W.Wilson@sun.com 		if (filebench_shm->shm_eventgen_hz == NULL) {
915184Sek110237 			(void) sleep(1);
925184Sek110237 			continue;
937946SAndrew.W.Wilson@sun.com 		} else {
947946SAndrew.W.Wilson@sun.com 			rate = avd_get_int(filebench_shm->shm_eventgen_hz);
95*9801SAndrew.W.Wilson@sun.com 			if (rate > 0) {
96*9801SAndrew.W.Wilson@sun.com 				filebench_shm->shm_eventgen_enabled = TRUE;
97*9801SAndrew.W.Wilson@sun.com 			} else {
98*9801SAndrew.W.Wilson@sun.com 				continue;
99*9801SAndrew.W.Wilson@sun.com 			}
1005184Sek110237 		}
1017946SAndrew.W.Wilson@sun.com 
1025184Sek110237 		/* Sleep for 10xperiod */
1035184Sek110237 		sleeptime.tv_sec = 0;
1047946SAndrew.W.Wilson@sun.com 		sleeptime.tv_nsec = FB_SEC2NSEC / rate;
1056391Saw148015 
1065184Sek110237 		sleeptime.tv_nsec *= 10;
1075184Sek110237 		if (sleeptime.tv_nsec < 1000UL)
1085184Sek110237 			sleeptime.tv_nsec = 1000UL;
1096391Saw148015 
1106391Saw148015 		sleeptime.tv_sec = sleeptime.tv_nsec / FB_SEC2NSEC;
1115184Sek110237 		if (sleeptime.tv_sec > 0)
1126391Saw148015 			sleeptime.tv_nsec -= (sleeptime.tv_sec * FB_SEC2NSEC);
1136391Saw148015 
1145184Sek110237 		(void) nanosleep(&sleeptime, NULL);
1155184Sek110237 		delta = gethrtime() - last;
1165184Sek110237 		last = gethrtime();
1177946SAndrew.W.Wilson@sun.com 		count = (rate * delta) / FB_SEC2NSEC;
1185184Sek110237 
1195184Sek110237 		filebench_log(LOG_DEBUG_SCRIPT,
1206286Saw148015 		    "delta %llums count %d",
1216286Saw148015 		    (u_longlong_t)(delta / 1000000), count);
1225184Sek110237 
1235184Sek110237 		/* Send 'count' events */
1246391Saw148015 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
1255184Sek110237 		/* Keep the producer with a max of 5 second depth */
1267946SAndrew.W.Wilson@sun.com 		if (filebench_shm->shm_eventgen_q < (5 * rate))
1276391Saw148015 			filebench_shm->shm_eventgen_q += count;
1285184Sek110237 
1296391Saw148015 		(void) pthread_cond_signal(&filebench_shm->shm_eventgen_cv);
1305184Sek110237 
1316391Saw148015 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
1325184Sek110237 	}
1335184Sek110237 }
1345184Sek110237 
1355184Sek110237 /*
1365184Sek110237  * Creates a thread to run the event generator eventgen_thread
1375184Sek110237  * routine. Shuts down filebench if the eventgen thread cannot
1385184Sek110237  * be created.
1395184Sek110237  */
1405184Sek110237 void
eventgen_init(void)1415184Sek110237 eventgen_init(void)
1425184Sek110237 {
1435184Sek110237 	/*
1445184Sek110237 	 * Linux does not like it if the first
1455184Sek110237 	 * argument to pthread_create is null. It actually
1465184Sek110237 	 * segv's. -neel
1475184Sek110237 	 */
1485184Sek110237 	pthread_t tid;
1495184Sek110237 
1505184Sek110237 	if (pthread_create(&tid, NULL,
1515184Sek110237 	    (void *(*)(void*))eventgen_thread, 0) != 0) {
1525184Sek110237 		filebench_log(LOG_ERROR, "create timer thread failed: %s",
1535184Sek110237 		    strerror(errno));
1545184Sek110237 		filebench_shutdown(1);
1555184Sek110237 	}
1565184Sek110237 }
1575184Sek110237 
1585184Sek110237 /*
1595184Sek110237  * Puts the current event rate in the integer portion of the
1605184Sek110237  * supplied var_t. Returns a pointer to the var_t.
1615184Sek110237  */
1625184Sek110237 var_t *
eventgen_ratevar(var_t * var)1635184Sek110237 eventgen_ratevar(var_t *var)
1645184Sek110237 {
1657946SAndrew.W.Wilson@sun.com 	VAR_SET_INT(var, avd_get_int(filebench_shm->shm_eventgen_hz));
1665184Sek110237 	return (var);
1675184Sek110237 }
1685184Sek110237 
1695184Sek110237 /*
1705184Sek110237  * Sets the event generator rate to that supplied by
1717946SAndrew.W.Wilson@sun.com  * var_t *rate.
1725184Sek110237  */
1735184Sek110237 void
eventgen_setrate(avd_t rate)1747946SAndrew.W.Wilson@sun.com eventgen_setrate(avd_t rate)
1755184Sek110237 {
1767946SAndrew.W.Wilson@sun.com 	filebench_shm->shm_eventgen_hz = rate;
1777946SAndrew.W.Wilson@sun.com 	if (rate == NULL) {
1787946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_ERROR,
1797946SAndrew.W.Wilson@sun.com 		    "eventgen_setrate() called without a rate");
1807946SAndrew.W.Wilson@sun.com 		return;
1817946SAndrew.W.Wilson@sun.com 	}
1827946SAndrew.W.Wilson@sun.com 
1837946SAndrew.W.Wilson@sun.com 	if (AVD_IS_VAR(rate)) {
1847946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_VERBOSE,
1857946SAndrew.W.Wilson@sun.com 		    "Eventgen rate taken from variable");
1867946SAndrew.W.Wilson@sun.com 	} else {
1877946SAndrew.W.Wilson@sun.com 		filebench_log(LOG_VERBOSE, "Eventgen: %llu per second",
1887946SAndrew.W.Wilson@sun.com 		    (u_longlong_t)avd_get_int(rate));
1897946SAndrew.W.Wilson@sun.com 	}
1905184Sek110237 }
1915184Sek110237 
1925184Sek110237 /*
1937946SAndrew.W.Wilson@sun.com  * Clears the event queue so we have a clean start
1945184Sek110237  */
1955184Sek110237 void
eventgen_reset(void)1965184Sek110237 eventgen_reset(void)
1975184Sek110237 {
1986391Saw148015 	filebench_shm->shm_eventgen_q = 0;
1995184Sek110237 }
200