From b616fdb0671d611dc18cf6a826450dfae8c54af2 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Tue, 18 Feb 2020 21:16:53 -0700
Subject: [PATCH] Keep track of ticks each context uses

---
 src/context/context.rs    |  3 +++
 src/context/switch.rs     |  3 ++-
 src/scheme/sys/context.rs | 19 +++++++++++++++++--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/context/context.rs b/src/context/context.rs
index f144f600..aca7da5c 100644
--- a/src/context/context.rs
+++ b/src/context/context.rs
@@ -122,6 +122,8 @@ pub struct Context {
     pub running: bool,
     /// CPU ID, if locked
     pub cpu_id: Option<usize>,
+    /// Number of timer ticks executed
+    pub ticks: u64,
     /// Current system call
     pub syscall: Option<(usize, usize, usize, usize, usize, usize)>,
     /// Head buffer to use when system call buffers are not page aligned
@@ -197,6 +199,7 @@ impl Context {
             status: Status::Blocked,
             running: false,
             cpu_id: None,
+            ticks: 0,
             syscall: None,
             syscall_head,
             syscall_tail,
diff --git a/src/context/switch.rs b/src/context/switch.rs
index 0280a842..b7767804 100644
--- a/src/context/switch.rs
+++ b/src/context/switch.rs
@@ -67,7 +67,7 @@ pub unsafe fn switch() -> bool {
     use core::ops::DerefMut;
 
     //set PIT Interrupt counter to 0, giving each process same amount of PIT ticks
-    PIT_TICKS.store(0, Ordering::SeqCst);
+    let ticks = PIT_TICKS.swap(0, Ordering::SeqCst);
 
     // Set the global lock to avoid the unsafe operations below from causing issues
     while arch::CONTEXT_SWITCH_LOCK.compare_and_swap(false, true, Ordering::SeqCst) {
@@ -86,6 +86,7 @@ pub unsafe fn switch() -> bool {
                 .current()
                 .expect("context::switch: not inside of context");
             let mut context = context_lock.write();
+            context.ticks += ticks as u64 + 1; // Always round ticks up
             from_ptr = context.deref_mut() as *mut Context;
         }
 
diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs
index c25f40dd..5ff5bcfb 100644
--- a/src/scheme/sys/context.rs
+++ b/src/scheme/sys/context.rs
@@ -6,7 +6,7 @@ use crate::context;
 use crate::syscall::error::Result;
 
 pub fn resource() -> Result<Vec<u8>> {
-    let mut string = format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<8}{}\n",
+    let mut string = format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<8}{:<8}{}\n",
                              "PID",
                              "PGID",
                              "PPID",
@@ -18,6 +18,7 @@ pub fn resource() -> Result<Vec<u8>> {
                              "ENS",
                              "STAT",
                              "CPU",
+                             "TICKS",
                              "MEM",
                              "NAME");
     {
@@ -57,6 +58,19 @@ pub fn resource() -> Result<Vec<u8>> {
                 format!("?")
             };
 
+            let ticks = context.ticks;
+            let ticks_string = if ticks >= 1000 * 1000 * 1000 * 1000 {
+                format!("{} T", ticks / 1000 / 1000 / 1000 / 1000)
+            } else if ticks >= 1000 * 1000 * 1000 {
+                format!("{} G", ticks / 1000 / 1000 / 1000)
+            } else if ticks >= 1000 * 1000 {
+                format!("{} M", ticks / 1000 / 1000)
+            } else if ticks >= 1000 {
+                format!("{} K", ticks / 1000)
+            } else {
+                format!("{}", ticks)
+            };
+
             let mut memory = 0;
             if let Some(ref kfx) = context.kstack {
                 memory += kfx.len();
@@ -96,7 +110,7 @@ pub fn resource() -> Result<Vec<u8>> {
             let name_bytes = context.name.lock();
             let name = str::from_utf8(&name_bytes).unwrap_or("");
 
-            string.push_str(&format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<8}{}\n",
+            string.push_str(&format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<8}{:<8}{}\n",
                                context.id.into(),
                                context.pgid.into(),
                                context.ppid.into(),
@@ -108,6 +122,7 @@ pub fn resource() -> Result<Vec<u8>> {
                                context.ens.into(),
                                stat_string,
                                cpu_string,
+                               ticks_string,
                                memory_string,
                                name));
         }
-- 
GitLab