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 #define LOG_TAG "pixelstats: TempResidencyStats"
18
19 #include <aidl/android/frameworks/stats/IStats.h>
20 #include <android-base/chrono_utils.h>
21 #include <android-base/file.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/strings.h>
24 #include <android/binder_manager.h>
25 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
26 #include <pixelstats/TempResidencyReporter.h>
27 #include <utils/Log.h>
28
29 #include <cinttypes>
30
31 namespace android {
32 namespace hardware {
33 namespace google {
34 namespace pixel {
35
36 using aidl::android::frameworks::stats::IStats;
37 using aidl::android::frameworks::stats::VendorAtom;
38 using aidl::android::frameworks::stats::VendorAtomValue;
39 using android::base::ReadFileToString;
40 using android::base::WriteStringToFile;
41 using android::hardware::google::pixel::PixelAtoms::ThermalDfsStats;
42
updateOffsetAndCheckBound(int * offset,const int & bytes_read,const int & data_len)43 bool updateOffsetAndCheckBound(int *offset, const int &bytes_read, const int &data_len) {
44 *offset += bytes_read;
45 return *offset <= data_len;
46 }
47
48 /**
49 * Parse file_contents and read residency stats into stats.
50 */
parse_file_contents(std::string file_contents,std::map<std::string,TempResidencyStats> * stats)51 bool parse_file_contents(std::string file_contents,
52 std::map<std::string, TempResidencyStats> *stats) {
53 const char *data = file_contents.c_str();
54 int data_len = file_contents.length();
55 char sensor_name[32];
56 int offset = 0;
57 int bytes_read;
58
59 while (sscanf(data + offset, "THERMAL ZONE: %31s\n%n", sensor_name, &bytes_read) == 1) {
60 TempResidencyStats temp_residency_stats;
61 int64_t temp_res_value;
62 int num_stats_buckets;
63 int index = 0;
64 if (!updateOffsetAndCheckBound(&offset, bytes_read, data_len))
65 return false;
66
67 std::string sensor_name_str = sensor_name;
68
69 if (!sscanf(data + offset, "MAX_TEMP: %f\n%n", &temp_residency_stats.max_temp,
70 &bytes_read) ||
71 !updateOffsetAndCheckBound(&offset, bytes_read, data_len))
72 return false;
73
74 if (!sscanf(data + offset, "MAX_TEMP_TIMESTAMP: %" PRId64 "s\n%n",
75 &temp_residency_stats.max_temp_timestamp, &bytes_read) ||
76 !updateOffsetAndCheckBound(&offset, bytes_read, data_len))
77 return false;
78
79 if (!sscanf(data + offset, "MIN_TEMP: %f\n%n", &temp_residency_stats.min_temp,
80 &bytes_read) ||
81 !updateOffsetAndCheckBound(&offset, bytes_read, data_len))
82 return false;
83
84 if (!sscanf(data + offset, "MIN_TEMP_TIMESTAMP: %" PRId64 "s\n%n",
85 &temp_residency_stats.min_temp_timestamp, &bytes_read) ||
86 !updateOffsetAndCheckBound(&offset, bytes_read, data_len))
87 return false;
88
89 if (!sscanf(data + offset, "NUM_TEMP_RESIDENCY_BUCKETS: %d\n%n", &num_stats_buckets,
90 &bytes_read) ||
91 !updateOffsetAndCheckBound(&offset, bytes_read, data_len))
92 return false;
93
94 while (index < num_stats_buckets) {
95 if (sscanf(data + offset, "-inf - %*d ====> %" PRId64 "ms\n%n", &temp_res_value,
96 &bytes_read) != 1 &&
97 sscanf(data + offset, "%*d - %*d ====> %" PRId64 "ms\n%n", &temp_res_value,
98 &bytes_read) != 1 &&
99 sscanf(data + offset, "%*d - inf ====> %" PRId64 "ms\n\n%n", &temp_res_value,
100 &bytes_read) != 1)
101 return false;
102
103 temp_residency_stats.temp_residency_buckets.push_back(temp_res_value);
104 index++;
105
106 offset += bytes_read;
107 if ((offset >= data_len) && (index < num_stats_buckets))
108 return false;
109 }
110 (*stats)[sensor_name_str] = temp_residency_stats;
111 }
112 return true;
113 }
114
115 /**
116 * Logs the Temperature residency stats for every thermal zone.
117 */
logTempResidencyStats(const std::shared_ptr<IStats> & stats_client,std::string_view temperature_residency_path,std::string_view temperature_residency_reset_path)118 void TempResidencyReporter::logTempResidencyStats(
119 const std::shared_ptr<IStats> &stats_client, std::string_view temperature_residency_path,
120 std::string_view temperature_residency_reset_path) {
121 if (temperature_residency_path.empty() || temperature_residency_reset_path.empty()) {
122 ALOGV("TempResidency Stats/Reset path not specified");
123 return;
124 }
125 std::string file_contents;
126 if (!ReadFileToString(temperature_residency_path.data(), &file_contents)) {
127 ALOGE("Unable to read TempResidencyStatsPath");
128 return;
129 }
130 std::map<std::string, TempResidencyStats> stats_map;
131 if (!parse_file_contents(file_contents, &stats_map)) {
132 ALOGE("Fail to parse TempResidencyStatsPath");
133 return;
134 }
135 if (!stats_map.size())
136 return;
137 ::android::base::boot_clock::time_point curTime = ::android::base::boot_clock::now();
138 int64_t since_last_update_ms =
139 std::chrono::duration_cast<std::chrono::milliseconds>(curTime - prevTime).count();
140
141 // Reset the stats for next day collection and if failed return without reporting to report the
142 // combined residency next day.
143 if (!WriteStringToFile(std::to_string(1), temperature_residency_reset_path.data())) {
144 ALOGE("Failed to reset TempResidencyStats");
145 return;
146 }
147
148 auto stats_map_iterator = stats_map.begin();
149 VendorAtomValue tmp_atom_value;
150
151 // Iterate through stats_map by sensor_name
152 while (stats_map_iterator != stats_map.end()) {
153 std::vector<VendorAtomValue> values;
154 const auto &sensor_name_str = stats_map_iterator->first;
155 const auto &temp_residency_stats = stats_map_iterator->second;
156 const auto &temp_residency_buckets_count =
157 temp_residency_stats.temp_residency_buckets.size();
158 if (temp_residency_buckets_count > kMaxBucketLen) {
159 stats_map_iterator++;
160 continue;
161 }
162 tmp_atom_value.set<VendorAtomValue::stringValue>(sensor_name_str);
163 values.push_back(tmp_atom_value);
164 tmp_atom_value.set<VendorAtomValue::longValue>(since_last_update_ms);
165 values.push_back(tmp_atom_value);
166 // Iterate over every temperature residency buckets
167 for (const auto &temp_residency_bucket : temp_residency_stats.temp_residency_buckets) {
168 tmp_atom_value.set<VendorAtomValue::longValue>(temp_residency_bucket);
169 values.push_back(tmp_atom_value);
170 }
171 // Fill the remaining residency buckets with 0.
172 int remaining_residency_buckets_count = kMaxBucketLen - temp_residency_buckets_count;
173 if (remaining_residency_buckets_count > 0) {
174 tmp_atom_value.set<VendorAtomValue::longValue>(0);
175 values.insert(values.end(), remaining_residency_buckets_count, tmp_atom_value);
176 }
177 tmp_atom_value.set<VendorAtomValue::floatValue>(temp_residency_stats.max_temp);
178 values.push_back(tmp_atom_value);
179 tmp_atom_value.set<VendorAtomValue::longValue>(temp_residency_stats.max_temp_timestamp);
180 values.push_back(tmp_atom_value);
181 tmp_atom_value.set<VendorAtomValue::floatValue>(temp_residency_stats.min_temp);
182 values.push_back(tmp_atom_value);
183 tmp_atom_value.set<VendorAtomValue::longValue>(temp_residency_stats.min_temp_timestamp);
184 values.push_back(tmp_atom_value);
185 // Send vendor atom to IStats HAL
186 VendorAtom event = {.reverseDomainName = "",
187 .atomId = PixelAtoms::Atom::kVendorTempResidencyStats,
188 .values = std::move(values)};
189 ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
190 if (!ret.isOk())
191 ALOGE("Unable to report VendorTempResidencyStats to Stats service");
192
193 stats_map_iterator++;
194 }
195 prevTime = curTime;
196 }
197
198 } // namespace pixel
199 } // namespace google
200 } // namespace hardware
201 } // namespace android
202