1 /*
2 * Copyright (C) 2024 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 #include "host/libs/vm_manager/crosvm_cpu.h"
17
18 #include <string>
19 #include <vector>
20
21 #include <android-base/strings.h>
22 #include <json/value.h>
23
24 #include "common/libs/utils/json.h"
25 #include "common/libs/utils/result.h"
26
27 namespace cuttlefish {
28 namespace {
29
SerializeFreqDomains(const std::map<int,std::vector<int>> & freq_domains)30 std::string SerializeFreqDomains(
31 const std::map<int, std::vector<int>>& freq_domains) {
32 std::stringstream freq_domain_arg;
33 bool first_vector = true;
34
35 for (const std::pair<int, std::vector<int>>& pair : freq_domains) {
36 if (!first_vector) {
37 freq_domain_arg << ",";
38 }
39 first_vector = false;
40
41 freq_domain_arg << "[" << android::base::Join(pair.second, ",") << "]";
42 }
43
44 return {std::format("[{}]", freq_domain_arg.str())};
45 }
46
47 } // namespace
48
CrosvmCpuArguments(const Json::Value & vcpu_config_json)49 Result<std::vector<std::string>> CrosvmCpuArguments(
50 const Json::Value& vcpu_config_json) {
51 std::vector<std::string> cpu_arguments;
52
53 std::map<int, std::vector<int>> freq_domains;
54 std::string affinity_arg = "--cpu-affinity=";
55 std::string capacity_arg = "--cpu-capacity=";
56 std::string ipc_ratio_arg = "--cpu-ipc-ratio=";
57 std::string frequencies_arg = "--cpu-frequencies-khz=";
58 std::string cgroup_path_arg = "--vcpu-cgroup-path=";
59 std::string freq_domain_arg;
60
61 const std::string parent_cgroup_path =
62 CF_EXPECT(GetValue<std::string>(vcpu_config_json, {"cgroup_path"}));
63 cgroup_path_arg += parent_cgroup_path;
64
65 const Json::Value cpus_json =
66 CF_EXPECT(GetValue<Json::Value>(vcpu_config_json, {"cpus"}),
67 "Missing vCPUs config!");
68
69 // Get the number of vCPUs from the number of cpu configurations.
70 auto cpus = cpus_json.size();
71
72 for (size_t i = 0; i < cpus; i++) {
73 if (i != 0) {
74 capacity_arg += ",";
75 ipc_ratio_arg += ",";
76 affinity_arg += ":";
77 frequencies_arg += ";";
78 }
79
80 std::string cpu_cluster = fmt::format("--cpu-cluster={}", i);
81
82 // Assume that non-contiguous logical CPU ids are malformed.
83 std::string cpu = fmt::format("cpu{}", i);
84 const Json::Value cpu_json = CF_EXPECT(
85 GetValue<Json::Value>(cpus_json, {cpu}), "Missing vCPU config!");
86
87 const int affinity = CF_EXPECT(GetValue<int>(cpu_json, {"affinity"}));
88 std::string affine_arg = fmt::format("{}={}", i, affinity);
89
90 const std::string freqs =
91 CF_EXPECT(GetValue<std::string>(cpu_json, {"frequencies"}));
92 std::string freq_arg = fmt::format("{}={}", i, freqs);
93
94 const int capacity = CF_EXPECT(GetValue<int>(cpu_json, {"capacity"}));
95 std::string cap_arg = fmt::format("{}={}", i, capacity);
96
97 const int cpu_ipc_ratio = CF_EXPECT(GetValue<int>(cpu_json, {"ipc_ratio"}));
98 std::string ipc_arg = fmt::format("{}={}", i, cpu_ipc_ratio);
99
100 const int domain = CF_EXPECT(GetValue<int>(cpu_json, {"freq_domain"}));
101
102 freq_domains[domain].push_back(i);
103
104 freq_domain_arg = SerializeFreqDomains(freq_domains);
105
106 capacity_arg += cap_arg;
107 affinity_arg += affine_arg;
108 frequencies_arg += freq_arg;
109 ipc_ratio_arg += ipc_arg;
110
111 cpu_arguments.emplace_back(std::move(cpu_cluster));
112 }
113
114 cpu_arguments.emplace_back(std::move(affinity_arg));
115 cpu_arguments.emplace_back(std::move(capacity_arg));
116 cpu_arguments.emplace_back(std::move(ipc_ratio_arg));
117 cpu_arguments.emplace_back(std::move(frequencies_arg));
118 cpu_arguments.emplace_back(std::move(cgroup_path_arg));
119 cpu_arguments.emplace_back("--virt-cpufreq-upstream");
120
121 cpu_arguments.emplace_back(
122 fmt::format("--cpus={},freq-domains={}", cpus, freq_domain_arg));
123
124 return cpu_arguments;
125 }
126
127 } // namespace cuttlefish
128