-
Notifications
You must be signed in to change notification settings - Fork 1
/
slg.c
2235 lines (1749 loc) · 60.9 KB
/
slg.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.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#include <assert.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <time.h>
// Include the header
#include "slg.h"
#define DO_NOTHING 0
#define DO_WRITE 1
#define DO_READ 2
/** Main start and stop time */
struct timeval start_time;
struct timeval stop_time;
uint64_t start_time_cycles;
/** Global average stats of all clients */
long double global_avgCT;
unsigned long long nb_CT = 0;
long double global_avgRT = 0;
unsigned long long nb_RT = 0;
long double global_avgCRT = 0;
unsigned long long nb_CRT = 0;
/*global var are only available in compute stats*/
unsigned long global_errors;
unsigned long global_nbRequestsTaken;
unsigned long global_maxCT;
unsigned long global_minCT;
unsigned long long global_total_bytes_recv;
unsigned long long global_total_resp_recv;
unsigned long long file_nb_req;
unsigned long long file_nb_req_successful;
unsigned long long file_nb_reads;
/** Client data */
int currentClientPort;
/** For slave/daemon behaviour */
char * reportingAddress;
int slave_num;
/** Socket file descriptor */
int rs;
struct sockaddr_in sock_r, sock_w;
unsigned int len_w, len_r;
/** The two threads of the load generator */
pthread_t reading_thread;
pthread_t writing_thread;
/** application parameters with default values */
char * host;
unsigned int port;
unsigned int nb_clients;
unsigned int nb_msg_per_connection;
unsigned long long duration;
unsigned int delay;
/** All the clients will be save in a table pointed by this pointer */
client_t *all_clients;
/** Options for sockets */
int tcp_no_delay;
int reuse_addr;
/** Linger on close if data present; socked will be closed immediatly
* Linger is initialized during init_parameters
*/
struct linger linger;
/** buffers for socket inputs/outputs */
int sendwin;
int rcvwin;
/**Socket and var for accepting master connection**/
int waitingMasterPort;
int masterListenOrderSocket;
// My name
char name[256];
struct sockaddr_in server_addr;
//Master fds for connect
fd_set master_connect_fds;
int max_connect_socket = 0;
//write thread fills it, read thread empties it.
circularBuffer * toReadBuffer;
//wreadrite thread fills it, write thread empties it.
circularBuffer * toWriteBuffer;
#if UNIQUE_FILE_ACCESS_PATTERN
static char * file = "/index.html";
#elif SPECWEB99_FILE_ACCESS_PATTERN
/** Values used for specweb */
// Number of directories - based on load value
static int num_dirs;
// Zipf distribution table for directory
static double* dir_zipf;
#elif SPECWEB05_FILE_ACCESS_PATTERN
// Number of directories - based on SPECWEB05_SIMULTANEOUS_SESSIONS value
static int num_dirs;
#endif
static uint64_t proc_freq;
static int my_hostname;
uint64_t computeCPUhz(){
uint64_t start,stop,acc;
int i;
int nbIter = 1;
acc = 0;
for(i = 0; i< nbIter; i++){
rdtscll(start);
usleep(1000000); //Sleep for approx 100ms
rdtscll(stop);
acc += stop - start;
}
acc = acc / nbIter;
#if DEBUG_LEVEL == 1
DEBUG_TMP("Found %llu cycles for 100ms\n",(long long unsigned) acc);
#else
DEBUG("Found %llu cycles for 100ms\n",(long long unsigned) acc);
#endif //DEBUG_LEVEL
return acc*nbIter;
}
#if NON_BLOCKING_SOCKET
int setNonblocking(int fd) {
DEBUG("Setting fd %d in a non-blocking mode\n",fd);
int x;
x = fcntl(fd, F_GETFL, 0);
return fcntl(fd, F_SETFL, x | O_NONBLOCK);
}
#endif
void getSocketPeerInfo(int s, char * name, int len, unsigned short * p) {
struct sockaddr_in csin;
unsigned int size = sizeof(csin);
int v = getpeername(s, (struct sockaddr*)&csin, &size);
if (v!=0) {
PANIC("warning, invalid fd: %d.\n", s);
}
strncpy(name, inet_ntoa(csin.sin_addr), len);
*p = htons(csin.sin_port);
}
#if SPECWEB99_FILE_ACCESS_PATTERN
/**
* Setup table of Zipf distribution values according to given size
*/
void setupZipf(double* table, int size) {
double zipf_sum;
int i;
for (i = 1; i <= size; i++) {
table[i-1] = (double)1.0 / (double)i;
}
zipf_sum = 0.0;
for (i = 1; i <= size; i++) {
zipf_sum += table[i-1];
table[i-1] = zipf_sum;
}
table[size-1] = 0.0;
table[0] = 0.0;
for (i = 0; i < size; i++) {
table[i] = 1.0 - (table[i] / zipf_sum);
}
}
/**
* Return index into Zipf table of random number chosen from 0.0 to 1.0
*/
int zipf(double* table) {
double r = (double) rand() / ((double)RAND_MAX+1.);
int i = 0;
while (r < table[i]) {
i++;
}
return i-1;
}
int get_new_value_from_table(double* table, int table_size) {
double r = (double) rand() / ((double)RAND_MAX+1.);
int i = 0;
while (r > table[i] && r < table_size) {
i++;
}
if(r==table_size){
return -1;
}
else{
return i;
}
}
#elif SPECWEB05_FILE_ACCESS_PATTERN
/** See http://www.cse.usf.edu/~christen/tools/genzipf.c **/
int zipf(double alpha, int n)
{
static int first = 1; // Static first time flag
static double c = 0; // Normalization constant
double z; // Uniform random number (0 < z < 1)
double sum_prob; // Sum of probabilities
double zipf_value = 0; // Computed exponential value to be returned
int i; // Loop counter
// Compute normalization constant on first call only
if (first)
{
for (i=1; i<=n; i++)
c = c + (1.0 / pow((double) i, alpha));
c = 1.0 / c;
first = 0;
}
// Pull a uniform random number (0 < z < 1)
do
{
z = (double) rand() / ((double)RAND_MAX+1.);
}
while ((z == 0) || (z == 1));
// Map z to the value
sum_prob = 0;
for (i=1; i<=n; i++)
{
sum_prob = sum_prob + c / pow((double) i, alpha);
if (sum_prob >= z)
{
zipf_value = i;
break;
}
}
// Assert that zipf_value is between 1 and N
assert((zipf_value >=1) && (zipf_value <= n));
return(zipf_value);
}
int get_new_value_from_table(double* table, int table_size) {
double r = (double) rand() / ((double)RAND_MAX+1.);
int i = 0;
while (r > table[i] && r < table_size) {
i++;
}
if(r==table_size){
return -1;
}
else{
return i;
}
}
#endif
/**
* Set default values for global variables
*/
void init_parameters() {
/* Default values for client configuration */
host = "localhost";
port = 8080;
nb_clients = 0;
duration = 0;
nb_msg_per_connection = 0;
delay = 0;
/* SOCKETS DEFAULT VALUES */
// if 1 = naggle is disable, if 0 = naggle is enable
tcp_no_delay = 1;
// if 1 = bind can reuse local adresses
reuse_addr = 1;
// Initalizing linger
linger.l_onoff = 1;
/*0 = off (l_linger ignored), nonzero = on */
linger.l_linger =0;
/*0 = discard data, nonzero = wait for data sent */
//buffers for socket inputs/outputs
sendwin = OPT_USE_DEFAULT_SOCK_BUF_SIZE;
rcvwin = OPT_USE_DEFAULT_SOCK_BUF_SIZE;
#if SPECWEB99_FILE_ACCESS_PATTERN
/* Specweb initialization */
num_dirs = (25 + (((400000.0 / 122000.0) * SPECWEB99_LOAD)/5.0));
// class freq and file order have already been initialized
dir_zipf = (double*) malloc(sizeof(double)*num_dirs);
assert(dir_zipf!=NULL);
setupZipf(dir_zipf,num_dirs);
/* Initialisation du générateur aléatoire*/
srand (time(NULL));
#elif SPECWEB05_FILE_ACCESS_PATTERN
/* Specweb initialization */
num_dirs = SPECWEB05_DIRSCALING * SPECWEB05_SIMULTANEOUS_SESSIONS;
/* Initialisation du générateur aléatoire*/
srand (0);
#endif
}
/**
* Init the socket of the distant server where load will go.
*/
void init_server_target(client_t* client, char *hostname, int port) {
struct in_addr *ip_addr;
//Init remote server struct
if (hostname) {
client->ent = gethostbyname(hostname);
if (client->ent == NULL) {
PANIC("lookup on server's name \"%s\" failed\n", hostname);
}
ip_addr = (struct in_addr *)(*(client->ent->h_addr_list));
client->soc_address.sin_family = AF_INET;
bcopy(ip_addr, &(client->soc_address.sin_addr), sizeof(struct in_addr));
}
if (!client->ent) {
if (hostname){
PANIC("error - didn't get host info for %s\n", hostname);
}
else{
PANIC("error - never called gethostbyname\n");
}
}
if (port)
client->soc_address.sin_port = htons(port);
}
/**
* Change the stae of a client
* This function protects client state manipulation
*/
void change_state(client_t *client, states_t new_state) {
DEBUG("client %d change state %d to %d at %s\n", client->number, client->state, new_state, getCurrentTime());
DEBUG("Changing state for client %d from ",client->number);
switch (client->state) {
case ST_READING:
DEBUG("ST_READING to ");
break;
case ST_WRITING:
DEBUG("ST_WRITING to ");
break;
case ST_CONNECT:
DEBUG("ST_CONNECT to ");
break;
default: //ST_ENDED, ST_WAITING
PANIC("%s() l.%d: Should not happen.\n", __FUNCTION__, __LINE__);
}
client->state = new_state;
switch (new_state) {
case ST_READING:
DEBUG("ST_READING\n");
break;
case ST_WRITING:
DEBUG("ST_WRITING\n");
// Reinitializing values
memset(client->read_hdr_buf,'\0',MAX_HDR_LENGTH);
client->bytes_read = 0;
client->bytes_write = 0;
client->content_length = 0;
client->header_length = 0;
break;
case ST_CONNECT:
DEBUG("ST_CONNECT\n");
// Reinitializing values
client->nbRequestsOnIteration = 0;
client->fd = -1;
client->bytes_read = 0;
client->bytes_write = 0;
client->content_length = 0;
client->header_length = 0;
break;
case ST_WAITING:
DEBUG("ST_WAITING\n");
//Nothing to do
break;
}
}
#define MULTIPLE_INTERFACES 0
#if MULTIPLE_INTERFACES
/*#define LOAD_BALANCING 1
static int current_target = 0;
#define NB_ITFS 20
static char* targets[NB_ITFS] = {
"192.168.20.100",
"192.168.21.100",
"192.168.22.100",
"192.168.23.100",
"192.168.24.100",
"192.168.25.100",
"192.168.26.100",
"192.168.27.100",
"192.168.28.100",
"192.168.29.100",
"192.168.30.100",
"192.168.31.100",
"192.168.32.100",
"192.168.33.100",
"192.168.34.100",
"192.168.35.100",
"192.168.36.100",
"192.168.37.100",
"192.168.38.100",
"192.168.39.100",
};*/
/*#define LOAD_BALANCING 0
static int current_target = 0;
#define NB_ITFS 6
static char* targets[NB_ITFS] = {
"192.168.20.100",
"192.168.21.100",
"192.168.22.100",
"192.168.23.100",
"192.168.24.100",
"192.168.25.100",
};*/
#define LOAD_BALANCING 0
static int current_target = 0;
#define NB_ITFS 20
static char* targets[NB_ITFS] = {
"192.168.20.100",
"192.168.21.100",
"192.168.22.100",
"192.168.23.100",
"192.168.24.100",
"192.168.25.100",
"192.168.26.100",
"192.168.27.100",
"192.168.28.100",
"192.168.29.100",
"192.168.30.100",
"192.168.31.100",
"192.168.32.100",
"192.168.33.100",
"192.168.34.100",
"192.168.35.100",
"192.168.36.100",
"192.168.37.100",
"192.168.38.100",
"192.168.39.100",
};
#if LOAD_BALANCING
static int interfaces_pending[NB_ITFS];
static int nbInit = 0;
#endif
#endif
void init_client_socket(client_t* client) {
#if MULTIPLE_INTERFACES
#if LOAD_BALANCING
int min = -1;
int itf = 0;
for(itf = 0; itf < NB_ITFS; itf++){
if(min < 0 || interfaces_pending[itf] < min) {
current_target = itf;
min = interfaces_pending[itf];
}
}
nbInit ++;
client->current_target = current_target;
interfaces_pending[current_target]++;
if(nbInit == 1000) {
for(itf = 0; itf < NB_ITFS; itf++){
printf("%s : %d\n", targets[itf], interfaces_pending[itf]);
}
}
#else
current_target = (current_target + 1) % (sizeof(targets)/sizeof(char*));
#endif
init_server_target(client, targets[current_target], port);
#endif
//Getting Socket
client->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client->fd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
assert(client->fd < FD_SETSIZE);
//Enables local address reuse
if (setsockopt(client->fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr))
< 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
#ifdef USE_RST
//Warning: this option make the client send a RST instead of a classic FIN on the tcp connection.
//This sends a RST because this option make the socket especially port available directly after a close() call.
//Do this only if we initiate the close
#if !CLOSE_AFTER_REQUEST
if (setsockopt(client->fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))
< 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
#endif //CLOSE_AFTER_REQUEST
#endif //USE_RST
#if NON_BLOCKING_SOCKET
// Set the socket in a non-blocking mode
setNonblocking(client->fd);
#endif
//Take CT start time
assert(gettimeofday(&client->CTstart,NULL) == 0);
//Take CRT start time
assert(gettimeofday(&client->CRTstart,NULL) == 0);
}
void add_client_to_master_write_set(client_t* client) {
FD_SET(client->fd, &master_connect_fds);
if( (client->fd) > max_connect_socket) {
max_connect_socket = client->fd;
}
}
void remove_client_from_master_write_set(client_t* client) {
FD_CLR(client->fd, &master_connect_fds);
if(max_connect_socket == client->fd) {
//finding new max num socket
int i;
for(i=max_connect_socket-1; i>=0;i--) {
if(FD_ISSET(i, &master_connect_fds)) {
max_connect_socket = i;
break;
}
}
if(i<0) {
//No socket to select
max_connect_socket = 0;
}
}
}
/**
* Connect in non-blcking mode a client to the server
*/
states_t initialize_connect_client_to_server(client_t* client) {
DEBUG("Call to %s\n",__FUNCTION__);
if (client->fd == -1) {
init_client_socket(client);
DEBUG("client %d first time try to connect at %s\n",
client->number,getCurrentTime());
}
int status = connect(client->fd, (struct sockaddr *)&client->soc_address, sizeof(client->soc_address));
int err= errno;
if ((status == -1) && (err != EINPROGRESS && err != EADDRNOTAVAIL && err != ECONNABORTED)) {
PRINT_ALERT("Client %d get an error for its connect. errno is %d (%s), status = %d\n",
client->number, err, strerror(err), status);
exit(EXIT_FAILURE);
}
else if (err == EADDRNOTAVAIL) {
PRINT_ALERT("No more free local port. Will try later\n");
return ST_CONNECT;
}
else if (err == ECONNABORTED) {
//http://www.wlug.org.nz/ECONNABORTED
PRINT_ALERT("Client %d get an error for its connect. errno is ECONNABORTED (nb_clients: %d)\n",
client->number, nb_clients);
return ST_CONNECT;
//exit(EXIT_FAILURE);
}
if(status == 0){
assert(0 && "Connect return 0 immediately ? (Should not happen but if so, we must add a finalize_client_connect.)");
// Connect OK
add_client_to_master_write_set(client);
// Updating client info
change_state(client, ST_WRITING);
return ST_WRITING;
}
else{
DEBUG("Initiate non blocking connect for client %d\n",client->number);
add_client_to_master_write_set(client);
client->connect_in_progress = 1;
return ST_CONNECT;
}
}
/* The signal SIGPIPE handler function */
void sigpipe_handler(int signal) {
/** IGNORE **/
PRINT_ALERT("ignoring SIGPIPE: %d\n", signal);
}
void sigterm_handler(int signal) {
printf("SIGTERM received. Exiting.\n");
exit(0*signal);
}
/**
* Finalize the connection of the client when a non-blocking connect completion
* was wait on select.
*/
states_t finalize_connect_client_to_server(client_t* client) {
DEBUG("Call to %s\n",__FUNCTION__);
//Handle connect select.
client->connect_in_progress = 0;
// Socket selected for write
socklen_t lon = sizeof(int);
int valopt;
if (getsockopt(client->fd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) {
PRINT_ALERT("Error for client %d in getsockopt() %d - %s\n", client->number, errno, strerror(errno)) ;
exit(0);
}
// Check the value returned...
if (valopt) {
switch (valopt) {
case ETIMEDOUT:
DEBUG("client %d connect ETIMEDOUT at %s\n", client->number, getCurrentTime());
return ST_CONNECT;
case ECONNREFUSED:
client->nb_connect_attempts ++;
if (client->nb_connect_attempts >= NB_CONNECT_ATTEMPTS_MAX) {
PANIC("To much ECONNREFUSED (%d). exiting.\n", NB_CONNECT_ATTEMPTS_MAX);
}
PRINT_ALERT("client %d ECONNREFUSED nb_connect_attempts: %d at %s\n", client->number,
client->nb_connect_attempts, getCurrentTime());
return ST_CONNECT;
/** Critical error cases **/
case EINPROGRESS:
case EALREADY:
default:
perror("connect");
PANIC("Client %d, error while trying to connect. Errno is %d (%s)\n",client->number,errno, strerror(valopt));
}
}
client->nb_connect_attempts = 0;
/* End of connect */
struct timeval stop_time;
assert(gettimeofday(&stop_time,NULL) == 0);
long double ct_time = (long double)compare_time(&(client->CTstart), &stop_time);
SET_IF_MIN(global_minCT, ct_time);
SET_IF_MAX(global_maxCT, ct_time);
global_avgCT += ct_time;
nb_CT++;
DEBUG("client %d CT: %lu at %s\n",
client->number,
compare_time(&(client->CTstart), &stop_time),
getCurrentTime());
DEBUG("Client %d is now connected\n",client->number);
struct sockaddr_in csin;
unsigned int size = sizeof(csin);
getsockname(client->fd, (struct sockaddr*)&csin, &size);
// A client will always use the same port number
client->port = ntohs(csin.sin_port);
/* Updating socket values */
// Disable Naggle algorithm ?
if (setsockopt(client->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&tcp_no_delay, sizeof(tcp_no_delay))
< 0) {
PRINT_ALERT("Error with fd %d\n", client->fd);
perror("setsockopt TCP_NODELAY");
}
//Set send buffer size
if (setsockopt(client->fd, SOL_SOCKET, SO_SNDBUF, &sendwin, sizeof(sendwin))
< 0) {
PRINT_ALERT("Error with fd %d\n", client->fd);
perror("setsockopt SO_SNDBUF");
}
//Set receive buffer size
if (setsockopt(client->fd, SOL_SOCKET, SO_RCVBUF, &rcvwin, sizeof(rcvwin))
< 0) {
PRINT_ALERT("Error with fd %d\n", client->fd);
perror("setsockopt SO_RCVBUF");
}
// Updating client info
change_state(client, ST_WRITING);
return ST_WRITING;
}
/**
* Choose an url (default or specweb-like distribution
*/
char * choose_url(__attribute__((unused)) client_t* client) {
char * url;
#if SPECWEB99_FILE_ACCESS_PATTERN
/* Use a specweb distribution */
int dir = zipf(dir_zipf);
int theclass = get_new_value_from_table((double*) class_freq, SPECWEB99_NB_CLASS);
int file = get_new_value_from_table((double *) file_freq, SPECWEB99_NB_FILES);
assert(theclass>=0 && theclass < SPECWEB99_NB_CLASS);
assert(file>=0 && file < SPECWEB99_NB_FILES);
// URL size must be less than 63 (+\0)
url = calloc(64,sizeof(char));
assert(url != NULL);
snprintf(url,63,"%sdir%05d/class%d_%d",DEFAULT_DIR,dir,theclass,file);
#elif SPECWEB05_FILE_ACCESS_PATTERN
double d = (double) rand () / ((double)RAND_MAX+1.);
int url_size = 128;
if(d < type_freq[0]){
url = calloc(url_size,sizeof(char));
assert(url != NULL);
int d2 = rand () % IMAGES_NB_FILES;
snprintf(url,url_size,"/support/images/%s",images_files[d2]);
}
else if(d < type_freq[1]){
url = calloc(url_size,sizeof(char));
assert(url != NULL);
int d2 = rand () % PHP_NB_FILES;
snprintf(url,url_size,"/support/%s",php_files[d2]);
}
else {
/* Use a specweb distribution */
int dir = zipf(SPECWEB05_ZIPF_ALPHA, num_dirs);
// TODO Check it
int class = get_new_value_from_table((double*) class_freq, SPECWEB05_NB_CLASS);
int file = get_new_value_from_table((double *) file_freq[class], SPECWEB05_MAX_FILE_PER_CLASS);
if(class == -1 || file == -1 || file_freq[class][file] == -1){
PANIC("Big bug in SpecWeb05\n");
}
// URL size must be less than 63 (+\0)
url = calloc(url_size,sizeof(char));
assert(url != NULL);
snprintf(url,url_size,"/support/downloads/dir%010d/download%d_%d",dir,class,file);
}
#elif UNIQUE_FILE_ACCESS_PATTERN
/* Use always the same url */
url = file;
#endif
DEBUG("[Client %d] URL : %s\n",url, client_no);
return url;
}
/**
* Build an http request
*/
void build_http_request(client_t* client) {
char* url = choose_url(client);
client->req_unique_id++;
int req_length = snprintf(client->request_buf, REQUEST_BUFFER_SIZE,
"GET %s HTTP/1.1\r\n"
//"Request unique id: %d-%u-%d\r\n"
"Host: %s\r\n"
"Accept: text/plain,text/html,*/*\r\n\r\n",
url,
//client->number, client->req_unique_id,my_hostname,
host);
#if !UNIQUE_FILE_ACCESS_PATTERN
free(url);
#endif
DEBUG("Client %d will send : %s", client->number, client->request_buf);
/* We accept all type of response */
client->request_total_len = req_length;
assert(client->request_total_len == strlen(client->request_buf));
assert(client->request_total_len < REQUEST_BUFFER_SIZE);
}
#define MIX_GET_SET 10 // X% of set ops
#define VALUE_SIZE 450 // Be careful with REQUEST_BUFFER_SIZE
#define HOW_MANY_KEYS 10000
void build_memcached_request(client_t* client) {
static int next_key = 0;
int req_length;
if(rand()%100 >= MIX_GET_SET) {
static char * get_requests[HOW_MANY_KEYS];
static int get_req_length[HOW_MANY_KEYS];
static int already_memset = 0;
if(!already_memset){
int i = 0;
for(i = 0; i < HOW_MANY_KEYS; i++){
int ret = asprintf(
&get_requests[i],
"get key%d\r\n",
i
);
if(ret == -1){
PRINT_ALERT("Error\n");
exit(-1);
}
get_req_length[i] = ret;
}
already_memset = 1;
}
req_length = get_req_length[next_key];
memcpy(client->request_buf, get_requests[next_key],req_length);
} else {
static char * set_requests[HOW_MANY_KEYS];
static int set_req_length[HOW_MANY_KEYS];
static int already_memset = 0;
if(!already_memset){
int i = 0;
for(i = 0; i < HOW_MANY_KEYS; i++){
int ret = asprintf(
&set_requests[i],
"set key%d 0 0 %d\r\n%*.*s\r\n",
i,
VALUE_SIZE, VALUE_SIZE, VALUE_SIZE,
"value");
if(ret == -1){
PRINT_ALERT("Error\n");
exit(-1);
}
set_req_length[i] = ret;
}
already_memset = 1;
}
req_length = set_req_length[next_key];
memcpy(client->request_buf, set_requests[next_key],req_length);
}
client->request_total_len = req_length;
next_key = (next_key+1)%HOW_MANY_KEYS;
DEBUG("Client %d will send : %s", client->number, client->request_buf);
}
/*
* note on is_in_last_loop_and_not_last_request param:
* if 0: first call
* if -1 recurssive call somewhere else
* if 1 recurssive call in last loop and not last request
*/
states_t treat_error(client_t* client) {
client->errors++;
DEBUG_TMP("Client %d on port %d get %lu error reading\n",client->number, client->port, client->errors);
// Free the outgoing port
int status = close(client->fd);
if(status==-1){
PANIC("Client %d on port %d get an error closing socket in treat error\n",client->number, client->port);
}
client->fd = -1;