Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/evaluation_context.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 18 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 20 | #include <memory> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 21 | #include <string> |
Ben Chan | 42458e2 | 2017-02-07 15:33:32 -0800 | [diff] [blame] | 22 | #include <utility> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 23 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 24 | #include <base/bind.h> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 25 | #include <base/json/json_writer.h> |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 26 | #include <base/location.h> |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame] | 27 | #include <base/strings/string_util.h> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 28 | #include <base/values.h> |
| 29 | |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 30 | #include "update_engine/common/system_state.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/utils.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 32 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 33 | using base::Callback; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 34 | using base::Closure; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 35 | using base::Time; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 36 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 37 | using brillo::MessageLoop; |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 38 | using chromeos_update_engine::SystemState; |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 39 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 40 | using std::unique_ptr; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 41 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | // Returns whether |curr_time| surpassed |ref_time|; if not, also checks whether |
| 45 | // |ref_time| is sooner than the current value of |*reeval_time|, in which case |
| 46 | // the latter is updated to the former. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 47 | bool IsTimeGreaterThanHelper(Time ref_time, Time curr_time, Time* reeval_time) { |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 48 | if (curr_time > ref_time) |
| 49 | return true; |
| 50 | // Remember the nearest reference we've checked against in this evaluation. |
| 51 | if (*reeval_time > ref_time) |
| 52 | *reeval_time = ref_time; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // If |expires| never happens (maximal value), returns the maximal interval; |
| 57 | // otherwise, returns the difference between |expires| and |curr|. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 58 | TimeDelta GetTimeout(Time curr, Time expires) { |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 59 | if (expires.is_max()) |
| 60 | return TimeDelta::Max(); |
| 61 | return expires - curr; |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 66 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 67 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 68 | EvaluationContext::EvaluationContext( |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 69 | TimeDelta evaluation_timeout, |
| 70 | TimeDelta expiration_timeout, |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 71 | unique_ptr<Callback<void(EvaluationContext*)>> unregister_cb) |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 72 | : evaluation_timeout_(evaluation_timeout), |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 73 | expiration_timeout_(expiration_timeout), |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 74 | unregister_cb_(std::move(unregister_cb)), |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 75 | weak_ptr_factory_(this) { |
| 76 | ResetEvaluation(); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 77 | ResetExpiration(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 80 | EvaluationContext::~EvaluationContext() { |
| 81 | RemoveObserversAndTimeout(); |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 82 | if (unregister_cb_.get()) |
| 83 | unregister_cb_->Run(this); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 86 | unique_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 87 | for (auto& it : value_cache_) { |
| 88 | if (it.first->GetMode() == kVariableModeAsync) |
| 89 | it.first->RemoveObserver(this); |
| 90 | } |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 91 | MessageLoop::current()->CancelTask(timeout_event_); |
| 92 | timeout_event_ = MessageLoop::kTaskIdNull; |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 93 | |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 94 | return std::move(callback_); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 97 | TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const { |
| 98 | if (monotonic_deadline.is_max()) |
| 99 | return TimeDelta::Max(); |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 100 | TimeDelta remaining = |
| 101 | monotonic_deadline - SystemState::Get()->clock()->GetMonotonicTime(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 102 | return std::max(remaining, TimeDelta()); |
| 103 | } |
| 104 | |
| 105 | Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 106 | return (timeout.is_max() |
| 107 | ? Time::Max() |
| 108 | : SystemState::Get()->clock()->GetMonotonicTime() + timeout); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 111 | void EvaluationContext::ValueChanged(BaseVariable* var) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 112 | DLOG(INFO) << "ValueChanged() called for variable " << var->GetName(); |
| 113 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 116 | void EvaluationContext::OnTimeout() { |
| 117 | DLOG(INFO) << "OnTimeout() called due to " |
| 118 | << (timeout_marks_expiration_ ? "expiration" : "poll interval"); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 119 | timeout_event_ = MessageLoop::kTaskIdNull; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 120 | is_expired_ = timeout_marks_expiration_; |
| 121 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 124 | void EvaluationContext::OnValueChangedOrTimeout() { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 125 | // Copy the callback handle locally, allowing it to be reassigned. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 126 | unique_ptr<Closure> callback = RemoveObserversAndTimeout(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 127 | |
| 128 | if (callback.get()) |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 129 | callback->Run(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 132 | bool EvaluationContext::IsWallclockTimeGreaterThan(Time timestamp) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 133 | return IsTimeGreaterThanHelper( |
| 134 | timestamp, evaluation_start_wallclock_, &reevaluation_time_wallclock_); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 137 | bool EvaluationContext::IsMonotonicTimeGreaterThan(Time timestamp) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 138 | return IsTimeGreaterThanHelper( |
| 139 | timestamp, evaluation_start_monotonic_, &reevaluation_time_monotonic_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void EvaluationContext::ResetEvaluation() { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 143 | const auto* clock = SystemState::Get()->clock(); |
| 144 | evaluation_start_wallclock_ = clock->GetWallclockTime(); |
| 145 | evaluation_start_monotonic_ = clock->GetMonotonicTime(); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 146 | reevaluation_time_wallclock_ = Time::Max(); |
| 147 | reevaluation_time_monotonic_ = Time::Max(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 148 | evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 149 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 150 | // Remove the cached values of non-const variables |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 151 | for (auto it = value_cache_.begin(); it != value_cache_.end();) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 152 | if (it->first->GetMode() == kVariableModeConst) { |
| 153 | ++it; |
| 154 | } else { |
| 155 | it = value_cache_.erase(it); |
| 156 | } |
| 157 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 160 | void EvaluationContext::ResetExpiration() { |
| 161 | expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_); |
| 162 | is_expired_ = false; |
| 163 | } |
| 164 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 165 | bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 166 | // Check that the method was not called more than once. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 167 | if (callback_.get()) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 168 | LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once."; |
| 169 | return false; |
| 170 | } |
| 171 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 172 | // Check that the context did not yet expire. |
| 173 | if (is_expired()) { |
| 174 | LOG(ERROR) << "RunOnValueChangeOrTimeout called on an expired context."; |
| 175 | return false; |
| 176 | } |
| 177 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 178 | // Handle reevaluation due to a Is{Wallclock,Monotonic}TimeGreaterThan(). We |
| 179 | // choose the smaller of the differences between evaluation start time and |
| 180 | // reevaluation time among the wallclock and monotonic scales. |
| 181 | TimeDelta timeout = std::min( |
| 182 | GetTimeout(evaluation_start_wallclock_, reevaluation_time_wallclock_), |
| 183 | GetTimeout(evaluation_start_monotonic_, reevaluation_time_monotonic_)); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 184 | |
| 185 | // Handle reevaluation due to async or poll variables. |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 186 | bool waiting_for_value_change = false; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 187 | for (auto& it : value_cache_) { |
| 188 | switch (it.first->GetMode()) { |
| 189 | case kVariableModeAsync: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 190 | DLOG(INFO) << "Waiting for value on " << it.first->GetName(); |
| 191 | it.first->AddObserver(this); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 192 | waiting_for_value_change = true; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 193 | break; |
| 194 | case kVariableModePoll: |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 195 | timeout = std::min(timeout, it.first->GetPollInterval()); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 196 | break; |
| 197 | case kVariableModeConst: |
| 198 | // Ignored. |
| 199 | break; |
| 200 | } |
| 201 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 202 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 203 | // Check if the re-evaluation is actually being scheduled. If there are no |
| 204 | // events waited for, this function should return false. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 205 | if (!waiting_for_value_change && timeout.is_max()) |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 206 | return false; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 207 | |
| 208 | // Ensure that we take into account the expiration timeout. |
| 209 | TimeDelta expiration = RemainingTime(expiration_monotonic_deadline_); |
| 210 | timeout_marks_expiration_ = expiration < timeout; |
| 211 | if (timeout_marks_expiration_) |
| 212 | timeout = expiration; |
| 213 | |
| 214 | // Store the reevaluation callback. |
| 215 | callback_.reset(new Closure(callback)); |
| 216 | |
| 217 | // Schedule a timeout event, if one is set. |
| 218 | if (!timeout.is_max()) { |
| 219 | DLOG(INFO) << "Waiting for timeout in " |
| 220 | << chromeos_update_engine::utils::FormatTimeDelta(timeout); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 221 | timeout_event_ = MessageLoop::current()->PostDelayedTask( |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 222 | FROM_HERE, |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 223 | base::Bind(&EvaluationContext::OnTimeout, |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 224 | weak_ptr_factory_.GetWeakPtr()), |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 225 | timeout); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 231 | string EvaluationContext::DumpContext() const { |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 232 | auto variables = std::make_unique<base::DictionaryValue>(); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 233 | for (auto& it : value_cache_) { |
| 234 | variables->SetString(it.first->GetName(), it.second.ToString()); |
| 235 | } |
| 236 | |
| 237 | base::DictionaryValue value; |
Ben Chan | 42458e2 | 2017-02-07 15:33:32 -0800 | [diff] [blame] | 238 | value.Set("variables", std::move(variables)); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 239 | value.SetString( |
| 240 | "evaluation_start_wallclock", |
| 241 | chromeos_update_engine::utils::ToString(evaluation_start_wallclock_)); |
| 242 | value.SetString( |
| 243 | "evaluation_start_monotonic", |
| 244 | chromeos_update_engine::utils::ToString(evaluation_start_monotonic_)); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 245 | |
| 246 | string json_str; |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 247 | base::JSONWriter::WriteWithOptions( |
| 248 | value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_str); |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame] | 249 | base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 250 | |
| 251 | return json_str; |
| 252 | } |
| 253 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 254 | } // namespace chromeos_update_manager |