blob: 0c4ef2151a80261df5ea140c73b86f1e632263dc [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 Deymo509dd532015-06-10 14:11:05 -070022using chromeos::MessageLoop;
Alex Deymo41a75a72014-04-15 15:36:22 -070023using chromeos_update_engine::ClockInterface;
David Zeuthenc1490282014-04-29 16:25:03 -070024using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070025using std::unique_ptr;
Alex Deymo23949d42014-02-05 15:20:59 -080026
Gilad Arnolda65fced2014-07-23 09:01:31 -070027namespace {
28
29// Returns whether |curr_time| surpassed |ref_time|; if not, also checks whether
30// |ref_time| is sooner than the current value of |*reeval_time|, in which case
31// the latter is updated to the former.
Alex Deymof329b932014-10-30 01:37:48 -070032bool IsTimeGreaterThanHelper(Time ref_time, Time curr_time,
33 Time* reeval_time) {
Gilad Arnolda65fced2014-07-23 09:01:31 -070034 if (curr_time > ref_time)
35 return true;
36 // Remember the nearest reference we've checked against in this evaluation.
37 if (*reeval_time > ref_time)
38 *reeval_time = ref_time;
39 return false;
40}
41
42// If |expires| never happens (maximal value), returns the maximal interval;
43// otherwise, returns the difference between |expires| and |curr|.
Alex Deymof329b932014-10-30 01:37:48 -070044TimeDelta GetTimeout(Time curr, Time expires) {
Gilad Arnolda65fced2014-07-23 09:01:31 -070045 if (expires.is_max())
46 return TimeDelta::Max();
47 return expires - curr;
48}
49
50} // namespace
51
Alex Deymo63784a52014-05-28 10:46:14 -070052namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080053
Gilad Arnold83ffdda2014-08-08 13:30:31 -070054EvaluationContext::EvaluationContext(
55 ClockInterface* clock,
56 TimeDelta evaluation_timeout,
57 TimeDelta expiration_timeout,
Ben Chan02f7c1d2014-10-18 15:18:02 -070058 unique_ptr<Callback<void(EvaluationContext*)>> unregister_cb)
Alex Deymo41a75a72014-04-15 15:36:22 -070059 : clock_(clock),
Gilad Arnoldb2271992014-06-19 12:35:24 -070060 evaluation_timeout_(evaluation_timeout),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070061 expiration_timeout_(expiration_timeout),
Ben Chan02f7c1d2014-10-18 15:18:02 -070062 unregister_cb_(std::move(unregister_cb)),
Alex Deymo41a75a72014-04-15 15:36:22 -070063 weak_ptr_factory_(this) {
64 ResetEvaluation();
Gilad Arnoldfd45a732014-08-07 15:53:46 -070065 ResetExpiration();
Alex Deymo41a75a72014-04-15 15:36:22 -070066}
67
Alex Deymo53556ec2014-03-17 10:05:57 -070068EvaluationContext::~EvaluationContext() {
69 RemoveObserversAndTimeout();
Gilad Arnold83ffdda2014-08-08 13:30:31 -070070 if (unregister_cb_.get())
71 unregister_cb_->Run(this);
Alex Deymo53556ec2014-03-17 10:05:57 -070072}
73
Ben Chan02f7c1d2014-10-18 15:18:02 -070074unique_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() {
Alex Deymo53556ec2014-03-17 10:05:57 -070075 for (auto& it : value_cache_) {
76 if (it.first->GetMode() == kVariableModeAsync)
77 it.first->RemoveObserver(this);
78 }
Alex Deymo509dd532015-06-10 14:11:05 -070079 MessageLoop::current()->CancelTask(timeout_event_);
80 timeout_event_ = MessageLoop::kTaskIdNull;
Gilad Arnold83ffdda2014-08-08 13:30:31 -070081
Ben Chan02f7c1d2014-10-18 15:18:02 -070082 return unique_ptr<Closure>(callback_.release());
Alex Deymo53556ec2014-03-17 10:05:57 -070083}
84
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070085TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const {
86 if (monotonic_deadline.is_max())
87 return TimeDelta::Max();
88 TimeDelta remaining = monotonic_deadline - clock_->GetMonotonicTime();
89 return std::max(remaining, TimeDelta());
90}
91
92Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) {
93 return (timeout.is_max() ? Time::Max() :
94 clock_->GetMonotonicTime() + timeout);
Alex Deymo23949d42014-02-05 15:20:59 -080095}
96
Alex Deymo53556ec2014-03-17 10:05:57 -070097void EvaluationContext::ValueChanged(BaseVariable* var) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070098 DLOG(INFO) << "ValueChanged() called for variable " << var->GetName();
99 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700100}
101
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700102void EvaluationContext::OnTimeout() {
103 DLOG(INFO) << "OnTimeout() called due to "
104 << (timeout_marks_expiration_ ? "expiration" : "poll interval");
Alex Deymo509dd532015-06-10 14:11:05 -0700105 timeout_event_ = MessageLoop::kTaskIdNull;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700106 is_expired_ = timeout_marks_expiration_;
107 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700108}
109
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700110void EvaluationContext::OnValueChangedOrTimeout() {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700111 // Copy the callback handle locally, allowing it to be reassigned.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700112 unique_ptr<Closure> callback = RemoveObserversAndTimeout();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700113
114 if (callback.get())
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700115 callback->Run();
Alex Deymo41a75a72014-04-15 15:36:22 -0700116}
117
Alex Deymof329b932014-10-30 01:37:48 -0700118bool EvaluationContext::IsWallclockTimeGreaterThan(Time timestamp) {
Gilad Arnolda65fced2014-07-23 09:01:31 -0700119 return IsTimeGreaterThanHelper(timestamp, evaluation_start_wallclock_,
120 &reevaluation_time_wallclock_);
121}
122
Alex Deymof329b932014-10-30 01:37:48 -0700123bool EvaluationContext::IsMonotonicTimeGreaterThan(Time timestamp) {
Gilad Arnolda65fced2014-07-23 09:01:31 -0700124 return IsTimeGreaterThanHelper(timestamp, evaluation_start_monotonic_,
125 &reevaluation_time_monotonic_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700126}
127
128void EvaluationContext::ResetEvaluation() {
Gilad Arnolda65fced2014-07-23 09:01:31 -0700129 evaluation_start_wallclock_ = clock_->GetWallclockTime();
130 evaluation_start_monotonic_ = clock_->GetMonotonicTime();
131 reevaluation_time_wallclock_ = Time::Max();
132 reevaluation_time_monotonic_ = Time::Max();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700133 evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700134
Alex Deymo53556ec2014-03-17 10:05:57 -0700135 // Remove the cached values of non-const variables
136 for (auto it = value_cache_.begin(); it != value_cache_.end(); ) {
137 if (it->first->GetMode() == kVariableModeConst) {
138 ++it;
139 } else {
140 it = value_cache_.erase(it);
141 }
142 }
Alex Deymo53556ec2014-03-17 10:05:57 -0700143}
144
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700145void EvaluationContext::ResetExpiration() {
146 expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_);
147 is_expired_ = false;
148}
149
Alex Deymo53556ec2014-03-17 10:05:57 -0700150bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700151 // Check that the method was not called more than once.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700152 if (callback_.get()) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700153 LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once.";
154 return false;
155 }
156
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700157 // Check that the context did not yet expire.
158 if (is_expired()) {
159 LOG(ERROR) << "RunOnValueChangeOrTimeout called on an expired context.";
160 return false;
161 }
162
Gilad Arnolda65fced2014-07-23 09:01:31 -0700163 // Handle reevaluation due to a Is{Wallclock,Monotonic}TimeGreaterThan(). We
164 // choose the smaller of the differences between evaluation start time and
165 // reevaluation time among the wallclock and monotonic scales.
166 TimeDelta timeout = std::min(
167 GetTimeout(evaluation_start_wallclock_, reevaluation_time_wallclock_),
168 GetTimeout(evaluation_start_monotonic_, reevaluation_time_monotonic_));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700169
170 // Handle reevaluation due to async or poll variables.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700171 bool waiting_for_value_change = false;
Alex Deymo53556ec2014-03-17 10:05:57 -0700172 for (auto& it : value_cache_) {
173 switch (it.first->GetMode()) {
174 case kVariableModeAsync:
Alex Deymo53556ec2014-03-17 10:05:57 -0700175 DLOG(INFO) << "Waiting for value on " << it.first->GetName();
176 it.first->AddObserver(this);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700177 waiting_for_value_change = true;
Alex Deymo53556ec2014-03-17 10:05:57 -0700178 break;
179 case kVariableModePoll:
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700180 timeout = std::min(timeout, it.first->GetPollInterval());
Alex Deymo53556ec2014-03-17 10:05:57 -0700181 break;
182 case kVariableModeConst:
183 // Ignored.
184 break;
185 }
186 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700187
Alex Deymo53556ec2014-03-17 10:05:57 -0700188 // Check if the re-evaluation is actually being scheduled. If there are no
189 // events waited for, this function should return false.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700190 if (!waiting_for_value_change && timeout.is_max())
Alex Deymo53556ec2014-03-17 10:05:57 -0700191 return false;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700192
193 // Ensure that we take into account the expiration timeout.
194 TimeDelta expiration = RemainingTime(expiration_monotonic_deadline_);
195 timeout_marks_expiration_ = expiration < timeout;
196 if (timeout_marks_expiration_)
197 timeout = expiration;
198
199 // Store the reevaluation callback.
200 callback_.reset(new Closure(callback));
201
202 // Schedule a timeout event, if one is set.
203 if (!timeout.is_max()) {
204 DLOG(INFO) << "Waiting for timeout in "
205 << chromeos_update_engine::utils::FormatTimeDelta(timeout);
Alex Deymo509dd532015-06-10 14:11:05 -0700206 timeout_event_ = MessageLoop::current()->PostDelayedTask(
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700207 base::Bind(&EvaluationContext::OnTimeout,
Alex Deymodb799532014-03-21 13:00:00 -0700208 weak_ptr_factory_.GetWeakPtr()),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700209 timeout);
Alex Deymo53556ec2014-03-17 10:05:57 -0700210 }
211
Alex Deymo53556ec2014-03-17 10:05:57 -0700212 return true;
213}
214
David Zeuthenc1490282014-04-29 16:25:03 -0700215string EvaluationContext::DumpContext() const {
216 base::DictionaryValue* variables = new base::DictionaryValue();
217 for (auto& it : value_cache_) {
218 variables->SetString(it.first->GetName(), it.second.ToString());
219 }
220
221 base::DictionaryValue value;
222 value.Set("variables", variables); // Adopts |variables|.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700223 value.SetString(
224 "evaluation_start_wallclock",
225 chromeos_update_engine::utils::ToString(evaluation_start_wallclock_));
226 value.SetString(
227 "evaluation_start_monotonic",
228 chromeos_update_engine::utils::ToString(evaluation_start_monotonic_));
David Zeuthenc1490282014-04-29 16:25:03 -0700229
230 string json_str;
231 base::JSONWriter::WriteWithOptions(&value,
232 base::JSONWriter::OPTIONS_PRETTY_PRINT,
233 &json_str);
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -0700234 base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str);
David Zeuthenc1490282014-04-29 16:25:03 -0700235
236 return json_str;
237}
238
Alex Deymo63784a52014-05-28 10:46:14 -0700239} // namespace chromeos_update_manager