1 /*
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 
17 #ifndef SRC_TRACE_PROCESSOR_SQLITE_STATS_TABLE_H_
18 #define SRC_TRACE_PROCESSOR_SQLITE_STATS_TABLE_H_
19 
20 #include <limits>
21 #include <memory>
22 
23 #include "src/trace_processor/sqlite/sqlite_table.h"
24 #include "src/trace_processor/storage/stats.h"
25 #include "src/trace_processor/storage/trace_storage.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
30 // The stats table contains diagnostic info and errors that are either:
31 // - Collected at trace time (e.g., ftrace buffer overruns).
32 // - Generated at parsing time (e.g., clock events out-of-order).
33 class StatsTable : public SqliteTable {
34  public:
35   enum Column { kName = 0, kIndex, kSeverity, kSource, kValue, kDescription };
36   class Cursor : public SqliteTable::Cursor {
37    public:
38     Cursor(StatsTable*);
39 
40     // Implementation of SqliteTable::Cursor.
41     int Filter(const QueryConstraints&,
42                sqlite3_value**,
43                FilterHistory) override;
44     int Next() override;
45     int Eof() override;
46     int Column(sqlite3_context*, int N) override;
47 
48    private:
49     Cursor(Cursor&) = delete;
50     Cursor& operator=(const Cursor&) = delete;
51 
52     Cursor(Cursor&&) noexcept = default;
53     Cursor& operator=(Cursor&&) = default;
54 
55     StatsTable* table_ = nullptr;
56     const TraceStorage* storage_ = nullptr;
57     size_t key_ = 0;
58     TraceStorage::Stats::IndexMap::const_iterator index_{};
59   };
60 
61   static void RegisterTable(sqlite3* db, const TraceStorage* storage);
62 
63   StatsTable(sqlite3*, const TraceStorage*);
64 
65   // Table implementation.
66   util::Status Init(int, const char* const*, SqliteTable::Schema*) override;
67   std::unique_ptr<SqliteTable::Cursor> CreateCursor() override;
68   int BestIndex(const QueryConstraints&, BestIndexInfo*) override;
69 
70  private:
71   const TraceStorage* const storage_;
72 };
73 }  // namespace trace_processor
74 }  // namespace perfetto
75 
76 #endif  // SRC_TRACE_PROCESSOR_SQLITE_STATS_TABLE_H_
77