xref: /onnv-gate/usr/src/cmd/filebench/common/eventgen.c (revision 6391:f317d2de8920)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * The event generator in this module is the producer half of a
30  * metering system which blocks flows using consumer routines in the
31  * flowop_library.c module. Four routines in that module can limit rates
32  * by event rate (flowoplib_eventlimit), by I/O operations rate
33  * (flowoplib_iopslimit()), by operations rate (flowoplib_opslimit),
34  * or by I/O bandwidth limit (flowoplib_bwlimit). By setting appropriate
35  * event generation rates, required calls per second, I/O ops per second,
36  * file system ops per second, or I/O bandwidth per second limits can
37  * be set. Note, the generated events are shared with all consumer
38  * flowops, of which their will be one for each process / thread
39  * instance which has a consumer flowop defined in it.
40  */
41 
42 #include <sys/time.h>
43 #include "vars.h"
44 #include "eventgen.h"
45 #include "filebench.h"
46 #include "flowop.h"
47 #include "ipc.h"
48 
49 /*
50  * Prints "how to use" information for the eventgen module
51  */
52 void
53 eventgen_usage(void)
54 {
55 	(void) fprintf(stderr, "eventgen rate=<rate>\n");
56 	(void) fprintf(stderr, "\n");
57 }
58 
59 /*
60  * The producer side of the event system.
61  * Once eventgen_hz has been set by eventgen_setrate(),
62  * the routine sends eventgen_hz events per second until
63  * the program terminates. Events are posted by incrementing
64  * filebench_shm->shm_eventgen_q by the number of generated
65  * events then signalling the condition variable
66  * filebench_shm->shm_eventgen_cv to indicate to event consumers
67  * that more events are available.
68  *
69  * Eventgen_thread attempts to sleep for 10 event periods,
70  * then, once awakened, determines how many periods actually
71  * passed since sleeping, and issues a set of events equal
72  * to the number of periods that it slept, thus keeping the
73  * average rate at the requested rate.
74  */
75 static void
76 eventgen_thread(void)
77 {
78 	hrtime_t last;
79 
80 	last = gethrtime();
81 
82 	/* CONSTCOND */
83 	while (1) {
84 		struct timespec sleeptime;
85 		hrtime_t delta;
86 		int count;
87 
88 		if (filebench_shm->shm_eventgen_hz == 0) {
89 			(void) sleep(1);
90 			continue;
91 		}
92 		/* Sleep for 10xperiod */
93 		sleeptime.tv_sec = 0;
94 		sleeptime.tv_nsec = FB_SEC2NSEC /
95 		    filebench_shm->shm_eventgen_hz;
96 
97 		sleeptime.tv_nsec *= 10;
98 		if (sleeptime.tv_nsec < 1000UL)
99 			sleeptime.tv_nsec = 1000UL;
100 
101 		sleeptime.tv_sec = sleeptime.tv_nsec / FB_SEC2NSEC;
102 		if (sleeptime.tv_sec > 0)
103 			sleeptime.tv_nsec -= (sleeptime.tv_sec * FB_SEC2NSEC);
104 
105 		(void) nanosleep(&sleeptime, NULL);
106 		delta = gethrtime() - last;
107 		last = gethrtime();
108 		count = (filebench_shm->shm_eventgen_hz * delta) / FB_SEC2NSEC;
109 
110 		filebench_log(LOG_DEBUG_SCRIPT,
111 		    "delta %llums count %d",
112 		    (u_longlong_t)(delta / 1000000), count);
113 
114 		/* Send 'count' events */
115 		(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
116 		/* Keep the producer with a max of 5 second depth */
117 		if (filebench_shm->shm_eventgen_q <
118 		    (5 * filebench_shm->shm_eventgen_hz))
119 			filebench_shm->shm_eventgen_q += count;
120 
121 		(void) pthread_cond_signal(&filebench_shm->shm_eventgen_cv);
122 
123 		(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
124 	}
125 }
126 
127 /*
128  * Creates a thread to run the event generator eventgen_thread
129  * routine. Shuts down filebench if the eventgen thread cannot
130  * be created.
131  */
132 void
133 eventgen_init(void)
134 {
135 	/*
136 	 * Linux does not like it if the first
137 	 * argument to pthread_create is null. It actually
138 	 * segv's. -neel
139 	 */
140 	pthread_t tid;
141 
142 	if (pthread_create(&tid, NULL,
143 	    (void *(*)(void*))eventgen_thread, 0) != 0) {
144 		filebench_log(LOG_ERROR, "create timer thread failed: %s",
145 		    strerror(errno));
146 		filebench_shutdown(1);
147 	}
148 }
149 
150 /*
151  * Puts the current event rate in the integer portion of the
152  * supplied var_t. Returns a pointer to the var_t.
153  */
154 var_t *
155 eventgen_ratevar(var_t *var)
156 {
157 	VAR_SET_INT(var, filebench_shm->shm_eventgen_hz);
158 	return (var);
159 }
160 
161 /*
162  * Sets the event generator rate to that supplied by
163  * fbint_t rate.
164  */
165 void
166 eventgen_setrate(fbint_t rate)
167 {
168 	filebench_shm->shm_eventgen_hz = (int)rate;
169 }
170 
171 /*
172  * Turns off the event generator by setting the rate to zero
173  */
174 void
175 eventgen_reset(void)
176 {
177 	filebench_shm->shm_eventgen_q = 0;
178 }
179