Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MonotonicTime for use with timeouts #55

Closed
wants to merge 1 commit into from

Conversation

flixr
Copy link
Contributor

@flixr flixr commented Feb 25, 2017

Add a new MonotonicTime which uses CLOCK_MONOTONIC to prevent issues with time jumps.

If clock_gettime is not available it will fall back to normal wall time... but that shouldn't be the case on many Linux systems anymore.

Didn't look at the Windows implementation so far, also just returns wall time there.

See ros/bond_core#16 and ros/nodelet_core#35 for discussions why this could be useful.

@flixr
Copy link
Contributor Author

flixr commented Mar 1, 2017

@wjwwood @mikaelarguedas What do you think about adding a new time class like MonotonicTime (or maybe better named SteadyTime in accordance to C++11 naming?).

This would already make it much easier to compute time deltas that should not be affected by NTP, PTP, etc adjusting the system time.

#endif
#else
ros_walltime(sec, nsec);
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a great place to use std::chrono::steady_clock or std::chrono::high_resolution_clock when it returns true for is_steady, since they're portable.

However, I'm not sure that would be acceptable within roscpp_core since it's a C++11 only feature.

@@ -256,8 +256,41 @@ namespace ros
static bool isSystemTime() { return true; }
};

/**
* \brief Time representation. Always monotonic-clock time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted here that it is unaffected by ROS time, just like "wall" time is not.

struct timeval timeofday;
gettimeofday(&timeofday,NULL);
sec = timeofday.tv_sec;
nsec = timeofday.tv_usec * 1000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to avoid this on macOS (also other BSD systems which don't have clock_gettime), but I'm hesitant to ask for it since the existing wall time system doesn't use it.

I've implemented something that is monotonic and works on Linux, and macOS for ROS 2:

https://github.com/ros2/rcl/blob/545cb5602c4bf9d000f0ebe62b9d95cb9bf59469/rcl/src/rcl/time_unix.c#L73-L102

There's also something for Windows:

https://github.com/ros2/rcl/blob/66208db876bba344a6733f4aa33451a028085612/rcl/src/rcl/time_win32.c#L49-L65

You're welcome to steal those implementations if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks...
On a sidenote:
I'm not convinced that it is a good idea to use the CLOCK_MONOTONIC_RAW (even if its available) for steady time. IMHO CLOCK_MONOTONIC is always what we want here, namely it doesn't jump, but the clock drift is adjusted...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's been too long since I've looked at it to respond intelligently 😄, but I'll review the use of CLOCK_MONOTONIC_RAW. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sec = start.tv_sec;
nsec = start.tv_nsec;
#else
// what to do if clock_gettime is not available???
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't merge this with this code path in here, since I'm pretty sure that it isn't monotonic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I would actually just leave the case when clock_gettime is not available out completely, as I don't know of any way to get a monotonic clock otherwise and this should not be the case on any actually used Linux versions anymore...

@flixr flixr mentioned this pull request Mar 3, 2017
@flixr
Copy link
Contributor Author

flixr commented Mar 3, 2017

closing in favor of #57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants