kenrel API for NTP

kernel 提供两个API(即系统调用 system call)给应用程序NTP,去校准kernel system clock

Kernel Application Program Interface

The kernel application program interface (API) is used by the NTP protocol daemon (or equivalent) to discipline the system clock and set various parameters necessary for its correct operation. The API is used by application programs to read the system clock and determine its health and expected error values. Following is a description of the interface, as well as the control and monitoring variables involved.

The API consists of two Unix system calls, ntp_gettime() and ntp_adjtime(). The ntp_gettime() call returns the current system time in either timespec format in seconds and nanooseconds, or timeval format in seconds and microseconds, as determined by the particular operating system. In addition to the time value, this system call returns variables representing the maximum credible error and estimated error of the time value in microseconds and the current offset of International Atomic Time (TAI) relative to Universal Coordinated Time (UTC) when available. The ntp_adjtime() call is used to set and read certain kernel state variables according to a set of mode bits in the call. To set the variables requires superuser permission, but to read them requires no special permissions. Both system calls return a code indicating the current status of the system clock; that is, whether a leap second is pending or whether the clock is synchronized to a working reference source.

Following is a description of the various values used by the API, including state variables and control/status bits. Detailed calling sequences and structure definitions are in the timex.h header file included in the distribution.

adjtimex / ntp_adjtime - tune kernel clock

#man 2 adjtimex,其实kernel提供了两个system call ,用来调整kernel clock,一个是,adjtimex,一个是ntp_adjtime()

Linux uses David L. Mills’ clock adjustment algorithm (see RFC 1305). The system call adjtimex() reads and optionally sets adjustment parameters for this algorithm. It takes a pointer to a timex structure, updates kernel parameters from field values, and returns the same structure with current kernel values. This structure is declared as follows:

ntptime命令是ntpd包里的一个工具,它不是通过adjtimex系统调用对kernel clock进行调整的,而是通过,ntp_adjtime系统调用;

所以,推荐使用ntp_adjtime这个系统调用,而不是adjtimex!!!

http://man7.org/linux/man-pages/man2/adjtimex.2.html

但是原理上,这两个system call是没有区别的!!

adjtimex 和 ntp_adjtime 系统调用的返回值

RETURN VALUE top

   On success, adjtimex() and ntp_adjtime() return the clock state; that
is, one of the following values: TIME_OK Clock synchronized, no leap second adjustment pending. TIME_INS Indicates that a leap second will be added at the end of
the UTC day. TIME_DEL Indicates that a leap second will be deleted at the end
of the UTC day. TIME_OOP Insertion of a leap second is in progress. TIME_WAIT A leap-second insertion or deletion has been completed.
This value will be returned until the next ADJ_STATUS
operation clears the STA_INS and STA_DEL flags. TIME_ERROR The system clock is not synchronized to a reliable
server. This value is returned when any of the following
holds true: * Either STA_UNSYNC or STA_CLOCKERR is set. * STA_PPSSIGNAL is clear and either STA_PPSFREQ or
STA_PPSTIME is set. * STA_PPSTIME and STA_PPSJITTER are both set. * STA_PPSFREQ is set and either STA_PPSWANDER or
STA_PPSJITTER is set. The symbolic name TIME_BAD is a synonym for TIME_ERROR,
provided for backward compatibility. Note that starting with Linux 3.4, the call operates asynchronously
and the return value usually will not reflect a state change caused
by the call itself. On failure, these calls return -1 and set errno.

timex 结构体

man 2 adjtimex 你会看到 timex结构体

       struct timex {
int modes; /* mode selector */
long offset; /* time offset (usec) */
long freq; /* frequency offset (scaled ppm) */
long maxerror; /* maximum error (usec) */
long esterror; /* estimated error (usec) */
int status; /* clock command/status */
long constant; /* pll time constant */
long precision; /* clock precision (usec) (read-only) */
long tolerance; /* clock frequency tolerance (ppm)
(read-only) */
struct timeval time; /* current time (read-only) */
long tick; /* usecs between clock ticks */
};

The ntp_adjtime() System Call

The ntp_adjtime() system call is used to set and read kernel variables used by kernel. It operates using the timex structure described in the timex.h header file. This structure is used both to change the values of certain kernel variables and to return the current values. Root privilege is required to change the values. Following are the variables that can be read and written. The return codes are described in the Return Codes and the Leap-Second State Machine section on this page.

The ntp_gettime() System Call

The ntp_gettime() system call is used to read the current system time and related error variables. It uses the ntptimeval structure described in the timex.h header file. This structure includes the following variables. The return codes are described in the Return Codes and the Leap-Second State Machine section on this page.

kernel对NTP的API,系统调用函数的更多相关文章

  1. (转)linux下的系统调用函数到内核函数的追踪

    转载网址:http://blog.csdn.net/maochengtao/article/details/23598433 使用的 glibc : glibc-2.17使用的 linux kerne ...

  2. kernel笔记——库文件与系统调用

    库文件 先从我们熟悉的c库入手,理解系统调用(system call).c代码中调用printf函数,经历了以下调用过程:   最终输出的功能由内核中write调用完成,c库封装了系统调用. 对于以下 ...

  3. API各函数作用简介

    API各函数作用简介 1.控件与消息函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeI ...

  4. 简介几种系统调用函数:write、read、open、close、ioctl

    在 Linux 中,一切(或几乎一切)都是文件,因此,文件操作在 Linux 中是十分重要的,为此,Linux 系统直接提供了一些函数用于对文件和设备进行访问和控制,这些函数被称为系统调用(sysca ...

  5. 使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程

    使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程 http://blog.csdn.net/liujiayu2/article/details/5 ...

  6. 【Linux】文件操作函数(系统调用函数)

    重点在于学习--思路与方法 举一反三 一.文件描述符 系统分配给文件的数字编号 二.函数学习 P.S.Man命令使用方法 manual 前三个章节 命令:系统调用函数:库函数 man read //r ...

  7. LInux文件基础知识和文件目录操作(系统调用函数方式)

    1.进程是处于活动状态的程序,某个用户通过操作系统运行程序所产生的进程代表着该用户的行为.如果用户不具备访问某个目录和文件的权限,那么该用户的进程也不能访问. 2.Linux系统中文件安全机制是通过给 ...

  8. Windows API 常用函数---转载

    Windows API 常用函数 2014-10-15 14:21  xiashengwang  阅读(2105)  评论(0)  编辑  收藏 .Net中虽然类库很强的,但还是有些时候功能有限,掌握 ...

  9. 【XLL 文档翻译】【第2部分】C API 回调函数 Excel4, Excel12

    Excel4 和 Excel12 函数使得 DLL 可以调用 Excel 工作表函数.宏表函数.命令.XLL特定函数或命令.最近的一些 Excel 版本都支持 Excel12 函数.这两个函数支持下面 ...

随机推荐

  1. eclipse实现批量修改文件的编码方式

    http://blog.csdn.net/haorengoodman/article/details/38493007 在eclipse+MyEclipse环境下,打开一个jsp文件,经常发现汉字无法 ...

  2. mdl 锁 SYSTEMTAP跟踪

    systemtap : 各种资源的使用限制由所生成的C代码中的宏来设置.这些值可在编译时由-D选项来重写.下面描述了部分挑选出来的宏: MAXNESTING 递归函数的最大调用层数,默认值是10. M ...

  3. 一个使用命令行编译Android项目的工具类

    一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...

  4. 125条常见的java面试、笔试题大汇总

    1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解所有问题,而仅仅是选择当中的一部分,临时不用部分细节.抽象包含两个方面,一是过程抽象. ...

  5. 【cl】eclipse基本设置(字体、配置JDK)

    字体       找到上面的菜单“windows”打开Preferences            在弹出的设置窗口中找到“colors and fonts”                    将 ...

  6. VC++玩转Native Wifi API 2---Wifi on与wifi off

     有心栽花花不开,无心插柳柳成排. 今天要说的这个wifi on\off是在软件层面控制无线网卡的开和关. 问题听起来简单,调查起来复杂.但解决起来却也简单.关键函数便是Native wifi a ...

  7. DeepLearning to digit recognizer in kaggle

    DeepLearning to digit recongnizer in kaggle 近期在看deeplearning,于是就找了kaggle上字符识别进行练习.这里我主要用两种工具箱进行求解.并比 ...

  8. 【BZOJ2806】【CTSC2012】Cheat 广义后缀自动机+二分+Dp

    题目 题目在这里 思路&做法 我们先对标准作文库建广义后缀自动机. 然后对于每一篇阿米巴的作文, 我们首先把放到广义后缀自动机跑一遍, 对于每一个位置, 记录公共子串的长度\((\)即代码和下 ...

  9. Android平台下的TCP/IP传输(客户端)

    在工科类项目中,嵌入式系统与软件系统或后台数据库之间的信息传输是实现“物联网”的一种必要的途径,对已简单概念的物联网,通常形式都是一个单片机/嵌入式系统实现数据的采集及其处理,通过蓝牙,wifi或者是 ...

  10. Python关于super()函数的理解

    看下面的例子: class A: def __init__(self, name): self.name = name def bb(self): print('没事就爱瞎BB') class B(A ...