语境:
uname -srvmpio
Linux 3.10.0-1062.1.1.el7.x86_64 #1 SMP Fri Sep 13 22:55:44 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
我有一个切片,其中有:
systemctl cat system-mystuff.slice
# /etc/systemd/system/system-mystuff.slice
[Unit]
Description=mystuff resources slice
DefaultDependencies=no
Before=slices.target
[Slice]
MemoryAccounting=yes
MemoryMax=4G
MemoryHigh=3.75G
该切片的成员也在MemoryAccounting=yes
其 .service 文件中。
当我跑步时
systemd-cgtop -n1 -b -m | grep system-mystuff
我看到以下输出:
/system.slice/system-mystuff.slice - - 2.3G - -
/system.slice/system-mystuff.slice/[email protected] 1 - 246.6M - -
/system.slice/system-mystuff.slice/process2.service 1 - 82.7M - -
/system.slice/system-mystuff.slice/[email protected] 1 - 49.5M - -
/system.slice/system-mystuff.slice/process4.service 2 - 8.6M - -
/system.slice/system-mystuff.slice/process5.service 1 - 8.4M - -
/system.slice/system-mystuff.slice/process6.service 3 - 8.2M - -
/system.slice/system-mystuff.slice/process7.service 1 - 7.3M - -
/system.slice/system-mystuff.slice/[email protected] 1 - 6.1M - -
/system.slice/system-mystuff.slice/process9.service 1 - 6.0M - -
/system.slice/system-mystuff.slice/process10.service 4 - 1.4M - -
/system.slice/system-mystuff.slice/process11.service 2 - 1.1M - -
/system.slice/system-mystuff.slice/process12.service 1 - 620.0K - -
/system.slice/system-mystuff.slice/process13.service 1 - 392.0K - -
/system.slice/system-mystuff.slice/process14.service 1 - 308.0K - -
/system.slice/system-mystuff.slice/process15.service 1 - 260.0K - -
/system.slice/system-mystuff.slice/process16.service 1 - 184.0K - -
/system.slice/system-mystuff.slice/process17.service 1 - 180.0K - -
/system.slice/system-mystuff.slice/process18.service 1 - - - -
/system.slice/system-mystuff.slice/process19.service 1 - - - -
当我对列求和时,切片中的所有进程使用了约 430 MB 的内存。
作为我研究的一部分,我发现https://www.mjmwired.net/kernel/Documentation/cgroups/memory.txt 5.2 和 5.5 节指出内存使用量的计算方式为:RSS+CACHE(+SWAP)
https://www.mjmwired.net/kernel/Documentation/cgroups/memory.txt#560
可以肯定的是,我编写了一个脚本来遍历 /sys/fs/cgroup/memory/system.slice/system-mystuff.slice 文件系统,并查看切片中每个进程的 memory.stat 文件。然后为每个进程添加total_rss+total_cache+total_swap。这些总数的总和大致匹配:~430 MB。
所以我的问题是从哪里systemd-cgtop
获得 2.3 GB 的数字?
答案1
内存是共享的。您将拥有一份共享库(例如 glibc)的副本,并且许多用户将其计入其内存使用量的一部分。