Line data Source code
1 : /********************************************************************\
2 : Name: mdump.c
3 : Created by: Pierre-Andre Amaudruz
4 :
5 : Contents: Dump event on screen with MIDAS or YBOS data format
6 :
7 : $Id$
8 :
9 : \********************************************************************/
10 :
11 : #include "midas.h"
12 : #include "msystem.h"
13 : #include "mrpc.h"
14 : #include "mdsupport.h"
15 : #include "midasio.h"
16 : #include "mstrlcpy.h"
17 :
18 : #include <vector>
19 :
20 : #define REP_HEADER 1
21 : #define REP_RECORD 2
22 : #define REP_LENGTH 3
23 : #define REP_EVENT 4
24 : #define REP_BANKLIST 5
25 :
26 : char bank_name[4], sbank_name[4];
27 : INT hBufEvent;
28 : INT save_dsp = 1, evt_display = 0;
29 : INT speed = 0, dsp_time = 0, dsp_fmt = 0, dsp_mode = 0, bl = -1;
30 : INT consistency = 0, disp_bank_list = 0, openzip = 0;
31 : BOOL via_callback;
32 : INT i, data_fmt;
33 : double count = 0;
34 : KEY key;
35 : HNDLE hSubkey;
36 : INT event_id, event_msk;
37 :
38 : struct FMT_ID {
39 : WORD id;
40 : WORD msk;
41 : WORD fmt;
42 : char Fmt[16];
43 : char Eqname[256];
44 :
45 0 : FMT_ID() // ctor
46 0 : {
47 0 : id = 0;
48 0 : msk = 0;
49 0 : fmt = 0;
50 0 : Fmt[0] = 0;
51 0 : Eqname[0] = 0;
52 0 : }
53 : };
54 :
55 : std::vector<FMT_ID> eq;
56 :
57 : /*-------- Check data format ------------*/
58 0 : DWORD data_format_check(EVENT_HEADER *pevent, INT *i) {
59 :
60 : INT jj, ii;
61 0 : BOOL dupflag = FALSE;
62 :
63 : /* check in the active FE list for duplicate event ID */
64 0 : ii = 0;
65 0 : while (eq[ii].fmt) {
66 0 : jj = ii + 1;
67 : /* problem occur when duplicate ID with different data format */
68 0 : while (eq[jj].fmt) {
69 0 : if ((eq[jj].fmt != eq[ii].fmt)
70 0 : && (eq[jj].id == eq[ii].id)
71 0 : && (eq[jj].msk == eq[ii].msk)
72 0 : && eq[ii].id != 0) {
73 0 : printf("Duplicate eventID[%d] between Eq:%s & %s ", eq[jj].id, eq[jj].Eqname,
74 0 : eq[ii].Eqname);
75 0 : printf("Dumping event in raw format\n");
76 0 : dupflag = TRUE;
77 : }
78 0 : jj++;
79 : }
80 0 : ii++;
81 : }
82 0 : if (data_fmt != 0) {
83 0 : *i = 0;
84 0 : strcpy(eq[*i].Fmt, "GIVEN");
85 0 : return data_fmt;
86 : } else {
87 0 : *i = 0;
88 0 : if (dupflag)
89 0 : strcpy(eq[*i].Fmt, "DUPLICATE");
90 : else {
91 : do {
92 0 : if (pevent->event_id == eq[*i].id)
93 0 : return eq[*i].fmt;
94 0 : (*i)++;
95 0 : } while (eq[*i].fmt);
96 : }
97 : }
98 0 : return 0;
99 : }
100 :
101 0 : void md_all_info_display(int seqno, int runno, TMEvent* e)
102 : {
103 0 : printf("Evt#%d- ", seqno);
104 0 : printf("%irun 0x%4.4uxid 0x%4.4uxmsk %5dmevt#", runno, e->event_id, e->trigger_mask, seqno);
105 0 : printf("%5del/x%x %5dserial\n", int(e->data.size()), int(e->data.size()), e->serial_number);
106 0 : }
107 :
108 : /*----- Replog function ----------------------------------------*/
109 0 : int replog(int data_fmt, char *rep_file, int bl, int action, int max_event_size) {
110 : static char bars[] = "|/-\\";
111 : static int i_bar;
112 :
113 0 : TMReaderInterface* r = TMNewReader(rep_file);
114 :
115 : /* open data file */
116 0 : if (r->fError) {
117 0 : fprintf(stderr, "Cannot open %s: %s\n", rep_file, r->fErrorString.c_str());
118 0 : r->Close();
119 0 : delete r;
120 0 : return (-1);
121 : }
122 :
123 0 : int seqno = 0;
124 0 : int runno = 0;
125 :
126 : //printf("skip %d\n", bl);
127 :
128 0 : while (bl > 0) {
129 0 : TMEvent* e = TMReadEvent(r);
130 0 : if (!e) {
131 0 : printf("\n");
132 0 : return -1;
133 : }
134 0 : seqno++;
135 0 : if (e->event_id == uint16_t(EVENTID_BOR))
136 0 : runno = e->serial_number;
137 0 : if (seqno < bl) {
138 0 : printf("Skipping event_# ... ");
139 0 : printf("%d \r", seqno);
140 0 : fflush(stdout);
141 : } else {
142 0 : printf("\n");
143 0 : delete e;
144 0 : break;
145 : }
146 0 : delete e;
147 : }
148 :
149 0 : switch (action) {
150 0 : case REP_HEADER:
151 : case REP_RECORD:
152 : // MIDAS format does not implement REP_HEADER and REP_RECORD
153 : // ///* get only physical record header */
154 : // //if (md_physrec_skip(data_fmt, bl) != MD_SUCCESS)
155 : // // return (-1);
156 : // do {
157 : // if (action == REP_HEADER) {
158 : // status = md_all_info_display(D_HEADER);
159 : // } else if (action == REP_RECORD) {
160 : // status = md_physrec_display(data_fmt);
161 : // } if ((status == MD_DONE) || (bl != -1)) {
162 : // break;
163 : // }
164 : // } while (md_physrec_get(data_fmt, &physrec, &physize) == MD_SUCCESS);
165 0 : break;
166 :
167 0 : case REP_LENGTH:
168 : case REP_EVENT:
169 : case REP_BANKLIST:
170 : ///* skip will read atleast on record */
171 : //if (md_physrec_skip(data_fmt, bl) != MD_SUCCESS)
172 : // return (-1);
173 0 : i = 0;
174 :
175 0 : TMEvent prev_e;
176 :
177 0 : TMEvent* e = NULL;
178 : while (1) {
179 0 : if (e)
180 0 : delete e;
181 0 : TMEvent* e = TMReadEvent(r);
182 0 : if (!e)
183 0 : break;
184 0 : if (e->error)
185 0 : continue;
186 0 : seqno++;
187 :
188 0 : if (e->event_id == uint16_t(EVENTID_BOR))
189 0 : runno = e->serial_number;
190 :
191 0 : if ((consistency == 1) && (data_fmt == FORMAT_MIDAS)) {
192 : // if ID is given skip the inconsistency event of different ID
193 0 : if ((event_id != EVENTID_ALL) && (e->event_id != event_id)) {
194 0 : continue; // Next event
195 0 : } else if (e->serial_number != prev_e.serial_number + 1) {
196 : /* event header : show last event consistent and new one (ID may be different!) */
197 0 : printf("\nLast - Evid:%4.4x- Mask:%4.4x- Serial:%i- Time:0x%x- Dsize:%i/0x%x\n",
198 0 : prev_e.event_id, prev_e.trigger_mask, prev_e.serial_number, prev_e.time_stamp,
199 : prev_e.data_size, prev_e.data_size);
200 0 : printf("Now - Evid:%4.4x- Mask:%4.4x- Serial:%i- Time:0x%x- Dsize:%i/0x%x\n",
201 0 : e->event_id, e->trigger_mask, e->serial_number,
202 : e->time_stamp, e->data_size, e->data_size);
203 : } else {
204 : // last and current SN are seprate by one
205 0 : printf("Consistency check: %c - %i (Data size:%i)\r", bars[i_bar++ % 4],
206 : prev_e.serial_number, prev_e.data_size);
207 0 : fflush(stdout);
208 : }
209 : // save current header for later comparison
210 0 : prev_e = *e;
211 0 : continue; // Next event
212 : } // consistency==1
213 0 : if (action == REP_LENGTH) {
214 0 : md_all_info_display(seqno, runno, e);
215 : }
216 0 : if ((action == REP_BANKLIST) || (disp_bank_list == 1)) {
217 0 : if (e->event_id == uint16_t(EVENTID_BOR) ||
218 0 : e->event_id == uint16_t(EVENTID_EOR) || e->event_id == uint16_t(EVENTID_MESSAGE))
219 0 : continue;
220 0 : printf("Evid:%4.4x- Mask:%4.4x- Serial:%d- Time:0x%x- Dsize:%d/0x%x\n",
221 0 : e->event_id, e->trigger_mask, e->serial_number, e->time_stamp,
222 : e->data_size, e->data_size);
223 0 : e->FindAllBanks();
224 0 : std::string banklist;
225 0 : for (unsigned b=0; b<e->banks.size(); b++) {
226 0 : banklist += msprintf("%4s", e->banks[b].name.c_str());
227 : }
228 0 : printf("#banks:%i Bank list:-%s-\n", int(e->banks.size()), banklist.c_str());
229 0 : } else if ((action == REP_EVENT) && (event_id == EVENTID_ALL) && (event_msk == TRIGGER_ALL) &&
230 0 : (sbank_name[0] == 0)) { /* a quick by-pass to prevent bank search if not necessary */
231 0 : printf("------------------------ Event# %i --------------------------------\n", i++);
232 0 : md_event_display(e->data.data(), data_fmt, dsp_mode, dsp_fmt, sbank_name);
233 0 : } else if (action == REP_EVENT) {
234 0 : if (e->event_id == uint16_t(EVENTID_BOR) ||
235 0 : e->event_id == uint16_t(EVENTID_EOR) || e->event_id == uint16_t(EVENTID_MESSAGE)) {
236 0 : printf("Searching for Bank -%s- Skiping event...%i\r", sbank_name, i++);
237 0 : fflush(stdout);
238 0 : continue;
239 : }
240 0 : uint16_t id = e->event_id;
241 0 : uint16_t msk = e->trigger_mask;
242 : /* Search bank if necessary */
243 0 : TMBank* b = NULL;
244 0 : if (sbank_name[0] != 0) { /* bank name given through argument list */
245 0 : b = e->FindBank(sbank_name);
246 : }
247 : /* check user request through switch setting (id, msk ,bank) */
248 0 : if ((event_msk != TRIGGER_ALL) || (event_id != EVENTID_ALL) ||
249 0 : (sbank_name[0] != 0)) { /* check request or skip event if not satisfied */
250 0 : if (((event_id != EVENTID_ALL) && (id != event_id)) || /* id check ==> skip */
251 0 : ((event_msk != TRIGGER_ALL) && (msk != event_msk)) || /* msk check ==> skip */
252 0 : ((sbank_name[0] != 0) && !b)) { /* bk check ==> skip *//* skip event */
253 0 : printf("Searching for Bank -%s- Skiping event...%i\r", sbank_name, i++);
254 0 : fflush(stdout);
255 : } else { /* request match ==> display any event */
256 0 : printf("------------------------ Event# %i --------------------------------\n", i++);
257 0 : md_event_display(e->data.data(), data_fmt, dsp_mode, dsp_fmt, sbank_name);
258 : }
259 : } else { /* no user request ==> display any event */
260 0 : printf("------------------------ Event# %i --------------------------------\n", i++);
261 0 : md_event_display(e->data.data(), data_fmt, dsp_mode, dsp_fmt, sbank_name);
262 : }
263 : }
264 0 : }
265 0 : if (e)
266 0 : delete e;
267 0 : break;
268 : } /* switch */
269 :
270 : /* close data file */
271 0 : printf("\n");
272 :
273 0 : r->Close();
274 0 : delete r;
275 :
276 0 : return 0;
277 : }
278 :
279 : /*----- receive_event ----------------------------------------------*/
280 0 : void process_event(HNDLE hBuf, HNDLE request_id, EVENT_HEADER *pheader, void *pevent) {
281 : static char bars[] = "|/-\\";
282 : static int i_bar;
283 : static EVENT_HEADER pevh;
284 : INT internal_data_fmt, status, index, size;
285 : char banklist[STRING_BANKLIST_MAX];
286 : BANK_HEADER *pmbh;
287 : DWORD bklen, bktyp;
288 :
289 0 : if (speed == 1) {
290 : /* accumulate received event size */
291 0 : size = pheader->data_size + sizeof(EVENT_HEADER);
292 0 : count += size;
293 0 : return;
294 : }
295 0 : if (consistency == 1) {
296 0 : if (pheader->serial_number != pevh.serial_number + 1) {
297 : /* event header */
298 0 : printf("\nLast - Evid:%4.4x- Mask:%4.4x- Serial:%i- Time:0x%x- Dsize:%i/0x%x\n",
299 0 : pevh.event_id, pevh.trigger_mask, pevh.serial_number, pevh.time_stamp,
300 : pevh.data_size, pevh.data_size);
301 0 : printf("Now - Evid:%4.4x- Mask:%4.4x- Serial:%i- Time:0x%x- Dsize:%i/0x%x\n",
302 0 : pheader->event_id, pheader->trigger_mask, pheader->serial_number,
303 : pheader->time_stamp, pheader->data_size, pheader->data_size);
304 : } else {
305 0 : printf("Consistency check: %c - %i (Data size:%i)\r", bars[i_bar++ % 4],
306 : pheader->serial_number, pheader->data_size);
307 0 : fflush(stdout);
308 : }
309 0 : memcpy((char *) &pevh, (char *) pheader, sizeof(EVENT_HEADER));
310 0 : return;
311 : }
312 0 : if (evt_display > 0) {
313 0 : evt_display--;
314 :
315 0 : internal_data_fmt = data_format_check(pheader, &index);
316 :
317 : /* pointer to data section */
318 0 : pmbh = (BANK_HEADER *) pevent;
319 :
320 : /* display header comes ALWAYS from MIDAS header */
321 0 : printf("------------------------ Event# %i ------------------------\n",
322 : save_dsp - evt_display);
323 : /* selection based on data format */
324 0 : if ((internal_data_fmt == FORMAT_MIDAS) &&
325 0 : (md_event_swap(FORMAT_MIDAS, pheader) >= MD_SUCCESS)) { /* ---- MIDAS FMT ---- */
326 0 : if (sbank_name[0] != 0) {
327 : BANK *pmbk;
328 0 : if (bk_find(pmbh, sbank_name, &bklen, &bktyp, (void **) &pmbk) ==
329 : SS_SUCCESS) { /* bank name given through argument list */
330 0 : status = bk_list(pmbh, banklist);
331 0 : printf("#banks:%i Bank list:-%s-", status, banklist);
332 0 : if (bk_is32a(pmbh))
333 0 : pmbk = (BANK *) (((char *) pmbk) - sizeof(BANK32A));
334 0 : else if (bk_is32(pmbh))
335 0 : pmbk = (BANK *) (((char *) pmbk) - sizeof(BANK32));
336 : else
337 0 : pmbk = (BANK *) (((char *) pmbk) - sizeof(BANK));
338 0 : md_bank_display(pmbh, pmbk, FORMAT_MIDAS, dsp_mode, dsp_fmt);
339 : } else {
340 0 : status = bk_list(pmbh, banklist);
341 0 : printf("Bank -%s- not found (%i) in ", sbank_name, status);
342 0 : printf("#banks:%i Bank list:-%s-\n", status, banklist);
343 : }
344 : } else { /* Full event or bank list only */
345 0 : if (disp_bank_list) {
346 : /* event header */
347 0 : printf("Evid:%4.4x- Mask:%4.4x- Serial:%d- Time:0x%x- Dsize:%d/0x%x\n",
348 0 : pheader->event_id, pheader->trigger_mask, pheader->serial_number,
349 : pheader->time_stamp, pheader->data_size, pheader->data_size);
350 0 : status = bk_list(pmbh, banklist);
351 0 : printf("#banks:%i Bank list:-%s-\n", status, banklist);
352 : } else
353 0 : md_event_display(pheader, FORMAT_MIDAS, dsp_mode, dsp_fmt, sbank_name);
354 : }
355 : } else { /* unknown format just dump midas event */
356 0 : printf("Data format not supported: \"%s\"\n", eq[index].Fmt);
357 0 : md_event_display(pheader, FORMAT_MIDAS, DSP_RAW, dsp_fmt, sbank_name);
358 : }
359 0 : if (evt_display == 0) {
360 : /* do not produce shutdown message */
361 0 : cm_set_msg_print(MT_ERROR, 0, NULL);
362 0 : cm_disconnect_experiment();
363 0 : exit(0);
364 : }
365 0 : if (dsp_time != 0)
366 0 : ss_sleep(dsp_time);
367 : }
368 0 : return;
369 : }
370 :
371 : /*------------------------------------------------------------------*/
372 0 : int main(int argc, char **argv) {
373 : HNDLE hDB, hKey;
374 : char host_name[HOST_NAME_LENGTH], expt_name[NAME_LENGTH], str[80];
375 0 : char buf_name[32] = EVENT_BUFFER_NAME, rep_file[128];
376 : unsigned int status, start_time, stop_time;
377 0 : BOOL debug = FALSE, rep_flag;
378 : INT ch, request_id, size, get_flag, action, single, i, max_event_size;
379 : BUFFER_HEADER buffer_header;
380 :
381 : /* set default */
382 0 : host_name[0] = 0;
383 0 : expt_name[0] = 0;
384 0 : sbank_name[0] = 0;
385 0 : rep_file[0] = 0;
386 0 : event_id = EVENTID_ALL;
387 0 : event_msk = TRIGGER_ALL;
388 0 : evt_display = 1;
389 0 : get_flag = GET_NONBLOCKING;
390 0 : dsp_fmt = DSP_UNK;
391 0 : dsp_mode = DSP_BANK;
392 0 : via_callback = TRUE;
393 0 : rep_flag = FALSE;
394 0 : dsp_time = 0;
395 0 : speed = 0;
396 0 : single = 0;
397 0 : consistency = 0;
398 0 : action = REP_EVENT;
399 0 : max_event_size = DEFAULT_MAX_EVENT_SIZE;
400 :
401 : /* Get if existing the pre-defined experiment */
402 0 : cm_get_environment(host_name, sizeof(host_name), expt_name, sizeof(expt_name));
403 :
404 : /* scan arg list for -x which specify the replog configuration */
405 0 : for (i = 1; i < argc; i++) {
406 0 : if (strncmp(argv[i], "-x", 2) == 0) {
407 0 : if (i + 1 == argc)
408 0 : goto repusage;
409 0 : if (strncmp(argv[++i], "online", 6) != 0) {
410 0 : rep_flag = TRUE;
411 0 : break;
412 : }
413 : }
414 : }
415 0 : if (rep_flag) {
416 : /* get Replay argument list */
417 0 : data_fmt = 0;
418 0 : for (i = 1; i < argc; i++) {
419 0 : if (argv[i][0] == '-' && argv[i][1] == 'd')
420 0 : debug = TRUE;
421 0 : else if (strncmp(argv[i], "-single", 7) == 0)
422 0 : single = 1;
423 0 : else if (strncmp(argv[i], "-j", 2) == 0)
424 0 : disp_bank_list = 1;
425 0 : else if (strncmp(argv[i], "-y", 2) == 0)
426 0 : consistency = 1;
427 0 : else if (argv[i][0] == '-') {
428 0 : if (i + 1 >= argc || argv[i + 1][0] == '-')
429 0 : goto repusage;
430 0 : if (strncmp(argv[i], "-t", 2) == 0) {
431 0 : mstrlcpy(str, argv[++i], sizeof(str));
432 0 : if (strncmp(str, "m", 1) == 0)
433 0 : data_fmt = FORMAT_MIDAS;
434 0 : } else if (strncmp(argv[i], "-b", 2) == 0)
435 0 : memcpy(sbank_name, argv[++i], 4);
436 0 : else if (strncmp(argv[i], "-i", 2) == 0)
437 0 : event_id = atoi(argv[++i]);
438 0 : else if (strncmp(argv[i], "-k", 2) == 0)
439 0 : event_msk = atoi(argv[++i]);
440 0 : else if (strncmp(argv[i], "-a", 2) == 0)
441 0 : max_event_size = atoi(argv[++i]);
442 0 : else if (strncmp(argv[i], "-m", 2) == 0) {
443 0 : mstrlcpy(str, argv[++i], sizeof(str));
444 0 : if (strncmp(str, "r", 1) == 0)
445 0 : dsp_mode = DSP_RAW;
446 0 : if (strncmp(str, "b", 1) == 0)
447 0 : dsp_mode = DSP_BANK;
448 0 : } else if (strncmp(argv[i], "-w", 2) == 0) {
449 0 : mstrlcpy(str, argv[++i], sizeof(str));
450 0 : if (strncmp(str, "h", 1) == 0)
451 0 : action = REP_HEADER;
452 0 : else if (strncmp(str, "r", 1) == 0)
453 0 : action = REP_RECORD;
454 0 : else if (strncmp(str, "l", 1) == 0)
455 0 : action = REP_LENGTH;
456 0 : else if (strncmp(str, "e", 1) == 0)
457 0 : action = REP_EVENT;
458 0 : else if (strncmp(str, "j", 1) == 0)
459 0 : action = REP_BANKLIST;
460 0 : } else if (strncmp(argv[i], "-f", 2) == 0) {
461 0 : mstrlcpy(str, argv[++i], sizeof(str));
462 0 : if (strncmp(str, "d", 1) == 0)
463 0 : dsp_fmt = DSP_DEC;
464 0 : if (strncmp(str, "x", 1) == 0)
465 0 : dsp_fmt = DSP_HEX;
466 0 : if (strncmp(str, "a", 1) == 0)
467 0 : dsp_fmt = DSP_ASC;
468 0 : } else if (strncmp(argv[i], "-r", 2) == 0)
469 0 : bl = atoi(argv[++i]);
470 0 : else if (strncmp(argv[i], "-x", 2) == 0) {
471 0 : if (i + 1 == argc)
472 0 : goto repusage;
473 0 : strcpy(rep_file, argv[++i]);
474 : } else {
475 0 : repusage:
476 0 : printf("mdump for replay -x file name : file to inspect\n");
477 0 : printf(" -m mode : Display mode either Bank or raw\n");
478 0 : printf(" -b bank name : search for bank name (case sensitive)\n");
479 0 : printf(" -i evt_id (any) : event id from the FE\n");
480 0 : printf(" -[single] : Request single bank only (to be used with -b)\n");
481 0 : printf(" -y : Serial number consistency check(-i supported)\n");
482 0 : printf(" -j : Display # of banks and bank name list only for all the event\n");
483 0 : printf(" -k mask (any) : trigger_mask from FE setting\n");
484 0 : printf(">>> -i and -k are valid for YBOS ONLY if EVID bank is present in the event\n");
485 0 : printf(" -w what : [h]eader, [r]ecord, [l]ength\n");
486 0 : printf(" [e]vent, [j]bank_list (same as -j)\n");
487 0 : printf(">>> Header & Record are not supported for MIDAS as no physical record structure exists\n");
488 0 : printf(" -f format (auto): data representation ([x]/[d]/[a]scii) def:bank header content\n");
489 0 : printf(" -r # : skip event(MIDAS) to #\n");
490 0 : printf(" -a bytes : max event size to support (defaults to %d bytes)\n",
491 : DEFAULT_MAX_EVENT_SIZE);
492 0 : return 0;
493 : }
494 : }
495 : }
496 : } else { // Online
497 : /* get parameters for online */
498 0 : for (i = 1; i < argc; i++) {
499 0 : if (argv[i][0] == '-' && argv[i][1] == 'd')
500 0 : debug = TRUE;
501 0 : else if (strncmp(argv[i], "-s", 2) == 0)
502 0 : speed = 1;
503 0 : else if (strncmp(argv[i], "-y", 2) == 0)
504 0 : consistency = 1;
505 0 : else if (strncmp(argv[i], "-j", 2) == 0)
506 0 : disp_bank_list = 1;
507 0 : else if (argv[i][0] == '-') {
508 0 : if (i + 1 >= argc || argv[i + 1][0] == '-')
509 0 : goto usage;
510 0 : else if (strncmp(argv[i], "-x", 2) == 0)
511 0 : strncpy(rep_file, argv[++i], 4);
512 0 : else if (strncmp(argv[i], "-b", 2) == 0)
513 0 : memcpy(sbank_name, argv[++i], 4);
514 0 : else if (strncmp(argv[i], "-l", 2) == 0)
515 0 : save_dsp = evt_display = atoi(argv[++i]);
516 0 : else if (strncmp(argv[i], "-w", 2) == 0)
517 0 : dsp_time = 1000 * (atoi(argv[++i]));
518 0 : else if (strncmp(argv[i], "-m", 2) == 0) {
519 0 : mstrlcpy(str, argv[++i], sizeof(str));
520 0 : if (strncmp(str, "r", 1) == 0)
521 0 : dsp_mode = DSP_RAW;
522 0 : if (strncmp(str, "y", 1) == 0)
523 0 : dsp_mode = DSP_BANK;
524 0 : } else if (strncmp(argv[i], "-g", 2) == 0) {
525 0 : mstrlcpy(str, argv[++i], sizeof(str));
526 0 : if (strncmp(str, "s", 1) == 0)
527 0 : get_flag = GET_NONBLOCKING;
528 0 : if (strncmp(str, "a", 1) == 0)
529 0 : get_flag = GET_ALL;
530 0 : } else if (strncmp(argv[i], "-f", 2) == 0) {
531 0 : mstrlcpy(str, argv[++i], sizeof(str));
532 0 : if (strncmp(str, "d", 1) == 0)
533 0 : dsp_fmt = DSP_DEC;
534 0 : if (strncmp(str, "x", 1) == 0)
535 0 : dsp_fmt = DSP_HEX;
536 0 : if (strncmp(str, "a", 1) == 0)
537 0 : dsp_fmt = DSP_ASC;
538 0 : } else if (strncmp(argv[i], "-i", 2) == 0)
539 0 : event_id = atoi(argv[++i]);
540 0 : else if (strncmp(argv[i], "-k", 2) == 0)
541 0 : event_msk = atoi(argv[++i]);
542 0 : else if (strncmp(argv[i], "-z", 2) == 0)
543 0 : strcpy(buf_name, argv[++i]);
544 0 : else if (strncmp(argv[i], "-t", 2) == 0) {
545 0 : mstrlcpy(str, argv[++i], sizeof(str));
546 0 : if (strncmp(str, "m", 1) == 0)
547 0 : data_fmt = FORMAT_MIDAS;
548 0 : } else if (strncmp(argv[i], "-h", 2) == 0)
549 0 : strcpy(host_name, argv[++i]);
550 0 : else if (strncmp(argv[i], "-e", 2) == 0)
551 0 : strcpy(expt_name, argv[++i]);
552 : else {
553 0 : usage:
554 0 : printf("mdump for online -l # : display # events (look 1)\n");
555 : printf
556 0 : (" -f format (auto): data representation ([x]/[d]/[a]scii) def:bank header content\n");
557 : printf
558 0 : (" -w time : insert wait in [sec] between each display\n");
559 : printf
560 0 : (" -m mode : Display mode either Bank or raw\n");
561 : printf
562 0 : (" -j : Display # of banks and bank name list only for all the event\n");
563 : printf
564 0 : (" -b bank name : search for bank name (case sensitive)\n");
565 : printf
566 0 : (" -i evt_id (any) : event id from the FE\n");
567 : printf
568 0 : (" -k mask (any) : trigger_mask from FE setting\n");
569 : printf
570 0 : (" -g type : sampling mode either SOME or all)\n");
571 : printf
572 0 : (" -s : report buffer data rate and fill level\n");
573 : printf
574 0 : (" -s -d : for use with -s: also report all buffer clients and requests\n");
575 : printf
576 0 : (" -t type (auto) : Bank format (Midas/Ybos)\n");
577 : printf
578 0 : (" -x Source : Data source selection def:online (see -x -h)\n");
579 : printf
580 0 : (" -y : Serial number consistency check\n");
581 : printf
582 0 : (">>> in case of -y it is recommented to used -g all\n");
583 : printf
584 0 : (" -z buffer name : Midas buffer name default:[SYSTEM]\n");
585 : printf
586 0 : (" [-h Hostname] [-e Experiment]\n\n");
587 0 : return 0;
588 : }
589 : }
590 : }
591 : }
592 :
593 : // Set flag to inform that we're coming from mdump -> open zip (.mid.gz)
594 0 : if ((strstr(argv[0], "mdump") == NULL)) openzip = 0; else openzip = 1;
595 :
596 0 : if ((sbank_name[0] != 0) && single) dsp_mode += 1;
597 :
598 0 : if (rep_flag && data_fmt == 0) {
599 : char *pext;
600 0 : if ((pext = strrchr(rep_file, '.')) != 0) {
601 0 : if (equal_ustring(pext + 1, "mid")) {
602 0 : data_fmt = FORMAT_MIDAS;
603 0 : } else if (equal_ustring(pext + 1, "gz")) {
604 0 : if ((pext = strchr(rep_file, '.')) != 0) {
605 0 : if (strstr(pext + 1, "mid"))
606 0 : data_fmt = FORMAT_MIDAS;
607 : } else {
608 0 : printf("\n>>> data type (-t) should be set by hand in -x mode for tape <<< \n\n");
609 0 : goto usage;
610 : }
611 0 : } else if (equal_ustring(pext + 1, "lz4")) {
612 0 : data_fmt = FORMAT_MIDAS;
613 0 : } else if (equal_ustring(pext + 1, "bz2")) {
614 0 : data_fmt = FORMAT_MIDAS;
615 : } else {
616 : printf
617 0 : ("\n>>> data type (-t) should be set by hand in -x mode for tape <<< \n\n");
618 0 : goto usage;
619 : }
620 : }
621 : }
622 :
623 : /* steer to replog function */
624 0 : if (rep_flag) {
625 0 : replog(data_fmt, rep_file, bl, action, max_event_size);
626 0 : return 0;
627 : } else {
628 : /* check parameters */
629 0 : if (evt_display < 1 || evt_display > 9999) {
630 0 : printf("mdump-F- <-display arg> out of range (1:9999)\n");
631 0 : return -1;
632 : }
633 : }
634 0 : if (dsp_time < 0 || dsp_time > 100) {
635 0 : printf("mdump-F- <-delay arg> out of range (1:100)\n");
636 0 : return -1;
637 : }
638 :
639 : /* do not produce startup message */
640 0 : cm_set_msg_print(MT_ERROR, 0, NULL);
641 :
642 : /* connect to experiment */
643 0 : status = cm_connect_experiment(host_name, expt_name, "mdump", 0);
644 0 : if (status != CM_SUCCESS)
645 0 : return 1;
646 :
647 : #ifdef _DEBUG
648 : cm_set_watchdog_params(TRUE, 0);
649 : #endif
650 :
651 : /* open the shared memory buffer with proper size */
652 0 : status = bm_open_buffer(buf_name, DEFAULT_BUFFER_SIZE, &hBufEvent);
653 0 : if (status != BM_SUCCESS && status != BM_CREATED) {
654 0 : cm_msg(MERROR, "mdump", "Cannot open buffer \"%s\", bm_open_buffer() status %d", buf_name, status);
655 0 : goto error;
656 : }
657 : /* set the buffer cache size if requested */
658 0 : bm_set_cache_size(hBufEvent, 100000, 0);
659 :
660 : /* place a request for a specific event id */
661 0 : bm_request_event(hBufEvent, (WORD) event_id, (WORD) event_msk, get_flag, &request_id, process_event);
662 :
663 0 : start_time = 0;
664 0 : if (speed == 1)
665 0 : printf("- MIDAS revision: %s -- Enter <!> to Exit ------- Midas Dump in Speed test mode ---\n",
666 : cm_get_revision());
667 : else
668 0 : printf("- MIDAS revision: %s -- Enter <!> to Exit ------- Midas Dump ---\n", cm_get_revision());
669 :
670 : /* connect to the database */
671 0 : cm_get_experiment_database(&hDB, &hKey);
672 :
673 : { /* ID block */
674 0 : INT l = 0;
675 : /* check if dir exists */
676 0 : if (db_find_key(hDB, 0, "/equipment", &hKey) == DB_SUCCESS) {
677 : char strtmp[256], equclient[256];
678 0 : for (i = 0;; i++) {
679 0 : db_enum_key(hDB, hKey, i, &hSubkey);
680 0 : if (!hSubkey)
681 0 : break;
682 0 : db_get_key(hDB, hSubkey, &key);
683 0 : FMT_ID f;
684 0 : eq.push_back(f);
685 0 : mstrlcpy(eq[l].Eqname, key.name, sizeof(eq[l].Eqname));
686 : /* check if client running this equipment is present */
687 : /* extract client name from equipment */
688 0 : size = sizeof(equclient);
689 0 : sprintf(strtmp, "/equipment/%s/common/Frontend name", key.name);
690 0 : db_get_value(hDB, 0, strtmp, equclient, &size, TID_STRING, TRUE);
691 :
692 : /* search client name under /system/clients/xxx/name */
693 : /* Outcommented 22 Dec 1997 SR because of problem when
694 : mdump is started before frontend
695 : if (status = cm_exist(equclient,FALSE) != CM_SUCCESS)
696 : continue;
697 : */
698 0 : size = sizeof(WORD);
699 0 : sprintf(strtmp, "/equipment/%s/common/event ID", key.name);
700 0 : db_get_value(hDB, 0, strtmp, &(eq[l]).id, &size, TID_WORD, TRUE);
701 :
702 0 : size = sizeof(WORD);
703 0 : sprintf(strtmp, "/equipment/%s/common/Trigger mask", key.name);
704 0 : db_get_value(hDB, 0, strtmp, &(eq[l]).msk, &size, TID_WORD, TRUE);
705 :
706 0 : size = sizeof(str);
707 0 : sprintf(strtmp, "/equipment/%s/common/Format", key.name);
708 0 : db_get_value(hDB, 0, strtmp, str, &size, TID_STRING, TRUE);
709 0 : if (equal_ustring(str, "MIDAS")) {
710 0 : eq[l].fmt = FORMAT_MIDAS;
711 0 : strcpy(eq[l].Fmt, "MIDAS");
712 0 : } else if (equal_ustring(str, "FIXED")) {
713 0 : eq[l].fmt = FORMAT_FIXED;
714 0 : strcpy(eq[l].Fmt, "FIXED");
715 : } else {
716 0 : cm_msg(MERROR, "mdump", "Unknown format \"%s\" in equipment \"%s\"", str, key.name);
717 0 : eq[l].fmt = 0;
718 0 : strcpy(eq[l].Fmt, "UNKNOWN");
719 : }
720 0 : l++;
721 0 : }
722 : }
723 :
724 : /* for equipment */
725 : /* check for EBuilder */
726 0 : if (db_find_key(hDB, 0, "/EBuilder/Settings", &hKey) == DB_SUCCESS) {
727 0 : sprintf(eq[l].Eqname, "EBuilder");
728 : /* check if client running this equipment is present */
729 : /* search client name under /system/clients/xxx/name */
730 : /* Outcommented 22 Dec 1997 SR because of problem when
731 : mdump is started before frontend
732 : if (status = cm_exist(equclient,FALSE) != CM_SUCCESS)
733 : continue;
734 : */
735 0 : size = sizeof(WORD);
736 0 : db_get_value(hDB, hKey, "Event ID", &(eq[l]).id, &size, TID_WORD, TRUE);
737 :
738 0 : size = sizeof(WORD);
739 0 : db_get_value(hDB, hKey, "Trigger mask", &(eq[l]).msk, &size, TID_WORD, TRUE);
740 :
741 0 : size = sizeof(str);
742 0 : db_get_value(hDB, hKey, "Format", str, &size, TID_STRING, TRUE);
743 0 : if (equal_ustring(str, "MIDAS")) {
744 0 : eq[l].fmt = FORMAT_MIDAS;
745 0 : strcpy(eq[l].Fmt, "MIDAS");
746 : } else {
747 0 : printf("Format unknown for Event Builder \"%s\"\n", str);
748 0 : goto error;
749 : }
750 0 : l++;
751 : }
752 :
753 : /* Debug */
754 0 : if (debug) {
755 0 : i = 0;
756 0 : printf("ID\tMask\tFormat\tEq_name\n");
757 0 : while (eq.size() > 0 && eq[i].fmt) {
758 0 : printf("%d\t%d\t%s\t%s\n", eq[i].id, eq[i].msk, eq[i].Fmt, eq[i].Eqname);
759 0 : i++;
760 : }
761 : }
762 : } /* ID block */
763 :
764 : do {
765 0 : if (via_callback)
766 0 : status = cm_yield(1000);
767 0 : if (speed == 1) {
768 : /* calculate rates each second */
769 0 : if (ss_millitime() - start_time > 1000) {
770 0 : stop_time = ss_millitime();
771 0 : double rate = count / 1024.0 / 1024.0 / ((stop_time - start_time) / 1000.0);
772 :
773 : /* get information about filling level of the buffer */
774 0 : bm_get_buffer_info(hBufEvent, &buffer_header);
775 0 : int filled = buffer_header.read_pointer - buffer_header.write_pointer;
776 0 : if (filled <= 0)
777 0 : filled += buffer_header.size;
778 :
779 0 : int max_used = 0;
780 0 : int max_used_client = 0;
781 0 : int now = ss_millitime();
782 :
783 0 : if (debug) {
784 :
785 0 : printf("buffer name [%s], clients: %d, max: %d, size: %d, rp: %d, wp: %d, ine: %d, oute: %d\n",
786 : buffer_header.name,
787 : buffer_header.num_clients,
788 : buffer_header.max_client_index,
789 : buffer_header.size,
790 : buffer_header.read_pointer,
791 : buffer_header.write_pointer,
792 : buffer_header.num_in_events,
793 : buffer_header.num_out_events
794 : );
795 :
796 0 : for (int i = 0; i < buffer_header.max_client_index; i++) {
797 0 : if (buffer_header.client[i].pid) {
798 0 : int used = buffer_header.write_pointer - buffer_header.client[i].read_pointer;
799 0 : if (used < 0)
800 0 : used += buffer_header.size;
801 :
802 0 : if (buffer_header.client[i].all_flag) {
803 0 : if (used > max_used) {
804 0 : max_used = used;
805 0 : max_used_client = i;
806 : }
807 : }
808 :
809 0 : printf(" client %d: name [%s], pid: %d, port: %d, rp: %d, used: %d, max_req: %d, read_wait: %d, write_wait: %d, wake_up: %d, get_all: %d, active: %d, timeout: %d\n",
810 : i,
811 0 : buffer_header.client[i].name,
812 : buffer_header.client[i].pid,
813 : buffer_header.client[i].port,
814 : buffer_header.client[i].read_pointer,
815 : used,
816 : buffer_header.client[i].max_request_index,
817 : buffer_header.client[i].read_wait,
818 : buffer_header.client[i].write_wait,
819 : buffer_header.client[i].wake_up,
820 : buffer_header.client[i].all_flag,
821 0 : now - buffer_header.client[i].last_activity,
822 : buffer_header.client[i].watchdog_timeout);
823 :
824 0 : for (int j = 0; j < buffer_header.client[i].max_request_index; j++)
825 0 : if (buffer_header.client[i].event_request[j].valid)
826 0 : printf(" request %d: id: %d, valid: %d, event_id: %d, trigger_mask: 0x%x, type: %d\n",
827 : j,
828 : buffer_header.client[i].event_request[j].id,
829 : buffer_header.client[i].event_request[j].valid,
830 0 : buffer_header.client[i].event_request[j].event_id,
831 0 : buffer_header.client[i].event_request[j].trigger_mask,
832 : buffer_header.client[i].event_request[j].sampling_type);
833 : }
834 : }
835 : }
836 0 : printf("buffer name [%s], ", buffer_header.name);
837 0 : printf("filled: %4.1f%%, ", 100 - 100.0 * filled / buffer_header.size);
838 0 : printf("used: %4.1f%% by [%s], ", 100.0 * max_used / buffer_header.size,
839 0 : buffer_header.client[max_used_client].name);
840 0 : printf("rate: %1.3f MiB/sec\n", rate);
841 :
842 0 : start_time = stop_time;
843 0 : count = 0;
844 : }
845 : } // Speed
846 :
847 : /* speed */
848 : /* check keyboard */
849 0 : ch = 0;
850 0 : if (ss_kbhit()) {
851 0 : ch = ss_getchar(0);
852 0 : if (ch == -1)
853 0 : ch = getchar();
854 0 : if ((char) ch == '!')
855 0 : break;
856 : }
857 0 : } while (status != RPC_SHUTDOWN && status != SS_ABORT);
858 :
859 0 : error:
860 : /* do not produce shutdown message */
861 0 : cm_set_msg_print(MT_ERROR, 0, NULL);
862 :
863 0 : cm_disconnect_experiment();
864 :
865 0 : return 1;
866 : }
867 :
|