1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.hotspot;
  24 
  25 import jdk.vm.ci.code.CompiledCode;
  26 import jdk.vm.ci.code.InstalledCode;
  27 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  28 import jdk.vm.ci.hotspot.HotSpotVMEventListener;
  29 import org.graalvm.compiler.code.CompilationResult;
  30 import org.graalvm.compiler.debug.Debug;
  31 import org.graalvm.compiler.debug.GraalDebugConfig;
  32 import org.graalvm.compiler.serviceprovider.GraalServices;
  33 
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 
  37 public class HotSpotGraalVMEventListener implements HotSpotVMEventListener {
  38 
  39     private final HotSpotGraalRuntime runtime;
  40     private List<HotSpotCodeCacheListener> listeners;
  41 
  42     HotSpotGraalVMEventListener(HotSpotGraalRuntime runtime) {
  43         this.runtime = runtime;
  44         listeners = new ArrayList<>();
  45         for (HotSpotCodeCacheListener listener : GraalServices.load(HotSpotCodeCacheListener.class)) {
  46             listeners.add(listener);
  47         }
  48     }
  49 
  50     @Override
  51     public void notifyShutdown() {
  52         runtime.shutdown();
  53     }
  54 
  55     @Override
  56     public void notifyInstall(HotSpotCodeCacheProvider codeCache, InstalledCode installedCode, CompiledCode compiledCode) {
  57         if (Debug.isDumpEnabled(Debug.BASIC_LEVEL)) {
  58             CompilationResult compResult = Debug.contextLookup(CompilationResult.class);
  59             assert compResult != null : "can't dump installed code properly without CompilationResult";
  60             Debug.dump(Debug.BASIC_LEVEL, installedCode, "After code installation");
  61         }
  62         if (Debug.isLogEnabled()) {
  63             Debug.log("%s", codeCache.disassemble(installedCode));
  64         }
  65         for (HotSpotCodeCacheListener listener : listeners) {
  66             listener.notifyInstall(codeCache, installedCode, compiledCode);
  67         }
  68     }
  69 
  70     @Override
  71     public void notifyBootstrapFinished() {
  72         runtime.notifyBootstrapFinished();
  73         if (GraalDebugConfig.Options.ClearMetricsAfterBootstrap.getValue(runtime.getOptions())) {
  74             runtime.clearMeters();
  75         }
  76     }
  77 }