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 */
23package org.graalvm.compiler.hotspot;
24
25import java.util.ArrayList;
26import java.util.List;
27
28import org.graalvm.compiler.code.CompilationResult;
29import org.graalvm.compiler.debug.DebugContext;
30import org.graalvm.compiler.debug.DebugOptions;
31import org.graalvm.compiler.serviceprovider.GraalServices;
32
33import jdk.vm.ci.code.CompiledCode;
34import jdk.vm.ci.code.InstalledCode;
35import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
36import jdk.vm.ci.hotspot.HotSpotVMEventListener;
37
38public class HotSpotGraalVMEventListener implements HotSpotVMEventListener {
39
40    private final HotSpotGraalRuntime runtime;
41    private List<HotSpotCodeCacheListener> listeners;
42
43    HotSpotGraalVMEventListener(HotSpotGraalRuntime runtime) {
44        this.runtime = runtime;
45        listeners = new ArrayList<>();
46        for (HotSpotCodeCacheListener listener : GraalServices.load(HotSpotCodeCacheListener.class)) {
47            listeners.add(listener);
48        }
49    }
50
51    @Override
52    public void notifyShutdown() {
53        runtime.shutdown();
54    }
55
56    @Override
57    public void notifyInstall(HotSpotCodeCacheProvider codeCache, InstalledCode installedCode, CompiledCode compiledCode) {
58        DebugContext debug = DebugContext.forCurrentThread();
59        if (debug.isDumpEnabled(DebugContext.BASIC_LEVEL)) {
60            CompilationResult compResult = debug.contextLookup(CompilationResult.class);
61            assert compResult != null : "can't dump installed code properly without CompilationResult";
62            debug.dump(DebugContext.BASIC_LEVEL, installedCode, "After code installation");
63        }
64        if (debug.isLogEnabled()) {
65            debug.log("%s", codeCache.disassemble(installedCode));
66        }
67        for (HotSpotCodeCacheListener listener : listeners) {
68            listener.notifyInstall(codeCache, installedCode, compiledCode);
69        }
70    }
71
72    @Override
73    public void notifyBootstrapFinished() {
74        runtime.notifyBootstrapFinished();
75        if (DebugOptions.ClearMetricsAfterBootstrap.getValue(runtime.getOptions())) {
76            runtime.clearMetrics();
77        }
78    }
79}
80