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
| #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <arpa/inet.h> #include <time.h> #include <iostream> #include <string.h> #include <stdint.h> #include <csignal> #include <iostream> #include <assert.h> #include <math.h>
using namespace std;
const float CPU_freq = 2.194844;
typedef struct timestamp { uint32_t coreId; uint64_t value; } timestamp_t;
inline void read_timestamp_counter(timestamp_t * t) { uint64_t highTick, lowTick; asm volatile ("rdtscp" : "=d"(highTick), "=a"(lowTick), "=c"(t->coreId)); t->value = highTick << 32 | lowTick; }
inline uint64_t diff_timestamps(const timestamp_t * before, const timestamp_t * after) { assert(before->coreId == after->coreId); return after->value - before->value; }
inline uint64_t cycle_since_timestamp(const timestamp_t * previous) { timestamp_t now; read_timestamp_counter(&now); return diff_timestamps(previous, &now); }
uint64_t get_time(){ struct timespec tv; clock_gettime(CLOCK_REALTIME,&tv); uint64_t clock = (tv.tv_sec % 1000) * 1e9 + tv.tv_nsec; return clock; }
typedef unsigned int SOCKET; #define INVALID_SOCKET -1 #define SOCKET_ERROR -1
#define BUFFER_SZ 1200 #define SEND_BUF_LEN 512
int server_port = 33456; int client_port = 33457;
const char* ServerIP = "127.0.0.1";
int Send_num = 200000;
const int RTT_LEN = 1000000; uint64_t *rtt = new uint64_t[RTT_LEN];
int mode = -1;
int main(int argc, char *argv[]){ if(argc != 2){ std::cout << "Usage: APP NAME, MODE(0:Servver, 1:Client)" << std::endl; } mode = atoi(argv[1]);
if(mode == 0){ SOCKET m_sock; char buffer[BUFFER_SZ]; if((m_sock = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP )) == INVALID_SOCKET) { return 0; } sockaddr_in my_addr; sockaddr_in remote_addr; memset(&my_addr,0,sizeof(sockaddr_in)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons( server_port );
if( bind(m_sock ,(sockaddr *)&my_addr , sizeof(sockaddr)) == SOCKET_ERROR) { printf("Socket bind failed\n"); close(m_sock); return 0; }
int sin_size=sizeof(struct sockaddr_in);
cout << "waiting for packets..." << endl; for(int i=0; i < Send_num; i++) { int nRecEcho = recvfrom(m_sock, (char*)buffer, BUFFER_SZ , 0, (struct sockaddr *)&remote_addr, (socklen_t *)&sin_size); if (nRecEcho < 0) { printf("[SERVER]recv error/n"); break; } else if (nRecEcho != 512){ printf("invalid packet received!\n"); } int len = sendto(m_sock, buffer, nRecEcho, 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)); if(len == -1){ std::cout << "[SERVER]send failed!" << std::endl; } } close(m_sock); cout << "complete!" << endl; } else{ SOCKET m_sock; char buffer[SEND_BUF_LEN];
if((m_sock = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP )) == INVALID_SOCKET) { return 0; } sockaddr_in remote_addr; memset(&remote_addr,0,sizeof(sockaddr_in)); remote_addr.sin_family = AF_INET; remote_addr.sin_addr.s_addr = inet_addr(ServerIP); remote_addr.sin_port = htons( server_port );
sockaddr_in my_addr; memset(&my_addr,0,sizeof(sockaddr_in)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons( client_port );
if( bind(m_sock ,(sockaddr *)&my_addr , sizeof(sockaddr)) == SOCKET_ERROR) { printf("Socket bind failed\n"); close(m_sock); return 0; }
memset(buffer, 36, SEND_BUF_LEN);
int sin_size=sizeof(struct sockaddr_in);
for(int i=0; i < Send_num; i++){ timestamp_t start; read_timestamp_counter(&start);
buffer[0] = 0xff; buffer[1] = 0xee; buffer[2] = 0x00; buffer[3] = 0x02; memcpy(buffer + 110, &i, sizeof(i)); memcpy(buffer + 50, &(start), sizeof(start)); int len = sendto(m_sock, buffer, SEND_BUF_LEN, 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)); if(len == -1){ std::cout << "socket send failed!" << std::endl; }
int nRecEcho = recvfrom(m_sock, (char*)buffer, BUFFER_SZ , 0, (struct sockaddr *)&remote_addr, (socklen_t *)&sin_size); if (nRecEcho < 0) { printf("[CLIENT]recv error/n"); break; } else if (nRecEcho != 512){ printf("[CLIENT]invalid packet received!\n"); }
uint64_t latency = cycle_since_timestamp(&start); rtt[i] = latency / CPU_freq / 2; } close(m_sock);
uint64_t min = 99999, max = 0, sum = 0; for(int i=0; i < Send_num; i++){ if (rtt[i] < min){ min = rtt[i]; } if (rtt[i] > max){ max = rtt[i]; } sum += rtt[i]; } double avg = (double)sum / Send_num; double std = 0.0; double tmp = 0.0; for(int i=0; i < Send_num; i++){ tmp += (rtt[i] - avg) * (rtt[i] - avg); } std = sqrt(tmp/Send_num);
cout << "num: " << Send_num << endl; cout << "avg: " << sum / Send_num << endl; cout << "min: " << min << endl; cout << "max: " << max << endl; cout << "std: " << std << endl;
cout << "total receive valid packets: " << Send_num << endl; }
return 0; }
|