当前位置:网站首页>AOSP ~ NTP ( 网络时间协议 )
AOSP ~ NTP ( 网络时间协议 )
2022-07-03 10:23:00 【南柯好萌】
网络时间协议NTP(Network Time Protocol)是TCP/IP协议族里面的一个应用层协议,用来使客户端和服务器之间进行时钟同步,提供高精准度的时间校正。NTP服务器从权威时钟源(例如原子钟、GPS)接收精确的协调世界时UTC,客户端再从服务器请求和接收时间。
NTP基于UDP报文进行传输,使用的UDP端口号为123。
为什么时钟同步很重要?
出于诸多原因,精确的时间对于网络至关重要,比如:
- 网络管理:从不同网络设备采集来的日志信息进行分析时,需要以时间作为参照依据。如果不同设备上的系统时间不一致,会因先后顺序等问题给故障定位带来障碍。
- 计费系统:计费业务对于时间尤其敏感,要求所有设备的时间保持一致,否则会引起计费不准确,导致用户质疑、投诉等。
- 协同处理:多个系统协同处理同一个复杂事件,为保证正确的执行顺序,多个系统必须参考同一时钟。
- 系统时间:某些应用或服务需要准确的时间来标记用户登录、交易等操作信息,确保可追溯记录。
- 因此有一个统一的标准时间对于网络而言意义重大。
NTP就是用来使网络中的各个主机时钟同步的一种协议,他把主机的时钟同步到协调世界时UTC,其精度在LAN网络内可达1毫秒内,在WAN网络上可以达到几十毫秒内。
NTP协议发展历史
NTP是由美国Delaware大学David L .Mills教授设计的,是最早用于网络中时钟同步的标准之一。NTP是从时间协议和ICMP时间戳报文演变而来,NTP的版本演进如下所示。
NTP时钟层级
NTP允许客户端从服务器请求和接收时间,而服务器又从权威时钟源(例如原子钟、GPS)接收精确的协调世界时UTC。
NTP以层级来组织模型结构,层级中的每层被称为Stratum。通常将从权威时钟获得时钟同步的NTP服务器的层数设置为Stratum 1,并将其作为主时间服务器,为网络中其他的设备提供时钟同步。而Stratum 2则从Stratum 1获取时间,Stratum 3从Stratum 2获取时间,以此类推。时钟层数的取值范围为1~16,取值越小,时钟准确度越高。层数为1~15的时钟处于同步状态;层数为16的时钟被认为是未同步的,不能使用的。
NTP同步原理
NTP最典型的授时方式是Client/Server方式,如下图所示。
- 客户端首先向服务端发送一个NTP请求报文,其中包含了该报文离开客户端的时间戳t1;
- NTP请求报文到达NTP服务器,此时NTP服务器的时刻为t2。当服务端接收到该报文时,NTP服务器处理之后,于t3时刻发出NTP应答报文。该应答报文中携带报文离开NTP客户端时的时间戳t1、到达NTP服务器时的时间戳t2、离开NTP服务器时的时间戳t3;
- 客户端在接收到响应报文时,记录报文返回的时间戳t4。
- 客户端用上述4个时间戳参数就能够计算出2个关键参数:
- NTP报文从客户端到服务器的往返延迟delay。

- 客户端与服务端之间的时间差offset。
根据方程组:
可以解得时间差为:
NTP客户端根据计算得到的offset来调整自己的时钟,实现与NTP服务器的时钟同步。
AOSP
- 修改时区
在编译的时候比如 device.mk的文件中添加如下 来修改默认时区.
PRODUCT_PROPERTY_OVERRIDES += \
persist.sys.timezone=Asia/Shanghai
- 修改NTP服务器地址:
在 frameworks/base/core/res/res/values/config.xml 文件中 找到
<string translatable="false" name="config_ntpServer">ntp7.aliyun.com</string>
ADB
adb shell "settings put global ntp_server ntp1.aliyun.com"
边栏推荐
- CSRF
- 程序进程管理工具-go supervisor
- AMS Series 1 - AMS startup process
- Solicitation for JGG special issue: spatio-temporal omics
- 英特尔13代酷睿旗舰曝光,单核5.5GHz
- Bi skills - permission axis
- php服务器 与redis交互大量CLOSE_WAIT分析
- 10. Nacos source code construction
- Oracle收回权限 & 创建角色
- Illustrated network: what is virtual router redundancy protocol VRRP?
猜你喜欢

多维度监控:智能监控的数据基础

11. Provider service registration of Nacos service registration source code analysis

封装一个koa分布式锁中间件来解决幂等或重复请求的问题

基于I2C协议的驱动开发
![[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder](/img/1f/729594930c7c97d3e731987f4c3645.png)
[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder

Application of high-precision indoor positioning technology in safety management of smart factory

Leetcode 46: full arrangement

Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)

英特尔13代酷睿旗舰曝光,单核5.5GHz

Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture
随机推荐
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
Internet Socket (非)阻塞write/read n个字节
一文搞懂Go语言Context
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials
11. Provider service registration of Nacos service registration source code analysis
How can UI automated testing get out of trouble? How to embody the value?
How did I grow up in the past eight years as a test engineer of meituan? I hope technicians can gain something after reading it
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
asyncio 警告 DeprecationWarning: There is no current event loop
如何:配置 ClickOnce 信任提示行为
如何成为一名高级数字 IC 设计工程师(1-4)Verilog 编码语法篇:表达式
The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
Processes and threads
[VTK] vtkPolydataToImageStencil 源码解读
Encapsulation attempt of network request framework of retro + kotlin + MVVM
如何让让别人畏惧你
POI excel 单元格换行
Commonly used discrete random distribution