Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nudge the user to kill programs using excessive CPU #1020

Merged
merged 12 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions compiler/base/orchestrator/src/coordinator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use futures::{
future::{BoxFuture, OptionFuture},
Future, FutureExt,
stream::BoxStream,
Future, FutureExt, StreamExt,
};
use once_cell::sync::Lazy;
use serde::Deserialize;
Expand All @@ -23,7 +24,7 @@ use tokio::{
task::{JoinHandle, JoinSet},
time::{self, MissedTickBehavior},
};
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::{io::SyncIoBridge, sync::CancellationToken};
use tracing::{instrument, trace, trace_span, warn, Instrument};

Expand Down Expand Up @@ -416,6 +417,25 @@ impl CargoTomlModifier for ExecuteRequest {
}
}

#[derive(Debug, Clone)]
pub struct ExecuteStatus {
pub resident_set_size_bytes: u64,
pub total_time_secs: f64,
}

impl From<CommandStatistics> for ExecuteStatus {
fn from(value: CommandStatistics) -> Self {
let CommandStatistics {
total_time_secs,
resident_set_size_bytes,
} = value;
Self {
resident_set_size_bytes,
total_time_secs,
}
}
}

#[derive(Debug, Clone)]
pub struct ExecuteResponse {
pub success: bool,
Expand Down Expand Up @@ -1257,6 +1277,19 @@ impl Container {
}
.boxed();

let status_rx = tokio_stream::wrappers::ReceiverStream::new(status_rx)
.map(|s| {
let CommandStatistics {
total_time_secs,
resident_set_size_bytes,
} = s;
ExecuteStatus {
resident_set_size_bytes,
total_time_secs,
}
})
.boxed();

Ok(ActiveExecution {
task,
stdin_tx,
Expand Down Expand Up @@ -1781,7 +1814,7 @@ pub struct ActiveExecution {
pub stdin_tx: mpsc::Sender<String>,
pub stdout_rx: mpsc::Receiver<String>,
pub stderr_rx: mpsc::Receiver<String>,
pub status_rx: mpsc::Receiver<CommandStatistics>,
pub status_rx: BoxStream<'static, ExecuteStatus>,
}

impl fmt::Debug for ActiveExecution {
Expand Down Expand Up @@ -3197,19 +3230,13 @@ mod tests {
stdin_tx: _,
stdout_rx,
stderr_rx,
mut status_rx,
status_rx,
} = coordinator
.begin_execute(token.clone(), request)
.await
.unwrap();

let statuses = async {
let mut statuses = Vec::new();
while let Some(s) = status_rx.recv().await {
statuses.push(s);
}
statuses
};
let statuses = status_rx.collect::<Vec<_>>();

let output = WithOutput::try_absorb(task, stdout_rx, stderr_rx);

Expand Down
6 changes: 3 additions & 3 deletions tests/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.5)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
capybara (3.39.2)
addressable
Expand Down Expand Up @@ -29,7 +29,7 @@ GEM
rack (3.0.8)
rack-test (2.1.0)
rack (>= 1.3)
regexp_parser (2.8.2)
regexp_parser (2.8.3)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
Expand All @@ -45,7 +45,7 @@ GEM
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubyzip (2.3.2)
selenium-webdriver (4.15.0)
selenium-webdriver (4.16.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
Expand Down
64 changes: 64 additions & 0 deletions tests/spec/features/excessive_execution_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'json'

require 'spec_helper'
require 'support/editor'
require 'support/playground_actions'

RSpec.feature "Excessive executions", type: :feature, js: true do
include PlaygroundActions

before do
visit "/?#{config_overrides}"
editor.set(code)
end

scenario "a notification is shown" do
within(:header) { click_on("Run") }
within(:notification, text: 'will be automatically killed') do
expect(page).to have_button 'Kill the process now'
expect(page).to have_button 'Allow the process to continue'
end
end

scenario "the process is automatically killed if nothing is done" do
within(:header) { click_on("Run") }
expect(page).to have_selector(:notification, text: 'will be automatically killed', wait: 2)
expect(page).to_not have_selector(:notification, text: 'will be automatically killed', wait: 4)
expect(page).to have_content("Exited with signal 9")
end

scenario "the process can continue running" do
within(:header) { click_on("Run") }
within(:notification, text: 'will be automatically killed') do
click_on 'Allow the process to continue'
end
within(:output, :stdout) do
expect(page).to have_content("Exited normally")
end
end

def editor
Editor.new(page)
end

def code
<<~EOF
use std::time::{Duration, Instant};

fn main() {
let start = Instant::now();
while start.elapsed() < Duration::from_secs(5) {}
println!("Exited normally");
}
EOF
end

def config_overrides
config = {
killGracePeriodS: 3.0,
excessiveExecutionTimeS: 0.5,
}

"whte_rbt.obj=#{config.to_json}"
end
end
4 changes: 4 additions & 0 deletions tests/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
css { '[data-test-id = "stdin"]' }
end

Capybara.add_selector(:notification) do
css { '[data-test-id = "notification"]' }
end

RSpec.configure do |config|
config.after(:example, :js) do
page.execute_script <<~JS
Expand Down
15 changes: 15 additions & 0 deletions ui/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ module.exports = {
code: 120,
},
],
'no-restricted-syntax': [
'error',
{
message: 'Use `useAppDispatch` instead',
selector: 'CallExpression[callee.name="useDispatch"]',
},
{
message: 'Use `useAppSelector` instead',
selector: 'CallExpression[callee.name="useSelector"]',
},
],
'no-trailing-spaces': 'error',
quotes: ['error', 'single'],

Expand Down Expand Up @@ -65,13 +76,17 @@ module.exports = {
'BuildMenu.tsx',
'ButtonSet.tsx',
'Header.tsx',
'Notifications.tsx',
'PopButton.tsx',
'Stdin.tsx',
'actions.ts',
'api.ts',
'compileActions.ts',
'configureStore.ts',
'editor/AceEditor.tsx',
'editor/SimpleEditor.tsx',
'hooks.ts',
'observer.ts',
'reducers/browser.ts',
'reducers/client.ts',
'reducers/code.ts',
Expand Down
4 changes: 4 additions & 0 deletions ui/frontend/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ node_modules
!BuildMenu.tsx
!ButtonSet.tsx
!Header.tsx
!Notifications.tsx
!PopButton.tsx
!Stdin.tsx
!actions.ts
!api.ts
!compileActions.ts
!configureStore.ts
!editor/AceEditor.tsx
!editor/SimpleEditor.tsx
!hooks.ts
!observer.ts
!reducers/browser.ts
!reducers/client.ts
!reducers/code.ts
Expand Down
15 changes: 7 additions & 8 deletions ui/frontend/AdvancedOptionsMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import React, { useCallback } from 'react';
import { useSelector, useDispatch } from 'react-redux';

import * as config from './reducers/configuration';
import { Either as EitherConfig, Select as SelectConfig } from './ConfigElement';
import MenuGroup from './MenuGroup';
import { State } from './reducers';
import * as selectors from './selectors';
import { Backtrace, Channel, Edition } from './types';
import { useAppDispatch, useAppSelector } from './hooks';

const AdvancedOptionsMenu: React.FC = () => {
const isEditionDefault = useSelector(selectors.isEditionDefault);
const edition = useSelector((state: State) => state.configuration.edition);
const isBacktraceSet = useSelector(selectors.getBacktraceSet);
const backtrace = useSelector((state: State) => state.configuration.backtrace);
const isEditionDefault = useAppSelector(selectors.isEditionDefault);
const edition = useAppSelector((state) => state.configuration.edition);
const isBacktraceSet = useAppSelector(selectors.getBacktraceSet);
const backtrace = useAppSelector((state) => state.configuration.backtrace);

const dispatch = useDispatch();
const dispatch = useAppDispatch();

const changeEdition = useCallback((e: Edition) => dispatch(config.changeEdition(e)), [dispatch]);
const changeBacktrace = useCallback((b: Backtrace) => dispatch(config.changeBacktrace(b)), [dispatch]);

const channel = useSelector((state: State) => state.configuration.channel);
const channel = useAppSelector((state) => state.configuration.channel);
const switchText = (channel !== Channel.Nightly) ? ' (will select nightly Rust)' : '';

return (
Expand Down
25 changes: 12 additions & 13 deletions ui/frontend/BuildMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useCallback } from 'react';
import { useSelector } from 'react-redux';

import ButtonMenuItem from './ButtonMenuItem';
import MenuAside from './MenuAside';
import MenuGroup from './MenuGroup';
import * as actions from './actions';
import { useAppDispatch } from './configureStore';
import { useAppDispatch, useAppSelector } from './hooks';
import * as selectors from './selectors';

import styles from './BuildMenu.module.css';
Expand All @@ -14,7 +13,7 @@ interface BuildMenuProps {
close: () => void;
}

const useDispatchAndClose = (action: () => actions.ThunkAction, close: () => void) => {
const useAppDispatchAndClose = (action: () => actions.ThunkAction, close: () => void) => {
const dispatch = useAppDispatch();

return useCallback(() => {
Expand All @@ -24,17 +23,17 @@ const useDispatchAndClose = (action: () => actions.ThunkAction, close: () => voi
};

const BuildMenu: React.FC<BuildMenuProps> = (props) => {
const isHirAvailable = useSelector(selectors.isHirAvailable);
const wasmLikelyToWork = useSelector(selectors.wasmLikelyToWork);
const isHirAvailable = useAppSelector(selectors.isHirAvailable);
const wasmLikelyToWork = useAppSelector(selectors.wasmLikelyToWork);

const compile = useDispatchAndClose(actions.performCompile, props.close);
const compileToAssembly = useDispatchAndClose(actions.performCompileToAssembly, props.close);
const compileToLLVM = useDispatchAndClose(actions.performCompileToLLVM, props.close);
const compileToMir = useDispatchAndClose(actions.performCompileToMir, props.close);
const compileToHir = useDispatchAndClose(actions.performCompileToNightlyHir, props.close);
const compileToWasm = useDispatchAndClose(actions.performCompileToWasm, props.close);
const execute = useDispatchAndClose(actions.performExecute, props.close);
const test = useDispatchAndClose(actions.performTest, props.close);
const compile = useAppDispatchAndClose(actions.performCompile, props.close);
const compileToAssembly = useAppDispatchAndClose(actions.performCompileToAssembly, props.close);
const compileToLLVM = useAppDispatchAndClose(actions.performCompileToLLVM, props.close);
const compileToMir = useAppDispatchAndClose(actions.performCompileToMir, props.close);
const compileToHir = useAppDispatchAndClose(actions.performCompileToNightlyHir, props.close);
const compileToWasm = useAppDispatchAndClose(actions.performCompileToWasm, props.close);
const execute = useAppDispatchAndClose(actions.performExecute, props.close);
const test = useAppDispatchAndClose(actions.performTest, props.close);

return (
<MenuGroup title="What do you want to do?">
Expand Down
17 changes: 8 additions & 9 deletions ui/frontend/ChannelMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { Fragment, useCallback } from 'react';
import { useSelector, useDispatch } from 'react-redux';

import MenuGroup from './MenuGroup';
import SelectOne from './SelectOne';

import * as config from './reducers/configuration';
import * as selectors from './selectors';
import State from './state';
import { Channel } from './types';
import { useAppDispatch, useAppSelector } from './hooks';

import styles from './ChannelMenu.module.css';

Expand All @@ -16,14 +15,14 @@ interface ChannelMenuProps {
}

const ChannelMenu: React.FC<ChannelMenuProps> = props => {
const channel = useSelector((state: State) => state.configuration.channel);
const stableVersion = useSelector(selectors.stableVersionText);
const betaVersion = useSelector(selectors.betaVersionText);
const nightlyVersion = useSelector(selectors.nightlyVersionText);
const betaVersionDetails = useSelector(selectors.betaVersionDetailsText);
const nightlyVersionDetails = useSelector(selectors.nightlyVersionDetailsText);
const channel = useAppSelector((state) => state.configuration.channel);
const stableVersion = useAppSelector(selectors.stableVersionText);
const betaVersion = useAppSelector(selectors.betaVersionText);
const nightlyVersion = useAppSelector(selectors.nightlyVersionText);
const betaVersionDetails = useAppSelector(selectors.betaVersionDetailsText);
const nightlyVersionDetails = useAppSelector(selectors.nightlyVersionDetailsText);

const dispatch = useDispatch();
const dispatch = useAppDispatch();
const changeChannel = useCallback((channel: Channel) => {
dispatch(config.changeChannel(channel));
props.close();
Expand Down
Loading