4. 时间同步 - wuyaxiezitai/OpenPTrack-notebook GitHub Wiki

这一部分包括如何安装标准的网络时间控制(NTP),并且描述了在我们的测试台中工作的两个不同的NTP配置

NTP

两个节点之间的时间控制对于OpenPTrack的工作起着决定性的作用。NTPD已经足够满足此要求。

当前,检测必须具有不超过一个视频帧的窗口(在30 fps下,约33毫秒),理想情况下,要生成有效的跟踪,则要少得多。在实际系统里,我们的目标是在校准或跟踪之前,NTP检测出少于10毫秒的偏移和抖动。

主机不同步的常见症状是拆分(指一个人显示出两个不同位置)。

时间同步

OpenPTrack多摄像头的整合需要每一个主机的CPU都做到时间同步,对于目标平台(Ubuntu)来说,在接下来的配置中建议使用NTP。一旦某一台及其被选为服务器,其他的CPU都要同步这一台机器的时间。

首先,安装NTP:

sudo apt-get install ntp

然后,编辑客户端和服务器的配置文件。

N.B.:我们已经成功的使用两种NTP的配置。第一种配置用于较小的网络(5个或者更少的CPU)。这种配置也能够快速的统一每个CPU上的时间,偏移能够快速收缩。第二种配置方法用于更大的网络,例如多于5个CPU的网络。我们发现这种配置方式能够更稳定的运行在大的网络中,偏移基本不会出现巨大变化,但是网络中的时间同步需要花费更多的时间。

配置1

server:/etc/ntp.conf:

filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
#server 0.ubuntu.pool.ntp.org
#server 1.ubuntu.pool.ntp.org
#server 2.ubuntu.pool.ntp.org
#server 3.ubuntu.pool.ntp.org
#server 0.north-america.pool.ntp.org iburst
server ntp.ucla.edu
server 127.127.1.0
fudge 127.127.1.0 stratum 10


# Use Ubuntu's ntp server as a fallback.
#server ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page 
<http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
#restrict -4 default kod notrap nomodify nopeer noquery
#restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
#restrict 127.0.0.1
#restrict ::1
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust

# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
broadcast 192.168.100.255

对于要同步到此主服务器上的其他节点,如果主节点未联机,请注释掉第一个外部服务器。请注意,这里假定网络为 192.168.100.X(如果必要,更改使其匹配网络)。

client:/etc/ntp.cof: 用户端有很多其他的“资料”需要移动。某些设置会干扰将从属/客户端同步到LAN上的主机的最终简单目标。为此,将 /etc/ntp.conf 文件的内容替换为下列内容:

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile /var/lib/ntp/ntp.drift
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# Specify one or more NTP servers.
server 192.168.100.101 iburst
disable auth
broadcastclient

192.168.100.101必须改成选好的主机/LAN 时间服务器的IP

客户端 ntp.conf 需要增加其他非主机节点。

配置2

server: /etc/ntp.conf:

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.

statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.

#server 64.67.62.194 iburst

# Do not use a low minpoll/maxpoll, to keep things typical to outside world
server time1.ucla.edu iburst
server time2.ucla.edu iburst

peer 192.168.100.102 minpoll 4 maxpoll 6 iburst
peer 192.168.100.103 minpoll 4 maxpoll 6 iburst
peer 192.168.100.104 minpoll 4 maxpoll 6 iburst
peer 192.168.100.105 minpoll 4 maxpoll 6 iburst
peer 192.168.100.106 minpoll 4 maxpoll 6 iburst

# We are the master, so listen on the subnet but no modification
restrict 192.168.100.0 mask 255.255.255.0 

# Local users may interrogate the ntp server more closely.
# Needed for ntpq if we use restrict default ignore
restrict 127.0.0.1
restrict ::1

N.B.:在上述配置中,三个部分的代码需要修改:

部分1

# Do not use a low minpoll/maxpoll, to keep things typical to outside world
server time1.ucla.edu iburst
server time2.ucla.edu iburst

两个UCLA NTP服务器需要改为最接近安装实际位置的NTP服务器。

部分2

peer 192.168.100.102 minpoll 4 maxpoll 6 iburst
peer 192.168.100.103 minpoll 4 maxpoll 6 iburst
peer 192.168.100.104 minpoll 4 maxpoll 6 iburst
peer 192.168.100.105 minpoll 4 maxpoll 6 iburst
peer 192.168.100.106 minpoll 4 maxpoll 6 iburst

这一部分会显示出你网络中的没有作为服务器的CPU。上述代码表示,这个网络中有6个CPU,主机的IP地址为192.168.100.101;其他的CPU的IP地址为192.168.102-.106。

部分3

# We are the master, so listen on the subnet but no modification
restrict 192.168.100.0 mask 255.255.255.0 

192.168.100.0表示你设置的网络的子网络,如果你的子网络是.0,你的代码将会变为:

# We are the master, so listen on the subnet but no modification
restrict 192.168.0.0 mask 255.255.255.0 

client:/etc/ntp.conf:

#/etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Restrict what we listen to
restrict default ignore

# We are a peer so allow devices on the subnet to modify
restrict 192.168.100.0 mask 255.255.255.0

# Local users may interrogate the ntp server more closely.
# Needed for ntpq if we use restrict default ignore
restrict 127.0.0.1
restrict ::1

# Sync with our peers
## NOTE THIS MUST BE EDITED PER MACHINE
## TO LIST OTHER PEERS, THIS IS AN EXAMPLE
## FOR 102
peer 192.168.100.101 minpoll 4 maxpoll 6 iburst
peer 192.168.100.103 minpoll 4 maxpoll 6 iburst
peer 192.168.100.104 minpoll 4 maxpoll 6 iburst
peer 192.168.100.105 minpoll 4 maxpoll 6 iburst
peer 192.168.100.106 minpoll 4 maxpoll 6 iburst

N.B.:同样,这里也有两个地方需要变动:

变动1

# We are the master, so listen on the subnet but no modification
restrict 192.168.100.0 mask 255.255.255.0 

192.168.100.0表示你设置的网络的子网络,如果你的子网络是.0,你的代码将会变为:

# We are the master, so listen on the subnet but no modification
restrict 192.168.0.0 mask 255.255.255.0

变动2

peer 192.168.100.102 minpoll 4 maxpoll 6 iburst
peer 192.168.100.103 minpoll 4 maxpoll 6 iburst
peer 192.168.100.104 minpoll 4 maxpoll 6 iburst
peer 192.168.100.105 minpoll 4 maxpoll 6 iburst
peer 192.168.100.106 minpoll 4 maxpoll 6 iburst

这一部分会显示出你网络中的没有作为服务器的CPU。上述代码表示,这个网络中有6个CPU,主机的IP地址为192.168.100.101;其他的CPU的IP地址为192.168.102-.106。

配置完成!但是为了确保时间同步(在配置完成之后,这时间同步只会在配置1中起作用):

NTP调整是渐进的,但是 iburst 设置将收敛速度加快到了5-10秒,如果很急的话可以试试:

sudo service ntp stop
sudo ntpd -gq
sudo service ntp start

同时还有一个选做的操作,每次启动系统自动启动(这将增加几秒钟的启动时间),在 /etc/rc.local 中:

/etc/init.d/ntp stop
until ping -nq -c3 8.8.8.8; do
echo "Waiting for network..."
done
ntpd -gq
/etc/init.d/ntp start )&

要验证NTP是否收敛

配置1的验证方法

无论如何,为了证实时间同步,在每个主机上敲:

ntpq --peers

主控应该显示如下,或类似内容:

remote       	refid  	st t when poll reach   delay   offset  jitter
==============================================================================
*0.north-america.p  .GPS.        	1 u	8   64  377   25.118   -2.756   
2.843
LOCAL(0)    	.LOCL.      	10 l 1164   64	0	0.000	0.000   0.000
 192.168.100.255 .BCST.      	16 u	-   64	0	0.000	0.000   0.000

其他主机应该显示如下或类似内容:

	remote       	refid  	st t when poll reach   delay   offset  jitter
==============================================================================
*opt-node-4.loca 164.67.62.199	2 u	2   64   37	0.078   -1.867   1.670

配置2的验证方法

一旦服务器运行:

ntpq -p

主控将会显示如下或类似内容:

         remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+time1.ucla.edu  .GPS.            1 u   13   64    7    1.017    1.382   0.132
*time2.ucla.edu  .GPS.            1 u   13   64    7    0.677    1.292   0.159
 o2              192.168.100.101  3 u   18   16    2    2.745  -24.158  62.103
 o3              192.168.100.101  3 u    1   16    0    0.000    0.000   0.000
 o4              192.168.100.101  3 u   21   16    2    0.389   65.593  33.983
 o5              192.168.100.101  3 u    4   16   77    4.893   -5.649  54.487
 o6              192.168.100.101  3 u    6   16   76    0.357   25.253  15.913

节点现在就已经同步了时间同步是很有必要的,时间同步越好,追踪的效果也就越好。