Skip to content

Commit

Permalink
setup tokio runtime, remove old net stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
inodentry committed Aug 28, 2024
1 parent 12b8eb0 commit 7e9fb47
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 153 deletions.
6 changes: 6 additions & 0 deletions lib/app/mw_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ branch = "minewars"
[dependencies.iyes_cli]
git = "https://github.com/IyesGames/iyes_cli"
branch = "minewars"

[dependencies.tokio]
version = "1.38.0"
features = [
"full",
]
10 changes: 8 additions & 2 deletions lib/app/mw_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ pub mod prelude {
pub use bevy_asset_loader::prelude::*;
}

use mw_app_core::TokioRuntime;
use mw_engine::settings_manager::SettingsStore;
use settings::EngineSetupSettings;

use crate::prelude::*;

mod settings;
mod net;
mod user;

mod camera;
Expand Down Expand Up @@ -42,7 +42,6 @@ pub fn plugin(app: &mut App) {
crate::map::plugin,
crate::cit::plugin,
crate::player::plugin,
crate::net::plugin,
crate::settings::plugin,
crate::splash::plugin,
crate::ui::plugin,
Expand All @@ -64,6 +63,13 @@ pub fn setup_bevy_app() -> App {
app.insert_resource(ClearColor(Color::BLACK));
let setup_settings = app.world().resource::<SettingsStore>()
.get::<EngineSetupSettings>().cloned().unwrap();
let tokio_rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(setup_settings.cpu_threads_net)
.thread_name("minewars-net")
.enable_all()
.build()
.expect("Could not set up tokio runtime.");
app.insert_resource(TokioRuntime(tokio_rt));
let bevy_plugins = DefaultPlugins;
let bevy_plugins = bevy_plugins.set(WindowPlugin {
primary_window: Some(Window {
Expand Down
10 changes: 0 additions & 10 deletions lib/app/mw_app/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
use crate::prelude::*;

pub fn plugin(app: &mut App) {
app.init_resource::<NetInfo>();
}

#[derive(Resource, Default)]
pub struct NetInfo {
pub rtt: Option<Duration>,
pub server_addr: Option<SocketAddr>,
}
49 changes: 17 additions & 32 deletions lib/app/mw_app/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,37 +148,22 @@ impl Setting for GameViewSettings {}
#[reflect(Setting)]
pub struct EngineSetupSettings {
pub pipelined_rendering: bool,
pub cpu_threads_compute_min: usize,
pub cpu_threads_compute_max: usize,
pub cpu_threads_compute_pct: f32,
pub cpu_threads_async_compute_min: usize,
pub cpu_threads_async_compute_max: usize,
pub cpu_threads_async_compute_pct: f32,
pub cpu_threads_io_min: usize,
pub cpu_threads_io_max: usize,
pub cpu_threads_io_pct: f32,
pub cpu_threads_net: usize,
pub cpu_threads_compute: usize,
pub cpu_threads_async_compute: usize,
pub cpu_threads_io: usize,
}

impl Default for EngineSetupSettings {
fn default() -> Self {
let physical = num_cpus::get_physical();
let logical = num_cpus::get();
let compute_require = if physical < 4 {
logical
} else {
physical
};
EngineSetupSettings {
pipelined_rendering: true,
cpu_threads_compute_min: compute_require,
cpu_threads_compute_max: logical,
cpu_threads_compute_pct: 100.0,
cpu_threads_async_compute_min: 2,
cpu_threads_async_compute_max: 4,
cpu_threads_async_compute_pct: 25.0,
cpu_threads_io_min: 2,
cpu_threads_io_max: 4,
cpu_threads_io_pct: 25.0,
cpu_threads_net: if physical > 4 { 4 } else { 2 },
cpu_threads_compute: if physical < 6 { logical } else { physical },
cpu_threads_async_compute: physical.min(logical).max(2),
cpu_threads_io: physical.min(logical).max(2),
}
}
}
Expand All @@ -189,19 +174,19 @@ impl From<&EngineSetupSettings> for TaskPoolOptions {
min_total_threads: 1,
max_total_threads: std::usize::MAX,
io: bevy::core::TaskPoolThreadAssignmentPolicy {
min_threads: s.cpu_threads_io_min,
max_threads: s.cpu_threads_io_max,
percent: s.cpu_threads_io_pct / 100.0,
min_threads: s.cpu_threads_io,
max_threads: s.cpu_threads_io,
percent: 1.0,
},
async_compute: bevy::core::TaskPoolThreadAssignmentPolicy {
min_threads: s.cpu_threads_async_compute_min,
max_threads: s.cpu_threads_async_compute_max,
percent: s.cpu_threads_async_compute_pct / 100.0,
min_threads: s.cpu_threads_async_compute,
max_threads: s.cpu_threads_async_compute,
percent: 1.0,
},
compute: bevy::core::TaskPoolThreadAssignmentPolicy {
min_threads: s.cpu_threads_compute_min,
max_threads: s.cpu_threads_compute_max,
percent: s.cpu_threads_compute_pct / 100.0,
min_threads: s.cpu_threads_compute,
max_threads: s.cpu_threads_compute,
percent: 1.0,
},
}
}
Expand Down
98 changes: 1 addition & 97 deletions lib/app/mw_app/src/ui/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,14 @@ use iyes_perf_ui::entry::PerfUiEntry;
use mw_app_core::map::{tile::TileOwner, GridCursor, GridCursorTileEntity};
use mw_common::{game::TileKind, grid::Pos, plid::PlayerId};

use crate::{net::NetInfo, prelude::*};
use crate::prelude::*;

pub fn plugin(app: &mut App) {
app.add_perf_ui_simple_entry::<PerfUiNetRtt>();
app.add_perf_ui_simple_entry::<PerfUiGridCursor>();
app.add_perf_ui_simple_entry::<PerfUiTileKind>();
app.add_perf_ui_simple_entry::<PerfUiTileOwner>();
}

/// Custom Perf UI entry to show Network Ping
#[derive(Component)]
pub struct PerfUiNetRtt {
/// The label text to display, to allow customization
pub label: String,
/// Should we display units?
pub display_units: bool,
/// Highlight the value if it goes above this threshold
pub threshold_highlight: Option<f32>,
/// Support color gradients!
pub color_gradient: ColorGradient,
/// Width for formatting the string
pub digits: u8,
/// Precision for formatting the string
pub precision: u8,
/// Required to ensure the entry appears in the correct place in the Perf UI
pub sort_key: i32,
}

impl Default for PerfUiNetRtt {
fn default() -> Self {
PerfUiNetRtt {
label: String::new(),
display_units: true,
threshold_highlight: Some(50.0),
color_gradient: ColorGradient::new_preset_gyr(10.0, 20.0, 40.0).unwrap(),
digits: 3,
precision: 2,
sort_key: iyes_perf_ui::utils::next_sort_key(),
}
}
}

/// Custom Perf UI entry to show Grid Cursor coordinates
#[derive(Component)]
pub struct PerfUiGridCursor {
Expand Down Expand Up @@ -100,68 +66,6 @@ impl Default for PerfUiTileOwner {
}
}

impl PerfUiEntry for PerfUiNetRtt {
type Value = f64;
type SystemParam = Option<SRes<NetInfo>>;

fn label(&self) -> &str {
if self.label.is_empty() {
"Network Ping"
} else {
&self.label
}
}

fn sort_key(&self) -> i32 {
self.sort_key
}

fn update_value(
&self,
netinfo: &mut <Self::SystemParam as SystemParam>::Item<'_, '_>,
) -> Option<Self::Value> {
netinfo.as_mut()
.and_then(|netinfo| netinfo.rtt)
.map(|rtt| rtt.as_secs_f64() * 1000.0)
}

fn format_value(
&self,
value: &Self::Value,
) -> String {
let mut s = iyes_perf_ui::utils::format_pretty_float(self.digits, self.precision, *value);
if self.display_units {
s.push_str(" ms");
}
s
}

fn width_hint(&self) -> usize {
let w = iyes_perf_ui::utils::width_hint_pretty_float(self.digits, self.precision);
if self.display_units {
w + 3
} else {
w
}
}

fn value_color(
&self,
value: &Self::Value,
) -> Option<Color> {
self.color_gradient.get_color_for_value(*value as f32)
}

fn value_highlight(
&self,
value: &Self::Value,
) -> bool {
self.threshold_highlight
.map(|t| (*value as f32) > t)
.unwrap_or(false)
}
}

impl PerfUiEntry for PerfUiTileKind {
type Value = TileKind;
type SystemParam = (
Expand Down
6 changes: 6 additions & 0 deletions lib/app/mw_app_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ features = ["assets"]
[dependencies.iyes_cli]
git = "https://github.com/IyesGames/iyes_cli"
branch = "minewars"

[dependencies.tokio]
version = "1.38.0"
features = [
"full",
]
11 changes: 11 additions & 0 deletions lib/app/mw_app_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ pub mod settings;

use crate::prelude::*;

/// Access to our Tokio Runtime, for things that need to run in tokio.
///
/// Currently this is the proprietary netcode, but there may also be
/// FOSS uses in the future, which is why this is defined here.
///
/// The tokio runtime is set up by `mw_app` along with its setup of
/// Bevy's runtime. It will insert this resource before running the
/// Bevy App. It can be assumed to always be available.
#[derive(Resource)]
pub struct TokioRuntime(pub tokio::runtime::Runtime);

pub fn plugin(app: &mut App) {
// external plugins
app.add_plugins((
Expand Down
10 changes: 0 additions & 10 deletions lib/app/mw_app_io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ tracing = "0.1.40"
version = "1.0.204"
features = [ "derive" ]

[dependencies.tokio]
version = "1.38.0"
features = [ "full" ]

[dependencies.tokio-util]
version = "0.7.11"
features = [
"full",
]

[dependencies.bevy]
version = "0.14.0"
default-features = false
Expand Down
2 changes: 0 additions & 2 deletions lib/app/mw_app_io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod prelude {

pub mod cli;
pub mod offline_host;
// pub mod net;
pub mod mwfile;

pub mod settings;
Expand All @@ -18,6 +17,5 @@ pub fn plugin(app: &mut App) {
app.add_plugins((
crate::cli::plugin,
crate::settings::plugin,
// crate::net::plugin,
));
}
Empty file.
Empty file removed lib/app/mw_app_io/src/net/host.rs
Empty file.

0 comments on commit 7e9fb47

Please sign in to comment.