Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make os.clock use clock_gettime on FreeBSD #1364

Conversation

Ketasaja
Copy link
Contributor

@Ketasaja Ketasaja commented Aug 11, 2024

I think clock() is processor time used by the program on FreeBSD (that's what it is in Linux). On Linux, Apple, and Windows, os.clock() isn't specific to the program.

Would CLOCK_MONOTONIC_RAW be better or worse here, since it doesn't slew time for adjustments like NTP? QueryPerformanceCounter on Windows isn't slewed for NTP, I don't know if mach_absolute_time is. It's only supported since Linux 2.6.28 though.

@vegorov-rbx
Copy link
Collaborator

I think clock() is processor time used by the program on FreeBSD (that's what it is in Linux). On Linux, Apple, and Windows, os.clock() isn't specific to the program.

Luau doesn't specify that is has to be either of those.
In fact, we do not have a concrete contract on the origin of the value as you can see in the docs:

function os.clock(): number

Returns a high-precision timestamp (in seconds) that doesn’t have a defined baseline, but can be used to measure duration with sub-microsecond precision.

Note that this is different from how PUC Lua defines os.clock.

But if your change is mainly about improving precision of the value, that's a good improvement to have.


Would CLOCK_MONOTONIC_RAW be better or worse here, since it doesn't slew time for adjustments like NTP?

Given that we already have timestamps taken as CLOCK_MONOTONIC on Linux, I don't think there's a reason to use _RAW version on FreeBSD.


Finally, the file you modified is actually used for 'time trace' profiling file recording, not for the os.clock function (in 'lperf.cpp').
'Time trace' improvement is still good though.

@Ketasaja
Copy link
Contributor Author

Ketasaja commented Aug 14, 2024

@vegorov-rbx I made the change to lperf.cpp, thanks.

But if your change is mainly about improving precision of the value, that's a good improvement to have.

My original phrasing was bad. I understand the baseline isn't defined, my intent was to make FreeBSD's increasing of the value not ignore time spent during context switches, and not tick faster with parallelism, since it doesn't on other major platforms. This avoids Lua's mistake.

I don't think it increases precision because FreeBSD does not comply with the requirement that CLOCKS_PER_SEC be defined as one million.

I think for benchmarking there could be another os function for CPU time used by the current process only, but that would need an RFC.

@vegorov-rbx
Copy link
Collaborator

I don't think it increases precision because FreeBSD does not comply with the requirement that CLOCKS_PER_SEC be defined as one million.

Doesn't that mean the exact opposite?
Since FreeBSD defines CLOCKS_PER_SEC to be 128, smallest duration that can be measured is 7.8125ms.
I expect that clock_gettime precision is much better than that.

@Ketasaja
Copy link
Contributor Author

Doesn't that mean the exact opposite?

You're right, it should increase precision.

@vegorov-rbx
Copy link
Collaborator

I checked multiple language runtimes and everyone uses CLOCK_MONOTONIC, so I would say there's no need to switch and it is unlikely we will accept additional function RFCs at this time.
Users can always include their own custom functions or disable NTP on their device if it's causing measurable issues.

@Ketasaja
Copy link
Contributor Author

Ketasaja commented Aug 14, 2024

I should mention I'm assuming __FreeBSD__ would be defined when compiling for potential future console operating systems based on FreeBSD. I can include a more specialized macro if this weren't the case.

@vegorov-rbx
Copy link
Collaborator

vegorov-rbx commented Aug 14, 2024

I don't know what you're talking about.

Luau already has code under __FreeBSD__ define for FreeBSD users.

@vegorov-rbx vegorov-rbx merged commit 1788e23 into luau-lang:master Aug 14, 2024
7 checks passed
@vegorov-rbx
Copy link
Collaborator

Thank you!

@Ketasaja Ketasaja deleted the make-os.clock-use-clock_gettime-on-FreeBSD branch August 14, 2024 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants