6#include "dawn/prog/sampling.hxx"
12#include "dawn/debug.hxx"
13#include "dawn/io/common.hxx"
14#include "dawn/io/ddata.hxx"
18int CProgSampling::configureDesc(
const CDescObject &desc)
20 const SObjectCfg::SObjectCfgItem *item =
nullptr;
21 const SObjectId::UObjectId *ids;
28 for (
size_t i = 0; i < desc.
getSize(); i++)
34 DAWNERR(
"Unsupported sampling config 0x%08" PRIx32
"\n", item->
cfgid.
v);
44 DAWNERR(
"Invalid IOBIND size %d, must be even\n", item->
cfgid.
s.
size);
49 ids =
reinterpret_cast<const SObjectId::UObjectId *
>(item->
data);
51 for (j = 0; j < nbind; j++)
53 ret = allocObject(ids[j * 2].v, ids[j * 2 + 1].v);
67 DAWNERR(
"Invalid INTERVAL size %d\n", item->
cfgid.
s.
size);
71 data =
reinterpret_cast<const uint32_t *
>(item->
data);
80 DAWNERR(
"Invalid PRIORITY size %d\n", item->
cfgid.
s.
size);
84 priority =
static_cast<int>(item->
data[0]);
92 DAWNERR(
"Invalid FLAGS item\n");
96 flags = item->
data[0];
102 DAWNERR(
"Unsupported sampling config 0x%08" PRIx32
"\n", item->
cfgid.
v);
115 DAWNINFO(
"allocate sampling 0x%" PRIx32
" -> 0x%" PRIx32
"\n", srcId, targetId);
117 b =
new (std::nothrow) SSamplingBind();
124 b->targetId = targetId;
143 if (data ==
nullptr || len != 1)
154static void deadlineAdd(
struct timespec &ts, uint32_t us)
156 ts.tv_nsec +=
static_cast<long>(us % 1000000) * 1000L;
157 ts.tv_sec += us / 1000000 + ts.tv_nsec / 1000000000L;
158 ts.tv_nsec %= 1000000000L;
161void CProgSampling::thread()
164 struct timespec next;
171 DAWNINFO(
"start sampling thread\n");
190 if (period != 0 && !armed)
192 clock_gettime(CLOCK_MONOTONIC, &next);
198 for (i = 0; i < binds.size(); i++)
202 ret = b->src->getData(*b->iodata, 1);
205 DAWNERR(
"getData failed for src %zu (error %d)\n", i, ret);
209 ret = b->target->setData(*b->iodata);
212 DAWNERR(
"setData failed for target %zu (error %d)\n", i, ret);
229 deadlineAdd(next, period);
230 clock_gettime(CLOCK_MONOTONIC, &now);
231 if (now.tv_sec > next.tv_sec + 1 ||
232 (now.tv_sec == next.tv_sec + 1 && now.tv_nsec > next.tv_nsec))
237 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next,
nullptr);
239 while (!threadCtl.shouldQuit());
242CProgSampling::~CProgSampling()
245 sem_destroy(&wakeup);
254 ret = configureDesc(
getDesc());
257 DAWNERR(
"Sampling configure failed (error %d)\n", ret);
272 for (i = 0; i < binds.size(); i++)
274 delete binds[i]->iodata;
291 for (i = 0; i < binds.size(); i++)
295 b->src =
getIO(b->srcId);
298 DAWNERR(
"Source IO 0x%" PRIx32
" not found\n", b->srcId);
304 DAWNERR(
"Source IO 0x%" PRIx32
" is not readable\n", b->srcId);
308 b->target =
getIO(b->targetId);
311 DAWNERR(
"Target IO 0x%" PRIx32
" not found\n", b->targetId);
317 DAWNERR(
"Target IO 0x%" PRIx32
" dtype mismatch\n", b->targetId);
325#ifdef CONFIG_DAWN_IO_NOTIFY
333 DAWNERR(
"Failed to initialize target %zu (error %d)\n", i, ret);
340 DAWNERR(
"Target IO 0x%" PRIx32
" shape mismatch\n", b->targetId);
349 DAWNERR(
"Failed to allocate data buffer for src %zu\n", i);
361 while (sem_trywait(&wakeup) == 0)
370 threadCtl.setThreadPriority(priority);
371 threadCtl.setThreadFunc([
this]() { thread(); });
372 return threadCtl.threadStart();
381 return threadCtl.threadStop();
404 return threadCtl.isRunning();
CIOCommon * getIO(SObjectId::ObjectId id)
Get an I/O object by ID.
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemNext(size_t &offset) const
Get config item at current offset and advance past it.
virtual size_t getDataDim() const =0
Get data vector dimension.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
virtual size_t getDataSize() const =0
Get data size in bytes.
virtual bool isRead() const =0
Check if IO supports read operations.
@ CMD_TRIGGER2
Object-specific trigger slot 2.
@ CMD_TRIGGER1
Object-specific trigger slot 1.
CDescObject & getDesc()
Get descriptor object for this object.
uint8_t getDtype() const
Get data type field.
int prepareWritableTarget(CIOCommon *io, size_t dim, bool notify, size_t batch=1)
Check a writable output; shape a deferred virt output or check an already shaped one.
@ PROG_CLASS_SAMPLING
Periodic data sampler.
int trigger(uint8_t cmd)
Execute a trigger command.
int doStart()
Start implementation hook.
bool hasThread() const
Check if a background thread is active.
@ FLAG_AUTO_START
Sample from start, without a trigger.
int configure()
Configure object from descriptor data.
int onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Pre-update hook for runtime configuration writes.
int init()
One-time initialize object after bindings are resolved.
int doStop()
Stop implementation hook.
int deinit()
De-initialize object.
@ PROG_SAMPLING_CFG_INTERVAL
Sampling interval (microseconds).
@ PROG_SAMPLING_CFG_IOBIND
I/O binding configuration.
@ PROG_SAMPLING_CFG_PRIORITY
Sampling thread priority (0 = default).
@ PROG_SAMPLING_CFG_FLAGS
Behavior flags.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
static uint8_t objectCfgGetId(const ObjectCfgId objcfg)
Extract configuration identifier from ConfigID.
Out-of-tree user-extension hooks for Dawn.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
uint32_t ObjectId
ObjectID type - single 32-bit value.
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
struct dawn::SObjectCfg::UObjectCfgId::@334307326077356333363326153167141302007100111136 s
Bit-field structure for named member access.
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).