-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamper.cpp
76 lines (66 loc) · 2.38 KB
/
timestamper.cpp
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
#include <time.h>
#include "timestamper.hh"
namespace timestamper = t::plugins::timestamper;
timestamper::cfg_t::cfg_t(const std::string & clock_source_name)
{
#define checkassignclocksource(whichclock) \
if (clock_source_name == #whichclock) \
clock_source = whichclock;
checkassignclocksource(CLOCK_REALTIME);
checkassignclocksource(CLOCK_REALTIME_COARSE);
checkassignclocksource(CLOCK_MONOTONIC);
checkassignclocksource(CLOCK_MONOTONIC_COARSE);
checkassignclocksource(CLOCK_MONOTONIC_RAW);
checkassignclocksource(CLOCK_BOOTTIME);
checkassignclocksource(CLOCK_PROCESS_CPUTIME_ID);
checkassignclocksource(CLOCK_THREAD_CPUTIME_ID);
}
double timestamper::cfg_t::process()
{
struct timespec timespec = {.tv_sec = 0, .tv_nsec = 0};
if (clock_gettime(clock_source, ×pec) == 0)
return timespec.tv_sec + timespec.tv_nsec * 1e-9;
return std::numeric_limits<double>::quiet_NaN();
}
timestamper::if_t::if_t(algo_comm_t & algo_comm,
const std::string & configured_name)
: MHAPlugin::plugin_t<cfg_t>("Gets current time in seconds during each"
" process callback and publishes the"
" result as AC variable " + configured_name,
algo_comm)
, time(algo_comm,configured_name, std::numeric_limits<double>::quiet_NaN())
{
insert_member(clock_source);
patchbay.connect(&clock_source.writeaccess, this, &if_t::update);
}
void timestamper::if_t::prepare(mhaconfig_t&)
{
time.data = std::numeric_limits<double>::quiet_NaN();
update();
}
void timestamper::if_t::update()
{
if (is_prepared())
push_config(new cfg_t(clock_source.data.get_value()));
}
template<class mha_signal_t>
mha_signal_t* timestamper::if_t::process(mha_signal_t* s)
{
time.data = poll_config()->process();
return s;
}
MHAPLUGIN_CALLBACKS(timestamper,timestamper::if_t,wave,wave)
MHAPLUGIN_PROC_CALLBACK(timestamper,timestamper::if_t,spec,spec)
MHAPLUGIN_DOCUMENTATION\
(timestamper,
"acvariables time",
"Publishes time of invocation of the process() function of this plugin"
" in AC space as a double-precision floating point value in seconds."
" Interpretation of the value depends on the clock_source used."
)
// Local variables:
// compile-command: "make"
// c-basic-offset: 4
// indent-tabs-mode: nil
// coding: utf-8-unix
// End: