Skip to content

top: implement SUMMARY display #306

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

Merged
merged 10 commits into from
Mar 11, 2025

Conversation

Bluemangoo
Copy link
Contributor

@Bluemangoo Bluemangoo commented Jan 13, 2025

Resolves #207

Tasks:

  • time and uptime
  • active user count
  • cpu load average
  • task summary
  • cpu load
  • memory and swap summary
  • determine memory unit from --scale-summary-mem

I add crate systemstat for some of the values.

top - 13:03:00 up 1:33, 1 user, load average: 2.01, 1.48, 1.28
Tasks: 1040 total, 6 running, 974 sleeping, 0 stopped, 0 zombie
%Cpu(s):  13.9 us, 3.3 sy, 0.0 ni, 82.4 id, 0.3 wa, 0.0 hi, 0.1 si, 0.0 st
MiB Mem :   7636.4 total,    433.3 free,   6415.1 used,    788.0 buff/cache
MiB Swap:  30518.0 total,  27218.5 free,   3299.5 used,   1221.3 avail Mem

@Krysztal112233
Copy link
Collaborator

That's great. But can I ask if it can you use text instead of image? That would be more search friendly.

@Bluemangoo
Copy link
Contributor Author

Bluemangoo commented Jan 15, 2025

There are no way to measure load average on Windows. We can only get an instantaneous CPU load with this command:

wmic cpu get loadpercentage

The Python wheel psutil emulates what Linux does (for sure, need time to collect data):

https://github.com/giampaolo/psutil/blob/d4b37d9628e634c00a2990e488b1cc38ea6a41b5/psutil/arch/windows/wmi.c#L16-L54

I'm not sure what should we do: ignore it, or do what psutil does.

tips: psutil doesn't work well on my computer. it shows (0.0, 0.0, 0.0)

@Krysztal112233
Copy link
Collaborator

Krysztal112233 commented Jan 15, 2025

In fact, the crate we used required us does the same thing:

https://github.com/uutils/procps/blob/c2c0baba2d0db19f40c04f86df073c5614333858/src/uu/top/src/top.rs#L53C1-L57C54

    // Must refresh twice.
    // https://docs.rs/sysinfo/0.31.2/sysinfo/struct.System.html#method.refresh_cpu_usage
    picker::sysinfo().write().unwrap().refresh_all();
    sleep(Duration::from_millis(200));
    picker::sysinfo().write().unwrap().refresh_all();

You can even use a multi-threaded background to refresh this data if sysinfo cannot doesn't meet your needs.

@sylvestre sylvestre force-pushed the feature/top-summary branch from a7c23e8 to dbac1d6 Compare January 18, 2025 22:13
@Bluemangoo Bluemangoo force-pushed the feature/top-summary branch 3 times, most recently from 9086112 to 8ec35c2 Compare January 23, 2025 11:15
@sylvestre sylvestre force-pushed the feature/top-summary branch from 8ec35c2 to 1a09cbd Compare January 23, 2025 11:45
@Bluemangoo Bluemangoo force-pushed the feature/top-summary branch 3 times, most recently from 55eebf7 to 3b8691d Compare January 23, 2025 12:32
todo:
io wait(only Linux now)
cpu load average for Windows
cpu load for Macos
active user count
determine memory unit from `--scale-summary-mem`
@Bluemangoo
Copy link
Contributor Author

Bluemangoo commented Jan 23, 2025

i add libsystemd-sys to get correct active users count(on linux).

starting with systemd 256.7-2, utmp support has been disabled. This
was done as utmp is not Y2038 safe.

After this change, /run/utmp is no longer created by systemd

i check if system is booted by systemd, if so get active user count from it, or from utmp, like what gnu does.

so the apt package libsystemd-dev is required. shall i install it in workflows?

@Krysztal112233
Copy link
Collaborator

i add libsystemd-sys to get correct active users count(on linux).

starting with systemd 256.7-2, utmp support has been disabled. This
was done as utmp is not Y2038 safe.

After this change, /run/utmp is no longer created by systemd

i check if system is booted by systemd, if so get active user count from it, or from utmp, like what gnu does.

so the apt package libsystemd-dev is required. shall i install it in workflows?

Is there any other way to detect the system is initialized with systemd? (e.g. some features on the Init process?)

@Krysztal112233
Copy link
Collaborator

Krysztal112233 commented Jan 24, 2025

I think using libsystemd-sys may introduce additional compilation dependencies that aren't necessary...and identify them by identifying some features might be better such as

~ cat /proc/1/status  
Name:	systemd
Umask:	0000
State:	S (sleeping)
Tgid:	1
Ngid:	0
Pid:	1
PPid:	0
TracerPid:	0
Uid:	0	0	0	0
Gid:	0	0	0	0
FDSize:	256
Groups:	 
NStgid:	1

As you can see the field Name is systemd.

This method cannot work on other system so you have to use something such as #[cfg] to support other system, support Linux is enough currently.

@Bluemangoo
Copy link
Contributor Author

Bluemangoo commented Jan 24, 2025

I would like to mention that platform-specific dependencies do not work with workspace dependencies. Therefore, I specify the version of these dependencies in top's Cargo.toml file, rather than that in the root.

@Bluemangoo
Copy link
Contributor Author

Bluemangoo commented Jan 25, 2025

The way CPU time is calculated in Windows differs from that in Linux. On Windows, CPU time is divided into the following categories: idle, kernel, user, dpc, and interrupt. The dpc time is similar to Linux's soft interrupt, while the interrupt time is specifically for hardware.

Should I display them in the following format:
%Cpu(s): 0.7 us, 50.0 sy, 49.3 id, 0.0 hi, 0.0 si
or in this format:
%Cpu(s): 0.7 us, 50.0 sy, 49.3 id, 0.0 hi, 0.0 dpc?

Additionally, in the Windows documentation, it mentions reserved fields (dpc, interrupt) in the struct:

typedef struct
_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER Reserved1[2];
    ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;

I found a possible explanation in psutil.

typedef struct {
    LARGE_INTEGER IdleTime;
    LARGE_INTEGER KernelTime;
    LARGE_INTEGER UserTime;
    LARGE_INTEGER DpcTime;
    LARGE_INTEGER InterruptTime;
    ULONG InterruptCount;
} _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;

@sylvestre
Copy link
Contributor

please let me know when I can review it. It doesn't have to be completed to land!

@Bluemangoo
Copy link
Contributor Author

It seems that the load average on Windows may require opening a new issue. The CPU load for macOS is still to be done.

you can begin reviewing the rest of the content.

@Bluemangoo Bluemangoo force-pushed the feature/top-summary branch 4 times, most recently from 07ff31f to 9400acc Compare March 8, 2025 16:24
@Bluemangoo Bluemangoo force-pushed the feature/top-summary branch from 8e0cd3d to d29e05a Compare March 8, 2025 16:34
@Bluemangoo
Copy link
Contributor Author

all fixed.

@Bluemangoo Bluemangoo force-pushed the feature/top-summary branch from a22e069 to db8ecf5 Compare March 10, 2025 10:50
@Krysztal112233
Copy link
Collaborator

Krysztal112233 commented Mar 11, 2025

Here's some bug comes from upstream, this pr will be merged after new version of uucore released.

@Bluemangoo
Copy link
Contributor Author

not severe issue, you can also merge first, and update later. I hope #292 is not waiting for this PR.

@Krysztal112233 Krysztal112233 merged commit ffb8266 into uutils:main Mar 11, 2025
14 checks passed
Copy link

codecov bot commented Mar 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (16e31d6) to head (db8ecf5).
Report is 26 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #306   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Krysztal112233 Krysztal112233 mentioned this pull request Mar 11, 2025
4 tasks
@Bluemangoo Bluemangoo deleted the feature/top-summary branch March 11, 2025 06:49
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.

Implement SUMMARY display for top
3 participants