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

used / available / free memory is wrong on Linux #1071

Closed
srid opened this issue Sep 21, 2023 · 9 comments · Fixed by #1119
Closed

used / available / free memory is wrong on Linux #1071

srid opened this issue Sep 21, 2023 · 9 comments · Fixed by #1119
Labels

Comments

@srid
Copy link

srid commented Sep 21, 2023

Describe the bug

My environment,

  • sysinfo.version = "0.29.10"
  • 13th Gen Intel(R) Core(TM) i9-13900 (Hetzner Dedicated server)
  • rustc 1.73.0-nightly (32303b219 2023-07-29)

The following Rust code,

        println!(
            "Memory used / free / available / total => {} / {} / {} / {}",
            bytes_human_readable(sys.used_memory()),
            bytes_human_readable(sys.free_memory()),
            bytes_human_readable(sys.available_memory()),
            bytes_human_readable(sys.total_memory())
        );

outputs:

Memory used / free / available / total => 55 GB / 6 GB / 6 GB / 62 GB

Note that both free and available are detected as 6GB, which is wrong. The correct values are reported by the free command:

❯ free -m
               total        used        free      shared  buff/cache   available
Mem:           64058       13762        1670          17       48624       49565
Swap:              0           0           0

To Reproduce

https://github.com/juspay/nix-browser/compare/2a2e72fc6aafba640b94bc18d418377716a240e7..2a7404807db29bd1005ef1d5b2be62a800dac791

@srid srid added the bug label Sep 21, 2023
@srid srid changed the title available / free memory is wrong on Linux used / available / free memory is wrong on Linux Sep 21, 2023
@srid srid changed the title used / available / free memory is wrong on Linux used / available / free memory is wrong on Linux Sep 21, 2023
@srid
Copy link
Author

srid commented Sep 21, 2023

/proc/meminfo
❯ cat /proc/meminfo
MemTotal:       65595448 kB
MemFree:         1709008 kB
MemAvailable:   50753656 kB
Buffers:         3273820 kB
Cached:         42092296 kB
SwapCached:            0 kB
Active:         19315980 kB
Inactive:       39329500 kB
Active(anon):   13258948 kB
Inactive(anon):    38612 kB
Active(file):    6057032 kB
Inactive(file): 39290888 kB
Unevictable:       10496 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Zswap:                 0 kB
Zswapped:              0 kB
Dirty:                76 kB
Writeback:             0 kB
AnonPages:      13281840 kB
Mapped:           380944 kB
Shmem:             18152 kB
KReclaimable:    4426336 kB
Slab:            4957284 kB
SReclaimable:    4426336 kB
SUnreclaim:       530948 kB
KernelStack:       13068 kB
PageTables:        35988 kB
SecPageTables:         0 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    32797724 kB
Committed_AS:   17006928 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       46716 kB
VmallocChunk:          0 kB
Percpu:            32768 kB
AnonHugePages:     45056 kB
ShmemHugePages:     8192 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      304756 kB
DirectMap2M:     5722112 kB
DirectMap1G:    60817408 kB

@GuillaumeGomez
Copy link
Owner

What does htop display?

@srid
Copy link
Author

srid commented Sep 21, 2023

htop is in accord with free -m, thus behaves correctly. Do you need any diagnostic info aside from the /proc/meminfo pasted above?

image

@GuillaumeGomez
Copy link
Owner

No, I think I have all needed. You can also check what's wrong yourself, which would be much faster than waiting for me as I'm a super busy lately... The code which gets this information is in https://github.com/GuillaumeGomez/sysinfo/blob/0.29/src/linux/system.rs#L252. If you could check what is done wrong, it'd be really helpful (sending a fix would be the best! :D)

@Treasure-u
Copy link

I also meet this problem and I think if we want to get used / avialble / free memory like command "free -h",maybe we should not use the part of cgroups to sub our memory (https://docs.rs/sysinfo/0.29.10/src/sysinfo/linux/system.rs.html#286-318)

@GuillaumeGomez
Copy link
Owner

I'm really confused about this cgroup thing. Maybe there is a way to figure out when to use it or not...

@Treasure-u
Copy link

After looking at the sysinfo source code, I found that it seems that cgroups are only considered in the memory part, in fact, the /proc/meminfo data does not need to consider cgroups, I hope my answer is correct and helpful to you :)

@GuillaumeGomez
Copy link
Owner

I'm investigating this issue again. @srid, can you provide me the content of the following files please:

/sys/fs/cgroup/memory.current
/sys/fs/cgroup/memory.max
/sys/fs/cgroup/memory/memory.usage_in_bytes
/sys/fs/cgroup/memory/memory.limit_in_bytes

@GuillaumeGomez
Copy link
Owner

In the meantime, I decided to move the cgroup limits computation into its own function so it can be queried separately in #1119.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants