blob: 84a74f2ee498d5e03c245d92ebc9a5d6030f816e [file] [log] [blame]
Lalit Maganti11d851c2018-06-12 13:22:17 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ioannis Ilkoseff38f52018-10-29 10:37:55 +000017#ifndef SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
18#define SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_
Lalit Maganti11d851c2018-06-12 13:22:17 +010019
Primiano Tucci02c11762019-08-30 00:57:59 +020020#include <sqlite3.h>
21
Primiano Tucci24647142018-08-13 21:18:24 +020022#include <atomic>
Primiano Tucci5968caf2018-08-06 10:31:46 +010023#include <functional>
Primiano Tucci11a79e72019-10-29 22:13:10 +010024#include <string>
Lalit Maganti77095012019-02-19 13:13:59 +000025#include <vector>
Lalit Maganti11d851c2018-06-12 13:22:17 +010026
Primiano Tucci2c5488f2019-06-01 03:27:28 +010027#include "perfetto/ext/base/string_view.h"
Ioannis Ilkoseff38f52018-10-29 10:37:55 +000028#include "perfetto/trace_processor/basic_types.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010029#include "perfetto/trace_processor/status.h"
Ioannis Ilkosf8371292018-11-05 16:47:35 +000030#include "perfetto/trace_processor/trace_processor.h"
Lalit Maganti65800eb2020-01-30 15:18:59 +000031#include "src/trace_processor/sqlite/db_sqlite_table.h"
32#include "src/trace_processor/sqlite/query_cache.h"
Lalit Maganti03d10622019-08-27 14:47:39 +010033#include "src/trace_processor/sqlite/scoped_db.h"
Eric Secklera7870e62019-11-01 10:11:58 +000034#include "src/trace_processor/trace_processor_storage_impl.h"
Lalit Maganti11d851c2018-06-12 13:22:17 +010035
Eric Secklerc622b8b2019-10-21 09:54:00 +010036#include "src/trace_processor/metrics/metrics.h"
Lalit Maganti3d9bbff2020-04-15 13:40:58 +010037#include "src/trace_processor/util/descriptors.h"
Eric Secklerc622b8b2019-10-21 09:54:00 +010038
Lalit Maganti11d851c2018-06-12 13:22:17 +010039namespace perfetto {
40namespace trace_processor {
41
Primiano Tucci7e330292018-08-24 19:10:52 +020042// Coordinates the loading of traces from an arbitrary source and allows
Lalit Maganti11d851c2018-06-12 13:22:17 +010043// execution of SQL queries on the events in these traces.
Eric Secklera7870e62019-11-01 10:11:58 +000044class TraceProcessorImpl : public TraceProcessor,
45 public TraceProcessorStorageImpl {
Lalit Maganti11d851c2018-06-12 13:22:17 +010046 public:
Ioannis Ilkoseff38f52018-10-29 10:37:55 +000047 explicit TraceProcessorImpl(const Config&);
Lalit Maganti11d851c2018-06-12 13:22:17 +010048
Ioannis Ilkosf8371292018-11-05 16:47:35 +000049 ~TraceProcessorImpl() override;
Lalit Maganti11d851c2018-06-12 13:22:17 +010050
Eric Secklera7870e62019-11-01 10:11:58 +000051 // TraceProcessorStorage implementation:
Lalit Maganti1f067182019-05-09 14:50:05 +010052 util::Status Parse(std::unique_ptr<uint8_t[]>, size_t) override;
Ioannis Ilkosf8371292018-11-05 16:47:35 +000053 void NotifyEndOfFile() override;
Lalit Maganti11d851c2018-06-12 13:22:17 +010054
Eric Secklera7870e62019-11-01 10:11:58 +000055 // TraceProcessor implementation:
Lalit Magantiaac2f652019-04-30 12:16:21 +010056 Iterator ExecuteQuery(const std::string& sql,
57 int64_t time_queued = 0) override;
Lalit Maganti77095012019-02-19 13:13:59 +000058
Lalit Maganti5f8727d2019-05-24 13:38:50 +010059 util::Status RegisterMetric(const std::string& path,
60 const std::string& sql) override;
61
62 util::Status ExtendMetricsProto(const uint8_t* data, size_t size) override;
63
Lalit Magantid71a9452019-05-09 15:13:24 +010064 util::Status ComputeMetric(const std::vector<std::string>& metric_names,
65 std::vector<uint8_t>* metrics) override;
Lalit Magantid9f86b62019-04-08 11:11:51 +010066
Ioannis Ilkosf8371292018-11-05 16:47:35 +000067 void InterruptQuery() override;
Primiano Tucci24647142018-08-13 21:18:24 +020068
Primiano Tucci11a79e72019-10-29 22:13:10 +010069 size_t RestoreInitialTables() override;
70
Primiano Tucciee2ce1d2019-11-01 19:14:17 +010071 std::string GetCurrentTraceName() override;
72 void SetCurrentTraceName(const std::string&) override;
73
Lalit Maganti427b8332020-05-27 16:09:45 +010074 void EnableMetatrace() override;
75
76 util::Status DisableAndReadMetatrace(
77 std::vector<uint8_t>* trace_proto) override;
78
Lalit Maganti11d851c2018-06-12 13:22:17 +010079 private:
Lalit Maganti77095012019-02-19 13:13:59 +000080 // Needed for iterators to be able to delete themselves from the vector.
81 friend class IteratorImpl;
82
Lalit Maganti65800eb2020-01-30 15:18:59 +000083 template <typename Table>
84 void RegisterDbTable(const Table& table) {
Lalit Magantibdbf2d12020-02-12 14:54:16 +000085 DbSqliteTable::RegisterTable(*db_, query_cache_.get(), Table::Schema(),
86 &table, table.table_name());
Lalit Maganti65800eb2020-01-30 15:18:59 +000087 }
88
Lalit Maganti920f4f92020-02-19 13:45:05 +000089 void RegisterDynamicTable(
90 std::unique_ptr<DbSqliteTable::DynamicTableGenerator> generator) {
91 DbSqliteTable::RegisterTable(*db_, query_cache_.get(),
92 std::move(generator));
93 }
94
Eric Secklera7870e62019-11-01 10:11:58 +000095 ScopedDb db_;
Lalit Maganti65800eb2020-01-30 15:18:59 +000096 std::unique_ptr<QueryCache> query_cache_;
Primiano Tucci24647142018-08-13 21:18:24 +020097
Stephen Nuskobc76a6c2019-12-03 11:55:27 +000098 DescriptorPool pool_;
Lalit Maganti2553b802019-05-24 13:25:58 +010099 std::vector<metrics::SqlMetricFile> sql_metrics_;
100
Lalit Maganti77095012019-02-19 13:13:59 +0000101 std::vector<IteratorImpl*> iterators_;
102
Primiano Tucci24647142018-08-13 21:18:24 +0200103 // This is atomic because it is set by the CTRL-C signal handler and we need
104 // to prevent single-flow compiler optimizations in ExecuteQuery().
105 std::atomic<bool> query_interrupted_{false};
Primiano Tucci11a79e72019-10-29 22:13:10 +0100106
107 // Keeps track of the tables created by the ingestion process. This is used
108 // by RestoreInitialTables() to delete all the tables/view that have been
109 // created after that point.
110 std::vector<std::string> initial_tables_;
Primiano Tucciee2ce1d2019-11-01 19:14:17 +0100111
112 std::string current_trace_name_;
113 uint64_t bytes_parsed_ = 0;
Lalit Maganti11d851c2018-06-12 13:22:17 +0100114};
Lalit Maganti77095012019-02-19 13:13:59 +0000115
Lalit Maganti77095012019-02-19 13:13:59 +0000116
Lalit Maganti11d851c2018-06-12 13:22:17 +0100117} // namespace trace_processor
118} // namespace perfetto
119
Ioannis Ilkoseff38f52018-10-29 10:37:55 +0000120#endif // SRC_TRACE_PROCESSOR_TRACE_PROCESSOR_IMPL_H_