blob: 337da56ecad5b4f6b70a6598ea2c4ea6acc3ea25 [file] [log] [blame]
Than McIntosh07f00fd2015-04-17 15:10:43 -04001/*
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 Gamped9084f62018-01-10 11:37:20 -080027#include <android-base/logging.h>
Andreas Gampedfd67942017-12-05 09:19:56 -080028#include <android-base/properties.h>
Than McIntosh07f00fd2015-04-17 15:10:43 -040029
30#include "cpuconfig.h"
Than McIntosh07f00fd2015-04-17 15:10:43 -040031
32#define SYSFSCPU "/sys/devices/system/cpu"
33
34HardwireCpuHelper::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
47HardwireCpuHelper::~HardwireCpuHelper()
48{
49 if (mpdecision_stopped_) {
50 RestartMpdecision();
51 }
52}
53
54bool HardwireCpuHelper::GetMpdecisionRunning()
55{
Andreas Gampedfd67942017-12-05 09:19:56 -080056 return android::base::GetProperty("init.svc.mpdecision", "") == "running";
Than McIntosh07f00fd2015-04-17 15:10:43 -040057}
58
59
60int 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
75void 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 Gamped9084f62018-01-10 11:37:20 -080084 PLOG(WARNING) << "open failed for " << ss.str();
Than McIntosh07f00fd2015-04-17 15:10:43 -040085 }
86}
87
88void HardwireCpuHelper::StopMpdecision()
89{
Andreas Gampedfd67942017-12-05 09:19:56 -080090 if (!android::base::SetProperty("ctl.stop", "mpdecision")) {
Andreas Gamped9084f62018-01-10 11:37:20 -080091 LOG(ERROR) << "setprop ctl.stop mpdecision failed";
Than McIntosh07f00fd2015-04-17 15:10:43 -040092 }
93}
94
95void HardwireCpuHelper::RestartMpdecision()
96{
97 // Don't try to offline the cores we previously onlined -- let
98 // mpdecision figure out what to do
99
Andreas Gampedfd67942017-12-05 09:19:56 -0800100 if (!android::base::SetProperty("ctl.start", "mpdecision")) {
Andreas Gamped9084f62018-01-10 11:37:20 -0800101 LOG(ERROR) << "setprop ctl.start mpdecision failed";
Than McIntosh07f00fd2015-04-17 15:10:43 -0400102 }
103}