| 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 | |
| Andreas Gampe | d9084f6 | 2018-01-10 11:37:20 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
| Andreas Gampe | dfd6794 | 2017-12-05 09:19:56 -0800 | [diff] [blame] | 28 | #include <android-base/properties.h> |
| Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 29 | |
| 30 | #include "cpuconfig.h" |
| Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 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 | { |
| Andreas Gampe | dfd6794 | 2017-12-05 09:19:56 -0800 | [diff] [blame] | 56 | return android::base::GetProperty("init.svc.mpdecision", "") == "running"; |
| Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | int HardwireCpuHelper::GetNumCores() |
| 61 | { |
| 62 | int ncores = -1; |
| 63 | std::string possible(SYSFSCPU "/possible"); |
| 64 | FILE *fp = fopen(possible.c_str(), "re"); |
| 65 | if (fp) { |
| 66 | unsigned lo = 0, hi = 0; |
| 67 | if (fscanf(fp, "%u-%u", &lo, &hi) == 2) { |
| 68 | ncores = hi - lo + 1; |
| 69 | } |
| 70 | fclose(fp); |
| 71 | } |
| 72 | return ncores; |
| 73 | } |
| 74 | |
| 75 | void HardwireCpuHelper::OnlineCore(int i, int onoff) |
| 76 | { |
| 77 | std::stringstream ss; |
| 78 | ss << SYSFSCPU "/cpu" << i << "/online"; |
| 79 | FILE *fp = fopen(ss.str().c_str(), "we"); |
| 80 | if (fp) { |
| 81 | fprintf(fp, onoff ? "1\n" : "0\n"); |
| 82 | fclose(fp); |
| 83 | } else { |
| Andreas Gampe | d9084f6 | 2018-01-10 11:37:20 -0800 | [diff] [blame] | 84 | PLOG(WARNING) << "open failed for " << ss.str(); |
| Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | void HardwireCpuHelper::StopMpdecision() |
| 89 | { |
| Andreas Gampe | dfd6794 | 2017-12-05 09:19:56 -0800 | [diff] [blame] | 90 | if (!android::base::SetProperty("ctl.stop", "mpdecision")) { |
| Andreas Gampe | d9084f6 | 2018-01-10 11:37:20 -0800 | [diff] [blame] | 91 | LOG(ERROR) << "setprop ctl.stop mpdecision failed"; |
| Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | void HardwireCpuHelper::RestartMpdecision() |
| 96 | { |
| 97 | // Don't try to offline the cores we previously onlined -- let |
| 98 | // mpdecision figure out what to do |
| 99 | |
| Andreas Gampe | dfd6794 | 2017-12-05 09:19:56 -0800 | [diff] [blame] | 100 | if (!android::base::SetProperty("ctl.start", "mpdecision")) { |
| Andreas Gampe | d9084f6 | 2018-01-10 11:37:20 -0800 | [diff] [blame] | 101 | LOG(ERROR) << "setprop ctl.start mpdecision failed"; |
| Than McIntosh | 07f00fd | 2015-04-17 15:10:43 -0400 | [diff] [blame] | 102 | } |
| 103 | } |