-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog-server.c
216 lines (181 loc) · 5.84 KB
/
log-server.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
/*
* FS test (log-server.c)
* Receive the progress of writer threads and record it to file
*
* (C) Copyright 2010-2012 TOSHIBA CORPORATION
*
* This source code is licensed under the GNU General Public License,
* Version 2. See the file COPYING for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "config.h"
#define NUM_RING_BUF 3
#define NUM_BUF 2048
#define BUF_LEN 1024
#define FILE_PATH "./log/log.txt"
static int net_ready = 0;
static int w_progress = -1;
static char rbuf[NUM_RING_BUF][NUM_BUF][BUF_LEN];
static pthread_mutex_t *log_mutex; /* mutex for log buffer */
struct net_arg_struct {
int id;
};
struct io_arg_struct {
int id;
};
struct net_arg_struct net_args;
struct io_arg_struct io_args;
void net_recv_write(int soc, struct sockaddr_in *sockaddr)
{
ssize_t num_recv;
FILE *fd;
char rbufp[BUF_LEN];
char newfile[256];
char *tok;
char *ios;
fd = fopen(FILE_PATH, "w");
while(1){
num_recv = recvfrom(soc, rbufp, BUF_LEN, 0, NULL, NULL);
if(num_recv == -1) {
perror("recv");
close(soc);
exit(EXIT_FAILURE);
}
fprintf(fd, "%s", rbufp);
if (!strncmp(rbufp, "88", 2)){
fflush(fd);
fclose(fd);
tok = strtok(rbufp, "\t"); // 8888
tok = strtok(NULL, "\t"); // len
ios = strtok(NULL, "\t\n"); // fsize
ios = strtok(NULL, "\t\n"); // iosched
strcpy(newfile, "log-");
strcat(newfile, ios);
strcat(newfile, "-");
strcat(newfile, tok);
strcat(newfile, ".txt");
rename(FILE_PATH, newfile);
fd = fopen(FILE_PATH, "w");
}
}
/* NEVER REACHD */
fclose(fd);
}
void *net_thread(void *net_args)
{
int recv_soc;
int recv_port = COMM_PORT;
int stat;
struct sockaddr_in recv_socaddr;
// struct net_arg_struct *na = (struct net_arg_struct *)net_args;
fprintf(stderr,"Starting NET thread: %d\n", __LINE__);
/*
* Initialize socket
*/
memset(&recv_socaddr, 0, sizeof(struct sockaddr_in));
recv_socaddr.sin_port = htons(recv_port);
recv_socaddr.sin_family = AF_INET;
recv_socaddr.sin_addr.s_addr = htonl(INADDR_ANY);
recv_soc = socket(AF_INET, SOCK_DGRAM, 0);
stat = bind(recv_soc, (struct sockaddr_in *) &recv_socaddr,
sizeof(struct sockaddr_in));
net_recv_write(recv_soc, &recv_socaddr);
/* NEVER REACHD */
return 0;
}
void net_recv(int soc, struct sockaddr_in *sockaddr, struct net_arg_struct *na)
{
int i, j;
ssize_t num_recv;
char *rbufp;
for (i = 0; i< NUM_RING_BUF; i++){
if (w_progress == i){
fprintf(stderr,"Buffer overflow: %d\n", __LINE__);
}
pthread_mutex_lock(&log_mutex[i]);
net_ready = 1;
for(j = 0; j<NUM_BUF; j++){
rbufp = rbuf[i][j];
num_recv = recvfrom(soc, rbufp, BUF_LEN, 0, NULL, NULL);
if(num_recv == -1) {
perror("recv");
close(soc);
exit(EXIT_FAILURE);
}
fprintf(stderr,"RECV: %s", rbufp);
}
pthread_mutex_unlock(&log_mutex[i]);
}
if (w_progress == -1){
fprintf(stderr,"File I/O too slow\n");
exit(EXIT_FAILURE);
}
}
void *fio_thread(void *io_args)
{
int i, j;
char *rbufp;
FILE *fd;
// struct io_arg_struct *ia = (struct io_arg_struct *)io_args;
fprintf(stderr,"Starting LOG thread: %d\n", __LINE__);
fd = fopen(FILE_PATH, "w");
/*
* wait for net thread
*/
while(net_ready != 1){
sched_yield();
}
fprintf(stderr,"Starting logging: %d\n", __LINE__);
while(1){
// log_out(fd, ia);
for (i = 0; i<NUM_RING_BUF; i++){
pthread_mutex_lock(&log_mutex[i]);
w_progress = i;
for (j = 0; j<NUM_BUF; j++){
rbufp = rbuf[i][j];
// fprintf(stderr, "%s", rbufp);
fprintf(fd, "%s", rbufp);
}
pthread_mutex_unlock(&log_mutex[i]);
}
}
/* NEVER REACHD */
}
int main()
{
int i;
char com;
// pthread_t iothr;
pthread_t netthr;
/*
* Initialize
*/
log_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t) * NUM_RING_BUF);
for(i=0; i<NUM_RING_BUF; i++){
pthread_mutex_init(&log_mutex[i], NULL);
}
if(pthread_create(&netthr, NULL, net_thread, &net_args)){
// fprintf(stderr,"File I/O thread create: %d\n", __LINE__);
exit(EXIT_FAILURE);
}
while(1){
printf("COMMAND(q): ");
scanf("%c", &com);
if (com == 'q'){
// flush_buffer();
fflush(NULL);
exit(0);
}else{
fflush(NULL);
}
}
/* NEVER REACHD */
pthread_join(netthr, NULL);
}