C Odd results from clock_gettime() with CLOCK_MONOTONIC

Hello,
I was trying to use a LUA timer called chronos and I was getting strange results. I've pulled the code out and put it in a little loop and I still see strange results. The author of said time utility doesn't have FreeBSD so I suspect somethings been missed? There are two specific things I'm seeing. Time will 'jump backwards', sometimes between executions of the app, sometimes during an excecution. The other problem is even though I'm calling this every second, The difference between each loop is very uneven. I've marked the jumps backwards with -->?

I'm just using the stock cc compiler. Does anyone have any advice?

sh:
russellh@g1 ~/G/c/src> uname -a
FreeBSD g1.highfell.home 11.1-RELEASE-p4 FreeBSD 11.1-RELEASE-p4 #0: Tue Nov 14 06:12:40 UTC 2017     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64
russellh@g1 ~/G/c/src> cc --version
FreeBSD clang version 4.0.0 (tags/RELEASE_400/final 297347) (based on LLVM 4.0.0)
Target: x86_64-unknown-freebsd11.1
Thread model: posix
InstalledDir: /usr/bin

C:
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>


int main(int argc, char **argv){
struct timespec t_info;
static struct timespec res_info = {.tv_nsec = 0, .tv_sec = 0};
static double multiplier;
 
 
  clock_getres(CLOCK_MONOTONIC, &res_info);
  multiplier = 1.0   / (1.e9 / res_info.tv_nsec);
  for(int i = 0; i <= 15; i++){
    if(clock_gettime(CLOCK_MONOTONIC, &t_info) != 0){
      printf("clock_gettime() failed:%s", strerror(errno));
    }
    printf("%f\n", t_info.tv_sec + (t_info.tv_nsec * multiplier));
    sleep(1);
  }

}

Here is the output.
Code:
russellh@g1 ~/G/c/src> cc mytime.c 
russellh@g1 ~/G/c/src> ./a.out
4264.321903
4274.823337
4278.999190
russellh@g1 ~/G/c/src> cc mytime.c 
russellh@g1 ~/G/c/src> ./a.out
4409.501599 --> ?
4145.343659
4154.049346
4155.546900
4162.458647
4168.932261
4172.420506
4177.465081
4180.468677
4183.389691
4186.491191
4204.113855
4207.908541
4211.193828
4218.294940
4222.871288
russellh@g1 ~/G/c/src> ./a.out
4325.681940
4328.961412
4334.820021
4336.201834
4341.375508
4345.174830
4364.074884
4369.851933
4380.744512
4390.618527
4405.436151
4408.980734
4418.470125
4424.248196
4435.808110
4437.884603
russellh@g1 ~/G/c/src> ./a.out
4265.945151 -->?
4273.016090
4280.875134
4299.141639
4303.652692
4319.094120
4336.561913
4350.830378
4359.792455
4368.482427
4374.329014
4385.747730
4391.749110
4400.586724
4402.807322
4410.012389
russellh@g1 ~/G/c/src> ./a.out
4444.365967
4463.468821
4469.198883
4474.206685
4490.491063
4495.236033
4499.905021
4503.284361
4243.517571 -->?
4262.540516
4281.821264
4284.943037
4286.775947
4290.944493
4299.621735
4303.122631
russellh@g1 ~/G/c/src> ./a.out
4349.833726
4368.662198
4375.928788
4381.127763
4382.482153
4389.818911
4391.562718
4402.820749
4404.374248
4410.265466
4416.861106
4425.478082
4430.364565
4433.665173
4435.147484
4446.135610
russellh@g1 ~/G/c/src> ./a.out
4308.583511 --> ?
4316.202274
4320.841325
4323.506185
4332.537014
4349.035509
4354.856638
4358.346376
4365.198328
4367.926833
4374.371846
4375.748002
4378.946699
4383.867833
4388.078416
4399.359076
russellh@g1 ~/G/c/src> ./a.out
4514.440120
4517.562128
4523.017021
4529.908574
4534.280706
4536.371499
4544.422894
4545.861360
4553.576769
4559.297716
4560.689508
4565.046318
4296.444034 -->?
4298.852662
4307.238628
4312.351327
 
There are two specific things I'm seeing. Time will 'jump backwards', sometimes between executions of the app, sometimes during an excecution. The other problem is even though I'm calling this every second, The difference between each loop is very uneven.
Are you on a virtual machine?
 
Back
Top