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<std::string,std::vector<int>> & freq_domains)30 std::string SerializeFreqDomains(
31     const std::map<std::string, std::vector<int>>& freq_domains) {
32   std::stringstream freq_domain_arg;
33   bool first_vector = true;
34 
35   for (const std::pair<std::string, 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<std::string, std::vector<int>> freq_domains;
54   std::string affinity_arg = "--cpu-affinity=";
55   std::string capacity_arg = "--cpu-capacity=";
56   std::string frequencies_arg = "--cpu-frequencies-khz=";
57   std::string cgroup_path_arg = "--vcpu-cgroup-path=";
58   std::string freq_domain_arg;
59 
60   const std::string parent_cgroup_path =
61       CF_EXPECT(GetValue<std::string>(vcpu_config_json, {"cgroup_path"}));
62   cgroup_path_arg += parent_cgroup_path;
63 
64   const Json::Value cpus_json =
65       CF_EXPECT(GetValue<Json::Value>(vcpu_config_json, {"cpus"}),
66                 "Missing vCPUs config!");
67 
68   // Get the number of vCPUs from the number of cpu configurations.
69   auto cpus = cpus_json.size();
70 
71   for (size_t i = 0; i < cpus; i++) {
72     if (i != 0) {
73       capacity_arg += ",";
74       affinity_arg += ":";
75       frequencies_arg += ";";
76     }
77 
78     std::string cpu_cluster = fmt::format("--cpu-cluster={}", i);
79 
80     // Assume that non-contiguous logical CPU ids are malformed.
81     std::string cpu = fmt::format("cpu{}", i);
82     const Json::Value cpu_json = CF_EXPECT(
83         GetValue<Json::Value>(cpus_json, {cpu}), "Missing vCPU config!");
84 
85     const std::string affinity =
86         CF_EXPECT(GetValue<std::string>(cpu_json, {"affinity"}));
87     std::string affine_arg = fmt::format("{}={}", i, affinity);
88 
89     const std::string freqs =
90         CF_EXPECT(GetValue<std::string>(cpu_json, {"frequencies"}));
91     std::string freq_arg = fmt::format("{}={}", i, freqs);
92 
93     const std::string capacity =
94         CF_EXPECT(GetValue<std::string>(cpu_json, {"capacity"}));
95     std::string cap_arg = fmt::format("{}={}", i, capacity);
96 
97     const std::string domain =
98         CF_EXPECT(GetValue<std::string>(cpu_json, {"freq_domain"}));
99 
100     freq_domains[domain].push_back(i);
101 
102     freq_domain_arg = SerializeFreqDomains(freq_domains);
103 
104     capacity_arg += cap_arg;
105     affinity_arg += affine_arg;
106     frequencies_arg += freq_arg;
107 
108     cpu_arguments.emplace_back(std::move(cpu_cluster));
109   }
110 
111   cpu_arguments.emplace_back(std::move(affinity_arg));
112   cpu_arguments.emplace_back(std::move(capacity_arg));
113   cpu_arguments.emplace_back(std::move(frequencies_arg));
114   cpu_arguments.emplace_back(std::move(cgroup_path_arg));
115   cpu_arguments.emplace_back("--virt-cpufreq-upstream");
116 
117   cpu_arguments.emplace_back(
118       fmt::format("--cpus={},freq-domains={}", cpus, freq_domain_arg));
119 
120   return cpu_arguments;
121 }
122 
123 }  // namespace cuttlefish
124