Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 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 | */ |
Benjamin Schwartz | d7746c4 | 2020-09-08 14:59:32 -0700 | [diff] [blame] | 16 | // TODO(b/167628903): Delete this file |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 17 | #define LOG_TAG "libpixelpowerstats" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Benjamin Schwartz | a1f868e | 2019-05-06 11:31:49 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
| 21 | |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 22 | #include <pixelpowerstats/PowerStatsUtils.h> |
| 23 | #include <pixelpowerstats/WlanStateResidencyDataProvider.h> |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 24 | #include <cstdio> |
| 25 | #include <cstring> |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace google { |
| 30 | namespace pixel { |
| 31 | namespace powerstats { |
| 32 | |
| 33 | const uint32_t ACTIVE_ID = 0; |
| 34 | const uint32_t DEEPSLEEP_ID = 1; |
| 35 | |
Benjamin Schwartz | 30f7d1f | 2019-01-09 08:26:36 -0800 | [diff] [blame] | 36 | WlanStateResidencyDataProvider::WlanStateResidencyDataProvider(uint32_t id, std::string path) |
| 37 | : mPath(std::move(path)), mPowerEntityId(id) {} |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 38 | |
| 39 | bool WlanStateResidencyDataProvider::getResults( |
Benjamin Schwartz | a1f868e | 2019-05-06 11:31:49 -0700 | [diff] [blame] | 40 | std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) { |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 41 | PowerEntityStateResidencyResult result = { |
| 42 | .powerEntityId = mPowerEntityId, |
| 43 | .stateResidencyData = {{.powerEntityStateId = ACTIVE_ID}, |
| 44 | {.powerEntityStateId = DEEPSLEEP_ID}}}; |
| 45 | |
Benjamin Schwartz | a1f868e | 2019-05-06 11:31:49 -0700 | [diff] [blame] | 46 | std::string wlanDriverStatus = android::base::GetProperty("wlan.driver.status", "unloaded"); |
| 47 | if (wlanDriverStatus != "ok") { |
Benjamin Schwartz | 60027eb | 2021-04-01 14:16:04 -0700 | [diff] [blame] | 48 | LOG(DEBUG) << __func__ << ": wlan is " << wlanDriverStatus; |
Benjamin Schwartz | a1f868e | 2019-05-06 11:31:49 -0700 | [diff] [blame] | 49 | // 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 Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 60 | size_t numFieldsRead = 0; |
| 61 | const size_t numFields = 4; |
Benjamin Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 62 | size_t len = 0; |
| 63 | char *line = nullptr; |
| 64 | |
| 65 | while ((numFieldsRead < numFields) && (getline(&line, &len, fp.get()) != -1)) { |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 66 | 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 Schwartz | 6c6e787 | 2019-01-02 15:38:12 -0800 | [diff] [blame] | 83 | free(line); |
| 84 | |
Benjamin Schwartz | 3b2518d | 2018-11-28 09:34:02 -0800 | [diff] [blame] | 85 | // 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 | |
| 96 | std::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 Schwartz | a1f868e | 2019-05-06 11:31:49 -0700 | [diff] [blame] | 107 | } // namespace android |