Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8269851

OperatingSystemMXBean getProcessCpuLoad reports incorrect process cpu usage in containers

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b09

      Backports

        Description

        This is on behalf of bobpengxie@tencent.com

        If JVM runs in a container which is limited CPU resource (quota, shares, cpu),
        getProcessCpuLoad is incorrect.
        getCpuLoad is correct.


        set --cpu-quota=50000 --cpu-period=50000 to cgroup cpuset
        my machine has 8 cores

        cgexec -g cpu:cpuset ${java_path} TestCgroup


        process:12.338474364318467
        system98.79130556666667

        ```
        import com.sun.management.OperatingSystemMXBean;

        import java.lang.management.ManagementFactory;

        public class TestCgroup {
            private static OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
                    OperatingSystemMXBean.class);

            public static void main(String[] args) {
                new Thread(() -> {
                    int i = 0;
                    while (true) {
                        i++;
                        if (i > 10000) {
                            i = i - 1000;
                        }
                    }

                }).start();

                while (true) {


                    System.out.println("process:"+osBean.getProcessCpuLoad() * 100);

                    System.out.println("system"+osBean.getSystemCpuLoad() * 100);
                    System.out.println(osBean.getAvailableProcessors());
                    try {
                        Thread.sleep(3000);

                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }

            }
        }
        ```

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                jiefu Jie Fu
                Reporter:
                jiefu Jie Fu
                Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: