xref: /aosp_15_r20/frameworks/av/media/psh_utils/include/psh_utils/PowerStats.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 #pragma once
18 
19 #include "HealthStats.h"
20 
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace android::media::psh_utils {
26 
27 // See powerstats_util.proto and powerstats_util.pb.h
28 
29 struct PowerStats {
30     struct Metadata {
31         // Represents the start time measured in milliseconds since boot of the
32         // interval or point in time when stats were gathered.
33         uint64_t start_time_since_boot_ms;
34 
35         // Represents the start time measured in milliseconds since epoch of the
36         // interval or point in time when stats were gathered.
37         uint64_t start_time_epoch_ms;
38 
39         // In monotonic clock.
40         uint64_t start_time_monotonic_ms;
41 
42         // If PowerStats represent an interval, the duration field will be set will
43         // the millisecond duration of stats collection. It will be unset for point
44         // stats.
45         // This is in boottime.
46         uint64_t duration_ms;
47 
48         // This is in monotonic time, and does not include suspend.
49         uint64_t duration_monotonic_ms;
50 
51         std::string toString() const;
52 
53         Metadata operator+=(const Metadata& other);
54         Metadata operator-=(const Metadata& other);
55         Metadata operator+(const Metadata& other) const;
56         Metadata operator-(const Metadata& other) const;
57         bool operator==(const Metadata& other) const = default;
58     };
59 
60     struct StateResidency {
61         std::string entity_name;
62         std::string state_name;
63         uint64_t time_ms;
64         uint64_t entry_count;
65 
66         std::string toString() const;
67 
68         StateResidency operator+=(const StateResidency& other);
69         StateResidency operator-=(const StateResidency& other);
70         StateResidency operator+(const StateResidency& other) const;
71         StateResidency operator-(const StateResidency& other) const;
72         bool operator==(const StateResidency& other) const = default;
73     };
74 
75     struct RailEnergy {
76         std::string subsystem_name;
77         std::string rail_name;
78         uint64_t energy_uws;
79 
80         std::string toString() const;
81         RailEnergy operator+=(const RailEnergy& other);
82         RailEnergy operator-=(const RailEnergy& other);
83         RailEnergy operator+(const RailEnergy& other) const;
84         RailEnergy operator-(const RailEnergy& other) const;
85         bool operator==(const RailEnergy& other) const = default;
86     };
87 
88     HealthStats health_stats;
89 
90     std::string normalizedEnergy(const std::string& prefix = {}) const;
91 
92     // Returns {seconds, joules, watts} from all rails containing a matching string.
93     std::tuple<float, float, float> energyFrom(const std::string& railMatcher) const;
94     std::string toString(const std::string& prefix = {}) const;
95 
96     PowerStats operator+=(const PowerStats& other);
97     PowerStats operator-=(const PowerStats& other);
98     PowerStats operator+(const PowerStats& other) const;
99     PowerStats operator-(const PowerStats& other) const;
100     bool operator==(const PowerStats& other) const = default;
101 
102     Metadata metadata{};
103     // These are sorted by name.
104     std::vector<StateResidency> power_entity_state_residency;
105     std::vector<RailEnergy> rail_energy;
106 };
107 
108 } // namespace android::media::psh_utils
109