blob: 8341edfb027a195bb3b51ac708718ebb437b5365 [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#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 Chen4dab5222018-12-20 17:53:01 +080016#include <gtest/gtest.h>
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080017
18#include "runtime_probe/probe_function.h"
Wei-Han Chen64e93a32018-12-20 20:07:18 +080019#include "runtime_probe/probe_result_checker.h"
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080020
21namespace runtime_probe {
22
23class ProbeStatement {
Kevin Lin266f09e2020-08-19 15:18:28 +080024 // 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 Chen8daa23d2018-12-18 17:17:38 +080048 public:
Kevin Linaf1ee812020-06-23 16:53:53 +080049 static std::unique_ptr<ProbeStatement> FromValue(std::string component_name,
50 const base::Value& dv);
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080051
Kevin Lin266f09e2020-08-19 15:18:28 +080052 // 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 Chen8daa23d2018-12-18 17:17:38 +080059 ProbeFunction::DataType Eval() const;
60
Kevin Linaf1ee812020-06-23 16:53:53 +080061 base::Optional<base::Value> GetInformation() const {
62 if (information_)
63 return information_->Clone();
64 return base::nullopt;
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080065 }
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 Chen64e93a32018-12-20 20:07:18 +080073 std::unique_ptr<ProbeResultChecker> expect_;
Kevin Linaf1ee812020-06-23 16:53:53 +080074 base::Optional<base::Value> information_;
Wei-Han Chen4dab5222018-12-20 17:53:01 +080075
76 FRIEND_TEST(ProbeConfigTest, LoadConfig);
Wei-Han Chen64e93a32018-12-20 20:07:18 +080077 FRIEND_TEST(ProbeStatementTest, TestEval);
Wei-Han Chen8daa23d2018-12-18 17:17:38 +080078};
79
80} // namespace runtime_probe
81
82#endif // RUNTIME_PROBE_PROBE_STATEMENT_H_