Skip to content

Bring back conversion of process CPU time on macOS (#1638) #1691

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

erikolofsson
Copy link

Fixes #1638

@BenBE BenBE added bug 🐛 Something isn't working MacOS 🍏 MacOS / Darwin related issues labels May 1, 2025
@erikolofsson erikolofsson requested a review from Explorer09 May 3, 2025 16:06
@Explorer09
Copy link
Contributor

I don't have any more comment with this code now. But perhaps is good to explain what's the difference between this PR and #1643. Does this supersede #1643?

Cc @aestriplex

@erikolofsson erikolofsson requested a review from BenBE May 8, 2025 06:40
uint64_t Platform_machTicksToNanoseconds(uint64_t mach_ticks) {
uint64_t ticks_quot = mach_ticks / Platform_nanosecondsPerMachTickDenom;
uint64_t ticks_rem = mach_ticks % Platform_nanosecondsPerMachTickDenom;

Copy link
Contributor

Choose a reason for hiding this comment

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

   assert(ticks_quot <= UINT64_MAX / Platform_nanosecondsPerMachTickNumer);
   assert(Platform_nanosecondsPerMachTickDenom <= UINT64_MAX / Platform_nanosecondsPerMachTickNumer);

These two constraints are what prevent the overflow from happening in this function. Not sure if these two assertions are worth adding into the function, but I mention these just for completeness sake.

The first assertion is related to a time overflow (like Y2K) that might be out of scope of htop.
The second assertion can always hold true when mach_timebase_info_data_t uses 32-bit integers for both numerator and denominator fields.

Copy link
Member

Choose a reason for hiding this comment

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

They don't exactly hurt, but given this is what ultimately gives the time values used in htop, they should not straight up cause htop to terminate seemingly at random. Instead normal handling that causes a runtime warning (once) could be done instead …

Can you elaborate on the intuition of the second assertion?

Copy link
Contributor

@Explorer09 Explorer09 May 9, 2025

Choose a reason for hiding this comment

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

@BenBE

ticks_rem < Platform_nanosecondsPerMachTickDenom ;
ticks_rem * Platform_nanosecondsPerMachTickNumer < Platform_nanosecondsPerMachTickDenom * Platform_nanosecondsPerMachTickNumer
(or Platform_nanosecondsPerMachTickNumer == 0)

When Platform_nanosecondsPerMachTickDenom * Platform_nanosecondsPerMachTickNumer is less than 2^64, ticks_rem * Platform_nanosecondsPerMachTickNumer will be less than 2^64 as well, i.e. never overflows.

Copy link
Author

Choose a reason for hiding this comment

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

Do you want me to do any changes?

The overflow should only happen when the result in nano-seconds overflows, which would already be a problem in other places in the application. This is after 584 CPU-years.

Copy link
Member

Choose a reason for hiding this comment

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

Optional. I mostly asked for some clarifications here.

If you decided to update the PR, be sure to avoid direct asserts (for the reasons mentioned above), and instead choose a way that is as obvious as possible, that this is some intentional error value.

Copy link
Contributor

Choose a reason for hiding this comment

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

@erikolofsson My view is that the time overflow bug that would happen 584 years later (the overflow on part1) is out of scope, but without an in-code comment, the (ticks_rem * Platform_nanosecondsPerMachTickNumer) in part2 can be mistaken as prone to overflow. (It actually doesn't overflow for the reason I stated above.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working MacOS 🍏 MacOS / Darwin related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Htop 3.4.0-dev not showing CPU percentage on apple silicon
3 participants