Skip to content

Commit 03c52fa

Browse files
daidavid1Databean
authored andcommitted
Move parent task to cgroup before other tasks are spawned
In addition to moving crosvm threads, move the parent spawning process before forks/execs occur. This ensures that vCPU threads get exclusive pCPU access. Bug: 408290580 Test: m && adb reboot and saw all threads moved
1 parent 6ff70da commit 03c52fa

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

base/cvd/cuttlefish/host/commands/run_cvd/boot_state_machine.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ DEFINE_int32(reboot_notification_fd, CF_DEFAULTS_REBOOT_NOTIFICATION_FD,
5555
namespace cuttlefish {
5656
namespace {
5757

58+
Result<void> MoveSelfToCgroup(const std::string& id) {
59+
auto to_path_file = "/sys/fs/cgroup/vsoc-" + id + "-cf/cgroup.procs";
60+
auto pid = std::to_string(getpid());
61+
SharedFD fd = SharedFD::Open(to_path_file, O_WRONLY | O_APPEND);
62+
CF_EXPECT(fd->IsOpen(),
63+
"failed to open " << to_path_file << ": " << fd->StrError());
64+
if (WriteAll(fd, pid) != pid.size()) {
65+
return CF_ERR("failed to write to" << to_path_file);
66+
}
67+
68+
return {};
69+
}
70+
5871
Result<void> MoveThreadsToCgroup(const std::string& from_path,
5972
const std::string& to_path) {
6073
std::string file_path = from_path + "/cgroup.threads";
@@ -210,6 +223,12 @@ Result<SharedFD> ProcessLeader(
210223
CF_EXPECT(SharedFD::Fifo(instance.restore_adbd_pipe_name(), 0600),
211224
"Unable to create adbd restore fifo");
212225
}
226+
227+
// Move to designated cgroup path when running with vcpufreq enabled.
228+
if (!instance.vcpu_config_path().empty()) {
229+
CF_EXPECT(MoveSelfToCgroup(instance.id()));
230+
}
231+
213232
/* These two paths result in pretty different process state, but both
214233
* achieve the same goal of making the current process the leader of a
215234
* process group, and are therefore grouped together. */

0 commit comments

Comments
 (0)