-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathloader_fsm.c
1517 lines (1359 loc) · 50.7 KB
/
loader_fsm.c
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* loader_fsm.c
*
* 2007 Copyright (c)
* Robert Iakobashvili, <[email protected]>
* 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
*/
// must be the first include
#include "fdsetsize.h"
#include <stdlib.h>
#include <errno.h>
#include "client.h"
#include "loader.h"
#include "batch.h"
#include "conf.h"
#include "heap.h"
#include "screen.h"
#include "cl_alloc.h"
/*
Number of request rate timer invocations per second used to
spread the load within each second. Should evenly divide 1000 msec.
*/
static const int req_rate_timer_invs_per_sec = 5;
/*
Empirical fudge value to reduce request rate timer interval after
dividing 1000 msec into req_rate_timer_invs_per_sec invocations
so that the overhead of multiple timer invocations is taken into
account and the sum total of these invocations stays around 1000 msec.
*/
static const int req_rate_timer_fudge = 20;
static int load_error_state (client_context* cctx, unsigned long now_time,
unsigned long *wait_msec);
static int load_init_state (client_context* cctx, unsigned long now_time,
unsigned long *wait_msec);
static int load_urls_state (client_context* cctx, unsigned long now_time,
unsigned long *wait_msec);
static int load_final_ok_state (client_context* cctx, unsigned long now_time,
unsigned long *wait_msec);
/*
Table of loading functions in order to call an appropiate for
a certain state loading function just by using the state number
as an index. As we are starting our states from (-1),
the actual call will be with (state + 1) as an index, used
in load_next_step ().
*/
const load_state_func load_state_func_table [] =
{
load_error_state,
load_init_state,
load_urls_state,
load_final_ok_state,
};
static int pick_up_next_url (client_context* cctx);
static int fetching_first_cycling_url (client_context* cctx);
static void advance_cycle_num (client_context* cctx);
static int setup_url (client_context* cctx);
static int handle_screen_input_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param);
static int handle_logfile_rewinding_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param);
static int
handle_gradual_increase_clients_num_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param);
static int handle_cctx_sleeping_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param);
static int handle_cctx_url_completion_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param);
static int handle_req_rate_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param);
static int client_remove_from_load (batch_context* bctx,
client_context* cctx);
static int client_add_to_load (batch_context* bctx,
client_context* cctx,
unsigned long now_time);
static int fetching_decision (client_context* cctx, url_context* url);
static int orderly_sched_clients (batch_context* bctx, int clients_to_sched);
static int req_rate_sched_clients (batch_context* bctx);
static int get_free_client (batch_context* bctx, client_context **pcctx);
/*****************************************************************************
* Function name - alloc_init_timer_waiting_queue
*
* Description - Allocates and initializes timer waiting queue
*
* Input size - maximum possible size of the queue
* Input/Output **wq - waiting queue to be allocated, initialized and
* returned back
*
G * Return Code/Output - On success -0, on error -1
******************************************************************************/
int alloc_init_timer_waiting_queue (size_t size, timer_queue** wq)
{
timer_queue* tq = NULL;
*wq = NULL;
if (! (tq = cl_calloc (1, sizeof (heap))))
{
fprintf (stderr, "%s - error: failed to allocate queue.\n", __func__);
return -1;
}
if (tq_init (tq,
size, /* tq size */
10, /* tq increase step; 0 - means don't increase */
size /* number of nodes to prealloc */
) == -1)
{
fprintf (stderr, "%s - error: failed to initialize waiting queue.\n",
__func__);
free (tq);
return -1;
}
*wq = tq;
return 0;
}
/*****************************************************************************
* Function name - init_timers_and_add_initial_clients_to_load
*
* Description - Really inits timers and adds initial clients to load
*
*Input *bctx - pointer to a batch context
* now-time - current timestamp
* Return Code/Output - On success -0, on error -1
******************************************************************************/
int init_timers_and_add_initial_clients_to_load (batch_context* bctx,
unsigned long now_time)
{
//client_context* cctx = bctx->cctx_array;
/*
Init logfile rewinding timer and schedule it.
*/
const unsigned long logfile_timer_msec =
1000*LOGFILE_TEST_TIMER_PERIOD;
bctx->logfile_timer_node.next_timer = now_time + logfile_timer_msec;
bctx->logfile_timer_node.period = logfile_timer_msec;
bctx->logfile_timer_node.func_timer = handle_logfile_rewinding_timer;
if (tq_schedule_timer (bctx->waiting_queue,
&bctx->logfile_timer_node) == -1)
{
fprintf (stderr, "%s - error: tq_schedule_timer () failed.\n", __func__);
return -1;
}
#if 0
else
{
fprintf (cctx->file_output, "SCHED: %s - logfile_timer_id is %ld.\n",
__func__, logfile_timer_id);
}
#endif
/*
Init screen input testing timer and schedule it.
*/
bctx->screen_input_timer_node.next_timer = now_time + 3000;
bctx->screen_input_timer_node.period = 1000;
bctx->screen_input_timer_node.func_timer = handle_screen_input_timer;
if (tq_schedule_timer (bctx->waiting_queue, &bctx->screen_input_timer_node)== -1)
{
fprintf (stderr, "%s - error: tq_schedule_timer () failed.\n", __func__);
return -1;
}
#if 0
else
{
fprintf (cctx->file_output, "SCHED: %s - screen_input_timer_id is %ld.\n",
__func__, screen_input_timer_id);
}
#endif
bctx->start_time = bctx->last_measure = now_time;
bctx->active_clients_count = bctx->sleeping_clients_count =0;
if (add_loading_clients (bctx) == -1)
{
fprintf (stderr, "%s error: add_loading_clients () failed.\n", __func__);
return -1;
}
if (bctx->do_client_num_gradual_increase)
{
/*
Schedule the gradual loading clients increase timer.
*/
bctx->clients_num_inc_timer_node.next_timer = now_time + 1000;
bctx->clients_num_inc_timer_node.period = 1000;
bctx->clients_num_inc_timer_node.func_timer =
handle_gradual_increase_clients_num_timer;
if (tq_schedule_timer (bctx->waiting_queue,
&bctx->clients_num_inc_timer_node) == -1)
{
fprintf (stderr, "%s - error: tq_schedule_timer () failed.\n", __func__);
return -1;
}
#if 0
else
{
fprintf (cctx->file_output, "SCHED: %s - clients_num_inc_id is %ld.\n",
__func__, clients_num_inc_id);
}
#endif
}
if (bctx->req_rate)
{
/*
Schedule fixied request rate timer.
*/
bctx->req_rate_timer_node.next_timer = now_time + 1000;
bctx->req_rate_timer_node.period = 1000/req_rate_timer_invs_per_sec -
req_rate_timer_fudge;
bctx->req_rate_timer_node.func_timer = handle_req_rate_timer;
if (tq_schedule_timer (bctx->waiting_queue,
&bctx->req_rate_timer_node) == -1)
{
fprintf (stderr, "%s - error: tq_schedule_timer () failed.\n",
__func__);
return -1;
}
}
return 0;
}
/**************************************************************************
* Function name - cancel_periodic_timers
*
* Description - Cancels scheduled periodic timers
*
* Input *bctx - pointer to batch context
* Return Code/Output - On success -0, on error -1
***************************************************************************/
int cancel_periodic_timers (batch_context* bctx)
{
if (bctx->logfile_timer_node.timer_id != -1)
{
tq_cancel_timer (bctx->waiting_queue,
bctx->logfile_timer_node.timer_id);
bctx->logfile_timer_node.timer_id = -1;
}
if (bctx->clients_num_inc_timer_node.timer_id != -1)
{
tq_cancel_timer (bctx->waiting_queue,
bctx->clients_num_inc_timer_node.timer_id);
bctx->clients_num_inc_timer_node.timer_id = -1;
}
if (bctx->screen_input_timer_node.timer_id != -1)
{
tq_cancel_timer (bctx->waiting_queue,
bctx->screen_input_timer_node.timer_id);
bctx->screen_input_timer_node.timer_id = -1;
}
if (bctx->req_rate_timer_node.timer_id != -1)
{
tq_cancel_timer (bctx->waiting_queue,
bctx->req_rate_timer_node.timer_id);
bctx->req_rate_timer_node.timer_id = -1;
}
return 0;
}
/*****************************************************************************
* Function name - load_next_step
*
* Description - Called at initialization and further after url-fetch completion
* indication (that may be an error status as well). Either sets
* to a client the next url to load, or marks the client being at a
* completion state: CSTATE_ERROR or CSTATE_FINISHED_OK.
*
* Input - *cctx - pointer to the client context
* now_time - current timestamp in msec
* Input/Output *sched_now - pointer to an int, when the value of *sched_now is
* true, the client is scheduled right now without timer queue.
* Return Code/Output - CSTATE enumeration with the state of loading
******************************************************************************/
int load_next_step (client_context* cctx,
unsigned long now_time,
int* sched_now)
{
batch_context* bctx = cctx->bctx;
int rval_load = CSTATE_ERROR;
unsigned long interleave_waiting_time = 0;
*sched_now = 0;
/*
Cancel the url completion timer, if it was scheduled.
*/
if (cctx->tid_url_completion != -1)
{
#if 0
if (cctx->tid_url_completion == clients_num_inc_id)
{
fprintf (cctx->file_output,
"SCHED: %s - cctx->tid_url_completion == clients_num_inc_id.\n",
__func__);
}
#endif
tq_cancel_timer (bctx->waiting_queue, cctx->tid_url_completion);
cctx->tid_url_completion = -1;
}
/* Remove handle from the multiple handle, if it was added there before. */
if (cctx->client_state != CSTATE_INIT)
{
if (client_remove_from_load (bctx, cctx) == -1)
{
fprintf (stderr, "%s - client_remove_from_load () failed.\n", __func__);
return -1;
}
}
/*
When load_error_state () gets client (in CSTATE_ERROR) and
<recoverable_error_state> is true (the default), it recovers the
client and sets the first cycling state to it. However, operational
statistics should record it as a failed operation in op_stat_update.
Therefore, remembering here possible error state.
*/
int recoverable_error_state = cctx->client_state;
if (bctx->run_time && (now_time - bctx->start_time >= bctx->run_time))
{
rval_load = CSTATE_FINISHED_OK;
bctx->requests_completed = 1;
}
else
/*
Initialize virtual client's CURL handle for the next step of loading by calling
load_<state-name>_state() function relevant for a particular client state.
*/
rval_load = load_state_func_table[cctx->client_state+1] (cctx,
now_time,
&interleave_waiting_time);
/*
Update operational statistics
*/
op_stat_update (&bctx->op_delta,
(recoverable_error_state == CSTATE_ERROR) ?
recoverable_error_state : rval_load,
cctx->preload_state,
cctx->url_curr_index,
cctx->preload_url_curr_index);
if (fetching_first_cycling_url (cctx))
{
/* Update CAPS numbers */
op_stat_call_init_count_inc (&bctx->op_delta);
}
/*
Coming to the error or the finished states, just return without more
scheduling the client any more.
*/
if (rval_load == CSTATE_ERROR || rval_load == CSTATE_FINISHED_OK)
{
/*
GF
At this point this client is finished, and there are no more URLs to fetch.
But the client may still be holding a connection open, which may prevent other
clients in the batch from connecting to the server. If we wait until the end-of-batch
general cleanup to close connections, then these other clients may never connect, and
the only way the batch will end is to have all these waiting clients time out. So we
should close out this client's connections here. Setting client->handle = 0 will prevent
curl_easy_cleanup from being called again in the general cleanup.
*/
// cleanup of each handle is a hint to multi-handle to
// decrease the num of connections to server
if (cctx->handle)
{
curl_easy_cleanup (cctx->handle);
cctx->handle = 0;
}
if (rval_load == CSTATE_ERROR)
{
// Re-init clients in CSTATE_ERROR state to enable their optional
// scheduling
cctx->handle = curl_easy_init ();
}
return rval_load;
}
/*
Schedule virtual clients by adding them to multi-handle,
if the clients are not in error or finished final states.
*/
if (!interleave_waiting_time)
{
/* Schedule the client immediately */
if (client_add_to_load (bctx, cctx, now_time) == -1)
{
fprintf (stderr, "%s - error: client_add_to_load () failed .\n",
__func__);
return -1;
}
else
{
*sched_now = 1;
}
}
else
{
//PRINTF("load_next_step: ctx %p schedule next load in %d seconds\n",
// cctx,(int) interleave_waiting_time/1000);
/*
Postpone client scheduling for the interleave_waiting_time msec by
placing it to the timer queue. Schedule the timer now.
*/
cctx->tn.next_timer = now_time + interleave_waiting_time;
cctx->tn.period = 0;
cctx->tn.func_timer = handle_cctx_sleeping_timer;
if ((cctx->tid_sleeping = tq_schedule_timer (bctx->waiting_queue,
(struct timer_node *) cctx)) == -1)
{
fprintf (stderr, "%s - error: tq_schedule_timer () failed.\n", __func__);
return -1;
}
else
{
#if 0
fprintf (cctx->file_output, "SCHED: %s - cctx->tid_sleeping is %ld.\n",
__func__, cctx->tid_sleeping);
#endif
bctx->sleeping_clients_count++;
}
#if 0
fprintf (stderr, "%s - scheduled client to wq with wtime %ld\n",
__func__, interleave_waiting_time);
#endif
}
return rval_load;
}
/******************************************************************************
* Function name - add_loading_clients
*
* Description - Initialization of our virtual clients (CURL handles)
* setting first url to fetch and scheduling them according to
* clients increment for gradual loading.
*
* Input - *bctx - pointer to the batch of contexts
* Return Code/Output - On Success - 0, on error or request to unreg timer - (-1)
*******************************************************************************/
int add_loading_clients (batch_context* bctx)
{
//client_context* cctx = bctx->cctx_array;
long clients_to_sched = 0;
/*
Return, if initial gradual scheduling of all new clients has been stopped
*/
if (bctx->stop_client_num_gradual_increase)
{
#if 0
fprintf (cctx->file_output,
"SCHED: %s - returning on zero >stop_client_num_gradual_increase.\n",
__func__);
#endif
return 0; // Returning 0 means do not stop the timer
}
/*
Return, if initial gradual scheduling of all new clients has been accomplished.
*/
if (bctx->client_num_max <= bctx->clients_current_sched_num)
{
bctx->do_client_num_gradual_increase = 0;
#if 0
fprintf (cctx->file_output,
"SCHED: do_client_num_gradual_increase = 0 on client_num_max %d"
" and clients_current_sched_num %d \n",
bctx->client_num_max, bctx->clients_current_sched_num);
#endif
return -1; // Returning (-1) means - stop the timer
}
/* Calculate number of the new clients to schedule. */
if (!bctx->clients_current_sched_num && bctx->client_num_start)
{
/* first time scheduling - zero bctx->clients_current_sched_num */
clients_to_sched = bctx->client_num_start;
}
else
{
clients_to_sched = bctx->clients_rampup_inc ?
min (bctx->clients_rampup_inc, bctx->client_num_max -
bctx->clients_current_sched_num) : bctx->client_num_max;
}
//fprintf (stderr, "%s - adding %ld clients.\n", __func__, clients_to_sched);
#if 0
fprintf (cctx->file_output,
"SCHED: clients_to_sched %ld, bctx->clients_current_sched_num %d.\n",
clients_to_sched, bctx->clients_current_sched_num);
#endif
/*
Schedule new clients by initializing their CURL handle with
URL, etc. parameters and adding it to MCURL multi-handle.
Defer activation to timer if fixed request rate is specified.
*/
if (!bctx->req_rate)
{
if (orderly_sched_clients (bctx, clients_to_sched) < 0)
return -1;
}
/*
Re-calculate assisting counters and enable do_client_num_gradual_increase
flag, if required.
*/
bctx->clients_current_sched_num += clients_to_sched;
if (bctx->clients_rampup_inc)
{
if (bctx->clients_current_sched_num < bctx->client_num_max)
{
bctx->do_client_num_gradual_increase = 1;
}
}
return 0;
}
/*******************************************************************************
* Function name - add_loading_clients_num
*
* Description - Adding a number of clients to load
*
* Input - *bctx - pointer to the batch of contexts
* add_number - number of clients to add to load
* Return Code/Output - On Success - 0, on error (-1)
*******************************************************************************/
int add_loading_clients_num (batch_context* bctx, int add_number)
{
if (add_number <= 0)
{
return -1;
}
if (bctx->client_num_max <= bctx->clients_current_sched_num)
{
return -1; // No room to add more
}
/* Calculate number of the new clients to schedule. */
const long clients_to_sched = min (add_number,
bctx->client_num_max -
bctx->clients_current_sched_num);
//fprintf (stderr, "%s - adding %ld clients.\n", __func__, clients_to_sched);
/*
Schedule new clients by initializing their CURL handle with
URL, etc. parameters and adding it to MCURL multi-handle.
Defer activation to timer if fixed request rate is specified.
*/
if (!bctx->req_rate)
{
if (orderly_sched_clients (bctx, clients_to_sched) < 0)
return -1;
}
bctx->clients_current_sched_num += clients_to_sched;
return 0;
}
/*******************************************************************************
* Function name - dispatch_expired_timers
*
* Description - Fetches from the waiting timer queue timers and dispatches them
* by calling timer-node specific handle_timeout () method. Among
* other expired timers dispatches waiting clients (kept in
* timer-queue to respect url interleave timeouts), where
* func_timer () function of client timer-node adds the clients
* to our loading machinery.
*
* Input - *bctx - pointer to the batch of contexts;
* now_time - current time passed in msec
* Return Code/Output - On Success - 0 or positive number eq to the num of
* scheduled timers,
* on Error -1
*******************************************************************************/
int
dispatch_expired_timers (batch_context* bctx, unsigned long now_time)
{
timer_queue* tq = bctx->waiting_queue;
int count =0;
if (!tq)
return -1;
if (tq_empty (tq))
return 0;
while (! tq_empty (tq))
{
unsigned long time_nearest = tq_time_to_nearest_timer (tq);
if (time_nearest <= now_time)
{
if (tq_dispatch_nearest_timer (tq, bctx, now_time) == -1)
{
// fprintf (stderr, "%s - error: tq_dispatch_nearest_timer () failed "
// "or handle_timer () returns (-1).\n", __func__);
return -1;
}
else
{
count++;
}
}
else
break;
}
return count;
}
/******************************************************************************
* Function name - client_add_to_load
*
* Description - Adds client context to the batch context multiple handle
* for loading
*
* Input - *bctx - pointer to the batch context
* *cctx - pointer to the client context
* now_time - current time in msec
* Return Code/Output - On success -0, on error - (-1)
*******************************************************************************/
static int client_add_to_load (batch_context* bctx,
client_context* cctx,
unsigned long now_time)
{
/* Remember the previous state and url index: fur operational statistics */
cctx->preload_state = cctx->client_state;
cctx->preload_url_curr_index = cctx->url_curr_index;
/* Schedule the client immediately */
cctx->req_sent_timestamp = now_time;
if (curl_multi_add_handle (bctx->multiple_handle, cctx->handle) == CURLM_OK)
{
unsigned long timer_url_completion = 0;
if (current_url_completion_timeout (&timer_url_completion,
&bctx->url_ctx_array[cctx->url_curr_index],
now_time) == -1)
{
fprintf (stderr,
"%s - error: current_url_completion_timeout () failed.\n",
__func__);
return -1;
}
if (timer_url_completion)
{
cctx->tn.next_timer = now_time + timer_url_completion;
cctx->tn.period = 0;
cctx->tn.func_timer = handle_cctx_url_completion_timer;
if ((cctx->tid_url_completion = tq_schedule_timer (bctx->waiting_queue,
(struct timer_node *) cctx)) == -1)
{
fprintf (stderr,
"%s - error: tq_schedule_timer () failed for url-completion.\n",
__func__);
return -1;
}
#if 0
else
{
fprintf (cctx->file_output,
"SCHED: %s - cctx->tid_url_completion is %ld.\n",
__func__, cctx->tid_url_completion);
}
#endif
}
bctx->active_clients_count++;
// fprintf (stderr, "%s - client added.\n", __func__);
}
else
{
fprintf (stderr, "%s - curl_multi_add_handle () failed.\n", __func__);
return -1;
}
return 0;
}
/******************************************************************************
* Function name - client_remove_from_load
*
* Description - Removes client context to from the kept in batch context
* multiple handle, thus, removing the client from the loading
* machinery
*
* Input - *bctx - pointer to the batch context
* *cctx - pointer to the client context
* Return Code/Output - On success -0, on error - (-1)
*****************************************************************************/
static int client_remove_from_load (batch_context* bctx, client_context* cctx)
{
if (curl_multi_remove_handle (bctx->multiple_handle, cctx->handle) == CURLM_OK)
{
if (bctx->active_clients_count > 0)
{
bctx->active_clients_count--;
}
//fprintf (stderr, "%s - client removed.\n", __func__);
}
else
{
fprintf (stderr, "%s - curl_multi_remove_handle () failed.\n", __func__);
return -1;
}
return 0;
}
/******************************************************************************
* Function name - handle_gradual_increase_clients_num_timer
*
* Description - Handling of one second timer to increase gradually number of
* loading clients.
*
* Input - *timer_node - pointer to timer_node structure
* *pvoid_param - pointer to some extra data; here batch context
* *ulong_param - some extra data.
* Return Code/Output - On success -0, on error - (-1)
******************************************************************************/
static int handle_gradual_increase_clients_num_timer (timer_node* timer_node,
void* pvoid_param,
unsigned long ulong_param)
{
batch_context* bctx = (batch_context *) pvoid_param;
(void) timer_node;
(void) ulong_param;
//client_context* cctx = bctx->cctx_array;
//fprintf (cctx->file_output, "SCHED: %s - entered.\n", __func__);
if (add_loading_clients (bctx) == -1)
{
//fprintf (stderr, "%s add_loading_clients () returns -1.\n", __func__);
#if 0
fprintf (cctx->file_output,
"SCHED: %s - add_loading_clients failed.\n",
__func__);
#endif
return -1;
}
//fprintf (cctx->file_output, "SCHED: %s - returning 0.\n", __func__);
//fprintf (stderr, "%s - runs.\n", __func__);
return 0;
}
/****************************************************************************************
* Function name - handle_logfile_rewinding_timer
*
* Description - Handling of logfile controlling periodic timer
*
* Input - *timer_node - pointer to timer node structure
* *pvoid_param - pointer to some extra data; here batch context
* *ulong_param - some extra data.
* Return Code/Output - On success -0, on error - (-1)
****************************************************************************************/
static int handle_logfile_rewinding_timer (timer_node* timer_node,
void* pvoid_param,
unsigned long ulong_param)
{
batch_context* bctx = (batch_context *) pvoid_param;
(void) timer_node;
(void) ulong_param;
if (rewind_logfile_above_maxsize (bctx->cctx_array->file_output) == -1)
{
fprintf (stderr, "%s - rewind_logfile_above_maxsize() failed .\n",
__func__);
return -1;
}
//fprintf (stderr, "%s - runs.\n", __func__);
return 0;
}
/******************************************************************************
* Function name - handle_screen_input_timer
*
* Description - Handling of screen imput
*
* Input - *timer_node - pointer to timer node structure
* *pvoid_param - pointer to some extra data; here batch context
* *ulong_param - some extra data.
*
* Return Code/Output - On success -0, on error - (-1)
******************************************************************************/
static int handle_screen_input_timer (timer_node* timer_node,
void* pvoid_param,
unsigned long ulong_param)
{
batch_context* bctx = (batch_context *) pvoid_param;
(void) timer_node;
(void) ulong_param;
screen_test_keyboard_input (bctx);
//fprintf (stderr, "%s - runs.\n", __func__);
return 0;
}
/*************************************************************************
* Function name - handle_cctx_sleeping_timer
*
* Description - Handling of timer for a client waiting in the waiting queue to
* respect url interleave timeout. Schedules the client to perform
* the next loading operation.
*
* Input - *tn - pointer to timer node structure
* *pvoid_param - pointer to some extra data; here batch context
* *ulong_param - some extra data.
* Return Code/Output - On success -0, on error - (-1)
***************************************************************************/
int handle_cctx_sleeping_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param)
{
(void)pvoid_param;
(void)ulong_param;
client_context* cctx = (client_context *) tn;
batch_context* bctx = cctx->bctx;
url_context* url = &bctx->url_ctx_array[cctx->url_curr_index];
bctx->sleeping_clients_count--;
if (url->fresh_connect)
{
/*
On a fresh connect we reset the connection and go to sleep.
The call to setup_url (cctx) is postponed to the timer handler
timer handler.
*/
// Setup the new url.
setup_url (cctx);
}
const unsigned long now_time = get_tick_count ();
return client_add_to_load (bctx, cctx, now_time);
}
/*****************************************************************************
* Function name - put_free_client
*
* Description - Puts a client on the list of clients free to send
* a fixed rate request
*
* Input - *cctx - pointer to the client context
* Return Code/Output - On success 0, on error -1
******************************************************************************/
int put_free_client (client_context* cctx)
{
batch_context *bctx = cctx->bctx;
if (bctx->free_clients_count >= bctx->client_num_max)
/* Debugging, should not happen :-) */
{
fprintf (stderr,"%s - error: no room in free client client list.\n",
__func__);
return -1;
}
int free_client_no = cctx - bctx->cctx_array + 1;
if (free_client_no < 0 || free_client_no > bctx->client_num_max)
/* Debugging, should not happen :-) */
{
fprintf (stderr,"%s - error: invalid free client number %d.\n",
__func__, free_client_no);
return -1;
}
if (bctx->free_clients[bctx->free_clients_count])
/* Debugging, should not happen :-) */
{
fprintf (stderr,
"%s - error: non-empty free client list entry at count %d.\n",
__func__, bctx->free_clients_count);
return -1;
}
bctx->free_clients[bctx->free_clients_count++] = free_client_no;
return 0;
}
/*************************************************************************
* Function name - handle_cctx_url_completion_timer
*
* Description - Handling of timer for a client waiting in the waiting queue to
* respect url interleave timeout. Schedules the client to perform
* the next loading operation.
*
* Input - *tn - pointer to timer node structure
* *pvoid_param - pointer to some extra data; here batch context
* *ulong_param - some extra data.
* Return Code/Output - On success -0, on error - (-1)
***************************************************************************/
static int handle_cctx_url_completion_timer (timer_node* tn,
void* pvoid_param,
unsigned long ulong_param)
{
client_context* cctx = (client_context *) tn;
batch_context* bctx = cctx->bctx;
(void)pvoid_param;
(void)ulong_param;
int sched_now;
cctx->tid_url_completion = -1;
// Increment operational statistics
op_stat_timeouted (&bctx->op_delta, cctx->url_curr_index);
// Considering url completion timeout as an error
// TODO - make it configurable
stat_url_timeout_err_inc (cctx);
cctx->client_state = CSTATE_ERROR;
const unsigned long now_time = get_tick_count ();
if (verbose_logging)
{
fprintf (cctx->file_output,
"%ld %ld %ld %s !! ERUT url completion timeout: url: %s\n",
now_time - bctx->start_time,
cctx->cycle_num, cctx->url_curr_index, cctx->client_name,
bctx->url_ctx_array[cctx->url_curr_index].url_str);
}
/*
If fixed request rate is specified, free the client, the next step
is loaded on the request rate timer.
*/
return (bctx->req_rate) ? put_free_client(cctx) :
load_next_step(cctx, now_time, &sched_now);
}
/*************************************************************************