-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.cpp
More file actions
133 lines (115 loc) · 4.26 KB
/
trace.cpp
File metadata and controls
133 lines (115 loc) · 4.26 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
/*
* =====================================================================================
*
* Filename: trace.c
*
* Description:
*
* Version: 1.0
* Created: 2019年01月08日 14时52分20秒
* Revision: none
* Compiler: gcc
*
* Author: lilei.feng , lilei.feng@pku.edu.cn
* Company: Peking University
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include "common.h"
#define TRACE_MAX_LEN 512
const char *trace_sock_path = "trace.sock";
int main(int argc, char *argv[]){
if(argc != 4) return -1;
int reader_num = atoi(argv[1]);
int tag_num = atoi(argv[2]);
int tar_round = atoi(argv[3]);
int round = 0;
time_t t = time(0);
struct tm ttt = *localtime(&t);
char result_file_path[128];
char trace_file_path[128];
int server_fd = unix_domain_server_init(trace_sock_path);
int conn[2];
conn[0] = accept(server_fd, NULL, NULL);
conn[1] = accept(server_fd, NULL, NULL);
int maxfd = conn[0] > conn[1] ? conn[0] : conn[1];
int recvlen = 0;
char recvbuf[TRACE_MAX_LEN];
sprintf(result_file_path, "result-%4d-%02d-%02d_%02d_%02d_%02d_%dreader_%dtag.csv", ttt.tm_year + 1900, ttt.tm_mon + 1, ttt.tm_mday, ttt.tm_hour, ttt.tm_min, ttt.tm_sec, reader_num, tag_num);
sprintf(trace_file_path, "trace-%4d-%02d-%02d_%02d_%02d_%02d_%dreader_%dtag.log", ttt.tm_year + 1900, ttt.tm_mon + 1, ttt.tm_mday, ttt.tm_hour, ttt.tm_min, ttt.tm_sec, reader_num, tag_num);
int res_fd = open(result_file_path, O_RDWR|O_CREAT, 0664);
int trace_fd = open(trace_file_path, O_RDWR|O_CREAT, 0664);
while(1){
fd_set fds;
memset(&fds, 0, sizeof(fd_set));
FD_SET(conn[0], &fds);
FD_SET(conn[1], &fds);
int ret = select(maxfd + 1, &fds, NULL, NULL, NULL);
if(ret > 0){
if(FD_ISSET(conn[0], &fds)){
memset(recvbuf, 0, sizeof(recvbuf));
recvlen = read(conn[0], recvbuf, TRACE_MAX_LEN);
write(trace_fd, recvbuf, recvlen);
if(recvlen >= 6 && (memcmp(recvbuf, "FINISH", 6) == 0)){
round++;
printf("round:%d\n", round);
if(round < tar_round){
write(conn[1], "RESET", 5);
write(conn[0], "RESET", 5);
}else{
write(conn[1], "KILL", 4);
write(conn[0], "KILL", 4);
break;
}
}
char *res_str = NULL;
if(res_str = strstr(recvbuf, "elapsed_time:")){
char *end_str = strstr(res_str, "\n");
char tmp[64];
snprintf(tmp, end_str-res_str, "%s, ", res_str);
}
}
if(FD_ISSET(conn[1], &fds)){
memset(recvbuf, 0, sizeof(recvbuf));
recvlen = read(conn[1], recvbuf, TRACE_MAX_LEN);
write(trace_fd, recvbuf, recvlen);
if(recvlen >= 6 && (memcmp(recvbuf, "FINISH", 6) == 0)){
round++;
printf("round:%d\n", round);
if(round < tar_round){
write(conn[1], "RESET", 5);
write(conn[0], "RESET", 5);
}else{
write(conn[1], "KILL", 4);
write(conn[0], "KILL", 4);
break;
}
}
char *res_str = NULL;
if(res_str = strstr(recvbuf, "elapsed_time:")){
write(res_fd, res_str, strlen(res_str));
}
}
}else{
printf("socket error!\n");
return -1;
}
}
close(trace_fd);
close(server_fd);
return 0;
}