blob: fb8d2c74445d80993d98871fec817f4bf19c2274 [file] [log] [blame]
Kevin Lin266f09e2020-08-19 15:18:28 +08001// 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 Chen8daa23d2018-12-18 17:17:38 +08004
5#include <memory>
6
7#include <base/values.h>
8
9#include "runtime_probe/probe_statement.h"
10
11namespace runtime_probe {
12
13namespace {
14
Kevin Linaf1ee812020-06-23 16:53:53 +080015void FilterValueByKey(base::Value* dv, const std::set<std::string>& keys) {
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080016 std::vector<std::string> keys_to_delete;
Kevin Linaf1ee812020-06-23 16:53:53 +080017 for (const auto& entry : dv->DictItems()) {
18 if (keys.find(entry.first) == keys.end()) {
19 keys_to_delete.push_back(entry.first);
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080020 }
21 }
22 for (const auto& k : keys_to_delete) {
Kevin Linaf1ee812020-06-23 16:53:53 +080023 dv->RemoveKey(k);
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080024 }
25}
26
27} // namespace
Wei-Han Chen64e93a32018-12-20 20:07:18 +080028
Kevin Linaf1ee812020-06-23 16:53:53 +080029std::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 Chen8daa23d2018-12-18 17:17:38 +080035
36 // Parse required field "eval"
Kevin Linaf1ee812020-06-23 16:53:53 +080037 const auto* eval_value = dv.FindDictKey("eval");
hschamf0166d02020-02-07 11:22:09 +090038 if (!eval_value) {
Yong Hongee153742021-03-18 01:07:59 +080039 LOG(ERROR) << "\"eval\" should be a dictionary.";
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080040 return nullptr;
41 }
Kevin Linaf1ee812020-06-23 16:53:53 +080042 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 Chen8daa23d2018-12-18 17:17:38 +080046 return nullptr;
47 }
Kevin Linaf1ee812020-06-23 16:53:53 +080048 std::unique_ptr<ProbeStatement> instance{new ProbeStatement()};
49 instance->component_name_ = component_name;
50 instance->eval_ = std::move(function);
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080051
52 // Parse optional field "keys"
Kevin Linaf1ee812020-06-23 16:53:53 +080053 const auto* keys_value = dv.FindListKey("keys");
hschamf0166d02020-02-07 11:22:09 +090054 if (!keys_value) {
Kevin Linaf1ee812020-06-23 16:53:53 +080055 VLOG(3) << "\"keys\" does not exist or is not a list";
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080056 } else {
hschamf0166d02020-02-07 11:22:09 +090057 for (const auto& v : keys_value->GetList()) {
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080058 // Currently, destroy all previously inserted valid elems
hschamf0166d02020-02-07 11:22:09 +090059 if (!v.is_string()) {
Kevin Linaf1ee812020-06-23 16:53:53 +080060 LOG(ERROR) << "\"keys\" should be a list of string: " << *keys_value;
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080061 instance->key_.clear();
Kevin Linaf1ee812020-06-23 16:53:53 +080062 break;
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080063 }
hschamf0166d02020-02-07 11:22:09 +090064 instance->key_.insert(v.GetString());
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080065 }
66 }
67
68 // Parse optional field "expect"
69 // TODO(b:121354690): Make expect useful
Kevin Linaf1ee812020-06-23 16:53:53 +080070 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 Chen8daa23d2018-12-18 17:17:38 +080073 } else {
Kevin Linaf1ee812020-06-23 16:53:53 +080074 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 Chen8daa23d2018-12-18 17:17:38 +080081 }
Wei-Han Chen64e93a32018-12-20 20:07:18 +080082
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080083 // Parse optional field "information"
Kevin Linaf1ee812020-06-23 16:53:53 +080084 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 Chen8daa23d2018-12-18 17:17:38 +080089 }
90
91 return instance;
92}
93
94ProbeFunction::DataType ProbeStatement::Eval() const {
95 auto results = eval_->Eval();
Wei-Han Chen64e93a32018-12-20 20:07:18 +080096
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080097 if (!key_.empty()) {
Kevin Linaf1ee812020-06-23 16:53:53 +080098 std::for_each(results.begin(), results.end(),
99 [this](auto& result) { FilterValueByKey(&result, key_); });
Wei-Han Chen8daa23d2018-12-18 17:17:38 +0800100 }
Wei-Han Chen64e93a32018-12-20 20:07:18 +0800101
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 Chen8daa23d2018-12-18 17:17:38 +0800114 return results;
115}
116} // namespace runtime_probe