1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
|
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
#include <fstream>
#include <zlib.h>
#include <cstdio>
#include <chrono>
#include <boost/algorithm/string.hpp>
#include "DataFormats/FEDRawData/interface/FEDHeader.h"
#include "DataFormats/FEDRawData/interface/FEDTrailer.h"
#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/TCDS/interface/TCDSRaw.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/InputSourceDescription.h"
#include "FWCore/Framework/interface/InputSourceMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/UnixSignalHandlers.h"
#include "EventFilter/Utilities/interface/GlobalEventNumber.h"
#include "EventFilter/Utilities/interface/SourceRawFile.h"
#include "EventFilter/Utilities/interface/FedRawDataInputSource.h"
#include "EventFilter/Utilities/interface/SourceCommon.h"
#include "EventFilter/Utilities/interface/DataPointDefinition.h"
#include "EventFilter/Utilities/interface/FFFNamingSchema.h"
#include "EventFilter/Utilities/interface/AuxiliaryMakers.h"
#include "DataFormats/Provenance/interface/EventAuxiliary.h"
#include "DataFormats/Provenance/interface/EventID.h"
#include "DataFormats/Provenance/interface/Timestamp.h"
#include "EventFilter/Utilities/interface/crc32c.h"
//JSON file reader
#include "EventFilter/Utilities/interface/reader.h"
using namespace evf::FastMonState;
using namespace edm::streamer;
FedRawDataInputSource::FedRawDataInputSource(edm::ParameterSet const& pset, edm::InputSourceDescription const& desc)
: edm::RawInputSource(pset, desc),
defPath_(pset.getUntrackedParameter<std::string>("buDefPath", "")),
eventChunkSize_(pset.getUntrackedParameter<unsigned int>("eventChunkSize", 32) * 1048576),
eventChunkBlock_(pset.getUntrackedParameter<unsigned int>("eventChunkBlock", 32) * 1048576),
numConcurrentReads_(pset.getUntrackedParameter<int>("numConcurrentReads", -1)),
numBuffers_(pset.getUntrackedParameter<unsigned int>("numBuffers", 2)),
maxBufferedFiles_(pset.getUntrackedParameter<unsigned int>("maxBufferedFiles", 2)),
getLSFromFilename_(pset.getUntrackedParameter<bool>("getLSFromFilename", true)),
alwaysStartFromFirstLS_(pset.getUntrackedParameter<bool>("alwaysStartFromFirstLS", false)),
verifyChecksum_(pset.getUntrackedParameter<bool>("verifyChecksum", true)),
useL1EventID_(pset.getUntrackedParameter<bool>("useL1EventID", false)),
testTCDSFEDRange_(
pset.getUntrackedParameter<std::vector<unsigned int>>("testTCDSFEDRange", std::vector<unsigned int>())),
fileNames_(pset.getUntrackedParameter<std::vector<std::string>>("fileNames", std::vector<std::string>())),
fileListMode_(pset.getUntrackedParameter<bool>("fileListMode", false)),
fileDiscoveryMode_(pset.getUntrackedParameter<bool>("fileDiscoveryMode", false)),
fileListLoopMode_(pset.getUntrackedParameter<bool>("fileListLoopMode", false)),
runNumber_(edm::Service<evf::EvFDaqDirector>()->getRunNumber()),
daqProvenanceHelper_(edm::TypeID(typeid(FEDRawDataCollection))),
eventID_(),
processHistoryID_(),
currentLumiSection_(0),
tcds_pointer_(nullptr),
eventsThisLumi_(0) {
char thishost[256];
gethostname(thishost, 255);
edm::LogInfo("FedRawDataInputSource") << "Construction. read-ahead chunk size -: " << std::endl
<< (eventChunkSize_ / 1048576) << " MB on host " << thishost;
if (!testTCDSFEDRange_.empty()) {
if (testTCDSFEDRange_.size() != 2) {
throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection")
<< "Invalid TCDS Test FED range parameter";
}
MINTCDSuTCAFEDID_ = testTCDSFEDRange_[0];
MAXTCDSuTCAFEDID_ = testTCDSFEDRange_[1];
}
long autoRunNumber = -1;
if (fileListMode_) {
autoRunNumber = initFileList();
edm::Service<evf::EvFDaqDirector>()->setFileListMode();
if (!fileListLoopMode_) {
if (autoRunNumber < 0)
throw cms::Exception("FedRawDataInputSource::FedRawDataInputSource") << "Run number not found from filename";
//override run number
runNumber_ = (edm::RunNumber_t)autoRunNumber;
edm::Service<evf::EvFDaqDirector>()->overrideRunNumber((unsigned int)autoRunNumber);
}
}
processHistoryID_ = daqProvenanceHelper_.daqInit(productRegistryUpdate(), processHistoryRegistryForUpdate());
setNewRun();
//todo:autodetect from file name (assert if names differ)
setRunAuxiliary(new edm::RunAuxiliary(runNumber_, edm::Timestamp::beginOfTime(), edm::Timestamp::invalidTimestamp()));
//make sure that chunk size is N * block size
assert(eventChunkSize_ >= eventChunkBlock_);
readBlocks_ = eventChunkSize_ / eventChunkBlock_;
if (readBlocks_ * eventChunkBlock_ != eventChunkSize_)
eventChunkSize_ = readBlocks_ * eventChunkBlock_;
if (!numBuffers_)
throw cms::Exception("FedRawDataInputSource::FedRawDataInputSource")
<< "no reading enabled with numBuffers parameter 0";
if (numConcurrentReads_ <= 0)
numConcurrentReads_ = numBuffers_ - 1;
readingFilesCount_ = 0;
heldFilesCount_ = 0;
if (!crc32c_hw_test())
edm::LogError("FedRawDataInputSource::FedRawDataInputSource") << "Intel crc32c checksum computation unavailable";
//get handles to DaqDirector and FastMonitoringService because getting them isn't possible in readSupervisor thread
if (fileListMode_) {
try {
fms_ = static_cast<evf::FastMonitoringService*>(edm::Service<evf::FastMonitoringService>().operator->());
} catch (cms::Exception const&) {
edm::LogInfo("FedRawDataInputSource") << "No FastMonitoringService found in the configuration";
}
} else {
fms_ = static_cast<evf::FastMonitoringService*>(edm::Service<evf::FastMonitoringService>().operator->());
if (!fms_) {
throw cms::Exception("FedRawDataInputSource") << "FastMonitoringService not found";
}
}
daqDirector_ = edm::Service<evf::EvFDaqDirector>().operator->();
if (!daqDirector_)
cms::Exception("FedRawDataInputSource") << "EvFDaqDirector not found";
useFileBroker_ = daqDirector_->useFileBroker();
if (useFileBroker_)
edm::LogInfo("FedRawDataInputSource") << "EvFDaqDirector/Source configured to use file service";
//set DaqDirector to delete files in preGlobalEndLumi callback
if (fms_) {
daqDirector_->setFMS(fms_);
fms_->setInputSource(this);
fms_->setInState(inInit);
fms_->setInStateSup(inInit);
}
//should delete chunks when run stops
for (unsigned int i = 0; i < numBuffers_; i++) {
freeChunks_.push(new InputChunk(eventChunkSize_));
}
quit_threads_ = false;
//prepare data shared by threads
for (unsigned int i = 0; i < (unsigned)numConcurrentReads_; i++) {
thread_quit_signal.push_back(false);
workerJob_.push_back(ReaderInfo(nullptr, nullptr));
cvReader_.push_back(std::make_unique<std::condition_variable>());
tid_active_.push_back(0);
}
//start threads
for (unsigned int i = 0; i < (unsigned)numConcurrentReads_; i++) {
//wait for each thread to complete initialization
std::unique_lock<std::mutex> lk(startupLock_);
workerThreads_.push_back(new std::thread(&FedRawDataInputSource::readWorker, this, i));
startupCv_.wait(lk);
}
runAuxiliary()->setProcessHistoryID(processHistoryID_);
}
FedRawDataInputSource::~FedRawDataInputSource() {
quit_threads_ = true;
if (startedSupervisorThread_)
fileDeleterThread_->join();
//delete any remaining open files
if (!fms_ || !fms_->exceptionDetected()) {
for (auto it = filesToDelete_.begin(); it != filesToDelete_.end(); it++)
it->second.reset();
} else {
//skip deleting files with exception
for (auto it = filesToDelete_.begin(); it != filesToDelete_.end(); it++) {
//it->second->unsetDeleteFile();
if (fms_->isExceptionOnData(it->second->lumi_))
it->second->unsetDeleteFile();
else
it->second.reset();
}
//disable deleting current file with exception
if (currentFile_.get())
if (fms_->isExceptionOnData(currentFile_->lumi_))
currentFile_->unsetDeleteFile();
}
if (startedSupervisorThread_) {
readSupervisorThread_->join();
} else {
//join aux threads in case the supervisor thread was not started
for (unsigned int i = 0; i < workerThreads_.size(); i++) {
std::unique_lock<std::mutex> lk(mReader_);
thread_quit_signal[i] = true;
cvReader_[i]->notify_one();
lk.unlock();
workerThreads_[i]->join();
delete workerThreads_[i];
}
}
}
void FedRawDataInputSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.setComment("File-based Filter Farm input source for reading raw data from BU ramdisk");
desc.addUntracked<unsigned int>("eventChunkSize", 32)->setComment("Input buffer (chunk) size");
desc.addUntracked<unsigned int>("eventChunkBlock", 32)
->setComment("Block size used in a single file read call (must be smaller or equal to buffer size)");
desc.addUntracked<int>("numConcurrentReads", -1)
->setComment("Max number of concurrent reads. If not positive, it will be set to numBuffers - 1");
desc.addUntracked<unsigned int>("numBuffers", 2)->setComment("Number of buffers used for reading input");
desc.addUntracked<unsigned int>("maxBufferedFiles", 2)
->setComment("Maximum number of simultaneously buffered raw files");
desc.addUntracked<unsigned int>("alwaysStartFromfirstLS", false)
->setComment("Force source to start from LS 1 if server provides higher lumisection number");
desc.addUntracked<bool>("verifyChecksum", true)
->setComment("Verify event CRC-32C checksum of FRDv5 and higher or Adler32 with v3 and v4");
desc.addUntracked<bool>("useL1EventID", false)
->setComment("Use L1 event ID from FED header if true or from TCDS FED if false");
desc.addUntracked<std::vector<unsigned int>>("testTCDSFEDRange", std::vector<unsigned int>())
->setComment("[min, max] range to search for TCDS FED ID in test setup");
desc.addUntracked<bool>("fileListMode", false)
->setComment("Use fileNames parameter to directly specify raw files to open");
desc.addUntracked<bool>("fileDiscoveryMode", false)
->setComment("Use filesystem discovery and assignment of files by renaming");
desc.addUntracked<std::vector<std::string>>("fileNames", std::vector<std::string>())
->setComment("file list used when fileListMode is enabled");
desc.setAllowAnything();
descriptions.add("source", desc);
}
edm::RawInputSource::Next FedRawDataInputSource::checkNext() {
if (!startedSupervisorThread_) {
//this thread opens new files and dispatches reading to worker readers
std::unique_lock<std::mutex> lk(startupLock_);
readSupervisorThread_ = std::make_unique<std::thread>(&FedRawDataInputSource::readSupervisor, this);
fileDeleterThread_ = std::make_unique<std::thread>(&FedRawDataInputSource::fileDeleter, this);
startedSupervisorThread_ = true;
startupCv_.wait(lk);
}
//signal hltd to start event accounting
if (!currentLumiSection_)
daqDirector_->createProcessingNotificationMaybe();
setMonState(inWaitInput);
switch (nextEvent()) {
case evf::EvFDaqDirector::runEnded: {
//maybe create EoL file in working directory before ending run
struct stat buf;
if (!useFileBroker_ && currentLumiSection_ > 0) {
bool eolFound = (stat(daqDirector_->getEoLSFilePathOnBU(currentLumiSection_).c_str(), &buf) == 0);
if (eolFound) {
const std::string fuEoLS = daqDirector_->getEoLSFilePathOnFU(currentLumiSection_);
bool found = (stat(fuEoLS.c_str(), &buf) == 0);
if (!found) {
daqDirector_->lockFULocal2();
int eol_fd =
open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
close(eol_fd);
daqDirector_->unlockFULocal2();
}
}
}
//also create EoR file in FU data directory
bool eorFound = (stat(daqDirector_->getEoRFilePathOnFU().c_str(), &buf) == 0);
if (!eorFound) {
int eor_fd = open(daqDirector_->getEoRFilePathOnFU().c_str(),
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
close(eor_fd);
}
reportEventsThisLumiInSource(currentLumiSection_, eventsThisLumi_);
eventsThisLumi_ = 0;
resetLuminosityBlockAuxiliary();
edm::LogInfo("FedRawDataInputSource") << "----------------RUN ENDED----------------";
return Next::kStop;
}
case evf::EvFDaqDirector::noFile: {
//this is not reachable
return Next::kEvent;
}
case evf::EvFDaqDirector::newLumi: {
//std::cout << "--------------NEW LUMI---------------" << std::endl;
return Next::kEvent;
}
default: {
if (!getLSFromFilename_) {
//get new lumi from file header
if (event_->lumi() > currentLumiSection_) {
reportEventsThisLumiInSource(currentLumiSection_, eventsThisLumi_);
eventsThisLumi_ = 0;
maybeOpenNewLumiSection(event_->lumi());
}
}
if (fileListMode_ || fileListLoopMode_)
eventRunNumber_ = runNumber_;
else
eventRunNumber_ = event_->run();
L1EventID_ = event_->event();
setEventCached();
return Next::kEvent;
}
}
}
void FedRawDataInputSource::maybeOpenNewLumiSection(const uint32_t lumiSection) {
if (!luminosityBlockAuxiliary() || luminosityBlockAuxiliary()->luminosityBlock() != lumiSection) {
if (!useFileBroker_) {
if (currentLumiSection_ > 0) {
const std::string fuEoLS = daqDirector_->getEoLSFilePathOnFU(currentLumiSection_);
struct stat buf;
bool found = (stat(fuEoLS.c_str(), &buf) == 0);
if (!found) {
daqDirector_->lockFULocal2();
int eol_fd =
open(fuEoLS.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
close(eol_fd);
daqDirector_->createBoLSFile(lumiSection, false);
daqDirector_->unlockFULocal2();
}
} else
daqDirector_->createBoLSFile(lumiSection, true); //needed for initial lumisection
}
currentLumiSection_ = lumiSection;
resetLuminosityBlockAuxiliary();
timeval tv;
gettimeofday(&tv, nullptr);
const edm::Timestamp lsopentime((unsigned long long)tv.tv_sec * 1000000 + (unsigned long long)tv.tv_usec);
edm::LuminosityBlockAuxiliary* lumiBlockAuxiliary = new edm::LuminosityBlockAuxiliary(
runAuxiliary()->run(), lumiSection, lsopentime, edm::Timestamp::invalidTimestamp());
setLuminosityBlockAuxiliary(lumiBlockAuxiliary);
luminosityBlockAuxiliary()->setProcessHistoryID(processHistoryID_);
edm::LogInfo("FedRawDataInputSource") << "New lumi section was opened. LUMI -: " << lumiSection;
}
}
inline evf::EvFDaqDirector::FileStatus FedRawDataInputSource::nextEvent() {
evf::EvFDaqDirector::FileStatus status = evf::EvFDaqDirector::noFile;
while ((status = getNextEvent()) == evf::EvFDaqDirector::noFile) {
if (edm::shutdown_flag.load(std::memory_order_relaxed))
break;
}
return status;
}
inline evf::EvFDaqDirector::FileStatus FedRawDataInputSource::getNextEvent() {
if (setExceptionState_)
threadError();
if (!currentFile_.get()) {
evf::EvFDaqDirector::FileStatus status = evf::EvFDaqDirector::noFile;
setMonState(inWaitInput);
{
IdleSourceSentry ids(fms_);
if (!fileQueue_.try_pop(currentFile_)) {
//sleep until wakeup (only in single-buffer mode) or timeout
std::unique_lock<std::mutex> lkw(mWakeup_);
if (cvWakeup_.wait_for(lkw, std::chrono::milliseconds(100)) == std::cv_status::timeout || !currentFile_.get())
return evf::EvFDaqDirector::noFile;
}
}
status = currentFile_->status_;
if (status == evf::EvFDaqDirector::runEnded) {
setMonState(inRunEnd);
currentFile_.reset();
return status;
} else if (status == evf::EvFDaqDirector::runAbort) {
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Run has been aborted by the input source reader thread";
} else if (status == evf::EvFDaqDirector::newLumi) {
setMonState(inNewLumi);
if (getLSFromFilename_) {
if (currentFile_->lumi_ > currentLumiSection_) {
reportEventsThisLumiInSource(currentLumiSection_, eventsThisLumi_);
eventsThisLumi_ = 0;
maybeOpenNewLumiSection(currentFile_->lumi_);
}
} else { //let this be picked up from next event
status = evf::EvFDaqDirector::noFile;
}
currentFile_.reset();
return status;
} else if (status == evf::EvFDaqDirector::newFile) {
currentFileIndex_++;
} else
assert(false);
}
setMonState(inProcessingFile);
//file is empty
if (!currentFile_->fileSize_) {
readingFilesCount_--;
heldFilesCount_--;
//try to open new lumi
assert(currentFile_->nChunks_ == 0);
if (getLSFromFilename_)
if (currentFile_->lumi_ > currentLumiSection_) {
reportEventsThisLumiInSource(currentLumiSection_, eventsThisLumi_);
eventsThisLumi_ = 0;
maybeOpenNewLumiSection(currentFile_->lumi_);
}
//immediately delete empty file
currentFile_.reset();
return evf::EvFDaqDirector::noFile;
}
//file is finished
if (currentFile_->bufferPosition_ == currentFile_->fileSize_) {
readingFilesCount_--;
if (fileListMode_)
heldFilesCount_--;
//release last chunk (it is never released elsewhere)
freeChunks_.push(currentFile_->chunks_[currentFile_->currentChunk_]);
if (currentFile_->nEvents_ >= 0 && currentFile_->nEvents_ != int(currentFile_->nProcessed_)) {
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Fully processed " << currentFile_->nProcessed_ << " from the file " << currentFile_->fileName_
<< " but according to BU JSON there should be " << currentFile_->nEvents_ << " events";
}
//TODO:try to wake up supervisor thread which might be sleeping waiting for the free chunk
bufferInputRead_ = 0;
setMonState(inReadCleanup);
if (!daqDirector_->isSingleStreamThread() && !fileListMode_) {
//put the file in pending delete list;
std::unique_lock<std::mutex> lkw(fileDeleteLock_);
filesToDelete_.push_back(std::pair<int, std::unique_ptr<InputFile>>(currentFileIndex_, std::move(currentFile_)));
} else {
//in single-thread and stream jobs, events are already processed
currentFile_.reset();
}
setMonState(inProcessingFile);
return evf::EvFDaqDirector::noFile;
}
//handle RAW file header
if (currentFile_->bufferPosition_ == 0 && currentFile_->rawHeaderSize_ > 0) {
if (currentFile_->fileSize_ <= currentFile_->rawHeaderSize_) {
if (currentFile_->fileSize_ < currentFile_->rawHeaderSize_)
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Premature end of input file while reading file header";
edm::LogWarning("FedRawDataInputSource")
<< "File with only raw header and no events received in LS " << currentFile_->lumi_;
if (getLSFromFilename_)
if (currentFile_->lumi_ > currentLumiSection_) {
reportEventsThisLumiInSource(currentLumiSection_, eventsThisLumi_);
eventsThisLumi_ = 0;
maybeOpenNewLumiSection(currentFile_->lumi_);
}
}
//advance buffer position to skip file header (chunk will be acquired later)
currentFile_->chunkPosition_ += currentFile_->rawHeaderSize_;
currentFile_->bufferPosition_ += currentFile_->rawHeaderSize_;
}
//file is too short
if (currentFile_->fileSize_ - currentFile_->bufferPosition_ < FRDHeaderVersionSize[detectedFRDversion_]) {
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Premature end of input file while reading event header";
}
{
//wait for the current chunk to become added to the vector
setMonState(inWaitChunk);
{
IdleSourceSentry ids(fms_);
while (!currentFile_->waitForChunk(currentFile_->currentChunk_)) {
std::unique_lock<std::mutex> lkw(mWakeup_);
cvWakeupAll_.wait_for(lkw, std::chrono::milliseconds(100));
if (setExceptionState_)
threadError();
}
}
setMonState(inChunkReceived);
//check if header is at the boundary of two chunks
chunkIsFree_ = false;
unsigned char* dataPosition;
//read header, copy it to a single chunk if necessary
if (currentFile_->fileSizeLeft() < FRDHeaderVersionSize[detectedFRDversion_])
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Premature end of input file (missing:"
<< (FRDHeaderVersionSize[detectedFRDversion_] - currentFile_->fileSizeLeft())
<< ") while reading event data for next event header";
bool chunkEnd =
currentFile_->advance(mWakeup_, cvWakeupAll_, dataPosition, FRDHeaderVersionSize[detectedFRDversion_]);
event_ = std::make_unique<FRDEventMsgView>(dataPosition);
if (event_->size() > eventChunkSize_) {
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< " event id:" << event_->event() << " lumi:" << event_->lumi() << " run:" << event_->run()
<< " of size:" << event_->size() << " bytes does not fit into a chunk of size:" << eventChunkSize_
<< " bytes";
}
const uint32_t msgSize = event_->size() - FRDHeaderVersionSize[detectedFRDversion_];
if (currentFile_->fileSizeLeft() < msgSize) {
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Premature end of input file (missing:" << (msgSize - currentFile_->fileSizeLeft())
<< ") while reading event data for event " << event_->event() << " lumi:" << event_->lumi();
}
if (chunkEnd) {
//header was at the chunk boundary, we will have to move payload as well
currentFile_->moveToPreviousChunk(msgSize, FRDHeaderVersionSize[detectedFRDversion_]);
chunkIsFree_ = true;
} else {
//header was contiguous, but check if payload fits the chunk
if (eventChunkSize_ - currentFile_->chunkPosition_ < msgSize) {
//rewind to header start position
currentFile_->rewindChunk(FRDHeaderVersionSize[detectedFRDversion_]);
//copy event to a chunk start and move pointers
setMonState(inWaitChunk);
{
IdleSourceSentry ids(fms_);
chunkEnd = currentFile_->advance(
mWakeup_, cvWakeupAll_, dataPosition, FRDHeaderVersionSize[detectedFRDversion_] + msgSize);
}
setMonState(inChunkReceived);
assert(chunkEnd);
chunkIsFree_ = true;
//header is moved
event_ = std::make_unique<FRDEventMsgView>(dataPosition);
} else {
//everything is in a single chunk, only move pointers forward
chunkEnd = currentFile_->advance(mWakeup_, cvWakeupAll_, dataPosition, msgSize);
assert(!chunkEnd);
chunkIsFree_ = false;
}
}
//sanity-check check that the buffer position has not exceeded file size after preparing event
if (currentFile_->fileSize_ < currentFile_->bufferPosition_) {
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Exceeded file size by " << currentFile_->bufferPosition_ - currentFile_->fileSize_
<< " after reading last event declared size of " << event_->size() << " bytes";
}
}
setMonState(inChecksumEvent);
if (verifyChecksum_ && event_->version() >= 5) {
uint32_t crc = 0;
crc = crc32c(crc, (const unsigned char*)event_->payload(), event_->eventSize());
if (crc != event_->crc32c()) {
if (fms_)
fms_->setExceptionDetected(currentLumiSection_);
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "Found a wrong crc32c checksum: expected 0x" << std::hex << event_->crc32c() << " but calculated 0x"
<< crc;
}
} else if (event_->version() < 5)
throw cms::Exception("FedRawDataInputSource::getNextEvent")
<< "FRD event version " << event_->version() << " (< 5) is no longer supported";
setMonState(inCachedEvent);
currentFile_->nProcessed_++;
return evf::EvFDaqDirector::sameFile;
}
void FedRawDataInputSource::read(edm::EventPrincipal& eventPrincipal) {
setMonState(inReadEvent);
std::unique_ptr<FEDRawDataCollection> rawData(new FEDRawDataCollection);
bool tcdsInRange;
edm::Timestamp tstamp = fillFEDRawDataCollection(*rawData, tcdsInRange);
if (useL1EventID_) {
eventID_ = edm::EventID(eventRunNumber_, currentLumiSection_, L1EventID_);
edm::EventAuxiliary aux(eventID_, processGUID(), tstamp, event_->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
aux.setProcessHistoryID(processHistoryID_);
makeEvent(eventPrincipal, aux);
} else if (tcds_pointer_ == nullptr) {
if (!GTPEventID_) {
throw cms::Exception("FedRawDataInputSource::read")
<< "No TCDS or GTP FED in event with FEDHeader EID -: " << L1EventID_;
}
eventID_ = edm::EventID(eventRunNumber_, currentLumiSection_, GTPEventID_);
edm::EventAuxiliary aux(eventID_, processGUID(), tstamp, event_->isRealData(), edm::EventAuxiliary::PhysicsTrigger);
aux.setProcessHistoryID(processHistoryID_);
makeEvent(eventPrincipal, aux);
} else {
const FEDHeader fedHeader(tcds_pointer_);
tcds::Raw_v1 const* tcds = reinterpret_cast<tcds::Raw_v1 const*>(tcds_pointer_ + FEDHeader::length);
edm::EventAuxiliary aux =
evf::evtn::makeEventAuxiliary(tcds,
eventRunNumber_,
currentLumiSection_,
event_->isRealData(),
static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.triggerType()),
processGUID(),
!fileListLoopMode_,
!tcdsInRange);
aux.setProcessHistoryID(processHistoryID_);
makeEvent(eventPrincipal, aux);
}
std::unique_ptr<edm::WrapperBase> edp(new edm::Wrapper<FEDRawDataCollection>(std::move(rawData)));
eventPrincipal.put(daqProvenanceHelper_.productDescription(), std::move(edp), daqProvenanceHelper_.dummyProvenance());
eventsThisLumi_++;
setMonState(inReadCleanup);
//resize vector if needed
while (streamFileTracker_.size() <= eventPrincipal.streamID()) {
std::unique_lock<std::mutex> lkw(fileDeleteLock_);
streamFileTracker_.push_back(-1);
}
streamFileTracker_[eventPrincipal.streamID()] = currentFileIndex_;
setMonState(inNoRequest);
if (chunkIsFree_)
freeChunks_.push(currentFile_->chunks_[currentFile_->currentChunk_ - 1]);
chunkIsFree_ = false;
return;
}
edm::Timestamp FedRawDataInputSource::fillFEDRawDataCollection(FEDRawDataCollection& rawData, bool& tcdsInRange) {
edm::TimeValue_t time;
timeval stv;
gettimeofday(&stv, nullptr);
time = stv.tv_sec;
time = (time << 32) + stv.tv_usec;
edm::Timestamp tstamp(time);
uint32_t eventSize = event_->eventSize();
unsigned char* event = (unsigned char*)event_->payload();
GTPEventID_ = 0;
tcds_pointer_ = nullptr;
tcdsInRange = false;
uint16_t selectedTCDSFed = 0;
unsigned int fedsInEvent = 0;
while (eventSize > 0) {
assert(eventSize >= FEDTrailer::length);
eventSize -= FEDTrailer::length;
const FEDTrailer fedTrailer(event + eventSize);
const uint32_t fedSize = fedTrailer.fragmentLength() << 3; //trailer length counts in 8 bytes
assert(eventSize >= fedSize - FEDHeader::length);
eventSize -= (fedSize - FEDHeader::length);
const FEDHeader fedHeader(event + eventSize);
const uint16_t fedId = fedHeader.sourceID();
if (fedId > FEDNumbering::MAXFEDID) {
throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection") << "Out of range FED ID : " << fedId;
} else if (fedId >= MINTCDSuTCAFEDID_ && fedId <= MAXTCDSuTCAFEDID_) {
if (!selectedTCDSFed) {
selectedTCDSFed = fedId;
tcds_pointer_ = event + eventSize;
if (fedId >= FEDNumbering::MINTCDSuTCAFEDID && fedId <= FEDNumbering::MAXTCDSuTCAFEDID) {
tcdsInRange = true;
}
} else
throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection")
<< "Second TCDS FED ID " << fedId << " found. First ID: " << selectedTCDSFed;
}
if (fedId == FEDNumbering::MINTriggerGTPFEDID) {
if (evf::evtn::evm_board_sense(event + eventSize, fedSize))
GTPEventID_ = evf::evtn::get(event + eventSize, true);
else
GTPEventID_ = evf::evtn::get(event + eventSize, false);
//evf::evtn::evm_board_setformat(fedSize);
const uint64_t gpsl = evf::evtn::getgpslow(event + eventSize);
const uint64_t gpsh = evf::evtn::getgpshigh(event + eventSize);
tstamp = edm::Timestamp(static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl));
}
//take event ID from GTPE FED
if (fedId == FEDNumbering::MINTriggerEGTPFEDID && GTPEventID_ == 0) {
if (evf::evtn::gtpe_board_sense(event + eventSize)) {
GTPEventID_ = evf::evtn::gtpe_get(event + eventSize);
}
}
fedsInEvent++;
FEDRawData& fedData = rawData.FEDData(fedId);
fedData.resize(fedSize);
memcpy(fedData.data(), event + eventSize, fedSize);
}
assert(eventSize == 0);
if (fedsInEvent != expectedFedsInEvent_ && expectedFedsInEvent_)
edm::LogWarning("DataModeFRDStriped:::fillFRDCollection")
<< "Event " << event_->event() << " does not contain same number of FEDs as previous: " << fedsInEvent << "/"
<< expectedFedsInEvent_;
return tstamp;
}
void FedRawDataInputSource::rewind_() {}
void FedRawDataInputSource::fileDeleter() {
bool stop = false;
while (!stop) {
std::vector<InputFile*> deleteVec;
{
unsigned int lastFileLS = 0;
bool fileLSOpen = false;
std::unique_lock<std::mutex> lkw(fileDeleteLock_);
auto it = filesToDelete_.begin();
while (it != filesToDelete_.end()) {
bool fileIsBeingProcessed = false;
//check if file LS has already reached global EoL, reuse cached check
if (!(lastFileLS && lastFileLS == it->second->lumi_)) {
lastFileLS = it->second->lumi_;
fileLSOpen = daqDirector_->lsWithFilesOpen(lastFileLS);
}
for (unsigned int i = 0; i < streamFileTracker_.size(); i++) {
if (it->first == streamFileTracker_.at(i)) {
//only skip if LS is open
if (fileLSOpen && (!fms_ || !fms_->streamIsIdle(i))) {
fileIsBeingProcessed = true;
break;
}
}
}
if (!fileIsBeingProcessed && (!fms_ || !fms_->isExceptionOnData(it->second->lumi_))) {
std::string fileToDelete = it->second->fileName_;
//do not actuallt delete, but do it later
deleteVec.push_back(it->second.get());
//deletion will happen later
it->second.release();
it = filesToDelete_.erase(it);
} else
it++;
}
}
//do this after lock is released to avoid contention
for (auto v : deleteVec) {
//deletion happens here
delete v;
heldFilesCount_--;
}
deleteVec.clear();
if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed))
stop = true;
usleep(500000);
}
}
void FedRawDataInputSource::readSupervisor() {
bool stop = false;
unsigned int currentLumiSection = 0;
{
std::unique_lock<std::mutex> lk(startupLock_);
startupCv_.notify_one();
}
uint32_t ls = 0;
uint32_t monLS = 1;
uint32_t lockCount = 0;
uint64_t sumLockWaitTimeUs = 0.;
while (!stop) {
//wait for at least one free thread and chunk
int counter = 0;
//held files include files queued in the deleting thread.
//We require no more than maxBufferedFiles + 2 of total held files until deletion
while (workerPool_.empty() || freeChunks_.empty() || readingFilesCount_ >= maxBufferedFiles_ ||
heldFilesCount_ >= maxBufferedFiles_ + 2) {
//report state to monitoring
if (fms_) {
bool copy_active = false;
for (auto j : tid_active_)
if (j)
copy_active = true;
if (readingFilesCount_ >= maxBufferedFiles_)
setMonStateSup(inSupFileLimit);
else if (heldFilesCount_ >= maxBufferedFiles_ + 2)
setMonStateSup(inSupFileHeldLimit);
else if (freeChunks_.empty()) {
if (copy_active)
setMonStateSup(inSupWaitFreeChunkCopying);
else
setMonStateSup(inSupWaitFreeChunk);
} else {
if (copy_active)
setMonStateSup(inSupWaitFreeThreadCopying);
else
setMonStateSup(inSupWaitFreeThread);
}
}
std::unique_lock<std::mutex> lkw(mWakeup_);
//sleep until woken up by condition or a timeout
if (cvWakeup_.wait_for(lkw, std::chrono::milliseconds(100)) == std::cv_status::timeout) {
counter++;
if (!(counter % 6000)) {
edm::LogWarning("FedRawDataInputSource")
<< "No free chunks or threads. Worker pool empty:" << workerPool_.empty()
<< ", free chunks empty:" << freeChunks_.empty()
<< ", number of files buffered (held):" << readingFilesCount_ << "(" << heldFilesCount_ << ")"
<< " / " << maxBufferedFiles_;
}
LogDebug("FedRawDataInputSource") << "No free chunks or threads...";
}
if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed)) {
stop = true;
break;
}
}
//if this is reached, there are enough buffers and threads to proceed or processing is instructed to stop
if (stop)
break;
//look for a new file
std::string nextFile;
uint32_t fileSizeIndex;
int64_t fileSizeFromMetadata;
if (fms_) {
setMonStateSup(inSupBusy);
fms_->startedLookingForFile();
}
evf::EvFDaqDirector::FileStatus status = evf::EvFDaqDirector::noFile;
uint16_t rawHeaderSize = 0;
uint32_t lsFromRaw = 0;
int32_t serverEventsInNewFile = -1;
int rawFd = -1;
int backoff_exp = 0;
//entering loop which tries to grab new file from ramdisk
while (status == evf::EvFDaqDirector::noFile) {
//check if hltd has signalled to throttle input
counter = 0;
while (daqDirector_->inputThrottled()) {
if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed))
break;
unsigned int nConcurrentLumis = daqDirector_->numConcurrentLumis();
unsigned int nOtherLumis = nConcurrentLumis > 0 ? nConcurrentLumis - 1 : 0;
unsigned int checkLumiStart = currentLumiSection > nOtherLumis ? currentLumiSection - nOtherLumis : 1;
bool hasDiscardedLumi = false;
for (unsigned int i = checkLumiStart; i <= currentLumiSection; i++) {
if (daqDirector_->lumisectionDiscarded(i)) {
edm::LogWarning("FedRawDataInputSource") << "Source detected that the lumisection is discarded -: " << i;
hasDiscardedLumi = true;
break;
}
}
if (hasDiscardedLumi)
break;
setMonStateSup(inThrottled);
if (!(counter % 50))
edm::LogWarning("FedRawDataInputSource") << "Input throttled detected, reading files is paused...";
usleep(100000);
counter++;
}
if (quit_threads_.load(std::memory_order_relaxed) || edm::shutdown_flag.load(std::memory_order_relaxed)) {
stop = true;
break;
}
assert(rawFd == -1);
uint64_t thisLockWaitTimeUs = 0.;
setMonStateSup(inSupLockPolling);
if (fileListMode_) {
//return LS if LS not set, otherwise return file
status = getFile(ls, nextFile, fileSizeIndex, thisLockWaitTimeUs);
if (status == evf::EvFDaqDirector::newFile) {
uint16_t rawDataType;
if (evf::EvFDaqDirector::parseFRDFileHeader(nextFile,
rawFd,
rawHeaderSize,
rawDataType,
lsFromRaw,
serverEventsInNewFile,
fileSizeFromMetadata,
false,
false,
false) != 0) {
//error
setExceptionState_ = true;
stop = true;
break;
}
if (!getLSFromFilename_)
ls = lsFromRaw;
}
} else if (!useFileBroker_)
status = daqDirector_->updateFuLock(
ls, nextFile, fileSizeIndex, rawHeaderSize, thisLockWaitTimeUs, setExceptionState_);
else {
status = daqDirector_->getNextFromFileBroker(currentLumiSection,
ls,
nextFile,
rawFd,
rawHeaderSize,
serverEventsInNewFile,
fileSizeFromMetadata,
thisLockWaitTimeUs,
true,
fileDiscoveryMode_);
}
setMonStateSup(inSupBusy);
//cycle through all remaining LS even if no files get assigned
if (currentLumiSection != ls && status == evf::EvFDaqDirector::runEnded)
status = evf::EvFDaqDirector::noFile;
//monitoring of lock wait time
if (thisLockWaitTimeUs > 0.)
sumLockWaitTimeUs += thisLockWaitTimeUs;
lockCount++;
if (ls > monLS) {
monLS = ls;
if (lockCount)
if (fms_)
fms_->reportLockWait(monLS, sumLockWaitTimeUs, lockCount);
lockCount = 0;
sumLockWaitTimeUs = 0;
}
//check again for any remaining index/EoLS files after EoR file is seen
if (status == evf::EvFDaqDirector::runEnded && !fileListMode_ && !useFileBroker_) {
setMonStateSup(inRunEnd);
usleep(100000);
//now all files should have appeared in ramdisk, check again if any raw files were left behind
status = daqDirector_->updateFuLock(
ls, nextFile, fileSizeIndex, rawHeaderSize, thisLockWaitTimeUs, setExceptionState_);
if (currentLumiSection != ls && status == evf::EvFDaqDirector::runEnded)
status = evf::EvFDaqDirector::noFile;
}
if (status == evf::EvFDaqDirector::runEnded) {
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::runEnded));
fileQueue_.push(std::move(inf));
stop = true;
break;
}
//error from filelocking function
if (status == evf::EvFDaqDirector::runAbort) {
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::runAbort, 0));
fileQueue_.push(std::move(inf));
stop = true;
break;
}
//queue new lumisection
if (getLSFromFilename_) {
if (ls > currentLumiSection) {
if (!useFileBroker_) {
//file locking
//setMonStateSup(inSupNewLumi);
currentLumiSection = ls;
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, currentLumiSection));
fileQueue_.push(std::move(inf));
} else {
//new file service
if (currentLumiSection == 0 && !alwaysStartFromFirstLS_) {
if (daqDirector_->getStartLumisectionFromEnv() > 1) {
//start transitions from LS specified by env, continue if not reached
if (ls < daqDirector_->getStartLumisectionFromEnv()) {
//skip file if from earlier LS than specified by env
if (rawFd != -1) {
close(rawFd);
rawFd = -1;
}
status = evf::EvFDaqDirector::noFile;
continue;
} else {
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, ls));
fileQueue_.push(std::move(inf));
}
} else if (ls < 100) {
//look at last LS file on disk to start from that lumisection (only within first 100 LS)
unsigned int lsToStart = daqDirector_->getLumisectionToStart();
for (unsigned int nextLS = std::min(lsToStart, ls); nextLS <= ls; nextLS++) {
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, nextLS));
fileQueue_.push(std::move(inf));
}
} else {
//start from current LS
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, ls));
fileQueue_.push(std::move(inf));
}
} else {
//queue all lumisections after last one seen to avoid gaps
for (unsigned int nextLS = currentLumiSection + 1; nextLS <= ls; nextLS++) {
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::newLumi, nextLS));
fileQueue_.push(std::move(inf));
}
}
currentLumiSection = ls;
//wakeup main thread for the new non-data file obj
std::unique_lock<std::mutex> lkw(mWakeup_);
cvWakeupAll_.notify_all();
}
}
//else
if (currentLumiSection > 0 && ls < currentLumiSection) {
edm::LogError("FedRawDataInputSource")
<< "Got old LS (" << ls << ") file from EvFDAQDirector! Expected LS:" << currentLumiSection
<< ". Aborting execution." << std::endl;
if (rawFd != -1)
close(rawFd);
rawFd = -1;
std::unique_ptr<InputFile> inf(new InputFile(evf::EvFDaqDirector::runAbort, 0));
fileQueue_.push(std::move(inf));
stop = true;
break;
}
}
int dbgcount = 0;
if (status == evf::EvFDaqDirector::noFile) {
setMonStateSup(inSupNoFile);
dbgcount++;
if (!(dbgcount % 20))
LogDebug("FedRawDataInputSource") << "No file for me... sleep and try again...";
if (!useFileBroker_)
usleep(100000);
else {
backoff_exp = std::min(4, backoff_exp); // max 1.6 seconds
//backoff_exp=0; // disabled!
int sleeptime = (int)(100000. * pow(2, backoff_exp));
usleep(sleeptime);
backoff_exp++;
}
} else
backoff_exp = 0;
}
//end of file grab loop, parse result
if (status == evf::EvFDaqDirector::newFile) {
setMonStateSup(inSupNewFile);
LogDebug("FedRawDataInputSource") << "The director says to grab -: " << nextFile;
std::string rawFile;
//file service will report raw extension
if (useFileBroker_ || rawHeaderSize)
rawFile = nextFile;
else {
std::filesystem::path rawFilePath(nextFile);
rawFile = rawFilePath.replace_extension(".raw").string();
}
struct stat st;
int stat_res = stat(rawFile.c_str(), &st);
if (stat_res == -1) {
edm::LogError("FedRawDataInputSource") << "Can not stat file (" << errno << ") :- " << rawFile << std::endl;
setExceptionState_ = true;
break;
}
uint64_t fileSize = st.st_size;
if (fms_) {
setMonStateSup(inSupBusy);
fms_->stoppedLookingForFile(ls);
setMonStateSup(inSupNewFile);
}
int eventsInNewFile;
if (fileListMode_) {
if (fileSize == 0)
eventsInNewFile = 0;
else
eventsInNewFile = -1;
} else {
std::string empty;
if (!useFileBroker_) {
if (rawHeaderSize) {
int rawFdEmpty = -1;
uint16_t rawHeaderCheck;
bool fileFound;
eventsInNewFile = daqDirector_->grabNextJsonFromRaw(
nextFile, rawFdEmpty, rawHeaderCheck, fileSizeFromMetadata, fileFound, 0, true);
assert(fileFound && rawHeaderCheck == rawHeaderSize);
daqDirector_->unlockFULocal();
} else
eventsInNewFile = daqDirector_->grabNextJsonFileAndUnlock(nextFile);
} else
eventsInNewFile = serverEventsInNewFile;
assert(eventsInNewFile >= 0);
assert((eventsInNewFile > 0) ==
(fileSize > rawHeaderSize)); //file without events must be empty or contain only header
}
{
//calculate number of needed chunks
unsigned int neededChunks = fileSize / eventChunkSize_;
if (fileSize % eventChunkSize_)
neededChunks++;
std::unique_ptr<InputFile> newInputFile(new InputFile(evf::EvFDaqDirector::FileStatus::newFile,
ls,
rawFile,
!fileListMode_,
rawFd,
fileSize,
rawHeaderSize,
neededChunks,
eventsInNewFile,
this));
readingFilesCount_++;
heldFilesCount_++;
auto newInputFilePtr = newInputFile.get();
fileQueue_.push(std::move(newInputFile));
for (unsigned int i = 0; i < neededChunks; i++) {
if (fms_) {
bool copy_active = false;
for (auto j : tid_active_)
if (j)
copy_active = true;
if (copy_active)
setMonStateSup(inSupNewFileWaitThreadCopying);
else
setMonStateSup(inSupNewFileWaitThread);
}
//get thread
unsigned int newTid = 0xffffffff;
while (!workerPool_.try_pop(newTid)) {
usleep(100000);
if (quit_threads_.load(std::memory_order_relaxed)) {
stop = true;
break;
}
}
if (fms_) {
bool copy_active = false;
for (auto j : tid_active_)
if (j)
copy_active = true;
if (copy_active)
setMonStateSup(inSupNewFileWaitChunkCopying);
else
setMonStateSup(inSupNewFileWaitChunk);
}
InputChunk* newChunk = nullptr;
while (!freeChunks_.try_pop(newChunk)) {
usleep(100000);
if (quit_threads_.load(std::memory_order_relaxed)) {
stop = true;
break;
}
}
if (newChunk == nullptr) {
//return unused tid if we received shutdown (nullptr chunk)
if (newTid != 0xffffffff)
workerPool_.push(newTid);
stop = true;
break;
}
if (stop)
break;
setMonStateSup(inSupNewFile);
std::unique_lock<std::mutex> lk(mReader_);
unsigned int toRead = eventChunkSize_;
if (i == neededChunks - 1 && fileSize % eventChunkSize_)
toRead = fileSize % eventChunkSize_;
newChunk->reset(i * eventChunkSize_, toRead, i);
workerJob_[newTid].first = newInputFilePtr;
workerJob_[newTid].second = newChunk;
//wake up the worker thread
cvReader_[newTid]->notify_one();
}
}
}
}
setMonStateSup(inRunEnd);
//make sure threads finish reading
unsigned numFinishedThreads = 0;
while (numFinishedThreads < workerThreads_.size()) {
unsigned tid = 0;
while (!workerPool_.try_pop(tid)) {
usleep(10000);
}
std::unique_lock<std::mutex> lk(mReader_);
thread_quit_signal[tid] = true;
cvReader_[tid]->notify_one();
numFinishedThreads++;
}
for (unsigned int i = 0; i < workerThreads_.size(); i++) {
workerThreads_[i]->join();
delete workerThreads_[i];
}
}
void FedRawDataInputSource::readWorker(unsigned int tid) {
bool init = true;
while (true) {
tid_active_[tid] = false;
std::unique_lock<std::mutex> lk(mReader_);
workerJob_[tid].first = nullptr;
workerJob_[tid].second = nullptr;
assert(!thread_quit_signal[tid]); //should never get it here
workerPool_.push(tid);
if (init) {
std::unique_lock<std::mutex> lks(startupLock_);
init = false;
startupCv_.notify_one();
}
cvWakeup_.notify_all();
cvReader_[tid]->wait(lk);
lk.unlock();
if (thread_quit_signal[tid])
return;
tid_active_[tid] = true;
//timeval ts_copystart;
//gettimeofday(&ts_copystart, nullptr);
InputFile* file;
InputChunk* chunk;
assert(workerJob_[tid].first != nullptr && workerJob_[tid].second != nullptr);
file = workerJob_[tid].first;
chunk = workerJob_[tid].second;
//skip reading initial header size in first chunk if inheriting file descriptor (already set at appropriate position)
unsigned int bufferLeft = (chunk->offset_ == 0 && file->rawFd_ != -1) ? file->rawHeaderSize_ : 0;
//if only one worker thread exists, use single fd for all operations
//if more worker threads exist, use rawFd_ for only the first read operation and then close file
int fileDescriptor;
bool fileOpenedHere = false;
if (numConcurrentReads_ == 1) {
fileDescriptor = file->rawFd_;
if (fileDescriptor == -1) {
fileDescriptor = open(file->fileName_.c_str(), O_RDONLY);
fileOpenedHere = true;
file->rawFd_ = fileDescriptor;
}
} else {
if (chunk->offset_ == 0) {
fileDescriptor = file->rawFd_;
file->rawFd_ = -1;
if (fileDescriptor == -1) {
fileDescriptor = open(file->fileName_.c_str(), O_RDONLY);
fileOpenedHere = true;
}
} else {
fileDescriptor = open(file->fileName_.c_str(), O_RDONLY);
fileOpenedHere = true;
}
}
if (fileDescriptor < 0) {
edm::LogError("FedRawDataInputSource") << "readWorker failed to open file -: " << file->fileName_
<< " fd:" << fileDescriptor << " error: " << strerror(errno);
setExceptionState_ = true;
continue;
}
if (fileOpenedHere) { //fast forward to this chunk position
off_t pos = 0;
pos = lseek(fileDescriptor, chunk->offset_, SEEK_SET);
if (pos == -1) {
edm::LogError("FedRawDataInputSource")
<< "readWorker failed to seek file -: " << file->fileName_ << " fd:" << fileDescriptor << " to offset "
<< chunk->offset_ << " error: " << strerror(errno);
setExceptionState_ = true;
continue;
}
}
LogDebug("FedRawDataInputSource") << "Reader thread opened file -: TID: " << tid << " file: " << file->fileName_
<< " at offset " << lseek(fileDescriptor, 0, SEEK_CUR);
unsigned int skipped = bufferLeft;
auto start = std::chrono::high_resolution_clock::now();
for (unsigned int i = 0; i < readBlocks_; i++) {
ssize_t last;
//protect against reading into next block
last = ::read(fileDescriptor,
(void*)(chunk->buf_ + bufferLeft),
std::min(chunk->usedSize_ - bufferLeft, (uint64_t)eventChunkBlock_));
if (last < 0) {
edm::LogError("FedRawDataInputSource") << "readWorker failed to read file -: " << file->fileName_
<< " fd:" << fileDescriptor << " error: " << strerror(errno);
setExceptionState_ = true;
break;
}
if (last > 0)
bufferLeft += last;
if (last < eventChunkBlock_) { //last read
//check if this is last block, then total read size must match file size
if (!(chunk->usedSize_ - skipped == i * eventChunkBlock_ + (unsigned int)last)) {
edm::LogError("FedRawDataInputSource")
<< "readWorker failed to read file -: " << file->fileName_ << " fd:" << fileDescriptor << " last:" << last
<< " expectedChunkSize:" << chunk->usedSize_
<< " readChunkSize:" << (skipped + i * eventChunkBlock_ + last) << " skipped:" << skipped
<< " block:" << (i + 1) << "/" << readBlocks_ << " error: " << strerror(errno);
setExceptionState_ = true;
}
break;
}
}
if (setExceptionState_)
continue;
auto end = std::chrono::high_resolution_clock::now();
auto diff = end - start;
std::chrono::milliseconds msec = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
LogDebug("FedRawDataInputSource") << " finished reading block -: " << (bufferLeft / (1024. * 1024)) << " MB"
<< " in " << msec.count() << " ms ("
<< (bufferLeft / (1024. * 1024.)) / double(msec.count()) << " GB/s)";
if (chunk->offset_ + bufferLeft == file->fileSize_) { //file reading finished using same fd
close(fileDescriptor);
fileDescriptor = -1;
if (numConcurrentReads_ == 1)
file->rawFd_ = -1;
}
if (numConcurrentReads_ > 1 && fileDescriptor != -1)
close(fileDescriptor);
//detect FRD event version. Skip file Header if it exists
if (detectedFRDversion_ == 0 && chunk->offset_ == 0) {
detectedFRDversion_ = *((uint16_t*)(chunk->buf_ + file->rawHeaderSize_));
}
assert(detectedFRDversion_ <= FRDHeaderMaxVersion);
//maybe lock is not needed here
std::unique_lock<std::mutex> lkw(mWakeup_);
//chunk->readComplete_ =
// true; //this is atomic to secure the sequential buffer fill before becoming available for processing)
file->chunks_[chunk->fileIndex_] = chunk; //put the completed chunk in the file chunk vector at predetermined index
chunk->readComplete_ =
true; //this is atomic to secure the sequential buffer fill before becoming available for processing)
//wakeup for chunk
cvWakeupAll_.notify_all();
//timeval ts_copyend;
//gettimeofday(&ts_copyend, nullptr);
//long deltat = (ts_copyend.tv_usec - ts_copystart.tv_usec) + (ts_copyend.tv_sec - ts_copystart.tv_sec) * 1000000;
//std::cout << "WORKER_COPYTIME:" << deltat*0.000001 << " sec " << std::endl;
}
}
void FedRawDataInputSource::threadError() {
quit_threads_ = true;
throw cms::Exception("FedRawDataInputSource:threadError") << " file reader thread error ";
}
inline void FedRawDataInputSource::setMonState(evf::FastMonState::InputState state) {
if (fms_)
fms_->setInState(state);
}
inline void FedRawDataInputSource::setMonStateSup(evf::FastMonState::InputState state) {
if (fms_)
fms_->setInStateSup(state);
}
inline bool InputFile::advance(std::mutex& m,
std::condition_variable& cv,
unsigned char*& dataPosition,
const size_t size) {
parent_->setMonState(inWaitChunk);
//wait for chunk
while (!waitForChunk(currentChunk_)) {
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, std::chrono::milliseconds(100));
if (parent_->exceptionState())
parent_->threadError();
}
parent_->setMonState(inChunkReceived);
dataPosition = chunks_[currentChunk_]->buf_ + chunkPosition_;
size_t currentLeft = chunks_[currentChunk_]->size_ - chunkPosition_;
if (currentLeft < size) {
//we need next chunk
assert(chunks_.size() > currentChunk_ + 1);
parent_->setMonState(inWaitChunk);
while (!waitForChunk(currentChunk_ + 1)) {
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, std::chrono::milliseconds(100));
if (parent_->exceptionState())
parent_->threadError();
}
parent_->setMonState(inChunkReceived);
//copy everything to beginning of the first chunk
dataPosition -= chunkPosition_;
assert(dataPosition == chunks_[currentChunk_]->buf_);
memmove(chunks_[currentChunk_]->buf_, chunks_[currentChunk_]->buf_ + chunkPosition_, currentLeft);
memcpy(chunks_[currentChunk_]->buf_ + currentLeft, chunks_[currentChunk_ + 1]->buf_, size - currentLeft);
//set pointers at the end of the old data position
bufferPosition_ += size;
chunkPosition_ = size - currentLeft;
currentChunk_++;
return true;
} else {
chunkPosition_ += size;
bufferPosition_ += size;
return false;
}
}
void InputFile::moveToPreviousChunk(const size_t size, const size_t offset) {
//this will fail in case of events that are too large
assert(size < chunks_[currentChunk_]->size_ - chunkPosition_);
assert(size - offset < chunks_[currentChunk_]->size_);
memcpy(chunks_[currentChunk_ - 1]->buf_ + offset, chunks_[currentChunk_]->buf_ + chunkPosition_, size);
chunkPosition_ += size;
bufferPosition_ += size;
}
void InputFile::rewindChunk(const size_t size) {
chunkPosition_ -= size;
bufferPosition_ -= size;
}
InputFile::~InputFile() {
if (rawFd_ != -1)
close(rawFd_);
if (deleteFile_) {
for (auto& fileName : fileNames_) {
if (!fileName.empty()) {
const std::filesystem::path filePath(fileName);
try {
//sometimes this fails but file gets deleted
LogDebug("FedRawDataInputSource:InputFile") << "Deleting input file -:" << fileName;
std::filesystem::remove(filePath);
continue;
} catch (const std::filesystem::filesystem_error& ex) {
edm::LogError("FedRawDataInputSource:InputFile")
<< " - deleteFile BOOST FILESYSTEM ERROR CAUGHT -: " << ex.what() << ". Trying again.";
} catch (std::exception& ex) {
edm::LogError("FedRawDataInputSource:InputFile")
<< " - deleteFile std::exception CAUGHT -: " << ex.what() << ". Trying again.";
}
std::filesystem::remove(filePath);
}
}
}
}
//single-buffer mode file reading
void FedRawDataInputSource::readNextChunkIntoBuffer(InputFile* file) {
uint32_t existingSize = 0;
if (fileDescriptor_ < 0) {
bufferInputRead_ = 0;
if (file->rawFd_ == -1) {
fileDescriptor_ = open(file->fileName_.c_str(), O_RDONLY);
if (file->rawHeaderSize_)
lseek(fileDescriptor_, file->rawHeaderSize_, SEEK_SET);
} else
fileDescriptor_ = file->rawFd_;
//skip header size in destination buffer (chunk position was already adjusted)
bufferInputRead_ += file->rawHeaderSize_;
existingSize += file->rawHeaderSize_;
if (fileDescriptor_ >= 0)
LogDebug("FedRawDataInputSource") << "opened file -: " << std::endl << file->fileName_;
else {
throw cms::Exception("FedRawDataInputSource:readNextChunkIntoBuffer")
<< "failed to open file " << std::endl
<< file->fileName_ << " fd:" << fileDescriptor_;
}
//fill chunk (skipping file header if present)
for (unsigned int i = 0; i < readBlocks_; i++) {
const ssize_t last = ::read(fileDescriptor_,
(void*)(file->chunks_[0]->buf_ + existingSize),
eventChunkBlock_ - (i == readBlocks_ - 1 ? existingSize : 0));
bufferInputRead_ += last;
existingSize += last;
}
} else {
//continue reading
if (file->chunkPosition_ == 0) { //in the rare case the last byte barely fit
for (unsigned int i = 0; i < readBlocks_; i++) {
const ssize_t last = ::read(fileDescriptor_, (void*)(file->chunks_[0]->buf_ + existingSize), eventChunkBlock_);
bufferInputRead_ += last;
existingSize += last;
}
} else {
//event didn't fit in last chunk, so leftover must be moved to the beginning and completed
uint32_t existingSizeLeft = eventChunkSize_ - file->chunkPosition_;
memmove((void*)file->chunks_[0]->buf_, file->chunks_[0]->buf_ + file->chunkPosition_, existingSizeLeft);
//calculate amount of data that can be added
const uint32_t blockcount = file->chunkPosition_ / eventChunkBlock_;
const uint32_t leftsize = file->chunkPosition_ % eventChunkBlock_;
for (uint32_t i = 0; i < blockcount; i++) {
const ssize_t last =
::read(fileDescriptor_, (void*)(file->chunks_[0]->buf_ + existingSizeLeft), eventChunkBlock_);
bufferInputRead_ += last;
existingSizeLeft += last;
}
if (leftsize) {
const ssize_t last = ::read(fileDescriptor_, (void*)(file->chunks_[0]->buf_ + existingSizeLeft), leftsize);
bufferInputRead_ += last;
}
file->chunkPosition_ = 0; //data was moved to beginning of the chunk
}
}
if (bufferInputRead_ == file->fileSize_) { // no more data in this file
if (fileDescriptor_ != -1) {
LogDebug("FedRawDataInputSource") << "Closing input file -: " << std::endl << file->fileName_;
close(fileDescriptor_);
file->rawFd_ = fileDescriptor_ = -1;
}
}
}
void FedRawDataInputSource::reportEventsThisLumiInSource(unsigned int lumi, unsigned int events) {
std::lock_guard<std::mutex> lock(monlock_);
auto itr = sourceEventsReport_.find(lumi);
if (itr != sourceEventsReport_.end())
itr->second += events;
else
sourceEventsReport_[lumi] = events;
}
std::pair<bool, unsigned int> FedRawDataInputSource::getEventReport(unsigned int lumi, bool erase) {
std::lock_guard<std::mutex> lock(monlock_);
auto itr = sourceEventsReport_.find(lumi);
if (itr != sourceEventsReport_.end()) {
std::pair<bool, unsigned int> ret(true, itr->second);
if (erase)
sourceEventsReport_.erase(itr);
return ret;
} else
return std::pair<bool, unsigned int>(false, 0);
}
long FedRawDataInputSource::initFileList() {
std::sort(fileNames_.begin(), fileNames_.end(), [](std::string a, std::string b) {
if (a.rfind('/') != std::string::npos)
a = a.substr(a.rfind('/'));
if (b.rfind('/') != std::string::npos)
b = b.substr(b.rfind('/'));
return b > a;
});
if (!fileNames_.empty()) {
//get run number from first file in the vector
std::filesystem::path fileName = fileNames_[0];
std::string fileStem = fileName.stem().string();
if (fileStem.find("file://") == 0)
fileStem = fileStem.substr(7);
else if (fileStem.find("file:") == 0)
fileStem = fileStem.substr(5);
auto end = fileStem.find('_');
if (fileStem.find("run") == 0) {
std::string runStr = fileStem.substr(3, end - 3);
try {
//get long to support test run numbers < 2^32
long rval = std::stol(runStr);
edm::LogInfo("FedRawDataInputSource") << "Autodetected run number in fileListMode -: " << rval;
return rval;
} catch (const std::exception&) {
edm::LogWarning("FedRawDataInputSource")
<< "Unable to autodetect run number in fileListMode from file -: " << fileName;
}
}
}
return -1;
}
evf::EvFDaqDirector::FileStatus FedRawDataInputSource::getFile(unsigned int& ls,
std::string& nextFile,
uint32_t& fsize,
uint64_t& lockWaitTime) {
if (fileListIndex_ < fileNames_.size()) {
nextFile = fileNames_[fileListIndex_];
if (nextFile.find("file://") == 0)
nextFile = nextFile.substr(7);
else if (nextFile.find("file:") == 0)
nextFile = nextFile.substr(5);
std::filesystem::path fileName = nextFile;
std::string fileStem = fileName.stem().string();
if (fileStem.find("ls"))
fileStem = fileStem.substr(fileStem.find("ls") + 2);
if (fileStem.find('_'))
fileStem = fileStem.substr(0, fileStem.find('_'));
if (!fileListLoopMode_)
ls = std::stoul(fileStem);
else //always starting from LS 1 in loop mode
ls = 1 + loopModeIterationInc_;
//fsize = 0;
//lockWaitTime = 0;
fileListIndex_++;
return evf::EvFDaqDirector::newFile;
} else {
if (!fileListLoopMode_)
return evf::EvFDaqDirector::runEnded;
else {
//loop through files until interrupted
loopModeIterationInc_++;
fileListIndex_ = 0;
return getFile(ls, nextFile, fsize, lockWaitTime);
}
}
}
|