Skip to content

Commit 63f8893

Browse files
committed
Add type to represent --cpus
To reuse parsing for metrics gathering. Bug: 442026647
1 parent 30b8267 commit 63f8893

File tree

7 files changed

+125
-6
lines changed

7 files changed

+125
-6
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ cf_cc_library(
272272
"//cuttlefish/host/commands/assemble_cvd/flags:blank_data_image_mb",
273273
"//cuttlefish/host/commands/assemble_cvd/flags:boot_image",
274274
"//cuttlefish/host/commands/assemble_cvd/flags:bootloader",
275+
"//cuttlefish/host/commands/assemble_cvd/flags:cpus",
275276
"//cuttlefish/host/commands/assemble_cvd/flags:display_proto",
276277
"//cuttlefish/host/commands/assemble_cvd/flags:initramfs_path",
277278
"//cuttlefish/host/commands/assemble_cvd/flags:kernel_path",

base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd_flags.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
#define DEFINE_vec DEFINE_string
2929

30-
DEFINE_vec(cpus, std::to_string(CF_DEFAULTS_CPUS),
31-
"Virtual CPU count.");
3230
DEFINE_vec(data_policy, CF_DEFAULTS_DATA_POLICY,
3331
"How to handle userdata partition."
3432
" Either 'use_existing', 'create_if_missing', 'resize_up_to', or "

base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd_flags.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#define DECLARE_vec DECLARE_string
2222

23-
DECLARE_vec(cpus);
2423
DECLARE_vec(data_policy);
2524
DECLARE_vec(gdb_port);
2625

base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "cuttlefish/host/commands/assemble_cvd/flags/blank_data_image_mb.h"
5353
#include "cuttlefish/host/commands/assemble_cvd/flags/boot_image.h"
5454
#include "cuttlefish/host/commands/assemble_cvd/flags/bootloader.h"
55+
#include "cuttlefish/host/commands/assemble_cvd/flags/cpus.h"
5556
#include "cuttlefish/host/commands/assemble_cvd/flags/display_proto.h"
5657
#include "cuttlefish/host/commands/assemble_cvd/flags/initramfs_path.h"
5758
#include "cuttlefish/host/commands/assemble_cvd/flags/kernel_path.h"
@@ -455,7 +456,7 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
455456
vsock_guest_cid));
456457
std::vector<std::string> vsock_guest_group_vec =
457458
CF_EXPECT(GET_FLAG_STR_VALUE(vsock_guest_group));
458-
std::vector<int> cpus_vec = CF_EXPECT(GET_FLAG_INT_VALUE(cpus));
459+
CpusFlag cpus_values = CF_EXPECT(CpusFlag::FromGlobalGflags());
459460
BlankDataImageMbFlag blank_data_image_mb_values =
460461
CF_EXPECT(BlankDataImageMbFlag::FromGlobalGflags(guest_configs));
461462
std::vector<int> gdb_port_vec = CF_EXPECT(GET_FLAG_INT_VALUE(gdb_port));
@@ -861,10 +862,11 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
861862

862863
instance.set_session_id(iface_config.mobile_tap.session_id);
863864

864-
instance.set_cpus(cpus_vec[instance_index]);
865+
instance.set_cpus(cpus_values.ForIndex(instance_index));
865866
// make sure all instances have multiple of 2 then SMT mode
866867
// if any of instance doesn't have multiple of 2 then NOT SMT
867-
CF_EXPECT(!smt_vec[instance_index] || cpus_vec[instance_index] % 2 == 0,
868+
CF_EXPECT(!smt_vec[instance_index] ||
869+
cpus_values.ForIndex(instance_index) % 2 == 0,
868870
"CPUs must be a multiple of 2 in SMT mode");
869871
instance.set_smt(smt_vec[instance_index]);
870872

base/cvd/cuttlefish/host/commands/assemble_cvd/flags/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ cf_cc_library(
7777
],
7878
)
7979

80+
cf_cc_library(
81+
name = "cpus",
82+
srcs = ["cpus.cc"],
83+
hdrs = ["cpus.h"],
84+
deps = [
85+
"//cuttlefish/common/libs/utils:result",
86+
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
87+
"//libbase",
88+
"@gflags",
89+
],
90+
)
91+
8092
cf_cc_library(
8193
name = "display_proto",
8294
srcs = ["display_proto.cc"],
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (C) 2025 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 "cuttlefish/host/commands/assemble_cvd/flags/cpus.h"
18+
19+
#include <cstdlib>
20+
#include <string>
21+
#include <vector>
22+
23+
#include <android-base/parseint.h>
24+
#include <android-base/strings.h>
25+
#include <gflags/gflags.h>
26+
27+
#include "cuttlefish/common/libs/utils/result.h"
28+
#include "cuttlefish/host/commands/assemble_cvd/flags_defaults.h"
29+
30+
DEFINE_string(cpus, std::to_string(CF_DEFAULTS_CPUS), "Virtual CPU count.");
31+
32+
namespace cuttlefish {
33+
34+
Result<CpusFlag> CpusFlag::FromGlobalGflags() {
35+
gflags::CommandLineFlagInfo flag_info =
36+
gflags::GetCommandLineFlagInfoOrDie("cpus");
37+
int default_value;
38+
CF_EXPECTF(android::base::ParseInt(flag_info.default_value, &default_value),
39+
"Failed to parse value as integer: \"{}\"",
40+
flag_info.default_value);
41+
42+
std::vector<std::string> string_values =
43+
android::base::Split(flag_info.current_value, ",");
44+
std::vector<int> values(string_values.size());
45+
46+
for (int i = 0; i < string_values.size(); i++) {
47+
if (string_values[i] == "unset" || string_values[i] == "\"unset\"") {
48+
values[i] = default_value;
49+
} else {
50+
CF_EXPECTF(android::base::ParseInt(string_values[i], &values[i]),
51+
"Failed to parse value as integer: \"{}\"", string_values[i]);
52+
}
53+
}
54+
return CpusFlag(default_value, std::move(values));
55+
}
56+
57+
int CpusFlag::ForIndex(std::size_t index) const {
58+
if (index < cpus_values_.size()) {
59+
return cpus_values_[index];
60+
} else {
61+
return default_value_;
62+
}
63+
}
64+
65+
CpusFlag::CpusFlag(const int default_value, std::vector<int> cpus_values)
66+
: default_value_(default_value), cpus_values_(cpus_values) {}
67+
68+
} // namespace cuttlefish
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2025 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+
#pragma once
18+
19+
#include <cstdlib>
20+
#include <vector>
21+
22+
#include "cuttlefish/common/libs/utils/result.h"
23+
24+
namespace cuttlefish {
25+
26+
class CpusFlag {
27+
public:
28+
static Result<CpusFlag> FromGlobalGflags();
29+
30+
int ForIndex(std::size_t index) const;
31+
32+
private:
33+
CpusFlag(const int, std::vector<int>);
34+
35+
int default_value_ = 0;
36+
std::vector<int> cpus_values_;
37+
};
38+
39+
} // namespace cuttlefish

0 commit comments

Comments
 (0)