Belle II Software  release-06-01-15
dummy_data_distrib.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <iostream>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/time.h>
15 #include <time.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <arpa/inet.h>
19 #include <limits.h>
20 #include <signal.h>
21 #include <poll.h>
22 
23 
25 // Parameter for data-contents
27 //#define REDUCED_DATA
28 //#define CRC_ON
29 #define LISTENQ 1
30 #define NUM_CLIENTS 5
31 #define MAX_EVENT 1000
32 
33 // Format (PCIe40)
34 #define NW_SEND_HEADER 6
35 #define NW_SEND_TRAILER 2
36 
37 #define NW_RAW_HEADER 8
38 #define NW_RAW_TRAILER 4
39 
40 #ifdef REDUCED_DATA
41 #define NW_B2L_HEADER 3
42 #define NW_B2L_TRAILER 2
43 #else
44 #define NW_B2L_HEADER 7
45 #define NW_B2L_TRAILER 3
46 #endif
47 
48 #define CTIME_VAL 0x12345601
49 
50 using namespace std;
51 
52 unsigned short CalcCRC16LittleEndian(unsigned short crc16, const int buf[], int nwords)
53 {
54 
55  if (nwords < 0) {
56  char err_buf[500];
57  sprintf(err_buf, "nwords value(%d) is invalid. Cannot calculate CRC16. Exiting...\n %s %s %d\n",
58  nwords, __FILE__, __PRETTY_FUNCTION__, __LINE__);
59  printf("%s", err_buf); fflush(stdout);
60  string err_str = err_buf;
61  throw (err_str);
62  }
63 
64  const unsigned short CRC16Table0x1021[ 256 ] = {
65  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
66  0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
67  0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
68  0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
69  0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
70  0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
71  0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
72  0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
73 
74  0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
75  0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
76  0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
77  0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
78  0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
79  0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
80  0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
81  0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
82 
83  0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
84  0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
85  0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
86  0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
87  0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
88  0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
89  0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
90  0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
91 
92  0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
93  0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
94  0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
95  0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
96  0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
97  0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
98  0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
99  0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
100  };
101 
102  int cnt = 0, nints = 0;
103  // printf("### %.8x %.4x\n", buf[ 0 ], crc16);
104  while (nwords != 0) {
105 
106  unsigned char temp_buf = *((unsigned char*)(buf + nints) + (-(cnt % 4) + 3));
107  crc16 = CRC16Table0x1021[(crc16 >> (16 - CHAR_BIT)) ^ temp_buf ] ^ (crc16 << CHAR_BIT);
108  // printf("%.2x %.4x\n", temp_buf, crc16);
109  if ((cnt % 4) == 3) {
110  nwords--;
111  nints++;
112  // printf("### %.8x\n", buf[ nints ] );
113  }
114 
115  cnt++;
116  }
117 
118 
119  return crc16;
120 
121 }
122 
123 
124 double getTimeSec()
125 {
126  struct timeval t;
127  gettimeofday(&t, NULL);
128  return (t.tv_sec + t.tv_usec * 1.e-6 - 1417570000.);
129 }
130 
131 int fillDataContents(int* buf, int nwords_per_fee, unsigned int node_id, int ncpr, int nhslb, int run)
132 {
133  int nwords = NW_SEND_HEADER + NW_SEND_TRAILER +
134  ncpr * (NW_RAW_HEADER +
135  (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
136  + NW_RAW_TRAILER);
137 
138  // Send Header
139  int offset = 0;
140  buf[ offset + 0 ] = nwords;
141  buf[ offset + 1 ] = 6;
142  buf[ offset + 2 ] = (1 << 16) | ncpr;
143  unsigned int exp_run = run << 8;
144  buf[ offset + 3 ] = exp_run;
145  buf[ offset + 5 ] = node_id;
146  offset += NW_SEND_HEADER;
147 
148  for (int k = 0; k < ncpr; k++) {
149  int top_pos = offset;
150  //
151  // RawHeader
152  //
153  int cpr_nwords = NW_RAW_HEADER +
154  (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb
155  + NW_RAW_TRAILER;
156  int finesse_nwords = nwords_per_fee + NW_B2L_HEADER + NW_B2L_TRAILER;
157  unsigned int ctime = CTIME_VAL;
158  unsigned int utime = 0x98765432;
159 
160  buf[ offset + 0 ] = cpr_nwords;
161 #ifdef REDUCED_DATA
162  buf[ offset + 1 ] = 0x7f7f020c;
163 #else
164  buf[ offset + 1 ] = 0x7f7f820c;
165 #endif
166  buf[ offset + 2 ] = exp_run;
167  printf("run_no %d\n", exp_run); fflush(stdout);
168  buf[ offset + 4 ] = ctime;
169  buf[ offset + 5 ] = utime;
170  buf[ offset + 6 ] = node_id + k;
171  buf[ offset + 7 ] = 0x34567890;
172  offset += NW_RAW_HEADER;
173 
174  for (int i = 0; i < nhslb ; i++) {
175 #ifdef REDUCED_DATA
176  buf[ offset + 0 ] = nwords_per_fee + 3;
177  buf[ offset + 1 ] = 0xffaa0000;
178  buf[ offset + 2 ] = ctime;
179 #else
180  buf[ offset + 0 ] = nwords_per_fee + 7;
181  buf[ offset + 1 ] = 0xffaa0000;
182  buf[ offset + 3 ] = ctime;
183  buf[ offset + 4 ] = utime;
184  buf[ offset + 5 ] = exp_run;
185  buf[ offset + 6 ] = ctime;
186 #endif
187  offset += NW_B2L_HEADER;
188 
189  for (int j = offset; j < offset + nwords_per_fee; j++) {
190  buf[ j ] = rand();
191  }
192  offset += nwords_per_fee;
193 
194 #ifdef REDUCED_DATA
195  buf[ offset ] = 0;
196  buf[ offset + 1 ] = 0xff550000;
197 #else
198  buf[ offset ] = ctime;
199  buf[ offset + 1 ] = 0;
200  buf[ offset + 2 ] = 0xff550000;
201 #endif
202 
203  offset += NW_B2L_TRAILER;
204  }
205  buf[ offset ] = 0x0; // error bits
206  buf[ offset + 1 ] = 0x0; // error slots
207  buf[ offset + 2 ] = 0x0; // XOR checksum
208  buf[ offset + 3 ] = 0x7fff0006;
209  offset += NW_RAW_TRAILER;
210  }
211 
212  // Send trailer
213  buf[ offset ] = 0;
214  buf[ offset + 1 ] = 0x7fff0000;
215  offset += NW_SEND_TRAILER;
216  return offset;
217 }
218 
219 
220 
221 inline void addEvent(int* buf, int nwords_per_fee, unsigned int event, int ncpr, int nhslb)
222 //inline void addEvent(int* buf, int nwords, unsigned int event)
223 {
224  int offset = 0;
225  int prev_offset;
226  buf[ offset + 4 ] = event;
227  offset += NW_SEND_HEADER;
228 
229  for (int k = 0; k < ncpr; k++) {
230  int nwords = buf[ offset ];
231  int posback_xorchksum = 2;
232  int pos_xorchksum = offset + nwords - posback_xorchksum;
233  prev_offset = offset;
234  if (buf[ offset + 4 ] != CTIME_VAL) {
235  printf("[FATAL] data-production error 2 0x%.x", buf[ offset + 4 ]);
236  fflush(stdout);
237  exit(1);
238  }
239  // RawHeader
240  buf[ pos_xorchksum ] ^= buf[ offset + 3];
241  buf[ offset + 3] = event;
242  buf[ pos_xorchksum ] ^= buf[ offset + 3];
243 
244  // COPPER header
245  offset += NW_RAW_HEADER;
246  for (int i = 0; i < nhslb ; i++) {
247  if ((buf[ offset + 1 ] & 0xffff0000) != 0xffaa0000) {
248  printf("[FATAL] data-production error 3 : 0x%.x hslb %d cpr %d\n", buf[ offset ], i, k);
249  fflush(stdout);
250  exit(1);
251  }
252  buf[ offset + 1 ] = 0xffaa0000 + (event & 0xffff);
253  buf[ offset + 3 ] = event;
254 
255 #ifdef CRC_ON
256  int* crc_buf = buf + offset + 2; // 1 => size of HSLB B2L header
257  int crc_nwords = nwords_per_fee + 5; // 5 => size of FEE B2L header
258  unsigned short temp_crc16 = CalcCRC16LittleEndian(0xffff, crc_buf, crc_nwords);
259  buf[ offset + NW_B2L_HEADER + nwords_per_fee + 1 ] = ((event & 0x0000ffff) << 16) | temp_crc16;
260 #endif
261 
262 #ifdef REDUCED_DATA
263  offset += NW_B2L_HEADER + nwords_per_fee + NW_B2L_TRAILER;
264 #else
265  offset += NW_B2L_HEADER + nwords_per_fee + NW_B2L_TRAILER;
266 #endif
267  }
268  offset += NW_RAW_TRAILER;
269  unsigned int xor_chksum = 0;
270  unsigned int xor_chksum2 = 0;
271  }
272 
273 }
274 
275 
276 int main(int argc, char** argv)
277 {
278 
279  printf("###################################################\n");
280 #ifdef REDUCED_DATA
281  printf("# PCIe40 data after reduction (#define REDUCED_DATA) #\n");
282 #else
283  printf("# PCIe40 data before reduction (//#define REDUCED_DATA) #\n");
284 #endif
285  printf("###################################################\n");
286 
287  if (argc != 6) {
288  printf("Usage : %s <node ID> <run#> <nwords of det. buf per FEE> <# of CPR per COPPER> <# of HSLBs>\n", argv[ 0 ]);
289  exit(1);
290  }
291 
292  //
293  // network connection
294  //
295  int listenfd;
296  struct sockaddr_in servaddr;
297  struct pollfd client[NUM_CLIENTS + 1];
298 
299  //
300  // dummy data
301  //
302  unsigned int node_id = 0;
303  sscanf(argv[1], "0x%x", &node_id);
304 
305  int run_no = atoi(argv[2]);
306  int nwords_per_fee = atoi(argv[3]);
307  int ncpr = atoi(argv[4]);
308  int nhslb = atoi(argv[5]);
309 
310  int total_words = NW_SEND_HEADER + NW_SEND_TRAILER +
311  ncpr * (NW_RAW_HEADER + (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb +
312  NW_RAW_TRAILER);
313  printf("TET %d %d %d %d %d\n ", NW_SEND_HEADER + NW_SEND_TRAILER, ncpr,
314  NW_RAW_HEADER, (NW_B2L_HEADER + NW_B2L_TRAILER + nwords_per_fee) * nhslb, NW_RAW_TRAILER);
315  int buff[total_words];
316 
317  //
318  // Prepare header
319  //
320  int temp_ret = fillDataContents(buff, nwords_per_fee, node_id, ncpr, nhslb, run_no);
321  if (temp_ret != total_words) {
322  printf("[FATAL] data-production error 1 %d %d\n", total_words, temp_ret);
323  fflush(stdout);
324  exit(1);
325  }
326 
327  listenfd = socket(AF_INET, SOCK_STREAM, 0);
328  memset(&servaddr, 0, sizeof(servaddr));
329  servaddr.sin_family = AF_INET;
330  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
331  servaddr.sin_port = htons(30000);
332 
333  int flags = 1;
334  int ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)sizeof(flags));
335  if (ret < 0) {
336  perror("Failed to set REUSEADDR");
337  }
338 
339  bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
340 
341  listen(listenfd, LISTENQ);
342 
343  client[0].fd = listenfd;
344  client[0].events = POLLRDNORM;
345 
346  for (int i = 1; i <= NUM_CLIENTS; i++) {
347  client[i].fd = -1;
348  }
349 
350  int maxi = 0;
351  int id = 0;
352  int nconn = 0;
353 
354  while (1) {
355  int nready = poll(client, maxi + 1, -1);
356  if (client[0].revents & POLLRDNORM) {
357  printf("Accepting..."); fflush(stdout);
358  int connfd = accept(listenfd, (struct sockaddr*) NULL, NULL);
359  for (id = 1; id <= NUM_CLIENTS; id++) {
360  if (client[id].fd < 0) {
361  client[id].fd = connfd;
362  client[id].events = POLLRDNORM;
363  nconn++;
364  printf("Done. connections (%d/%d)", nconn, NUM_CLIENTS); fflush(stdout);
365  break;
366  }
367  }
368  if (id > NUM_CLIENTS) {
369  perror("[FATAL] too many clients.");
370  exit(1);
371  }
372  if (id > maxi) maxi = id;
373  if (nconn >= NUM_CLIENTS) break;
374  // if( -nready <= 0 ) continue;
375 
376  } else {
377  printf("No connection request. Still waiting..."); fflush(stdout);
378  }
379  }
380 
381  printf("All connections accepted\n"); fflush(stdout);
382 
383  double init_time = getTimeSec();
384  double prev_time = init_time;
385 
386  unsigned long long int cnt = 0;
387  unsigned long long int prev_cnt = 0;
388  unsigned long long int start_cnt = 300000;
389 
390 #ifdef MAX_EVENT
391  for (int j = 0; j < MAX_EVENT; j++) {
392 #else
393  for (;;) {
394 #endif
395  // addEvent(buff, total_words, cnt);
396  addEvent(buff, nwords_per_fee, cnt, ncpr, nhslb);
397  // printf("cnt %d bytes\n", cnt*total_words); fflush(stdout);
398  // sprintf( buff, "event %d dessa", cnt );
399 
400  // for(int i = 0 ; i < total_words ; i++){
401  // printf("%.8x ", buff[ i ]);
402  // if( i % 10 == 9 ) printf("\n");
403  // }
404 
405  for (int i = 1 ; i <= NUM_CLIENTS ; i++) {
406  int ret = 0;
407  if ((ret = write(client[i].fd, buff, total_words * sizeof(int))) <= 0) {
408  printf("[FATAL] Return value %d\n", ret);
409  fflush(stdout);
410  exit(1);
411  }
412  }
413 
414  cnt++;
415 
416  if (cnt == start_cnt) init_time = getTimeSec();
417  if (cnt % 10000 == 1) {
418  if (cnt > start_cnt) {
419  double cur_time = getTimeSec();
420  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",
421  run_no,
422  cnt,
423  cur_time - init_time,
424  NUM_CLIENTS * (cnt - prev_cnt)*total_words * sizeof(int) / 1000000. / (cur_time - prev_time),
425  (cnt - prev_cnt) / (cur_time - prev_time) / 1000. ,
426  NUM_CLIENTS * (cnt - start_cnt)*total_words * sizeof(int) / 1000000. / (cur_time - init_time),
427  (cnt - start_cnt) / (cur_time - init_time) / 1000. , total_words);
428 
429  fflush(stdout);
430  prev_time = cur_time;
431  prev_cnt = cnt;
432  } else {
433  // printf("Eve %lld\n", cnt);fflush(stdout);
434  }
435  }
436  }
437 
438 
439  for (int i = 1; i <= NUM_CLIENTS; i++) {
440  close(client[i].fd);
441  }
442 
443 
444 }
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75