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