Kevin Lin | 266f09e | 2020-08-19 15:18:28 +0800 | [diff] [blame] | 1 | // Copyright 2018 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. |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 4 | |
| 5 | #include <memory> |
| 6 | |
| 7 | #include <base/values.h> |
| 8 | |
| 9 | #include "runtime_probe/probe_statement.h" |
| 10 | |
| 11 | namespace runtime_probe { |
| 12 | |
| 13 | namespace { |
| 14 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 15 | void FilterValueByKey(base::Value* dv, const std::set<std::string>& keys) { |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 16 | std::vector<std::string> keys_to_delete; |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 17 | for (const auto& entry : dv->DictItems()) { |
| 18 | if (keys.find(entry.first) == keys.end()) { |
| 19 | keys_to_delete.push_back(entry.first); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | for (const auto& k : keys_to_delete) { |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 23 | dv->RemoveKey(k); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
| 27 | } // namespace |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 28 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 29 | std::unique_ptr<ProbeStatement> ProbeStatement::FromValue( |
| 30 | std::string component_name, const base::Value& dv) { |
| 31 | if (!dv.is_dict()) { |
| 32 | LOG(ERROR) << "ProbeStatement::FromValue takes a dictionary as parameter"; |
| 33 | return nullptr; |
| 34 | } |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 35 | |
| 36 | // Parse required field "eval" |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 37 | const auto* eval_value = dv.FindDictKey("eval"); |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 38 | if (!eval_value) { |
Yong Hong | ee15374 | 2021-03-18 01:07:59 +0800 | [diff] [blame] | 39 | LOG(ERROR) << "\"eval\" should be a dictionary."; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 40 | return nullptr; |
| 41 | } |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 42 | auto function = ProbeFunction::FromValue(*eval_value); |
| 43 | if (!function) { |
| 44 | LOG(ERROR) << "Component " << component_name |
| 45 | << " doesn't contain a valid probe function."; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 46 | return nullptr; |
| 47 | } |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 48 | std::unique_ptr<ProbeStatement> instance{new ProbeStatement()}; |
| 49 | instance->component_name_ = component_name; |
| 50 | instance->eval_ = std::move(function); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 51 | |
| 52 | // Parse optional field "keys" |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 53 | const auto* keys_value = dv.FindListKey("keys"); |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 54 | if (!keys_value) { |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 55 | VLOG(3) << "\"keys\" does not exist or is not a list"; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 56 | } else { |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 57 | for (const auto& v : keys_value->GetList()) { |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 58 | // Currently, destroy all previously inserted valid elems |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 59 | if (!v.is_string()) { |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 60 | LOG(ERROR) << "\"keys\" should be a list of string: " << *keys_value; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 61 | instance->key_.clear(); |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 62 | break; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 63 | } |
hscham | f0166d0 | 2020-02-07 11:22:09 +0900 | [diff] [blame] | 64 | instance->key_.insert(v.GetString()); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | // Parse optional field "expect" |
| 69 | // TODO(b:121354690): Make expect useful |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 70 | const auto* expect_value = dv.FindDictKey("expect"); |
| 71 | if (!expect_value) { |
| 72 | VLOG(3) << "\"expect\" does not exist or is not a dictionary"; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 73 | } else { |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 74 | auto checker = ProbeResultChecker::FromValue(*expect_value); |
| 75 | if (!checker) { |
| 76 | VLOG(1) << "Component " << component_name |
| 77 | << " doesn't contain a valid checker."; |
| 78 | } else { |
| 79 | instance->expect_ = std::move(checker); |
| 80 | } |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 81 | } |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 82 | |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 83 | // Parse optional field "information" |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 84 | const auto* information = dv.FindDictKey("information"); |
| 85 | if (!information) { |
| 86 | VLOG(3) << "\"information\" does not exist or is not a dictionary"; |
| 87 | } else { |
| 88 | instance->information_ = information->Clone(); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return instance; |
| 92 | } |
| 93 | |
| 94 | ProbeFunction::DataType ProbeStatement::Eval() const { |
| 95 | auto results = eval_->Eval(); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 96 | |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 97 | if (!key_.empty()) { |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 98 | std::for_each(results.begin(), results.end(), |
| 99 | [this](auto& result) { FilterValueByKey(&result, key_); }); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 100 | } |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 101 | |
| 102 | if (expect_) { |
| 103 | // |expect_->Apply| will return false if the probe result is considered |
| 104 | // invalid. |
| 105 | // |std::partition| will move failed elements to end of list, |first_fail| |
| 106 | // will point the the first failed element. |
| 107 | auto first_failure = std::partition( |
| 108 | results.begin(), results.end(), |
| 109 | [this](auto& result) { return expect_->Apply(&result); }); |
| 110 | // Remove failed elements. |
| 111 | results.erase(first_failure, results.end()); |
| 112 | } |
| 113 | |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 114 | return results; |
| 115 | } |
| 116 | } // namespace runtime_probe |