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