adamu

adamu

System.system_time vs System.os_time

Does anybody know the practical implications of using System.os_time/0 vs System.system_time/0?

I notice DateTime.utc_now/0 uses os_time/0.

There is some mention of the differences in the docs for system_time/0:

It is the VM view of the os_time/0. They may not match in
case of time warps although the VM works towards aligning
them. This time is not monotonic.

Likewise, the Erlang docs also discuss some implications:

OS System Time

The operating systems view of POSIX time. To retrieve it, call os:system_time(). This may or may not be an accurate view of POSIX time. This time may typically be adjusted both backwards and forwards without limitation. That is, time warps may be observed.

Erlang System Time

The Erlang runtime systems view of POSIX time. To retrieve it, call erlang:system_time().

This time may or may not be an accurate view of POSIX time, and may or may not align with OS system time. The runtime system works towards aligning the two system times. Depending on the time warp mode used, this can be achieved by letting Erlang system time perform a time warp.

However, besides the talk of time warps and what exactly each of the functions return, there is never really any advice about when you would want one or the other.

First 10 of 13 Posts Switch mode

al2o3cr

al2o3cr

I happened across this article recently - the linked section digs into which flavors of time you might want for various applications:

adamu

adamu OP

Thanks for the link, super interesting. It covers the difference between monotonic and wall time well, but it doesn’t really cover OS Time vs System Time. The erlang docs seem to suggest that for getting the wall time, System Time should be the default, however, DateTime in Elixir uses OS Time. I wonder if there’s a reason for that?

josevalim

josevalim

Creator of Elixir

I would say OS time is correct. For an embedded device Or desktop application, if the os time changes, then it should be immediately reflected in the app. The same for log timestamps and so on. For servers I may not Matter anyway as they are usually in UTC and do not observe jumps.

adamu

adamu OP

That seems to make sense, but if that’s the case then what is the point of system time and time warps existing at all?

Quoting the erlang docs again:

The number of problems that arise when you always expect the wall clock time on the system to be correct can be immense. Erlang therefore introduced the “corrected estimate of time”, or the “time correction”, many years ago.

And

As we align Erlang system time with OS system time by changing the time offset, we can enable a time correction that tries to adjust the frequency of the Erlang monotonic clock to be as correct as possible. This makes time measurements using Erlang monotonic time more accurate and precise.

This is saying that there are problems being solved and accuracy and precision benefits to Erlang monotonic time, but it does not mention why Erlang system time (based on Erlang monotonic time) would be preferable to OS system time.

learn you some Erlang also discusses this, but again does not make a recommendation between System and OS time.

BartOtten

BartOtten

As most servers use Network Time Protocol (NTP) the time may jump as time drifts and it’s synchronized with the external clock. Have seen this happen in production and the results weren’t pretty. Scanner imports were rejected because the servers’ clock jumped backward, even when it was a small amount.

Those are the cases where system_time seems to be designed for, where ‘system’ is ‘Erlang’. You can tune how it behaves when such jump (or drift) happens. For instance: only allow ‘time’ to go forward, no jumps (using correction).

As a result the time you get might not immediately be the time you expect, but it’s more robust and predictable from a systems’ perspective.

Recommendations

os_time for user observable time as it matches expectation and wall clock. Examples:

  • the new log entry has a timestamp matching the clock on the wall
  • the clock shown on the screen matches the clock on the wall

system_time for internal time(rs) & time measuring as external interference with the clock is dampened and the clock is more predictable for processes. Examples:

  • order events by timestamp will always match order by incremental id
  • processes expected to run on a strict interval will not alert due to a time jump

ps. I am not an expert
pps. There is no better way then getting the answer from an expert than posting a wrong answer. So when it’s wrong we will find out soon.

13
Post #5
adamu

adamu OP

It seems like both of these cases could be handled by using monotonic time. Do you see a use for Erlang system time specifically over monotonic time here?

jj1bdx

jj1bdx

Primary source of document in Erlang “system time”: Time and Time Correction in Erlang

If you’re depending on precise time continuity and non-repudiation of time (i.e., monotonicity of time) within Erlang VM, use erlang:system_time/0-1.

adamu

adamu OP

Yes I have quoted that document multiple times above :slight_smile:

The question is really:

When should we prefer to use the Erlang system time over OS time, as opposed to simply monotonic time, either as erlang:monotonic_time or as System.monotonic_time/0?

BartOtten

BartOtten

Erlang monotonic time increases since some unspecified point in time
Even Erlang system time is based on Erlang monotonic time. By adding current Erlang monotonic time with current time offset, you get current Erlang system time.
To retrieve the current time offset, callerlang:time_offset/0.

When you also want to use the time other than ‘time elapsed’ (eg. print or store start- and stop time of measurement) you better use system time as monotonic time has no reference to a ‘clock on the wall’ at all.

justincjohnson

justincjohnson

In case this is helpful for anyone…

I just ran into a case where on my MacBook Pro, if I have an iex session running and then let my laptop sleep, when I wake my laptop in the morning, System.system_time() in the existing iex session will lag behind System.system_time() in a new iex session. As far as I can tell, the lag will remain until I restart the VM. I’m running Erlang/OTP 25 and Elixir 1.14.3.

This problem manifested as an invalid nbf (not before) claim in an oauth token because the times were being compared with System.system_time() and not System.os_time().

I’m still investigating to see if this behavior changed in recent versions of Erlang/OTP, since we hadn’t seen this prior to a recent upgrade.

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement