"Fossies" - the Fresh Open Source Software Archive 
Member "httperf-0.9.0/src/gen/rate.c" (7 Apr 2007, 5061 Bytes) of package /linux/www/old/httperf-0.9.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "rate.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 httperf -- a tool for measuring web server performance
3 Copyright 2000-2007 Hewlett-Packard Company and Contributors listed in
4 AUTHORS file. Originally contributed by David Mosberger-Tang
5
6 This file is part of httperf, a web server performance measurment
7 tool.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 In addition, as a special exception, the copyright holders give
15 permission to link the code of this work with the OpenSSL project's
16 "OpenSSL" library (or with modified versions of it that use the same
17 license as the "OpenSSL" library), and distribute linked combinations
18 including the two. You must obey the GNU General Public License in
19 all respects for all of the code used other than "OpenSSL". If you
20 modify this file, you may extend this exception to your version of the
21 file, but you are not obligated to do so. If you do not wish to do
22 so, delete this exception statement from your version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 02110-1301, USA
33 */
34
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include <httperf.h>
40 #include <event.h>
41 #include <object.h>
42 #include <rate.h>
43 #include <timer.h>
44
45 /* By pushing the random number generator state into the caller via
46 the xsubi array below, we gain some test repeatability. For
47 example, let us say one generator was starting sessions, and a
48 different generator was controlling requests within a session. If
49 both processes were sharing the same random number generator, then
50 a premature session termination would change the subsequent session
51 arrival spacing. */
52
53 static Time
54 next_arrival_time_det (Rate_Generator *rg)
55 {
56 return rg->rate->mean_iat;
57 }
58
59 static Time
60 next_arrival_time_uniform (Rate_Generator *rg)
61 {
62 Time lower, upper;
63
64 lower = rg->rate->min_iat;
65 upper = rg->rate->max_iat;
66
67 return lower + (upper - lower)*erand48 (rg->xsubi);
68 }
69
70 static Time
71 next_arrival_time_exp (Rate_Generator *rg)
72 {
73 Time mean = rg->rate->mean_iat;
74
75 return -mean*log (1.0 - erand48 (rg->xsubi));
76 }
77
78 static void
79 tick (Timer *t, Any_Type arg)
80 {
81 Time delay, now = timer_now ();
82 Rate_Generator *rg = arg.vp;
83
84 rg->timer = 0;
85 if (rg->done)
86 return;
87
88 while (now > rg->next_time)
89 {
90 delay = (*rg->next_interarrival_time) (rg);
91 if (verbose > 2)
92 fprintf (stderr, "next arrival delay = %.4f\n", delay);
93 rg->next_time += delay;
94 rg->done = ((*rg->tick) (rg->arg) < 0);
95 if (rg->done)
96 return;
97 }
98 rg->timer = timer_schedule ((Timer_Callback) tick, arg, rg->next_time - now);
99 }
100
101 static void
102 done (Event_Type type, Object *obj, Any_Type reg_arg, Any_Type call_arg)
103 {
104 Rate_Generator *rg = reg_arg.vp;
105
106 if (rg->done)
107 return;
108 rg->done = ((*rg->tick) (rg->arg) < 0);
109 }
110
111 void
112 rate_generator_start (Rate_Generator *rg, Event_Type completion_event)
113 {
114 Time (*func) (struct Rate_Generator *rg);
115 Any_Type arg;
116 Time delay;
117
118 /* Initialize random number generator with the init values here, all
119 rate generators (although independent) will follow the same
120 sequence of random values. We factor in the client's id to make
121 sure no two machines running httperf generate identical random
122 numbers. May want to pass these values as args to
123 rate_generator_start in the future. */
124 rg->xsubi[0] = 0x1234 ^ param.client.id;
125 rg->xsubi[1] = 0x5678 ^ (param.client.id << 8);
126 rg->xsubi[2] = 0x9abc ^ ~param.client.id;
127
128 arg.vp = rg;
129 if (rg->rate->rate_param > 0.0)
130 {
131 switch (rg->rate->dist)
132 {
133 case DETERMINISTIC: func = next_arrival_time_det; break;
134 case UNIFORM: func = next_arrival_time_uniform; break;
135 case EXPONENTIAL: func = next_arrival_time_exp; break;
136 default:
137 fprintf (stderr, "%s: unrecognized interarrival distribution %d\n",
138 prog_name, rg->rate->dist);
139 exit (-1);
140 }
141 rg->next_interarrival_time = func;
142 delay = (*func) (rg);
143 /* bias `next time' so that timeouts are rounded to the closest
144 tick: */
145 rg->next_time = timer_now () + delay;
146 rg->timer = timer_schedule ((Timer_Callback) tick, arg, delay);
147 }
148 else
149 /* generate callbacks sequentially: */
150 event_register_handler (completion_event, done, arg);
151
152 rg->start = timer_now ();
153 rg->done = ((*rg->tick) (rg->arg) < 0);
154 }
155
156 void
157 rate_generator_stop (Rate_Generator *rg)
158 {
159 if (rg->timer)
160 {
161 timer_cancel (rg->timer);
162 rg->timer = 0;
163 }
164 rg->done = 1;
165 }