|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2023, 2023, BELLSOFT. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | +package com.oracle.svm.core.jfr.events; |
| 27 | + |
| 28 | +import org.graalvm.nativeimage.IsolateThread; |
| 29 | +import org.graalvm.nativeimage.StackValue; |
| 30 | + |
| 31 | +import com.oracle.svm.core.Uninterruptible; |
| 32 | +import com.oracle.svm.core.heap.VMOperationInfos; |
| 33 | +import com.oracle.svm.core.jdk.Jvm; |
| 34 | +import com.oracle.svm.core.jdk.UninterruptibleUtils; |
| 35 | +import com.oracle.svm.core.jfr.JfrEvent; |
| 36 | +import com.oracle.svm.core.jfr.JfrNativeEventWriter; |
| 37 | +import com.oracle.svm.core.jfr.JfrNativeEventWriterData; |
| 38 | +import com.oracle.svm.core.jfr.JfrNativeEventWriterDataAccess; |
| 39 | +import com.oracle.svm.core.jfr.JfrTicks; |
| 40 | +import com.oracle.svm.core.thread.JavaVMOperation; |
| 41 | +import com.oracle.svm.core.thread.ThreadCpuTimeSupport; |
| 42 | +import com.oracle.svm.core.thread.VMThreads; |
| 43 | +import com.oracle.svm.core.threadlocal.FastThreadLocalFactory; |
| 44 | +import com.oracle.svm.core.threadlocal.FastThreadLocalLong; |
| 45 | +import com.oracle.svm.core.util.TimeUtils; |
| 46 | + |
| 47 | +public class ThreadCPULoadEvent { |
| 48 | + private static final FastThreadLocalLong cpuTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.cpuTimeTL"); |
| 49 | + private static final FastThreadLocalLong userTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.userTimeTL"); |
| 50 | + private static final FastThreadLocalLong wallclockTimeTL = FastThreadLocalFactory.createLong("ThreadCPULoadEvent.wallclockTimeTL"); |
| 51 | + |
| 52 | + private static volatile int lastActiveProcessorCount; |
| 53 | + |
| 54 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 55 | + public static void initWallclockTime(IsolateThread isolateThread) { |
| 56 | + wallclockTimeTL.set(isolateThread, getCurrentTime()); |
| 57 | + } |
| 58 | + |
| 59 | + @Uninterruptible(reason = "Accesses a JFR buffer.") |
| 60 | + public static void emit(IsolateThread isolateThread) { |
| 61 | + if (!JfrEvent.ThreadCPULoad.shouldEmit()) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + long curCpuTime = getThreadCpuTime(isolateThread, true); |
| 66 | + long prevCpuTime = cpuTimeTL.get(isolateThread); |
| 67 | + |
| 68 | + long curTime = getCurrentTime(); |
| 69 | + long prevTime = wallclockTimeTL.get(isolateThread); |
| 70 | + wallclockTimeTL.set(isolateThread, curTime); |
| 71 | + |
| 72 | + /* Threshold of 1 ms. */ |
| 73 | + if (curCpuTime - prevCpuTime < 1 * TimeUtils.nanosPerMilli) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + long curUserTime = getThreadCpuTime(isolateThread, false); |
| 78 | + long prevUserTime = userTimeTL.get(isolateThread); |
| 79 | + |
| 80 | + long curSystemTime = curCpuTime - curUserTime; |
| 81 | + long prevSystemTime = prevCpuTime - prevUserTime; |
| 82 | + |
| 83 | + /* |
| 84 | + * The user and total cpu usage clocks can have different resolutions, which can make us see |
| 85 | + * decreasing system time. Ensure time doesn't go backwards. |
| 86 | + */ |
| 87 | + if (prevSystemTime > curSystemTime) { |
| 88 | + curCpuTime += prevSystemTime - curSystemTime; |
| 89 | + curSystemTime = prevSystemTime; |
| 90 | + } |
| 91 | + |
| 92 | + int processorsCount = getProcessorCount(); |
| 93 | + |
| 94 | + long userTime = curUserTime - prevUserTime; |
| 95 | + long systemTime = curSystemTime - prevSystemTime; |
| 96 | + long wallClockTime = curTime - prevTime; |
| 97 | + float totalAvailableTime = wallClockTime * processorsCount; |
| 98 | + |
| 99 | + /* Avoid reporting percentages above the theoretical max. */ |
| 100 | + if (userTime + systemTime > wallClockTime) { |
| 101 | + long excess = userTime + systemTime - wallClockTime; |
| 102 | + curCpuTime -= excess; |
| 103 | + if (userTime > excess) { |
| 104 | + userTime -= excess; |
| 105 | + curUserTime -= excess; |
| 106 | + } else { |
| 107 | + excess -= userTime; |
| 108 | + curUserTime -= userTime; |
| 109 | + userTime = 0; |
| 110 | + systemTime -= excess; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + cpuTimeTL.set(isolateThread, curCpuTime); |
| 115 | + userTimeTL.set(isolateThread, curUserTime); |
| 116 | + |
| 117 | + JfrNativeEventWriterData data = StackValue.get(JfrNativeEventWriterData.class); |
| 118 | + JfrNativeEventWriterDataAccess.initializeThreadLocalNativeBuffer(data); |
| 119 | + |
| 120 | + JfrNativeEventWriter.beginSmallEvent(data, JfrEvent.ThreadCPULoad); |
| 121 | + JfrNativeEventWriter.putLong(data, JfrTicks.elapsedTicks()); |
| 122 | + JfrNativeEventWriter.putEventThread(data); |
| 123 | + JfrNativeEventWriter.putFloat(data, userTime / totalAvailableTime); |
| 124 | + JfrNativeEventWriter.putFloat(data, systemTime / totalAvailableTime); |
| 125 | + JfrNativeEventWriter.endSmallEvent(data); |
| 126 | + } |
| 127 | + |
| 128 | + public static void emitEvents() { |
| 129 | + /* This is safe because the VM operation rechecks if the event should be emitted. */ |
| 130 | + if (shouldEmitUnsafe()) { |
| 131 | + EmitThreadCPULoadEventsOperation vmOp = new EmitThreadCPULoadEventsOperation(); |
| 132 | + vmOp.enqueue(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 137 | + private static int getProcessorCount() { |
| 138 | + /* |
| 139 | + * This should but does not take the container support into account. Unfortunately, it is |
| 140 | + * currently not possible to call Containers.activeProcessorCount() from uninterruptible |
| 141 | + * code. |
| 142 | + */ |
| 143 | + int curProcessorCount = Jvm.JVM_ActiveProcessorCount(); |
| 144 | + int prevProcessorCount = lastActiveProcessorCount; |
| 145 | + lastActiveProcessorCount = curProcessorCount; |
| 146 | + |
| 147 | + /* |
| 148 | + * If the number of processors decreases, we don't know at what point during the sample |
| 149 | + * interval this happened, so use the largest number to try to avoid percentages above 100%. |
| 150 | + */ |
| 151 | + return UninterruptibleUtils.Math.max(curProcessorCount, prevProcessorCount); |
| 152 | + } |
| 153 | + |
| 154 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 155 | + private static long getThreadCpuTime(IsolateThread isolateThread, boolean includeSystemTime) { |
| 156 | + long threadCpuTime = ThreadCpuTimeSupport.getInstance().getThreadCpuTime( |
| 157 | + VMThreads.findOSThreadHandleForIsolateThread(isolateThread), includeSystemTime); |
| 158 | + return (threadCpuTime < 0) ? 0 : threadCpuTime; |
| 159 | + } |
| 160 | + |
| 161 | + @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) |
| 162 | + private static long getCurrentTime() { |
| 163 | + return System.nanoTime(); |
| 164 | + } |
| 165 | + |
| 166 | + @Uninterruptible(reason = "Used to avoid the VM operation if it is not absolutely needed.") |
| 167 | + private static boolean shouldEmitUnsafe() { |
| 168 | + /* The returned value is racy. */ |
| 169 | + return JfrEvent.ThreadCPULoad.shouldEmit(); |
| 170 | + } |
| 171 | + |
| 172 | + private static final class EmitThreadCPULoadEventsOperation extends JavaVMOperation { |
| 173 | + EmitThreadCPULoadEventsOperation() { |
| 174 | + super(VMOperationInfos.get(EmitThreadCPULoadEventsOperation.class, "Emit ThreadCPULoad events", SystemEffect.SAFEPOINT)); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + protected void operate() { |
| 179 | + for (IsolateThread isolateThread = VMThreads.firstThread(); isolateThread.isNonNull(); isolateThread = VMThreads.nextThread(isolateThread)) { |
| 180 | + emit(isolateThread); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments