Skip to content

use monotonic, higher resolution jiffies#1141

Open
mxork wants to merge 4 commits into
ashinn:masterfrom
mxork:jiffy-higher-res
Open

use monotonic, higher resolution jiffies#1141
mxork wants to merge 4 commits into
ashinn:masterfrom
mxork:jiffy-higher-res

Conversation

@mxork

@mxork mxork commented Jul 14, 2026

Copy link
Copy Markdown

The current-jiffy was 1:1 with current-second, but assuming that its meant to be used more for measuring intervals and not wall time, I replaced it with more suitable clocks:

  1. posix uses CLOCK_MONOTONIC. an argument could be made for CLOCK_MONOTONIC_RAW on linux.
  2. windows uses QueryPerformanceCounter
  3. plan9 keep using nsec. they have cycles, but its not a clock out of the box.

I haven't tested the windows or plan9 code, but it looks sane to me. Advice on cross-compiling from linux to windows appreciated. I couldn't bring myself to install mingw under wine, but I also couldn't get make CC=mingw... PLATFORM=windows to Just Work.

Comment thread lib/scheme/time.c Outdated
// :note could use errno, here
if (err)
return sexp_user_exception(ctx, self, "couldn't get current jiffy", SEXP_FALSE);
return sexp_make_fixnum(1000000000*tv.tv_sec + tv.tv_nsec);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... on a 32-bit system, this overflows after the system has been up for 4 seconds.

You could check and make a bignum as needed, but that becomes always on 32-bit systems. Probably better to conditionally define jiffies-per-second as a smaller value, say millisecond granularity, on 32-bit systems.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I'll polish up a patch for it tomorrow.

A millisecond is an awfully long time. But nothing better to be done within the constraints of the spec.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep using chibi, you'll come to think a millisecond is fast! 😉

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patched and pushed. hopefully I didn't whiff an ifdef.

@mxork

mxork commented Jul 14, 2026

Copy link
Copy Markdown
Author

I don't know why the CMAKE builds decided to all explode; not clear its from my change?

@ashinn ashinn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread lib/scheme/time.c Outdated
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return sexp_make_fixnum(frequency.QuadPart);
#elif !defined(PLAN9)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as the next branch, you can unify them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack.

Comment thread lib/scheme/time.c Outdated
if (err)
return sexp_user_exception(ctx, self, "couldn't get current jiffy", SEXP_FALSE);
uint64_t current_ns = 1000000000*tv.tv_sec + tv.tv_nsec;
#if SEXP_MS_JIFFY

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is repeated below, you can move it out of the current_ns computation just once at the end.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants