1 /*
2  * Copyright (C) 2021 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_ANDROID_INTERNAL_POWER_STATS_H_
18 #define SRC_ANDROID_INTERNAL_POWER_STATS_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 // This header declares proxy functions defined in
24 // libperfetto_android_internal.so that allow traced_probes to access internal
25 // android functions (e.g., hwbinder).
26 // Do not add any include to either perfetto headers or android headers. See
27 // README.md for more.
28 
29 namespace perfetto {
30 namespace android_internal {
31 
32 const int32_t ALL_UIDS_FOR_CONSUMER = -1;
33 
34 struct RailDescriptor {
35   // Index corresponding to the rail
36   uint32_t index;
37   // Name of the rail
38   char rail_name[64];
39   // Name of the subsystem to which this rail belongs
40   char subsys_name[64];
41   // Hardware sampling rate
42   uint32_t sampling_rate;
43 };
44 
45 struct RailEnergyData {
46   // Index corresponding to RailDescriptor.index
47   uint32_t index;
48   // Time since device boot(CLOCK_BOOTTIME) in milli-seconds
49   uint64_t timestamp;
50   // Accumulated energy since device boot in microwatt-seconds (uWs)
51   uint64_t energy;
52 };
53 
54 struct EnergyConsumerInfo {
55   // Unique ID of this energy consumer.  Matches the ID in a
56   // EnergyEstimationBreakdown.
57   int32_t energy_consumer_id;
58 
59   // For a group of energy consumers of the same logical type, sorting by
60   // ordinal gives their physical order. Ordinals must be consecutive integers
61   // starting from 0.
62   int32_t ordinal;
63 
64   // Type of this energy consumer.
65   char type[64];
66 
67   // Unique name of this energy consumer. Vendor/device specific. Opaque to
68   // framework.
69   char name[64];
70 };
71 
72 struct EnergyEstimationBreakdown {
73   // Energy consumer ID.
74   int32_t energy_consumer_id;
75 
76   // Process uid.  ALL_UIDS_FOR_CONSUMER represents energy for all processes
77   // for the energy_consumer_id.
78   int32_t uid;
79 
80   // Energy usage in microwatts-second(µWs).
81   int64_t energy_uws;
82 };
83 
84 extern "C" {
85 
86 // These functions are not thread safe unless specified otherwise.
87 
88 bool __attribute__((visibility("default")))
89 GetAvailableRails(RailDescriptor*, size_t* size_of_arr);
90 
91 bool __attribute__((visibility("default")))
92 GetRailEnergyData(RailEnergyData*, size_t* size_of_arr);
93 
94 bool __attribute__((visibility("default")))
95 GetEnergyConsumerInfo(EnergyConsumerInfo* consumers, size_t* size_of_arr);
96 
97 // Retrieve the energy estimation breakdown for all energy consumer.  For each
98 // consumer, there will be an entry with a uid of ALL_UIDS_FOR_CONSUMER,
99 // followed by the energy breakdown for each process contributing to that
100 // consumer.
101 bool __attribute__((visibility("default")))
102 GetEnergyConsumed(EnergyEstimationBreakdown* breakdown, size_t* size_of_arr);
103 
104 }  // extern "C"
105 
106 }  // namespace android_internal
107 }  // namespace perfetto
108 
109 #endif  // SRC_ANDROID_INTERNAL_POWER_STATS_H_
110