Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2015, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string> |
| 23 | #include <sstream> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include <cutils/properties.h> |
| 28 | |
| 29 | #include "cpuconfig.h" |
| 30 | #include "perfprofdutils.h" |
| 31 | |
| 32 | #define SYSFSCPU "/sys/devices/system/cpu" |
| 33 | |
| 34 | HardwireCpuHelper::HardwireCpuHelper(bool perform) |
| 35 | : mpdecision_stopped_(false) |
| 36 | { |
| 37 | if (perform && GetMpdecisionRunning()) { |
| 38 | mpdecision_stopped_ = true; |
| 39 | StopMpdecision(); |
| 40 | int ncores = GetNumCores(); |
| 41 | for (int i = 0; i < ncores; ++i) { |
| 42 | OnlineCore(i, 1); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | HardwireCpuHelper::~HardwireCpuHelper() |
| 48 | { |
| 49 | if (mpdecision_stopped_) { |
| 50 | RestartMpdecision(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | bool HardwireCpuHelper::GetMpdecisionRunning() |
| 55 | { |
| 56 | char propBuf[PROPERTY_VALUE_MAX]; |
| 57 | property_get("init.svc.mpdecision", propBuf, ""); |
| 58 | return strcmp(propBuf, "running") == 0; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int HardwireCpuHelper::GetNumCores() |
| 63 | { |
| 64 | int ncores = -1; |
| 65 | std::string possible(SYSFSCPU "/possible"); |
| 66 | FILE *fp = fopen(possible.c_str(), "re"); |
| 67 | if (fp) { |
| 68 | unsigned lo = 0, hi = 0; |
| 69 | if (fscanf(fp, "%u-%u", &lo, &hi) == 2) { |
| 70 | ncores = hi - lo + 1; |
| 71 | } |
| 72 | fclose(fp); |
| 73 | } |
| 74 | return ncores; |
| 75 | } |
| 76 | |
| 77 | void HardwireCpuHelper::OnlineCore(int i, int onoff) |
| 78 | { |
| 79 | std::stringstream ss; |
| 80 | ss << SYSFSCPU "/cpu" << i << "/online"; |
| 81 | FILE *fp = fopen(ss.str().c_str(), "we"); |
| 82 | if (fp) { |
| 83 | fprintf(fp, onoff ? "1\n" : "0\n"); |
| 84 | fclose(fp); |
| 85 | } else { |
| 86 | W_ALOGW("open failed for %s", ss.str().c_str()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void HardwireCpuHelper::StopMpdecision() |
| 91 | { |
| 92 | if (property_set("ctl.stop", "mpdecision")) { |
| 93 | W_ALOGE("setprop ctl.stop mpdecision failed"); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void HardwireCpuHelper::RestartMpdecision() |
| 98 | { |
| 99 | // Don't try to offline the cores we previously onlined -- let |
| 100 | // mpdecision figure out what to do |
| 101 | |
| 102 | if (property_set("ctl.start", "mpdecision")) { |
| 103 | W_ALOGE("setprop ctl.start mpdecision failed"); |
| 104 | } |
| 105 | } |