comparison graal/com.oracle.jvmci.options/src/com/oracle/jvmci/options/OptionValue.java @ 21554:b1530a6cce8c

renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 23:21:15 +0200
parents graal/com.oracle.graal.options/src/com/oracle/graal/options/OptionValue.java@07f2a49f0bfb
children d563baeca9df
comparison
equal deleted inserted replaced
21553:0910a9497b02 21554:b1530a6cce8c
1 /*
2 * Copyright (c) 2013, 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.jvmci.options;
24
25 import java.io.*;
26 import java.util.*;
27 import java.util.Map.Entry;
28
29 /**
30 * An option value.
31 */
32 public class OptionValue<T> {
33
34 /**
35 * Temporarily changes the value for an option. The {@linkplain OptionValue#getValue() value} of
36 * {@code option} is set to {@code value} until {@link OverrideScope#close()} is called on the
37 * object returned by this method.
38 * <p>
39 * Since the returned object is {@link AutoCloseable} the try-with-resource construct can be
40 * used:
41 *
42 * <pre>
43 * try (OverrideScope s = OptionValue.override(myOption, myValue) {
44 * // code that depends on myOption == myValue
45 * }
46 * </pre>
47 */
48 public static OverrideScope override(OptionValue<?> option, Object value) {
49 OverrideScope current = getOverrideScope();
50 if (current == null) {
51 if (!value.equals(option.getValue())) {
52 return new SingleOverrideScope(option, value);
53 }
54 Map<OptionValue<?>, Object> overrides = Collections.emptyMap();
55 return new MultipleOverridesScope(current, overrides);
56 }
57 return new MultipleOverridesScope(current, option, value);
58 }
59
60 /**
61 * Temporarily changes the values for a set of options. The {@linkplain OptionValue#getValue()
62 * value} of each {@code option} in {@code overrides} is set to the corresponding {@code value}
63 * in {@code overrides} until {@link OverrideScope#close()} is called on the object returned by
64 * this method.
65 * <p>
66 * Since the returned object is {@link AutoCloseable} the try-with-resource construct can be
67 * used:
68 *
69 * <pre>
70 * Map&lt;OptionValue, Object&gt; overrides = new HashMap&lt;&gt;();
71 * overrides.put(myOption1, myValue1);
72 * overrides.put(myOption2, myValue2);
73 * try (OverrideScope s = OptionValue.override(overrides) {
74 * // code that depends on myOption == myValue
75 * }
76 * </pre>
77 */
78 public static OverrideScope override(Map<OptionValue<?>, Object> overrides) {
79 OverrideScope current = getOverrideScope();
80 if (current == null && overrides.size() == 1) {
81 Entry<OptionValue<?>, Object> single = overrides.entrySet().iterator().next();
82 OptionValue<?> option = single.getKey();
83 Object overrideValue = single.getValue();
84 if (!overrideValue.equals(option.getValue())) {
85 return new SingleOverrideScope(option, overrideValue);
86 }
87 }
88 return new MultipleOverridesScope(current, overrides);
89 }
90
91 /**
92 * Temporarily changes the values for a set of options. The {@linkplain OptionValue#getValue()
93 * value} of each {@code option} in {@code overrides} is set to the corresponding {@code value}
94 * in {@code overrides} until {@link OverrideScope#close()} is called on the object returned by
95 * this method.
96 * <p>
97 * Since the returned object is {@link AutoCloseable} the try-with-resource construct can be
98 * used:
99 *
100 * <pre>
101 * try (OverrideScope s = OptionValue.override(myOption1, myValue1, myOption2, myValue2) {
102 * // code that depends on myOption == myValue
103 * }
104 * </pre>
105 *
106 * @param overrides overrides in the form {@code [option1, override1, option2, override2, ...]}
107 */
108 public static OverrideScope override(Object... overrides) {
109 OverrideScope current = getOverrideScope();
110 if (current == null && overrides.length == 2) {
111 OptionValue<?> option = (OptionValue<?>) overrides[0];
112 Object overrideValue = overrides[1];
113 if (!overrideValue.equals(option.getValue())) {
114 return new SingleOverrideScope(option, overrideValue);
115 }
116 }
117 Map<OptionValue<?>, Object> map = Collections.emptyMap();
118 for (int i = 0; i < overrides.length; i += 2) {
119 OptionValue<?> option = (OptionValue<?>) overrides[i];
120 Object overrideValue = overrides[i + 1];
121 if (!overrideValue.equals(option.getValue())) {
122 if (map.isEmpty()) {
123 map = new HashMap<>();
124 }
125 map.put(option, overrideValue);
126 }
127 }
128 return new MultipleOverridesScope(current, map);
129 }
130
131 private static final ThreadLocal<OverrideScope> overrideScopeTL = new ThreadLocal<>();
132
133 protected static OverrideScope getOverrideScope() {
134 return overrideScopeTL.get();
135 }
136
137 protected static void setOverrideScope(OverrideScope overrideScope) {
138 overrideScopeTL.set(overrideScope);
139 }
140
141 private T initialValue;
142
143 /**
144 * The raw option value.
145 */
146 protected T value;
147
148 private OptionDescriptor descriptor;
149
150 private long reads;
151 private OptionValue<?> next;
152 private static OptionValue<?> head;
153
154 private static final boolean ShowReadsHistogram = Boolean.getBoolean("graal.showOptionValueReadsHistogram");
155
156 private static void addToHistogram(OptionValue<?> option) {
157 if (ShowReadsHistogram) {
158 synchronized (OptionValue.class) {
159 option.next = head;
160 head = option;
161 }
162 }
163 }
164
165 @SuppressWarnings("unchecked")
166 public OptionValue(T value) {
167 this.initialValue = value;
168 this.value = (T) UNINITIALIZED;
169 addToHistogram(this);
170 }
171
172 private static final Object UNINITIALIZED = "UNINITIALIZED";
173
174 /**
175 * Creates an uninitialized option value for a subclass that initializes itself
176 * {@link #initialValue() lazily}.
177 */
178 @SuppressWarnings("unchecked")
179 protected OptionValue() {
180 this.initialValue = (T) UNINITIALIZED;
181 this.value = (T) UNINITIALIZED;
182 addToHistogram(this);
183 }
184
185 /**
186 * Lazy initialization of value.
187 */
188 protected T initialValue() {
189 throw new InternalError("Uninitialized option value must override initialValue()");
190 }
191
192 /**
193 * Sets the descriptor for this option.
194 */
195 public void setDescriptor(OptionDescriptor descriptor) {
196 this.descriptor = descriptor;
197 }
198
199 /**
200 * Returns the descriptor for this option, if it has been set by
201 * {@link #setDescriptor(OptionDescriptor)}.
202 */
203 public OptionDescriptor getDescriptor() {
204 return descriptor;
205 }
206
207 /**
208 * Gets the name of this option. The name for an option value with a null
209 * {@linkplain #setDescriptor(OptionDescriptor) descriptor} is the value of
210 * {@link Object#toString()}.
211 */
212 public String getName() {
213 return descriptor == null ? super.toString() : (descriptor.getDeclaringClass().getName() + "." + descriptor.getName());
214 }
215
216 @Override
217 public String toString() {
218 return getName() + "=" + getValue();
219 }
220
221 /**
222 * The initial value specified in source code. The returned value is not affected by calls to
223 * {@link #setValue(Object)} or registering {@link OverrideScope}s. Therefore, it is also not
224 * affected by options set on the command line.
225 */
226 public T getInitialValue() {
227 if (initialValue == UNINITIALIZED) {
228 initialValue = initialValue();
229 }
230 return initialValue;
231 }
232
233 /**
234 * Returns true if the option has the same value that was set in the source code.
235 */
236 public boolean hasInitialValue() {
237 if (!(this instanceof StableOptionValue)) {
238 OverrideScope overrideScope = getOverrideScope();
239 if (overrideScope != null) {
240 T override = overrideScope.getOverride(this);
241 if (override != null) {
242 return false;
243 }
244 }
245 }
246 return value == UNINITIALIZED || Objects.equals(value, getInitialValue());
247 }
248
249 /**
250 * Gets the value of this option.
251 */
252 public T getValue() {
253 if (ShowReadsHistogram) {
254 reads++;
255 }
256 if (!(this instanceof StableOptionValue)) {
257 OverrideScope overrideScope = getOverrideScope();
258 if (overrideScope != null) {
259 T override = overrideScope.getOverride(this);
260 if (override != null) {
261 return override;
262 }
263 }
264 }
265 if (value != UNINITIALIZED) {
266 return value;
267 } else {
268 return getInitialValue();
269 }
270 }
271
272 /**
273 * Gets the values of this option including overridden values.
274 *
275 * @param c the collection to which the values are added. If null, one is allocated.
276 * @return the collection to which the values were added in order from most overridden to
277 * current value
278 */
279 @SuppressWarnings("unchecked")
280 public Collection<T> getValues(Collection<T> c) {
281 Collection<T> values = c == null ? new ArrayList<>() : c;
282 if (!(this instanceof StableOptionValue)) {
283 OverrideScope overrideScope = getOverrideScope();
284 if (overrideScope != null) {
285 overrideScope.getOverrides(this, (Collection<Object>) values);
286 }
287 }
288 if (value != UNINITIALIZED) {
289 values.add(value);
290 } else {
291 values.add(getInitialValue());
292 }
293 return values;
294 }
295
296 /**
297 * Sets the value of this option.
298 */
299 @SuppressWarnings("unchecked")
300 public void setValue(Object v) {
301 this.value = (T) v;
302 }
303
304 /**
305 * An object whose {@link #close()} method reverts the option value overriding initiated by
306 * {@link OptionValue#override(OptionValue, Object)} or {@link OptionValue#override(Map)}.
307 */
308 public abstract static class OverrideScope implements AutoCloseable {
309
310 private Map<DerivedOptionValue<?>, Object> derivedCache = null;
311
312 public <T> T getDerived(DerivedOptionValue<T> key) {
313 if (derivedCache == null) {
314 derivedCache = new HashMap<>();
315 }
316 @SuppressWarnings("unchecked")
317 T ret = (T) derivedCache.get(key);
318 if (ret == null) {
319 ret = key.createValue();
320 derivedCache.put(key, ret);
321 }
322 return ret;
323 }
324
325 abstract void addToInherited(Map<OptionValue<?>, Object> inherited);
326
327 abstract <T> T getOverride(OptionValue<T> option);
328
329 abstract void getOverrides(OptionValue<?> option, Collection<Object> c);
330
331 public abstract void close();
332 }
333
334 static class SingleOverrideScope extends OverrideScope {
335
336 private final OptionValue<?> option;
337 private final Object value;
338
339 public SingleOverrideScope(OptionValue<?> option, Object value) {
340 if (option instanceof StableOptionValue) {
341 throw new IllegalArgumentException("Cannot override stable option " + option);
342 }
343 this.option = option;
344 this.value = value;
345 setOverrideScope(this);
346 }
347
348 @Override
349 void addToInherited(Map<OptionValue<?>, Object> inherited) {
350 inherited.put(option, value);
351 }
352
353 @SuppressWarnings("unchecked")
354 @Override
355 <T> T getOverride(OptionValue<T> key) {
356 if (key == this.option) {
357 return (T) value;
358 }
359 return null;
360 }
361
362 @Override
363 void getOverrides(OptionValue<?> key, Collection<Object> c) {
364 if (key == this.option) {
365 c.add(value);
366 }
367 }
368
369 @Override
370 public void close() {
371 setOverrideScope(null);
372 }
373 }
374
375 static class MultipleOverridesScope extends OverrideScope {
376 final OverrideScope parent;
377 final Map<OptionValue<?>, Object> overrides;
378
379 public MultipleOverridesScope(OverrideScope parent, OptionValue<?> option, Object value) {
380 this.parent = parent;
381 this.overrides = new HashMap<>();
382 if (parent != null) {
383 parent.addToInherited(overrides);
384 }
385 if (option instanceof StableOptionValue) {
386 throw new IllegalArgumentException("Cannot override stable option " + option);
387 }
388 if (!value.equals(option.getValue())) {
389 this.overrides.put(option, value);
390 }
391 if (!overrides.isEmpty()) {
392 setOverrideScope(this);
393 }
394 }
395
396 MultipleOverridesScope(OverrideScope parent, Map<OptionValue<?>, Object> overrides) {
397 this.parent = parent;
398 if (overrides.isEmpty() && parent == null) {
399 this.overrides = Collections.emptyMap();
400 return;
401 }
402 this.overrides = new HashMap<>();
403 if (parent != null) {
404 parent.addToInherited(this.overrides);
405 }
406 for (Map.Entry<OptionValue<?>, Object> e : overrides.entrySet()) {
407 OptionValue<?> option = e.getKey();
408 if (option instanceof StableOptionValue) {
409 throw new IllegalArgumentException("Cannot override stable option " + option);
410 }
411 if (!e.getValue().equals(option.getValue())) {
412 this.overrides.put(option, e.getValue());
413 }
414 }
415 if (!this.overrides.isEmpty()) {
416 setOverrideScope(this);
417 }
418 }
419
420 @Override
421 void addToInherited(Map<OptionValue<?>, Object> inherited) {
422 if (parent != null) {
423 parent.addToInherited(inherited);
424 }
425 inherited.putAll(overrides);
426 }
427
428 @SuppressWarnings("unchecked")
429 @Override
430 <T> T getOverride(OptionValue<T> option) {
431 return (T) overrides.get(option);
432 }
433
434 @Override
435 void getOverrides(OptionValue<?> option, Collection<Object> c) {
436 Object v = overrides.get(option);
437 if (v != null) {
438 c.add(v);
439 }
440 if (parent != null) {
441 parent.getOverrides(option, c);
442 }
443 }
444
445 @Override
446 public void close() {
447 if (!overrides.isEmpty()) {
448 setOverrideScope(parent);
449 }
450 }
451 }
452
453 static {
454 if (ShowReadsHistogram) {
455 Runtime.getRuntime().addShutdownHook(new Thread() {
456 @Override
457 public void run() {
458 ArrayList<OptionValue<?>> options = new ArrayList<>();
459 for (OptionValue<?> option = head; option != null; option = option.next) {
460 options.add(option);
461 }
462 Collections.sort(options, new Comparator<OptionValue<?>>() {
463
464 public int compare(OptionValue<?> o1, OptionValue<?> o2) {
465 if (o1.reads < o2.reads) {
466 return -1;
467 } else if (o1.reads > o2.reads) {
468 return 1;
469 } else {
470 return o1.getName().compareTo(o2.getName());
471 }
472 }
473 });
474 PrintStream out = System.out;
475 out.println("=== OptionValue reads histogram ===");
476 for (OptionValue<?> option : options) {
477 out.println(option.reads + "\t" + option);
478 }
479 }
480 });
481 }
482 }
483 }