• 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 ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
18 #define LOG_TAG "android.hardware.power@1.2-service.wahoo-libperfmgr"
19 
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 #include <android-base/strings.h>
24 #include <android-base/stringprintf.h>
25 
26 #include <mutex>
27 
28 #include <utils/Log.h>
29 #include <utils/Trace.h>
30 
31 #include "Power.h"
32 #include "power-helper.h"
33 
34 /* RPM runs at 19.2Mhz. Divide by 19200 for msec */
35 #define RPM_CLK 19200
36 
37 extern struct stat_pair rpm_stat_map[];
38 
39 namespace android {
40 namespace hardware {
41 namespace power {
42 namespace V1_2 {
43 namespace implementation {
44 
45 using ::android::hardware::power::V1_0::Feature;
46 using ::android::hardware::power::V1_0::PowerStatePlatformSleepState;
47 using ::android::hardware::power::V1_0::Status;
48 using ::android::hardware::power::V1_1::PowerStateSubsystem;
49 using ::android::hardware::power::V1_1::PowerStateSubsystemSleepState;
50 using ::android::hardware::hidl_vec;
51 using ::android::hardware::Return;
52 using ::android::hardware::Void;
53 
Power()54 Power::Power() :
55         mHintManager(HintManager::GetFromJSON("/vendor/etc/powerhint.json")),
56         mInteractionHandler(mHintManager),
57         mVRModeOn(false),
58         mSustainedPerfModeOn(false),
59         mEncoderModeOn(false) {
60     mInteractionHandler.Init();
61 
62     std::string state = android::base::GetProperty(kPowerHalStateProp, "");
63     if (state == "VIDEO_ENCODE") {
64         ALOGI("Initialize with VIDEO_ENCODE on");
65         mHintManager->DoHint("VIDEO_ENCODE");
66         mEncoderModeOn = true;
67     } else if (state ==  "SUSTAINED_PERFORMANCE") {
68         ALOGI("Initialize with SUSTAINED_PERFORMANCE on");
69         mHintManager->DoHint("SUSTAINED_PERFORMANCE");
70         mSustainedPerfModeOn = true;
71     } else if (state == "VR_MODE") {
72         ALOGI("Initialize with VR_MODE on");
73         mHintManager->DoHint("VR_MODE");
74         mVRModeOn = true;
75     } else if (state == "VR_SUSTAINED_PERFORMANCE") {
76         ALOGI("Initialize with SUSTAINED_PERFORMANCE and VR_MODE on");
77         mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
78         mSustainedPerfModeOn = true;
79         mVRModeOn = true;
80     } else {
81         ALOGI("Initialize PowerHAL");
82     }
83 
84     state = android::base::GetProperty(kPowerHalAudioProp, "");
85     if (state == "LOW_LATENCY") {
86         ALOGI("Initialize with AUDIO_LOW_LATENCY on");
87         mHintManager->DoHint("AUDIO_LOW_LATENCY");
88     }
89 }
90 
91 // Methods from ::android::hardware::power::V1_0::IPower follow.
setInteractive(bool)92 Return<void> Power::setInteractive(bool /* interactive */)  {
93     return Void();
94 }
95 
powerHint(PowerHint_1_0 hint,int32_t data)96 Return<void> Power::powerHint(PowerHint_1_0 hint, int32_t data) {
97     if (!isSupportedGovernor()) {
98         return Void();
99     }
100 
101     switch(hint) {
102         case PowerHint_1_0::INTERACTION:
103             if (mVRModeOn || mSustainedPerfModeOn) {
104                 ALOGV("%s: ignoring due to other active perf hints", __func__);
105             } else {
106                 mInteractionHandler.Acquire(data);
107             }
108             break;
109         case PowerHint_1_0::VIDEO_ENCODE:
110             if (mVRModeOn || mSustainedPerfModeOn) {
111                 ALOGV("%s: ignoring due to other active perf hints", __func__);
112                 break;
113             }
114             ATRACE_BEGIN("video_encode");
115             if (mVRModeOn || mSustainedPerfModeOn) {
116                 ALOGV("%s: ignoring due to other active perf hints", __func__);
117             } else {
118                 if (data) {
119                     // Hint until canceled
120                     ATRACE_INT("video_encode_lock", 1);
121                     mHintManager->DoHint("VIDEO_ENCODE");
122                     ALOGD("VIDEO_ENCODE ON");
123                     if (!android::base::SetProperty(kPowerHalStateProp, "VIDEO_ENCODE")) {
124                         ALOGE("%s: could not set powerHAL state property to VIDEO_ENCODE", __func__);
125                     }
126                     mEncoderModeOn = true;
127                 } else {
128                     ATRACE_INT("video_encode_lock", 0);
129                     mHintManager->EndHint("VIDEO_ENCODE");
130                     ALOGD("VIDEO_ENCODE OFF");
131                     if (!android::base::SetProperty(kPowerHalStateProp, "")) {
132                         ALOGE("%s: could not clear powerHAL state property", __func__);
133                     }
134                     mEncoderModeOn = false;
135                 }
136             }
137             ATRACE_END();
138             break;
139         case PowerHint_1_0::SUSTAINED_PERFORMANCE:
140             if (data && !mSustainedPerfModeOn) {
141                 ALOGD("SUSTAINED_PERFORMANCE ON");
142                 if (!mVRModeOn) { // Sustained mode only.
143                     mHintManager->DoHint("SUSTAINED_PERFORMANCE");
144                     if (!android::base::SetProperty(kPowerHalStateProp, "SUSTAINED_PERFORMANCE")) {
145                         ALOGE("%s: could not set powerHAL state property to SUSTAINED_PERFORMANCE", __func__);
146                     }
147                 } else { // Sustained + VR mode.
148                     mHintManager->EndHint("VR_MODE");
149                     mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
150                     if (!android::base::SetProperty(kPowerHalStateProp, "VR_SUSTAINED_PERFORMANCE")) {
151                         ALOGE("%s: could not set powerHAL state property to VR_SUSTAINED_PERFORMANCE", __func__);
152                     }
153                 }
154                 mSustainedPerfModeOn = true;
155             } else if (!data && mSustainedPerfModeOn) {
156                 ALOGD("SUSTAINED_PERFORMANCE OFF");
157                 mHintManager->EndHint("VR_SUSTAINED_PERFORMANCE");
158                 mHintManager->EndHint("SUSTAINED_PERFORMANCE");
159                 if (mVRModeOn) { // Switch back to VR Mode.
160                     mHintManager->DoHint("VR_MODE");
161                     if (!android::base::SetProperty(kPowerHalStateProp, "VR_MODE")) {
162                         ALOGE("%s: could not set powerHAL state property to VR_MODE", __func__);
163                     }
164                 } else {
165                     if (!android::base::SetProperty(kPowerHalStateProp, "")) {
166                         ALOGE("%s: could not clear powerHAL state property", __func__);
167                     }
168                 }
169                 mSustainedPerfModeOn = false;
170             }
171             break;
172         case PowerHint_1_0::VR_MODE:
173             if (data && !mVRModeOn) {
174                 ALOGD("VR_MODE ON");
175                 if (!mSustainedPerfModeOn) { // VR mode only.
176                     mHintManager->DoHint("VR_MODE");
177                     if (!android::base::SetProperty(kPowerHalStateProp, "VR_MODE")) {
178                         ALOGE("%s: could not set powerHAL state property to VR_MODE", __func__);
179                     }
180                 } else { // Sustained + VR mode.
181                     mHintManager->EndHint("SUSTAINED_PERFORMANCE");
182                     mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
183                     if (!android::base::SetProperty(kPowerHalStateProp, "VR_SUSTAINED_PERFORMANCE")) {
184                         ALOGE("%s: could not set powerHAL state property to VR_SUSTAINED_PERFORMANCE", __func__);
185                     }
186                 }
187                 mVRModeOn = true;
188             } else if (!data && mVRModeOn) {
189                 ALOGD("VR_MODE OFF");
190                 mHintManager->EndHint("VR_SUSTAINED_PERFORMANCE");
191                 mHintManager->EndHint("VR_MODE");
192                 if (mSustainedPerfModeOn) { // Switch back to sustained Mode.
193                     mHintManager->DoHint("SUSTAINED_PERFORMANCE");
194                     if (!android::base::SetProperty(kPowerHalStateProp, "SUSTAINED_PERFORMANCE")) {
195                         ALOGE("%s: could not set powerHAL state property to SUSTAINED_PERFORMANCE", __func__);
196                     }
197                 } else {
198                     if (!android::base::SetProperty(kPowerHalStateProp, "")) {
199                         ALOGE("%s: could not clear powerHAL state property", __func__);
200                     }
201                 }
202                 mVRModeOn = false;
203             }
204             break;
205         case PowerHint_1_0::LAUNCH:
206             ATRACE_BEGIN("launch");
207             if (mVRModeOn || mSustainedPerfModeOn) {
208                 ALOGV("%s: ignoring due to other active perf hints", __func__);
209             } else {
210                 if (data) {
211                     // Hint until canceled
212                     ATRACE_INT("launch_lock", 1);
213                     mHintManager->DoHint("LAUNCH");
214                     ALOGD("LAUNCH ON");
215                 } else {
216                     ATRACE_INT("launch_lock", 0);
217                     mHintManager->EndHint("LAUNCH");
218                     ALOGD("LAUNCH OFF");
219                 }
220             }
221             ATRACE_END();
222             break;
223         default:
224             break;
225 
226     }
227     return Void();
228 }
229 
setFeature(Feature,bool)230 Return<void> Power::setFeature(Feature /*feature*/, bool /*activate*/)  {
231     //Nothing to do
232     return Void();
233 }
234 
getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb)235 Return<void> Power::getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) {
236 
237     hidl_vec<PowerStatePlatformSleepState> states;
238     uint64_t stats[MAX_PLATFORM_STATS * MAX_RPM_PARAMS] = {0};
239     uint64_t *values;
240     struct PowerStatePlatformSleepState *state;
241     int ret;
242 
243     states.resize(PLATFORM_SLEEP_MODES_COUNT);
244 
245     ret = extract_platform_stats(stats);
246     if (ret != 0) {
247         states.resize(0);
248         goto done;
249     }
250 
251     /* Update statistics for XO_shutdown */
252     state = &states[RPM_MODE_XO];
253     state->name = "XO_shutdown";
254     values = stats + (RPM_MODE_XO * MAX_RPM_PARAMS);
255 
256     state->residencyInMsecSinceBoot = values[1];
257     state->totalTransitions = values[0];
258     state->supportedOnlyInSuspend = false;
259     state->voters.resize(XO_VOTERS);
260     for(size_t i = 0; i < XO_VOTERS; i++) {
261         int voter = static_cast<int>(i + XO_VOTERS_START);
262         state->voters[i].name = rpm_stat_map[voter].label;
263         values = stats + (voter * MAX_RPM_PARAMS);
264         state->voters[i].totalTimeInMsecVotedForSinceBoot = values[0] / RPM_CLK;
265         state->voters[i].totalNumberOfTimesVotedSinceBoot = values[1];
266     }
267 
268     /* Update statistics for VMIN state */
269     state = &states[RPM_MODE_VMIN];
270     state->name = "VMIN";
271     values = stats + (RPM_MODE_VMIN * MAX_RPM_PARAMS);
272 
273     state->residencyInMsecSinceBoot = values[1];
274     state->totalTransitions = values[0];
275     state->supportedOnlyInSuspend = false;
276     state->voters.resize(VMIN_VOTERS);
277     //Note: No filling of state voters since VMIN_VOTERS = 0
278 
279 done:
280     _hidl_cb(states, Status::SUCCESS);
281     return Void();
282 }
283 
get_wlan_low_power_stats(struct PowerStateSubsystem * subsystem)284 static int get_wlan_low_power_stats(struct PowerStateSubsystem *subsystem) {
285     uint64_t stats[WLAN_POWER_PARAMS_COUNT] = {0};
286     struct PowerStateSubsystemSleepState *state;
287 
288     subsystem->name = "wlan";
289 
290     if (extract_wlan_stats(stats) != 0) {
291         subsystem->states.resize(0);
292         return -1;
293     }
294 
295     subsystem->states.resize(WLAN_STATES_COUNT);
296 
297     /* Update statistics for Active State */
298     state = &subsystem->states[WLAN_STATE_ACTIVE];
299     state->name = "Active";
300     state->residencyInMsecSinceBoot = stats[CUMULATIVE_TOTAL_ON_TIME_MS];
301     state->totalTransitions = stats[DEEP_SLEEP_ENTER_COUNTER];
302     state->lastEntryTimestampMs = 0; //FIXME need a new value from Qcom
303     state->supportedOnlyInSuspend = false;
304 
305     /* Update statistics for Deep-Sleep state */
306     state = &subsystem->states[WLAN_STATE_DEEP_SLEEP];
307     state->name = "Deep-Sleep";
308     state->residencyInMsecSinceBoot = stats[CUMULATIVE_SLEEP_TIME_MS];
309     state->totalTransitions = stats[DEEP_SLEEP_ENTER_COUNTER];
310     state->lastEntryTimestampMs = stats[LAST_DEEP_SLEEP_ENTER_TSTAMP_MS];
311     state->supportedOnlyInSuspend = false;
312 
313     return 0;
314 }
315 
316 enum easel_state {
317     EASEL_OFF = 0,
318     EASEL_ON,
319     EASEL_SUSPENDED,
320     NUM_EASEL_STATES
321 };
322 
323 // Get low power stats for easel subsystem
get_easel_low_power_stats(struct PowerStateSubsystem * subsystem)324 static int get_easel_low_power_stats(struct PowerStateSubsystem *subsystem) {
325     // This implementation is a workaround to provide minimal visibility into
326     // Easel state behavior until canonical low power stats are supported.
327     // It takes an "external observer" snapshot of the current Easel state every
328     // time it is called, and synthesizes an artificial sleep state that will
329     // behave similarly to real stats if Easel gets "wedged" in the "on" state.
330     static std::mutex statsLock;
331     static uint64_t totalOnSnapshotCount = 0;
332     static uint64_t totalNotOnSnapshotCount = 0;
333     unsigned long currentState;
334     struct PowerStateSubsystemSleepState *state;
335 
336     subsystem->name = "Easel";
337 
338     if (get_easel_state(&currentState) != 0) {
339         subsystem->states.resize(0);
340         return -1;
341     }
342 
343     if (currentState >= NUM_EASEL_STATES) {
344         ALOGE("%s: unrecognized Easel state(%lu)", __func__, currentState);
345         return -1;
346     }
347 
348     subsystem->states.resize(1);
349 
350     // Since we are storing stats locally but can have multiple parallel
351     // callers, locking is required to ensure stats are not corrupted.
352     std::lock_guard<std::mutex> lk(statsLock);
353 
354     // Update statistics for synthetic sleep state.  We combine OFF and
355     // SUSPENDED to act as a composite "not on" state so the numbers will behave
356     // like a real sleep state.
357     if ((currentState == EASEL_OFF) || (currentState == EASEL_SUSPENDED)) {
358         totalNotOnSnapshotCount++;
359     } else {
360         totalOnSnapshotCount++;
361     }
362 
363     // Update statistics for synthetic sleep state, where
364     // totalTransitions = cumulative count of Easel state0 (as seen by PowerHAL)
365     // residencyInMsecsSinceBoot = cumulative count of Easel state1 (as seen by
366     //   PowerHAL)
367     // lastEntryTimestampMs = cumulative count of Easel state2 (as seen by
368     //   PowerHAL)
369     state = &subsystem->states[0];
370     state->name = "SyntheticSleep";
371     state->totalTransitions = totalOnSnapshotCount;
372     state->residencyInMsecSinceBoot = totalNotOnSnapshotCount;
373     state->lastEntryTimestampMs = 0;  // No added value for the workaround
374     state->supportedOnlyInSuspend = false;
375 
376     return 0;
377 }
378 
379 // Methods from ::android::hardware::power::V1_1::IPower follow.
getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb)380 Return<void> Power::getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) {
381     hidl_vec<PowerStateSubsystem> subsystems;
382 
383     subsystems.resize(SUBSYSTEM_COUNT);
384 
385     // Get WLAN subsystem low power stats.
386     if (get_wlan_low_power_stats(&subsystems[SUBSYSTEM_WLAN]) != 0) {
387         ALOGE("%s: failed to process wlan stats", __func__);
388     }
389 
390     // Get Easel subsystem low power stats.
391     if (get_easel_low_power_stats(&subsystems[SUBSYSTEM_EASEL]) != 0) {
392         ALOGE("%s: failed to process Easel stats", __func__);
393     }
394 
395     _hidl_cb(subsystems, Status::SUCCESS);
396     return Void();
397 }
398 
isSupportedGovernor()399 bool Power::isSupportedGovernor() {
400     std::string buf;
401     if (android::base::ReadFileToString("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", &buf)) {
402         buf = android::base::Trim(buf);
403     }
404     // Only support EAS 1.2, legacy EAS
405     if (buf == "schedutil" || buf == "sched") {
406         return true;
407     } else {
408         LOG(ERROR) << "Governor not supported by powerHAL, skipping";
409         return false;
410     }
411 }
412 
powerHintAsync(PowerHint_1_0 hint,int32_t data)413 Return<void> Power::powerHintAsync(PowerHint_1_0 hint, int32_t data) {
414     // just call the normal power hint in this oneway function
415     return powerHint(hint, data);
416 }
417 
418 // Methods from ::android::hardware::power::V1_2::IPower follow.
powerHintAsync_1_2(PowerHint_1_2 hint,int32_t data)419 Return<void> Power::powerHintAsync_1_2(PowerHint_1_2 hint, int32_t data) {
420     if (!isSupportedGovernor()) {
421         return Void();
422     }
423 
424     switch(hint) {
425         case PowerHint_1_2::AUDIO_LOW_LATENCY:
426             ATRACE_BEGIN("audio_low_latency");
427             if (data) {
428                 // Hint until canceled
429                 ATRACE_INT("audio_low_latency_lock", 1);
430                 mHintManager->DoHint("AUDIO_LOW_LATENCY");
431                 ALOGD("AUDIO LOW LATENCY ON");
432                 if (!android::base::SetProperty(kPowerHalAudioProp, "LOW_LATENCY")) {
433                     ALOGE("%s: could not set powerHAL audio state property to LOW_LATENCY", __func__);
434                 }
435             } else {
436                 ATRACE_INT("audio_low_latency_lock", 0);
437                 mHintManager->EndHint("AUDIO_LOW_LATENCY");
438                 ALOGD("AUDIO LOW LATENCY OFF");
439                 if (!android::base::SetProperty(kPowerHalAudioProp, "")) {
440                     ALOGE("%s: could not clear powerHAL audio state property", __func__);
441                 }
442             }
443             ATRACE_END();
444             break;
445         case PowerHint_1_2::AUDIO_STREAMING:
446             ATRACE_BEGIN("audio_streaming");
447             if (data) {
448                 // Hint until canceled
449                 ATRACE_INT("audio_streaming_lock", 1);
450                 mHintManager->DoHint("AUDIO_STREAMING");
451                 ALOGD("AUDIO STREAMING ON");
452             } else {
453                 ATRACE_INT("audio_streaming_lock", 0);
454                 mHintManager->EndHint("AUDIO_STREAMING");
455                 ALOGD("AUDIO STREAMING OFF");
456             }
457             ATRACE_END();
458             break;
459         case PowerHint_1_2::CAMERA_LAUNCH:
460             ATRACE_BEGIN("camera_launch");
461             if (data > 0) {
462                 ATRACE_INT("camera_launch_lock", 1);
463                 mHintManager->DoHint("CAMERA_LAUNCH", std::chrono::milliseconds(data));
464                 ALOGD("CAMERA LAUNCH ON: %d MS, LAUNCH ON: 2500 MS", data);
465                 // boosts 2.5s for launching
466                 mHintManager->DoHint("LAUNCH", std::chrono::milliseconds(2500));
467             } else if (data == 0) {
468                 ATRACE_INT("camera_launch_lock", 0);
469                 mHintManager->EndHint("CAMERA_LAUNCH");
470                 ALOGD("CAMERA LAUNCH OFF");
471             } else {
472                 ALOGE("CAMERA LAUNCH INVALID DATA: %d", data);
473             }
474             ATRACE_END();
475             break;
476         case PowerHint_1_2::CAMERA_STREAMING:
477             ATRACE_BEGIN("camera_streaming");
478             if (data > 0) {
479                 ATRACE_INT("camera_streaming_lock", 1);
480                 mHintManager->DoHint("CAMERA_STREAMING", std::chrono::milliseconds(data));
481                 ALOGD("CAMERA STREAMING ON: %d MS", data);
482             } else if (data == 0) {
483                 ATRACE_INT("camera_streaming_lock", 0);
484                 mHintManager->EndHint("CAMERA_STREAMING");
485                 ALOGD("CAMERA STREAMING OFF");
486             } else {
487                 ALOGE("CAMERA STREAMING INVALID DATA: %d", data);
488             }
489             ATRACE_END();
490             break;
491         case PowerHint_1_2::CAMERA_SHOT:
492             ATRACE_BEGIN("camera_shot");
493             if (data > 0) {
494                 ATRACE_INT("camera_shot_lock", 1);
495                 mHintManager->DoHint("CAMERA_SHOT", std::chrono::milliseconds(data));
496                 ALOGD("CAMERA SHOT ON: %d MS", data);
497             } else if (data == 0) {
498                 ATRACE_INT("camera_shot_lock", 0);
499                 mHintManager->EndHint("CAMERA_SHOT");
500                 ALOGD("CAMERA SHOT OFF");
501             } else {
502                 ALOGE("CAMERA SHOT INVALID DATA: %d", data);
503             }
504             ATRACE_END();
505             break;
506         default:
507             return powerHint(static_cast<PowerHint_1_0>(hint), data);
508     }
509     return Void();
510 }
511 
boolToString(bool b)512 constexpr const char* boolToString(bool b) {
513     return b ? "true" : "false";
514 }
515 
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)516 Return<void> Power::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
517     if (handle != nullptr && handle->numFds >= 1) {
518         int fd = handle->data[0];
519 
520         std::string buf(android::base::StringPrintf("HintManager Running: %s\n"
521                                                     "VRMode: %s\n"
522                                                     "SustainedPerformanceMode: %s\n"
523                                                     "VideoEncodeMode: %s\n",
524                                                     boolToString(mHintManager->IsRunning()),
525                                                     boolToString(mVRModeOn),
526                                                     boolToString(mSustainedPerfModeOn),
527                                                     boolToString(mEncoderModeOn)));
528         // Dump nodes through libperfmgr
529         mHintManager->DumpToFd(fd);
530         if (!android::base::WriteStringToFd(buf, fd)) {
531             PLOG(ERROR) << "Failed to dump state to fd";
532         }
533         fsync(fd);
534     }
535     return Void();
536 }
537 
538 }  // namespace implementation
539 }  // namespace V1_2
540 }  // namespace power
541 }  // namespace hardware
542 }  // namespace android
543