11 #include <netinet/in.h>
12 #include <sys/socket.h>
28 #define NW_SEND_HEADER 6
29 #define NW_SEND_TRAILER 2
30 #define NW_RAW_HEADER 12
31 #define NW_RAW_TRAILER 2
36 #define NW_COPPER_HEADER 0
37 #define NW_B2L_HEADER 2
38 #define NW_B2L_TRAILER 1
39 #define NW_COPPER_TRAILER 0
41 #define NW_COPPER_HEADER 13
42 #define NW_B2L_HEADER 6
43 #define NW_B2L_TRAILER 3
44 #define NW_COPPER_TRAILER 3
49 unsigned short CalcCRC16LittleEndian(
unsigned short crc16,
const int buf[],
int nwords)
54 sprintf(err_buf,
"nwords value(%d) is invalid. Cannot calculate CRC16. Exiting...\n %s %s %d\n",
55 nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__);
56 printf(
"%s", err_buf); fflush(stdout);
57 string err_str = err_buf;
61 const unsigned short CRC16Table0x1021[ 256 ] = {
62 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
63 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
64 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
65 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
66 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
67 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
68 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
69 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
71 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
72 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
73 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
74 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
75 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
76 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
77 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
78 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
80 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
81 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
82 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
83 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
84 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
85 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
86 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
87 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
89 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
90 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
91 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
92 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
93 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
94 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
95 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
96 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
99 int cnt = 0, nints = 0;
101 while (nwords != 0) {
103 unsigned char temp_buf = *((
unsigned char*)(buf + nints) + (-(cnt % 4) + 3));
104 crc16 = CRC16Table0x1021[(crc16 >> (16 - CHAR_BIT)) ^ temp_buf ] ^ (crc16 << CHAR_BIT);
106 if ((cnt % 4) == 3) {
124 gettimeofday(&t, NULL);
125 return (t.tv_sec + t.tv_usec * 1.e-6 - 1417570000.);
128 int fillDataContents(
int* buf,
int nwords_per_fee,
unsigned int node_id,
int ncpr,
int nhslb,
int run)
130 int nwords = NW_SEND_HEADER + NW_SEND_TRAILER +
131 ncpr * (NW_RAW_HEADER + NW_COPPER_HEADER +
132 (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
133 + NW_COPPER_TRAILER + NW_RAW_TRAILER);
138 buf[ offset + 0 ] = nwords;
139 buf[ offset + 1 ] = 6;
140 buf[ offset + 2 ] = (1 << 16) | ncpr;
141 unsigned int exp_run = run << 8;
142 buf[ offset + 3 ] = exp_run;
143 buf[ offset + 5 ] = node_id;
148 for (
int k = 0; k < ncpr; k++) {
149 int top_pos = offset;
153 int cpr_nwords = NW_RAW_HEADER + NW_COPPER_HEADER +
154 (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
155 + NW_COPPER_TRAILER + NW_RAW_TRAILER;
156 int finesse_nwords = nwords_per_fee + NW_B2L_HEADER + NW_B2L_TRAILER;
157 unsigned int ctime = 0x12345601;
158 unsigned int utime = 0x98765432;
161 buf[ offset + 0 ] = cpr_nwords;
163 buf[ offset + 1 ] = 0x7f7f020c;
165 buf[ offset + 1 ] = 0x7f7f820c;
167 buf[ offset + 2 ] = exp_run;
168 printf(
"run_no %d\n", exp_run); fflush(stdout);
169 buf[ offset + 4 ] = ctime;
170 buf[ offset + 5 ] = utime;
171 buf[ offset + 6 ] = node_id + k;
172 buf[ offset + 7 ] = 0x34567890;
173 buf[ offset + 8 ] = NW_RAW_HEADER + NW_COPPER_HEADER;
174 buf[ offset + 9 ] = buf[ offset + 8 ] + finesse_nwords;
176 buf[ offset + 10 ] = buf[ offset + 9 ] + finesse_nwords;
178 buf[ offset + 10 ] = buf[ offset + 9 ];
181 buf[ offset + 11 ] = buf[ offset + 10 ] + finesse_nwords;
183 buf[ offset + 11 ] = buf[ offset + 10 ];
189 int top_pos_cpr = offset;
192 buf[ offset + 0 ] = 0x7fff0008;
193 buf[ offset + 2 ] = 0;
194 buf[ offset + 3 ] = 0;
195 buf[ offset + 4 ] = 0;
196 buf[ offset + 5 ] = 0;
197 buf[ offset + 6 ] = 0;
198 buf[ offset + 7 ] = 0xfffffafa;
199 buf[ offset + 8 ] = cpr_nwords - (NW_RAW_HEADER + 7 + 2 + NW_RAW_TRAILER);
200 buf[ offset + 9 ] = finesse_nwords;
203 buf[ offset + 10 ] = finesse_nwords;
205 buf[ offset + 11 ] = finesse_nwords;
207 buf[ offset + 12 ] = finesse_nwords;
209 buf[ offset + 12 ] = 0;
212 buf[ offset + 11 ] = 0;
213 buf[ offset + 12 ] = 0;
216 buf[ offset + 10 ] = 0;
217 buf[ offset + 11 ] = 0;
218 buf[ offset + 12 ] = 0;
226 for (
int i = 0; i < nhslb ; i++) {
228 buf[ offset + 0 ] = 0xffaa0000;
229 buf[ offset + 1 ] = ctime;
232 buf[ offset + 0 ] = 0xffaa0000;
233 buf[ offset + 1 ] = ctime;
234 buf[ offset + 3 ] = utime;
235 buf[ offset + 4 ] = exp_run;
236 buf[ offset + 5 ] = ctime;
240 for (
int j = offset; j < offset + nwords_per_fee; j++) {
243 offset += nwords_per_fee;
248 buf[ offset ] = ctime;
249 buf[ offset + 1 ] = 0;
250 buf[ offset + 2 ] = 0xff550000;
258 buf[ offset ] = 0xfffff5f5;
259 buf[ offset + 1 ] = buf[ top_pos_cpr ];
260 for (
int j = top_pos_cpr + 1; j < offset + 1; j++) {
261 buf[ offset + 1 ] ^= buf[ j ];
263 buf[ offset + 2 ] = 0x7fff0009;
266 buf[ offset ] = buf[ top_pos ];
267 for (
int j = top_pos + 1; j < offset; j++) {
268 buf[ offset ] ^= buf[ j ];
270 buf[ offset + 1 ] = 0x7fff0006;
276 buf[ offset + 1 ] = 0x7fff0000;
286 inline void addEvent(
int* buf,
int nwords_per_fee,
unsigned int event,
int ncpr,
int nhslb)
291 buf[ offset + 4 ] = event;
292 offset += NW_SEND_HEADER;
294 for (
int k = 0; k < ncpr; k++) {
295 int nwords = buf[ offset ];
296 int posback_xorchksum = 2;
297 int pos_xorchksum = offset + nwords - posback_xorchksum;
298 prev_offset = offset;
299 if (buf[ offset + 4 ] != 0x12345601) {
300 printf(
"[FATAL] data-production error 2 0x%.x", buf[ offset + 4 ]);
305 buf[ pos_xorchksum ] ^= buf[ offset + 3];
306 buf[ offset + 3] = event;
307 buf[ pos_xorchksum ] ^= buf[ offset + 3];
310 offset += NW_RAW_HEADER + NW_COPPER_HEADER +
311 nhslb * (NW_B2L_HEADER + nwords_per_fee + NW_B2L_TRAILER)
312 + NW_COPPER_TRAILER + NW_RAW_TRAILER;
314 int pos_xorchksum_cpr = offset + nwords - 4;
315 buf[ pos_xorchksum ] ^= buf[ pos_xorchksum_cpr ];
318 offset += NW_RAW_HEADER;
320 buf[ pos_xorchksum ] ^= buf[ offset + 1];
321 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 1];
322 buf[ offset + 1 ] = event;
323 buf[ pos_xorchksum ] ^= buf[ offset + 1];
324 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 1];
325 offset += NW_COPPER_HEADER;
327 for (
int i = 0; i < nhslb ; i++) {
328 if ((buf[ offset ] & 0xffff0000) != 0xffaa0000) {
329 printf(
"[FATAL] data-production error 3 : 0x%.x hslb %d cpr %d\n", buf[ offset ], i, k);
334 buf[ pos_xorchksum ] ^= buf[ offset + 0];
335 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 0];
336 buf[ offset + 0 ] = 0xffaa0000 + (
event & 0xffff);
337 buf[ pos_xorchksum ] ^= buf[ offset + 0];
338 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 0];
340 buf[ pos_xorchksum ] ^= buf[ offset + 2];
341 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 2];
342 buf[ offset + 2 ] = event;
343 buf[ pos_xorchksum ] ^= buf[ offset + 2];
344 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 2];
347 int* crc_buf = buf + offset + 1;
348 int crc_nwords = nwords_per_fee + 5;
349 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, crc_buf, crc_nwords);
350 buf[ pos_xorchksum ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
351 buf[ pos_xorchksum_cpr ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
352 buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ] = ((
event & 0x0000ffff) << 16) | temp_crc16;
354 buf[ pos_xorchksum ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
355 buf[ pos_xorchksum_cpr ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
359 offset += NW_B2L_HEADER + nwords_per_fee + NW_B2L_TRAILER;
362 offset += NW_COPPER_TRAILER + NW_RAW_TRAILER;
363 unsigned int xor_chksum = 0;
364 unsigned int xor_chksum2 = 0;
366 buf[ pos_xorchksum ] ^= buf[ pos_xorchksum_cpr ];
375 int main(
int argc,
char** argv)
378 printf(
"###################################################\n");
380 printf(
"# Data after reduction (#define REDUCED_DATA) #\n");
382 printf(
"# Data before reduction (//#define REDUCED_DATA) #\n");
384 printf(
"###################################################\n");
388 printf(
"Usage : %s <node ID> <run#> <nwords of det. buf per FEE> <# of CPR per COPPER> <# of HSLBs>\n", argv[ 0 ]);
393 printf(
"Usage : %s <node ID> <run#> <nwords of det. buf per FEE> <# of CPR per COPPER> <# of HSLBs> <ropc hostname> <ropc port> argc %d\n",
399 unsigned int node_id = 0;
400 sscanf(argv[1],
"0x%x", &node_id);
403 int run_no = atoi(argv[2]);
406 int nwords_per_fee = atoi(argv[3]);
407 int ncpr = atoi(argv[4]);
408 int nhslb = atoi(argv[5]);
410 int listenfd, connfd;
411 struct sockaddr_in servaddr;
412 int total_words = NW_SEND_HEADER + NW_SEND_TRAILER +
413 ncpr * (NW_RAW_HEADER + NW_COPPER_HEADER + (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb + NW_COPPER_TRAILER +
415 printf(
"TET %d %d %d %d %d\n ", NW_SEND_HEADER + NW_SEND_TRAILER, ncpr,
416 NW_RAW_HEADER + NW_COPPER_HEADER, (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb, NW_COPPER_TRAILER + NW_RAW_TRAILER);
418 int buff[total_words];
424 int temp_ret = fillDataContents(buff, nwords_per_fee, node_id, ncpr, nhslb, run_no);
425 if (temp_ret != total_words) {
426 printf(
"[FATAL] data-production error %d %d\n", total_words, temp_ret);
433 listenfd = socket(AF_INET, SOCK_STREAM, 0);
434 memset(&servaddr, 0,
sizeof(servaddr));
435 servaddr.sin_family = AF_INET;
436 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
437 servaddr.sin_port = htons(30000);
440 int ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)
sizeof(flags));
442 perror(
"Failed to set REUSEADDR");
445 bind(listenfd, (
struct sockaddr*)&servaddr,
sizeof(servaddr));
447 listen(listenfd, LISTENQ);
448 printf(
"Accepting..."); fflush(stdout);
449 connfd = accept(listenfd, (
struct sockaddr*) NULL, NULL);
450 printf(
"Done."); fflush(stdout);
458 if ((connfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
459 perror(
"[FATAL] socket error");
463 memset(&servaddr, 0,
sizeof(servaddr));
464 servaddr.sin_family = AF_INET;
465 servaddr.sin_port = htons(atoi(argv[7]));
466 if (inet_pton(AF_INET, argv[6], &servaddr.sin_addr) <= 0) {
467 perror(
"[FATAL] inetpton error");
472 if (connect(connfd, (
struct sockaddr*)&servaddr,
sizeof(servaddr)) < 0) {
473 perror(
"Failed to connect");
483 printf(
"Connection Accepted\n"); fflush(stdout);
485 double init_time = getTimeSec();
486 double prev_time = init_time;
488 unsigned long long int cnt = 0;
489 unsigned long long int prev_cnt = 0;
490 unsigned long long int start_cnt = 300000;
493 addEvent(buff, nwords_per_fee, cnt, ncpr, nhslb);
503 if ((ret = write(connfd, buff, total_words *
sizeof(
int))) <= 0) {
504 printf(
"[FATAL] Return value %d\n", ret);
511 if (cnt == start_cnt) init_time = getTimeSec();
512 if (cnt % 10000 == 1) {
513 if (cnt > start_cnt) {
514 double cur_time = getTimeSec();
515 printf(
"run %d evt %lld time %.1lf dataflow %.1lf MB/s rate %.2lf kHz : so far dataflow %.1lf MB/s rate %.2lf kHz size %d\n",
518 cur_time - init_time,
519 (cnt - prev_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - prev_time),
520 (cnt - prev_cnt) / (cur_time - prev_time) / 1000. ,
521 (cnt - start_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - init_time),
522 (cnt - start_cnt) / (cur_time - init_time) / 1000. , total_words);
525 prev_time = cur_time;
int main(int argc, char **argv)
Run all tests.