Implemented enough of Visualizer::*Input* to send packets to Artisans.
[aesalon.git] / modules / cpuTime / src / collector / cpuTime.c
blobef1a356ea09853b9cf6c0a617b82720649755c59
1 #include <stdio.h>
2 #include <time.h>
3 #include <errno.h>
5 #include "informer/Informer.h"
7 static pthread_t AM_threadID;
9 static void *run(void *param);
11 static void *run(void *param) {
12 ModuleID moduleID = (ModuleID)(uint64_t) param;
13 int64_t interval_us = AI_ConfigurationLong("cpuTime:interval");
14 /* If the interval is not set, use the default of 1 ms. */
15 if(interval_us == -1) interval_us = 1000;
17 printf("interval_us: %li\n", interval_us);
19 struct timespec interval;
20 interval.tv_sec = interval_us / 1000000;
21 interval.tv_nsec = (interval_us % 1000000)*1000;
22 /* Use nanosleep() to sleep for the given period.
23 NOTE: this may cause issues with systems that overuse signals . . .
25 while(1) {
26 struct timespec value;
27 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &value);
28 uint64_t timestamp = AI_Timestamp();
30 AI_StartPacket(moduleID);
31 *(uint64_t *)AI_PacketSpace(8) = timestamp;
32 *(uint64_t *)AI_PacketSpace(8) = value.tv_nsec + (value.tv_sec * 1000000000);
33 AI_EndPacket();
35 struct timespec remaining;
36 int ret = nanosleep(&interval, &remaining);
37 while(ret == -1 && errno == EINTR) {
38 printf("Interrupted by signal!\n");
39 ret = nanosleep(&remaining, &remaining);
43 return NULL;
46 void __attribute__((constructor)) AC_EXPORT AM_Construct() {
47 AI_Construct();
49 ModuleID moduleID = AI_ConfigurationLong("cpuTime:moduleID");
51 pthread_create(&AM_threadID, NULL, run, (void *)(uint64_t)moduleID);
53 AI_ModuleLoaded("cpuTime", moduleID);
56 void __attribute__((destructor)) AC_EXPORT AM_Destruct() {