blob: d7a4e50c4d91020f29ecc8325638c88ab715da2e [file] [log] [blame]
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -08001/*
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 */
Benjamin Schwartzd7746c42020-09-08 14:59:32 -070016// TODO(b/167628903): Delete this file
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080017#define LOG_TAG "libpixelpowerstats"
18
19#include <android-base/logging.h>
Benjamin Schwartza1f868e2019-05-06 11:31:49 -070020#include <android-base/properties.h>
21
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080022#include <pixelpowerstats/PowerStatsUtils.h>
23#include <pixelpowerstats/WlanStateResidencyDataProvider.h>
Benjamin Schwartz6c6e7872019-01-02 15:38:12 -080024#include <cstdio>
25#include <cstring>
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080026
27namespace android {
28namespace hardware {
29namespace google {
30namespace pixel {
31namespace powerstats {
32
33const uint32_t ACTIVE_ID = 0;
34const uint32_t DEEPSLEEP_ID = 1;
35
Benjamin Schwartz30f7d1f2019-01-09 08:26:36 -080036WlanStateResidencyDataProvider::WlanStateResidencyDataProvider(uint32_t id, std::string path)
37 : mPath(std::move(path)), mPowerEntityId(id) {}
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080038
39bool WlanStateResidencyDataProvider::getResults(
Benjamin Schwartza1f868e2019-05-06 11:31:49 -070040 std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) {
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080041 PowerEntityStateResidencyResult result = {
42 .powerEntityId = mPowerEntityId,
43 .stateResidencyData = {{.powerEntityStateId = ACTIVE_ID},
44 {.powerEntityStateId = DEEPSLEEP_ID}}};
45
Benjamin Schwartza1f868e2019-05-06 11:31:49 -070046 std::string wlanDriverStatus = android::base::GetProperty("wlan.driver.status", "unloaded");
47 if (wlanDriverStatus != "ok") {
Benjamin Schwartz60027eb2021-04-01 14:16:04 -070048 LOG(DEBUG) << __func__ << ": wlan is " << wlanDriverStatus;
Benjamin Schwartza1f868e2019-05-06 11:31:49 -070049 // Return 0s for Wlan stats, because the driver is unloaded
50 results.insert(std::make_pair(mPowerEntityId, result));
51 return true;
52 }
53
54 // Using FILE* instead of std::ifstream for performance reasons (b/122253123)
55 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
56 if (!fp) {
57 PLOG(ERROR) << __func__ << ":Failed to open file " << mPath;
58 return false;
59 }
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080060 size_t numFieldsRead = 0;
61 const size_t numFields = 4;
Benjamin Schwartz6c6e7872019-01-02 15:38:12 -080062 size_t len = 0;
63 char *line = nullptr;
64
65 while ((numFieldsRead < numFields) && (getline(&line, &len, fp.get()) != -1)) {
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080066 uint64_t stat = 0;
67 if (utils::extractStat(line, "cumulative_sleep_time_ms:", stat)) {
68 result.stateResidencyData[1].totalTimeInStateMs = stat;
69 ++numFieldsRead;
70 } else if (utils::extractStat(line, "cumulative_total_on_time_ms:", stat)) {
71 result.stateResidencyData[0].totalTimeInStateMs = stat;
72 ++numFieldsRead;
73 } else if (utils::extractStat(line, "deep_sleep_enter_counter:", stat)) {
74 result.stateResidencyData[0].totalStateEntryCount = stat;
75 result.stateResidencyData[1].totalStateEntryCount = stat;
76 ++numFieldsRead;
77 } else if (utils::extractStat(line, "last_deep_sleep_enter_tstamp_ms:", stat)) {
78 result.stateResidencyData[1].lastEntryTimestampMs = stat;
79 ++numFieldsRead;
80 }
81 }
82
Benjamin Schwartz6c6e7872019-01-02 15:38:12 -080083 free(line);
84
Benjamin Schwartz3b2518d2018-11-28 09:34:02 -080085 // End of file was reached and not all state data was parsed. Something
86 // went wrong
87 if (numFieldsRead != numFields) {
88 LOG(ERROR) << __func__ << ": failed to parse stats for wlan";
89 return false;
90 }
91
92 results.insert(std::make_pair(mPowerEntityId, result));
93 return true;
94}
95
96std::vector<PowerEntityStateSpace> WlanStateResidencyDataProvider::getStateSpaces() {
97 return {
98 {.powerEntityId = mPowerEntityId,
99 .states = {{.powerEntityStateId = ACTIVE_ID, .powerEntityStateName = "Active"},
100 {.powerEntityStateId = DEEPSLEEP_ID, .powerEntityStateName = "Deep-Sleep"}}}};
101}
102
103} // namespace powerstats
104} // namespace pixel
105} // namespace google
106} // namespace hardware
Benjamin Schwartza1f868e2019-05-06 11:31:49 -0700107} // namespace android