• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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_IMPORTERS_COMMON_TRACKS_INTERNAL_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACKS_INTERNAL_H_
19 
20 #include <array>
21 #include <cstddef>
22 #include <cstdint>
23 #include <string_view>
24 #include <tuple>
25 
26 #include "perfetto/ext/base/hash.h"
27 #include "src/trace_processor/containers/string_pool.h"
28 
29 namespace perfetto::trace_processor::tracks {
30 
31 template <typename... T>
32 using DimensionsT = std::tuple<T...>;
33 
34 struct DimensionBlueprintBase {
35   std::string_view name;
36 };
37 
38 template <typename T>
39 struct DimensionBlueprintT : DimensionBlueprintBase {
40   using type = T;
41 };
42 
43 struct NameBlueprintT {
44   struct Auto {
45     using name_t = std::nullptr_t;
46   };
47   struct Static {
48     using name_t = std::nullptr_t;
49     const char* name;
50   };
51   struct Dynamic {
52     using name_t = StringPool::Id;
53   };
54   struct FnBase {
55     using name_t = std::nullptr_t;
56   };
57   template <typename F>
58   struct Fn : FnBase {
59     F fn;
60   };
61 };
62 
63 struct BlueprintBase {
64   std::string_view event_type;
65   std::string_view type;
66   base::Hasher hasher;
67   std::array<DimensionBlueprintBase, 8> dimension_blueprints;
68 };
69 
70 template <typename NB, typename UB, typename... DB>
71 struct BlueprintT : BlueprintBase {
72   using name_blueprint_t = NB;
73   using unit_blueprint_t = UB;
74   using name_t = typename NB::name_t;
75   using unit_t = typename UB::unit_t;
76   using dimension_blueprints_t = std::tuple<DB...>;
77   using dimensions_t = DimensionsT<typename DB::type...>;
78   name_blueprint_t name_blueprint;
79   unit_blueprint_t unit_blueprint;
80 };
81 
82 template <typename... T>
83 using DimensionBlueprintsT = std::tuple<T...>;
84 
85 struct UnitBlueprintT {
86   struct Unknown {
87     using unit_t = std::nullptr_t;
88   };
89   struct Static {
90     using unit_t = const char*;
91     const char* name;
92   };
93   struct Dynamic {
94     using unit_t = StringPool::Id;
95   };
96 };
97 
98 template <typename BlueprintT, typename Dims>
HashFromBlueprintAndDimensions(const BlueprintT & bp,const Dims & dims)99 constexpr uint64_t HashFromBlueprintAndDimensions(const BlueprintT& bp,
100                                                   const Dims& dims) {
101   base::Hasher hasher(bp.hasher);
102   std::apply([&](auto&&... args) { ((hasher.Update(args)), ...); }, dims);
103   return hasher.digest();
104 }
105 
106 }  // namespace perfetto::trace_processor::tracks
107 
108 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_TRACKS_INTERNAL_H_
109