xref: /aosp_15_r20/external/cronet/base/test/test_trace_processor_impl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // TestTraceProcessorImpl encapsulates Perfetto's TraceProcessor. This is needed
6 // to prevent symbol conflicts between libtrace_processor and libperfetto.
7 
8 #ifndef BASE_TEST_TEST_TRACE_PROCESSOR_IMPL_H_
9 #define BASE_TEST_TEST_TRACE_PROCESSOR_IMPL_H_
10 
11 #include <memory>
12 #include "test_trace_processor_export.h"
13 #include "third_party/abseil-cpp/absl/status/status.h"
14 #include "third_party/abseil-cpp/absl/types/variant.h"
15 
16 namespace perfetto::trace_processor {
17 struct Config;
18 class TraceProcessor;
19 }  // namespace perfetto::trace_processor
20 
21 namespace base::test {
22 
23 // Chrome uses a custom memory allocator in non-component builds that manages
24 // all allocations on the heap. Since test_trace_processor is a separate library
25 // even in a non-component build, Chrome is not able to free memory allocated by
26 // the code in this library. This leads to crashes when e.g. vectors created
27 // here are passed to the code in Chrome tests.
28 // So we define a custom class to hold the result of the query execution.
29 // Since its destructor is defined in this library, it will be freed using
30 // the same allocator that was used to create it.
31 // See crbug.com/1453617 for more info.
32 class TEST_TRACE_PROCESSOR_EXPORT QueryResultOrError {
33  public:
34   using QueryResult = std::vector<std::vector<std::string>>;
35 
36   QueryResultOrError(const QueryResult& result);
37   QueryResultOrError(const std::string& error);
38 
ok()39   bool ok() const { return error_.empty(); }
40 
result()41   const QueryResult& result() const { return result_; }
42 
error()43   const std::string& error() const { return error_; }
44 
45   ~QueryResultOrError();
46 
47  private:
48   QueryResult result_;
49   std::string error_;
50 };
51 
52 class TEST_TRACE_PROCESSOR_EXPORT TestTraceProcessorImpl {
53  public:
54   // Note: All arguments must be received as refs/ptrs as receiving them
55   // as moved copies, on Windows, causes them to be destroyed in
56   // TEST_TRACE_PROCESSOR_IMPL's DLL after having been allocated in the
57   // caller's DLL which is not allowed.
58 
59   TestTraceProcessorImpl();
60   ~TestTraceProcessorImpl();
61 
62   TestTraceProcessorImpl(TestTraceProcessorImpl&& other);
63   TestTraceProcessorImpl& operator=(TestTraceProcessorImpl&& other);
64 
65   absl::Status ParseTrace(const std::vector<char>& raw_trace);
66 
67   // Runs the sql query on the parsed trace and returns the result as a
68   // vector of strings.
69   QueryResultOrError ExecuteQuery(const std::string& sql) const;
70 
71   using PerfettoSQLModule = std::vector<std::pair<std::string, std::string>>;
72   // Overrides PerfettoSQL module with |name| and |files| containing pairs of
73   // strings {include_key, sql_file_contents}.
74   absl::Status OverrideSqlModule(const std::string& name,
75                                  const PerfettoSQLModule& module);
76 
77  private:
78   absl::Status ParseTrace(std::unique_ptr<uint8_t[]> buf, size_t size);
79 
80   std::unique_ptr<perfetto::trace_processor::Config> config_;
81   std::unique_ptr<perfetto::trace_processor::TraceProcessor> trace_processor_;
82 };
83 
84 }  // namespace base::test
85 
86 #endif  // BASE_TEST_TEST_TRACE_PROCESSOR_IMPL_H_
87