Skip to content

Commit

Permalink
Since the true_when field in the TimerShared struct actually has …
Browse files Browse the repository at this point in the history
…no effect, we should remove the `true_when` field.

It will be a win in memory type size, which reduces the size of `TimerShared` from 72 bytes to 64 bytes. It should be beneficial for memory cache.
  • Loading branch information
SebastianSchildt authored and wathenjiang committed May 15, 2024
1 parent 6fcd9c0 commit b76a36e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
4 changes: 2 additions & 2 deletions tokio/src/net/unix/ucred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl UCred {
))]
pub(crate) use self::impl_linux::get_peer_cred;

#[cfg(target_os = "netbsd")]
#[cfg(any(target_os = "netbsd", target_os = "nto"))]
pub(crate) use self::impl_netbsd::get_peer_cred;

#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
Expand Down Expand Up @@ -120,7 +120,7 @@ pub(crate) mod impl_linux {
}
}

#[cfg(target_os = "netbsd")]
#[cfg(any(target_os = "netbsd", target_os = "nto"))]
pub(crate) mod impl_netbsd {
use crate::net::unix::{self, UnixStream};

Expand Down
4 changes: 4 additions & 0 deletions tokio/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ impl Command {
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
pub fn uid(&mut self, id: u32) -> &mut Command {
#[cfg(target_os = "nto")]
let id = id as i32;
self.std.uid(id);
self
}
Expand All @@ -681,6 +683,8 @@ impl Command {
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
pub fn gid(&mut self, id: u32) -> &mut Command {
#[cfg(target_os = "nto")]
let id = id as i32;
self.std.gid(id);
self
}
Expand Down
5 changes: 0 additions & 5 deletions tokio/src/runtime/time/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ pub(crate) struct TimerShared {
/// registered.
cached_when: AtomicU64,

/// The true expiration time. Set by the timer future, read by the driver.
true_when: AtomicU64,

/// Current state. This records whether the timer entry is currently under
/// the ownership of the driver, and if not, its current state (not
/// complete, fired, error, etc).
Expand All @@ -356,7 +353,6 @@ unsafe impl Sync for TimerShared {}
impl std::fmt::Debug for TimerShared {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TimerShared")
.field("when", &self.true_when.load(Ordering::Relaxed))
.field("cached_when", &self.cached_when.load(Ordering::Relaxed))
.field("state", &self.state)
.finish()
Expand All @@ -375,7 +371,6 @@ impl TimerShared {
pub(super) fn new() -> Self {
Self {
cached_when: AtomicU64::new(0),
true_when: AtomicU64::new(0),
pointers: linked_list::Pointers::new(),
state: StateCell::default(),
_p: PhantomPinned,
Expand Down
46 changes: 18 additions & 28 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![cfg(all(feature = "full", tokio_unstable, not(target_os = "wasi")))]

use std::future::Future;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Barrier, Mutex};
use std::task::Poll;
use tokio::macros::support::poll_fn;

Expand Down Expand Up @@ -504,7 +504,7 @@ fn worker_overflow_count() {
}

#[test]
fn injection_queue_depth() {
fn injection_queue_depth_current_thread() {
use std::thread;

let rt = current_thread();
Expand All @@ -518,44 +518,34 @@ fn injection_queue_depth() {
.unwrap();

assert_eq!(1, metrics.injection_queue_depth());
}

#[test]
fn injection_queue_depth_multi_thread() {
let rt = threaded();
let handle = rt.handle().clone();
let metrics = rt.metrics();

// First we need to block the runtime workers
let (tx1, rx1) = std::sync::mpsc::channel();
let (tx2, rx2) = std::sync::mpsc::channel();
let (tx3, rx3) = std::sync::mpsc::channel();
let rx3 = Arc::new(Mutex::new(rx3));
let barrier1 = Arc::new(Barrier::new(3));
let barrier2 = Arc::new(Barrier::new(3));

rt.spawn(async move { rx1.recv().unwrap() });
rt.spawn(async move { rx2.recv().unwrap() });

// Spawn some more to make sure there are items
for _ in 0..10 {
let rx = rx3.clone();
// Spawn a task per runtime worker to block it.
for _ in 0..2 {
let barrier1 = barrier1.clone();
let barrier2 = barrier2.clone();
rt.spawn(async move {
rx.lock().unwrap().recv().unwrap();
barrier1.wait();
barrier2.wait();
});
}

thread::spawn(move || {
handle.spawn(async {});
})
.join()
.unwrap();
barrier1.wait();

let n = metrics.injection_queue_depth();
assert!(1 <= n, "{}", n);
assert!(15 >= n, "{}", n);

for _ in 0..10 {
tx3.send(()).unwrap();
for i in 0..10 {
assert_eq!(i, metrics.injection_queue_depth());
rt.spawn(async {});
}

tx1.send(()).unwrap();
tx2.send(()).unwrap();
barrier2.wait();
}

#[test]
Expand Down

0 comments on commit b76a36e

Please sign in to comment.