-
Notifications
You must be signed in to change notification settings - Fork 905
RFC: opal_clock_gettime() and opal_clock_getres() #9798
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2022 Cisco Systems, Inc. All rights reserved. | ||
* $COPYRIGHT$ | ||
* | ||
* Additional copyrights may follow | ||
* | ||
* $HEADER$ | ||
*/ | ||
|
||
/** @file clock_gettime.h | ||
* | ||
* Simple, portable wrappers around clock_gettime(3) and | ||
* clock_getres(3) to always get monotonically-increasing time. | ||
* | ||
* If the underlying OS does not have clock_gettime(3), use | ||
* gettimeofday(3) instead. | ||
* | ||
* We intentionally do not use the OPAL timer framework for | ||
* high-prevision time here; see | ||
* https://github.com/open-mpi/ompi/issues/3003 for more details. | ||
* | ||
* As of Dec 2021, it turns out that CLOCK_MONOTONIC can actually go | ||
* backwards on macOS (!). CLOCK_MONOTONIC does *not* go backwards on | ||
* Linux (or anywhere else we can find), though, even in the presence | ||
* of small NTP time adjustments -- e.g., adjtime(3) simply slightly | ||
* speeds up or slows down the system clock to make it eventually get | ||
* to the desired time. On macOS, we can use CLOCK_MONOTONIC_RAW, | ||
* which never goes backwards. | ||
* | ||
* Hence, for these wrappers, use CLOCK_MONOTONIC_RAW on Darwin, and | ||
* use CLOCK_MONOTONIC everywhere else. | ||
* | ||
* See | ||
* https://github.com/open-mpi/ompi/pull/8057#discussion_r762612710 | ||
* and | ||
* https://github.com/open-mpi/ompi/pull/8057#discussion_r762618783 | ||
* for more details. | ||
*/ | ||
|
||
#ifndef OPAL_UTIL_CLOCK_GETTIME_H_ | ||
#define OPAL_UTIL_CLOCK_GETTIME_H_ | ||
|
||
#include "opal_config.h" | ||
|
||
#if HAVE_TIME_H | ||
#include <time.h> | ||
#endif | ||
#if HAVE_SYS_TIME_H | ||
#include <sys/time.h> | ||
#endif | ||
|
||
#if OPAL_HAVE_CLOCK_GETTIME | ||
#if defined(__darwin__) | ||
#define OPAL_CLOCK_TYPE CLOCK_MONOTONIC_RAW | ||
#else | ||
#define OPAL_CLOCK_TYPE CLOCK_MONOTONIC | ||
#endif | ||
#endif // OPAL_HAVE_CLOCK_GETTIME | ||
|
||
#if !defined(HAVE_STRUCT_TIMESPEC_TV_NSEC) | ||
// Make sure that we have struct timespec; if not, define it. | ||
struct timespec { | ||
time_t tv_sec; | ||
long tv_nsec; | ||
}; | ||
#endif | ||
|
||
/** | ||
* Simple, portable wrapper around clock_gettime(3) for high-precision time. | ||
* | ||
* If the underlying system does not have clock_gettime(3), use | ||
* gettimeofday(3) instead. | ||
* | ||
* @param spec (OUT) Struct to return the time | ||
* @return Return value from underlying clock_gettime() | ||
*/ | ||
static inline int opal_clock_gettime(struct timespec *spec) | ||
{ | ||
#if OPAL_HAVE_CLOCK_GETTIME | ||
return clock_gettime(OPAL_CLOCK_TYPE, spec); | ||
#else | ||
// If we do not have clock_gettime(), fall back to gettimeofday() | ||
struct timeval tv; | ||
int ret = gettimeofday(&tv, NULL); | ||
|
||
spec->tv_sec = tv.tv_sec; | ||
// Elevate the micrseconds to nanoseconds | ||
spec->tv_nsec = tv.tv_usec * 1000; | ||
|
||
return ret; | ||
#endif | ||
} | ||
|
||
/** | ||
* Simple, portable wrapper around clock_getres(3) for high-precision time. | ||
* | ||
* If the underlying system does not have clock_gettime(3), return usec | ||
* precison (because opal_clock_gettime() will be using gettimeofday(3)). | ||
* | ||
* @param spec (OUT) Struct to return the resolution | ||
* @return Return value from underlying clock_getres() | ||
*/ | ||
static inline int opal_clock_getres(struct timespec *spec) | ||
{ | ||
#if OPAL_HAVE_CLOCK_GETTIME | ||
return clock_getres(OPAL_CLOCK_TYPE, spec); | ||
#else | ||
// If we don't have clock_gettime(), just return usec precision. | ||
spec->tv_sec = 0; | ||
spec->tv_nsec = 1000; | ||
|
||
return 0; | ||
#endif | ||
} | ||
|
||
#endif // OPAL_UTIL_CLOCK_GETTIME_H_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.