-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader_hyper.bak
478 lines (385 loc) · 13 KB
/
loader_hyper.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* loader_hyper.c
*
* 2006-2007 Copyright (c)
* Michael Moser, <moser.michael@gmail.com>
* Robert Iakobashvili, <coroberti@gmail.com>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Cooked using CURL-project example hypev.c with thanks to the
* great CURL-project authors and contributors.
*/
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <event2/event.h>
#include <string.h>
#include "batch.h"
#include "client.h"
#include "loader.h"
#include "conf.h"
#include "cl_alloc.h"
#include "screen.h"
//#define TIMER_NEXT_LOAD 20000 /*0.02 second*/
#define TIMER_NEXT_DUMP_STATISTICS 3
#if 0
#define PRINTF(args...) fprintf(stdout, ## args);
#else
#define PRINTF
#endif
typedef struct sock_info
{
curl_socket_t sockfd;
int action; /*CURL_POLL_IN CURL_POLL_OUT */
long timeout;
struct event *ev;
int evset;
} sock_info;
int still_running=1;
static struct timeval timeout_next_dump_stats={TIMER_NEXT_DUMP_STATISTICS, 0};
/* Check for completed transfers, and remove their easy handles */
/*Read datas from socket. -Simon*/
static void check_multi_info(batch_context *bctx)
{
CURLMsg *msg;
int msgs_left;
CURL *handle;
int cycle_counter = 0;
int scheduled_now_count = 0, scheduled_now = 0;
client_context *cctx = NULL;
char *curlinfo = NULL;
int st=0;
unsigned long now_time=get_tick_count ();
while((msg = curl_multi_info_read (bctx->multiple_handle, &msgs_left)))
{
if (msg->msg == CURLMSG_DONE)
{
handle = msg->easy_handle;
curl_easy_getinfo (handle, CURLINFO_PRIVATE, &curlinfo);
cctx = (client_context *)curlinfo;
if (!cctx)
{
return;
}
if (msg->data.result)
{
cctx->client_state = CSTATE_ERROR;
}
if (! (++cycle_counter % TIME_RECALCULATION_MSG_NUM))
{
now_time = get_tick_count ();
}
load_next_step (cctx, now_time, &scheduled_now);
if (scheduled_now)
{
scheduled_now_count++;
}
if (msgs_left <= 0)
{
break;
}
cycle_counter++;
}
}
if (dispatch_expired_timers (bctx, now_time) > 0 || scheduled_now_count)
{
curl_multi_socket_action(bctx->multiple_handle, CURL_SOCKET_TIMEOUT, 0, &st);
}
#if 0
{
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 20000;
evtimer_add(bctx->timer_event, &timeout);
}
#endif
return;
}
/************************************************************************
* Function name - event_cb_hyper
*
* Description - A libevent callback. Called by libevent when we get action on a socket.
* Input - fd - descriptor (socket)
* kind - a bitmask of events from libevent
* *userp - user pointer, we pass pointer to batch-context structure
* Return Code/Output - None
*************************************************************************/
static void event_sock_rw_cb (int fd, short kind, void *userp)
{
batch_context *bctx = (batch_context *) userp;
int still_running_num;
int bitset = 0;
if (kind & EV_READ)
{
bitset |= CURL_CSELECT_IN;
}
if (kind & EV_WRITE)
{
bitset |= CURL_CSELECT_OUT;
}
curl_multi_socket_action (bctx->multiple_handle, fd, bitset, &still_running_num);
//PRINTF("------Enter %s still_running_num=%d.\n", __FUNCTION__, still_running_num);
check_multi_info(bctx);
return;
}
/************************************************************************
* Function name - timer_cb_hyper
*
* Description - A libevent callback. Called by libevent when timeout expires
* Input - fd - descriptor (socket)
* kind - a bitmask of events from libevent
* *userp - user pointer, we pass pointer to batch-context structure
* Return Code/Output - None
*************************************************************************/
static void event_timer_cb (int fd, short kind, void *userp)
{
(void)fd;
(void)kind;
batch_context *bctx = (batch_context *)userp;
int st;
/*在CURL_SOCKET_TIMEOUT时间内调用socket_callback() */
/*Call curl_multi_socket_action(..., CURL_SOCKET_TIMEOUT, 0, ...) to kickstart everything. */
curl_multi_socket_action(bctx->multiple_handle, CURL_SOCKET_TIMEOUT, 0, &st);
//PRINTF("------Enter %s still_running_num=%d.\n", __FUNCTION__, st);
check_multi_info(bctx);
return;
}
/************************************************************************
* Function name - remsock
*
* Description - Clean up the sock_info structure
* Input - *sinfo - pointer to sinfo structure
* Return Code/Output - None
*************************************************************************/
static void remsock(sock_info *sinfo)
{
if (!sinfo)
{
return;
}
if (sinfo->evset)
{
event_free(sinfo->ev);
}
sinfo->evset = 0;
}
/************************************************************************
* Function name - setsock_hyper
*
* Description - Assign information to a sock_info structure
* Input - *sinfo - pointer to sinfo structure to fill
* socket - socket descriptor
* *handle - pointer to CURL library handle
* action - bitmask of events from curl library
* *bctx - pointer to batch context
* Return Code/Output - None
*************************************************************************/
static void setsock_hyper(sock_info*sinfo, curl_socket_t socket, CURL* handle, int action, batch_context* bctx)
{
int kind = (action & CURL_POLL_IN ? EV_READ : 0)|(action & CURL_POLL_OUT ? EV_WRITE : 0)|EV_PERSIST;
(void) handle;
sinfo->sockfd = socket;
sinfo->action = action;
if (sinfo->evset)
{
event_free(sinfo->ev);
}
//event_set( &sinfo->ev, sinfo->sockfd, kind, event_cb_hyper, bctx);
//event_base_set(bctx->eb, &sinfo->ev);
sinfo->ev = event_new(bctx->eb, sinfo->sockfd, kind, event_sock_rw_cb, bctx);
sinfo->evset=1;
event_add(sinfo->ev, NULL);
}
/************************************************************************
* Function name - addsock_hyper
*
* Description - Initialize a new sock_info structure
*
* Input - socket - socket descriptor
* *handle - pointer to CURL library handle
* action - bitmask of events from curl library
* *cctx - pointer to client context
* *sinfo - pointer to sinfo structure to fill
* *bctx - pointer to batch context
* Return Code/Output - None
*************************************************************************/
static void addsock_hyper(curl_socket_t socket, CURL *handle, int action, client_context *cctx, sock_info *sinfo, batch_context *bctx)
{
setsock_hyper (sinfo, socket, handle, action, bctx);
curl_multi_assign (bctx->multiple_handle, socket, cctx);
return;
}
/************************************************************************
* Function name - socket_callback
*
* Description - A libcurl socket callback. Called by libcurl, when there is an
* event on socket. Unfortunately, it is called only for the trasfer stage.
*
* Input - *handle - pointer to CURL handle
* socket - socket descriptor
* what - libcurl event bitmask
* *cbp - libcurl callback pointer; we pass batch context here
* *sockp - pointer to the handle user-assigned private data, here client-context
* Return Code/Output - On Success - 0, on Error -1
*************************************************************************/
static int multi_socket_cb (CURL *handle, curl_socket_t socket, int what, void *cbp, void *sockp)
{
batch_context* bctx = (batch_context *) cbp;
client_context* cctx = (client_context *) sockp;
sock_info *sinfo = (sock_info*) cctx ? cctx->ext_data : 0;
char *curlinfo = NULL;
//PRINTF("------Enter %s.\n", __FUNCTION__);
if (!cctx)
{
curl_easy_getinfo (handle, CURLINFO_PRIVATE, &curlinfo);
cctx = (client_context *)curlinfo;
if (cctx)
{
sinfo = (sock_info*)cctx->ext_data;
}
}
if (!cctx || !sinfo)
{
return 0;
}
if (what == CURL_POLL_REMOVE)
{
remsock(sinfo);
}
else
{
if (!sinfo->evset)
{
//PRINTF("Adding data: %s%s\n", what & CURL_POLL_IN ? "READ":"", what & CURL_POLL_OUT ? "WRITE":"" );
addsock_hyper (socket, handle, what, cctx, sinfo, bctx);
}
else
{
//PRINTF("Changing action from %d to %d\n", sinfo->action, what);
setsock_hyper(sinfo, socket, handle, what, bctx);
}
}
return 0;
}
/* Update the event timer after curl_multi library calls */
static int multi_timer_cb(CURLM *multi, long timeout_ms, batch_context *bctx)
{
struct timeval timeout;
(void)multi; /* unused */
timeout.tv_sec = timeout_ms/1000;
timeout.tv_usec = (timeout_ms%1000)*1000;
//PRINTF("------Enter %s timeout=%ld.\n", __FUNCTION__, timeout_ms);
evtimer_add(bctx->timer_event, &timeout);
return 0;
}
static int on_exit_hyper (batch_context* bctx)
{
still_running =0;
dump_final_statistics (bctx->cctx_array);
screen_release ();
if (bctx->waiting_queue)
{
/* Cancel periodic timers */
cancel_periodic_timers (bctx);
tq_release (bctx->waiting_queue);
free (bctx->waiting_queue);
bctx->waiting_queue = 0;
}
exit (0);
}
static void dump_statistics_timer_cb (int fd, short kind, void *userp)
{
(void)fd;
(void)kind;
batch_context *bctx = (batch_context *)userp;
unsigned long now_time = get_tick_count();
if (pending_active_and_waiting_clients_num (bctx) == 0 && bctx->do_client_num_gradual_increase == 0)
{
on_exit_hyper (bctx);
return ;
}
if (is_batch_group_leader (bctx))
{
dump_snapshot_interval (bctx, now_time);
}
evtimer_add(bctx->timer_next_dump_statistics, &timeout_next_dump_stats);
return;
}
/****************************************************************************************
* Function name - user_activity_hyper
*
* Description - Simulates user-activities using HYPER mode
* Input - *cctx_array - array of client contexts (related to a certain batch of clients)
*
* Return Code/Output - On Success - 0, on Error -1
****************************************************************************************/
/****************************************************************************************
1. init_timers_and_add_initial_clients_to_load()添加clients, 并调用curl_multi_add_handle ();
2. multi_timer_cb(), 增加timer_event事件。(由于执行curl_multi_add_handle ()会改变timeout,所以触发multi_timer_cb())
3. timer_cb_hyper(), 此函数会perform the easy handle, 并且在timeout时间内调用socket_callback()
4. socket_callback() add socket事件,并设定回调函数为event_cb_hyper()
5. event_cb_hyper()读取socket数据
****************************************************************************************/
int user_activity_hyper (client_context* cctx_array)
{
batch_context* bctx = cctx_array->bctx;
sock_info *sinfo;
int k;
unsigned long now_time=0;
if (!bctx)
{
fprintf (stderr, "%s - error: bctx is a NULL pointer.\n", __func__);
return -1;
}
/* Init libevent library */
bctx->eb = event_base_new();
bctx->timer_event = evtimer_new(bctx->eb, event_timer_cb, bctx);
bctx->timer_next_dump_statistics = evtimer_new(bctx->eb, dump_statistics_timer_cb, bctx);
evtimer_add(bctx->timer_next_dump_statistics, &timeout_next_dump_stats);
/* Set the socket callback on multi-handle */
/*执行curl_multi_socket_action()时调用此函数。*/
curl_multi_setopt (bctx->multiple_handle, CURLMOPT_SOCKETFUNCTION, multi_socket_cb);
curl_multi_setopt (bctx->multiple_handle, CURLMOPT_SOCKETDATA, bctx);
/*timeout值改变时调用此函数。执行curl_multi_add_handle ()时timeout值会改变。*/
curl_multi_setopt(bctx->multiple_handle, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
curl_multi_setopt(bctx->multiple_handle, CURLMOPT_TIMERDATA, bctx);
for (k = 0 ; k < bctx->client_num_max ; k++)
{
sinfo = cl_calloc (1, sizeof (sock_info));
if (!sinfo)
{
fprintf (stderr, "%s - error: allocation of sock_info failed.\n", __func__);
return -1;
}
bctx->cctx_array[k].ext_data = sinfo;
}
if (alloc_init_timer_waiting_queue (bctx->client_num_max + PERIODIC_TIMERS_NUMBER + 1, &bctx->waiting_queue) == -1)
{
fprintf (stderr, "%s - error: failed to alloc or init timer waiting queue.\n", __func__);
return -1;
}
now_time = get_tick_count ();
if (init_timers_and_add_initial_clients_to_load (bctx,now_time) == -1)
{
fprintf (stderr, "%s - error: init_timers_and_add_initial_clients_to_load () failed.\n", __func__);
return -1;
}
event_base_dispatch(bctx->eb);
on_exit_hyper (bctx);
return 0;
}