blob: 9ab67714d44f19030c2e9613ba1a1ded4bb45c81 [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>
Ben Chan02f7c1d2014-10-18 15:18:02 -07008#include <memory>
David Zeuthenc1490282014-04-29 16:25:03 -07009#include <string>
10
Alex Deymo53556ec2014-03-17 10:05:57 -070011#include <base/bind.h>
David Zeuthenc1490282014-04-29 16:25:03 -070012#include <base/json/json_writer.h>
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -070013#include <base/strings/string_util.h>
David Zeuthenc1490282014-04-29 16:25:03 -070014#include <base/values.h>
15
16#include "update_engine/utils.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070017
Gilad Arnold83ffdda2014-08-08 13:30:31 -070018using base::Callback;
Alex Deymo53556ec2014-03-17 10:05:57 -070019using base::Closure;
Alex Deymo41a75a72014-04-15 15:36:22 -070020using base::Time;
Alex Deymo23949d42014-02-05 15:20:59 -080021using base::TimeDelta;
Alex Deymo41a75a72014-04-15 15:36:22 -070022using chromeos_update_engine::ClockInterface;
David Zeuthenc1490282014-04-29 16:25:03 -070023using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070024using std::unique_ptr;
Alex Deymo23949d42014-02-05 15:20:59 -080025
Gilad Arnolda65fced2014-07-23 09:01:31 -070026namespace {
27
28// Returns whether |curr_time| surpassed |ref_time|; if not, also checks whether
29// |ref_time| is sooner than the current value of |*reeval_time|, in which case
30// the latter is updated to the former.
31bool IsTimeGreaterThanHelper(base::Time ref_time, base::Time curr_time,
32 base::Time* reeval_time) {
33 if (curr_time > ref_time)
34 return true;
35 // Remember the nearest reference we've checked against in this evaluation.
36 if (*reeval_time > ref_time)
37 *reeval_time = ref_time;
38 return false;
39}
40
41// If |expires| never happens (maximal value), returns the maximal interval;
42// otherwise, returns the difference between |expires| and |curr|.
43TimeDelta GetTimeout(base::Time curr, base::Time expires) {
44 if (expires.is_max())
45 return TimeDelta::Max();
46 return expires - curr;
47}
48
49} // namespace
50
Alex Deymo63784a52014-05-28 10:46:14 -070051namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080052
Gilad Arnold83ffdda2014-08-08 13:30:31 -070053EvaluationContext::EvaluationContext(
54 ClockInterface* clock,
55 TimeDelta evaluation_timeout,
56 TimeDelta expiration_timeout,
Ben Chan02f7c1d2014-10-18 15:18:02 -070057 unique_ptr<Callback<void(EvaluationContext*)>> unregister_cb)
Alex Deymo41a75a72014-04-15 15:36:22 -070058 : clock_(clock),
Gilad Arnoldb2271992014-06-19 12:35:24 -070059 evaluation_timeout_(evaluation_timeout),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070060 expiration_timeout_(expiration_timeout),
Ben Chan02f7c1d2014-10-18 15:18:02 -070061 unregister_cb_(std::move(unregister_cb)),
Alex Deymo41a75a72014-04-15 15:36:22 -070062 weak_ptr_factory_(this) {
63 ResetEvaluation();
Gilad Arnoldfd45a732014-08-07 15:53:46 -070064 ResetExpiration();
Alex Deymo41a75a72014-04-15 15:36:22 -070065}
66
Alex Deymo53556ec2014-03-17 10:05:57 -070067EvaluationContext::~EvaluationContext() {
68 RemoveObserversAndTimeout();
Gilad Arnold83ffdda2014-08-08 13:30:31 -070069 if (unregister_cb_.get())
70 unregister_cb_->Run(this);
Alex Deymo53556ec2014-03-17 10:05:57 -070071}
72
Ben Chan02f7c1d2014-10-18 15:18:02 -070073unique_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() {
Alex Deymo53556ec2014-03-17 10:05:57 -070074 for (auto& it : value_cache_) {
75 if (it.first->GetMode() == kVariableModeAsync)
76 it.first->RemoveObserver(this);
77 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070078 CancelMainLoopEvent(timeout_event_);
79 timeout_event_ = kEventIdNull;
Gilad Arnold83ffdda2014-08-08 13:30:31 -070080
Ben Chan02f7c1d2014-10-18 15:18:02 -070081 return unique_ptr<Closure>(callback_.release());
Alex Deymo53556ec2014-03-17 10:05:57 -070082}
83
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070084TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const {
85 if (monotonic_deadline.is_max())
86 return TimeDelta::Max();
87 TimeDelta remaining = monotonic_deadline - clock_->GetMonotonicTime();
88 return std::max(remaining, TimeDelta());
89}
90
91Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) {
92 return (timeout.is_max() ? Time::Max() :
93 clock_->GetMonotonicTime() + timeout);
Alex Deymo23949d42014-02-05 15:20:59 -080094}
95
Alex Deymo53556ec2014-03-17 10:05:57 -070096void EvaluationContext::ValueChanged(BaseVariable* var) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070097 DLOG(INFO) << "ValueChanged() called for variable " << var->GetName();
98 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -070099}
100
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700101void EvaluationContext::OnTimeout() {
102 DLOG(INFO) << "OnTimeout() called due to "
103 << (timeout_marks_expiration_ ? "expiration" : "poll interval");
104 timeout_event_ = kEventIdNull;
105 is_expired_ = timeout_marks_expiration_;
106 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700107}
108
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700109void EvaluationContext::OnValueChangedOrTimeout() {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700110 // Copy the callback handle locally, allowing it to be reassigned.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700111 unique_ptr<Closure> callback = RemoveObserversAndTimeout();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700112
113 if (callback.get())
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700114 callback->Run();
Alex Deymo41a75a72014-04-15 15:36:22 -0700115}
116
Gilad Arnolda65fced2014-07-23 09:01:31 -0700117bool EvaluationContext::IsWallclockTimeGreaterThan(base::Time timestamp) {
118 return IsTimeGreaterThanHelper(timestamp, evaluation_start_wallclock_,
119 &reevaluation_time_wallclock_);
120}
121
122bool EvaluationContext::IsMonotonicTimeGreaterThan(base::Time timestamp) {
123 return IsTimeGreaterThanHelper(timestamp, evaluation_start_monotonic_,
124 &reevaluation_time_monotonic_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700125}
126
127void EvaluationContext::ResetEvaluation() {
Gilad Arnolda65fced2014-07-23 09:01:31 -0700128 evaluation_start_wallclock_ = clock_->GetWallclockTime();
129 evaluation_start_monotonic_ = clock_->GetMonotonicTime();
130 reevaluation_time_wallclock_ = Time::Max();
131 reevaluation_time_monotonic_ = Time::Max();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700132 evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700133
Alex Deymo53556ec2014-03-17 10:05:57 -0700134 // Remove the cached values of non-const variables
135 for (auto it = value_cache_.begin(); it != value_cache_.end(); ) {
136 if (it->first->GetMode() == kVariableModeConst) {
137 ++it;
138 } else {
139 it = value_cache_.erase(it);
140 }
141 }
Alex Deymo53556ec2014-03-17 10:05:57 -0700142}
143
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700144void EvaluationContext::ResetExpiration() {
145 expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_);
146 is_expired_ = false;
147}
148
Alex Deymo53556ec2014-03-17 10:05:57 -0700149bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700150 // Check that the method was not called more than once.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700151 if (callback_.get()) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700152 LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once.";
153 return false;
154 }
155
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700156 // Check that the context did not yet expire.
157 if (is_expired()) {
158 LOG(ERROR) << "RunOnValueChangeOrTimeout called on an expired context.";
159 return false;
160 }
161
Gilad Arnolda65fced2014-07-23 09:01:31 -0700162 // Handle reevaluation due to a Is{Wallclock,Monotonic}TimeGreaterThan(). We
163 // choose the smaller of the differences between evaluation start time and
164 // reevaluation time among the wallclock and monotonic scales.
165 TimeDelta timeout = std::min(
166 GetTimeout(evaluation_start_wallclock_, reevaluation_time_wallclock_),
167 GetTimeout(evaluation_start_monotonic_, reevaluation_time_monotonic_));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700168
169 // Handle reevaluation due to async or poll variables.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700170 bool waiting_for_value_change = false;
Alex Deymo53556ec2014-03-17 10:05:57 -0700171 for (auto& it : value_cache_) {
172 switch (it.first->GetMode()) {
173 case kVariableModeAsync:
Alex Deymo53556ec2014-03-17 10:05:57 -0700174 DLOG(INFO) << "Waiting for value on " << it.first->GetName();
175 it.first->AddObserver(this);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700176 waiting_for_value_change = true;
Alex Deymo53556ec2014-03-17 10:05:57 -0700177 break;
178 case kVariableModePoll:
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700179 timeout = std::min(timeout, it.first->GetPollInterval());
Alex Deymo53556ec2014-03-17 10:05:57 -0700180 break;
181 case kVariableModeConst:
182 // Ignored.
183 break;
184 }
185 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700186
Alex Deymo53556ec2014-03-17 10:05:57 -0700187 // Check if the re-evaluation is actually being scheduled. If there are no
188 // events waited for, this function should return false.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700189 if (!waiting_for_value_change && timeout.is_max())
Alex Deymo53556ec2014-03-17 10:05:57 -0700190 return false;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700191
192 // Ensure that we take into account the expiration timeout.
193 TimeDelta expiration = RemainingTime(expiration_monotonic_deadline_);
194 timeout_marks_expiration_ = expiration < timeout;
195 if (timeout_marks_expiration_)
196 timeout = expiration;
197
198 // Store the reevaluation callback.
199 callback_.reset(new Closure(callback));
200
201 // Schedule a timeout event, if one is set.
202 if (!timeout.is_max()) {
203 DLOG(INFO) << "Waiting for timeout in "
204 << chromeos_update_engine::utils::FormatTimeDelta(timeout);
205 timeout_event_ = RunFromMainLoopAfterTimeout(
206 base::Bind(&EvaluationContext::OnTimeout,
Alex Deymodb799532014-03-21 13:00:00 -0700207 weak_ptr_factory_.GetWeakPtr()),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700208 timeout);
Alex Deymo53556ec2014-03-17 10:05:57 -0700209 }
210
Alex Deymo53556ec2014-03-17 10:05:57 -0700211 return true;
212}
213
David Zeuthenc1490282014-04-29 16:25:03 -0700214string EvaluationContext::DumpContext() const {
215 base::DictionaryValue* variables = new base::DictionaryValue();
216 for (auto& it : value_cache_) {
217 variables->SetString(it.first->GetName(), it.second.ToString());
218 }
219
220 base::DictionaryValue value;
221 value.Set("variables", variables); // Adopts |variables|.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700222 value.SetString(
223 "evaluation_start_wallclock",
224 chromeos_update_engine::utils::ToString(evaluation_start_wallclock_));
225 value.SetString(
226 "evaluation_start_monotonic",
227 chromeos_update_engine::utils::ToString(evaluation_start_monotonic_));
David Zeuthenc1490282014-04-29 16:25:03 -0700228
229 string json_str;
230 base::JSONWriter::WriteWithOptions(&value,
231 base::JSONWriter::OPTIONS_PRETTY_PRINT,
232 &json_str);
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -0700233 base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str);
David Zeuthenc1490282014-04-29 16:25:03 -0700234
235 return json_str;
236}
237
Alex Deymo63784a52014-05-28 10:46:14 -0700238} // namespace chromeos_update_manager