Belle II Software  release-08-01-10
des_ser_PCIe40_main.h
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 #ifndef DESSERPCIE40MAIN_H
9 #define DESSERPCIE40MAIN_H
10 
11 #include <iostream>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <netinet/in.h>
15 #include <netinet/tcp.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <time.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <arpa/inet.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <poll.h>
26 #include <netdb.h>
27 #include <vector>
28 #include <set>
29 #include <map>
30 #include <cstring>
31 #include <omp.h>
32 #include <mqueue.h>
33 #include <sys/wait.h>
34 #include <sys/ipc.h>
35 #include <sys/shm.h>
36 #include <sys/types.h>
37 #include <fstream>
38 #include <errno.h>
39 #include "pcie40_b2dma.h"
40 #include "dma_driverlib.h"
41 #include "pcie40_s_pages.h"
42 #include <rawdata/dataobjects/RawHeader_latest.h>
43 #include <rawdata/dataobjects/RawTrailer_latest.h>
44 #include <rawdata/dataobjects/PreRawCOPPERFormat_latest.h>
45 #include <rawdata/dataobjects/PostRawCOPPERFormat_latest.h>
46 
47 #define USE_ZMQ
48 #ifdef USE_ZMQ
49 #include "zmq.hpp"
50 #endif
51 
53 // Parameter for operation
55 //#define NOT_SEND
56 #define NUM_CLIENTS_PER_THREAD 1
57 #define NUM_SENDER_THREADS 5
58 //#define NUM_SENDER_THREADS 1
59 //#define NO_ERROR_STOP
60 
61 #define NODE_ID_TEST_BENCH 0x99000001
63 // Parameter for dummy-data
65 //#define DUMMY_REDUCED_DATA
66 
68 // Parameter for split ECL and ECLTRG data on recl3
70 //#define SPLIT_ECL_ECLTRG
71 #ifdef SPLIT_ECL_ECLTRG
72 #define NUM_SUB_EVE 2
73 #else
74 #define NUM_SUB_EVE 1
75 #endif // #ifdef SPLIT_ECL_ECLTRG
76 
78 // Parameter for data-contents
80 
81 //
82 // Format (PCIe40)
83 //
84 #define NW_RAW_HEADER 56
85 #define NW_RAW_TRAILER 4
86 #define NW_REDUCED_B2L_HEADER 2
87 #define NW_REDUCED_B2L_TRAILER 2
88 #define NW_NONREDUCED_B2L_HEADER 6
89 #define NW_NONREDUCED_B2L_TRAILER 3
90 
91 //
92 // Sender format
93 //
94 #define NW_SEND_HEADER 6
95 #define NW_SEND_TRAILER 2
96 
97 
98 //
99 // return value of checkEventData()
100 //
101 #define DATACHECK_OK 0
102 #define DATACHECK_NG 1
103 #define DATACHECK_OK_BUT_ERRFLAG_IN_HDR 2
104 
105 //
106 // Dummy data contents
107 //
108 #define CTIME_VAL 0x12345601
109 #ifdef DUMMY_REDUCED_DATA
110 #define NW_B2L_HEADER 3
111 #define NW_B2L_TRAILER 2
112 #else
113 #define NW_B2L_HEADER 7
114 #define NW_B2L_TRAILER 3
115 #endif
116 
117 #ifdef SPLIT_ECL_ECLTRG
118 #define ECLTRG_NODE_ID 0x13000001
119 #endif
120 
121 struct sender_argv {
122  int sender_id;
123  int run_no; // For dummy data
124  int nwords_per_fee; // For dummy data
125  int ncpr; // For dummy data
126  int nhslb; // For dummy data
127  unsigned int node_id;
128  std::vector< int > valid_ch;
129 };
130 
131 // format of ROB header
132 #define OFFSET_HDR 0
133 #define EVENT_LEN_POS 0+OFFSET_HDR
134 #define MAGIC_7F7F_POS 1+OFFSET_HDR
135 #define RUNNO_POS 2+OFFSET_HDR
136 #define EVENUM_POS 3+OFFSET_HDR
137 #define CTIME_POS 4+OFFSET_HDR
138 #define UTIME_POS 5+OFFSET_HDR
139 #define NODEID_POS 6+OFFSET_HDR
140 #define ERR_POS 7+OFFSET_HDR
141 #define POS_TABLE_POS 8+OFFSET_HDR
142 #define LEN_ROB_HEADER 56
143 #define LEN_ROB_TRAILER 4
144 
145 #define CRC_START_POS 1
146 #define NON_CRC_COUNTS_REDUCED 3
147 #define NON_CRC_COUNTS_NOTREDUCED 4
148 
149 // format b2link header
150 //#define FFAA_POS 0
151 #define LINK_EVE_POS 2
152 #define NO_PROC
153 #define CRC_CHECK
154 
155 #define MAX_EVENT_WORDS 500000 // 2MB
156 #define DMA_HDR_WORDS 8
157 
158 // DMA header format
159 #define DMA_WORDS_OF_256BITS 0
160 #define DMA_HDR_MAGIC 1
161 #define DMA_SIZE_IN_BYTES 2
162 #define MAX_DMA_WORDS_OF_256BITS 0xFF
163 
164 extern "C" int ecs_open(int dev, int bar);
165 extern "C" void ecs_close(int dev, int bar);
166 extern "C" unsigned ecs_read(int dev, int bar, unsigned add) ;
167 extern "C" int ecs_write(int dev, int bar, unsigned add, int val);
168 
169 // struct shm_crc {
170 // int cnt ;
171 // int complete ;
172 // int first_crc ;
173 // int crc_data ;
174 // // unsigned int data[ 10000 ] ;
175 // unsigned int data[ 9998 ] ;
176 // unsigned int eve;
177 // int ch;
178 // };
179 
180 
181 //
182 // CRC calculation
183 //
184 void crc_calc(unsigned int& crc, const unsigned int& data);
185 
186 unsigned int get_crc(unsigned int* data, int length, unsigned int initial_value);
187 
188 unsigned short CalcCRC16LittleEndian(unsigned short crc16, const int buf[], int nwords);
189 
190 //
191 // Get data information
192 //
193 int getEventNumber(unsigned int* data);
194 
195 int get1stChannel(unsigned int*& data);
196 
197 //
198 // Debug print
199 //
200 void printHeader(unsigned int* data);
201 
202 void printTrailer(unsigned int* data);
203 
204 void printData(unsigned int* data);
205 
206 void writeToFile(std::ofstream& the_file, unsigned int* data, int size);
207 
208 void printLine(unsigned int* data, int pos);
209 
210 void printEventData(unsigned int* data);
211 
212 void printEventData(unsigned int* data, int size);
213 
214 void printEventData(unsigned int* data, int size, int sender_id);
215 
216 void printFullData(unsigned int* data);
217 
218 void printEventNumberError(unsigned int*& data, const unsigned int evtnum, const unsigned int exprun, const int eve_diff,
219  const int sender_id);
220 
221 //
222 // Data check
223 //
224 void checkUtimeCtimeTRGType(unsigned int*& data, const int sender_id);
225 
226 int checkDMAHeader(unsigned int*& data, unsigned int& size, double& dsize, int& total_pages, int& index_pages);
227 
228 double getTimeSec();
229 
230 int checkEventData(int sender_id, unsigned int* data, unsigned int event_nwords, unsigned int& exprun,
231  unsigned int& evtnum, unsigned int node_id, std::vector< int > valid_ch);
232 
233 void checkEventGenerator(unsigned int* data, int i, unsigned int size);
234 
235 //
236 // Data manupilation
237 //
238 void reduceHdrTrl(unsigned int* data, unsigned int& event_nwords);
239 
240 int fillDataContents(int* buf, int nwords_per_fee, unsigned int node_id, int ncpr, int nhslb, int run);
241 
242 void addEvent(int* buf, int nwords_per_fee, unsigned int event, int ncpr, int nhslb);
243 
244 void split_Ecltrg(int sender_id, unsigned int* data, std::vector< int > valid_ch,
245  unsigned int* data_main, unsigned int* data_splitted,
246  int& event_nwords_main, int& event_nwords_splitted,
247  unsigned int splitted_node_id, std::vector< int > splitted_ch);
248 
249 void* sender(void* arg);
250 //int sender_id, int run_no, int nwords_per_fee, int ncpr, int nhslb, std::vector< int > valid_ch)
251 
252 #endif //DESSERPCIE40MAIN_H