1 | #include <poll.h>
|
---|
2 | #include <sys/time.h>
|
---|
3 | #include <sys/epoll.h>
|
---|
4 | #include <netinet/tcp.h>
|
---|
5 |
|
---|
6 | #include <cstring>
|
---|
7 | #include <cstdarg>
|
---|
8 | #include <list>
|
---|
9 |
|
---|
10 | #include <boost/algorithm/string/join.hpp>
|
---|
11 |
|
---|
12 | #include "queue.h"
|
---|
13 |
|
---|
14 | #include "MessageImp.h"
|
---|
15 | #include "EventBuilder.h"
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | #define MIN_LEN 32 // min #bytes needed to interpret FADheader
|
---|
20 | #define MAX_LEN 81920 // one max evt = 1024*2*36 + 8*36 + 72 + 4 = 74092 (data+boardheader+eventheader+endflag)
|
---|
21 |
|
---|
22 | //#define COMPLETE_EVENTS
|
---|
23 | //#define USE_POLL
|
---|
24 | //#define USE_EPOLL
|
---|
25 | //#define USE_SELECT
|
---|
26 | //#define COMPLETE_EPOLL
|
---|
27 |
|
---|
28 | // Reading only 1024: 13: 77Hz, 87%
|
---|
29 | // Reading only 1024: 12: 78Hz, 46%
|
---|
30 | // Reading only 300: 4: 250Hz, 92%
|
---|
31 | // Reading only 300: 3: 258Hz, 40%
|
---|
32 |
|
---|
33 | // Reading only four threads 1024: 13: 77Hz, 60%
|
---|
34 | // Reading only four threads 1024: 12: 78Hz, 46%
|
---|
35 | // Reading only four threads 300: 4: 250Hz, 92%
|
---|
36 | // Reading only four threads 300: 3: 258Hz, 40%
|
---|
37 |
|
---|
38 | // Default 300: 4: 249Hz, 92%
|
---|
39 | // Default 300: 3: 261Hz, 40%
|
---|
40 | // Default 1024: 13: 76Hz, 93%
|
---|
41 | // Default 1024: 12: 79Hz, 46%
|
---|
42 |
|
---|
43 | // Poll [selected] 1024: 13: 63Hz, 45%
|
---|
44 | // Poll [selected] 1024: 14: 63Hz, 63%
|
---|
45 | // Poll [selected] 1024: 15: 64Hz, 80%
|
---|
46 | // Poll [selected] 300: 4: 230Hz, 47%
|
---|
47 | // Poll [selected] 300: 3: 200Hz, 94%
|
---|
48 |
|
---|
49 | // Poll [all] 1024: 13: 65Hz, 47%
|
---|
50 | // Poll [all] 1024: 14: 64Hz, 59%
|
---|
51 | // Poll [all] 1024: 15: 62Hz, 67%
|
---|
52 | // Poll [all] 300: 4: 230Hz, 47%
|
---|
53 | // Poll [all] 300: 3: 230Hz, 35%
|
---|
54 |
|
---|
55 | // ==========================================================================
|
---|
56 |
|
---|
57 | bool runOpen(const EVT_CTRL2 &evt);
|
---|
58 | bool runWrite(const EVT_CTRL2 &evt);
|
---|
59 | void runClose();
|
---|
60 | void applyCalib(const EVT_CTRL2 &evt, const size_t &size);
|
---|
61 | void factOut(int severity, const char *message);
|
---|
62 | void factReportIncomplete (uint64_t rep);
|
---|
63 | void gotNewRun(RUN_CTRL2 &run);
|
---|
64 | void runFinished();
|
---|
65 | void factStat(const GUI_STAT &gj);
|
---|
66 | bool eventCheck(const EVT_CTRL2 &evt);
|
---|
67 | void debugHead(void *buf);
|
---|
68 |
|
---|
69 | // ==========================================================================
|
---|
70 |
|
---|
71 | int g_reset;
|
---|
72 |
|
---|
73 | size_t g_maxMem; //maximum memory allowed for buffer
|
---|
74 |
|
---|
75 | FACT_SOCK g_port[NBOARDS]; // .addr=string of IP-addr in dotted-decimal "ddd.ddd.ddd.ddd"
|
---|
76 |
|
---|
77 | uint gi_NumConnect[NBOARDS]; //4 crates * 10 boards
|
---|
78 |
|
---|
79 | GUI_STAT gj;
|
---|
80 |
|
---|
81 | // ==========================================================================
|
---|
82 |
|
---|
83 | namespace Memory
|
---|
84 | {
|
---|
85 | uint64_t inuse = 0;
|
---|
86 | uint64_t allocated = 0;
|
---|
87 |
|
---|
88 | uint64_t max_inuse = 0;
|
---|
89 |
|
---|
90 | std::mutex mtx;
|
---|
91 |
|
---|
92 | std::forward_list<void*> memory;
|
---|
93 |
|
---|
94 | void *malloc()
|
---|
95 | {
|
---|
96 | // No free slot available, next alloc would exceed max memory
|
---|
97 | if (memory.empty() && allocated+MAX_TOT_MEM>g_maxMem)
|
---|
98 | return NULL;
|
---|
99 |
|
---|
100 | // We will return this amount of memory
|
---|
101 | // This is not 100% thread safe, but it is not a super accurate measure anyway
|
---|
102 | inuse += MAX_TOT_MEM;
|
---|
103 | if (inuse>max_inuse)
|
---|
104 | max_inuse = inuse;
|
---|
105 |
|
---|
106 | void *mem = NULL;
|
---|
107 |
|
---|
108 | if (memory.empty())
|
---|
109 | {
|
---|
110 | // No free slot available, allocate a new one
|
---|
111 | allocated += MAX_TOT_MEM;
|
---|
112 | mem = new char[MAX_TOT_MEM];
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | // Get the next free slot from the stack and return it
|
---|
117 | const std::lock_guard<std::mutex> lock(mtx);
|
---|
118 | mem = memory.front();
|
---|
119 | memory.pop_front();
|
---|
120 | }
|
---|
121 |
|
---|
122 | memset(mem, 0, MAX_HEAD_MEM);
|
---|
123 | return mem;
|
---|
124 | };
|
---|
125 |
|
---|
126 | void free(void *mem)
|
---|
127 | {
|
---|
128 | if (!mem)
|
---|
129 | return;
|
---|
130 |
|
---|
131 | // Decrease the amont of memory in use accordingly
|
---|
132 | inuse -= MAX_TOT_MEM;
|
---|
133 |
|
---|
134 | // If the maximum memory has changed, we might be over the limit.
|
---|
135 | // In this case: free a slot
|
---|
136 | if (allocated>g_maxMem)
|
---|
137 | {
|
---|
138 | delete [] (char*)mem;
|
---|
139 | allocated -= MAX_TOT_MEM;
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | const std::lock_guard<std::mutex> lock(mtx);
|
---|
144 | memory.push_front(mem);
|
---|
145 | }
|
---|
146 |
|
---|
147 | };
|
---|
148 |
|
---|
149 | // ==========================================================================
|
---|
150 |
|
---|
151 | void factPrintf(int severity, const char *fmt, ...)
|
---|
152 | {
|
---|
153 | char str[1000];
|
---|
154 |
|
---|
155 | va_list ap;
|
---|
156 | va_start(ap, fmt);
|
---|
157 | vsnprintf(str, 1000, fmt, ap);
|
---|
158 | va_end(ap);
|
---|
159 |
|
---|
160 | factOut(severity, str);
|
---|
161 | }
|
---|
162 |
|
---|
163 | // ==========================================================================
|
---|
164 |
|
---|
165 | struct READ_STRUCT
|
---|
166 | {
|
---|
167 | enum buftyp_t
|
---|
168 | {
|
---|
169 | kStream,
|
---|
170 | kHeader,
|
---|
171 | kData,
|
---|
172 | #ifdef COMPLETE_EVENTS
|
---|
173 | kWait
|
---|
174 | #endif
|
---|
175 | };
|
---|
176 |
|
---|
177 | // ---------- connection ----------
|
---|
178 |
|
---|
179 | static uint activeSockets;
|
---|
180 |
|
---|
181 | int sockId; // socket id (board number)
|
---|
182 | int socket; // socket handle
|
---|
183 | bool connected; // is this socket connected?
|
---|
184 |
|
---|
185 | struct sockaddr_in SockAddr; // Socket address copied from wrapper during socket creation
|
---|
186 |
|
---|
187 | // ------------ epoll -------------
|
---|
188 |
|
---|
189 | static int fd_epoll;
|
---|
190 | static epoll_event events[NBOARDS];
|
---|
191 |
|
---|
192 | static void init();
|
---|
193 | static void close();
|
---|
194 | static int wait();
|
---|
195 | static READ_STRUCT *get(int i) { return reinterpret_cast<READ_STRUCT*>(events[i].data.ptr); }
|
---|
196 |
|
---|
197 | // ------------ buffer ------------
|
---|
198 |
|
---|
199 | buftyp_t bufTyp; // what are we reading at the moment: 0=header 1=data -1=skip ...
|
---|
200 |
|
---|
201 | uint32_t bufLen; // number of bytes left to read
|
---|
202 | uint8_t *bufPos; // next byte to read to the buffer next
|
---|
203 |
|
---|
204 | union
|
---|
205 | {
|
---|
206 | uint8_t B[MAX_LEN];
|
---|
207 | uint16_t S[MAX_LEN / 2];
|
---|
208 | uint32_t I[MAX_LEN / 4];
|
---|
209 | uint64_t L[MAX_LEN / 8];
|
---|
210 | PEVNT_HEADER H;
|
---|
211 | };
|
---|
212 |
|
---|
213 | timeval time;
|
---|
214 | uint64_t rateBytes;
|
---|
215 | uint32_t skip; // number of bytes skipped before start of event
|
---|
216 |
|
---|
217 | uint32_t len() const { return uint32_t(H.package_length)*2; }
|
---|
218 |
|
---|
219 | void swapHeader();
|
---|
220 | void swapData();
|
---|
221 |
|
---|
222 | // --------------------------------
|
---|
223 |
|
---|
224 | READ_STRUCT() : socket(-1), connected(false), rateBytes(0)
|
---|
225 | {
|
---|
226 | if (fd_epoll<0)
|
---|
227 | init();
|
---|
228 | }
|
---|
229 | ~READ_STRUCT()
|
---|
230 | {
|
---|
231 | destroy();
|
---|
232 | }
|
---|
233 |
|
---|
234 | void destroy();
|
---|
235 | bool create(sockaddr_in addr);
|
---|
236 | bool check(int, sockaddr_in addr);
|
---|
237 | bool read();
|
---|
238 | };
|
---|
239 |
|
---|
240 | int READ_STRUCT::wait()
|
---|
241 | {
|
---|
242 | // wait for something to do...
|
---|
243 | const int rc = epoll_wait(fd_epoll, events, NBOARDS, 100); // max, timeout[ms]
|
---|
244 | if (rc>=0)
|
---|
245 | return rc;
|
---|
246 |
|
---|
247 | if (errno==EINTR) // timout or signal interruption
|
---|
248 | return 0;
|
---|
249 |
|
---|
250 | factPrintf(MessageImp::kError, "epoll_wait failed: %m (rc=%d)", errno);
|
---|
251 | return -1;
|
---|
252 | }
|
---|
253 |
|
---|
254 | uint READ_STRUCT::activeSockets = 0;
|
---|
255 | int READ_STRUCT::fd_epoll = -1;
|
---|
256 | epoll_event READ_STRUCT::events[NBOARDS];
|
---|
257 |
|
---|
258 | void READ_STRUCT::init()
|
---|
259 | {
|
---|
260 | if (fd_epoll>=0)
|
---|
261 | return;
|
---|
262 |
|
---|
263 | #ifdef USE_EPOLL
|
---|
264 | fd_epoll = epoll_create(NBOARDS);
|
---|
265 | if (fd_epoll<0)
|
---|
266 | {
|
---|
267 | factPrintf(MessageImp::kError, "Waiting for data failed: %d (epoll_create,rc=%d)", errno);
|
---|
268 | return;
|
---|
269 | }
|
---|
270 | #endif
|
---|
271 | }
|
---|
272 |
|
---|
273 | void READ_STRUCT::close()
|
---|
274 | {
|
---|
275 | #ifdef USE_EPOLL
|
---|
276 | if (fd_epoll>=0 && ::close(fd_epoll)>0)
|
---|
277 | factPrintf(MessageImp::kFatal, "Closing epoll failed: %m (close,rc=%d)", errno);
|
---|
278 | #endif
|
---|
279 |
|
---|
280 | fd_epoll = -1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | bool READ_STRUCT::create(sockaddr_in sockAddr)
|
---|
284 | {
|
---|
285 | if (socket>=0)
|
---|
286 | return false;
|
---|
287 |
|
---|
288 | const int port = ntohs(sockAddr.sin_port) + 1;
|
---|
289 |
|
---|
290 | SockAddr.sin_family = sockAddr.sin_family;
|
---|
291 | SockAddr.sin_addr = sockAddr.sin_addr;
|
---|
292 | SockAddr.sin_port = htons(port);
|
---|
293 |
|
---|
294 | if ((socket = ::socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0)
|
---|
295 | {
|
---|
296 | factPrintf(MessageImp::kFatal, "Generating socket %d failed: %m (socket,rc=%d)", sockId, errno);
|
---|
297 | socket = -1;
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | int optval = 1;
|
---|
302 | if (setsockopt (socket, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(int)) < 0)
|
---|
303 | factPrintf(MessageImp::kInfo, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sockId, errno);
|
---|
304 |
|
---|
305 | optval = 10; //start after 10 seconds
|
---|
306 | if (setsockopt (socket, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(int)) < 0)
|
---|
307 | factPrintf(MessageImp::kInfo, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sockId, errno);
|
---|
308 |
|
---|
309 | optval = 10; //do every 10 seconds
|
---|
310 | if (setsockopt (socket, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(int)) < 0)
|
---|
311 | factPrintf(MessageImp::kInfo, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sockId, errno);
|
---|
312 |
|
---|
313 | optval = 2; //close after 2 unsuccessful tries
|
---|
314 | if (setsockopt (socket, SOL_TCP, TCP_KEEPCNT, &optval, sizeof(int)) < 0)
|
---|
315 | factPrintf(MessageImp::kInfo, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sockId, errno);
|
---|
316 |
|
---|
317 | factPrintf(MessageImp::kInfo, "Generated socket %d (%d)", sockId, socket);
|
---|
318 |
|
---|
319 | //connected = false;
|
---|
320 | activeSockets++;
|
---|
321 |
|
---|
322 | return true;
|
---|
323 | }
|
---|
324 |
|
---|
325 | void READ_STRUCT::destroy()
|
---|
326 | {
|
---|
327 | if (socket<0)
|
---|
328 | return;
|
---|
329 |
|
---|
330 | #ifdef USE_EPOLL
|
---|
331 | // strictly speaking this should not be necessary
|
---|
332 | if (fd_epoll>=0 && connected && epoll_ctl(fd_epoll, EPOLL_CTL_DEL, socket, NULL)<0)
|
---|
333 | factPrintf(MessageImp::kError, "epoll_ctrl failed: %m (EPOLL_CTL_DEL,rc=%d)", errno);
|
---|
334 | #endif
|
---|
335 |
|
---|
336 | if (::close(socket) > 0)
|
---|
337 | factPrintf(MessageImp::kFatal, "Closing socket %d failed: %m (close,rc=%d)", sockId, errno);
|
---|
338 | else
|
---|
339 | factPrintf(MessageImp::kInfo, "Closed socket %d (%d)", sockId, socket);
|
---|
340 |
|
---|
341 | socket = -1;
|
---|
342 | connected = false;
|
---|
343 | activeSockets--;
|
---|
344 | }
|
---|
345 |
|
---|
346 | bool READ_STRUCT::check(int sockDef, sockaddr_in addr)
|
---|
347 | {
|
---|
348 | // Continue in the most most likely case (performance)
|
---|
349 | //if (socket>=0 && sockDef!=0 && connected)
|
---|
350 | // return;
|
---|
351 | const int old = socket;
|
---|
352 |
|
---|
353 | // socket open, but should not be open
|
---|
354 | if (socket>=0 && sockDef==0)
|
---|
355 | destroy();
|
---|
356 |
|
---|
357 | // Socket closed, but should be open
|
---|
358 | if (socket<0 && sockDef!=0)
|
---|
359 | create(addr); //generate address and socket
|
---|
360 |
|
---|
361 | const bool retval = old!=socket;
|
---|
362 |
|
---|
363 | // Socket closed
|
---|
364 | if (socket<0)
|
---|
365 | return retval;
|
---|
366 |
|
---|
367 | // Socket open and connected: Nothing to do
|
---|
368 | if (connected)
|
---|
369 | return retval;
|
---|
370 |
|
---|
371 | //try to connect if not yet done
|
---|
372 | const int rc = connect(socket, (struct sockaddr *) &SockAddr, sizeof(SockAddr));
|
---|
373 | if (rc == -1)
|
---|
374 | return retval;
|
---|
375 |
|
---|
376 | connected = true;
|
---|
377 |
|
---|
378 | if (sockDef<0)
|
---|
379 | {
|
---|
380 | bufTyp = READ_STRUCT::kStream; // full data to be skipped
|
---|
381 | bufLen = MAX_LEN; // huge for skipping
|
---|
382 | }
|
---|
383 | else
|
---|
384 | {
|
---|
385 | bufTyp = READ_STRUCT::kHeader; // expect a header
|
---|
386 | bufLen = sizeof(PEVNT_HEADER); // max size to read at begining
|
---|
387 | }
|
---|
388 |
|
---|
389 | bufPos = B; // no byte read so far
|
---|
390 | skip = 0; // start empty
|
---|
391 |
|
---|
392 | factPrintf(MessageImp::kInfo, "Connected socket %d (%d)", sockId, socket);
|
---|
393 |
|
---|
394 | #ifdef USE_EPOLL
|
---|
395 | epoll_event ev;
|
---|
396 | ev.events = EPOLLIN;
|
---|
397 | ev.data.ptr = this; // user data (union: ev.ptr)
|
---|
398 | if (epoll_ctl(fd_epoll, EPOLL_CTL_ADD, socket, &ev)<0)
|
---|
399 | factPrintf(MessageImp::kError, "epoll_ctl failed: %m (EPOLL_CTL_ADD,rc=%d)", errno);
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | return retval;
|
---|
403 | }
|
---|
404 |
|
---|
405 | bool READ_STRUCT::read()
|
---|
406 | {
|
---|
407 | if (!connected)
|
---|
408 | return false;
|
---|
409 |
|
---|
410 | if (bufLen==0)
|
---|
411 | return true;
|
---|
412 |
|
---|
413 | const int32_t jrd = recv(socket, bufPos, bufLen, MSG_DONTWAIT);
|
---|
414 | // recv failed
|
---|
415 | if (jrd<0)
|
---|
416 | {
|
---|
417 | // There was just nothing waiting
|
---|
418 | if (errno==EWOULDBLOCK || errno==EAGAIN)
|
---|
419 | return false;
|
---|
420 |
|
---|
421 | factPrintf(MessageImp::kError, "Reading from socket %d failed: %m (recv,rc=%d)", sockId, errno);
|
---|
422 | return false;
|
---|
423 | }
|
---|
424 |
|
---|
425 | // connection was closed ...
|
---|
426 | if (jrd==0)
|
---|
427 | {
|
---|
428 | factPrintf(MessageImp::kInfo, "Socket %d closed by FAD", sockId);
|
---|
429 |
|
---|
430 | destroy();//DestroySocket(rd[i]); //generate address and socket
|
---|
431 | return false;
|
---|
432 | }
|
---|
433 |
|
---|
434 | rateBytes += jrd;
|
---|
435 |
|
---|
436 | // are we skipping this board ...
|
---|
437 | if (bufTyp==kStream)
|
---|
438 | return false;
|
---|
439 |
|
---|
440 | if (bufPos==B)
|
---|
441 | gettimeofday(&time, NULL);
|
---|
442 |
|
---|
443 | bufPos += jrd; //==> prepare for continuation
|
---|
444 | bufLen -= jrd;
|
---|
445 |
|
---|
446 | // not yet all read
|
---|
447 | return bufLen==0;
|
---|
448 | }
|
---|
449 |
|
---|
450 | void READ_STRUCT::swapHeader()
|
---|
451 | {
|
---|
452 | S[1] = ntohs(S[1]); // package_length (bytes not swapped!)
|
---|
453 | S[2] = ntohs(S[2]); // version_no
|
---|
454 | S[3] = ntohs(S[3]); // PLLLCK
|
---|
455 | S[4] = ntohs(S[4]); // trigger_crc
|
---|
456 | S[5] = ntohs(S[5]); // trigger_type
|
---|
457 |
|
---|
458 | I[3] = ntohl(I[3]); // trigger_id
|
---|
459 | I[4] = ntohl(I[4]); // fad_evt_counter
|
---|
460 | I[5] = ntohl(I[5]); // REFCLK_frequency
|
---|
461 |
|
---|
462 | S[12] = ntohs(S[12]); // board id
|
---|
463 | S[13] = ntohs(S[13]); // adc_clock_phase_shift
|
---|
464 | S[14] = ntohs(S[14]); // number_of_triggers_to_generate
|
---|
465 | S[15] = ntohs(S[15]); // trigger_generator_prescaler
|
---|
466 |
|
---|
467 | I[10] = ntohl(I[10]); // runnumber;
|
---|
468 | I[11] = ntohl(I[11]); // time;
|
---|
469 |
|
---|
470 | // Use back inserter??
|
---|
471 | for (int s=24; s<24+NTemp+NDAC; s++)
|
---|
472 | S[s] = ntohs(S[s]); // drs_temperature / dac
|
---|
473 | }
|
---|
474 |
|
---|
475 | void READ_STRUCT::swapData()
|
---|
476 | {
|
---|
477 | // swapEventHeaderBytes: End of the header. to channels now
|
---|
478 |
|
---|
479 | int i = 36;
|
---|
480 | for (int ePatchesCount = 0; ePatchesCount<4*9; ePatchesCount++)
|
---|
481 | {
|
---|
482 | S[i+0] = ntohs(S[i+0]);//id
|
---|
483 | S[i+1] = ntohs(S[i+1]);//start_cell
|
---|
484 | S[i+2] = ntohs(S[i+2]);//roi
|
---|
485 | S[i+3] = ntohs(S[i+3]);//filling
|
---|
486 |
|
---|
487 | i += 4+S[i+2];//skip the pixel data
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | // ==========================================================================
|
---|
492 |
|
---|
493 | bool checkRoiConsistency(const READ_STRUCT &rd, uint16_t roi[])
|
---|
494 | {
|
---|
495 | int xjr = -1;
|
---|
496 | int xkr = -1;
|
---|
497 |
|
---|
498 | //points to the very first roi
|
---|
499 | int roiPtr = sizeof(PEVNT_HEADER)/2 + 2;
|
---|
500 |
|
---|
501 | roi[0] = ntohs(rd.S[roiPtr]);
|
---|
502 |
|
---|
503 | for (int jr = 0; jr < 9; jr++)
|
---|
504 | {
|
---|
505 | roi[jr] = ntohs(rd.S[roiPtr]);
|
---|
506 |
|
---|
507 | if (roi[jr]>1024)
|
---|
508 | {
|
---|
509 | factPrintf(MessageImp::kError, "Illegal roi in channel %d (allowed: roi<=1024)", jr, roi[jr]);
|
---|
510 | return false;
|
---|
511 | }
|
---|
512 |
|
---|
513 | // Check that the roi of pixels jr are compatible with the one of pixel 0
|
---|
514 | if (jr!=8 && roi[jr]!=roi[0])
|
---|
515 | {
|
---|
516 | xjr = jr;
|
---|
517 | break;
|
---|
518 | }
|
---|
519 |
|
---|
520 | // Check that the roi of all other DRS chips on boards are compatible
|
---|
521 | for (int kr = 1; kr < 4; kr++)
|
---|
522 | {
|
---|
523 | const int kroi = ntohs(rd.S[roiPtr]);
|
---|
524 | if (kroi != roi[jr])
|
---|
525 | {
|
---|
526 | xjr = jr;
|
---|
527 | xkr = kr;
|
---|
528 | break;
|
---|
529 | }
|
---|
530 | roiPtr += kroi+4;
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | if (xjr>=0)
|
---|
535 | {
|
---|
536 | if (xkr<0)
|
---|
537 | factPrintf(MessageImp::kFatal, "Inconsistent Roi accross chips [DRS=%d], expected %d, got %d", xjr, roi[0], roi[xjr]);
|
---|
538 | else
|
---|
539 | factPrintf(MessageImp::kFatal, "Inconsistent Roi accross channels [DRS=%d Ch=%d], expected %d, got %d", xjr, xkr, roi[xjr], ntohs(rd.S[roiPtr]));
|
---|
540 |
|
---|
541 | return false;
|
---|
542 | }
|
---|
543 |
|
---|
544 | if (roi[8] < roi[0])
|
---|
545 | {
|
---|
546 | factPrintf(MessageImp::kError, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", roi[8], roi[0]);
|
---|
547 | return false;
|
---|
548 | }
|
---|
549 |
|
---|
550 | return true;
|
---|
551 | }
|
---|
552 |
|
---|
553 | list<shared_ptr<EVT_CTRL2>> evtCtrl;
|
---|
554 |
|
---|
555 | shared_ptr<EVT_CTRL2> mBufEvt(const READ_STRUCT &rd, shared_ptr<RUN_CTRL2> &actrun)
|
---|
556 | {
|
---|
557 | /*
|
---|
558 | checkroi consistence
|
---|
559 | find existing entry
|
---|
560 | if no entry, try to allocate memory
|
---|
561 | if entry and memory, init event structure
|
---|
562 | */
|
---|
563 |
|
---|
564 | uint16_t nRoi[9];
|
---|
565 | if (!checkRoiConsistency(rd, nRoi))
|
---|
566 | return shared_ptr<EVT_CTRL2>();
|
---|
567 |
|
---|
568 | for (auto it=evtCtrl.rbegin(); it!=evtCtrl.rend(); it++)
|
---|
569 | {
|
---|
570 | // A reference is enough because the evtCtrl holds the shared_ptr anyway
|
---|
571 | const shared_ptr<EVT_CTRL2> &evt = *it;
|
---|
572 |
|
---|
573 | // If the run is different, go on searching.
|
---|
574 | // We cannot stop searching if a lower run-id is found as in
|
---|
575 | // the case of the events, because theoretically, there
|
---|
576 | // can be the same run on two different days.
|
---|
577 | if (rd.H.runnumber != evt->runNum)
|
---|
578 | continue;
|
---|
579 |
|
---|
580 | // If the ID of the new event if higher than the last one stored
|
---|
581 | // in that run, we have to assign a new slot (leave the loop)
|
---|
582 | if (rd.H.fad_evt_counter > evt->evNum/* && runID == evtCtrl[k].runNum*/)
|
---|
583 | break;
|
---|
584 |
|
---|
585 | if (rd.H.fad_evt_counter != evt->evNum/* || runID != evtCtrl[k].runNum*/)
|
---|
586 | continue;
|
---|
587 |
|
---|
588 | // We have found an entry with the same runID and evtID
|
---|
589 | // Check if ROI is consistent
|
---|
590 | if (evt->nRoi != nRoi[0] || evt->nRoiTM != nRoi[8])
|
---|
591 | {
|
---|
592 | factPrintf(MessageImp::kError, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
|
---|
593 | evt->nRoi, evt->nRoiTM, nRoi[0], nRoi[8]);
|
---|
594 | return shared_ptr<EVT_CTRL2>();
|
---|
595 | }
|
---|
596 |
|
---|
597 | // count for inconsistencies
|
---|
598 | if (evt->trgNum != rd.H.trigger_id)
|
---|
599 | evt->Errors[0]++;
|
---|
600 | if (evt->trgTyp != rd.H.trigger_type)
|
---|
601 | evt->Errors[2]++;
|
---|
602 |
|
---|
603 | // It is maybe not likely, but the header of this board might have
|
---|
604 | // arrived earlier. (We could also update the run-info, but
|
---|
605 | // this should not make a difference here)
|
---|
606 | if ((rd.time.tv_sec==evt->time.tv_sec && rd.time.tv_usec<evt->time.tv_usec) ||
|
---|
607 | rd.time.tv_sec<evt->time.tv_sec)
|
---|
608 | evt->time = rd.time;
|
---|
609 |
|
---|
610 | //everything seems fine so far ==> use this slot ....
|
---|
611 | return evt;
|
---|
612 | }
|
---|
613 |
|
---|
614 | if (actrun->runId==rd.H.runnumber && (actrun->roi0 != nRoi[0] || actrun->roi8 != nRoi[8]))
|
---|
615 | {
|
---|
616 | factPrintf(MessageImp::kError, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d (runID=%d, evID=%d)",
|
---|
617 | actrun->roi0, actrun->roi8, nRoi[0], nRoi[8], rd.H.runnumber, rd.H.fad_evt_counter);
|
---|
618 | return shared_ptr<EVT_CTRL2>();
|
---|
619 | }
|
---|
620 |
|
---|
621 | EVT_CTRL2 *evt = new EVT_CTRL2;
|
---|
622 |
|
---|
623 | evt->time = rd.time;
|
---|
624 |
|
---|
625 | evt->runNum = rd.H.runnumber;
|
---|
626 | evt->evNum = rd.H.fad_evt_counter;
|
---|
627 |
|
---|
628 | evt->trgNum = rd.H.trigger_id;
|
---|
629 | evt->trgTyp = rd.H.trigger_type;
|
---|
630 |
|
---|
631 | evt->nRoi = nRoi[0];
|
---|
632 | evt->nRoiTM = nRoi[8];
|
---|
633 |
|
---|
634 | const bool newrun = actrun->runId != rd.H.runnumber;
|
---|
635 | if (newrun)
|
---|
636 | {
|
---|
637 | // Since we have started a new run, we know already when to close the
|
---|
638 | // previous run in terms of number of events
|
---|
639 | actrun->maxEvt = actrun->lastEvt;
|
---|
640 |
|
---|
641 | factPrintf(MessageImp::kInfo, "New run %d (evt=%d) registered with roi=%d(%d), prev=%d",
|
---|
642 | rd.H.runnumber, rd.H.fad_evt_counter, nRoi[0], nRoi[8], actrun->runId);
|
---|
643 |
|
---|
644 | // The new run is the active run now
|
---|
645 | actrun = shared_ptr<RUN_CTRL2>(new RUN_CTRL2);
|
---|
646 |
|
---|
647 | const time_t &tsec = evt->time.tv_sec;
|
---|
648 |
|
---|
649 | actrun->openTime = tsec;
|
---|
650 | actrun->closeTime = tsec + 3600 * 24; // max time allowed
|
---|
651 | actrun->runId = rd.H.runnumber;
|
---|
652 | actrun->roi0 = nRoi[0]; // FIXME: Make obsolete!
|
---|
653 | actrun->roi8 = nRoi[8]; // FIXME: Make obsolete!
|
---|
654 |
|
---|
655 | // Signal the fadctrl that a new run has been started
|
---|
656 | // Note this is the only place at which we can ensure that
|
---|
657 | // gotnewRun is called only once
|
---|
658 | gotNewRun(*actrun);
|
---|
659 | }
|
---|
660 |
|
---|
661 | // Keep pointer to run of this event
|
---|
662 | evt->runCtrl = actrun;
|
---|
663 |
|
---|
664 | // Increase the number of events we have started to receive in this run
|
---|
665 | actrun->lastTime = evt->time.tv_sec; // Time when the last event was received
|
---|
666 | actrun->lastEvt++;
|
---|
667 |
|
---|
668 | // An event can be the first and the last, but not the last and the first.
|
---|
669 | // Therefore gotNewRun is called before runFinished.
|
---|
670 | // runFinished signals that the last event of a run was just received. Processing
|
---|
671 | // might still be ongoing, but we can start a new run.
|
---|
672 | const bool cond1 = actrun->lastEvt < actrun->maxEvt; // max number of events not reached
|
---|
673 | const bool cond2 = actrun->lastTime < actrun->closeTime; // max time not reached
|
---|
674 | if (!cond1 || !cond2)
|
---|
675 | runFinished();
|
---|
676 |
|
---|
677 | // We don't mind here that this is not common to all events,
|
---|
678 | // because every coming event will fullfil the condition as well.
|
---|
679 | if (!cond1)
|
---|
680 | evt->closeRequest |= kRequestMaxEvtsReached;
|
---|
681 | if (!cond2)
|
---|
682 | evt->closeRequest |= kRequestMaxTimeReached;
|
---|
683 |
|
---|
684 | // Secure access to evtCtrl against access in CloseRunFile
|
---|
685 | // This should be the last... otherwise we can run into threading issues
|
---|
686 | // if the event is accessed before it is fully initialized.
|
---|
687 | evtCtrl.emplace_back(evt);
|
---|
688 | return evtCtrl.back();
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | void copyData(const READ_STRUCT &rBuf, EVT_CTRL2 *evt)
|
---|
693 | {
|
---|
694 | const int i = rBuf.sockId;
|
---|
695 |
|
---|
696 | memcpy(evt->FADhead+i, &rBuf.H, sizeof(PEVNT_HEADER));
|
---|
697 |
|
---|
698 | int src = sizeof(PEVNT_HEADER) / 2; // Header is 72 byte = 36 shorts
|
---|
699 |
|
---|
700 | // consistency of ROIs have been checked already (is it all correct?)
|
---|
701 | const uint16_t &roi = rBuf.S[src+2];
|
---|
702 |
|
---|
703 | // different sort in FAD board.....
|
---|
704 | EVENT *event = evt->fEvent;
|
---|
705 | for (int px = 0; px < 9; px++)
|
---|
706 | {
|
---|
707 | for (int drs = 0; drs < 4; drs++)
|
---|
708 | {
|
---|
709 | const int16_t pixC = rBuf.S[src+1]; // start-cell
|
---|
710 | const int16_t pixR = rBuf.S[src+2]; // roi
|
---|
711 | //here we should check if pixH is correct ....
|
---|
712 |
|
---|
713 | const int pixS = i*36 + drs*9 + px;
|
---|
714 |
|
---|
715 | event->StartPix[pixS] = pixC;
|
---|
716 |
|
---|
717 | memcpy(event->Adc_Data + pixS*roi, &rBuf.S[src+4], roi * 2);
|
---|
718 |
|
---|
719 | src += 4+pixR;
|
---|
720 |
|
---|
721 | // Treatment for ch 9 (TM channel)
|
---|
722 | if (px != 8)
|
---|
723 | continue;
|
---|
724 |
|
---|
725 | const int tmS = i*4 + drs;
|
---|
726 |
|
---|
727 | //and we have additional TM info
|
---|
728 | if (pixR > roi)
|
---|
729 | {
|
---|
730 | event->StartTM[tmS] = (pixC + pixR - roi) % 1024;
|
---|
731 |
|
---|
732 | memcpy(event->Adc_Data + tmS*roi + NPIX*roi, &rBuf.S[src - roi], roi * 2);
|
---|
733 | }
|
---|
734 | else
|
---|
735 | {
|
---|
736 | event->StartTM[tmS] = -1;
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | // ==========================================================================
|
---|
743 |
|
---|
744 | uint64_t reportIncomplete(const shared_ptr<EVT_CTRL2> &evt, const char *txt)
|
---|
745 | {
|
---|
746 | factPrintf(MessageImp::kWarn, "skip incomplete evt (run=%d, evt=%d, n=%d, %s)",
|
---|
747 | evt->runNum, evt->evNum, evtCtrl.size(), txt);
|
---|
748 |
|
---|
749 | uint64_t report = 0;
|
---|
750 |
|
---|
751 | char str[1000];
|
---|
752 |
|
---|
753 | int ik=0;
|
---|
754 | for (int ib=0; ib<NBOARDS; ib++)
|
---|
755 | {
|
---|
756 | if (ib%10==0)
|
---|
757 | str[ik++] = '|';
|
---|
758 |
|
---|
759 | const int jb = evt->board[ib];
|
---|
760 | if (jb>=0) // data received from that board
|
---|
761 | {
|
---|
762 | str[ik++] = '0'+(jb%10);
|
---|
763 | continue;
|
---|
764 | }
|
---|
765 |
|
---|
766 | // FIXME: This is not synchronous... it reports
|
---|
767 | // accoridng to the current connection status, not w.r.t. to the
|
---|
768 | // one when the event was taken.
|
---|
769 | if (gi_NumConnect[ib]==0) // board not connected
|
---|
770 | {
|
---|
771 | str[ik++] = 'x';
|
---|
772 | continue;
|
---|
773 | }
|
---|
774 |
|
---|
775 | // data from this board lost
|
---|
776 | str[ik++] = '.';
|
---|
777 | report |= ((uint64_t)1)<<ib;
|
---|
778 | }
|
---|
779 |
|
---|
780 | str[ik++] = '|';
|
---|
781 | str[ik] = 0;
|
---|
782 |
|
---|
783 | factOut(MessageImp::kWarn, str);
|
---|
784 |
|
---|
785 | return report;
|
---|
786 | }
|
---|
787 |
|
---|
788 | // ==========================================================================
|
---|
789 | // ==========================================================================
|
---|
790 |
|
---|
791 | void proc1(const shared_ptr<EVT_CTRL2> &);
|
---|
792 |
|
---|
793 | Queue<shared_ptr<EVT_CTRL2>> processingQueue1(bind(&proc1, placeholders::_1));
|
---|
794 |
|
---|
795 | void proc1(const shared_ptr<EVT_CTRL2> &evt)
|
---|
796 | {
|
---|
797 | applyCalib(*evt, processingQueue1.size());
|
---|
798 | }
|
---|
799 |
|
---|
800 | // If this is not convenient anymore, it could be replaced by
|
---|
801 | // a command queue, to which command+data is posted,
|
---|
802 | // (e.g. runOpen+runInfo, runClose+runInfo, evtWrite+evtInfo)
|
---|
803 | void writeEvt(const shared_ptr<EVT_CTRL2> &evt)
|
---|
804 | {
|
---|
805 | //const shared_ptr<RUN_CTRL2> &run = evt->runCtrl;
|
---|
806 | RUN_CTRL2 &run = *evt->runCtrl;
|
---|
807 |
|
---|
808 | // Is this a valid event or just an empty event to trigger run close?
|
---|
809 | // If this is not an empty event open the new run-file
|
---|
810 | // Empty events are there to trigger run-closing conditions
|
---|
811 | if (evt->valid())
|
---|
812 | {
|
---|
813 | // File not yet open
|
---|
814 | if (run.fileStat==kFileNotYetOpen)
|
---|
815 | {
|
---|
816 | // runOpen will close a previous run, if still open
|
---|
817 | if (!runOpen(*evt))
|
---|
818 | {
|
---|
819 | factPrintf(MessageImp::kError, "Could not open new file for run %d (evt=%d, runOpen failed)", evt->runNum, evt->evNum);
|
---|
820 | run.fileStat = kFileClosed;
|
---|
821 | return;
|
---|
822 | }
|
---|
823 |
|
---|
824 | factPrintf(MessageImp::kInfo, "Opened new file for run %d (evt=%d)", evt->runNum, evt->evNum);
|
---|
825 | run.fileStat = kFileOpen;
|
---|
826 | }
|
---|
827 |
|
---|
828 | // Here we have a valid calibration and can go on with that.
|
---|
829 | // It is important that _all_ events are sent for calibration (except broken ones)
|
---|
830 | processingQueue1.post(evt);
|
---|
831 | }
|
---|
832 |
|
---|
833 | // File already closed
|
---|
834 | if (run.fileStat==kFileClosed)
|
---|
835 | return;
|
---|
836 |
|
---|
837 | bool rc1 = true;
|
---|
838 | if (evt->valid())
|
---|
839 | {
|
---|
840 | rc1 = runWrite(*evt);
|
---|
841 | if (!rc1)
|
---|
842 | factPrintf(MessageImp::kError, "Writing event %d for run %d failed (runWrite)", evt->evNum, evt->runNum);
|
---|
843 | }
|
---|
844 |
|
---|
845 | // File not open... no need to close or to check for close
|
---|
846 | // ... this is the case if CloseRunFile was called before any file was opened.
|
---|
847 | if (run.fileStat!=kFileOpen)
|
---|
848 | return;
|
---|
849 |
|
---|
850 | // File is not yet to be closed.
|
---|
851 | if (rc1 && evt->closeRequest==kRequestNone)
|
---|
852 | return;
|
---|
853 |
|
---|
854 | runClose();
|
---|
855 | run.fileStat = kFileClosed;
|
---|
856 |
|
---|
857 | vector<string> reason;
|
---|
858 | if (evt->closeRequest&kRequestManual)
|
---|
859 | reason.emplace_back("close requested");
|
---|
860 | if (evt->closeRequest&kRequestTimeout)
|
---|
861 | reason.emplace_back("receive timeout");
|
---|
862 | if (evt->closeRequest&kRequestConnectionChange)
|
---|
863 | reason.emplace_back("connection changed");
|
---|
864 | if (evt->closeRequest&kRequestEventCheckFailed)
|
---|
865 | reason.emplace_back("event check failed");
|
---|
866 | if (evt->closeRequest&kRequestMaxTimeReached)
|
---|
867 | reason.push_back(to_string(run.closeTime-run.openTime)+"s reached");
|
---|
868 | if (evt->closeRequest&kRequestMaxEvtsReached)
|
---|
869 | reason.push_back(to_string(run.maxEvt)+" evts reached");
|
---|
870 | if (!rc1)
|
---|
871 | reason.emplace_back("runWrite failed");
|
---|
872 |
|
---|
873 | const string str = boost::algorithm::join(reason, ", ");
|
---|
874 | factPrintf(MessageImp::kInfo, "File closed because %s", str.c_str());
|
---|
875 | }
|
---|
876 |
|
---|
877 | Queue<shared_ptr<EVT_CTRL2>> secondaryQueue(bind(&writeEvt, placeholders::_1));
|
---|
878 |
|
---|
879 | void procEvt(const shared_ptr<EVT_CTRL2> &evt)
|
---|
880 | {
|
---|
881 | if (evt->valid())
|
---|
882 | {
|
---|
883 | EVENT *event = evt->fEvent;
|
---|
884 |
|
---|
885 | // This is already done in initMemory()
|
---|
886 | //event->Roi = evt->runCtrl->roi0;
|
---|
887 | //event->RoiTM = evt->runCtrl->roi8;
|
---|
888 | //event->EventNum = evt->evNum;
|
---|
889 | //event->TriggerNum = evt->trgNum;
|
---|
890 | //event->TriggerType = evt->trgTyp;
|
---|
891 |
|
---|
892 | event->NumBoards = evt->nBoard;
|
---|
893 |
|
---|
894 | event->Errors[0] = evt->Errors[0];
|
---|
895 | event->Errors[1] = evt->Errors[1];
|
---|
896 | event->Errors[2] = evt->Errors[2];
|
---|
897 | event->Errors[3] = evt->Errors[3];
|
---|
898 |
|
---|
899 | event->PCTime = evt->time.tv_sec;
|
---|
900 | event->PCUsec = evt->time.tv_usec;
|
---|
901 |
|
---|
902 | for (int ib=0; ib<NBOARDS; ib++)
|
---|
903 | event->BoardTime[ib] = evt->FADhead[ib].time;
|
---|
904 |
|
---|
905 | if (!eventCheck(*evt))
|
---|
906 | {
|
---|
907 | secondaryQueue.emplace(new EVT_CTRL2(kRequestEventCheckFailed, evt->runCtrl));
|
---|
908 | return;
|
---|
909 | }
|
---|
910 | }
|
---|
911 |
|
---|
912 | // If file is open post the event for being written
|
---|
913 | secondaryQueue.post(evt);
|
---|
914 | }
|
---|
915 |
|
---|
916 | // ==========================================================================
|
---|
917 | // ==========================================================================
|
---|
918 |
|
---|
919 | /*
|
---|
920 | task 1-4:
|
---|
921 |
|
---|
922 | lock1()-lock4();
|
---|
923 | while (1)
|
---|
924 | {
|
---|
925 | wait for signal [lockN]; // unlocked
|
---|
926 |
|
---|
927 | while (n!=10)
|
---|
928 | wait sockets;
|
---|
929 | read;
|
---|
930 |
|
---|
931 | lockM();
|
---|
932 | finished[n] = true;
|
---|
933 | signal(mainloop);
|
---|
934 | unlockM();
|
---|
935 | }
|
---|
936 |
|
---|
937 |
|
---|
938 | mainloop:
|
---|
939 |
|
---|
940 | while (1)
|
---|
941 | {
|
---|
942 | lockM();
|
---|
943 | while (!finished[0] || !finished[1] ...)
|
---|
944 | wait for signal [lockM]; // unlocked... signals can be sent
|
---|
945 | finished[0-1] = false;
|
---|
946 | unlockM()
|
---|
947 |
|
---|
948 | copy data to queue // locked
|
---|
949 |
|
---|
950 | lockN[0-3];
|
---|
951 | signalN[0-3];
|
---|
952 | unlockN[0-3];
|
---|
953 | }
|
---|
954 |
|
---|
955 |
|
---|
956 | */
|
---|
957 |
|
---|
958 | /*
|
---|
959 | while (g_reset)
|
---|
960 | {
|
---|
961 | shared_ptr<EVT_CTRL2> evt = new shared_ptr<>;
|
---|
962 |
|
---|
963 | // Check that all sockets are connected
|
---|
964 |
|
---|
965 | for (int i=0; i<40; i++)
|
---|
966 | if (rd[i].connected && epoll_ctl(fd_epoll, EPOLL_CTL_ADD, socket, NULL)<0)
|
---|
967 | factPrintf(kError, "epoll_ctrl failed: %m (EPOLL_CTL_ADD,rc=%d)", errno);
|
---|
968 |
|
---|
969 | while (g_reset)
|
---|
970 | {
|
---|
971 | if (READ_STRUCT::wait()<0)
|
---|
972 | break;
|
---|
973 |
|
---|
974 | if (rc_epoll==0)
|
---|
975 | break;
|
---|
976 |
|
---|
977 | for (int jj=0; jj<rc_epoll; jj++)
|
---|
978 | {
|
---|
979 | READ_STRUCT *rs = READ_STRUCT::get(jj);
|
---|
980 | if (!rs->connected)
|
---|
981 | continue;
|
---|
982 |
|
---|
983 | const bool rc_read = rs->read();
|
---|
984 | if (!rc_read)
|
---|
985 | continue;
|
---|
986 |
|
---|
987 | if (rs->bufTyp==READ_STRUCT::kHeader)
|
---|
988 | {
|
---|
989 | [...]
|
---|
990 | }
|
---|
991 |
|
---|
992 | [...]
|
---|
993 |
|
---|
994 | if (epoll_ctl(fd_epoll, EPOLL_CTL_DEL, socket, NULL)<0)
|
---|
995 | factPrintf(kError, "epoll_ctrl failed: %m (EPOLL_CTL_DEL,rc=%d)", errno);
|
---|
996 | }
|
---|
997 |
|
---|
998 | if (once_a_second)
|
---|
999 | {
|
---|
1000 | if (evt==timeout)
|
---|
1001 | break;
|
---|
1002 | }
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | if (evt.nBoards==actBoards)
|
---|
1006 | primaryQueue.post(evt);
|
---|
1007 | }
|
---|
1008 | */
|
---|
1009 |
|
---|
1010 | Queue<shared_ptr<EVT_CTRL2>> primaryQueue(bind(&procEvt, placeholders::_1));
|
---|
1011 |
|
---|
1012 | // This corresponds more or less to fFile... should we merge both?
|
---|
1013 | shared_ptr<RUN_CTRL2> actrun;
|
---|
1014 |
|
---|
1015 | void CloseRunFile()
|
---|
1016 | {
|
---|
1017 | // Currently we need actrun here, to be able to set kFileClosed.
|
---|
1018 | // Apart from that we have to ensure that there is an open file at all
|
---|
1019 | // which we can close.
|
---|
1020 | // Submission to the primary queue ensures that the event
|
---|
1021 | // is placed at the right place in the processing chain.
|
---|
1022 | // (Corresponds to the correct run)
|
---|
1023 | primaryQueue.emplace(new EVT_CTRL2(kRequestManual, actrun));
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | bool mainloop(READ_STRUCT *rd)
|
---|
1027 | {
|
---|
1028 | factPrintf(MessageImp::kInfo, "Starting EventBuilder main loop");
|
---|
1029 |
|
---|
1030 | primaryQueue.start();
|
---|
1031 | secondaryQueue.start();
|
---|
1032 | processingQueue1.start();;
|
---|
1033 |
|
---|
1034 | actrun = shared_ptr<RUN_CTRL2>(new RUN_CTRL2);
|
---|
1035 |
|
---|
1036 | //time in seconds
|
---|
1037 | time_t gi_SecTime = time(NULL)-1;
|
---|
1038 |
|
---|
1039 | //loop until global variable g_runStat claims stop
|
---|
1040 | g_reset = 0;
|
---|
1041 | while (g_reset == 0)
|
---|
1042 | {
|
---|
1043 | #ifdef USE_POLL
|
---|
1044 | int pp[40];
|
---|
1045 | int nn = 0;
|
---|
1046 | pollfd fds[40];
|
---|
1047 | for (int i=0; i<40; i++)
|
---|
1048 | {
|
---|
1049 | if (rd[i].socket>=0 && rd[i].connected && rd[i].bufLen>0)
|
---|
1050 | {
|
---|
1051 | fds[nn].fd = rd[i].socket;
|
---|
1052 | fds[nn].events = POLLIN;
|
---|
1053 | pp[nn] = i;
|
---|
1054 | nn++;
|
---|
1055 | }
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | const int rc_epoll = poll(fds, nn, 100);
|
---|
1059 | if (rc_epoll<0)
|
---|
1060 | break;
|
---|
1061 | #endif
|
---|
1062 |
|
---|
1063 | #ifdef USE_SELECT
|
---|
1064 | fd_set readfs;
|
---|
1065 | FD_ZERO(&readfs);
|
---|
1066 | int nfsd = 0;
|
---|
1067 | for (int i=0; i<NBOARDS; i++)
|
---|
1068 | if (rd[i].socket>=0 && rd[i].connected && rd[i].bufLen>0)
|
---|
1069 | {
|
---|
1070 | FD_SET(rd[i].socket, &readfs);
|
---|
1071 | if (rd[i].socket>nfsd)
|
---|
1072 | nfsd = rd[i].socket;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | timeval tv;
|
---|
1076 | tv.tv_sec = 0;
|
---|
1077 | tv.tv_usec = 100000;
|
---|
1078 | const int rc_select = select(nfsd+1, &readfs, NULL, NULL, &tv);
|
---|
1079 | // 0: timeout
|
---|
1080 | // -1: error
|
---|
1081 | if (rc_select<0)
|
---|
1082 | {
|
---|
1083 | factPrintf(MessageImp::kError, "Waiting for data failed: %d (select,rc=%d)", errno);
|
---|
1084 | continue;
|
---|
1085 | }
|
---|
1086 | #endif
|
---|
1087 |
|
---|
1088 | #ifdef USE_EPOLL
|
---|
1089 | const int rc_epoll = READ_STRUCT::wait();
|
---|
1090 | if (rc_epoll<0)
|
---|
1091 | break;
|
---|
1092 | #endif
|
---|
1093 |
|
---|
1094 | #if defined(USE_POLL)
|
---|
1095 | for (int jj=0; jj<nn; jj++)
|
---|
1096 | #endif
|
---|
1097 | #if defined(USE_EPOLL)
|
---|
1098 | for (int jj=0; jj<rc_epoll; jj++)
|
---|
1099 | #endif
|
---|
1100 | #if !defined(USE_EPOLL) && !defined(USE_POLL)
|
---|
1101 | for (int jj=0; jj<NBOARDS; jj++)
|
---|
1102 | #endif
|
---|
1103 | {
|
---|
1104 | #ifdef USE_SELECT
|
---|
1105 | if (!FD_ISSET(rs->socket, &readfs))
|
---|
1106 | continue;
|
---|
1107 | #endif
|
---|
1108 |
|
---|
1109 | #ifdef USE_POLL
|
---|
1110 | if ((fds[jj].revents&POLLIN)==0)
|
---|
1111 | continue;
|
---|
1112 | #endif
|
---|
1113 |
|
---|
1114 | #ifdef USE_EPOLL
|
---|
1115 | // FIXME: How to get i?
|
---|
1116 | READ_STRUCT *rs = READ_STRUCT::get(jj);
|
---|
1117 | #endif
|
---|
1118 |
|
---|
1119 | #ifdef USE_POLL
|
---|
1120 | // FIXME: How to get i?
|
---|
1121 | READ_STRUCT *rs = &rd[pp[jj]];
|
---|
1122 | #endif
|
---|
1123 |
|
---|
1124 | #if !defined(USE_POLL) && !defined(USE_EPOLL)
|
---|
1125 | const int i = (jj%4)*10 + (jj/4);
|
---|
1126 | READ_STRUCT *rs = &rd[i];
|
---|
1127 | #endif
|
---|
1128 |
|
---|
1129 | #ifdef COMPLETE_EVENTS
|
---|
1130 | if (rs->bufTyp==READ_STRUCT::kWait)
|
---|
1131 | continue;
|
---|
1132 | #endif
|
---|
1133 |
|
---|
1134 | // ==================================================================
|
---|
1135 |
|
---|
1136 | const bool rc_read = rs->read();
|
---|
1137 |
|
---|
1138 | // Connect might have gotten closed during read
|
---|
1139 | gi_NumConnect[rs->sockId] = rs->connected;
|
---|
1140 | gj.numConn[rs->sockId] = rs->connected;
|
---|
1141 |
|
---|
1142 | // Read either failed or disconnected, or the buffer is not yet full
|
---|
1143 | if (!rc_read)
|
---|
1144 | continue;
|
---|
1145 |
|
---|
1146 | // ==================================================================
|
---|
1147 |
|
---|
1148 | if (rs->bufTyp==READ_STRUCT::kHeader)
|
---|
1149 | {
|
---|
1150 | //check if startflag correct; else shift block ....
|
---|
1151 | // FIXME: This is not enough... this combination of
|
---|
1152 | // bytes can be anywhere... at least the end bytes
|
---|
1153 | // must be checked somewhere, too.
|
---|
1154 | uint k;
|
---|
1155 | for (k=0; k<sizeof(PEVNT_HEADER)-1; k++)
|
---|
1156 | {
|
---|
1157 | //if (rs->B[k]==0xfb && rs->B[k+1] == 0x01)
|
---|
1158 | if (*reinterpret_cast<uint16_t*>(rs->B+k) == 0x01fb)
|
---|
1159 | break;
|
---|
1160 | }
|
---|
1161 | rs->skip += k;
|
---|
1162 |
|
---|
1163 | //no start of header found
|
---|
1164 | if (k==sizeof(PEVNT_HEADER)-1)
|
---|
1165 | {
|
---|
1166 | rs->B[0] = rs->B[sizeof(PEVNT_HEADER)-1];
|
---|
1167 | rs->bufPos = rs->B+1;
|
---|
1168 | rs->bufLen = sizeof(PEVNT_HEADER)-1;
|
---|
1169 | continue;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | if (k > 0)
|
---|
1173 | {
|
---|
1174 | memmove(rs->B, rs->B+k, sizeof(PEVNT_HEADER)-k);
|
---|
1175 |
|
---|
1176 | rs->bufPos -= k;
|
---|
1177 | rs->bufLen += k;
|
---|
1178 |
|
---|
1179 | continue; // We need to read more (bufLen>0)
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | if (rs->skip>0)
|
---|
1183 | {
|
---|
1184 | factPrintf(MessageImp::kInfo, "Skipped %d bytes on port %d", rs->skip, rs->sockId);
|
---|
1185 | rs->skip = 0;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | // Swap the header entries from network to host order
|
---|
1189 | rs->swapHeader();
|
---|
1190 |
|
---|
1191 | rs->bufTyp = READ_STRUCT::kData;
|
---|
1192 | rs->bufLen = rs->len() - sizeof(PEVNT_HEADER);
|
---|
1193 |
|
---|
1194 | debugHead(rs->B); // i and fadBoard not used
|
---|
1195 |
|
---|
1196 | continue;
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | const uint16_t &end = *reinterpret_cast<uint16_t*>(rs->bufPos-2);
|
---|
1200 | if (end != 0xfe04)
|
---|
1201 | {
|
---|
1202 | factPrintf(MessageImp::kError, "End-of-event flag wrong on socket %2d for event %d (len=%d), got %04x",
|
---|
1203 | rs->sockId, rs->H.fad_evt_counter, rs->len(), end);
|
---|
1204 |
|
---|
1205 | // ready to read next header
|
---|
1206 | rs->bufTyp = READ_STRUCT::kHeader;
|
---|
1207 | rs->bufLen = sizeof(PEVNT_HEADER);
|
---|
1208 | rs->bufPos = rs->B;
|
---|
1209 | // FIXME: What to do with the validity flag?
|
---|
1210 | continue;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | // get index into mBuffer for this event (create if needed)
|
---|
1214 | const shared_ptr<EVT_CTRL2> evt = mBufEvt(*rs, actrun);
|
---|
1215 |
|
---|
1216 | // We have a valid entry, but no memory has yet been allocated
|
---|
1217 | if (evt && !evt->initMemory())
|
---|
1218 | {
|
---|
1219 | if (evt->reportMem)
|
---|
1220 | continue;
|
---|
1221 |
|
---|
1222 | factPrintf(MessageImp::kError, "No free memory left for %d (run=%d)", evt->evNum, evt->runNum);
|
---|
1223 | evt->reportMem = true;
|
---|
1224 | continue;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | // ready to read next header
|
---|
1228 | rs->bufTyp = READ_STRUCT::kHeader;
|
---|
1229 | rs->bufLen = sizeof(PEVNT_HEADER);
|
---|
1230 | rs->bufPos = rs->B;
|
---|
1231 |
|
---|
1232 | // Fatal error occured. Event cannot be processed. Skip it. Start reading next header.
|
---|
1233 | if (!evt)
|
---|
1234 | continue;
|
---|
1235 |
|
---|
1236 | // This should never happen
|
---|
1237 | if (evt->board[rs->sockId] != -1)
|
---|
1238 | {
|
---|
1239 | factPrintf(MessageImp::kError, "Got event %5d from board %3d (i=%3d, len=%5d) twice.",
|
---|
1240 | evt->evNum, rs->sockId, rs->sockId, rs->len());
|
---|
1241 | // FIXME: What to do with the validity flag?
|
---|
1242 | continue; // Continue reading next header
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | // Swap the data entries (board headers) from network to host order
|
---|
1246 | rs->swapData();
|
---|
1247 |
|
---|
1248 | // Copy data from rd[i] to mBuffer[evID]
|
---|
1249 | copyData(*rs, evt.get());
|
---|
1250 |
|
---|
1251 | #ifdef COMPLETE_EVENTS
|
---|
1252 | // Do not read anmymore from this board until the whole event has been received
|
---|
1253 | rs->bufTyp = READ_STRUCT::kWait;
|
---|
1254 | #endif
|
---|
1255 | // now we have stored a new board contents into Event structure
|
---|
1256 | evt->board[rs->sockId] = rs->sockId;
|
---|
1257 | evt->header = evt->FADhead+rs->sockId;
|
---|
1258 | evt->nBoard++;
|
---|
1259 |
|
---|
1260 | #ifdef COMPLETE_EPOLL
|
---|
1261 | if (epoll_ctl(READ_STRUCT::fd_epoll, EPOLL_CTL_DEL, rs->socket, NULL)<0)
|
---|
1262 | {
|
---|
1263 | factPrintf(MessageImp::kError, "epoll_ctrl failed: %m (EPOLL_CTL_DEL,rc=%d)", errno);
|
---|
1264 | break;
|
---|
1265 | }
|
---|
1266 | #endif
|
---|
1267 | // event not yet complete
|
---|
1268 | if (evt->nBoard < READ_STRUCT::activeSockets)
|
---|
1269 | continue;
|
---|
1270 |
|
---|
1271 | // All previous events are now flagged as incomplete ("expired")
|
---|
1272 | // and will be removed. (This is a bit tricky, because pop_front()
|
---|
1273 | // would invalidate the current iterator if not done _after_ the increment)
|
---|
1274 | for (auto it=evtCtrl.begin(); it!=evtCtrl.end(); )
|
---|
1275 | {
|
---|
1276 | const bool found = it->get()==evt.get();
|
---|
1277 | if (!found)
|
---|
1278 | reportIncomplete(*it, "expired");
|
---|
1279 | else
|
---|
1280 | primaryQueue.post(evt);
|
---|
1281 |
|
---|
1282 | it++;
|
---|
1283 | evtCtrl.pop_front();
|
---|
1284 |
|
---|
1285 | // We reached the current event, so we are done
|
---|
1286 | if (found)
|
---|
1287 | break;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | #ifdef COMPLETE_EPOLL
|
---|
1291 | for (int j=0; j<40; j++)
|
---|
1292 | {
|
---|
1293 | epoll_event ev;
|
---|
1294 | ev.events = EPOLLIN;
|
---|
1295 | ev.data.ptr = &rd[j]; // user data (union: ev.ptr)
|
---|
1296 | if (epoll_ctl(READ_STRUCT::fd_epoll, EPOLL_CTL_ADD, rd[j].socket, &ev)<0)
|
---|
1297 | {
|
---|
1298 | factPrintf(MessageImp::kError, "epoll_ctl failed: %m (EPOLL_CTL_ADD,rc=%d)", errno);
|
---|
1299 | return;
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 | #endif
|
---|
1303 |
|
---|
1304 | #ifdef COMPLETE_EVENTS
|
---|
1305 | for (int j=0; j<40; j++)
|
---|
1306 | {
|
---|
1307 | //if (rs->bufTyp==READ_STRUCT::kWait)
|
---|
1308 | {
|
---|
1309 | rs->bufTyp = READ_STRUCT::kHeader;
|
---|
1310 | rs->bufLen = sizeof(PEVNT_HEADER);
|
---|
1311 | rs->bufPos = rs->B;
|
---|
1312 | }
|
---|
1313 | }
|
---|
1314 | #endif
|
---|
1315 | } // end for loop over all sockets
|
---|
1316 |
|
---|
1317 | // ==================================================================
|
---|
1318 |
|
---|
1319 | const time_t actTime = time(NULL);
|
---|
1320 | if (actTime == gi_SecTime)
|
---|
1321 | {
|
---|
1322 | #if !defined(USE_SELECT) && !defined(USE_EPOLL) && !defined(USE_POLL)
|
---|
1323 | if (evtCtrl.empty())
|
---|
1324 | usleep(1);
|
---|
1325 | #endif
|
---|
1326 | continue;
|
---|
1327 | }
|
---|
1328 | gi_SecTime = actTime;
|
---|
1329 |
|
---|
1330 | // ==================================================================
|
---|
1331 | //loop over all active events and flag those older than read-timeout
|
---|
1332 | //delete those that are written to disk ....
|
---|
1333 |
|
---|
1334 | // This could be improved having the pointer which separates the queue with
|
---|
1335 | // the incomplete events from the queue with the complete events
|
---|
1336 | for (auto it=evtCtrl.begin(); it!=evtCtrl.end(); )
|
---|
1337 | {
|
---|
1338 | // A reference is enough because the shared_ptr is hold by the evtCtrl
|
---|
1339 | const shared_ptr<EVT_CTRL2> &evt = *it;
|
---|
1340 |
|
---|
1341 | // The first event is the oldest. If the first event within the
|
---|
1342 | // timeout window was received, we can stop searchinf further.
|
---|
1343 | if (evt->time.tv_sec>=actTime - 30)
|
---|
1344 | break;
|
---|
1345 |
|
---|
1346 | // This will result in the emission of a dim service.
|
---|
1347 | // It doesn't matter if that takes comparably long,
|
---|
1348 | // because we have to stop the run anyway.
|
---|
1349 | const uint64_t rep = reportIncomplete(evt, "timeout");
|
---|
1350 | factReportIncomplete(rep);
|
---|
1351 |
|
---|
1352 | it++;
|
---|
1353 | evtCtrl.pop_front();
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | // =================================================================
|
---|
1357 |
|
---|
1358 | gj.bufNew = evtCtrl.size(); //# incomplete events in buffer
|
---|
1359 | gj.bufEvt = primaryQueue.size(); //# complete events in buffer
|
---|
1360 | gj.bufWrite = secondaryQueue.size(); //# complete events in buffer
|
---|
1361 | gj.bufProc = processingQueue1.size(); //# complete events in buffer
|
---|
1362 | gj.bufTot = Memory::max_inuse/MAX_TOT_MEM;
|
---|
1363 | gj.usdMem = Memory::max_inuse;
|
---|
1364 | gj.totMem = Memory::allocated;
|
---|
1365 | gj.maxMem = g_maxMem;
|
---|
1366 |
|
---|
1367 | gj.deltaT = 1000; // temporary, must be improved
|
---|
1368 |
|
---|
1369 | bool changed = false;
|
---|
1370 |
|
---|
1371 | for (int ib=0; ib<NBOARDS; ib++)
|
---|
1372 | {
|
---|
1373 | gj.rateBytes[ib] = rd[ib].rateBytes;
|
---|
1374 | gj.totBytes[ib] += rd[ib].rateBytes;
|
---|
1375 |
|
---|
1376 | if (rd[ib].check(g_port[ib].sockDef, g_port[ib].sockAddr))
|
---|
1377 | changed = true;
|
---|
1378 |
|
---|
1379 | gi_NumConnect[ib] = rd[ib].connected;
|
---|
1380 | gj.numConn[ib] = rd[ib].connected;
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | factStat(gj);
|
---|
1384 |
|
---|
1385 | Memory::max_inuse = 0;
|
---|
1386 |
|
---|
1387 | for (int ib=0; ib<NBOARDS; ib++)
|
---|
1388 | rd[ib].rateBytes = 0;
|
---|
1389 |
|
---|
1390 | // =================================================================
|
---|
1391 |
|
---|
1392 | // This is a fake event to trigger possible run-closing conditions once a second
|
---|
1393 | // FIXME: This is not yet ideal because a file would never be closed
|
---|
1394 | // if a new file has been started and no events of the new file
|
---|
1395 | // have been received yet
|
---|
1396 | int request = kRequestNone;
|
---|
1397 |
|
---|
1398 | // If nothing was received for more than 5min, close file
|
---|
1399 | if (actTime-actrun->lastTime>300)
|
---|
1400 | request |= kRequestTimeout;
|
---|
1401 |
|
---|
1402 | // If connection status has changed
|
---|
1403 | if (changed)
|
---|
1404 | request |= kRequestConnectionChange;
|
---|
1405 |
|
---|
1406 | if (request!=kRequestNone)
|
---|
1407 | runFinished();
|
---|
1408 |
|
---|
1409 | if (actrun->fileStat==kFileOpen)
|
---|
1410 | primaryQueue.emplace(new EVT_CTRL2(request, actrun));
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | // 1: Stop, wait for event to get processed
|
---|
1414 | // 2: Stop, finish immediately
|
---|
1415 | // 101: Restart, wait for events to get processed
|
---|
1416 | // 101: Restart, finish immediately
|
---|
1417 | //
|
---|
1418 | const int gi_reset = g_reset;
|
---|
1419 |
|
---|
1420 | const bool abort = gi_reset%100==2;
|
---|
1421 |
|
---|
1422 | factPrintf(MessageImp::kInfo, "Stop reading ... RESET=%d (%s threads)", gi_reset, abort?"abort":"join");
|
---|
1423 |
|
---|
1424 | primaryQueue.wait(abort);
|
---|
1425 | secondaryQueue.wait(abort);
|
---|
1426 | processingQueue1.wait(abort);
|
---|
1427 |
|
---|
1428 | // Here we also destroy all runCtrl structures and hence close all open files
|
---|
1429 | evtCtrl.clear();
|
---|
1430 | actrun.reset();
|
---|
1431 |
|
---|
1432 | factPrintf(MessageImp::kInfo, "Exit read Process...");
|
---|
1433 | factPrintf(MessageImp::kInfo, "%ld Bytes flagged as in-use.", Memory::inuse);
|
---|
1434 |
|
---|
1435 | factStat(gj);
|
---|
1436 |
|
---|
1437 | return gi_reset>=100;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | // ==========================================================================
|
---|
1441 | // ==========================================================================
|
---|
1442 |
|
---|
1443 | void StartEvtBuild()
|
---|
1444 | {
|
---|
1445 | factPrintf(MessageImp::kInfo, "Starting EventBuilder++");
|
---|
1446 |
|
---|
1447 | memset(gi_NumConnect, 0, NBOARDS*sizeof(*gi_NumConnect));
|
---|
1448 |
|
---|
1449 | memset(&gj, 0, sizeof(GUI_STAT));
|
---|
1450 |
|
---|
1451 | gj.usdMem = Memory::inuse;
|
---|
1452 | gj.totMem = Memory::allocated;
|
---|
1453 | gj.maxMem = g_maxMem;
|
---|
1454 |
|
---|
1455 |
|
---|
1456 | READ_STRUCT rd[NBOARDS];
|
---|
1457 |
|
---|
1458 | // This is only that every socket knows its id (maybe we replace that by arrays instead of an array of sockets)
|
---|
1459 | for (int i=0; i<NBOARDS; i++)
|
---|
1460 | rd[i].sockId = i;
|
---|
1461 |
|
---|
1462 | while (mainloop(rd));
|
---|
1463 |
|
---|
1464 | //must close all open sockets ...
|
---|
1465 | factPrintf(MessageImp::kInfo, "Close all sockets...");
|
---|
1466 |
|
---|
1467 | READ_STRUCT::close();
|
---|
1468 |
|
---|
1469 | // Now all sockets get closed. This is not reflected in gi_NumConnect
|
---|
1470 | // The current workaround is to count all sockets as closed when the thread is not running
|
---|
1471 | }
|
---|