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/base/scoped_file.h"
24 #include "perfetto/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 DataSourceConfig;
31 class TraceWriter;
32 namespace base {
33 class TaskRunner;
34 }
35 
36 class AndroidPowerDataSource : public ProbesDataSource {
37  public:
38   static constexpr int kTypeId = 5;
39 
40   AndroidPowerDataSource(DataSourceConfig,
41                          base::TaskRunner*,
42                          TracingSessionID,
43                          std::unique_ptr<TraceWriter> writer);
44 
45   ~AndroidPowerDataSource() override;
46 
47   base::WeakPtr<AndroidPowerDataSource> GetWeakPtr() const;
48 
49   // ProbesDataSource implementation.
50   void Start() override;
51   void Flush(FlushRequestID, std::function<void()> callback) override;
52 
53  private:
54   struct DynamicLibLoader;
55 
56   void Tick();
57   void WriteBatteryCounters();
58   void WritePowerRailsData();
59 
60   base::TaskRunner* const task_runner_;
61   uint32_t poll_rate_ms_ = 0;
62   std::bitset<8> counters_enabled_;
63   bool rails_collection_enabled_;
64   bool rail_descriptors_logged_;
65   std::unique_ptr<TraceWriter> writer_;
66   std::unique_ptr<DynamicLibLoader> lib_;
67   base::WeakPtrFactory<AndroidPowerDataSource> weak_factory_;  // Keep last.
68 };
69 
70 }  // namespace perfetto
71 
72 #endif  // SRC_TRACED_PROBES_POWER_ANDROID_POWER_DATA_SOURCE_H_
73