comparison graal/com.oracle.max.graal.runtime/src/com/oracle/max/graal/runtime/CompilerImpl.java @ 2874:d90bf514d647

Renamed packages.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 08 Jun 2011 08:59:54 +0200
parents graal/com.oracle.max.graal.runtime/src/com/oracle/graal/runtime/CompilerImpl.java@0341b6424579
children 224412c24426
comparison
equal deleted inserted replaced
2873:810e2d253e00 2874:d90bf514d647
1 /*
2 * Copyright (c) 2011, 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 com.oracle.max.graal.runtime;
24
25 import java.io.*;
26 import java.lang.management.*;
27 import java.net.*;
28
29 import com.oracle.max.asm.target.amd64.*;
30 import com.oracle.max.graal.compiler.*;
31 import com.oracle.max.graal.runtime.logging.*;
32 import com.oracle.max.graal.runtime.server.*;
33 import com.sun.cri.ci.*;
34 import com.sun.cri.ri.*;
35 import com.sun.cri.xir.*;
36
37 /**
38 * Singleton class holding the instance of the C1XCompiler.
39 */
40 public final class CompilerImpl implements Compiler, Remote {
41
42 private static Compiler theInstance;
43 private static boolean PrintGCStats = false;
44
45 public static Compiler getInstance() {
46 return theInstance;
47 }
48
49 public static void initialize() {
50 if (theInstance != null) {
51 throw new IllegalStateException("Compiler already initialized");
52 }
53
54 String remote = System.getProperty("c1x.remote");
55 if (remote != null) {
56 // remote compilation (will not create a local Compiler)
57 try {
58 System.out.println("C1X compiler started in client/server mode, server: " + remote);
59 Socket socket = new Socket(remote, 1199);
60 ReplacingStreams streams = new ReplacingStreams(socket.getOutputStream(), socket.getInputStream());
61 streams.getInvocation().sendResult(new VMEntriesNative());
62
63 theInstance = (Compiler) streams.getInvocation().waitForResult(false);
64 } catch (IOException e1) {
65 System.out.println("Connection to compilation server FAILED.");
66 throw new RuntimeException(e1);
67 } catch (ClassNotFoundException e2) {
68 System.out.println("Connection to compilation server FAILED.");
69 throw new RuntimeException(e2);
70 }
71 } else {
72 // ordinary local compilation
73 theInstance = new CompilerImpl(null);
74 Runtime.getRuntime().addShutdownHook(new ShutdownThread());
75 }
76 }
77
78 public static Compiler initializeServer(VMEntries entries) {
79 assert theInstance == null;
80 theInstance = new CompilerImpl(entries);
81 Runtime.getRuntime().addShutdownHook(new ShutdownThread());
82 return theInstance;
83 }
84
85 public static class ShutdownThread extends Thread {
86
87 @Override
88 public void run() {
89 VMExitsNative.compileMethods = false;
90 if (C1XOptions.PrintMetrics) {
91 C1XMetrics.print();
92 }
93 if (C1XOptions.PrintTimers) {
94 C1XTimers.print();
95 }
96 if (PrintGCStats) {
97 printGCStats();
98 }
99 }
100 }
101
102 public static void printGCStats() {
103 long totalGarbageCollections = 0;
104 long garbageCollectionTime = 0;
105
106 for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
107 long count = gc.getCollectionCount();
108 if (count >= 0) {
109 totalGarbageCollections += count;
110 }
111
112 long time = gc.getCollectionTime();
113 if (time >= 0) {
114 garbageCollectionTime += time;
115 }
116 }
117
118 System.out.println("Total Garbage Collections: " + totalGarbageCollections);
119 System.out.println("Total Garbage Collection Time (ms): " + garbageCollectionTime);
120 }
121
122 private final VMEntries vmEntries;
123 private final VMExits vmExits;
124 private C1XCompiler compiler;
125
126 private final HotSpotRuntime runtime;
127 private final CiTarget target;
128 private final RiXirGenerator generator;
129 private final RiRegisterConfig registerConfig;
130
131 private CompilerImpl(VMEntries entries) {
132
133 // initialize VMEntries
134 if (entries == null) {
135 entries = new VMEntriesNative();
136 }
137
138 // initialize VMExits
139 VMExits exits = new VMExitsNative(this);
140
141 // logging, etc.
142 if (CountingProxy.ENABLED) {
143 exits = CountingProxy.getProxy(VMExits.class, exits);
144 entries = CountingProxy.getProxy(VMEntries.class, entries);
145 }
146 if (Logger.ENABLED) {
147 exits = LoggingProxy.getProxy(VMExits.class, exits);
148 entries = LoggingProxy.getProxy(VMEntries.class, entries);
149 }
150
151 // set the final fields
152 vmEntries = entries;
153 vmExits = exits;
154
155 // initialize compiler and C1XOptions
156 HotSpotVMConfig config = vmEntries.getConfiguration();
157 config.check();
158
159 // these options are important - c1x4hotspot will not generate correct code without them
160 C1XOptions.GenSpecialDivChecks = true;
161 C1XOptions.NullCheckUniquePc = true;
162 C1XOptions.InvokeSnippetAfterArguments = true;
163 C1XOptions.StackShadowPages = config.stackShadowPages;
164
165 runtime = new HotSpotRuntime(config, this);
166 registerConfig = runtime.globalStubRegConfig;
167
168 final int wordSize = 8;
169 final int stackFrameAlignment = 16;
170 target = new HotSpotTarget(new AMD64(), true, wordSize, stackFrameAlignment, config.vmPageSize, wordSize, true);
171
172 RiXirGenerator generator = new HotSpotXirGenerator(config, target, registerConfig, this);
173 if (Logger.ENABLED) {
174 this.generator = LoggingProxy.getProxy(RiXirGenerator.class, generator);
175 } else {
176 this.generator = generator;
177 }
178
179 }
180
181 @Override
182 public C1XCompiler getCompiler() {
183 if (compiler == null) {
184 compiler = new C1XCompiler(runtime, target, generator, registerConfig);
185 }
186 return compiler;
187 }
188
189 @Override
190 public VMEntries getVMEntries() {
191 return vmEntries;
192 }
193
194 @Override
195 public VMExits getVMExits() {
196 return vmExits;
197 }
198
199 }