diff --git a/zircon-loader/src/lib.rs b/zircon-loader/src/lib.rs index 4a5ff45aa..40f6a4626 100644 --- a/zircon-loader/src/lib.rs +++ b/zircon-loader/src/lib.rs @@ -192,7 +192,10 @@ fn spawn(thread: Arc) { let mut cx = thread.wait_for_run().await; trace!("go to user: {:#x?}", cx); debug!("switch to {}|{}", thread.proc().name(), thread.name()); + let tmp_time = kernel_hal::timer_now().as_nanos(); kernel_hal::context_run(&mut cx); + let time = kernel_hal::timer_now().as_nanos() - tmp_time; + thread.time_add(time); trace!("back from user: {:#x?}", cx); EXCEPTIONS_USER.add(1); let mut exit = false; diff --git a/zircon-object/src/task/thread.rs b/zircon-object/src/task/thread.rs index 437eb3c12..539c69f7c 100644 --- a/zircon-object/src/task/thread.rs +++ b/zircon-object/src/task/thread.rs @@ -116,6 +116,8 @@ struct ThreadInner { /// NOTE: This variable will never be `Suspended`. On suspended, the /// `suspend_count` is non-zero, and this represents the state before suspended. state: ThreadState, + /// The time this thread has run on cpu + time: u128, } impl Thread { @@ -345,6 +347,14 @@ impl Thread { pub fn get_exceptionate(&self) -> Arc { self.exceptionate.clone() } + + pub fn time_add(&self, time: u128) { + self.inner.lock().time += time; + } + + pub fn get_time(&self) -> u64 { + self.inner.lock().time as u64 + } } pub trait IntoResult { diff --git a/zircon-syscall/src/time.rs b/zircon-syscall/src/time.rs index 4e608f245..619d13081 100644 --- a/zircon-syscall/src/time.rs +++ b/zircon-syscall/src/time.rs @@ -27,7 +27,10 @@ impl Syscall<'_> { time.write(timer_now().as_nanos() as u64 + UTC_OFFSET.load(Ordering::Relaxed))?; Ok(()) } - ZX_CLOCK_THREAD => unimplemented!(), + ZX_CLOCK_THREAD => { + time.write(self.thread.get_time())?; + Ok(()) + } _ => Err(ZxError::NOT_SUPPORTED), } }