-
-
Notifications
You must be signed in to change notification settings - Fork 49
Show user-friendly memory size unit and accurate total physical available memory size #554
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
base: main
Are you sure you want to change the base?
Changes from all commits
5693c1f
76ea898
4d1be27
99ba1e2
0133177
81392c9
e8c607b
f519511
a82b688
1c39c70
efd0235
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| namespace Monitor.Utils { | ||
| const int BITS_IN_BYTES = 8; | ||
| const int MHZ_IN_GHZ = 1000; | ||
| const int IEC_UNIT_BASE = 1024; | ||
| const int NON_IEC_UNIT_BASE = 1000; | ||
|
|
||
| const string NOT_AVAILABLE = (_("N/A")); | ||
| const string NO_DATA = "\u2014"; | ||
|
|
@@ -74,6 +76,38 @@ public class Monitor.Utils.Strings { | |
| format_size (speed_in_bytes_per_second * BITS_IN_BYTES, BITS | ONLY_UNIT) | ||
| ); | ||
| } | ||
|
|
||
| public static string format_memory_size (uint64 size_in_bytes) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you leave some comments for what you're doing in this method?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ryonakano added comments in efd0235 hope the wording makes for a sensible explanation?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's mainly to explain the fix in 81392c9
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also see similar fix in #562 |
||
| uint64 unit = 1; | ||
| while (size_in_bytes >= unit * IEC_UNIT_BASE) { | ||
| unit *= IEC_UNIT_BASE; | ||
| } | ||
|
|
||
| // Adjust size for the second glib format_size () call with ONLY_UNIT flag set and IEC_UNITS flag excluded. | ||
| // Prevents mismatch between unit and value due to borderline overlap between IEC_UNIT_BASE (1024) and | ||
| // NON_IEC_UNIT_BASE (1000) values, say, if size is somewhere between 1000 and 1024 the result would be | ||
| // erroneously shown eg. for size 1020 MB it would have been "1020 GB" this adjustment corrects it | ||
| // by avoiding the mismatch thus showing "1020 MB" as intended. | ||
| var size_adjusted_for_non_iec_units = size_in_bytes * NON_IEC_UNIT_BASE / IEC_UNIT_BASE; | ||
|
|
||
| // If size is a perfect multiple/factor of IEC_UNIT_BASE (1024) then print it as a whole number | ||
| // else fall through to use standard glib format_size () to print with one decimal place. | ||
| if (size_in_bytes % unit == 0) { | ||
| ///TRANSLATORS: The first param is the numeric value of memory size. | ||
| ///The second param is the memory size such as "MB" or "GB" for megabytes or gigabytes. | ||
| return _("%llu %s").printf ( | ||
| size_in_bytes / unit, | ||
| format_size (size_adjusted_for_non_iec_units, ONLY_UNIT) | ||
| ); | ||
| } | ||
|
|
||
| ///TRANSLATORS: The first param is the numeric value (as string) of memory size. | ||
| ///The second param is the memory size such as "MB" or "GB" for megabytes or gigabytes. | ||
| return _("%s %s").printf ( | ||
| format_size (size_in_bytes, IEC_UNITS | ONLY_VALUE), | ||
| format_size (size_adjusted_for_non_iec_units, ONLY_UNIT) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public class Monitor.Utils.Colors : Object { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I understand correctly that you used GUdev to get "accurate total physical available memory size"? Could you tell me the differences of values between GTop and GUdev?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GUDev code gets you the actual installed physical memory size, meaning if your computer has 16 GiB it will get this exact number in bytes.
GTop on the other hand returns the "user accessible" memory size which is lower than what GUDev returns due to things like memory being reserved for kernel buffer cache, iGPU memory etc.
The switch from using GTop (still remains as fallback) to GUDev is shown in the before-after screenshots where you can see the "total memory" is lower with GTop used before.