当前位置:网站首页>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"
边栏推荐
- 如何清理v$rman_backup_job_details视图 报错ORA-02030
- [vtk] source code interpretation of vtkpolydatatoimagestencil
- Commonly used discrete random distribution
- 按键切换:按F1-F12都需要按Fn
- [proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder
- CSRF
- Lecture 1 number field
- How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression
- 一文搞懂Go语言Context
- Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
猜你喜欢

(2) Base

Driver development based on I2C protocol

面试题总结(2) IO模型,集合,NIO 原理,缓存穿透,击穿雪崩

The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years

Processes and threads

数据库增量备份 - DB INCR DB FULL

活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点

Matlab extracts numerical data from irregular txt files (simple and practical)

php服务器 与redis交互大量CLOSE_WAIT分析

A simple method of adding dividing lines in recyclerview
随机推荐
[vtk] source code interpretation of vtkpolydatatoimagestencil
Ext file system mechanism principle
How to become a senior digital IC Design Engineer (1-5) Verilog coding syntax: operand
[OBS] configFile in ini format of OBS
IIS does not take effect after modifying the configuration information
LeetCode 46:全排列
(二)进制
Technical experts from large factories: how can engineers improve their communication skills?
封装一个koa分布式锁中间件来解决幂等或重复请求的问题
CSRF
Processes and threads
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
Function details of CorelDRAW graphics suite 2022
Solutions of n-ary linear equations and their criteria
Struct function & function pointer
CorelDRAW Graphics Suite 2022新版功能详情介绍
用了这么久线程池,你真的知道如何合理配置线程数吗?
Arctangent entropy: the latest SCI paper in July 2022