Home | History | Annotate | Download | only in trace_processor
      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_WINDOW_OPERATOR_TABLE_H_
     18 #define SRC_TRACE_PROCESSOR_WINDOW_OPERATOR_TABLE_H_
     19 
     20 #include <limits>
     21 #include <memory>
     22 
     23 #include "src/trace_processor/table.h"
     24 
     25 namespace perfetto {
     26 namespace trace_processor {
     27 
     28 class TraceStorage;
     29 
     30 class WindowOperatorTable : public Table {
     31  public:
     32   enum Column {
     33     kRowId = 0,
     34     kQuantum = 1,
     35     kWindowStart = 2,
     36     kWindowDur = 3,
     37     kTs = 4,
     38     kDuration = 5,
     39     kQuantumTs = 6
     40   };
     41   class Cursor : public Table::Cursor {
     42    public:
     43     Cursor(WindowOperatorTable*);
     44 
     45     // Implementation of Table::Cursor.
     46     int Filter(const QueryConstraints& qc, sqlite3_value**) override;
     47     int Next() override;
     48     int Eof() override;
     49     int Column(sqlite3_context*, int N) override;
     50 
     51    private:
     52     // Defines the data to be generated by the table.
     53     enum FilterType {
     54       // Returns all the spans.
     55       kReturnAll = 0,
     56       // Only returns the first span of the table. Useful for UPDATE operations.
     57       kReturnFirst = 1,
     58     };
     59 
     60     int64_t window_start_ = 0;
     61     int64_t window_end_ = 0;
     62     int64_t step_size_ = 0;
     63 
     64     int64_t current_ts_ = 0;
     65     int64_t quantum_ts_ = 0;
     66     int64_t row_id_ = 0;
     67 
     68     FilterType filter_type_ = FilterType::kReturnAll;
     69 
     70     WindowOperatorTable* table_ = nullptr;
     71   };
     72 
     73   static void RegisterTable(sqlite3* db, const TraceStorage* storage);
     74 
     75   WindowOperatorTable(sqlite3*, const TraceStorage*);
     76 
     77   // Table implementation.
     78   base::Optional<Table::Schema> Init(int, const char* const*) override;
     79   std::unique_ptr<Table::Cursor> CreateCursor() override;
     80   int BestIndex(const QueryConstraints&, BestIndexInfo*) override;
     81   int Update(int, sqlite3_value**, sqlite3_int64*) override;
     82 
     83  private:
     84   int64_t quantum_ = 0;
     85   int64_t window_start_ = 0;
     86 
     87   // max of int64_t because SQLite technically only supports int64s and not
     88   // uint64s.
     89   int64_t window_dur_ = std::numeric_limits<int64_t>::max();
     90 };
     91 }  // namespace trace_processor
     92 }  // namespace perfetto
     93 
     94 #endif  // SRC_TRACE_PROCESSOR_WINDOW_OPERATOR_TABLE_H_
     95