Re-implemented module ID# assignment system.
[aesalon.git] / modules / cpuTime / src / collector / cpuTime.c
blob0d012313443e1e1a2d0692403e07bf6413fd9d20
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 *unused);
11 static void *run(void *unused) {
12 ModuleID moduleID = AI_ConfigurationLong("cpuTime:moduleID");
13 printf("moduleID: %i\n", moduleID);
14 printf("*****\n");
15 sleep(2);
16 int64_t interval_us = AI_ConfigurationLong("cpuTime:interval");
17 /* If the interval is not set, use the default of 1 ms. */
18 if(interval_us == -1) interval_us = 1000;
20 printf("interval_us: %li\n", interval_us);
22 struct timespec interval;
23 interval.tv_sec = interval_us / 1000000;
24 interval.tv_nsec = (interval_us % 1000000)*1000;
25 /* Use nanosleep() to sleep for the given period.
26 NOTE: this may cause issues with systems that overuse signals . . .
28 while(1) {
29 struct timespec value;
30 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &value);
31 uint64_t timestamp = AI_Timestamp();
33 AI_StartPacket(1);
34 *(uint64_t *)AI_PacketSpace(8) = timestamp;
35 *(uint64_t *)AI_PacketSpace(8) = value.tv_nsec + (value.tv_sec * 1000000000);
36 AI_EndPacket();
38 struct timespec remaining;
39 int ret = nanosleep(&interval, &remaining);
40 while(ret == -1 && errno == EINTR) {
41 printf("Interrupted by signal!\n");
42 ret = nanosleep(&remaining, &remaining);
46 return NULL;
49 void __attribute__((constructor)) AC_EXPORT AM_Construct() {
50 AI_Construct();
52 pthread_create(&AM_threadID, NULL, run, NULL);
55 void __attribute__((destructor)) AC_EXPORT AM_Destruct() {