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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 31 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 32 | using base::Callback; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 33 | using base::Closure; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 34 | using base::Time; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 35 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 36 | using brillo::MessageLoop; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 37 | using chromeos_update_engine::ClockInterface; |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 38 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 39 | using std::unique_ptr; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 40 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 41 | namespace { |
| 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 Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 46 | bool IsTimeGreaterThanHelper(Time ref_time, Time curr_time, Time* reeval_time) { |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 47 | 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 Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 57 | TimeDelta GetTimeout(Time curr, Time expires) { |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 58 | if (expires.is_max()) |
| 59 | return TimeDelta::Max(); |
| 60 | return expires - curr; |
| 61 | } |
| 62 | |
| 63 | } // namespace |
| 64 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 65 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 66 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 67 | EvaluationContext::EvaluationContext( |
| 68 | ClockInterface* clock, |
| 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) |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 72 | : clock_(clock), |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 73 | evaluation_timeout_(evaluation_timeout), |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 74 | expiration_timeout_(expiration_timeout), |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 75 | unregister_cb_(std::move(unregister_cb)), |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 76 | weak_ptr_factory_(this) { |
| 77 | ResetEvaluation(); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 78 | ResetExpiration(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 81 | EvaluationContext::~EvaluationContext() { |
| 82 | RemoveObserversAndTimeout(); |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 83 | if (unregister_cb_.get()) |
| 84 | unregister_cb_->Run(this); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 87 | unique_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 88 | for (auto& it : value_cache_) { |
| 89 | if (it.first->GetMode() == kVariableModeAsync) |
| 90 | it.first->RemoveObserver(this); |
| 91 | } |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 92 | MessageLoop::current()->CancelTask(timeout_event_); |
| 93 | timeout_event_ = MessageLoop::kTaskIdNull; |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 94 | |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 95 | return std::move(callback_); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 98 | TimeDelta 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 | |
| 105 | Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 106 | return (timeout.is_max() ? Time::Max() |
| 107 | : clock_->GetMonotonicTime() + timeout); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 110 | void EvaluationContext::ValueChanged(BaseVariable* var) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 111 | DLOG(INFO) << "ValueChanged() called for variable " << var->GetName(); |
| 112 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 115 | void EvaluationContext::OnTimeout() { |
| 116 | DLOG(INFO) << "OnTimeout() called due to " |
| 117 | << (timeout_marks_expiration_ ? "expiration" : "poll interval"); |
Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 118 | timeout_event_ = MessageLoop::kTaskIdNull; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 119 | is_expired_ = timeout_marks_expiration_; |
| 120 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 123 | void EvaluationContext::OnValueChangedOrTimeout() { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 124 | // Copy the callback handle locally, allowing it to be reassigned. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 125 | unique_ptr<Closure> callback = RemoveObserversAndTimeout(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 126 | |
| 127 | if (callback.get()) |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 128 | callback->Run(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 131 | bool EvaluationContext::IsWallclockTimeGreaterThan(Time timestamp) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 132 | return IsTimeGreaterThanHelper( |
| 133 | timestamp, evaluation_start_wallclock_, &reevaluation_time_wallclock_); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 136 | bool EvaluationContext::IsMonotonicTimeGreaterThan(Time timestamp) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 137 | return IsTimeGreaterThanHelper( |
| 138 | timestamp, evaluation_start_monotonic_, &reevaluation_time_monotonic_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void EvaluationContext::ResetEvaluation() { |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 142 | evaluation_start_wallclock_ = clock_->GetWallclockTime(); |
| 143 | evaluation_start_monotonic_ = clock_->GetMonotonicTime(); |
| 144 | reevaluation_time_wallclock_ = Time::Max(); |
| 145 | reevaluation_time_monotonic_ = Time::Max(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 146 | evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 147 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 148 | // Remove the cached values of non-const variables |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 149 | for (auto it = value_cache_.begin(); it != value_cache_.end();) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 150 | if (it->first->GetMode() == kVariableModeConst) { |
| 151 | ++it; |
| 152 | } else { |
| 153 | it = value_cache_.erase(it); |
| 154 | } |
| 155 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 158 | void EvaluationContext::ResetExpiration() { |
| 159 | expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_); |
| 160 | is_expired_ = false; |
| 161 | } |
| 162 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 163 | bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 164 | // Check that the method was not called more than once. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 165 | if (callback_.get()) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 166 | LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once."; |
| 167 | return false; |
| 168 | } |
| 169 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 170 | // 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 Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 176 | // 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 Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 182 | |
| 183 | // Handle reevaluation due to async or poll variables. |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 184 | bool waiting_for_value_change = false; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 185 | for (auto& it : value_cache_) { |
| 186 | switch (it.first->GetMode()) { |
| 187 | case kVariableModeAsync: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 188 | DLOG(INFO) << "Waiting for value on " << it.first->GetName(); |
| 189 | it.first->AddObserver(this); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 190 | waiting_for_value_change = true; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 191 | break; |
| 192 | case kVariableModePoll: |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 193 | timeout = std::min(timeout, it.first->GetPollInterval()); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 194 | break; |
| 195 | case kVariableModeConst: |
| 196 | // Ignored. |
| 197 | break; |
| 198 | } |
| 199 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 200 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 201 | // Check if the re-evaluation is actually being scheduled. If there are no |
| 202 | // events waited for, this function should return false. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 203 | if (!waiting_for_value_change && timeout.is_max()) |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 204 | return false; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 205 | |
| 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 Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 219 | timeout_event_ = MessageLoop::current()->PostDelayedTask( |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 220 | FROM_HERE, |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 221 | base::Bind(&EvaluationContext::OnTimeout, |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 222 | weak_ptr_factory_.GetWeakPtr()), |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 223 | timeout); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 229 | string EvaluationContext::DumpContext() const { |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 230 | auto variables = std::make_unique<base::DictionaryValue>(); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 231 | for (auto& it : value_cache_) { |
| 232 | variables->SetString(it.first->GetName(), it.second.ToString()); |
| 233 | } |
| 234 | |
| 235 | base::DictionaryValue value; |
Ben Chan | 42458e2 | 2017-02-07 15:33:32 -0800 | [diff] [blame] | 236 | value.Set("variables", std::move(variables)); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 237 | 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 Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 243 | |
| 244 | string json_str; |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 245 | base::JSONWriter::WriteWithOptions( |
| 246 | value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_str); |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame] | 247 | base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 248 | |
| 249 | return json_str; |
| 250 | } |
| 251 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 252 | } // namespace chromeos_update_manager |