-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcthreads.c
More file actions
161 lines (132 loc) · 3.72 KB
/
cthreads.c
File metadata and controls
161 lines (132 loc) · 3.72 KB
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
#include "include.h"
extern int errno;
static const char * const restrict fourzerofour= "HTTP/1.0 404 Not Found\x0d\x0a";
static const char * const restrict fivezeroone= "HTTP/1.0 501 Not Implemented\x0d\x0a";
static const char * const restrict twozerozero= "HTTP/1.0 200 OK\x0d\x0a";
static const char * const restrict get = "GET";
static const char * const restrict htv = " HTTP/1.0\x0d\x0a\x0d\x0a";
void * ct_thread(void* args) {
char * buffer, * ptr, * s_name;
int c, i, done, num_files, s_port, s_socket, p_port;
long int r, h_addr;
long long int slept = 0, rb;
size_t sz;
size_t BUFFER_SIZE = 5000;
struct ct_args_t * restrict params = (struct ct_args_t *) args;
struct ct_stats_t * restrict stats;
struct drand48_data state;
struct hostent* s_info;
struct list_t * restrict file_list;
struct node_t * restrict file;
struct sockaddr_in s_addr;
struct timeval rs1, rs2, ts1, ts2, fs1, fs2, ds;
buffer = (char *) calloc(sizeof(char), BUFFER_SIZE);
done = params->done;
file_list = params->file_list;
num_files = file_list->size;
s_info = params->s_info;
s_port = params->s_port;
p_port = params->p_port;
s_name = params->s_name;
stats = params->stats;
stats->rtimes = (int *) malloc(sizeof(int) * done);
stats->ftimes = (int *) malloc(sizeof(int) * done);
stats->dtimes = (int *) malloc(sizeof(int) * done);
stats->rate = (double *) malloc(sizeof(double) * done);
assert(stats->rtimes);
assert(stats->ftimes);
assert(stats->dtimes);
assert(stats->rate);
for(i = 0; i < done; ++i) {
stats->rtimes[i] = 0;
stats->ftimes[i] = 0;
stats->dtimes[i] = 0;
}
stats->tr = 0;
memcpy(&h_addr, s_info->h_addr, s_info->h_length);
s_addr.sin_addr.s_addr = h_addr;
s_addr.sin_port=htons(s_port);
s_addr.sin_family=AF_INET;
srand48_r(pthread_self(), &state);
gettimeofday(&fs1, NULL);
for(i = 0; i < done; ++i) {
gettimeofday(&ts1, NULL);
s_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
lrand48_r(&state, &r);
r = (int)(r % 100);
r *= 10000;
//usleep(r);
rb = r;
slept += rb;
lrand48_r(&state, &r);
r = (int)(r % num_files);
file = getval_n(file_list, r);
gettimeofday(&rs1, NULL);
if(connect(s_socket, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
perror("Could not connect to server");
close(s_socket);
continue;
}
ptr = (char *) file->data;
if(s_name)
sz = sprintf(buffer, "%s http://%s:%d%s %s", get, s_name, p_port, ptr, htv);
else
sz = sprintf(buffer, "%s %s %s", get, ptr, htv);
c = s_data(s_socket, buffer, sz);
#ifndef NDEBUG
printf("%d ", c);
fflush(stdout);
#endif
c = 0;
gettimeofday(&ds, NULL);
c = r_data_tv_c(s_socket, &buffer, &BUFFER_SIZE, &rs2);
stats->tr += c;
#ifndef NDEBUG
printf("%d\n", c);
fflush(stdout);
#endif
gettimeofday(&ts2, NULL);
if(c > 3) {
ptr = strstr(buffer, "200");
if(!ptr) {
ptr = strstr(buffer, "401");
if(ptr) {
++stats->BAD;
goto skip;
}
}
else
++stats->OK;
if(!ptr) {
ptr = strstr(buffer, "404");
if(ptr) {
++stats->FOUND;
goto skip;
}
}
if(!ptr) {
ptr = strstr(buffer, "501");
if(ptr) {
++stats->IMPL;
goto skip;
}
}
}
skip:
close(s_socket);
stats->rtimes[i] = (rs2.tv_sec - rs1.tv_sec) * 1000 * 1000;
stats->rtimes[i] += (rs2.tv_usec - rs1.tv_usec);
stats->ftimes[i] = (ts2.tv_sec - ts1.tv_sec) * 1000 * 1000;
stats->ftimes[i] += (ts2.tv_usec - ts1.tv_usec);
//stats->ftimes[i] -= rb;
stats->dtimes[i] = (ts2.tv_sec - ds.tv_sec) * 1000 * 1000;
stats->dtimes[i] += (ts2.tv_usec - ds.tv_usec);
}
gettimeofday(&fs2, NULL);
stats->ttime = (fs2.tv_sec - fs1.tv_sec) * 1000 * 1000;
stats->ttime += (fs2.tv_usec - fs1.tv_usec);
//stats->ttime -= slept;
free(args);
free(buffer);
return NULL;
}