blob: 04bc3eed99e15e12fea555a94e57f2a5abfc2de4 [file] [log] [blame]
Alex Deymo23949d42014-02-05 15:20:59 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/evaluation_context.h"
Alex Deymo23949d42014-02-05 15:20:59 -08006
Gilad Arnoldf9f85d62014-06-19 18:07:01 -07007#include <algorithm>
David Zeuthenc1490282014-04-29 16:25:03 -07008#include <string>
9
Alex Deymo53556ec2014-03-17 10:05:57 -070010#include <base/bind.h>
David Zeuthenc1490282014-04-29 16:25:03 -070011#include <base/json/json_writer.h>
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -070012#include <base/strings/string_util.h>
David Zeuthenc1490282014-04-29 16:25:03 -070013#include <base/values.h>
14
15#include "update_engine/utils.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070016
17using base::Closure;
Alex Deymo41a75a72014-04-15 15:36:22 -070018using base::Time;
Alex Deymo23949d42014-02-05 15:20:59 -080019using base::TimeDelta;
Alex Deymo41a75a72014-04-15 15:36:22 -070020using chromeos_update_engine::ClockInterface;
David Zeuthenc1490282014-04-29 16:25:03 -070021using std::string;
Alex Deymo23949d42014-02-05 15:20:59 -080022
Gilad Arnolda65fced2014-07-23 09:01:31 -070023namespace {
24
25// Returns whether |curr_time| surpassed |ref_time|; if not, also checks whether
26// |ref_time| is sooner than the current value of |*reeval_time|, in which case
27// the latter is updated to the former.
28bool IsTimeGreaterThanHelper(base::Time ref_time, base::Time curr_time,
29 base::Time* reeval_time) {
30 if (curr_time > ref_time)
31 return true;
32 // Remember the nearest reference we've checked against in this evaluation.
33 if (*reeval_time > ref_time)
34 *reeval_time = ref_time;
35 return false;
36}
37
38// If |expires| never happens (maximal value), returns the maximal interval;
39// otherwise, returns the difference between |expires| and |curr|.
40TimeDelta GetTimeout(base::Time curr, base::Time expires) {
41 if (expires.is_max())
42 return TimeDelta::Max();
43 return expires - curr;
44}
45
46} // namespace
47
Alex Deymo63784a52014-05-28 10:46:14 -070048namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080049
Gilad Arnoldb2271992014-06-19 12:35:24 -070050EvaluationContext::EvaluationContext(ClockInterface* clock,
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070051 TimeDelta evaluation_timeout,
52 TimeDelta expiration_timeout)
Alex Deymo41a75a72014-04-15 15:36:22 -070053 : clock_(clock),
Gilad Arnoldb2271992014-06-19 12:35:24 -070054 evaluation_timeout_(evaluation_timeout),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070055 expiration_timeout_(expiration_timeout),
Alex Deymo41a75a72014-04-15 15:36:22 -070056 weak_ptr_factory_(this) {
57 ResetEvaluation();
Gilad Arnoldfd45a732014-08-07 15:53:46 -070058 ResetExpiration();
Alex Deymo41a75a72014-04-15 15:36:22 -070059}
60
Alex Deymo53556ec2014-03-17 10:05:57 -070061EvaluationContext::~EvaluationContext() {
62 RemoveObserversAndTimeout();
63}
64
65void EvaluationContext::RemoveObserversAndTimeout() {
66 for (auto& it : value_cache_) {
67 if (it.first->GetMode() == kVariableModeAsync)
68 it.first->RemoveObserver(this);
69 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070070 CancelMainLoopEvent(timeout_event_);
71 timeout_event_ = kEventIdNull;
Alex Deymo53556ec2014-03-17 10:05:57 -070072}
73
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070074TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const {
75 if (monotonic_deadline.is_max())
76 return TimeDelta::Max();
77 TimeDelta remaining = monotonic_deadline - clock_->GetMonotonicTime();
78 return std::max(remaining, TimeDelta());
79}
80
81Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) {
82 return (timeout.is_max() ? Time::Max() :
83 clock_->GetMonotonicTime() + timeout);
Alex Deymo23949d42014-02-05 15:20:59 -080084}
85
Alex Deymo53556ec2014-03-17 10:05:57 -070086void EvaluationContext::ValueChanged(BaseVariable* var) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070087 DLOG(INFO) << "ValueChanged() called for variable " << var->GetName();
88 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -070089}
90
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070091void EvaluationContext::OnTimeout() {
92 DLOG(INFO) << "OnTimeout() called due to "
93 << (timeout_marks_expiration_ ? "expiration" : "poll interval");
94 timeout_event_ = kEventIdNull;
95 is_expired_ = timeout_marks_expiration_;
96 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -070097}
98
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070099void EvaluationContext::OnValueChangedOrTimeout() {
Alex Deymo53556ec2014-03-17 10:05:57 -0700100 RemoveObserversAndTimeout();
Alex Deymo41a75a72014-04-15 15:36:22 -0700101
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700102 // Copy the callback handle locally, allowing it to be reassigned.
103 scoped_ptr<Closure> callback(callback_.release());
104
105 if (callback.get())
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700106 callback->Run();
Alex Deymo41a75a72014-04-15 15:36:22 -0700107}
108
Gilad Arnolda65fced2014-07-23 09:01:31 -0700109bool EvaluationContext::IsWallclockTimeGreaterThan(base::Time timestamp) {
110 return IsTimeGreaterThanHelper(timestamp, evaluation_start_wallclock_,
111 &reevaluation_time_wallclock_);
112}
113
114bool EvaluationContext::IsMonotonicTimeGreaterThan(base::Time timestamp) {
115 return IsTimeGreaterThanHelper(timestamp, evaluation_start_monotonic_,
116 &reevaluation_time_monotonic_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700117}
118
119void EvaluationContext::ResetEvaluation() {
Gilad Arnolda65fced2014-07-23 09:01:31 -0700120 evaluation_start_wallclock_ = clock_->GetWallclockTime();
121 evaluation_start_monotonic_ = clock_->GetMonotonicTime();
122 reevaluation_time_wallclock_ = Time::Max();
123 reevaluation_time_monotonic_ = Time::Max();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700124 evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700125
Alex Deymo53556ec2014-03-17 10:05:57 -0700126 // Remove the cached values of non-const variables
127 for (auto it = value_cache_.begin(); it != value_cache_.end(); ) {
128 if (it->first->GetMode() == kVariableModeConst) {
129 ++it;
130 } else {
131 it = value_cache_.erase(it);
132 }
133 }
Alex Deymo53556ec2014-03-17 10:05:57 -0700134}
135
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700136void EvaluationContext::ResetExpiration() {
137 expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_);
138 is_expired_ = false;
139}
140
Alex Deymo53556ec2014-03-17 10:05:57 -0700141bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700142 // Check that the method was not called more than once.
143 if (callback_.get() != nullptr) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700144 LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once.";
145 return false;
146 }
147
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700148 // Check that the context did not yet expire.
149 if (is_expired()) {
150 LOG(ERROR) << "RunOnValueChangeOrTimeout called on an expired context.";
151 return false;
152 }
153
Gilad Arnolda65fced2014-07-23 09:01:31 -0700154 // Handle reevaluation due to a Is{Wallclock,Monotonic}TimeGreaterThan(). We
155 // choose the smaller of the differences between evaluation start time and
156 // reevaluation time among the wallclock and monotonic scales.
157 TimeDelta timeout = std::min(
158 GetTimeout(evaluation_start_wallclock_, reevaluation_time_wallclock_),
159 GetTimeout(evaluation_start_monotonic_, reevaluation_time_monotonic_));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700160
161 // Handle reevaluation due to async or poll variables.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700162 bool waiting_for_value_change = false;
Alex Deymo53556ec2014-03-17 10:05:57 -0700163 for (auto& it : value_cache_) {
164 switch (it.first->GetMode()) {
165 case kVariableModeAsync:
Alex Deymo53556ec2014-03-17 10:05:57 -0700166 DLOG(INFO) << "Waiting for value on " << it.first->GetName();
167 it.first->AddObserver(this);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700168 waiting_for_value_change = true;
Alex Deymo53556ec2014-03-17 10:05:57 -0700169 break;
170 case kVariableModePoll:
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700171 timeout = std::min(timeout, it.first->GetPollInterval());
Alex Deymo53556ec2014-03-17 10:05:57 -0700172 break;
173 case kVariableModeConst:
174 // Ignored.
175 break;
176 }
177 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700178
Alex Deymo53556ec2014-03-17 10:05:57 -0700179 // Check if the re-evaluation is actually being scheduled. If there are no
180 // events waited for, this function should return false.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700181 if (!waiting_for_value_change && timeout.is_max())
Alex Deymo53556ec2014-03-17 10:05:57 -0700182 return false;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700183
184 // Ensure that we take into account the expiration timeout.
185 TimeDelta expiration = RemainingTime(expiration_monotonic_deadline_);
186 timeout_marks_expiration_ = expiration < timeout;
187 if (timeout_marks_expiration_)
188 timeout = expiration;
189
190 // Store the reevaluation callback.
191 callback_.reset(new Closure(callback));
192
193 // Schedule a timeout event, if one is set.
194 if (!timeout.is_max()) {
195 DLOG(INFO) << "Waiting for timeout in "
196 << chromeos_update_engine::utils::FormatTimeDelta(timeout);
197 timeout_event_ = RunFromMainLoopAfterTimeout(
198 base::Bind(&EvaluationContext::OnTimeout,
Alex Deymodb799532014-03-21 13:00:00 -0700199 weak_ptr_factory_.GetWeakPtr()),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700200 timeout);
Alex Deymo53556ec2014-03-17 10:05:57 -0700201 }
202
Alex Deymo53556ec2014-03-17 10:05:57 -0700203 return true;
204}
205
David Zeuthenc1490282014-04-29 16:25:03 -0700206string EvaluationContext::DumpContext() const {
207 base::DictionaryValue* variables = new base::DictionaryValue();
208 for (auto& it : value_cache_) {
209 variables->SetString(it.first->GetName(), it.second.ToString());
210 }
211
212 base::DictionaryValue value;
213 value.Set("variables", variables); // Adopts |variables|.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700214 value.SetString(
215 "evaluation_start_wallclock",
216 chromeos_update_engine::utils::ToString(evaluation_start_wallclock_));
217 value.SetString(
218 "evaluation_start_monotonic",
219 chromeos_update_engine::utils::ToString(evaluation_start_monotonic_));
David Zeuthenc1490282014-04-29 16:25:03 -0700220
221 string json_str;
222 base::JSONWriter::WriteWithOptions(&value,
223 base::JSONWriter::OPTIONS_PRETTY_PRINT,
224 &json_str);
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -0700225 base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str);
David Zeuthenc1490282014-04-29 16:25:03 -0700226
227 return json_str;
228}
229
Alex Deymo63784a52014-05-28 10:46:14 -0700230} // namespace chromeos_update_manager