Merge pull request #22328 from babsingh/main4_jit · eclipse-openj9/openj9@2b4bbbc
@@ -39,8 +39,11 @@
3939 * typedef struct J9SysinfoCPUTime {
4040 *
4141 * I_64 timestamp; // time in nanoseconds from a fixed but arbitrary point in time
42- * I_64 cpuTime; // cumulative CPU utilization (sum of system and user time in nanoseconds) of all CPUs on the system.
42+ * I_64 cpuTime; // cumulative CPU utilization (sum of system and user time in nanoseconds) of all CPUs on the system
4343 * I_32 numberOfCpus; // number of CPUs as reported by the operating system
44+ * I_64 userTime; // total user time (in CPU ticks) across all CPUs on the system
45+ * I_64 systemTime; // total kernel time (in CPU ticks) across all CPUs on the system
46+ * I_64 idleTime; // total idle time (in CPU ticks) across all CPUs on the system
4447 *
4548 * } J9SysinfoCPUTime;
4649 *
@@ -108,22 +111,47 @@ int CpuUtilization::updateCpuUtil(J9JITConfig *jitConfig)
108111 {
109112int64_t prevTotalTimeUsedByVm = _prevVmSysTime + _prevVmUserTime;
110113int64_t newTotalTimeUsedByVm = vmCpuStats._systemTime + vmCpuStats._userTime;
114+double cpuLoad = 0.0;
115+double cpuIdle = 0.0;
111116112- _cpuUsage = (100 * (machineCpuStats.cpuTime - _prevMachineCpuTime)) / elapsedTime;
113- _cpuIdle = 100 * machineCpuStats.numberOfCpus - _cpuUsage;
114- _vmCpuUsage = (100 * (newTotalTimeUsedByVm - prevTotalTimeUsedByVm)) / elapsedTime;
115- }
117+if ((-1 != _prevMachineUserTime) && (-1 != _prevMachineSystemTime) && (-1 != _prevMachineIdleTime) &&
118+ (-1 != machineCpuStats.userTime) && (-1 != machineCpuStats.systemTime) && (-1 != machineCpuStats.idleTime))
119+ {
120+int64_t userDelta = machineCpuStats.userTime - _prevMachineUserTime;
121+int64_t systemDelta = machineCpuStats.systemTime - _prevMachineSystemTime;
122+int64_t idleDelta = machineCpuStats.idleTime - _prevMachineIdleTime;
123+int64_t totalDelta = userDelta + systemDelta + idleDelta;
116124117-if (machineCpuStats.numberOfCpus > 0)
118- {
119- _avgCpuUsage = _cpuUsage / machineCpuStats.numberOfCpus;
120- }
125+if (totalDelta > 0)
126+ {
127+ cpuLoad = (userDelta + systemDelta) / (double)totalDelta;
128+ cpuIdle = idleDelta / (double)totalDelta;
129+ }
130+ }
131+else
132+ {
133+int64_t cpuTimeDelta = machineCpuStats.cpuTime - _prevMachineCpuTime;
134+ cpuLoad = cpuTimeDelta / ((double)machineCpuStats.numberOfCpus * elapsedTime);
135+ cpuIdle = 1 - cpuLoad;
136+ }
137+138+ _avgCpuUsage = 100.0 * cpuLoad;
139+ _avgCpuIdle = 100.0 * cpuIdle;
121140122- _avgCpuIdle = 100 - _avgCpuUsage;
141+if (machineCpuStats.numberOfCpus > 0)
142+ {
143+ _cpuUsage = 100.0 * machineCpuStats.numberOfCpus * cpuLoad;
144+ _cpuIdle = 100.0 * machineCpuStats.numberOfCpus * cpuIdle;
145+ }
146+ _vmCpuUsage = (100 * (newTotalTimeUsedByVm - prevTotalTimeUsedByVm)) / elapsedTime;
147+ }
123148124149// remember values for next time
125150 _prevMachineUptime = machineCpuStats.timestamp;
126151 _prevMachineCpuTime = machineCpuStats.cpuTime;
152+ _prevMachineUserTime = machineCpuStats.userTime;
153+ _prevMachineSystemTime = machineCpuStats.systemTime;
154+ _prevMachineIdleTime = machineCpuStats.idleTime;
127155 _prevVmSysTime = vmCpuStats._systemTime;
128156 _prevVmUserTime = vmCpuStats._userTime;
129157@@ -147,6 +175,9 @@ int32_t CpuUtilization::updateCpuUsageCircularBuffer(J9JITConfig *jitConfig)
147175148176 _cpuUsageCircularBuffer[_cpuUsageCircularBufferIndex]._timeStamp = machineCpuStats.timestamp;
149177 _cpuUsageCircularBuffer[_cpuUsageCircularBufferIndex]._sampleSystemCpu = machineCpuStats.cpuTime;
178+ _cpuUsageCircularBuffer[_cpuUsageCircularBufferIndex]._sampleUserTime = machineCpuStats.userTime;
179+ _cpuUsageCircularBuffer[_cpuUsageCircularBufferIndex]._sampleSystemTime = machineCpuStats.systemTime;
180+ _cpuUsageCircularBuffer[_cpuUsageCircularBufferIndex]._sampleIdleTime = machineCpuStats.idleTime;
150181 _cpuUsageCircularBuffer[_cpuUsageCircularBufferIndex]._sampleJvmCpu = vmCpuStats._systemTime + vmCpuStats._userTime;
151182152183 _cpuUsageCircularBufferIndex = (_cpuUsageCircularBufferIndex + 1)%_cpuUsageCircularBufferSize;
@@ -170,6 +201,9 @@ CpuUtilization::CpuUtilization(J9JITConfig *jitConfig):
170201171202 _prevMachineUptime (0),
172203 _prevMachineCpuTime (0),
204+ _prevMachineUserTime (0),
205+ _prevMachineSystemTime (0),
206+ _prevMachineIdleTime (0),
173207 _prevVmSysTime (0),
174208 _prevVmUserTime (0),
175209