Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 1 | // 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 Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 5 | #include "update_engine/update_manager/evaluation_context.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 6 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 8 | #include <memory> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 12 | #include <base/json/json_writer.h> |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame] | 13 | #include <base/strings/string_util.h> |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 14 | #include <base/values.h> |
| 15 | |
| 16 | #include "update_engine/utils.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 17 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 18 | using base::Callback; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 19 | using base::Closure; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 20 | using base::Time; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 21 | using base::TimeDelta; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 22 | using chromeos_update_engine::ClockInterface; |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 23 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 24 | using std::unique_ptr; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 25 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 26 | namespace { |
| 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. |
| 31 | bool 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|. |
| 43 | TimeDelta 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 Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 51 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 52 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 53 | EvaluationContext::EvaluationContext( |
| 54 | ClockInterface* clock, |
| 55 | TimeDelta evaluation_timeout, |
| 56 | TimeDelta expiration_timeout, |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 57 | unique_ptr<Callback<void(EvaluationContext*)>> unregister_cb) |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 58 | : clock_(clock), |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 59 | evaluation_timeout_(evaluation_timeout), |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 60 | expiration_timeout_(expiration_timeout), |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 61 | unregister_cb_(std::move(unregister_cb)), |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 62 | weak_ptr_factory_(this) { |
| 63 | ResetEvaluation(); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 64 | ResetExpiration(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 67 | EvaluationContext::~EvaluationContext() { |
| 68 | RemoveObserversAndTimeout(); |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 69 | if (unregister_cb_.get()) |
| 70 | unregister_cb_->Run(this); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 73 | unique_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 74 | for (auto& it : value_cache_) { |
| 75 | if (it.first->GetMode() == kVariableModeAsync) |
| 76 | it.first->RemoveObserver(this); |
| 77 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 78 | CancelMainLoopEvent(timeout_event_); |
| 79 | timeout_event_ = kEventIdNull; |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 80 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 81 | return unique_ptr<Closure>(callback_.release()); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 84 | TimeDelta 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 | |
| 91 | Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) { |
| 92 | return (timeout.is_max() ? Time::Max() : |
| 93 | clock_->GetMonotonicTime() + timeout); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 96 | void EvaluationContext::ValueChanged(BaseVariable* var) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 97 | DLOG(INFO) << "ValueChanged() called for variable " << var->GetName(); |
| 98 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 101 | void 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 Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 109 | void EvaluationContext::OnValueChangedOrTimeout() { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 110 | // Copy the callback handle locally, allowing it to be reassigned. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 111 | unique_ptr<Closure> callback = RemoveObserversAndTimeout(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 112 | |
| 113 | if (callback.get()) |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 114 | callback->Run(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 117 | bool EvaluationContext::IsWallclockTimeGreaterThan(base::Time timestamp) { |
| 118 | return IsTimeGreaterThanHelper(timestamp, evaluation_start_wallclock_, |
| 119 | &reevaluation_time_wallclock_); |
| 120 | } |
| 121 | |
| 122 | bool EvaluationContext::IsMonotonicTimeGreaterThan(base::Time timestamp) { |
| 123 | return IsTimeGreaterThanHelper(timestamp, evaluation_start_monotonic_, |
| 124 | &reevaluation_time_monotonic_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void EvaluationContext::ResetEvaluation() { |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 128 | evaluation_start_wallclock_ = clock_->GetWallclockTime(); |
| 129 | evaluation_start_monotonic_ = clock_->GetMonotonicTime(); |
| 130 | reevaluation_time_wallclock_ = Time::Max(); |
| 131 | reevaluation_time_monotonic_ = Time::Max(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 132 | evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 133 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 134 | // 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 Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 144 | void EvaluationContext::ResetExpiration() { |
| 145 | expiration_monotonic_deadline_ = MonotonicDeadline(expiration_timeout_); |
| 146 | is_expired_ = false; |
| 147 | } |
| 148 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 149 | bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 150 | // Check that the method was not called more than once. |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 151 | if (callback_.get()) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 152 | LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once."; |
| 153 | return false; |
| 154 | } |
| 155 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 156 | // 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 Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 162 | // 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 Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 168 | |
| 169 | // Handle reevaluation due to async or poll variables. |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 170 | bool waiting_for_value_change = false; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 171 | for (auto& it : value_cache_) { |
| 172 | switch (it.first->GetMode()) { |
| 173 | case kVariableModeAsync: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 174 | DLOG(INFO) << "Waiting for value on " << it.first->GetName(); |
| 175 | it.first->AddObserver(this); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 176 | waiting_for_value_change = true; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 177 | break; |
| 178 | case kVariableModePoll: |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 179 | timeout = std::min(timeout, it.first->GetPollInterval()); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 180 | break; |
| 181 | case kVariableModeConst: |
| 182 | // Ignored. |
| 183 | break; |
| 184 | } |
| 185 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 186 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 187 | // Check if the re-evaluation is actually being scheduled. If there are no |
| 188 | // events waited for, this function should return false. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 189 | if (!waiting_for_value_change && timeout.is_max()) |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 190 | return false; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 191 | |
| 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 Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 207 | weak_ptr_factory_.GetWeakPtr()), |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 208 | timeout); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 214 | string 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 Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 222 | 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 Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 228 | |
| 229 | string json_str; |
| 230 | base::JSONWriter::WriteWithOptions(&value, |
| 231 | base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 232 | &json_str); |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame] | 233 | base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 234 | |
| 235 | return json_str; |
| 236 | } |
| 237 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 238 | } // namespace chromeos_update_manager |