Line data Source code
1 : #include <unistd.h> // usleep
2 : #include <chrono>
3 : #include <iostream>
4 :
5 : #include "dtcInterfaceLib/DTC.h"
6 : #include "dtcInterfaceLib/DTCSoftwareCFO.h"
7 :
8 : #include "TRACE/tracemf.h"
9 :
10 : #include "fragmentTester.h"
11 :
12 : using namespace DTCLib;
13 :
14 0 : void usage()
15 : {
16 0 : std::cout << "Usage: tester [loops = 1000] [simMode = 1] [simFile = \"\"]" << std::endl;
17 0 : exit(1);
18 : }
19 :
20 0 : int main(int argc, char* argv[])
21 : {
22 0 : auto loops = 1000;
23 0 : auto modeint = 1;
24 0 : std::string simFile = "";
25 0 : auto badarg = false;
26 0 : if (argc > 1)
27 : {
28 0 : auto tmp = atoi(argv[1]);
29 0 : if (tmp > 0)
30 0 : loops = tmp;
31 : else
32 0 : badarg = true;
33 : }
34 0 : if (argc > 2)
35 : {
36 0 : auto tmp = atoi(argv[2]);
37 0 : if (tmp > 0)
38 0 : modeint = tmp;
39 : else
40 0 : badarg = true;
41 : }
42 0 : if (argc > 3)
43 : {
44 0 : simFile = std::string(argv[3]);
45 : }
46 0 : if (argc > 4) badarg = true;
47 0 : if (badarg) usage(); // Exits.
48 :
49 0 : TRACE(1, "simFile is %s", simFile.c_str());
50 0 : auto mode = DTC_SimModeConverter::ConvertToSimMode(std::to_string(modeint));
51 0 : auto thisDTC = new DTC(mode, 0);
52 0 : if (simFile.size() > 0)
53 : {
54 0 : thisDTC->WriteSimFileToDTC(simFile, true, true);
55 : }
56 0 : TRACE(1, "thisDTC->GetSimMode: %i", thisDTC->GetSimMode());
57 0 : auto theCFO = new DTCSoftwareCFO(thisDTC, true);
58 0 : long loopCounter = 0;
59 0 : long count = 0;
60 : typedef uint8_t packet_t[16];
61 :
62 0 : while (loopCounter < loops)
63 : {
64 0 : TRACE(1, "mu2eReceiver::getNext: Starting CFO thread");
65 0 : uint64_t z = 0;
66 0 : DTC_EventWindowTag zero(z);
67 0 : TRACE(1, "Sending requests for %i timestamps, starting at %lu", BLOCK_COUNT_MAX, BLOCK_COUNT_MAX * loopCounter);
68 0 : theCFO->SendRequestsForRange(BLOCK_COUNT_MAX, DTC_EventWindowTag(BLOCK_COUNT_MAX * loopCounter));
69 :
70 0 : fragmentTester newfrag(BLOCK_COUNT_MAX * sizeof(packet_t) * 2);
71 :
72 0 : DTC_EventWindowTag expected_timestamp;
73 0 : auto firstLoop = true;
74 :
75 : // Get data from DTCReceiver
76 0 : TRACE(1, "mu2eReceiver::getNext: Starting DTCFragment Loop");
77 0 : while (newfrag.hdr_block_count() < BLOCK_COUNT_MAX)
78 : {
79 0 : TRACE(1, "Getting DTC Data");
80 0 : std::vector<std::unique_ptr<DTC_Event>> data;
81 0 : auto retryCount = 5;
82 0 : while (data.size() == 0 && retryCount >= 0)
83 : {
84 : try
85 : {
86 : // TRACE(4, "Calling theInterface->GetData(zero)");
87 0 : data = thisDTC->GetData(zero);
88 : // TRACE(4, "Done calling theInterface->GetData(zero)");
89 : }
90 0 : catch (std::exception const& ex)
91 : {
92 0 : std::cerr << ex.what() << std::endl;
93 0 : }
94 0 : retryCount--;
95 : // if (data.size() == 0) { usleep(10000); }
96 : }
97 0 : if (retryCount < 0 && data.size() == 0)
98 : {
99 0 : TRACE(1, "Retry count exceeded. Something is very wrong indeed");
100 0 : std::cout << "Had an error with block " << newfrag.hdr_block_count() << " of event " << loopCounter
101 0 : << std::endl;
102 0 : break;
103 : }
104 :
105 0 : auto ts = data[0]->GetEventWindowTag();
106 0 : if (firstLoop)
107 : {
108 0 : expected_timestamp = ts;
109 0 : firstLoop = false;
110 : }
111 0 : if (ts != expected_timestamp)
112 : {
113 0 : std::cerr << "WRONG TIMESTAMP DETECTED: 0x" << std::hex << ts.GetEventWindowTag(true) << " (expected: 0x"
114 0 : << expected_timestamp.GetEventWindowTag(true) << ")" << std::endl;
115 : }
116 0 : expected_timestamp = ts + 1;
117 : // int packetCount = first.GetPacketCount() + 1;
118 : // TRACE(1, "There are %lu data blocks in timestamp %lu. Packet count of first data block: %i", data.size(),
119 : // ts.GetEventWindowTag(true), packetCount);
120 :
121 0 : size_t totalSize = 0;
122 :
123 0 : for (size_t i = 0; i < data.size(); ++i)
124 : {
125 0 : totalSize += data[i]->GetEventByteCount();
126 : }
127 :
128 0 : auto diff = static_cast<int64_t>(totalSize + newfrag.dataSize()) - newfrag.fragSize();
129 0 : TRACE(4, "diff=%lli, totalSize=%zu, dataSize=%zu, fragSize=%zu", (long long)diff, totalSize, newfrag.dataSize(),
130 : newfrag.fragSize());
131 0 : if (diff > 0)
132 : {
133 0 : auto currSize = newfrag.fragSize();
134 0 : auto remaining = 1 - newfrag.hdr_block_count() / static_cast<double>(BLOCK_COUNT_MAX);
135 0 : auto newSize = static_cast<size_t>(currSize * remaining);
136 0 : TRACE(1, "mu2eReceiver::getNext: %zu + %zu > %zu, allocating space for %zu more bytes", totalSize,
137 : newfrag.dataSize(), newfrag.fragSize(), newSize + diff);
138 0 : newfrag.addSpace(static_cast<size_t>(diff + newSize));
139 : }
140 :
141 0 : TRACE(3, "Copying DTC packets into Mu2eFragment");
142 0 : auto offset = newfrag.dataBegin() + newfrag.dataSize();
143 0 : size_t intraBlockOffset = 0;
144 0 : for (size_t i = 0; i < data.size(); ++i)
145 : {
146 0 : TRACE(4, "Copying data from %p to %p (sz=%zu)", data[i]->GetRawBufferPointer(),
147 : reinterpret_cast<void*>(offset + intraBlockOffset), data[i]->GetEventByteCount());
148 0 : memcpy(reinterpret_cast<void*>(offset + intraBlockOffset), data[i]->GetRawBufferPointer(), data[i]->GetEventByteCount());
149 0 : intraBlockOffset += data[i]->GetEventByteCount();
150 : }
151 :
152 0 : TRACE(3, "Ending SubEvt %zu", newfrag.hdr_block_count());
153 0 : newfrag.endSubEvt(intraBlockOffset);
154 0 : }
155 :
156 0 : loopCounter++;
157 0 : count += newfrag.hdr_block_count();
158 : // auto file = fopen((std::string("tester_") + std::to_string(loopCounter) + ".bin").c_str(),"w+");
159 : // fwrite((void*)newfrag.dataBegin(), sizeof(uint8_t), newfrag.fragSize(), file);
160 0 : std::cout << "Event: " << loopCounter << ": " << newfrag.hdr_block_count() << " timestamps. (" << count << " total)"
161 0 : << std::endl;
162 0 : }
163 :
164 0 : delete theCFO;
165 0 : delete thisDTC;
166 :
167 0 : return 0;
168 0 : }
|