当前位置:网站首页>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"
边栏推荐
- Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
- 用了这么久线程池,你真的知道如何合理配置线程数吗?
- 浅析-JMM内存模型
- Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
- EPS电动转向系统分析
- Bi skills - permission axis
- 大厂技术专家:工程师如何提升沟通能力?
- Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
- Project management essence reading notes (6)
- "Core values of testing" and "super complete learning guide for 0 basic software testing" summarized by test engineers for 8 years
猜你喜欢

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

如何清理v$rman_backup_job_details视图 报错ORA-02030

Tencent micro app to get wechat user information

FL Studio 20 unlimited trial fruit arranger Download

用了这么久线程池,你真的知道如何合理配置线程数吗?

(2) Base

LeetCode 46:全排列

Stack, monotone stack, queue, monotone queue

A simple method of adding dividing lines in recyclerview

面试题总结(2) IO模型,集合,NIO 原理,缓存穿透,击穿雪崩
随机推荐
A simple method of adding dividing lines in recyclerview
VPP three-layer network interconnection configuration
IIS does not take effect after modifying the configuration information
FL Studio 20无限试用版水果编曲下载
Bi skills - permission axis
在腾讯云容器服务Node上执行 kubectl
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
[vtk] source code interpretation of vtkpolydatatoimagestencil
Static library vs shared library
BI技巧丨权限轴
Commonly used discrete random distribution
读书笔记:《心若菩提》 曹德旺
Technical experts from large factories: how can engineers improve their communication skills?
Intel 13th generation core flagship exposure, single core 5.5ghz
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
Processes and threads
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
Application of high-precision indoor positioning technology in safety management of smart factory
ASP. Net hotel management system