blob: 575f83acfc74420a21a1d7b0dd8167098c24792c [file] [log] [blame]
Jatin Lodhiad70777b2015-08-19 16:59:47 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package clock
6
7import (
8 "math"
9 "time"
10
Jatin Lodhiad70777b2015-08-19 16:59:47 -070011 "v.io/v23/context"
12 "v.io/v23/verror"
13 "v.io/x/lib/vlog"
Adam Sadovskyf2efeb52015-08-31 14:17:49 -070014 "v.io/x/ref/services/syncbase/server/util"
Jatin Lodhiad70777b2015-08-19 16:59:47 -070015)
16
17// This file contains code related to checking current system clock to see
18// if it has been changed by any external action.
19
20// runClockCheck estimates the current system time based on saved boottime
21// and elapsed time since boot and checks if the system clock shows the same
22// time. This involves the following steps:
23// 1) Check if system was rebooted since last run. If so update the saved
24// ClockData.
25// 2) Fetch stored ClockData. If none exists, this is the first time
26// runClockCheck has been run. Write new ClockData.
27// 3) Estimate current system clock time and check if the actual system clock
28// agrees with the estimation. If not update the skew value appropriately.
29// 4) Update saved elapsed time since boot. This is used to check if the system
30// was rebooted or not. TODO(jlodhia): work with device manager to provide a
31// way to notify syncbase if the system was just rebooted.
32func (c *VClock) runClockCheck(ctx *context.T) {
33 checkSystemRebooted(ctx, c)
34
35 clockData := &ClockData{}
36 if err := c.sa.GetClockData(ctx, clockData); err != nil {
37 if verror.ErrorID(err) == verror.ErrNoExist.ID {
38 // VClock's cron job to setup UTC time at boot is being run for the
39 // first time. Skew is not known, hence assigning 0.
40 writeNewClockData(ctx, c, 0)
41 } else {
42 vlog.Errorf("Error while fetching clock data: %v", err)
43 }
44 return
45 }
46
47 systemTime := c.clock.Now()
48 elapsedTime, err := c.clock.ElapsedTime()
49 if err != nil {
50 vlog.Errorf("Error while fetching elapsed time: %v", err)
51 return
52 }
53
54 newClockData := &ClockData{
55 SystemTimeAtBoot: clockData.SystemTimeAtBoot,
56 Skew: clockData.Skew,
57 ElapsedTimeSinceBoot: elapsedTime.Nanoseconds(),
58 }
59
60 estimatedClockTime := clockData.SystemBootTime().Add(elapsedTime)
61 diff := estimatedClockTime.Sub(systemTime)
62 if math.Abs(float64(diff.Nanoseconds())) > util.LocalClockDriftThreshold {
63 newClockData.Skew = newClockData.Skew + diff.Nanoseconds()
64 newSystemTimeAtBoot := systemTime.Add(-elapsedTime)
65 newClockData.SystemTimeAtBoot = newSystemTimeAtBoot.UnixNano()
66 }
67
68 if err := c.sa.SetClockData(ctx, newClockData); err != nil {
69 vlog.Errorf("Error while setting clock data: %v", err)
70 }
71}
72
73func writeNewClockData(ctx *context.T, c *VClock, skew time.Duration) {
74 systemTime := c.clock.Now()
75 elapsedTime, err := c.clock.ElapsedTime()
76 if err != nil {
77 vlog.Errorf("Error while fetching elapsed time: %v", err)
78 return
79 }
80 systemTimeAtBoot := systemTime.Add(-elapsedTime)
81 clockData := &ClockData{
82 SystemTimeAtBoot: systemTimeAtBoot.UnixNano(),
83 Skew: skew.Nanoseconds(),
84 ElapsedTimeSinceBoot: elapsedTime.Nanoseconds(),
85 }
86 if err := c.sa.SetClockData(ctx, clockData); err != nil {
87 vlog.Errorf("Error while setting clock data: %v", err)
88 }
89}
90
91// checkSystemRebooted compares the elapsed time stored during the last
92// run of runClockCheck() to the current elapsed time since boot provided
93// by system clock. Since elapsed time is monotonically increasing and cannot
94// be changed unless a reboot happens, if the current value is lower than the
95// previous value then a reboot has happened since last run. If so, update
96// the boot time and elapsed time since boot appropriately.
97func checkSystemRebooted(ctx *context.T, c *VClock) bool {
98 currentSysTime := c.clock.Now()
99 elapsedTime, err := c.clock.ElapsedTime()
100 if err != nil {
101 vlog.Errorf("Error while fetching elapsed time: %v", err)
102 return false
103 }
104
105 clockData := &ClockData{}
106 if err := c.sa.GetClockData(ctx, clockData); err != nil {
107 if verror.ErrorID(err) != verror.ErrNoExist.ID {
108 vlog.Errorf("Error while fetching clock delta: %v", err)
109 }
110 // In case of verror.ErrNoExist no clock data present. Nothing needed to
111 // be done. writeNewClockData() will write new clock data to storage.
112 return false
113 }
114
115 if elapsedTime.Nanoseconds() < clockData.ElapsedTimeSinceBoot {
116 // Since the elapsed time since last boot provided by the system is
117 // less than the elapsed time since boot seen the last time clockservice
118 // ran, the system must have rebooted in between.
119 clockData.SystemTimeAtBoot = currentSysTime.Add(-elapsedTime).UnixNano()
120 clockData.ElapsedTimeSinceBoot = elapsedTime.Nanoseconds()
121 if err := c.sa.SetClockData(ctx, clockData); err != nil {
122 vlog.Errorf("Error while setting clock data: %v", err)
123 }
124 return true
125 }
126 return false
127}