DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
toolbox.cpp
Go to the documentation of this file.
1
9#include "timing/toolbox.hpp"
10
11// PDT Headers
13
14// uHAL Headers
15#include "uhal/ValMem.hpp"
16
17// Boost Headers
18#include "boost/foreach.hpp"
19#include <boost/filesystem/operations.hpp>
20#include <boost/filesystem/path.hpp>
21
22// C++ Headers
23#include <cstdarg>
24#include <cstdint>
25#include <cstdio>
26#include <cstdlib>
27#include <ctime>
28#include <map>
29#include <stdexcept>
30#include <string>
31#include <utility>
32#include <vector>
33#include <wordexp.h>
34
35using namespace std;
36
37namespace dunedaq {
38namespace timing {
39
40//-----------------------------------------------------------------------------
42snapshot(const uhal::Node& node)
43{
45 std::map<string, uhal::ValWord<uint32_t>> value_words; // NOLINT(build/unsigned)
46
47 for (string n : node.getNodes()) {
48 value_words.insert(make_pair(n, node.getNode(n).read()));
49 }
50 node.getClient().dispatch();
51
52 Snapshot vals;
53 std::map<string, uhal::ValWord<uint32_t>>::iterator it; // NOLINT(build/unsigned)
54 for (it = value_words.begin(); it != value_words.end(); ++it)
55 vals.insert(make_pair(it->first, it->second.value()));
56
57 return vals;
58}
59//-----------------------------------------------------------------------------
60
61//-----------------------------------------------------------------------------
63snapshot(const uhal::Node& node, const std::string& regex)
64{
65 std::map<string, uhal::ValWord<uint32_t>> value_words; // NOLINT(build/unsigned)
66
67 for (string n : node.getNodes(regex)) {
68 value_words.insert(make_pair(n, node.getNode(n).read()));
69 }
70 node.getClient().dispatch();
71
72 Snapshot vals;
73 std::map<string, uhal::ValWord<uint32_t>>::iterator it; // NOLINT(build/unsigned)
74 for (it = value_words.begin(); it != value_words.end(); ++it)
75 vals.insert(make_pair(it->first, it->second.value()));
76
77 return vals;
78}
79//-----------------------------------------------------------------------------
80
81//-----------------------------------------------------------------------------
82void
83millisleep(const double& time_in_milliseconds)
84{
85 // using namespace uhal;
86 // logging();
87 double time_in_seconds(time_in_milliseconds / 1e3);
88 int integer_part(static_cast<int>(time_in_seconds));
89 double fractional_part(time_in_seconds - static_cast<double>(integer_part));
90 struct timespec sleep_time, return_time;
91 sleep_time.tv_sec = integer_part;
92 sleep_time.tv_nsec = static_cast<long>(fractional_part * 1e9); // NOLINT
93 nanosleep(&sleep_time, &return_time);
94}
95//-----------------------------------------------------------------------------
96
97//-----------------------------------------------------------------------------
98std::string
99strprintf(const char* fmt, ...) // NOLINT
100{
101 char* ret;
102 va_list ap;
103 va_start(ap, fmt);
104 vasprintf(&ret, fmt, ap); // NOLINT
105 va_end(ap);
106 std::string str(ret);
107 free(ret);
108 return str;
109}
110//-----------------------------------------------------------------------------
111
112//-----------------------------------------------------------------------------
113std::vector<std::string>
114shell_expand_paths(const std::string& path)
115{
116
117 std::vector<std::string> paths;
118 wordexp_t substituted_path;
119 int code = wordexp(path.c_str(), &substituted_path, WRDE_NOCMD);
120 if (code)
121 throw runtime_error("Failed expanding path: " + path);
122
123 for (std::size_t i = 0; i != substituted_path.we_wordc; i++)
124 paths.push_back(substituted_path.we_wordv[i]);
125
126 wordfree(&substituted_path);
127
128 return paths;
129}
130//-----------------------------------------------------------------------------
131
132//-----------------------------------------------------------------------------
133std::string
134shellExpandPath(const std::string& path)
135{
136 std::vector<std::string> paths = shell_expand_paths(path);
137
138 if (paths.size() > 1)
139 throw runtime_error("Failed to expand: multiple matches found");
140
141 return paths[0];
142}
143//-----------------------------------------------------------------------------
144
145//-----------------------------------------------------------------------------
146void
147throw_if_not_file(const std::string& path)
148{
149
150 // FIXME: Review the implementation. The function never returns
151 namespace fs = boost::filesystem;
152
153 // Check that the path exists and that it's not a directory
154 fs::path cfgFile(path);
155 if (!fs::exists(cfgFile)) {
156 throw FileNotFound(ERS_HERE, path);
157 } else if (fs::is_directory(cfgFile)) {
158 throw FileIsDirectory(ERS_HERE, path);
159 }
160
161 // return true;
162}
163//-----------------------------------------------------------------------------
164
165//-----------------------------------------------------------------------------
166uint8_t // NOLINT(build/unsigned)
167dec_rng(uint8_t word, uint8_t ibit, uint8_t nbits) // NOLINT(build/unsigned)
168{
169 return (word >> ibit) & ((1 << nbits) - 1);
170}
171//-----------------------------------------------------------------------------
172
173//-----------------------------------------------------------------------------
174uint64_t // NOLINT(build/unsigned)
175tstamp2int(uhal::ValVector<uint32_t> raw_timestamp) // NOLINT(build/unsigned)
176{
177 return (uint64_t)raw_timestamp[0] + ((uint64_t)raw_timestamp[1] << 32); // NOLINT(build/unsigned)
178}
179//-----------------------------------------------------------------------------
180
181//-----------------------------------------------------------------------------
182int64_t
184{
185 // get the current time
186 const auto now = std::chrono::system_clock::now();
187
188 // transform the time into a duration since the epoch
189 const auto epoch = now.time_since_epoch();
190
191 // cast the duration into seconds
192 const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);
193
194 // return the number of seconds
195 return seconds.count();
196}
197//-----------------------------------------------------------------------------
198
199//-----------------------------------------------------------------------------
200int64_t
202{
203 // get the current time
204 const auto now = std::chrono::system_clock::now();
205
206 // transform the time into a duration since the epoch
207 const auto epoch = now.time_since_epoch();
208
209 // cast the duration into milliseconds
210 const auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
211
212 // return the number of milliseconds
213 return milliseconds.count();
214}
215//-----------------------------------------------------------------------------
216
217//-----------------------------------------------------------------------------
218std::string
219format_timestamp(uint64_t raw_timestamp, uint32_t clock_frequency_hz) // NOLINT(build/unsigned)
220{
221 std::time_t sec_from_epoch = raw_timestamp / clock_frequency_hz;
222
223 struct tm* time = localtime(&sec_from_epoch); // NOLINT
224 char time_buffer[32];
225
226 strftime(time_buffer, sizeof time_buffer, "%a, %d %b %Y %H:%M:%S +0000", time);
227 return time_buffer;
228}
229//-----------------------------------------------------------------------------
230
231//-----------------------------------------------------------------------------
232std::string
233format_timestamp(uhal::ValVector<uint32_t> raw_timestamp, uint32_t clock_frequency_hz) // NOLINT(build/unsigned)
234{
235 uint64_t timestamp = tstamp2int(raw_timestamp); // NOLINT(build/unsigned)
236 return format_timestamp(timestamp, clock_frequency_hz);
237}
238//-----------------------------------------------------------------------------
239
240//-----------------------------------------------------------------------------
241double
242convert_bits_to_float(uint64_t bits, bool is_double_precision) // NOLINT(build/unsigned)
243{
244 uint32_t mantissa_shift = is_double_precision ? 52 : 23; // NOLINT(build/unsigned)
245 uint64_t exponent_mask = is_double_precision ? 0x7FF0000000000000 : 0x7f800000; // NOLINT(build/unsigned)
246 uint32_t bias = is_double_precision ? 1023 : 127; // NOLINT(build/unsigned)
247 uint32_t sign_shift = is_double_precision ? 63 : 31; // NOLINT(build/unsigned)
248
249 int32_t sign = (bits >> sign_shift) & 0x01;
250 uint32_t exponent_biased = ((bits & exponent_mask) >> mantissa_shift); // NOLINT(build/unsigned)
251 int32_t exponent = exponent_biased - bias;
252
253 int32_t power = -1;
254 double mantissa = 0.0;
255 for (uint32_t i = 0; i < mantissa_shift; ++i) { // NOLINT(build/unsigned)
256 uint64_t mantissa_bit = (bits >> (mantissa_shift - i - 1)) & 0x01; // NOLINT(build/unsigned)
257 mantissa += mantissa_bit * pow(2.0, power);
258 --power;
259 }
260
261 if (exponent_biased == 0) {
262 ++exponent;
263 if (mantissa == 0)
264 return 0;
265 } else {
266 mantissa += 1.0;
267 }
268 return (sign ? -1 : 1) * pow(2.0, exponent) * mantissa;
269}
270//-----------------------------------------------------------------------------
271
272//-----------------------------------------------------------------------------
274convert_value_to_board_type(uint32_t board_type) // NOLINT(build/unsigned)
275{
276 // not pleasnt, but works for now
277 if (board_type >= kBoardUnknown) {
278 throw UnknownBoardType(ERS_HERE, format_reg_value(board_type));
279 } else {
280 return static_cast<BoardType>(board_type);
281 }
282}
283//-----------------------------------------------------------------------------
284
285//-----------------------------------------------------------------------------
287convert_value_to_carrier_type(uint32_t carrier_type) // NOLINT(build/unsigned)
288{
289 // not pleasnt, but works for now
290 if (carrier_type >= kCarrierUnknown) {
291 throw UnknownCarrierType(ERS_HERE, format_reg_value(carrier_type));
292 } else {
293 return static_cast<CarrierType>(carrier_type);
294 }
295}
296//-----------------------------------------------------------------------------
297
298//-----------------------------------------------------------------------------
300convert_value_to_design_type(uint32_t design_type) // NOLINT(build/unsigned)
301{
302 // not pleasnt, but works for now
303 if (design_type >= kDesignUnknown) {
304 throw UnknownDesignType(ERS_HERE, format_reg_value(design_type));
305 } else {
306 return static_cast<DesignType>(design_type);
307 }
308}
309//-----------------------------------------------------------------------------
310
311template std::string
312timing::vec_fmt<uint32_t>(const std::vector<uint32_t>& vec); // NOLINT(build/unsigned)
313template std::string
314timing::short_vec_fmt<uint32_t>(const std::vector<uint32_t>& vec); // NOLINT(build/unsigned)
315
316//-----------------------------------------------------------------------------
317uint32_t // NOLINT(build/unsigned)
318locate(float xx[], unsigned long n, float x) // NOLINT
319{
320 uint32_t j, ju, jm, jl; // NOLINT(build/unsigned)
321 int ascnd;
322 jl = 0; // Initialize lower
323 ju = n + 1; // and upper limits.
324 ascnd = (xx[n] >= xx[1]); // NOLINT
325
326 while (ju - jl > 1) // If we are not yet done,
327 {
328 jm = (ju + jl) >> 1; // compute a midpoint,
329
330 if ((x >= xx[jm]) == ascnd) // added additional parenthesis
331 {
332 jl = jm; // and replace either the lower limit
333 } else {
334 ju = jm; // or the upper limit, as appropriate.
335 }
336 } // Repeat until the test condition is satisfied.
337
338 if (x == xx[1]) { // NOLINT
339 j = 1; // Then set the output
340 } else if (x == xx[n]) { // NOLINT
341 j = n - 1;
342 } else {
343 j = jl;
344 }
345
346 return j;
347}
348//-----------------------------------------------------------------------------
349
350//-----------------------------------------------------------------------------
351std::string
352format_firmware_version(uint32_t firmware_version) // NOLINT(build/unsigned)
353{
354 int major_firmware_version = (firmware_version >> 16) & 0xff;
355 int minor_firmware_version = (firmware_version >> 8) & 0xff;
356 int patch_firmware_version = (firmware_version >> 0) & 0xff;
357
358 std::stringstream firmware_version_stream;
359 firmware_version_stream << "v" << major_firmware_version << "." << minor_firmware_version << "." << patch_firmware_version;
360 return firmware_version_stream.str();
361}
362//-----------------------------------------------------------------------------
363
364} // namespace timing
365} // namespace dunedaq
#define ERS_HERE
static int64_t now()
int64_t get_seconds_since_epoch()
Definition toolbox.cpp:183
CarrierType convert_value_to_carrier_type(uint32_t darrier_type)
Definition toolbox.cpp:287
int64_t get_milliseconds_since_epoch()
Definition toolbox.cpp:201
std::string format_firmware_version(uint32_t firmware_version)
Definition toolbox.cpp:352
BoardType convert_value_to_board_type(uint32_t Board_type)
Definition toolbox.cpp:274
std::string vec_fmt(const std::vector< T > &vec)
Definition toolbox.hxx:382
std::string format_timestamp(uhal::ValVector< uint32_t > raw_timestamp, uint32_t clock_frequency_hz)
Definition toolbox.cpp:233
std::string strprintf(const char *fmt,...)
Definition toolbox.cpp:99
double convert_bits_to_float(uint64_t bits, bool is_double_precision=false)
Definition toolbox.cpp:242
std::vector< std::string > shell_expand_paths(const std::string &path)
Definition toolbox.cpp:114
DesignType convert_value_to_design_type(uint32_t design_type)
Definition toolbox.cpp:300
uint64_t tstamp2int(uhal::ValVector< uint32_t > raw_timestamp)
Definition toolbox.cpp:175
uint8_t dec_rng(uint8_t word, uint8_t ibit, uint8_t nbits=1)
Definition toolbox.cpp:167
uint32_t locate(float xx[], unsigned long n, float x)
Definition toolbox.cpp:318
std::map< std::string, uint32_t > Snapshot
std::string shellExpandPath(const std::string &path)
Definition toolbox.cpp:134
void throw_if_not_file(const std::string &path)
Definition toolbox.cpp:147
std::string short_vec_fmt(const std::vector< T > &vec)
Definition toolbox.hxx:399
std::string format_reg_value(T reg_value, uint32_t base)
Definition toolbox.hxx:117
void millisleep(const double &time_in_milliseconds)
Definition toolbox.cpp:83
Snapshot snapshot(const uhal::Node &node)
Walk & read the node structure.
Definition toolbox.cpp:42
Including Qt Headers.