12#include <netinet/in.h>
13#include <sys/socket.h>
29#define NW_SEND_HEADER 6
30#define NW_SEND_TRAILER 2
31#define NW_RAW_HEADER 12
32#define NW_RAW_TRAILER 2
37#define NW_COPPER_HEADER 0
38#define NW_B2L_HEADER 2
39#define NW_B2L_TRAILER 1
40#define NW_COPPER_TRAILER 0
42#define NW_COPPER_HEADER 13
43#define NW_B2L_HEADER 6
44#define NW_B2L_TRAILER 3
45#define NW_COPPER_TRAILER 3
50unsigned short CalcCRC16LittleEndian(
unsigned short crc16,
const int buf[],
int nwords)
55 sprintf(err_buf,
"nwords value(%d) is invalid. Cannot calculate CRC16. Exiting...\n %s %s %d\n",
56 nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__);
57 printf(
"%s", err_buf); fflush(stdout);
58 string err_str = err_buf;
62 const unsigned short CRC16Table0x1021[ 256 ] = {
63 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
64 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
65 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
66 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
67 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
68 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
69 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
70 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
72 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
73 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
74 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
75 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
76 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
77 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
78 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
79 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
81 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
82 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
83 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
84 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
85 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
86 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
87 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
88 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
90 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
91 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
92 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
93 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
94 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
95 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
96 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
97 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
100 int cnt = 0, nints = 0;
102 while (nwords != 0) {
104 unsigned char temp_buf = *((
unsigned char*)(buf + nints) + (-(cnt % 4) + 3));
105 crc16 = CRC16Table0x1021[(crc16 >> (16 - CHAR_BIT)) ^ temp_buf ] ^ (crc16 << CHAR_BIT);
107 if ((cnt % 4) == 3) {
125 gettimeofday(&t, NULL);
126 return (t.tv_sec + t.tv_usec * 1.e-6 - 1417570000.);
129int fillDataContents(
int* buf,
int nwords_per_fee,
unsigned int node_id,
int ncpr,
int nhslb,
int run)
131 int nwords = NW_SEND_HEADER + NW_SEND_TRAILER +
132 ncpr * (NW_RAW_HEADER + NW_COPPER_HEADER +
133 (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
134 + NW_COPPER_TRAILER + NW_RAW_TRAILER);
139 buf[ offset + 0 ] = nwords;
140 buf[ offset + 1 ] = 6;
141 buf[ offset + 2 ] = (1 << 16) | ncpr;
142 unsigned int exp_run = run << 8;
143 buf[ offset + 3 ] = exp_run;
144 buf[ offset + 5 ] = node_id;
149 for (
int k = 0; k < ncpr; k++) {
150 int top_pos = offset;
154 int cpr_nwords = NW_RAW_HEADER + NW_COPPER_HEADER +
155 (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
156 + NW_COPPER_TRAILER + NW_RAW_TRAILER;
157 int finesse_nwords = nwords_per_fee + NW_B2L_HEADER + NW_B2L_TRAILER;
158 unsigned int ctime = 0x12345601;
159 unsigned int utime = 0x98765432;
162 buf[ offset + 0 ] = cpr_nwords;
164 buf[ offset + 1 ] = 0x7f7f020c;
166 buf[ offset + 1 ] = 0x7f7f820c;
168 buf[ offset + 2 ] = exp_run;
169 printf(
"run_no %u\n", exp_run); fflush(stdout);
170 buf[ offset + 4 ] = ctime;
171 buf[ offset + 5 ] = utime;
172 buf[ offset + 6 ] = node_id + k;
173 buf[ offset + 7 ] = 0x34567890;
174 buf[ offset + 8 ] = NW_RAW_HEADER + NW_COPPER_HEADER;
175 buf[ offset + 9 ] = buf[ offset + 8 ] + finesse_nwords;
177 buf[ offset + 10 ] = buf[ offset + 9 ] + finesse_nwords;
179 buf[ offset + 10 ] = buf[ offset + 9 ];
182 buf[ offset + 11 ] = buf[ offset + 10 ] + finesse_nwords;
184 buf[ offset + 11 ] = buf[ offset + 10 ];
190 int top_pos_cpr = offset;
193 buf[ offset + 0 ] = 0x7fff0008;
194 buf[ offset + 2 ] = 0;
195 buf[ offset + 3 ] = 0;
196 buf[ offset + 4 ] = 0;
197 buf[ offset + 5 ] = 0;
198 buf[ offset + 6 ] = 0;
199 buf[ offset + 7 ] = 0xfffffafa;
200 buf[ offset + 8 ] = cpr_nwords - (NW_RAW_HEADER + 7 + 2 + NW_RAW_TRAILER);
201 buf[ offset + 9 ] = finesse_nwords;
204 buf[ offset + 10 ] = finesse_nwords;
206 buf[ offset + 11 ] = finesse_nwords;
208 buf[ offset + 12 ] = finesse_nwords;
210 buf[ offset + 12 ] = 0;
213 buf[ offset + 11 ] = 0;
214 buf[ offset + 12 ] = 0;
217 buf[ offset + 10 ] = 0;
218 buf[ offset + 11 ] = 0;
219 buf[ offset + 12 ] = 0;
227 for (
int i = 0; i < nhslb ; i++) {
229 buf[ offset + 0 ] = 0xffaa0000;
230 buf[ offset + 1 ] = ctime;
233 buf[ offset + 0 ] = 0xffaa0000;
234 buf[ offset + 1 ] = ctime;
235 buf[ offset + 3 ] = utime;
236 buf[ offset + 4 ] = exp_run;
237 buf[ offset + 5 ] = ctime;
241 for (
int j = offset; j < offset + nwords_per_fee; j++) {
244 offset += nwords_per_fee;
249 buf[ offset ] = ctime;
250 buf[ offset + 1 ] = 0;
251 buf[ offset + 2 ] = 0xff550000;
259 buf[ offset ] = 0xfffff5f5;
260 buf[ offset + 1 ] = buf[ top_pos_cpr ];
261 for (
int j = top_pos_cpr + 1; j < offset + 1; j++) {
262 buf[ offset + 1 ] ^= buf[ j ];
264 buf[ offset + 2 ] = 0x7fff0009;
267 buf[ offset ] = buf[ top_pos ];
268 for (
int j = top_pos + 1; j < offset; j++) {
269 buf[ offset ] ^= buf[ j ];
271 buf[ offset + 1 ] = 0x7fff0006;
277 buf[ offset + 1 ] = 0x7fff0000;
287inline 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 if (buf[ offset + 4 ] != 0x12345601) {
299 printf(
"[FATAL] data-production error 2 0x%.x", buf[ offset + 4 ]);
304 buf[ pos_xorchksum ] ^= buf[ offset + 3];
305 buf[ offset + 3] = event;
306 buf[ pos_xorchksum ] ^= buf[ offset + 3];
309 offset += NW_RAW_HEADER + NW_COPPER_HEADER +
310 nhslb * (NW_B2L_HEADER + nwords_per_fee + NW_B2L_TRAILER)
311 + NW_COPPER_TRAILER + NW_RAW_TRAILER;
313 int pos_xorchksum_cpr = offset + nwords - 4;
314 buf[ pos_xorchksum ] ^= buf[ pos_xorchksum_cpr ];
317 offset += NW_RAW_HEADER;
319 buf[ pos_xorchksum ] ^= buf[ offset + 1];
320 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 1];
321 buf[ offset + 1 ] = event;
322 buf[ pos_xorchksum ] ^= buf[ offset + 1];
323 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 1];
324 offset += NW_COPPER_HEADER;
326 for (
int i = 0; i < nhslb ; i++) {
327 if ((buf[ offset ] & 0xffff0000) != 0xffaa0000) {
328 printf(
"[FATAL] data-production error 3 : 0x%.x hslb %d cpr %d\n", buf[ offset ], i, k);
333 buf[ pos_xorchksum ] ^= buf[ offset + 0];
334 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 0];
335 buf[ offset + 0 ] = 0xffaa0000 + (
event & 0xffff);
336 buf[ pos_xorchksum ] ^= buf[ offset + 0];
337 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 0];
339 buf[ pos_xorchksum ] ^= buf[ offset + 2];
340 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 2];
341 buf[ offset + 2 ] = event;
342 buf[ pos_xorchksum ] ^= buf[ offset + 2];
343 buf[ pos_xorchksum_cpr ] ^= buf[ offset + 2];
346 int* crc_buf = buf + offset + 1;
347 int crc_nwords = nwords_per_fee + 5;
348 unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, crc_buf, crc_nwords);
349 buf[ pos_xorchksum ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
350 buf[ pos_xorchksum_cpr ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
351 buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ] = ((
event & 0x0000ffff) << 16) | temp_crc16;
353 buf[ pos_xorchksum ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
354 buf[ pos_xorchksum_cpr ] ^= buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ];
358 offset += NW_B2L_HEADER + nwords_per_fee + NW_B2L_TRAILER;
361 offset += NW_COPPER_TRAILER + NW_RAW_TRAILER;
363 buf[ pos_xorchksum ] ^= buf[ pos_xorchksum_cpr ];
372int main(
int argc,
char** argv)
375 printf(
"###################################################\n");
377 printf(
"# Data after reduction (#define REDUCED_DATA) #\n");
379 printf(
"# Data before reduction (//#define REDUCED_DATA) #\n");
381 printf(
"###################################################\n");
385 printf(
"Usage : %s <node ID> <run#> <nwords of det. buf per FEE> <# of CPR per COPPER> <# of HSLBs>\n", argv[ 0 ]);
390 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",
396 unsigned int node_id = 0;
397 sscanf(argv[1],
"0x%x", &node_id);
400 int run_no = atoi(argv[2]);
403 int nwords_per_fee = atoi(argv[3]);
404 int ncpr = atoi(argv[4]);
405 int nhslb = atoi(argv[5]);
407 int listenfd, connfd;
408 struct sockaddr_in servaddr;
409 int total_words = NW_SEND_HEADER + NW_SEND_TRAILER +
410 ncpr * (NW_RAW_HEADER + NW_COPPER_HEADER + (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb + NW_COPPER_TRAILER +
412 printf(
"TET %d %d %d %d %d\n ", NW_SEND_HEADER + NW_SEND_TRAILER, ncpr,
413 NW_RAW_HEADER + NW_COPPER_HEADER, (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb, NW_COPPER_TRAILER + NW_RAW_TRAILER);
415 vector<int> buff(total_words);
421 int temp_ret = fillDataContents(buff.data(), nwords_per_fee, node_id, ncpr, nhslb, run_no);
422 if (temp_ret != total_words) {
423 printf(
"[FATAL] data-production error %d %d\n", total_words, temp_ret);
430 listenfd = socket(AF_INET, SOCK_STREAM, 0);
431 memset(&servaddr, 0,
sizeof(servaddr));
432 servaddr.sin_family = AF_INET;
433 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
434 servaddr.sin_port = htons(30000);
437 int ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)
sizeof(flags));
439 perror(
"Failed to set REUSEADDR");
442 bind(listenfd, (
struct sockaddr*)&servaddr,
sizeof(servaddr));
444 listen(listenfd, LISTENQ);
445 printf(
"Accepting..."); fflush(stdout);
446 connfd = accept(listenfd, (
struct sockaddr*) NULL, NULL);
447 printf(
"Done."); fflush(stdout);
455 if ((connfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
456 perror(
"[FATAL] socket error");
460 memset(&servaddr, 0,
sizeof(servaddr));
461 servaddr.sin_family = AF_INET;
462 servaddr.sin_port = htons(atoi(argv[7]));
463 if (inet_pton(AF_INET, argv[6], &servaddr.sin_addr) <= 0) {
464 perror(
"[FATAL] inetpton error");
469 if (connect(connfd, (
struct sockaddr*)&servaddr,
sizeof(servaddr)) < 0) {
470 perror(
"Failed to connect");
480 printf(
"Connection Accepted\n"); fflush(stdout);
482 double init_time = getTimeSec();
483 double prev_time = init_time;
485 unsigned long long int cnt = 0;
486 unsigned long long int prev_cnt = 0;
487 unsigned long long int start_cnt = 300000;
490 addEvent(buff.data(), nwords_per_fee, cnt, ncpr, nhslb);
500 if ((Ret = write(connfd, buff.data(), total_words *
sizeof(
int))) <= 0) {
501 printf(
"[FATAL] Return value %d\n", Ret);
508 if (cnt == start_cnt) init_time = getTimeSec();
509 if (cnt % 10000 == 1) {
510 if (cnt > start_cnt) {
511 double cur_time = getTimeSec();
512 printf(
"run %d evt %llu time %.1lf dataflow %.1lf MB/s rate %.2lf kHz : so far dataflow %.1lf MB/s rate %.2lf kHz size %d\n",
515 cur_time - init_time,
516 (cnt - prev_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - prev_time),
517 (cnt - prev_cnt) / (cur_time - prev_time) / 1000.,
518 (cnt - start_cnt)*total_words *
sizeof(
int) / 1000000. / (cur_time - init_time),
519 (cnt - start_cnt) / (cur_time - init_time) / 1000., total_words);
522 prev_time = cur_time;