Skip to content

Commit

Permalink
Check system_memory from recorded event not from host machine
Browse files Browse the repository at this point in the history
Summary:
Currently, `buck2 replay` wouldn't display memory warnings accurately because
-  we were getting system memory from the host machine but not from the actual machine we built on.
- we didn't pass the buckconfig to replay to pick threshold from.

With D58812046,  we store the actual system memory as an instant event. This diff ensures that we use that memory (instead of host's memory) to display warnings via using the host's buckconfig. This is not perfect as we don't store the threshold from the user's machine. This could be improved later but I don't think buckconfig should change that much tbh so not prioritizing it now.

Reviewed By: stepancheg

Differential Revision: D58812023

fbshipit-source-id: d53ebe7d8cc5e695a4a0409a460e3847b71862d4
  • Loading branch information
ezgicicek authored and facebook-github-bot committed Jun 26, 2024
1 parent c4af324 commit 69a8d08
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
3 changes: 1 addition & 2 deletions app/buck2_client/src/commands/log/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use buck2_client_ctx::replayer::Replayer;
use buck2_client_ctx::signal_handler::with_simple_sigint_handler;
use buck2_client_ctx::subscribers::get::get_console_with_root;
use buck2_client_ctx::subscribers::subscribers::EventSubscribers;
use buck2_common::init::SystemWarningConfig;

use crate::commands::log::options::EventLogOptions;

Expand Down Expand Up @@ -63,7 +62,7 @@ impl ReplayCommand {
let (replayer, invocation) =
Replayer::new(event_log.get(&ctx).await?, speed, preload).await?;

let system_warning_config = SystemWarningConfig::default();
let system_warning_config = ctx.system_warning_config()?;
let console = get_console_with_root(
invocation.trace_id,
console_opts.console_type,
Expand Down
9 changes: 9 additions & 0 deletions app/buck2_client_ctx/src/client_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use buck2_cli_proto::client_context::HostPlatformOverride as GrpcHostPlatformOve
use buck2_cli_proto::client_context::PreemptibleWhen as GrpcPreemptibleWhen;
use buck2_cli_proto::ClientContext;
use buck2_common::argv::Argv;
use buck2_common::init::SystemWarningConfig;
use buck2_common::invocation_paths::InvocationPaths;
use buck2_core::error::buck2_hard_error_env;
use buck2_core::fs::working_dir::WorkingDir;
Expand Down Expand Up @@ -214,4 +215,12 @@ impl<'a> ClientCommandContext<'a> {
pub fn allow_vpnless(&self) -> anyhow::Result<bool> {
Ok(self.immediate_config.daemon_startup_config()?.allow_vpnless)
}

pub fn system_warning_config(&self) -> anyhow::Result<SystemWarningConfig> {
Ok(self
.immediate_config
.daemon_startup_config()?
.system_warning_config
.clone())
}
}
1 change: 1 addition & 0 deletions app/buck2_client_ctx/src/subscribers/simpleconsole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ where

if let Some(memory_pressure) = check_memory_pressure(
&self.observer().two_snapshots().last,
&self.observer().system_info(),
self.system_warning_config.memory_pressure_threshold_percent,
) {
echo!("{}", system_memory_exceeded_msg(&memory_pressure))?;
Expand Down
2 changes: 2 additions & 0 deletions app/buck2_client_ctx/src/subscribers/superconsole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ impl<'s> Component for BuckRootComponent<'s> {
let mut draw = DrawVertical::new(dimensions);

let last_snapshot_tuple = &self.state.simple_console.observer.two_snapshots().last;
let system_info = &self.state.simple_console.observer.system_info();
{
draw.draw(
&SystemWarningComponent {
last_snapshot_tuple,
system_info,
system_warning_config: &self.state.config.system_warning_config,
},
mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::subscribers::system_warning::system_memory_exceeded_msg;
/// This component is used to display system warnings for a command e.g. memory pressure, low disk space etc.
pub(crate) struct SystemWarningComponent<'a, T> {
pub(crate) last_snapshot_tuple: &'a Option<(T, buck2_data::Snapshot)>,
pub(crate) system_info: &'a buck2_data::SystemInfo,
pub(crate) system_warning_config: &'a SystemWarningConfig,
}

Expand All @@ -37,6 +38,7 @@ impl<'a, T> Component for SystemWarningComponent<'a, T> {

if let Some(memory_pressure) = check_memory_pressure(
self.last_snapshot_tuple,
self.system_info,
self.system_warning_config.memory_pressure_threshold_percent,
) {
lines.push(warning_styled(&system_memory_exceeded_msg(
Expand Down
6 changes: 3 additions & 3 deletions app/buck2_client_ctx/src/subscribers/system_warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use buck2_core::is_open_source;
use buck2_event_observer::humanized::HumanizedBytes;
use buck2_util::system_stats::system_memory_stats;

use crate::subscribers::recorder::process_memory;

Expand All @@ -34,13 +33,14 @@ pub(crate) fn system_memory_exceeded_msg(memory_pressure: &MemoryPressureHigh) -

pub(crate) fn check_memory_pressure<T>(
snapshot_tuple: &Option<(T, buck2_data::Snapshot)>,
system_info: &buck2_data::SystemInfo,
memory_pressure_threshold_percent: Option<u64>,
) -> Option<MemoryPressureHigh> {
// TODO (ezgi): use the recorded threshold, not the one from host's buckconfig.
memory_pressure_threshold_percent.and_then(|memory_pressure_threshold_percent| {
snapshot_tuple.as_ref().and_then(|(_, snapshot)| {
process_memory(snapshot).and_then(|process_memory| {
// TODO(ezgi): We should check the recorded system memory, not the real one so that replay would show the warnings properly.
let system_total_memory = system_memory_stats();
let system_total_memory = system_info.system_total_memory_bytes;
if (process_memory * 100 / system_total_memory) >= memory_pressure_threshold_percent
{
Some(MemoryPressureHigh {
Expand Down
9 changes: 9 additions & 0 deletions app/buck2_event_observer/src/event_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct EventObserver<E> {
pub action_stats: ActionStats,
re_state: ReState,
two_snapshots: TwoSnapshots, // NOTE: We got many more copies of this than we should.
system_info: buck2_data::SystemInfo,
session_info: SessionInfo,
test_state: TestState,
starlark_debugger_state: StarlarkDebuggerState,
Expand All @@ -48,6 +49,7 @@ where
action_stats: ActionStats::default(),
re_state: ReState::new(),
two_snapshots: TwoSnapshots::default(),
system_info: buck2_data::SystemInfo::default(),
session_info: SessionInfo {
trace_id,
test_session: None,
Expand Down Expand Up @@ -119,6 +121,9 @@ where
self.session_info.modern_dice = true;
}
}
SystemInfo(system_info) => {
self.system_info = system_info.clone();
}
_ => {}
}
}
Expand Down Expand Up @@ -147,6 +152,10 @@ where
&self.two_snapshots
}

pub fn system_info(&self) -> &buck2_data::SystemInfo {
&self.system_info
}

pub fn session_info(&self) -> &SessionInfo {
&self.session_info
}
Expand Down

0 comments on commit 69a8d08

Please sign in to comment.