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

The OperatingSystemMXBean.getProcessCpuLoad returns with -1

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      System where the method fails
      Os
      Linux 4.18.5-1.el7.elrepo.x86_64 #1 SMP Fri Aug 24 11:35:05 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux
      CPU Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz

      Works on the same os with this CPU
      Intel(R) Xeon(R) Silver 4215R CPU @ 3.20GHz
      Tried out JDK versions which not works
      8.342.07-4, 11.0.16.9.1, 17.0.1.12.1, 18.0.2.9.1
      Works only on jdk-19

      A DESCRIPTION OF THE PROBLEM :
      This issue only came up on this CPU and this Os. On other machine with the same Os the code works fine.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Build the minimal code on any of the above jdk versions (8.342.07-4, 11.0.16.9.1, 17.0.1.12.1, 18.0.2.9.1) like
      jdk-18.0.2.1/bin/javac Main.java
      and run it like
      jdk-18.0.2.1/bin/java Main

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      cpu util value 0.03203806502775575
      cpu util value 0.032299124900556885
      ACTUAL -
      cpu util value -1.0
      cpu util value -1.0

      ---------- BEGIN SOURCE ----------
      import java.lang.management.ManagementFactory;
      import java.lang.management.OperatingSystemMXBean;
      import java.util.Random;

      public class Main {

        public static void main(String[] args) {
          Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
              while (true) {
                int a = 1234;
                int b = a * new Random().nextInt();
              }
            }
          });
          thread.start();
          int i = 1;
          while (i < 10) {
            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
            double cpuUtil = ((com.sun.management.OperatingSystemMXBean) operatingSystemMXBean).getProcessCpuLoad();
            System.out.println("cpu util value " + cpuUtil);
            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            i++;
          }
          thread.stop();

        }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        1. lscpu_out.txt
          2 kB
        2. Main.java
          0.9 kB
        3. VM.info.txt
          34 kB

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: