clock_gettime with CLOCK_MONOTONIC shows non-monotonic behaviour (14.04,18.04)
I have an issue using the clock_gettime()
system-call with theCLOCK_MONOTONIC
clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:
uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
I am running the following code which creates a new pthread
. This newly created pthread
enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...)
system call:
#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
timespec tik;
timespec tok;
void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}
int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){
}
return 0;
}
Compiling this code with g++
typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*)
:
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec)
actually becomes negative which doesn't make sense since tok
is always set after tik
. Does anyone have an idea what the issue could be here?
Thanks!
Addition 1: I just tested the same code on 18.04 and the same issue occurred.
c clock realtime
|
show 3 more comments
I have an issue using the clock_gettime()
system-call with theCLOCK_MONOTONIC
clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:
uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
I am running the following code which creates a new pthread
. This newly created pthread
enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...)
system call:
#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
timespec tik;
timespec tok;
void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}
int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){
}
return 0;
}
Compiling this code with g++
typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*)
:
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec)
actually becomes negative which doesn't make sense since tok
is always set after tik
. Does anyone have an idea what the issue could be here?
Thanks!
Addition 1: I just tested the same code on 18.04 and the same issue occurred.
c clock realtime
the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using:setitimer()
as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'
– user3629249
Jan 16 at 21:15
in the thread function, the statement:return NULL;
would be better written as:pthread_exit( NULL );
– user3629249
Jan 16 at 21:20
the the main() function, rather than a forever loop, suggestpthread_join( loopthread, NULL );
– user3629249
Jan 16 at 21:22
the posted code is missing the statement:#include <stdbool.h>
which definesbool
,true
,false
– user3629249
Jan 16 at 21:26
regarding:timespec tok;
and similar statements: those statements should start with:struct timespec
– user3629249
Jan 16 at 21:28
|
show 3 more comments
I have an issue using the clock_gettime()
system-call with theCLOCK_MONOTONIC
clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:
uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
I am running the following code which creates a new pthread
. This newly created pthread
enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...)
system call:
#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
timespec tik;
timespec tok;
void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}
int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){
}
return 0;
}
Compiling this code with g++
typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*)
:
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec)
actually becomes negative which doesn't make sense since tok
is always set after tik
. Does anyone have an idea what the issue could be here?
Thanks!
Addition 1: I just tested the same code on 18.04 and the same issue occurred.
c clock realtime
I have an issue using the clock_gettime()
system-call with theCLOCK_MONOTONIC
clock. The problem is that the clock returned by this system call seems to behave non-monotonically. First of, some system information:
uname -a
4.4.0-138-generic #164~14.04.1-Ubuntu SMP Fri Oct 5 08:56:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
I am running the following code which creates a new pthread
. This newly created pthread
enters an infinite loop and is supposed to do a task every 100ms. In order to measure wall-clock time-intervals, the code uses the clock_gettime(CLOCK_MONOTONIC,...)
system call:
#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
timespec tik;
timespec tok;
void* loop(void* argptr){
while(true){
clock_gettime(CLOCK_MONOTONIC,&tik);
//
// do periodic task here
//
while(true){
clock_gettime(CLOCK_MONOTONIC,&tok);
if(tok.tv_nsec-tik.tv_nsec>100e6){
printf("10ms have passedn");
break;
}
}
}
return NULL;
}
int main()
{
//create a thread which does a periodic task every 100ms
pthread_t loopthread;
if(pthread_create(&loopthread,NULL,loop,NULL)){
fprintf(stderr,"Error creating thread");
return EXIT_FAILURE;
}
//main thread waits forever
while(true){
}
return 0;
}
Compiling this code with g++
typically prints a couple of lines to the console but then gets stuck in the inner while loop of the function void* loop(void*)
:
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
10ms have passed
A bit of debugging shows that the quantity (tok.tv_nsec-tik.tv_nsec)
actually becomes negative which doesn't make sense since tok
is always set after tik
. Does anyone have an idea what the issue could be here?
Thanks!
Addition 1: I just tested the same code on 18.04 and the same issue occurred.
c clock realtime
c clock realtime
edited Jan 15 at 20:13
Mantabit
asked Jan 15 at 19:30
MantabitMantabit
63
63
the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using:setitimer()
as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'
– user3629249
Jan 16 at 21:15
in the thread function, the statement:return NULL;
would be better written as:pthread_exit( NULL );
– user3629249
Jan 16 at 21:20
the the main() function, rather than a forever loop, suggestpthread_join( loopthread, NULL );
– user3629249
Jan 16 at 21:22
the posted code is missing the statement:#include <stdbool.h>
which definesbool
,true
,false
– user3629249
Jan 16 at 21:26
regarding:timespec tok;
and similar statements: those statements should start with:struct timespec
– user3629249
Jan 16 at 21:28
|
show 3 more comments
the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using:setitimer()
as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'
– user3629249
Jan 16 at 21:15
in the thread function, the statement:return NULL;
would be better written as:pthread_exit( NULL );
– user3629249
Jan 16 at 21:20
the the main() function, rather than a forever loop, suggestpthread_join( loopthread, NULL );
– user3629249
Jan 16 at 21:22
the posted code is missing the statement:#include <stdbool.h>
which definesbool
,true
,false
– user3629249
Jan 16 at 21:26
regarding:timespec tok;
and similar statements: those statements should start with:struct timespec
– user3629249
Jan 16 at 21:28
the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using:
setitimer()
as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'– user3629249
Jan 16 at 21:15
the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using:
setitimer()
as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'– user3629249
Jan 16 at 21:15
in the thread function, the statement:
return NULL;
would be better written as: pthread_exit( NULL );
– user3629249
Jan 16 at 21:20
in the thread function, the statement:
return NULL;
would be better written as: pthread_exit( NULL );
– user3629249
Jan 16 at 21:20
the the main() function, rather than a forever loop, suggest
pthread_join( loopthread, NULL );
– user3629249
Jan 16 at 21:22
the the main() function, rather than a forever loop, suggest
pthread_join( loopthread, NULL );
– user3629249
Jan 16 at 21:22
the posted code is missing the statement:
#include <stdbool.h>
which defines bool
, true
, false
– user3629249
Jan 16 at 21:26
the posted code is missing the statement:
#include <stdbool.h>
which defines bool
, true
, false
– user3629249
Jan 16 at 21:26
regarding:
timespec tok;
and similar statements: those statements should start with: struct timespec
– user3629249
Jan 16 at 21:28
regarding:
timespec tok;
and similar statements: those statements should start with: struct timespec
– user3629249
Jan 16 at 21:28
|
show 3 more comments
1 Answer
1
active
oldest
votes
Ok, the solution to this is pretty trivial, one simply needs to replace
tok.tv_nsec-tik.tv_nsec
with
(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)
since the struct timespec
which is set by clock_gettime(...)
has two members, one to store the nanoseconds (tv_nsec
) and one to store the seconds (tv_sec
).
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110039%2fclock-gettime-with-clock-monotonic-shows-non-monotonic-behaviour-14-04-18-04%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ok, the solution to this is pretty trivial, one simply needs to replace
tok.tv_nsec-tik.tv_nsec
with
(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)
since the struct timespec
which is set by clock_gettime(...)
has two members, one to store the nanoseconds (tv_nsec
) and one to store the seconds (tv_sec
).
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
add a comment |
Ok, the solution to this is pretty trivial, one simply needs to replace
tok.tv_nsec-tik.tv_nsec
with
(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)
since the struct timespec
which is set by clock_gettime(...)
has two members, one to store the nanoseconds (tv_nsec
) and one to store the seconds (tv_sec
).
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
add a comment |
Ok, the solution to this is pretty trivial, one simply needs to replace
tok.tv_nsec-tik.tv_nsec
with
(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)
since the struct timespec
which is set by clock_gettime(...)
has two members, one to store the nanoseconds (tv_nsec
) and one to store the seconds (tv_sec
).
Ok, the solution to this is pretty trivial, one simply needs to replace
tok.tv_nsec-tik.tv_nsec
with
(tok.tv_nsec+tok.tv_sec*1e9)-(tik.tv_nsec+tik.tv_sec*1e9)
since the struct timespec
which is set by clock_gettime(...)
has two members, one to store the nanoseconds (tv_nsec
) and one to store the seconds (tv_sec
).
answered Jan 15 at 21:05
MantabitMantabit
63
63
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
add a comment |
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
overall, this is a very poor method for timing an interval as the code will be executing in a loop, consuming (most) all the CPU cycles.
– user3629249
Jan 16 at 21:17
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
Thanks for your suggestions, I will have a look at the methods you mentioned. I guess setitimer() should be more precise than setting a QTimer? I also thought about using usleep(useconds_t) together with clock_gettime() to compute the sleep time?
– Mantabit
Jan 16 at 21:34
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1110039%2fclock-gettime-with-clock-monotonic-shows-non-monotonic-behaviour-14-04-18-04%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
the posted code is (almost) as bad as an assembly instruction that branches to itself. Suggest using:
setitimer()
as it is made for timing intervals and letting the program know when it has expired. Suggest reading the MAN page, especially the 'Notes:'– user3629249
Jan 16 at 21:15
in the thread function, the statement:
return NULL;
would be better written as:pthread_exit( NULL );
– user3629249
Jan 16 at 21:20
the the main() function, rather than a forever loop, suggest
pthread_join( loopthread, NULL );
– user3629249
Jan 16 at 21:22
the posted code is missing the statement:
#include <stdbool.h>
which definesbool
,true
,false
– user3629249
Jan 16 at 21:26
regarding:
timespec tok;
and similar statements: those statements should start with:struct timespec
– user3629249
Jan 16 at 21:28