• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #include "src/traced/probes/common/cpu_freq_info_for_testing.h"
18 
19 #include <dirent.h>
20 #include <sys/stat.h>
21 
22 #include <algorithm>
23 #include <memory>
24 
25 #include "perfetto/ext/base/file_utils.h"
26 #include "perfetto/ext/base/scoped_file.h"
27 #include "perfetto/ext/base/temp_file.h"
28 
29 namespace perfetto {
30 
31 namespace {
32 
33 const char kCpuFrequenciesAndroidLittleCore[] =
34     "300000 576000 748800 998400 1209600 1324800 1516800 1612800 1708800 \n";
35 
36 const char kCpuBoostFrequenciesAndroidLittleCore[] = "\n";
37 
38 const char kCpuFrequenciesAndroidBigCore[] =
39     "300000 652800 825600 979200 1132800 1363200 1536000 1747200 1843200 "
40     "1996800 \n";
41 
42 const char kCpuBoostFrequenciesAndroidBigCore[] = "2803200 \n";
43 
44 }  // namespace
45 
CpuFreqInfoForTesting()46 CpuFreqInfoForTesting::CpuFreqInfoForTesting()
47     : fake_cpu_dir_(base::TempDir::Create()) {
48   // Create a subset of /sys/devices/system/cpu.
49   AddDir("cpuidle");
50   AddDir("cpu0");
51   AddDir("cpu0/cpufreq");
52   AddFile("cpu0/cpufreq/scaling_available_frequencies",
53           kCpuFrequenciesAndroidLittleCore);
54   AddFile("cpu0/cpufreq/scaling_boost_frequencies",
55           kCpuBoostFrequenciesAndroidLittleCore);
56   AddDir("cpufreq");
57   AddDir("cpu1");
58   AddDir("cpu1/cpufreq");
59   AddFile("cpu1/cpufreq/scaling_available_frequencies",
60           kCpuFrequenciesAndroidBigCore);
61   AddFile("cpu1/cpufreq/scaling_boost_frequencies",
62           kCpuBoostFrequenciesAndroidBigCore);
63   AddDir("power");
64 }
65 
~CpuFreqInfoForTesting()66 CpuFreqInfoForTesting::~CpuFreqInfoForTesting() {
67   for (auto path : files_to_remove_)
68     RmFile(path);
69   std::reverse(dirs_to_remove_.begin(), dirs_to_remove_.end());
70   for (auto path : dirs_to_remove_)
71     RmDir(path);
72 }
73 
GetInstance()74 std::unique_ptr<CpuFreqInfo> CpuFreqInfoForTesting::GetInstance() {
75   return std::unique_ptr<CpuFreqInfo>(new CpuFreqInfo(fake_cpu_dir_.path()));
76 }
77 
AddDir(std::string path)78 void CpuFreqInfoForTesting::AddDir(std::string path) {
79   dirs_to_remove_.push_back(path);
80   mkdir(AbsolutePath(path).c_str(), 0755);
81 }
82 
AddFile(std::string path,std::string content)83 void CpuFreqInfoForTesting::AddFile(std::string path, std::string content) {
84   files_to_remove_.push_back(path);
85   base::ScopedFile fd(
86       base::OpenFile(AbsolutePath(path), O_WRONLY | O_CREAT | O_TRUNC, 0600));
87   PERFETTO_CHECK(base::WriteAll(fd.get(), content.c_str(), content.size()) ==
88                  static_cast<ssize_t>(content.size()));
89 }
90 
RmDir(std::string path)91 void CpuFreqInfoForTesting::RmDir(std::string path) {
92   PERFETTO_CHECK(rmdir(AbsolutePath(path).c_str()) == 0);
93 }
94 
RmFile(std::string path)95 void CpuFreqInfoForTesting::RmFile(std::string path) {
96   PERFETTO_CHECK(remove(AbsolutePath(path).c_str()) == 0);
97 }
98 
AbsolutePath(std::string path)99 std::string CpuFreqInfoForTesting::AbsolutePath(std::string path) {
100   return fake_cpu_dir_.path() + "/" + path;
101 }
102 
103 }  // namespace perfetto
104