OSDN Git Service

Merge "simpleperf: allow more opened files."
[android-x86/system-extras.git] / perfprofd / cpuconfig.cc
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 <android-base/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   return android::base::GetProperty("init.svc.mpdecision", "") == "running";
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 {
84     W_ALOGW("open failed for %s", ss.str().c_str());
85   }
86 }
87
88 void HardwireCpuHelper::StopMpdecision()
89 {
90   if (!android::base::SetProperty("ctl.stop", "mpdecision")) {
91     W_ALOGE("setprop ctl.stop mpdecision failed");
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
100   if (!android::base::SetProperty("ctl.start", "mpdecision")) {
101     W_ALOGE("setprop ctl.start mpdecision failed");
102   }
103 }