• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 
17 #define LOG_TAG "libpixelpowerstats"
18 
19 #include <android-base/logging.h>
20 #include <android-base/properties.h>
21 
22 #include <pixelpowerstats/PowerStatsUtils.h>
23 #include <pixelpowerstats/WlanStateResidencyDataProvider.h>
24 #include <cstdio>
25 #include <cstring>
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 
WlanStateResidencyDataProvider(uint32_t id,std::string path)36 WlanStateResidencyDataProvider::WlanStateResidencyDataProvider(uint32_t id, std::string path)
37     : mPath(std::move(path)), mPowerEntityId(id) {}
38 
getResults(std::unordered_map<uint32_t,PowerEntityStateResidencyResult> & results)39 bool WlanStateResidencyDataProvider::getResults(
40         std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) {
41     PowerEntityStateResidencyResult result = {
42         .powerEntityId = mPowerEntityId,
43         .stateResidencyData = {{.powerEntityStateId = ACTIVE_ID},
44                                {.powerEntityStateId = DEEPSLEEP_ID}}};
45 
46     std::string wlanDriverStatus = android::base::GetProperty("wlan.driver.status", "unloaded");
47     if (wlanDriverStatus != "ok") {
48         LOG(ERROR) << __func__ << ": wlan is " << wlanDriverStatus;
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     }
60     size_t numFieldsRead = 0;
61     const size_t numFields = 4;
62     size_t len = 0;
63     char *line = nullptr;
64 
65     while ((numFieldsRead < numFields) && (getline(&line, &len, fp.get()) != -1)) {
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 
83     free(line);
84 
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 
getStateSpaces()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
107 }  // namespace android
108