001/*
002 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot;
024
025import java.lang.reflect.*;
026import java.util.*;
027
028import jdk.internal.jvmci.code.*;
029import jdk.internal.jvmci.hotspot.*;
030import jdk.internal.jvmci.service.*;
031
032import com.oracle.graal.debug.*;
033import com.oracle.graal.hotspot.logging.*;
034
035@ServiceProvider(HotSpotVMEventListener.class)
036public class HotSpotGraalVMEventListener implements HotSpotVMEventListener {
037
038    @Override
039    public void notifyCompileTheWorld() throws Throwable {
040        CompilerToVM compilerToVM = HotSpotJVMCIRuntime.runtime().getCompilerToVM();
041        int iterations = CompileTheWorld.Options.CompileTheWorldIterations.getValue();
042        for (int i = 0; i < iterations; i++) {
043            compilerToVM.resetCompilationStatistics();
044            TTY.println("CompileTheWorld : iteration " + i);
045            CompileTheWorld ctw = new CompileTheWorld();
046            ctw.compile();
047        }
048        System.exit(0);
049    }
050
051    @Override
052    public void notifyShutdown() {
053        HotSpotGraalRuntime.runtime().shutdown();
054    }
055
056    @Override
057    public void compileMetaspaceMethod(long metaspaceMethod, int entryBCI, long jvmciEnv, int id) {
058        HotSpotResolvedJavaMethod method = HotSpotResolvedJavaMethodImpl.fromMetaspace(metaspaceMethod);
059        CompilationTask.compileMethod(method, entryBCI, jvmciEnv, id);
060    }
061
062    @Override
063    public void notifyInstall(HotSpotCodeCacheProvider codeCache, InstalledCode installedCode, CompilationResult compResult) {
064        if (Debug.isDumpEnabled()) {
065            Debug.dump(new Object[]{compResult, installedCode}, "After code installation");
066        }
067        if (Debug.isLogEnabled()) {
068            Debug.log("%s", codeCache.disassemble(installedCode));
069        }
070    }
071
072    @Override
073    public CompilerToVM completeInitialization(HotSpotJVMCIRuntime runtime, CompilerToVM compilerToVM) {
074        CompilerToVM toVM = compilerToVM;
075        if (CountingProxy.ENABLED) {
076            toVM = CountingProxy.getProxy(CompilerToVM.class, toVM);
077        }
078        if (Logger.ENABLED) {
079            toVM = LoggingProxy.getProxy(CompilerToVM.class, toVM);
080        }
081
082        if (Boolean.valueOf(System.getProperty("jvmci.printconfig"))) {
083            printConfig(runtime.getConfig());
084        }
085
086        return toVM;
087    }
088
089    private static void printConfig(HotSpotVMConfig config) {
090        Field[] fields = config.getClass().getDeclaredFields();
091        Map<String, Field> sortedFields = new TreeMap<>();
092        for (Field f : fields) {
093            f.setAccessible(true);
094            sortedFields.put(f.getName(), f);
095        }
096        for (Field f : sortedFields.values()) {
097            try {
098                Logger.info(String.format("%9s %-40s = %s", f.getType().getSimpleName(), f.getName(), Logger.pretty(f.get(config))));
099            } catch (Exception e) {
100            }
101        }
102    }
103}