-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move clock_adjtime wrapping to clockadj.c.
Signed-off-by: Miroslav Lichvar <[email protected]>
- Loading branch information
1 parent
58b1f3f
commit a29b1a4
Showing
5 changed files
with
134 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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 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,72 @@ | ||
/** | ||
* @file clockadj.c | ||
* @note Copyright (C) 2013 Richard Cochran <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#include <string.h> | ||
|
||
#include "clockadj.h" | ||
#include "missing.h" | ||
#include "print.h" | ||
|
||
#define NS_PER_SEC 1000000000LL | ||
|
||
void clockadj_set_freq(clockid_t clkid, double freq) | ||
{ | ||
struct timex tx; | ||
memset(&tx, 0, sizeof(tx)); | ||
tx.modes = ADJ_FREQUENCY; | ||
tx.freq = (long) (freq * 65.536); | ||
if (clock_adjtime(clkid, &tx) < 0) | ||
pr_err("failed to adjust the clock: %m"); | ||
} | ||
|
||
double clockadj_get_freq(clockid_t clkid) | ||
{ | ||
double f = 0.0; | ||
struct timex tx; | ||
memset(&tx, 0, sizeof(tx)); | ||
if (clock_adjtime(clkid, &tx) < 0) | ||
pr_err("failed to read out the clock frequency adjustment: %m"); | ||
else | ||
f = tx.freq / 65.536; | ||
return f; | ||
} | ||
|
||
void clockadj_step(clockid_t clkid, int64_t step) | ||
{ | ||
struct timex tx; | ||
int sign = 1; | ||
if (step < 0) { | ||
sign = -1; | ||
step *= -1; | ||
} | ||
memset(&tx, 0, sizeof(tx)); | ||
tx.modes = ADJ_SETOFFSET | ADJ_NANO; | ||
tx.time.tv_sec = sign * (step / NS_PER_SEC); | ||
tx.time.tv_usec = sign * (step % NS_PER_SEC); | ||
/* | ||
* The value of a timeval is the sum of its fields, but the | ||
* field tv_usec must always be non-negative. | ||
*/ | ||
if (tx.time.tv_usec < 0) { | ||
tx.time.tv_sec -= 1; | ||
tx.time.tv_usec += 1000000000; | ||
} | ||
if (clock_adjtime(clkid, &tx) < 0) | ||
pr_err("failed to step clock: %m"); | ||
} |
This file contains 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,47 @@ | ||
/** | ||
* @file clockadj.h | ||
* @brief Wraps clock_adjtime functionality. | ||
* @note Copyright (C) 2013 Miroslav Lichvar <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
#ifndef HAVE_CLOCKADJ_H | ||
#define HAVE_CLOCKADJ_H | ||
|
||
#include <inttypes.h> | ||
#include <time.h> | ||
|
||
/* | ||
* Set clock's frequency offset. | ||
* @param clkid A clock ID obtained using phc_open() or CLOCK_REALTIME. | ||
* @param freq The frequency offset in parts per billion (ppb). | ||
*/ | ||
void clockadj_set_freq(clockid_t clkid, double freq); | ||
|
||
/* | ||
* Read clock's frequency offset. | ||
* @param clkid A clock ID obtained using phc_open() or CLOCK_REALTIME. | ||
* @return The frequency offset in parts per billion (ppb). | ||
*/ | ||
double clockadj_get_freq(clockid_t clkid); | ||
|
||
/* | ||
* Step clock's time. | ||
* @param clkid A clock ID obtained using phc_open() or CLOCK_REALTIME. | ||
* @param step The time step in nanoseconds. | ||
*/ | ||
void clockadj_step(clockid_t clkid, int64_t step); | ||
|
||
#endif |
This file contains 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 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