-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseDependencyGraph.c
1631 lines (1371 loc) · 52.3 KB
/
parseDependencyGraph.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
/* The following program is
used to replay dependency
trees based page load process
in todays browser
Author: Mohammad Rajiullah
Date: April 2016
License: GNU General Public License v3
Developed for use by the EU H2020 NEAT project
*/
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <pthread.h>
#include <unistd.h>
#include <curl/curl.h>
#include <sys/time.h>
#include "cJSON.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/queue.h>
pthread_mutex_t thread_count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t thread_count_cv = PTHREAD_COND_INITIALIZER;
int thread_count = 0;
#define HTTP1 1
#define HTTP2 2
#define HTTPS 3
#define PHTTPGET 4
#define COOKIE_SIZE 512
#define NUM_HANDLES 1000
#define EASY_HANDLES 100
#define FIFO_BUFFER_SIZE 1024
#define STRING_BUFFER 1024
#define NANOSLEEP_MS_MULTIPLIER 1000000 // 1 millisecond = 1,000,000 Nanoseconds
#ifndef CURLPIPE_MULTIPLEX
/* This little trick will just make sure that we don't enable pipelining for
* libcurls old enough to not have this symbol. It is _not_ defined to zero in
* a recent libcurl header.
*/
#define CURLPIPE_MULTIPLEX 0
#endif
struct memory_chunk {
char *memory;
size_t size;
int enabled;
};
struct sctp_pipe_data {
uint32_t id;
uint32_t path_len;
uint32_t cookie_len;
uint32_t header_len;
uint32_t payload_len;
char path[FIFO_BUFFER_SIZE];
} __attribute__ ((packed));
struct phttpget_request {
pthread_mutex_t recv_mutex;
pthread_cond_t recv_cv;
uint8_t got_response;
struct sctp_pipe_data pipe_data;
TAILQ_ENTRY(phttpget_request) entries;
};
void createActivity(char *job_id);
int cJSON_HasArrayItem(cJSON *array, const char *string);
void onComplete(cJSON *obj_name);
int debug = 0;
int total_download_request_from_input=0;
double page_load_time = 0.0;
unsigned long page_size = 0;
int json_output = 1;
int object_count = 0;
int first_object = 0;
char *cookie_string;
int cookie_size = COOKIE_SIZE;
char *server = NULL;
char *testfile = NULL;
cJSON *json = NULL;
cJSON *this_objs_array = NULL;
cJSON *this_acts_array = NULL;
cJSON *map_start = NULL;
cJSON *map_complete = NULL;
cJSON *json_for_output=NULL;
cJSON *download_size=NULL;
cJSON *temp_download_size=NULL;
struct timeval start;
pthread_mutex_t lock;
void *curl_hnd[NUM_HANDLES];
CURL *easy[NUM_HANDLES];
CURLM *multi_handle = NULL;
int num_transfers = 0;
int protocol = HTTP1;
int transport_protocol = IPPROTO_TCP;
int request_count = 0;
int max_con = 6;
CURL *easyh1[EASY_HANDLES];
//CURL *easyh1;
int *worker_status;
//int worker_status[max_con] = {0, 0, 0, 0, 0, 0};
/* PHTTPGET STUFF BEGIN */
int fifo_in_fd = -1;
int fifo_out_fd = -1;
char *fifo_in_name = "/tmp/phttpget-out";
char *fifo_out_name = "/tmp/phttpget-in";
pthread_t phttpget_recv_thread;
pthread_t phttpget_program_thread;
uint32_t phttpget_request_counter = 0;
uint32_t phttpget_response_counter = 0;
pthread_mutex_t phttpget_write_mutex = PTHREAD_MUTEX_INITIALIZER;
TAILQ_HEAD(phttpget_request_queue, phttpget_request);
struct phttpget_request_queue phttpget_requests_pending;
/* PHTTPGET STUFF END */
/* a handle to number lookup, highly ineffective when we do many
transfers... */
static int hnd2num(CURL *hnd)
{
int i;
for(i=0; i< num_transfers; i++) {
if(curl_hnd[i] == hnd)
return i;
}
return 0; /* weird, but just a fail-safe */
}
static
void dump(const char *text, int num, unsigned char *ptr, size_t size,
char nohex)
{
size_t i;
size_t c;
unsigned int width=0x10;
if(nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
num, text, (long)size, (long)size);
for(i=0; i<size; i+= width) {
fprintf(stderr, "%4.4lx: ", (long)i);
if(!nohex) {
/* hex not disabled, show it */
for(c = 0; c < width; c++)
if(i+c < size)
fprintf(stderr, "%02x ", ptr[i+c]);
else
fputs(" ", stderr);
}
for(c = 0; (c < width) && (i+c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
i+=(c+2-width);
break;
}
fprintf(stderr, "%c",
(ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
i+=(c+3-width);
break;
}
}
fputc('\n', stderr); /* newline */
}
}
static
int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp)
{
const char *text;
int num = hnd2num(handle);
(void)handle; /* prevent compiler warning */
(void)userp;
switch(type) {
case CURLINFO_TEXT:
fprintf(stderr, "== %d Info: %s", num, data);
default: /* in case a new one is introduced to shock us */
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
}
dump(text, num, (unsigned char *)data, size, 1);
return 0;
}
static size_t
memory_callback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct memory_chunk *chunk = userp;
if (!chunk->enabled) {
return realsize;
}
chunk->memory = realloc(chunk->memory, chunk->size + realsize + 1);
if (chunk->memory == NULL) {
perror("realloc");
return 0;
}
memcpy(&(chunk->memory[chunk->size]), contents, realsize);
chunk->size += realsize;
chunk->memory[chunk->size] = 0;
return realsize;
}
int
init_worker()
{
int i;
int res;
for (i = 0; i < max_con; i++) {
easyh1[i] = curl_easy_init();
if (!easyh1[i]) {
return -1;
}
if ((res = curl_easy_setopt(easyh1[i], CURLOPT_TCP_NODELAY, 1L)) != CURLE_OK) {
fprintf(stderr, "cURL option error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
/* some servers don't like requests that are made without a user-agent
* field, so we provide one
*/
if ((res = curl_easy_setopt(easyh1[i], CURLOPT_USERAGENT, "pReplay/0.1")) != CURLE_OK) {
fprintf(stderr, "cURL option error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
}
return 0;
}
int
init_tls_worker()
{
int i;
int res;
for (i = 0; i < max_con; i++) {
easyh1[i] = curl_easy_init();
if (!easyh1[i]) {
return -1;
}
if ((res = curl_easy_setopt(easyh1[i], CURLOPT_TCP_NODELAY, 1L)) != CURLE_OK ||
(res = curl_easy_setopt(easyh1[i], CURLOPT_HEADER, 0L)) != CURLE_OK ||
(res = curl_easy_setopt(easyh1[i], CURLOPT_SSL_VERIFYPEER, 0L)) != CURLE_OK ||
(res = curl_easy_setopt(easyh1[i], CURLOPT_SSL_VERIFYHOST, 0L)) != CURLE_OK) {
fprintf(stderr, "cURL option error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
/* some servers don't like requests that are made without a user-agent
* field, so we provide one */
if ((res = curl_easy_setopt(easyh1[i], CURLOPT_USERAGENT, "pReplay/0.1")) != CURLE_OK) {
fprintf(stderr, "cURL option error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
}
return 0;
}
void *
run_worker(void *arg)
{
cJSON *obj_name = arg;
char url[400];
int j = cJSON_HasArrayItem(this_objs_array, cJSON_GetObjectItem(obj_name, "obj_id")->valuestring);
int i = 0;
int idle_worker_found = 0;
cJSON *obj = NULL;
cJSON *this_obj = NULL;
struct memory_chunk chunk;
int res = 0;
double bytes = 0;
long total_bytes = 0;
long header_bytes = 0;
double transfer_time = 0;
double end_time = 0;
struct timeval ts;
struct timeval te;
double time_in_mill;
if (j != -1){
obj = cJSON_GetArrayItem(this_objs_array, j);
this_obj= cJSON_GetObjectItem(obj, cJSON_GetObjectItem(obj_name, "obj_id")->valuestring);
if (protocol==HTTP1) {
snprintf(url, sizeof(url), "%s%s%s", "http://", server, cJSON_GetObjectItem(this_obj, "path")->valuestring);
} else if (protocol==HTTPS) {
snprintf(url, sizeof(url), "%s%s%s", "https://", server, cJSON_GetObjectItem(this_obj, "path")->valuestring);
}
while (1) {
pthread_mutex_lock(&lock);
for (i = 0; i < max_con; i++) {
if (worker_status[i] == 0){
idle_worker_found = 1;
worker_status[i] = 1;
break;
}
}
pthread_mutex_unlock(&lock);
if (idle_worker_found == 1) {
break;
}
}
gettimeofday(&ts,NULL);
if ((res = curl_easy_setopt(easyh1[i], CURLOPT_WRITEFUNCTION, memory_callback)) != CURLE_OK) {
fprintf(stderr, "cURL option error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
if ((res = curl_easy_setopt(easyh1[i], CURLOPT_WRITEDATA, (void *)&chunk)) != CURLE_OK) {
fprintf(stderr, "cURL option error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
chunk.memory = NULL;
chunk.size = 0;
chunk.enabled = 0;
//char *url1="http://example.com";
curl_easy_setopt(easyh1[i], CURLOPT_URL, url);
//curl_easy_setopt(easyh1[i], CURLOPT_COOKIEFILE, "cookie.tmp");
curl_easy_setopt(easyh1[i], CURLOPT_COOKIE, cookie_string);
curl_easy_setopt(easyh1[i], CURLOPT_PRIVATE, url);
chunk.size = 0;
if ((res=curl_easy_perform(easyh1[i])) != CURLE_OK){
//fprintf(stderr, "i= %d\n", i);
perror("Curl error");
exit(EXIT_FAILURE);
}
if ((res = curl_easy_getinfo(easyh1[i], CURLINFO_SIZE_DOWNLOAD, &bytes)) != CURLE_OK ||
(res = curl_easy_getinfo(easyh1[i], CURLINFO_HEADER_SIZE, &header_bytes)) != CURLE_OK ||
(res = curl_easy_getinfo(easyh1[i], CURLINFO_TOTAL_TIME, &transfer_time)) != CURLE_OK )
{
fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
worker_status[i] = 0;
gettimeofday(&te,NULL);
pthread_mutex_lock(&lock);
page_load_time=end_time=((te.tv_sec-start.tv_sec)*1000+(double)(te.tv_usec-start.tv_usec)/1000);
total_bytes=(long)bytes+header_bytes;
page_size+=(long)bytes;
object_count++;
if (json_output == 1) {
if (first_object == 0) {
//printf("{\"S\":%ld,\"T\":%f}",total_bytes, transfer_time);
cJSON_AddItemToArray(download_size,temp_download_size=cJSON_CreateObject());
time_in_mill = (ts.tv_sec) * 1000 + (ts.tv_usec) / 1000 ;
cJSON_AddNumberToObject(temp_download_size,"S",time_in_mill);
cJSON_AddNumberToObject(temp_download_size,"Sz",total_bytes);
cJSON_AddNumberToObject(temp_download_size,"T",transfer_time);
first_object = 1;
} else {
cJSON_AddItemToArray(download_size,temp_download_size=cJSON_CreateObject());
time_in_mill = (ts.tv_sec) * 1000 + (ts.tv_usec) / 1000 ;
cJSON_AddNumberToObject(temp_download_size,"S",time_in_mill);
cJSON_AddNumberToObject(temp_download_size,"Sz",total_bytes);
cJSON_AddNumberToObject(temp_download_size,"T",transfer_time);
// printf(",{\"S\":%ld,\"T\":%f}",total_bytes, transfer_time);
}
}
pthread_mutex_unlock(&lock);
if (debug == 1) {
fprintf(stderr, "[%f] Object_size: %ld, transfer_time: %f\n",
end_time,
(long)bytes + header_bytes,
transfer_time);
}
onComplete(obj_name);
pthread_mutex_lock(&thread_count_mutex);
thread_count--;
/*if (thread_count==0){
printf("BECOME 0 in run_worker\n");
fflush(stdout);
}*/
pthread_cond_signal(&thread_count_cv);
pthread_mutex_unlock(&thread_count_mutex);
}
return 0;
}
/*
check the total number of
parallel connections
*/
int
global_array_sum()
{
int i = 0;
int sum = 0;
for (i = 0; i < max_con; i++){
sum += worker_status[i];
}
return sum;
}
/* a handle to number lookup, highly ineffective when we do many
transfers... */
/*static int
hnd2num(CURL *hnd)
{
int i;
for (i=0; i< num_transfers; i++){
if (curl_hnd[i] == hnd) {
return i;
}
}*/
// return 0; /* weird, but just a fail-safe */
//}
void *
phttpget_start_programm() {
char phttpget_argument[STRING_BUFFER];
int phttpget_status = 0;
/* try to run phttpget in background */
snprintf(phttpget_argument, STRING_BUFFER, "HTTP_PIPE=YES HTTP_SCTP_MAX_STREAMS=%d ./phttpget %s > phttpget.log 2>&1", max_con, server);
//snprintf(phttpget_argument, STRING_BUFFER, "HTTP_PIPE=YES ./phttpget %s", server);
//fprintf(stderr, "%s\n", phttpget_argument);
phttpget_status = system(phttpget_argument);
fprintf(stderr, "phttpget terminated .... should not see me! - status : %d\n", phttpget_status);
exit(EXIT_FAILURE);
return 0;
}
/*
** read from incoming pipe and notify coresponding thread
*/
void *
phttpget_recv_handler()
{
struct phttpget_request *request = NULL;
struct sctp_pipe_data pipe_data_temp;
uint8_t found = 0;
ssize_t len = 0;
ssize_t len_left = 0;
char *bufptr = NULL;
if (debug) {
fprintf(stderr, "[%d][%s] - receiver thread started...\n", __LINE__, __func__);
}
while (1) {
len_left = sizeof(struct sctp_pipe_data);
bufptr = (char *) &pipe_data_temp;
while (len_left > 0) {
len = read(fifo_in_fd, bufptr + sizeof(struct sctp_pipe_data) - len_left, len_left);
if (len == 0 || len == -1) {
fprintf(stderr, "[%d][%s] - read failed\n", __LINE__, __func__);
exit(EXIT_FAILURE);
}
len_left -= len;
}
/* search for thread */
found = 0;
TAILQ_FOREACH(request, &phttpget_requests_pending, entries) {
if (request->pipe_data.id == pipe_data_temp.id) {
found = 1;
break;
}
}
/* thread not found - this should not happen ... */
if (!found) {
fprintf(stderr, "[%d][%s] - request %d (%s) not found - fix logic!!\n", __LINE__, __func__, pipe_data_temp.id, pipe_data_temp.path);
exit(EXIT_FAILURE);
}
/* copy pipe_data */
/* felix : ugly - todo! ... */
memcpy(&(request->pipe_data), &pipe_data_temp, sizeof(struct sctp_pipe_data));
/*if (debug && !json_output) {
fprintf(stderr, "handling response %d - %s\n", request->pipe_data.id, request->pipe_data.path);
}*/
/* notify waiting thread */
pthread_mutex_lock(&(request->recv_mutex));
request->got_response = 1;
pthread_cond_signal(&(request->recv_cv));
pthread_mutex_unlock(&(request->recv_mutex));
}
if (debug) {
fprintf(stderr, "[%d][%s] - read failed - read retured %d (%s)\n", __LINE__, __func__, errno, strerror(errno));
}
return 0;
}
/*
** request an url via phttpget pipe
*/
void *
phttpget_request_url(void *arg)
{
cJSON *obj_name = NULL;
int i = 0;
struct timeval te;
struct timeval time_request;
struct timeval time_response;
double end_time = 0.0;
long total_bytes = 0;
double transfer_time = 0.0;
struct phttpget_request *request = NULL;
cJSON *obj = NULL;
cJSON *this_obj = NULL;
ssize_t len = 0;
ssize_t len_left = 0;
char *bufptr = NULL;
obj_name = arg;
i = cJSON_HasArrayItem(this_objs_array, cJSON_GetObjectItem(obj_name, "obj_id")->valuestring);
if (i != -1) {
obj = cJSON_GetArrayItem(this_objs_array, i);
this_obj = cJSON_GetObjectItem(obj, cJSON_GetObjectItem(obj_name, "obj_id")->valuestring);
/* prepare request and enqueue to queue */
if ((request = malloc(sizeof(struct phttpget_request))) == NULL) {
fprintf(stderr, "[%d][%s] - malloc failed\n", __LINE__, __func__);
exit(EXIT_FAILURE);
}
memset(request, 0, sizeof(struct phttpget_request));
/* initialize mutex for this thread */
pthread_mutex_init(&(request->recv_mutex), NULL);
pthread_cond_init(&(request->recv_cv), NULL);
request->got_response = 0;
/* fill pipe data */
/* write url to buffer and save length */
request->pipe_data.path_len = snprintf(
request->pipe_data.path,
FIFO_BUFFER_SIZE,
"%s",
cJSON_GetObjectItem(this_obj, "path")->valuestring
);
/* check pathlen */
if (request->pipe_data.path_len <= 0) {
fprintf(stderr, "[%d][%s] - pathlen <= 0\n", __LINE__, __func__);
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&phttpget_write_mutex);
phttpget_request_counter++;
request->pipe_data.id = phttpget_request_counter;
request->pipe_data.cookie_len = cookie_size;
/* enqueue request */
TAILQ_INSERT_TAIL(&phttpget_requests_pending, request, entries);
/* write request to pipe */
len_left = sizeof(struct sctp_pipe_data);
bufptr = (char *) &(request->pipe_data);
while (len_left > 0) {
len = write(fifo_out_fd, bufptr + sizeof(struct sctp_pipe_data) - len_left, len_left);
if (len == -1 || len == 0) {
fprintf(stderr, "[%d][%s] - write failed\n", __LINE__, __func__);
//exit(EXIT_FAILURE);
break;
}
len_left -= len;
}
gettimeofday(&time_request, 0);
pthread_mutex_unlock(&phttpget_write_mutex);
/*
fprintf(stderr, "[%d][%s] - requesting %d : %s\n", __LINE__, __func__, request->pipe_data.id, request->pipe_data.path);
*/
/* wait for receiver thread */
pthread_mutex_lock(&(request->recv_mutex));
while (!request->got_response) {
pthread_cond_wait(&(request->recv_cv), &(request->recv_mutex));
}
pthread_mutex_unlock(&(request->recv_mutex));
gettimeofday(&time_response, 0);
transfer_time = ((time_response.tv_sec - time_request.tv_sec) * 1000 + (double)(time_response.tv_usec - time_request.tv_usec) / 1000);
phttpget_response_counter++;
/*
if (debug && !json_output) {
printf("####### PHTTPGET\n");
printf("response: %d\n", phttpget_response_counter);
printf("id : %d\n", request->pipe_data.id);
printf("payload : %d\n", request->pipe_data.size_payload);
printf("header : %d\n", request->pipe_data.size_header);
}
*/
/* cleanup and statistics */
pthread_mutex_destroy(&(request->recv_mutex));
pthread_cond_destroy(&(request->recv_cv));
gettimeofday(&te, NULL);
pthread_mutex_lock(&lock);
page_load_time = end_time= ((te.tv_sec - start.tv_sec) * 1000 + (double)(te.tv_usec - start.tv_usec) / 1000);
total_bytes = request->pipe_data.payload_len + request->pipe_data.header_len;
page_size += request->pipe_data.payload_len;
TAILQ_REMOVE(&phttpget_requests_pending, request, entries);
object_count++;
if (json_output == 1) {
if (first_object == 0) {
first_object = 1;
}
cJSON_AddItemToArray(download_size,temp_download_size = cJSON_CreateObject());
cJSON_AddNumberToObject(temp_download_size,"S", time_request.tv_sec * 1000 + time_request.tv_usec / 1000);
cJSON_AddNumberToObject(temp_download_size,"Sz", total_bytes);
cJSON_AddNumberToObject(temp_download_size,"T", transfer_time);
}
if (debug) {
fprintf(stderr,"[%f] Object_size: %u, transfer_time: %f\n", end_time, request->pipe_data.payload_len + request->pipe_data.header_len, transfer_time);
}
free(request);
pthread_mutex_unlock(&lock);
onComplete(obj_name);
/* reduce thread_count and signal finish */
pthread_mutex_lock(&thread_count_mutex);
thread_count--;
pthread_cond_signal(&thread_count_cv);
pthread_mutex_unlock(&thread_count_mutex);
} else {
if (debug) {
fprintf(stderr, "[%d][%s] - object not found - fix file!!!...\n", __LINE__, __func__);
exit(EXIT_FAILURE);
}
}
return 0;
}
void *
request_url(void *arg)
{
cJSON *obj_name = arg;
char url[400];
int i = cJSON_HasArrayItem(this_objs_array, cJSON_GetObjectItem(obj_name, "obj_id")->valuestring);
struct timeval te;
double end_time;
if (i != -1){
cJSON *obj= cJSON_GetArrayItem(this_objs_array, i);
cJSON * this_obj= cJSON_GetObjectItem(obj, cJSON_GetObjectItem(obj_name, "obj_id")->valuestring);
int still_running = 0; /* keep number of running handles */
CURL *eh;
FILE *out;
char filename[128];
int num=0;
double bytes = 0.0;
long total_bytes = 0;
long header_bytes = 0;
double transfer_time = 0.0;
int res = 0;
snprintf(url, sizeof url,"%s%s%s%s","http://", server, ":8000",cJSON_GetObjectItem(this_obj,"path")->valuestring);
//if (debug==1 && json_output==0)
//printf("URL: %s\n",url);
//printf("when_comp_start--: %d\n",cJSON_GetObjectItem(this_obj,"when_comp_start")->valueint);
gettimeofday(&te, NULL);
end_time = ((te.tv_sec - start.tv_sec) * 1000 + (double)(te.tv_usec - start.tv_usec) / 1000);
if (debug) {
fprintf(stderr,"[%f] URL: %s\n", end_time, url);
}
eh = curl_easy_init();
/* set options */
snprintf(filename, 128, "dl-%d", num);
out = fopen(filename, "wb");
// write to this file
curl_easy_setopt(eh, CURLOPT_WRITEDATA, out);
/* send all data to this function */
//curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_data);
/*Nagle off please */
curl_easy_setopt(eh, CURLOPT_TCP_NODELAY, 1L);
/* set the same URL */
curl_easy_setopt(eh, CURLOPT_URL, url);
/* send it verbose for max debuggaility */
curl_easy_setopt(eh, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(eh, CURLOPT_DEBUGFUNCTION, my_trace);
/* HTTP/2 please */
curl_easy_setopt(eh, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
/* we use a self-signed test server, skip verification during debugging */
curl_easy_setopt(eh, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(eh, CURLOPT_SSL_VERIFYHOST, 0L);
#if (CURLPIPE_MULTIPLEX > 0)
/* wait for pipe connection to confirm */
curl_easy_setopt(eh, CURLOPT_PIPEWAIT, 1L);
#endif
/* add the individual transfer */
curl_multi_add_handle(multi_handle, eh);
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
pthread_mutex_lock(&lock);
/* we start some action by calling perform right away */
curl_multi_perform(multi_handle, &still_running);
pthread_mutex_unlock(&lock);
do {
struct timeval timeout;
int rc; /* select() return code */
CURLMcode mc; /* curl_multi_fdset() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
long curl_timeo = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(multi_handle, &curl_timeo);
if (curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if (timeout.tv_sec > 1) {
timeout.tv_sec = 1;
} else {
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
}
/* get file descriptors from the transfers */
mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
if (mc != CURLM_OK) {
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
break;
}
/* On success the value of maxfd is guaranteed to be >= -1. We call
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
to sleep 100ms, which is the minimum suggested value in the
curl_multi_fdset() doc. */
if (maxfd == -1) {
#ifdef _WIN32
Sleep(100);
rc = 0;
#else
/* Portable sleep for platforms other than Windows. */
struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
rc = select(0, NULL, NULL, NULL, &wait);
#endif
} else {
/* Note that on some platforms 'timeout' may be modified by select().
If you need access to the original value save a copy beforehand. */
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
}
switch (rc) {
case -1:
/* select error */
break;
case 0:
default:
/* timeout or readable/writable sockets */
pthread_mutex_lock(&lock);
curl_multi_perform(multi_handle, &still_running);
pthread_mutex_unlock(&lock);
break;
}
} while (still_running);
if ((res = curl_easy_getinfo(eh, CURLINFO_SIZE_DOWNLOAD, &bytes)) != CURLE_OK ||
(res = curl_easy_getinfo(eh, CURLINFO_HEADER_SIZE, &header_bytes)) != CURLE_OK ||
(res = curl_easy_getinfo(eh, CURLINFO_TOTAL_TIME, &transfer_time)) != CURLE_OK ) {
fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res));
}
gettimeofday(&te, NULL);
pthread_mutex_lock(&lock);
page_load_time = end_time= ((te.tv_sec - start.tv_sec) * 1000 + (double)(te.tv_usec - start.tv_usec) / 1000);
total_bytes = (long)bytes + header_bytes;
page_size += (long)bytes;
object_count++;
pthread_mutex_unlock(&lock);
if (json_output == 1) {
if (first_object == 0) {
//printf("{\"S\":%ld,\"T\":%f}",total_bytes, transfer_time);
cJSON_AddItemToArray(download_size,temp_download_size=cJSON_CreateObject());
cJSON_AddNumberToObject(temp_download_size,"S",total_bytes);
cJSON_AddNumberToObject(temp_download_size,"T",transfer_time);
first_object = 1;
} else {
cJSON_AddItemToArray(download_size,temp_download_size=cJSON_CreateObject());
cJSON_AddNumberToObject(temp_download_size,"S",total_bytes);
cJSON_AddNumberToObject(temp_download_size,"T",transfer_time);
// printf(",{\"S\":%ld,\"T\":%f}",total_bytes, transfer_time);
}
}
if (debug) {
fprintf(stderr,"[%f] Object_size: %ld, transfer_time: %f\n", end_time, (long)bytes+header_bytes, transfer_time);
}
onComplete(obj_name);
pthread_mutex_lock(&thread_count_mutex);
thread_count--;
/*if (thread_count==0){
printf("BECOME 0 in request_url\n");
fflush(stdout);
}*/
pthread_cond_signal(&thread_count_cv);
pthread_mutex_unlock(&thread_count_mutex);
}
return 0;
}
int
cJSON_HasArrayItem(cJSON *array, const char *string)
{
cJSON *obj = NULL;
int i;
for (i = 0; i < cJSON_GetArraySize(array); i++) {
obj = cJSON_GetArrayItem(array, i);
if (cJSON_HasObjectItem(obj, string)) {
return i;
}
}
return -1;
}
int
checkDependedActivities(char *id)
{
int is_all_complete = 1;
int i = cJSON_HasArrayItem(this_acts_array, id);
int j = 0;
if (i != -1){
cJSON * objs = cJSON_GetArrayItem(this_acts_array, i);
cJSON * obj = cJSON_GetObjectItem(objs, id);
cJSON *deps = cJSON_GetObjectItem(obj, "deps");
for (j = 0; j < cJSON_GetArraySize(deps); j++){
cJSON * dep = cJSON_GetArrayItem(deps, j);
if (cJSON_GetObjectItem(dep, "time")->valueint < 0) {
if (cJSON_HasObjectItem(map_complete, cJSON_GetObjectItem(dep, "id")->valuestring)) {
if (cJSON_GetObjectItem(map_complete, cJSON_GetObjectItem(dep, "id")->valuestring)->valueint != 1) {
is_all_complete = 0;
break;
}
} else {
is_all_complete = 0;
break;
}
} else {
if (cJSON_HasObjectItem(map_start, cJSON_GetObjectItem(dep, "id")->valuestring)) {
if (cJSON_GetObjectItem(map_start, cJSON_GetObjectItem(dep, "id")->valuestring)->valueint != 1) {
is_all_complete=0;
break;
}
} else {
is_all_complete = 0;
break;
}
}
}
}
return is_all_complete;
}
void
onComplete(cJSON *obj_name)
{
struct timeval te;
int i = 0;
gettimeofday(&te, NULL);
double end_time = ((te.tv_sec - start.tv_sec) * 1000 + (double)(te.tv_usec - start.tv_usec) / 1000);
pthread_mutex_lock(&lock);
page_load_time=end_time=((te.tv_sec-start.tv_sec)*1000+(double)(te.tv_usec-start.tv_usec)/1000);
pthread_mutex_unlock(&lock);
cJSON_AddNumberToObject(obj_name, "ts_e", end_time);
if (debug) {
fprintf(stderr,"=== [onComplete][%f] {\"id\":%s,\"type\":%s,\"is_started\":%d,\"ts_s\":%f,\"ts_e\":%f}\n",
end_time,
cJSON_GetObjectItem(obj_name,"id")->valuestring,
cJSON_GetObjectItem(obj_name,"type")->valuestring,
cJSON_GetObjectItem(obj_name,"is_started")->valueint,
cJSON_GetObjectItem(obj_name,"ts_s")->valuedouble,
cJSON_GetObjectItem(obj_name,"ts_e")->valuedouble);
}
pthread_mutex_lock(&lock);