-
Notifications
You must be signed in to change notification settings - Fork 1
/
slg_master.c
1106 lines (886 loc) · 31.2 KB
/
slg_master.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
/*
Copyright (C) 2013
Fabien Gaud <fgaud@sfu.ca>, Baptiste Lepers <baptiste.lepers@inria.fr>,
Fabien Mottet <fabien.mottet@inria.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 or later, as published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <assert.h>
#include <getopt.h>
#include <time.h>
#include <sys/time.h>
//Specify the debug level
#define DEBUG_LEVEL -1
#include "master_slave.h"
#include "slg_master.h"
#include "debug_tools.h"
#include "stats_utils.h"
// Socket file descriptor
struct sockaddr_in sock_w;
unsigned int len_w;
// Receiving socket
int rs;
struct sockaddr_in server_addr;
// My name
char name[256];
master_stats_t * master_stats;
struct timeval start_time;
struct timeval stop_time;
//////////////////Global var////////////////////////////
slaves_infos_t * slaves_infos;
unsigned int nb_slaves;
char * targets;
unsigned int nb_waves = 0;
unsigned int nb_iterations_in_a_wave = 0;
unsigned int nb_clients = 0;
unsigned int duration = 0;
unsigned int nb_msg_per_connection = 0;
unsigned int delay = 0;
int percent_stats_kept_master = 100;
/** If not N-Copy only one server is targeted
* with possibly multiple itf
*/
int N_copy = 0;
/** Warmup ? **/
static int warmup_length = 0;
slaves_stats_t * slaves_stats = NULL;
unsigned int nb_clients_min = 0;
unsigned int nb_clients_max = 0;
int step = 0;
char * slavesInfos;
char * config_file = NULL;
typedef struct {
char* server;
int port;
}server_t;
server_t* servers;
int nb_distict_servers = 0;
///////////////////////////////////////////////////////
int more_output_for_stderr = 0;
void parse_config_file(char * conf_file) {
char buffer[2048];
FILE * stream;
stream = fopen(conf_file, "r");
if (stream == NULL) {
printf("file %s doesn't exist or bad permissions.\n", conf_file);
exit(EXIT_FAILURE);
}
int line_num = 0;
while (fgets(buffer, 2048, stream) != NULL) {
line_num++;
//jump useless lines
if (strchr(buffer, '#')==buffer) {
continue;
}
else if (strcmp(buffer, "\n")==0) {
continue;
}
const char delimiters[] = " =\n";
char *cp;
char * value;
char * param;
cp = strdup(buffer);
param = strtok(cp, delimiters);
value = strtok(NULL, delimiters);
if (value == NULL) {
printf("no value for parameter %s line %d\n", param, line_num);
exit(EXIT_FAILURE);
}
if (strcmp(param, "nb_iterations_in_a_wave") == 0) {
nb_iterations_in_a_wave = atoi(value);
}
else if (strcmp(param, "targets") ==0) {
targets = value;
}
else if (strcmp(param, "nb_clientsMin") ==0) {
nb_clients_min = atoi(value);
}
else if (strcmp(param, "nb_clientsMax") ==0) {
nb_clients_max = atoi(value);
}
else if (strcmp(param, "step") ==0) {
step = atoi(value);
}
else if (strcmp(param, "duration") ==0) {
duration = atoi(value);
}
else if (strcmp(param, "nb_msg_per_connection") ==0) {
nb_msg_per_connection = atoi(value);
}
else if (strcmp(param, "delay") ==0) {
delay = atoi(value);
}
else if (strcmp(param, "percent_stats_kept_master") ==0) {
percent_stats_kept_master = atoi(value);
}
else if (strcmp(param, "slaves") ==0) {
slavesInfos = value;
}
else if (strcmp(param, "N-copy") ==0) {
N_copy = atoi(value);
}
else if (strcmp(param, "warmup") == 0) {
warmup_length = atoi(value);
}
else {
PANIC("unknown config parameter line %d: %s\n", line_num, param);
}
}
fclose(stream);
}
void print_help_message() {
printf("-------------------------SLG_MASTER HELP MESSAGE-------------------------\n");
printf("parameters:\n");
printf("\t--param (-p): description\n");
printf("\t--targets (-t): list of target for each slave. (ip:port,ip:port)\n");
printf("\t--slaves (-S): list of slaves. (slave:port,slave:port)\n");
printf("\t--nb_iterations (-i): the number of iteration to do in a wave\n");
printf("\t--nb_clients_min (-m): each slave starts with this number of clients\n");
printf("\t--nb_clients_max (-M): each slave ends with this number of clients\n");
printf("\t--step (-s): the steps from nb_clients_min to nb_clients_max\n");
printf("\t--duration (-D): duration of one iteration (in seconds) \n");
printf("\t--nb_msg_per_connection (-n): number of request per connection\n");
printf("\t--delay* (-d): not yet implemented\n");
printf("\t--percent_stats_kept_master* (-p): the percentage number of samples kept on the master\n");
printf("\t--file* (-f): config file\n");
printf("\t-r: dump a summary on stderr to know where is the bench when redirecting in a file\n");
printf("Parameters with a * are optionnal, all others are mandatory.\n");
printf("The parameters from the command line overide the ones in the config file .\n");
printf("-------------------------------------------------------------------------\n");
exit(EXIT_SUCCESS);
}
//Check everything is initialized
void check_all_parameters_filled() {
if (targets == NULL) {
PANIC("targets not initialized.\n");
}
if (nb_iterations_in_a_wave == 0) {
PANIC("nb_iterations_in_a_wave not initialized.\n");
}
if (duration == 0) {
PANIC("nb_connections not initialized.\n");
}
if (nb_msg_per_connection == 0) {
PANIC("nb_msg_per_connection not initialized.\n");
}
if (percent_stats_kept_master<=0 || percent_stats_kept_master>100) {
PANIC("percent_stats_kept_slaves should be in [1,100].\n");
}
if (nb_clients_min == 0) {
PANIC("nb_clients_min not initialized.\n");
}
if (nb_clients_max == 0) {
PANIC("nb_clients_max not initialized.\n");
}
if (slavesInfos == NULL) {
PANIC("slavesInfos not initialized.\n");
}
if(N_copy != 0 && N_copy != 1){
PANIC("Unknown value for N_Copy");
}
if(warmup_length < 0){
PANIC("Unknown value for warmup");
}
return;
}
void parse_command_line(int argc, char** argv) {
//Command line has priority on config file, so let's do the file first
int i = 0;
while (i<argc) {
if ( (strcmp(argv[i], "-f")==0) || (strcmp(argv[i], "--file")==0)) {
if (i+1>=argc) {
printf("missing file name\n");
exit(EXIT_FAILURE);
}
config_file = argv[i+1];
parse_config_file(config_file);
break;
}
i++;
}
int c;
while (1) {
int option_index = 0;
static struct option long_options[] =
{
{ "nb_iterations", 1, 0, 'i' },
{ "targets", 1, 0, 't' },
{ "nb_clients_min", 1, 0, 'm' },
{ "nb_clients_max", 1, 0, 'M' },
{ "step", 1, 0, 's' },
{ "duration", 1, 0, 'D' },
{ "nb_msg_per_connection", 1, 0, 'n' },
{ "delay", 1, 0,'d' },
{ "slaves", 1, 0, 'S' },
{ "percent_stats_kept_master", 1, 0, 'P' },
{ "help", 0, 0, 'h' },
{ "file", 1, 0, 'f' },
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "i:t:m:M:s:D:n:d:S:f:P:rh",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'i':
//printf("option i with value '%s'\n", optarg);
nb_iterations_in_a_wave = atoi(optarg);
break;
case 't':
targets = optarg;
break;
case 'm':
nb_clients_min = atoi(optarg);
break;
case 'M':
nb_clients_max = atoi(optarg);
break;
case 's':
step = atoi(optarg);
break;
case 'D':
duration = atoi(optarg);
break;
case 'n':
nb_msg_per_connection = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 'f':
break;
case 'r':
more_output_for_stderr = 1;
break;
case 'h':
print_help_message();
break;
case 'P':
percent_stats_kept_master = atoi(optarg);
break;
case 'S':
slavesInfos = optarg;
break;
default:
exit(EXIT_FAILURE);
}
}
if (optind < argc) {
printf("invalid argument: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
exit(EXIT_FAILURE);
}
check_all_parameters_filled();
}
/**
* Initialise the stat tables
*/
void init_master_stat_report(int nb_waves, int nb_iterations_in_wave) {
int w;
for (w=0; w<nb_waves; w++) {
init_stats_sample_Lf(&master_stats[w].totalTime, nb_iterations_in_wave, "totalTime");
init_stats_sample_Lf(&master_stats[w].avgCT, nb_iterations_in_wave, "avgCT");
init_stats_sample_Lf(&master_stats[w].avgRT, nb_iterations_in_wave, "avgRT");
init_stats_sample_Lf(&master_stats[w].avgCRT, nb_iterations_in_wave, "avgCRT");
init_stats_sample_Lf(&master_stats[w].resp_rate, nb_iterations_in_wave, "resp_rate");
init_stats_sample_Lf(&master_stats[w].bytes_rate, nb_iterations_in_wave, "bytes_rate");
init_stats_sample_Lf(&master_stats[w].maxCT, nb_iterations_in_wave, "maxCT");
init_stats_sample_Lf(&master_stats[w].minCT, nb_iterations_in_wave, "minCT");
init_stats_sample_Lf(&master_stats[w].errors, nb_iterations_in_wave, "errors");
}
}
/**
* Fill the slave reported values in the corresponding case of the stats tables
*/
void fill_master_stat_report(
int nbSlaves,
__attribute__((unused)) slaves_stats_t * slaves,
int wave_number,
unsigned long duration)
{
int i;
int w = wave_number;
long double totalTime = 0;
long double avgCT = 0;
long double avgRT = 0;
long double avgCRT = 0;
long double bytes_rate = 0;
long double resp_rate = 0;
long double maxCT = 0;
long double minCT = 0;
long double errors = 0;
for (i=0; i<nbSlaves; i++) {
totalTime += slaves[i].totalTime;
avgCT += slaves[i].avgCT;
avgRT += slaves[i].avgRT;
avgCRT += slaves[i].avgCRT;
bytes_rate += slaves[i].bytes_recv;
resp_rate += slaves[i].resp_recv;
errors += slaves[i].errors;
}
insert_Lf(&master_stats[w].totalTime, totalTime/nbSlaves);
insert_Lf(&master_stats[w].avgCT, avgCT/nbSlaves);
insert_Lf(&master_stats[w].avgRT, avgRT/nbSlaves);
insert_Lf(&master_stats[w].avgCRT, avgCRT/nbSlaves);
// *1000000 because we want request per second not per usec
insert_Lf(&master_stats[w].bytes_rate, (bytes_rate/duration)*1000000);
insert_Lf(&master_stats[w].resp_rate, (resp_rate/duration)*1000000);
insert_Lf(&master_stats[w].maxCT, maxCT/nbSlaves);
insert_Lf(&master_stats[w].minCT, minCT/nbSlaves);
insert_Lf(&master_stats[w].errors, errors);
}
/**
* Prints the stats for one wave.
*/
void print_stats(int nb_client_min, int step, int nbSlaves, int wave_num) {
#if DEBUG_TASK
fprintf(stderr,"----------------------------------------------------------------\n");
fprintf(stderr,"All presented times are in microseconds(us), excepted those mentionned.\n");
fprintf(stderr,"All values are average values.\n");
fprintf(stderr,"----------------------------------------------------------------\n");
fprintf(stderr,"----------------------------------------------------------------\n");
fprintf(stderr,"----------------------MASTER RESULTS----------------------------\n");
fprintf(stderr,"----------------------------------------------------------------\n");
#endif
int w = wave_num;
if (more_output_for_stderr) {
fprintf(stderr, "[ %s ] %5d clients done, throughput: %Lf, stddev: %Lf\n",
getCurrentTime(),(nb_client_min+(w*step))*nbSlaves,
average_Lf(&master_stats[w].resp_rate),
stddev_Lf(&master_stats[w].resp_rate));
}
long double resp_rate_avg = percentil_average_Lf(&master_stats[w].resp_rate, percent_stats_kept_master);
long double resp_rate_stddev = percentil_stddev_Lf(&master_stats[w].resp_rate, percent_stats_kept_master);
long double bytes_rate_avg = percentil_average_Lf(&master_stats[w].bytes_rate, percent_stats_kept_master);
long double bytes_rate_stddev = percentil_stddev_Lf(&master_stats[w].bytes_rate, percent_stats_kept_master);
long double ct_avg = percentil_average_Lf(&master_stats[w].avgCT, percent_stats_kept_master);
long double ct_stddev = percentil_stddev_Lf(&master_stats[w].avgCT, percent_stats_kept_master);
long double rt_avg = percentil_average_Lf(&master_stats[w].avgRT, percent_stats_kept_master);
long double rt_stddev = percentil_stddev_Lf(&master_stats[w].avgRT, percent_stats_kept_master);
long double crt_avg = percentil_average_Lf(&master_stats[w].avgCRT, percent_stats_kept_master);
long double crt_stddev = percentil_stddev_Lf(&master_stats[w].avgCRT, percent_stats_kept_master);
long double tt_avg = percentil_average_Lf(&master_stats[w].totalTime, percent_stats_kept_master);
long double tt_stddev = percentil_stddev_Lf(&master_stats[w].totalTime, percent_stats_kept_master);
long double errors_avg = percentil_average_Lf(&master_stats[w].errors, percent_stats_kept_master);
long double errors_stddev = percentil_stddev_Lf(&master_stats[w].errors, percent_stats_kept_master);
//nb_clients*nbSlaves, master_stats.req_throughput, master_stats.bytes_throughput, master_stats.avgCT, master_stats.avgRT, master_stats.avgCRT, master_stats.totalTime
printf("%5d"
"\t%15.02Lf %3.02Lf%%"
"\t%15.02Lf %3.02Lf%%"
"\t%15.02Lf %3.02Lf%%"
"\t%15.02Lf %3.02Lf%%"
"\t%15.02Lf %3.02Lf%%"
"\t%15.02Lf %3.02Lf%%"
"\t%15.02Lf %3.02Lf%%\n",
(nb_client_min+(w*step))*nbSlaves,
resp_rate_avg,
resp_rate_stddev * 100. / resp_rate_avg,
bytes_rate_avg / (1024.*1024.) * 8.,
bytes_rate_stddev / bytes_rate_avg * 100.,
ct_avg,
ct_stddev * 100. / ct_avg,
rt_avg,
rt_stddev * 100. / rt_avg,
crt_avg,
crt_stddev * 100. / crt_avg,
tt_avg,
tt_stddev * 100. / tt_avg,
errors_avg,
errors_stddev * 100. / errors_avg );
#if DEBUG_TASK
for (w = 0; w < NB_PRINT_SEPARATORS; w++) {
fprintf(stderr,"-");
}
#endif
fprintf(stderr,"\n");
}
/**
* Init slaves_infos tab and generate for each slave, its addr, port, target and target_port
* slavesInfos: the string of slaves
* targets: the string of targets
* returns the number of slaves
*
*/
int init_slaves_info(char *slavesInfos, char * targets) {
const char delimiters[] = ",";
char *token, *cp;
int nb_slaves = 1;
int nb_targets = 1;
char * temp;
int i;
temp = slavesInfos;
//calculate nb_slaves:
while (1) {
temp = strchr(temp, delimiters[0]);
if (temp == NULL)
break;
temp += sizeof(char);
nb_slaves++;
}
temp = targets;
//calculate nb_targets:
while (1) {
temp = strchr(temp, delimiters[0]);
if (temp == NULL)
break;
temp += sizeof(char);
nb_targets++;
}
/** Servers structure **/
servers = calloc(nb_targets,sizeof(server_t));
if (nb_targets != nb_slaves) {
printf("Not the same number of targets and slaves\n");
exit(0);
}
slaves_infos = calloc(nb_slaves, sizeof(slaves_infos_t));
assert(slaves_infos!=NULL);
//Extract slaves info.
cp = strdup(slavesInfos);
token = strtok(cp, delimiters);
if (token == NULL) {
printf("Bad slaves adresses format\n");
exit(0);
}
for (i = 0; i<nb_slaves; i++) {
char * port = strchr(token, ':');
if (port == NULL) {
printf("Bad slaves addresse format\n");
exit(0);
}
*port = 0;
port += sizeof(char);
//Get hostname
strcpy(slaves_infos[i].addr, token);
//Get port
slaves_infos[i].port = atoi(port);
//init socket
slaves_infos[i].socket = -1;
token = strtok(NULL, delimiters);
if (token == NULL)
break;
}
//Extract targets info.
cp = strdup(targets);
token = strtok(cp, delimiters);
if (token == NULL) {
printf("Bad slaves adresses format\n");
exit(0);
}
for (i = 0; i<nb_targets; i++) {
char * port = strchr(token, ':');
if (port == NULL) {
printf("Bad targets address format\n");
exit(0);
}
*port = 0;
port += sizeof(char);
//Put target host
strcpy(slaves_infos[i].target, token);
//Get target port
slaves_infos[i].target_port = atoi(port);
/** Search if server already found **/
int j = 0;
for(j = 0; j < nb_distict_servers; j++){
if(strcmp(slaves_infos[i].target, servers[j].server) == 0
&& slaves_infos[i].target_port == servers[j].port){
/** Already inserted **/
break;
}
}
/** If not found **/
if(j == nb_distict_servers){
servers[j].server = slaves_infos[i].target;
servers[j].port = slaves_infos[i].target_port;
DEBUG_TMP("Found a new server : %s,%d\n", servers[j].server, servers[j].port );
nb_distict_servers++;
}
token = strtok(NULL, delimiters);
if (token == NULL)
break;
}
/** If not N-Copy only one server is targeted
* with possibly multiple itf
*/
if(!N_copy){
nb_distict_servers = 1;
}
return nb_slaves;
}
/**
* Connects the master to the slaves
*/
void connect_to_slaves(slaves_infos_t * slaves, int nb_slaves) {
int i = 0;
while (i<nb_slaves) {
int fd;
struct sockaddr_in fsin;
struct sockaddr_in soc_address;
struct sockaddr_in *sin_ptr = &soc_address;
//init server target
struct in_addr *ip_addr;
struct hostent *ent= NULL;
//Init remote server struct
if (slaves[i].addr) {
ent = gethostbyname(slaves[i].addr);
if (ent == NULL) {
fprintf(stderr, "lookup on slave's name \"%s\" failed\n", slaves[i].addr);
exit(-1);
}
ip_addr = (struct in_addr *)(*(ent->h_addr_list));
sin_ptr->sin_family = AF_INET;
bcopy(ip_addr, &(sin_ptr->sin_addr), sizeof(struct in_addr));
}
if (!ent) {
if (slaves[i].addr)
fprintf(stderr,"error - didn't get host info for %s\n", slaves[i].addr);
else
fprintf(stderr,"error - never called gethostbyname\n");
exit(-1);
}
sin_ptr->sin_port = htons(slaves[i].port);
//Init client side
//Getting Socket
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == -1) {
perror("socket");
//finish();
exit(0);
}
int reuse_addr = 1;
//Enables local address reuse
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr))
< 0) {
perror("setsockopt");
}
fsin.sin_family = AF_INET;
fsin.sin_addr.s_addr = htonl(INADDR_ANY);
fsin.sin_port = htons(0);
if (bind(fd, (struct sockaddr*)&fsin, sizeof(fsin)) < 0) {
fprintf(stderr, "(%s) bind error on port %d\n",__FUNCTION__, 0);
exit(-1);
}
int status;
status
= connect(fd, (struct sockaddr *)&soc_address, sizeof(soc_address));
if (status < 0) {
printf("Error on %s\n", slaves[i].addr);
perror("connect");
exit(-1);
}
slaves[i].socket = fd;
i++;
}
}
/**
* Sends order to slaves
* Warning: msg MUST be a string with a null termination character.
*/
void send_order_to_slaves(slaves_infos_t * slaves, int nb_slaves, int warmup) {
int i = 0;
int cnt = 0;
char msg[1024];
char name[256];
while (i<nb_slaves) {
bzero(msg, sizeof(msg));
bzero(name, sizeof(name));
gethostname(name, 255);
//creating the query string to send to the slaves.
//params: slave_number, reportingAddress, host, port, nb_clients, nb_iterations, nb_msg_per_connection, delay, slaves_interval_avg_percent
int _duration = duration;
if(warmup){
_duration = warmup_length;
}
sprintf(msg, "%d,%s,%s,%d,%d,%d,%d,%d\n",
i, name, slaves[i].target, slaves[i].target_port,
nb_clients, _duration, nb_msg_per_connection,
delay);
cnt = 0;
do {
cnt += write(slaves[i].socket, msg, strlen(msg));
if (cnt == -1) {
perror("Send failed");
exit(0);
}
} while ((unsigned int)cnt<strlen(msg));
if (cnt < 0) {
perror("sendto");
exit(1);
}
i++;
}
}
/**
* Read responses from the slaves
*/
void read_slaves_response(slaves_infos_t * slaves_infos, slaves_stats_t * slaves, int nb_slaves, int warmup) {
int i;
char* buf = malloc(RESPONSE_SIZE * sizeof(char));
if (buf == NULL) {
perror("malloc");
exit(0);
}
for (i = 0; i < nb_slaves; i++) {
//Read request
bzero(buf, RESPONSE_SIZE * sizeof(char));
//Reading message until \n
int rd = 0;
int totalLength = 0;
do {
rd = read(slaves_infos[i].socket, buf + totalLength, RESPONSE_SIZE - totalLength);
if (rd == 0) {
printf("Connection closed on socket %d (client %s) and slave response not full.\n", slaves_infos[i].socket, slaves_infos[i].addr);
printf("Already received %d bytes : %s\n", totalLength, buf);
exit(-1);
}
else if (rd == -1) {
printf("Error while reading on socket %d\n", slaves_infos[i].socket);
exit(-1);
}
totalLength += rd;
if (buf[totalLength - 1] == '\n') {
break;
}
} while (1);
DEBUG("Received data: %s\n", buf);
if (!warmup) {
//Filling slaves_stats with the incoming data :
struct sockaddr_in csin;
unsigned int size = sizeof(csin);
getpeername(slaves_infos[i].socket, (struct sockaddr*) &csin, &size);
//setting slave id
sprintf(slaves[i].id, "%s:%d", inet_ntoa(csin.sin_addr), htons(csin.sin_port));
//Getting results
// total time, global_bytes_throughput, global_req_throughput, global_avgCT, global_avgRT, global_avgCRT, global_minCT, global_maxCT, global_errors
const char delimiters[] = ",";
char *token, *cp;
cp = strdup(buf);
token = strtok(cp, delimiters);
slaves[i].totalTime = atol(token);
token = strtok(NULL, delimiters);
slaves[i].bytes_recv = atoll(token);
token = strtok(NULL, delimiters);
slaves[i].resp_recv = atol(token);
token = strtok(NULL, delimiters);
slaves[i].avgCT = atof(token);
token = strtok(NULL, delimiters);
slaves[i].avgRT = atof(token);
token = strtok(NULL, delimiters);
slaves[i].avgCRT = atof(token);
token = strtok(NULL, delimiters);
slaves[i].minCT = atol(token);
token = strtok(NULL, delimiters);
slaves[i].maxCT = atol(token);
token = strtok(NULL, delimiters);
slaves[i].errors = atol(token);
}
close(slaves_infos[i].socket);
DEBUG("\tMaster received responses from %s: %s\n", slaves[i].id, getCurrentTime());
}
free( buf);
}
//sum up the parameters
void print_parameter_summary(FILE* output) {
unsigned int i;
fprintf(output,"\n********** System Info **********\n");
do_proc_info(output, "/proc/sys/net/ipv4/tcp_tw_recycle");
do_proc_info(output, "/proc/sys/net/ipv4/tcp_fin_timeout");
do_proc_info(output, "/proc/sys/net/ipv4/ip_local_port_range");
fprintf(output,"*********************************\n\n");
char * config_file= NULL;
fprintf(output, "Parameters:\n");
if (config_file != NULL) {
fprintf(output, "\t- config_file: %s\n", config_file);
}
fprintf(output, "\t- targets: ");
for (i = 0; i<nb_slaves; i++) {
fprintf(output, "%s:%d", slaves_infos[i].target, slaves_infos[i].target_port);
if (i!=nb_slaves-1) {
fprintf(output, ",");
}
}
fprintf(output, "\n");
fprintf(output, "\t- slaves list: ");
for (i = 0; i<nb_slaves; i++) {
fprintf(output, "%s:%d", slaves_infos[i].addr, slaves_infos[i].port);
if (i!=nb_slaves-1) {
fprintf(output, ",");
}
}
fprintf(output, "\n");
fprintf(output,
"\t- nb slaves: %d\n"
"\t- nb iterations in a wave: %d\n"
"\t- nb waves: %d\n"
"\t- nb clients min:%d\n"
"\t- nb clients max:%d\n"
"\t- nb clients step:%d\n"
"\t- iteration duration:%d s\n"
"\t- nb msg per connection:%d\n"
"\t- master percent: %d%%\n"
"\t- sleep time between two iterations: %d (us)\n"
"\t- warmup: %d s\n",
nb_slaves, nb_iterations_in_a_wave, nb_waves, nb_clients_min,
nb_clients_max, step, duration,nb_msg_per_connection,
percent_stats_kept_master, SLEEP_TIME_BETWEEN_TWO_ITERATIONS, warmup_length);
#ifdef USE_RST
fprintf(output,"\t- Using RST flag for closing connection\n");
#endif
#if CLOSE_AFTER_REQUEST
fprintf(output, "\t--- WARNING: Close after request ... ---\n");
#endif
#ifdef HTTP_PROTOCOL
fprintf(output,"\t- Using HTTP protocol\n");
#elif MEMCACHED_PROTOCOL
fprintf(stderr,"\t- Using MEMCACHED protocol\n");
#endif
#if UNIQUE_FILE_ACCESS_PATTERN
fprintf(output, "\t- Accessing to a unique file\n");
#elif SPECWEB99_FILE_ACCESS_PATTERN
fprintf(output, "\t- Using SpecWeb99 distribution\n");
#elif SPECWEB05_FILE_ACCESS_PATTERN
fprintf(output, "\t- Using SpecWeb05 distribution\n");
#else
PRINT_ALERT("Unknown access file protocol\n");
exit(EXIT_FAILURE);
#endif
fprintf(output, "\t- N-Copy server : %s \n", N_copy ? "true" : "false");
//if(output != stderr){
fprintf(output,"\n%5s\t%10s %10s\t%14s %10s\t%10s %10s\t%10s %10s\t%10s %10s\t%14s %10s\t%10s %10s\n",
"nbC",
"req/s","sdev",
"Mbits/s","sdev",
"CT (µs)","sdev",
"RT (µs)","sdev",
"CRT (µs)","sdev",
"tavgTotalTime (µs)","stddev",
"errors","stddev");
for (i = 0; i < NB_PRINT_SEPARATORS; i++) {
fprintf(output,"-");
}
fprintf(output,"\n");
//}
}
void warmup_time(){
if(warmup_length <= 0){
return;
}
connect_to_slaves(slaves_infos, nb_slaves);
send_order_to_slaves(slaves_infos, nb_slaves, 1);
fprintf(stderr,">> Warmup %s\n",getCurrentTime());
//wait connections from slaves to read reports.
read_slaves_response(slaves_infos, NULL, nb_slaves, 1);
//STOP time
fprintf(stderr,"<< Warmup %s\n",getCurrentTime());
usleep(SLEEP_TIME_BETWEEN_TWO_ITERATIONS);
}
/**
*
* For each step, slg_master sends a wave onto the webserver:
* 1- A wave is composed of nb_iterations_in_a_wave basic loads.
* 2- A basic load is a single load generation order sent to all slg_slaves.
*
* So, there is one wave per number of cl DEBUG_TMP("Send special request to server ")ients to bench. (Each step creates a new wave).
*
*/
int main(int argc, char** argv) {
unsigned int i;
if (argc == 1) {
print_help_message();
}