Skip to content

Commit

Permalink
Rename uptime and realtime to boot and epoch time
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Oct 20, 2024
1 parent f8b368d commit 1e20614
Show file tree
Hide file tree
Showing 32 changed files with 114 additions and 112 deletions.
3 changes: 2 additions & 1 deletion doc/lisp.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ MOROS Lisp is a Lisp-1 dialect inspired by Scheme, Clojure, and Ruby!
- `read`, `write`, `append`
- `read-binary`, `write-binary`, `append-binary`
- `read-line`, `read-char`
- `uptime`, `realtime`
- `clock/boot`, `clock/epoch`
- `p`, `print`, `eprint`, `error`

### Math Library
Expand Down Expand Up @@ -176,6 +176,7 @@ Would produce the following output:

### Unreleased
- Add `dirname`, `filename`, `eprint`, and `error` functions
- Rename `uptime` to `clk/boot` and `realtime` to `clk/epoch`

### 0.7.1 (2024-06-20)
- Add `floor`, `ceil`, and `round` functions
Expand Down
16 changes: 8 additions & 8 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ commands to test the system or `install` to setup the
Created '/dev/ata/1/0'
Created '/dev/ata/1/1'
Created '/dev/clk'
Created '/dev/clk/uptime'
Created '/dev/clk/realtime'
Created '/dev/rtc'
Created '/dev/clk/boot'
Created '/dev/clk/epoch'
Created '/dev/clk/rtc'
Created '/dev/null'
Created '/dev/random'
Created '/dev/console'
Expand Down Expand Up @@ -270,7 +270,7 @@ You can print the date with `date`:
You can update the real time clock by writing the correct time to its device
file:

> print "2023-03-21 10:00:00" => /dev/rtc
> print "2023-03-21 10:00:00" => /dev/clk/rtc

> date
2023-03-21 10:00:00 +0000
Expand All @@ -297,12 +297,12 @@ Add `env TZ 7200` to `/ini/boot.sh` before `shell` to save the timezone:

There's a device file to get the number of seconds elapsed since Unix Epoch:

> read /dev/clk/realtime
> read /dev/clk/epoch
1682105344.624905

And another one since boot:

> read /dev/clk/uptime
> read /dev/clk/boot
1169.384929

## Aliases
Expand All @@ -312,7 +312,7 @@ You can add custom commands to the shell with the `alias` command.
For example you can define an `uptime` command that will read the device file
described above:

> alias uptime "read /dev/clk/uptime"
> alias uptime "read /dev/clk/boot"

> uptime
1406.304852
Expand Down Expand Up @@ -377,5 +377,5 @@ There is also a `ntp` script to synchronize the clock over the network:
> ntp
2023-03-21 10:00:00

> ntp => /dev/rtc
> ntp => /dev/clk/rtc
[12.111156] RTC 2023-03-21 10:00:00 +0000
2 changes: 1 addition & 1 deletion doc/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,5 @@ passed as an argument or defined in `/ini/ntp`:

It can be used to synchronize the real-time clock (RTC):

> ntp => /dev/rtc
> ntp => /dev/clk/rtc
[42.123456] RTC 2023-03-21 10:00:00 +0000
12 changes: 6 additions & 6 deletions dsk/lib/lisp/file.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@

# Clocks

(def (uptime)
"Returns the current value of the uptime clock"
(binary->number (read-binary "/dev/clk/uptime") "float"))
(def (clock/boot)
"Returns the number of seconds since boot"
(binary->number (read-binary "/dev/clk/boot") "float"))

(def (realtime)
"Returns the current value of the realtime clock"
(binary->number (read-binary "/dev/clk/realtime") "float"))
(def (clock/epoch)
"Returns the number of seconds since epoch"
(binary->number (read-binary "/dev/clk/epoch") "float"))

# Path

Expand Down
2 changes: 1 addition & 1 deletion dsk/tmp/lisp/geotime.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

(print
(if (= (len args) 1)
(geotime (str->num (first args)) (realtime))
(geotime (str->num (first args)) (clk/epoch))
(if (= (len args) 2)
(geotime (str->num (first args)) (str->num (second args)))
"Usage: geotime <longitude> [<timestamp>]")))
8 changes: 4 additions & 4 deletions src/api/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn read_float(path: &str) -> f64 {
0.0
}

pub fn uptime() -> f64 {
read_float("/dev/clk/uptime")
pub fn boot_time() -> f64 {
read_float("/dev/clk/boot")
}

pub fn realtime() -> f64 {
read_float("/dev/clk/realtime")
pub fn epoch_time() -> f64 {
read_float("/dev/clk/epoch")
}
6 changes: 3 additions & 3 deletions src/api/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ fn device_type(name: &str) -> Result<DeviceType, ()> {
"file" => Ok(DeviceType::File),
"console" => Ok(DeviceType::Console),
"random" => Ok(DeviceType::Random),
"uptime" => Ok(DeviceType::Uptime),
"realtime" => Ok(DeviceType::Realtime),
"rtc" => Ok(DeviceType::RTC),
"clk-boot" => Ok(DeviceType::BootTime),
"clk-epoch" => Ok(DeviceType::EpochTime),
"clk-rtc" => Ok(DeviceType::RTC),
"tcp" => Ok(DeviceType::TcpSocket),
"udp" => Ok(DeviceType::UdpSocket),
"vga-buffer" => Ok(DeviceType::VgaBuffer),
Expand Down
2 changes: 1 addition & 1 deletion src/api/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn now() -> OffsetDateTime {
}

pub fn now_utc() -> OffsetDateTime {
let s = clock::realtime(); // Since Unix Epoch
let s = clock::epoch_time(); // Since Unix Epoch
let ns = Duration::nanoseconds(
libm::floor(1e9 * (s - libm::floor(s))) as i64
);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/geocal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main(args: &[&str]) {
let timestamp = if args.len() == 4 {
args[3].parse().unwrap()
} else {
clock::realtime() as i64
clock::epoch_time() as i64
};

let week;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/geodate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main(args: &[&str]) {
let timestamp = if args.len() == 3 {
args[2].parse().expect("Could not parse timestamp")
} else {
clock::realtime()
clock::epoch_time()
};

let t = geodate::get_formatted_date(format, timestamp as i64, longitude);
Expand Down
4 changes: 2 additions & 2 deletions src/sys/ata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ impl Bus {
}

fn poll(&mut self, bit: Status, val: bool) -> Result<(), ()> {
let start = sys::clk::uptime();
let start = sys::clk::boot_time();
while self.status().get_bit(bit as usize) != val {
if sys::clk::uptime() - start > 1.0 {
if sys::clk::boot_time() - start > 1.0 {
debug!(
"ATA hanged while polling {:?} bit in status register",
bit
Expand Down
14 changes: 7 additions & 7 deletions src/sys/clk/boot.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::api::fs::{FileIO, IO};

#[derive(Debug, Clone)]
pub struct Uptime;
pub struct BootTime;

impl Uptime {
impl BootTime {
pub fn new() -> Self {
Self {}
}
Expand All @@ -13,9 +13,9 @@ impl Uptime {
}
}

impl FileIO for Uptime {
impl FileIO for BootTime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
let time = uptime().to_be_bytes();
let time = boot_time().to_be_bytes();
let n = time.len();
if buf.len() >= n {
buf[0..n].clone_from_slice(&time);
Expand All @@ -40,11 +40,11 @@ impl FileIO for Uptime {
}

// NOTE: This clock is monotonic
pub fn uptime() -> f64 {
pub fn boot_time() -> f64 {
super::time_between_ticks() * super::ticks() as f64
}

#[test_case]
fn test_uptime() {
assert!(uptime() > 0.0);
fn test_boot_time() {
assert!(boot_time() > 0.0);
}
14 changes: 7 additions & 7 deletions src/sys/clk/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const DAYS_BEFORE_MONTH: [u64; 13] = [
];

#[derive(Debug, Clone)]
pub struct Realtime;
pub struct EpochTime;

impl Realtime {
impl EpochTime {
pub fn new() -> Self {
Self {}
}
Expand All @@ -19,9 +19,9 @@ impl Realtime {
}
}

impl FileIO for Realtime {
impl FileIO for EpochTime {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
let time = realtime().to_be_bytes();
let time = epoch_time().to_be_bytes();
let n = time.len();
if buf.len() >= n {
buf[0..n].clone_from_slice(&time);
Expand All @@ -46,7 +46,7 @@ impl FileIO for Realtime {
}

// NOTE: This clock is not monotonic
pub fn realtime() -> f64 {
pub fn epoch_time() -> f64 {
let rtc = CMOS::new().rtc(); // Assuming GMT

let ts = 86400 * days_before_year(rtc.year as u64)
Expand Down Expand Up @@ -86,6 +86,6 @@ fn is_leap_year(year: u64) -> bool {
}

#[test_case]
fn test_realtime() {
assert!(realtime() > 1234567890.0);
fn test_epoch_time() {
assert!(epoch_time() > 1234567890.0);
}
6 changes: 3 additions & 3 deletions src/sys/clk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod rtc;
mod sleep;
mod timer;

pub use boot::{uptime, Uptime}; // TODO: Rename to boot_time
pub use epoch::{realtime, Realtime}; // TODO: Rename to epoch_time
pub use boot::{boot_time, BootTime}; // TODO: Rename to boot_time
pub use epoch::{epoch_time, EpochTime}; // TODO: Rename to epoch_time
pub use cmos::CMOS;
pub use rtc::RTC;
pub use sleep::{sleep, nanowait, halt};
Expand All @@ -27,7 +27,7 @@ pub fn init() {
}

pub fn log_rtc() {
let s = realtime();
let s = epoch_time();
let ns = Duration::nanoseconds(
libm::floor(1e9 * (s - libm::floor(s))) as i64
);
Expand Down
4 changes: 2 additions & 2 deletions src/sys/clk/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub fn halt() {
}

pub fn sleep(seconds: f64) {
let start = sys::clk::uptime();
while sys::clk::uptime() - start < seconds {
let start = sys::clk::boot_time();
while sys::clk::boot_time() - start < seconds {
halt();
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/sys/fs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::file::File;
use super::{dirname, filename, realpath, FileIO, IO};

use crate::sys::ata::Drive;
use crate::sys::clk::{RTC, Realtime, Uptime};
use crate::sys::clk::{RTC, EpochTime, BootTime};
use crate::sys::console::Console;
use crate::sys::net::socket::tcp::TcpSocket;
use crate::sys::net::socket::udp::UdpSocket;
Expand All @@ -23,8 +23,8 @@ pub enum DeviceType {
File = 1,
Console = 2,
Random = 3,
Uptime = 4,
Realtime = 5,
BootTime = 4,
EpochTime = 5,
RTC = 6,
TcpSocket = 7,
UdpSocket = 8,
Expand All @@ -44,8 +44,8 @@ impl TryFrom<&[u8]> for DeviceType {
1 => Ok(DeviceType::File),
2 => Ok(DeviceType::Console),
3 => Ok(DeviceType::Random),
4 => Ok(DeviceType::Uptime),
5 => Ok(DeviceType::Realtime),
4 => Ok(DeviceType::BootTime),
5 => Ok(DeviceType::EpochTime),
6 => Ok(DeviceType::RTC),
7 => Ok(DeviceType::TcpSocket),
8 => Ok(DeviceType::UdpSocket),
Expand All @@ -66,8 +66,8 @@ impl DeviceType {
pub fn buf(self) -> Vec<u8> {
let len = match self {
DeviceType::RTC => RTC::size(),
DeviceType::Uptime => Uptime::size(),
DeviceType::Realtime => Realtime::size(),
DeviceType::BootTime => BootTime::size(),
DeviceType::EpochTime => EpochTime::size(),
DeviceType::Console => Console::size(),
DeviceType::TcpSocket => TcpSocket::size(),
DeviceType::UdpSocket => UdpSocket::size(),
Expand All @@ -89,8 +89,8 @@ pub enum Device {
File(File),
Console(Console),
Random(Random),
Uptime(Uptime),
Realtime(Realtime),
BootTime(BootTime),
EpochTime(EpochTime),
RTC(RTC),
TcpSocket(TcpSocket),
UdpSocket(UdpSocket),
Expand All @@ -110,8 +110,8 @@ impl TryFrom<&[u8]> for Device {
DeviceType::File => Ok(Device::File(File::new())),
DeviceType::Console => Ok(Device::Console(Console::new())),
DeviceType::Random => Ok(Device::Random(Random::new())),
DeviceType::Uptime => Ok(Device::Uptime(Uptime::new())),
DeviceType::Realtime => Ok(Device::Realtime(Realtime::new())),
DeviceType::BootTime => Ok(Device::BootTime(BootTime::new())),
DeviceType::EpochTime => Ok(Device::EpochTime(EpochTime::new())),
DeviceType::RTC => Ok(Device::RTC(RTC::new())),
DeviceType::TcpSocket => Ok(Device::TcpSocket(TcpSocket::new())),
DeviceType::UdpSocket => Ok(Device::UdpSocket(UdpSocket::new())),
Expand Down Expand Up @@ -172,8 +172,8 @@ impl FileIO for Device {
Device::File(io) => io.read(buf),
Device::Console(io) => io.read(buf),
Device::Random(io) => io.read(buf),
Device::Uptime(io) => io.read(buf),
Device::Realtime(io) => io.read(buf),
Device::BootTime(io) => io.read(buf),
Device::EpochTime(io) => io.read(buf),
Device::RTC(io) => io.read(buf),
Device::TcpSocket(io) => io.read(buf),
Device::UdpSocket(io) => io.read(buf),
Expand All @@ -191,8 +191,8 @@ impl FileIO for Device {
Device::File(io) => io.write(buf),
Device::Console(io) => io.write(buf),
Device::Random(io) => io.write(buf),
Device::Uptime(io) => io.write(buf),
Device::Realtime(io) => io.write(buf),
Device::BootTime(io) => io.write(buf),
Device::EpochTime(io) => io.write(buf),
Device::RTC(io) => io.write(buf),
Device::TcpSocket(io) => io.write(buf),
Device::UdpSocket(io) => io.write(buf),
Expand All @@ -210,8 +210,8 @@ impl FileIO for Device {
Device::File(io) => io.close(),
Device::Console(io) => io.close(),
Device::Random(io) => io.close(),
Device::Uptime(io) => io.close(),
Device::Realtime(io) => io.close(),
Device::BootTime(io) => io.close(),
Device::EpochTime(io) => io.close(),
Device::RTC(io) => io.close(),
Device::TcpSocket(io) => io.close(),
Device::UdpSocket(io) => io.close(),
Expand All @@ -229,8 +229,8 @@ impl FileIO for Device {
Device::File(io) => io.poll(event),
Device::Console(io) => io.poll(event),
Device::Random(io) => io.poll(event),
Device::Uptime(io) => io.poll(event),
Device::Realtime(io) => io.poll(event),
Device::BootTime(io) => io.poll(event),
Device::EpochTime(io) => io.poll(event),
Device::RTC(io) => io.poll(event),
Device::TcpSocket(io) => io.poll(event),
Device::UdpSocket(io) => io.poll(event),
Expand Down
Loading

0 comments on commit 1e20614

Please sign in to comment.