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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 23 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 24 | |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 25 | EvaluationContext::EvaluationContext(ClockInterface* clock, |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 26 | TimeDelta evaluation_timeout, |
| 27 | TimeDelta expiration_timeout) |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 28 | : clock_(clock), |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 29 | evaluation_timeout_(evaluation_timeout), |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 30 | expiration_monotonic_deadline_(MonotonicDeadline(expiration_timeout)), |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 31 | weak_ptr_factory_(this) { |
| 32 | ResetEvaluation(); |
| 33 | } |
| 34 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 35 | EvaluationContext::~EvaluationContext() { |
| 36 | RemoveObserversAndTimeout(); |
| 37 | } |
| 38 | |
| 39 | void EvaluationContext::RemoveObserversAndTimeout() { |
| 40 | for (auto& it : value_cache_) { |
| 41 | if (it.first->GetMode() == kVariableModeAsync) |
| 42 | it.first->RemoveObserver(this); |
| 43 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 44 | CancelMainLoopEvent(timeout_event_); |
| 45 | timeout_event_ = kEventIdNull; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 48 | TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const { |
| 49 | if (monotonic_deadline.is_max()) |
| 50 | return TimeDelta::Max(); |
| 51 | TimeDelta remaining = monotonic_deadline - clock_->GetMonotonicTime(); |
| 52 | return std::max(remaining, TimeDelta()); |
| 53 | } |
| 54 | |
| 55 | Time EvaluationContext::MonotonicDeadline(TimeDelta timeout) { |
| 56 | return (timeout.is_max() ? Time::Max() : |
| 57 | clock_->GetMonotonicTime() + timeout); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 60 | void EvaluationContext::ValueChanged(BaseVariable* var) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 61 | DLOG(INFO) << "ValueChanged() called for variable " << var->GetName(); |
| 62 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 65 | void EvaluationContext::OnTimeout() { |
| 66 | DLOG(INFO) << "OnTimeout() called due to " |
| 67 | << (timeout_marks_expiration_ ? "expiration" : "poll interval"); |
| 68 | timeout_event_ = kEventIdNull; |
| 69 | is_expired_ = timeout_marks_expiration_; |
| 70 | OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 73 | void EvaluationContext::OnValueChangedOrTimeout() { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 74 | RemoveObserversAndTimeout(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 75 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 76 | // Copy the callback handle locally, allowing it to be reassigned. |
| 77 | scoped_ptr<Closure> callback(callback_.release()); |
| 78 | |
| 79 | if (callback.get()) |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 80 | callback->Run(); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool EvaluationContext::IsTimeGreaterThan(base::Time timestamp) { |
| 84 | if (evaluation_start_ > timestamp) |
| 85 | return true; |
| 86 | // We need to keep track of these calls to trigger a reevaluation. |
| 87 | if (reevaluation_time_ > timestamp) |
| 88 | reevaluation_time_ = timestamp; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | void EvaluationContext::ResetEvaluation() { |
| 93 | // It is not important if these two values are not in sync. The first value is |
| 94 | // a reference in time when the evaluation started, to device time-based |
| 95 | // values for the current evaluation. The second is a deadline for the |
| 96 | // evaluation which required a monotonic source of time. |
| 97 | evaluation_start_ = clock_->GetWallclockTime(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 98 | evaluation_monotonic_deadline_ = MonotonicDeadline(evaluation_timeout_); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 99 | reevaluation_time_ = Time::Max(); |
| 100 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 101 | // Remove the cached values of non-const variables |
| 102 | for (auto it = value_cache_.begin(); it != value_cache_.end(); ) { |
| 103 | if (it->first->GetMode() == kVariableModeConst) { |
| 104 | ++it; |
| 105 | } else { |
| 106 | it = value_cache_.erase(it); |
| 107 | } |
| 108 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool EvaluationContext::RunOnValueChangeOrTimeout(Closure callback) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 112 | // Check that the method was not called more than once. |
| 113 | if (callback_.get() != nullptr) { |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 114 | LOG(ERROR) << "RunOnValueChangeOrTimeout called more than once."; |
| 115 | return false; |
| 116 | } |
| 117 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 118 | // Check that the context did not yet expire. |
| 119 | if (is_expired()) { |
| 120 | LOG(ERROR) << "RunOnValueChangeOrTimeout called on an expired context."; |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | TimeDelta timeout(TimeDelta::Max()); |
| 125 | bool waiting_for_value_change = false; |
| 126 | |
| 127 | // Handle reevaluation due to a IsTimeGreaterThan() call. |
| 128 | if (!reevaluation_time_.is_max()) |
| 129 | timeout = reevaluation_time_ - evaluation_start_; |
| 130 | |
| 131 | // Handle reevaluation due to async or poll variables. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 132 | for (auto& it : value_cache_) { |
| 133 | switch (it.first->GetMode()) { |
| 134 | case kVariableModeAsync: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 135 | DLOG(INFO) << "Waiting for value on " << it.first->GetName(); |
| 136 | it.first->AddObserver(this); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 137 | waiting_for_value_change = true; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 138 | break; |
| 139 | case kVariableModePoll: |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 140 | timeout = std::min(timeout, it.first->GetPollInterval()); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 141 | break; |
| 142 | case kVariableModeConst: |
| 143 | // Ignored. |
| 144 | break; |
| 145 | } |
| 146 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 147 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 148 | // Check if the re-evaluation is actually being scheduled. If there are no |
| 149 | // events waited for, this function should return false. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 150 | if (!waiting_for_value_change && timeout.is_max()) |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 151 | return false; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 152 | |
| 153 | // Ensure that we take into account the expiration timeout. |
| 154 | TimeDelta expiration = RemainingTime(expiration_monotonic_deadline_); |
| 155 | timeout_marks_expiration_ = expiration < timeout; |
| 156 | if (timeout_marks_expiration_) |
| 157 | timeout = expiration; |
| 158 | |
| 159 | // Store the reevaluation callback. |
| 160 | callback_.reset(new Closure(callback)); |
| 161 | |
| 162 | // Schedule a timeout event, if one is set. |
| 163 | if (!timeout.is_max()) { |
| 164 | DLOG(INFO) << "Waiting for timeout in " |
| 165 | << chromeos_update_engine::utils::FormatTimeDelta(timeout); |
| 166 | timeout_event_ = RunFromMainLoopAfterTimeout( |
| 167 | base::Bind(&EvaluationContext::OnTimeout, |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 168 | weak_ptr_factory_.GetWeakPtr()), |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 169 | timeout); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 175 | string EvaluationContext::DumpContext() const { |
| 176 | base::DictionaryValue* variables = new base::DictionaryValue(); |
| 177 | for (auto& it : value_cache_) { |
| 178 | variables->SetString(it.first->GetName(), it.second.ToString()); |
| 179 | } |
| 180 | |
| 181 | base::DictionaryValue value; |
| 182 | value.Set("variables", variables); // Adopts |variables|. |
| 183 | value.SetString("evaluation_start", |
| 184 | chromeos_update_engine::utils::ToString(evaluation_start_)); |
| 185 | |
| 186 | string json_str; |
| 187 | base::JSONWriter::WriteWithOptions(&value, |
| 188 | base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 189 | &json_str); |
Gilad Arnold | 6e5ab5c | 2014-06-23 15:13:56 -0700 | [diff] [blame^] | 190 | base::TrimWhitespaceASCII(json_str, base::TRIM_TRAILING, &json_str); |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 191 | |
| 192 | return json_str; |
| 193 | } |
| 194 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 195 | } // namespace chromeos_update_manager |