Skip to content

Instantly share code, notes, and snippets.

@jiekang
Created March 15, 2021 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiekang/d3c0a7e11b689f57950ec009c5f8fff4 to your computer and use it in GitHub Desktop.
Save jiekang/d3c0a7e11b689f57950ec009c5f8fff4 to your computer and use it in GitHub Desktop.
JFR Event Substitution Example
/*
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.jfr;
//Checkstyle: allow reflection
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.concurrent.ConcurrentHashMap;
import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import com.oracle.graal.pointsto.infrastructure.OriginalClassProvider;
import com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.c.GraalAccess;
import com.oracle.svm.util.ReflectionUtil;
import jdk.jfr.Event;
import jdk.jfr.internal.EventWriter;
import jdk.jfr.internal.JVM;
import jdk.jfr.internal.SecuritySupport;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import sun.misc.Unsafe;
@Platforms(Platform.HOSTED_ONLY.class)
public class JfrEventSubstitution extends SubstitutionProcessor {
private final ResolvedJavaType jdkJfrEvent;
private final ConcurrentHashMap<ResolvedJavaType, Boolean> typeSubstitution;
JfrEventSubstitution(MetaAccessProvider metaAccess) {
jdkJfrEvent = metaAccess.lookupJavaType(Event.class);
ResolvedJavaType jdkJfrEventWriter = metaAccess.lookupJavaType(EventWriter.class);
typeSubstitution = new ConcurrentHashMap<>();
}
@Override
public ResolvedJavaType lookup(ResolvedJavaType type) {
if (!jdkJfrEvent.equals(type) && jdkJfrEvent.isAssignableFrom(type) && !type.isAbstract()) {
typeSubstitution.computeIfAbsent(type, JfrEventSubstitution::initEventClass);
}
return type;
}
private static Boolean initEventClass(ResolvedJavaType eventType) throws RuntimeException {
try {
Class<? extends Event> newEventClass = OriginalClassProvider.getJavaClass(GraalAccess.getOriginalSnippetReflection(), eventType).asSubclass(Event.class);
eventType.initialize();
SecuritySupport.registerEvent(newEventClass);
JfrJavaEvents.registerEventClass(newEventClass);
JVM.getJVM().retransformClasses(new Class<?>[]{newEventClass});
Field field = ReflectionUtil.lookupField(newEventClass, "eventHandler"); // EventInstrumentation.FIELD_EVENT_HANDLER
RuntimeReflection.register(field);
return Boolean.TRUE;
} catch (Throwable ex) {
throw VMError.shouldNotReachHere(ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment