最近同事说到Java的 ParallelGCThreads 参数,我翻了下jdk8的代码,发现 ParallelGCThreads 的参数默认值如下:
但是被提醒,发现即使在分配4核的容器上,GC线程数也为38。然后就想到应该和容器的资源限制有关——jvm可能无法觉察到当前容器的资源限制。
翻了下代码,发现最新版本的java是能感知容器的资源限制的,就按照jdk版本再翻了下代码:
写一个sleep 1000s的程序,然后运行:
./jdk1.8.0_144/bin/java -XX:+UseG1GC -XX:+ParallelRefProcEnabled Main
然后查看GC线程数目:
$ jstack $pid | grep 'Parallel GC Threads' | wc -l 38
一算就知道物理机器有56个核心(8+(56-8)*5/8=38)
然后使用+PrintFlagsFinal看下参数:
$ ./jdk1.8.0_144/bin/java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version | grep ParallelGCThreads uintx ParallelGCThreads =38 {product}
看来jdk8u144并无法读取容器配额。
然后发现,从 jdk 8u191版本 开始,Java就可以读取容器配额了:
运行同样的程序:
./jre1.8.0_191/bin/java -XX:+UseG1GC -XX:+ParallelRefProcEnabled Main
$ jstack $pid | grep 'Parallel GC Threads' | wc -l 4
查看实际参数:
$ ./jre1.8.0_191/bin/java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version | grep ParallelGCThreads uintx ParallelGCThreads =4 {product}
另外,jdk 8u191引入了PrintContainerInfo参数:
$ ./jre1.8.0_191/bin/java -XX:+UnlockDiagnosticVMOptions -XX:+PrintContainerInfo -version OSContainer::init: Initializing Container Support Path to /memory.limit_in_bytes is /sys/fs/cgroup/memory/memory.limit_in_bytes Memory Limit is: 10737418240 Path to /cpu.cfs_quota_us is /sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us CPU Quota is: 400000 Path to /cpu.cfs_period_us is /sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us CPU Period is: 100000 Path to /cpu.shares is /sys/fs/cgroup/cpu,cpuacct/cpu.shares CPU Shares is: 681 CPU Quota count based on quota/period: 4 CPU Share count based on shares: 1 OSContainer::active_processor_count: 4 Path to /cpu.cfs_quota_us is /sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us CPU Quota is: 400000 Path to /cpu.cfs_period_us is /sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us CPU Period is: 100000 Path to /cpu.shares is /sys/fs/cgroup/cpu,cpuacct/cpu.shares CPU Shares is: 681 CPU Quota count based on quota/period: 4 CPU Share count based on shares: 1 OSContainer::active_processor_count: 4 …… java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
可以看到,获取的内存限制、可用CPU数目都是对的了。
结合这个日志和代码,我们也可以看到如何获取容器配额:
首先从/proc/self/mounts中读取对应的资源的mount位置,比如cpu就是在 /sys/fs/cgroup/cpu,cpuacct
:
$ cat /proc/mounts | grep -E -w '(cpu|memory)' cgroup /sys/fs/cgroup/memory cgroup ro,nosuid,nodev,noexec,relatime,memory 0 0 cgroup /sys/fs/cgroup/cpu,cpuacct cgroup ro,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
对于内存:
$ cat /sys/fs/cgroup/memory/memory.limit_in_bytes 10737418240
对于cpu资源:
其一,可以通过quota/period来算:
$ cat /sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us #单CPU总时间片配额,微秒 100000 $ cat /sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us #时间片内,容器可占用的CPU时间 400000
比如上面就表示分配了4核。
也可以通过cpu.shares来获取:
$ cat /sys/fs/cgroup/cpu,cpuacct/cpu.shares 681
不过这个值是cpu占用份额,无法根据这个算出来可用cpu数量,所以基本没用…