summaryrefslogtreecommitdiff
path: root/trasmitter/clock.c
blob: 2d8bd617ba005f211229de20a147e616c1cc2138 (plain)
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
#include <stdio.h>
#include <time.h>
#include <sched.h>
 
void main(int argc,char **argv) {
  struct timespec begin_time,end_time,next_step,end_time2;
  struct sched_param sp;
 
  const int width=520000;
  const int pulses=30;
 
  sp.sched_priority=99;
 
  // to make this work, sudo setcap CAP_SYS_NICE=eip clock 
  sched_setscheduler(0,SCHED_RR,&sp);
 
  clock_gettime(CLOCK_REALTIME,&begin_time);
  clock_gettime(CLOCK_REALTIME,&next_step);
  for (int i=0;i<pulses;++i) {
    // do something slow 
    clock_gettime(CLOCK_REALTIME,&end_time2);
    // end of "slow" 
 
    next_step.tv_nsec += width;
 
    if (next_step.tv_nsec > 1000000000UL{
      next_step.tv_nsec -= 1000000000UL;
      ++next_step.tv_sec;
    }
 
    int rc = clock_nanosleep(CLOCK_REALTIME,
                             TIMER_ABSTIME,
                             &next_step,
                             NULL);
    if (rc) {
      printf("problems: %d\n",rc);
    }
  }
  clock_gettime(CLOCK_REALTIME,&end_time);
  clock_gettime(CLOCK_REALTIME,&end_time2);
 
  printf("began at %ld.09%ld\n",begin_time.tv_sec,begin_time.tv_nsec);
  printf("ended at %ld.09%ld\n",end_time.tv_sec,end_time.tv_nsec);
  printf("ended at %ld.09%ld\n",end_time2.tv_sec,end_time2.tv_nsec);
 
  long elapsed = end_time.tv_nsec - begin_time.tv_nsec;
  printf("elapsed: %ld\n",elapsed);
  printf("error: %ld\n",elapsed-(pulses*width));
}