• 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 
52  private:
53   struct DynamicLibLoader;
54 
55   void Tick();
56   void WriteBatteryCounters();
57   void WritePowerRailsData();
58   void WriteEnergyEstimationBreakdown();
59 
60   base::TaskRunner* const task_runner_;
61   uint32_t poll_interval_ms_ = 0;
62   std::bitset<8> counters_enabled_;
63   bool rails_collection_enabled_;
64   bool rail_descriptors_logged_;
65   bool energy_consumer_loggged_;
66   bool energy_breakdown_collection_enabled_;
67   std::unique_ptr<TraceWriter> writer_;
68   std::unique_ptr<DynamicLibLoader> lib_;
69   base::WeakPtrFactory<AndroidPowerDataSource> weak_factory_;  // Keep last.
70 };
71 
72 }  // namespace perfetto
73 
74 #endif  // SRC_TRACED_PROBES_POWER_ANDROID_POWER_DATA_SOURCE_H_
75