• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TRACED_PROBES_POWER_ANDROID_POWER_DATA_SOURCE_H_
18 #define SRC_TRACED_PROBES_POWER_ANDROID_POWER_DATA_SOURCE_H_
19 
20 #include <bitset>
21 #include <memory>
22 
23 #include "perfetto/ext/base/scoped_file.h"
24 #include "perfetto/ext/base/weak_ptr.h"
25 #include "perfetto/tracing/core/data_source_config.h"
26 #include "src/traced/probes/probes_data_source.h"
27 
28 namespace perfetto {
29 
30 class TraceWriter;
31 namespace base {
32 class TaskRunner;
33 }
34 
35 class AndroidPowerDataSource : public ProbesDataSource {
36  public:
37   static const ProbesDataSource::Descriptor descriptor;
38 
39   AndroidPowerDataSource(DataSourceConfig,
40                          base::TaskRunner*,
41                          TracingSessionID,
42                          std::unique_ptr<TraceWriter> writer);
43 
44   ~AndroidPowerDataSource() override;
45 
46   base::WeakPtr<AndroidPowerDataSource> GetWeakPtr() const;
47 
48   // ProbesDataSource implementation.
49   void Start() override;
50   void Flush(FlushRequestID, std::function<void()> callback) override;
51   void ClearIncrementalState() override;
52 
53  private:
54   struct DynamicLibLoader;
55 
56   void Tick();
57   void WriteBatteryCounters();
58   void WritePowerRailsData();
59   void WriteEnergyEstimationBreakdown();
60   void WriteEntityStateResidency();
61 
62   // Battery counters.
63   std::bitset<8> counters_enabled_;
64 
65   // Power rails.
66   bool rails_collection_enabled_ = false;
67 
68   // Energy estimation.
69   bool energy_breakdown_collection_enabled_ = false;
70 
71   // Entity state residency
72   bool entity_state_residency_collection_enabled_ = false;
73 
74   uint32_t poll_interval_ms_ = 0;
75   bool should_emit_descriptors_ = true;
76 
77   base::TaskRunner* const task_runner_;
78   std::unique_ptr<TraceWriter> writer_;
79   std::unique_ptr<DynamicLibLoader> lib_;
80   base::WeakPtrFactory<AndroidPowerDataSource> weak_factory_;  // Keep last.
81 };
82 
83 }  // namespace perfetto
84 
85 #endif  // SRC_TRACED_PROBES_POWER_ANDROID_POWER_DATA_SOURCE_H_
86