当前位置:网站首页>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"
边栏推荐
- 程序员的创业陷阱:接私活
- Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
- The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
- 00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
- Linear table sequence table comprehensive application problem p18
- Oracle 11g single machine cold standby database
- The element form shows the relationship between elementary transformation and elementary matrix
- 2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func mai
- Lecture 1 number field
- Internet socket (non) blocking write/read n bytes
猜你喜欢
多维度监控:智能监控的数据基础
The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but
Expandablelistview that can expand and shrink (imitating the list page of professional selection of Zhilian recruitment)
ASP.NET-酒店管理系統
数据库增量备份 - DB INCR DB FULL
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials
ASP. Net hotel management system
软考中级软件设计师该怎么备考
基于I2C协议的驱动开发
随机推荐
VPP three-layer network interconnection configuration
EPS电动转向系统分析
按键切换:按F1-F12都需要按Fn
php如何解决高并发问题
Oracle收回权限 & 创建角色
One hot code
线性表顺序表综合应用题P18
"Core values of testing" and "super complete learning guide for 0 basic software testing" summarized by test engineers for 8 years
Incremental database backup - DB incr DB full
Project management essence reading notes (6)
Processes and threads
AMS series - application startup process
Android log system
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
基于I2C协议的驱动开发
如何:配置 ClickOnce 信任提示行为
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
AIDL
反正切熵(Arctangent entropy):2022.7月最新SCI论文
Reading notes: heart like Bodhi, Cao Dewang