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 | #ifndef RUNTIME_PROBE_PROBE_STATEMENT_H_ |
| 6 | #define RUNTIME_PROBE_PROBE_STATEMENT_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <set> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <base/values.h> |
Wei-Han Chen | 4dab522 | 2018-12-20 17:53:01 +0800 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 17 | |
| 18 | #include "runtime_probe/probe_function.h" |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 19 | #include "runtime_probe/probe_result_checker.h" |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 20 | |
| 21 | namespace runtime_probe { |
| 22 | |
| 23 | class ProbeStatement { |
Kevin Lin | 266f09e | 2020-08-19 15:18:28 +0800 | [diff] [blame] | 24 | // Holds a probe statement with following JSON schema:: |
| 25 | // { |
| 26 | // "eval": <function_name:string> | |
| 27 | // <func:ProbeFunction> | |
| 28 | // [<func:ProbeFunction>], |
| 29 | // "keys": [<key:string>], |
| 30 | // "expect": <see |ProbeResultChecker|>, |
| 31 | // "information": <info:DictValue>, |
| 32 | // } |
| 33 | // |
| 34 | // For "eval", the case "[<func:ProbeFunction>]" will be transformed into:: |
| 35 | // (ProbeFunction) { |
| 36 | // "function_name": "sequence", |
| 37 | // "args": { |
| 38 | // "functions": [<func:ProbeFunction>] |
| 39 | // } |
| 40 | // } |
| 41 | // |
| 42 | // For "expect", the dictionary value should represent a ProbeResultChecker |
| 43 | // object. See ProbeResultChecker for more details. |
| 44 | // |
| 45 | // When evaluating a ProbeStatement, the ProbeFunction defined by "eval" will |
| 46 | // be called. The results will be filtered / processed by "keys" and "expect" |
| 47 | // rules. See ProbeStatement::Eval() for more details. |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 48 | public: |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 49 | static std::unique_ptr<ProbeStatement> FromValue(std::string component_name, |
| 50 | const base::Value& dv); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 51 | |
Kevin Lin | 266f09e | 2020-08-19 15:18:28 +0800 | [diff] [blame] | 52 | // Evaluate the probe statement. |
| 53 | // |
| 54 | // The process can be break into following steps: |
| 55 | // - Call probe function |eval_| |
| 56 | // - Filter results by |key_| (if |key_| is not empty) |
| 57 | // - Transform and check results by |expect_| (if |expect_| is not empty) |
| 58 | // - Return final results that passed |expect_| check. |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 59 | ProbeFunction::DataType Eval() const; |
| 60 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 61 | base::Optional<base::Value> GetInformation() const { |
| 62 | if (information_) |
| 63 | return information_->Clone(); |
| 64 | return base::nullopt; |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
| 68 | ProbeStatement() = default; |
| 69 | |
| 70 | std::string component_name_; |
| 71 | std::unique_ptr<ProbeFunction> eval_; |
| 72 | std::set<std::string> key_; |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 73 | std::unique_ptr<ProbeResultChecker> expect_; |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 74 | base::Optional<base::Value> information_; |
Wei-Han Chen | 4dab522 | 2018-12-20 17:53:01 +0800 | [diff] [blame] | 75 | |
| 76 | FRIEND_TEST(ProbeConfigTest, LoadConfig); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 77 | FRIEND_TEST(ProbeStatementTest, TestEval); |
Wei-Han Chen | 8daa23d | 2018-12-18 17:17:38 +0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } // namespace runtime_probe |
| 81 | |
| 82 | #endif // RUNTIME_PROBE_PROBE_STATEMENT_H_ |