blob: e796fec232ab431bae404a8d376fcf9ab8d81e2c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymo23949d42014-02-05 15:20:59 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/evaluation_context.h"
Alex Deymo23949d42014-02-05 15:20:59 -080018
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070019#include <algorithm>
Ben Chan02f7c1d2014-10-18 15:18:02 -070020#include <memory>
David Zeuthenc1490282014-04-29 16:25:03 -070021#include <string>
Ben Chan42458e22017-02-07 15:33:32 -080022#include <utility>
David Zeuthenc1490282014-04-29 16:25:03 -070023
Alex Deymo53556ec2014-03-17 10:05:57 -070024#include <base/bind.h>
David Zeuthenc1490282014-04-29 16:25:03 -070025#include <base/json/json_writer.h>
Alex Deymo0bb23412015-06-19 00:04:46 -070026#include <base/location.h>
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -070027#include <base/strings/string_util.h>
David Zeuthenc1490282014-04-29 16:25:03 -070028#include <base/values.h>
29
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/utils.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070031
Gilad Arnold83ffdda2014-08-08 13:30:31 -070032using base::Callback;
Alex Deymo53556ec2014-03-17 10:05:57 -070033using base::Closure;
Alex Deymo41a75a72014-04-15 15:36:22 -070034using base::Time;
Alex Deymo23949d42014-02-05 15:20:59 -080035using base::TimeDelta;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070036using brillo::MessageLoop;
Alex Deymo41a75a72014-04-15 15:36:22 -070037using chromeos_update_engine::ClockInterface;
David Zeuthenc1490282014-04-29 16:25:03 -070038using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070039using std::unique_ptr;
Alex Deymo23949d42014-02-05 15:20:59 -080040
Gilad Arnolda65fced2014-07-23 09:01:31 -070041namespace {
42
43// Returns whether |curr_time| surpassed |ref_time|; if not, also checks whether
44// |ref_time| is sooner than the current value of |*reeval_time|, in which case
45// the latter is updated to the former.
Amin Hassani4b717432019-01-14 16:24:20 -080046bool IsTimeGreaterThanHelper(Time ref_time, Time curr_time, Time* reeval_time) {
Gilad Arnolda65fced2014-07-23 09:01:31 -070047 if (curr_time > ref_time)
48 return true;
49 // Remember the nearest reference we've checked against in this evaluation.
50 if (*reeval_time > ref_time)
51 *reeval_time = ref_time;
52 return false;
53}
54
55// If |expires| never happens (maximal value), returns the maximal interval;
56// otherwise, returns the difference between |expires| and |curr|.
Alex Deymof329b932014-10-30 01:37:48 -070057TimeDelta GetTimeout(Time curr, Time expires) {
Gilad Arnolda65fced2014-07-23 09:01:31 -070058 if (expires.is_max())
59 return TimeDelta::Max();
60 return expires - curr;
61}
62
63} // namespace
64
Alex Deymo63784a52014-05-28 10:46:14 -070065namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080066
Gilad Arnold83ffdda2014-08-08 13:30:31 -070067EvaluationContext::EvaluationContext(
68 ClockInterface* clock,
69 TimeDelta evaluation_timeout,
70 TimeDelta expiration_timeout,
Ben Chan02f7c1d2014-10-18 15:18:02 -070071 unique_ptr<Callback<void(EvaluationContext*)>> unregister_cb)
Alex Deymo41a75a72014-04-15 15:36:22 -070072 : clock_(clock),
Gilad Arnoldb2271992014-06-19 12:35:24 -070073 evaluation_timeout_(evaluation_timeout),
Gilad Arnoldfd45a732014-08-07 15:53:46 -070074 expiration_timeout_(expiration_timeout),
Ben Chan02f7c1d2014-10-18 15:18:02 -070075 unregister_cb_(std::move(unregister_cb)),
Alex Deymo41a75a72014-04-15 15:36:22 -070076 weak_ptr_factory_(this) {
77 ResetEvaluation();
Gilad Arnoldfd45a732014-08-07 15:53:46 -070078 ResetExpiration();
Alex Deymo41a75a72014-04-15 15:36:22 -070079}
80
Alex Deymo53556ec2014-03-17 10:05:57 -070081EvaluationContext::~EvaluationContext() {
82 RemoveObserversAndTimeout();
Gilad Arnold83ffdda2014-08-08 13:30:31 -070083 if (unregister_cb_.get())
84 unregister_cb_->Run(this);
Alex Deymo53556ec2014-03-17 10:05:57 -070085}
86
Ben Chan02f7c1d2014-10-18 15:18:02 -070087unique_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() {
Alex Deymo53556ec2014-03-17 10:05:57 -070088 for (auto& it : value_cache_) {
89 if (it.first->GetMode() == kVariableModeAsync)
90 it.first->RemoveObserver(this);
91 }
Alex Deymo509dd532015-06-10 14:11:05 -070092 MessageLoop::current()->CancelTask(timeout_event_);
93 timeout_event_ = MessageLoop::kTaskIdNull;
Gilad Arnold83ffdda2014-08-08 13:30:31 -070094
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070095 return std::move(callback_);
Alex Deymo53556ec2014-03-17 10:05:57 -070096}
97
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070098TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const {
99 if (monotonic_deadline.is_max())
100 return TimeDelta::Max();
101 TimeDelta remaining = monotonic_deadline - clock_->GetMonotonicTime();
102 return std::max(remaining, TimeDelta());
103}
104
105Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) {
Amin Hassani4b717432019-01-14 16:24:20 -0800106 return (timeout.is_max() ? Time::Max()
107 : clock_->GetMonotonicTime() + timeout);
Alex Deymo23949d42014-02-05 15:20:59 -0800108}
109
Alex Deymo53556ec2014-03-17 10:05:57 -0700110void EvaluationContext::ValueChanged(BaseVariable* var) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700111 DLOG(INFO) << "ValueChanged() called for variable " << var->GetName();
112 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700113}
114
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700115void EvaluationContext::OnTimeout() {
116 DLOG(INFO) << "OnTimeout() called due to "
117 << (timeout_marks_expiration_ ? "expiration" : "poll interval");
Alex Deymo509dd532015-06-10 14:11:05 -0700118 timeout_event_ = MessageLoop::kTaskIdNull;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700119 is_expired_ = timeout_marks_expiration_;
120 OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700121}
122
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700123void EvaluationContext::OnValueChangedOrTimeout() {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700124 // Copy the callback handle locally, allowing it to be reassigned.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700125 unique_ptr<Closure> callback = RemoveObserversAndTimeout();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700126
127 if (callback.get())
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700128 callback->Run();
Alex Deymo41a75a72014-04-15 15:36:22 -0700129}
130
Alex Deymof329b932014-10-30 01:37:48 -0700131bool EvaluationContext::IsWallclockTimeGreaterThan(Time timestamp) {
Amin Hassani4b717432019-01-14 16:24:20 -0800132 return IsTimeGreaterThanHelper(
133 timestamp, evaluation_start_wallclock_, &reevaluation_time_wallclock_);
Gilad Arnolda65fced2014-07-23 09:01:31 -0700134}
135
Alex Deymof329b932014-10-30 01:37:48 -0700136bool EvaluationContext::IsMonotonicTimeGreaterThan(Time timestamp) {
Amin Hassani4b717432019-01-14 16:24:20 -0800137 return IsTimeGreaterThanHelper(
138 timestamp, evaluation_start_monotonic_, &reevaluation_time_monotonic_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700139}
140
141void EvaluationContext::ResetEvaluation() {
Gilad Arnolda65fced2014-07-23 09:01:31 -0700142 evaluation_start_wallclock_ = clock_->GetWallclockTime();
143 evaluation_start_monotonic_ = clock_->GetMonotonicTime();
144 reevaluation_time_wallclock_ = Time::Max();
145 reevaluation_time_monotonic_ = Time::Max();
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700146 evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_);
Alex Deymo41a75a72014-04-15 15:36:22 -0700147
Alex Deymo53556ec2014-03-17 10:05:57 -0700148 // Remove the cached values of non-const variables
Amin Hassani4b717432019-01-14 16:24:20 -0800149 for (auto it = value_cache_.begin(); it != value_cache_.end();) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700150 if (it->first->GetMode() == kVariableModeConst) {
151 ++it;
152 } else {
153 it = value_cache_.erase(it);
154 }
155 }
Alex Deymo53556ec2014-03-17 10:05:57 -0700156}
157
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700158void EvaluationContext::ResetExpiration() {
159 expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_);
160 is_expired_ = false;
161}
162
Alex Deymo53556ec2014-03-17 10:05:57 -0700163bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) {
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700164 // Check that the method was not called more than once.
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700165 if (callback_.get()) {
Alex Deymo53556ec2014-03-17 10:05:57 -0700166 LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once.";
167 return false;
168 }
169
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700170 // Check that the context did not yet expire.
171 if (is_expired()) {
172 LOG(ERROR) << "RunOnValueChangeOrTimeout called on an expired context.";
173 return false;
174 }
175
Gilad Arnolda65fced2014-07-23 09:01:31 -0700176 // Handle reevaluation due to a Is{Wallclock,Monotonic}TimeGreaterThan(). We
177 // choose the smaller of the differences between evaluation start time and
178 // reevaluation time among the wallclock and monotonic scales.
179 TimeDelta timeout = std::min(
180 GetTimeout(evaluation_start_wallclock_, reevaluation_time_wallclock_),
181 GetTimeout(evaluation_start_monotonic_, reevaluation_time_monotonic_));
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700182
183 // Handle reevaluation due to async or poll variables.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700184 bool waiting_for_value_change = false;
Alex Deymo53556ec2014-03-17 10:05:57 -0700185 for (auto& it : value_cache_) {
186 switch (it.first->GetMode()) {
187 case kVariableModeAsync:
Alex Deymo53556ec2014-03-17 10:05:57 -0700188 DLOG(INFO) << "Waiting for value on " << it.first->GetName();
189 it.first->AddObserver(this);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700190 waiting_for_value_change = true;
Alex Deymo53556ec2014-03-17 10:05:57 -0700191 break;
192 case kVariableModePoll:
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700193 timeout = std::min(timeout, it.first->GetPollInterval());
Alex Deymo53556ec2014-03-17 10:05:57 -0700194 break;
195 case kVariableModeConst:
196 // Ignored.
197 break;
198 }
199 }
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700200
Alex Deymo53556ec2014-03-17 10:05:57 -0700201 // Check if the re-evaluation is actually being scheduled. If there are no
202 // events waited for, this function should return false.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700203 if (!waiting_for_value_change && timeout.is_max())
Alex Deymo53556ec2014-03-17 10:05:57 -0700204 return false;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700205
206 // Ensure that we take into account the expiration timeout.
207 TimeDelta expiration = RemainingTime(expiration_monotonic_deadline_);
208 timeout_marks_expiration_ = expiration < timeout;
209 if (timeout_marks_expiration_)
210 timeout = expiration;
211
212 // Store the reevaluation callback.
213 callback_.reset(new Closure(callback));
214
215 // Schedule a timeout event, if one is set.
216 if (!timeout.is_max()) {
217 DLOG(INFO) << "Waiting for timeout in "
218 << chromeos_update_engine::utils::FormatTimeDelta(timeout);
Alex Deymo509dd532015-06-10 14:11:05 -0700219 timeout_event_ = MessageLoop::current()->PostDelayedTask(
Alex Deymo0bb23412015-06-19 00:04:46 -0700220 FROM_HERE,
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700221 base::Bind(&EvaluationContext::OnTimeout,
Alex Deymodb799532014-03-21 13:00:00 -0700222 weak_ptr_factory_.GetWeakPtr()),
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700223 timeout);
Alex Deymo53556ec2014-03-17 10:05:57 -0700224 }
225
Alex Deymo53556ec2014-03-17 10:05:57 -0700226 return true;
227}
228
David Zeuthenc1490282014-04-29 16:25:03 -0700229string EvaluationContext::DumpContext() const {
Ben Chanab5a0af2017-10-12 14:57:50 -0700230 auto variables = std::make_unique<base::DictionaryValue>();
David Zeuthenc1490282014-04-29 16:25:03 -0700231 for (auto& it : value_cache_) {
232 variables->SetString(it.first->GetName(), it.second.ToString());
233 }
234
235 base::DictionaryValue value;
Ben Chan42458e22017-02-07 15:33:32 -0800236 value.Set("variables", std::move(variables));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700237 value.SetString(
238 "evaluation_start_wallclock",
239 chromeos_update_engine::utils::ToString(evaluation_start_wallclock_));
240 value.SetString(
241 "evaluation_start_monotonic",
242 chromeos_update_engine::utils::ToString(evaluation_start_monotonic_));
David Zeuthenc1490282014-04-29 16:25:03 -0700243
244 string json_str;
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700245 base::JSONWriter::WriteWithOptions(
246 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_str);
Gilad Arnold6e5ab5c2014-06-23 15:13:56 -0700247 base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str);
David Zeuthenc1490282014-04-29 16:25:03 -0700248
249 return json_str;
250}
251
Alex Deymo63784a52014-05-28 10:46:14 -0700252} // namespace chromeos_update_manager