strange periodic behavior
Hey all,
I've been experimenting with writing a simple timer program using the native API. Basically, each loop, it writes the current timestamp into shared memory which then gets read by a slower loop and written to disk. The weird thing is, the period I get seems to always be half of what I've commanded. That is, if I set the frequency to 1000 Hz, the difference between each timestamp is a little over 2 ms (500 Hz). It works the same way for 10000 Hz and 20000 Hz. Does anyone know what causes this or how to fix it? Thanks.
-Brandt
Quoted 10 lines · Brandt Erickson
Hey all,
I've been experimenting with writing a simple timer program using the native API. Basically, each loop, it writes the current timestamp into shared memory which then gets read by a slower loop and written to disk. The weird thing is, the period I get seems to always be half of what I've commanded. That is, if I set the frequency to 1000 Hz, the difference between each timestamp is a little over 2 ms (500 Hz). It works the same way for 10000 Hz and 20000 Hz. Does anyone know what causes this or how to fix it? Thanks.
-Brandt
[FAQ] ;)
Is powersaving via CPU frequency scaling activated (my notebook, e.g., scales by 2 in that mode)? Make sure it's not. Also compare your findings with the output of the latency test tool. Does it suffer too?
Jan
Well, I'm working on a desktop but frequency scaling was enabled, so I recompiled the kernel without frequency scaling support and the behavior still persists. I've run the latency program from the test suite, but I'm not sure how I should interpret the output. What indicators am I looking for?
-Brandt
Quoted 22 lines
Quoted 4 lines · Brandt Erickson
Hey all,
I've been experimenting with writing a simple timer program using the native API. Basically, each loop, it writes the current timestamp into shared memory which then gets read by a slower loop and written to disk.Quoted 8 lines
The weird thing is, the period I get seems to always be half of what I've
commanded. That is, if I set the frequency to 1000 Hz, the difference between each timestamp is a little over 2 ms (500 Hz). It works the same
way for 10000 Hz and 20000 Hz. Does anyone know what causes this or how to fix it? Thanks.
-Brandt[FAQ] ;)
Is powersaving via CPU frequency scaling activated (my notebook, e.g., scales by 2 in that mode)? Make sure it's not. Also compare your findings with the output of the latency test tool. Does it suffer too?Jan
Quoted 3 lines · Brandt Erickson
Well, I'm working on a desktop but frequency scaling was enabled, so I recompiled the kernel without frequency scaling support and the behavior still persists.
Sure your kernel was updated? Check /proc/cpuinfo for varying numbers with and without system load.
Quoted 3 lines
I've run the latency program from the test suite, but I'm
not sure how I should interpret the output. What indicators am I looking for?
High jitters of the measured delays if the cpu frequency changes. Once the frequency is different from the calibration time, you either get too short or too long intervals when reading the time. You will not see any effect if your CPU is in the same state during both calibration and measuring time.
But maybe it is better to post a simple excerpt of your test which shows your problem and let us cross-check.
Jan
Quoted 2 lines
Sure your kernel was updated? Check
/proc/cpuinfofor varying numbers with and without system load.
Yes, the kernel is up-to-date. The config file on /boot confirms and there is no variation in frequency going from and idle load to ~full load.
Quoted 2 lines
But maybe it is better to post a simple excerpt of your test which shows your problem and let us cross-check.
sample output from the latency program is attached. Thanks for the help. -Brandt
Quoted 10 lines · Brandt Erickson
Quoted 2 lines
Sure your kernel was updated? Check
/proc/cpuinfofor varying numbers with and without system load.Yes, the kernel is up-to-date. The config file on /boot confirms and there is no variation in frequency going from and idle load to ~full load.
Quoted 2 lines
But maybe it is better to post a simple excerpt of your test which shows your problem and let us cross-check.
sample output from the latency program is attached. Thanks for the help.
Grmbl, my fault. Your test looks ok, but I was rather referring to your *code*.
Jan
Quoted 2 lines
Grmbl, my fault. Your test looks ok, but I was rather referring to your *code*.
Sorry, here's a small portion of what it outputs. The first column is the timestamp and the second is the overrun value from rt_task_wait_period. In this case, the "control loop" is supposed to be running at 10 KHz.
0.000087, 0
0.000302, 0
0.000522, 0
0.000742, 0
0.000962, 0
0.001182, 0
0.001402, 0
0.001623, 0
0.001843, 0
0.002063, 0
0.002283, 0
0.002503, 0
0.002723, 0
0.002944, 0
0.003164, 0
0.003384, 0
0.003604, 0
0.003824, 0
0.004045, 0
0.004265, 0
0.004485, 0
0.004705, 0
0.004925, 0
0.005146, 0
0.005366, 0
0.005586, 0
0.005806, 0
0.006026, 0
0.006246, 0
0.006467, 0
0.006687, 0
0.006907, 0
0.007127, 0
0.007347, 0
0.007568, 0
0.007788, 0
0.008008, 0
0.008228, 0
0.008448, 0
0.008669, 0
0.008889, 0
0.009109, 0
0.009329, 0
0.009549, 0
0.009769, 0
0.009990, 0
0.010210, 0
Quoted 7 lines · Brandt Erickson
Quoted 2 lines
Grmbl, my fault. Your test looks ok, but I was rather referring to your *code*.
Sorry, here's a small portion of what it outputs. The first column is the timestamp and the second is the overrun value from
rt_task_wait_period. In this case, the "control loop" is supposed to be running at 10 KHz.
Let's try this again :): Could you post your test program or better a reduced version of it that demonstrate the wrong timing? We can then analyse and/or compile and run it on some of our test systems.
Jan
Quoted 46 lines · Brandt Erickson
Quoted 2 lines
Grmbl, my fault. Your test looks ok, but I was rather referring to your *code*.
Sorry, here's a small portion of what it outputs. The first column is the timestamp and the second is the overrun value from
rt_task_wait_period. In this case, the "control loop" is supposed to be running at 10 KHz.0.000087, 0
0.000302, 0
0.000522, 0
0.000742, 0
0.000962, 0
0.001182, 0
0.001402, 0
0.001623, 0
0.001843, 0
0.002063, 0
0.002283, 0
0.002503, 0
0.002723, 0
0.002944, 0
0.003164, 0
0.003384, 0
0.003604, 0
0.003824, 0
0.004045, 0
0.004265, 0
0.004485, 0
0.004705, 0
0.004925, 0
0.005146, 0
0.005366, 0
0.005586, 0
0.005806, 0
0.006026, 0
0.006246, 0
0.006467, 0
0.006687, 0
0.006907, 0
0.007127, 0
0.007347, 0
0.007568, 0
0.007788, 0
0.008008, 0
0.008228, 0
0.008448, 0
Quoted 9 lines
0.008669, 0
0.008889, 0
0.009109, 0
0.009329, 0
0.009549, 0
0.009769, 0
0.009990, 0
0.010210, 0
[looking at it again]
Ah, this is drifting! The more important is the code to see what clock sources you use. They are obviously not in sync.
Jan
Turns out the timestamp was calculated using rt_timer_tsc() so on a machine with a processor speed close to 1GHz the value is close to the wall clock time but on a 2.2GHz machine the value is off. Using rt_timer_tsc2ns() solves the issue. Thanks for the help. -Brandt