Mercurial > hg > truffle
annotate graal/com.oracle.jvmci.debug/src/com/oracle/jvmci/debug/Fingerprint.java @ 21794:483b357016a6
Use export target explicitly in mx_graal.py when building JDK (JBS:GRAAL-52)
author | Stefan Anzinger <stefan.anzinger@oracle.com> |
---|---|
date | Mon, 08 Jun 2015 20:30:39 +0200 |
parents | d563baeca9df |
children |
rev | line source |
---|---|
18883 | 1 /* |
2 * Copyright (c) 2015, 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 */ | |
21554
b1530a6cce8c
renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
18894
diff
changeset
|
23 package com.oracle.jvmci.debug; |
18883 | 24 |
25 import java.util.*; | |
26 import java.util.stream.*; | |
27 | |
28 /** | |
29 * Facility for fingerprinting execution. | |
30 */ | |
31 public class Fingerprint implements AutoCloseable { | |
32 | |
21558
d563baeca9df
changed uses of Graal terminology to JVMCI (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
21554
diff
changeset
|
33 public static final String ENABLED_PROPERTY_NAME = "jvmci.fingerprint"; |
18883 | 34 |
35 /** | |
36 * Determines whether fingerprinting is enabled. This is set by the | |
37 * {@value #ENABLED_PROPERTY_NAME} system property when this class is initialized. | |
38 */ | |
39 public static final boolean ENABLED = Boolean.getBoolean(ENABLED_PROPERTY_NAME); | |
40 | |
41 private static final ThreadLocal<Fingerprint> current = ENABLED ? new ThreadLocal<>() : null; | |
42 | |
43 private final List<String> events; | |
44 private int index; | |
45 | |
46 /** | |
47 * Creates an object to record a fingerprint. | |
48 */ | |
49 public Fingerprint() { | |
50 events = new ArrayList<>(); | |
51 index = -1; | |
52 } | |
53 | |
54 /** | |
55 * Creates an object to verify execution matches a given fingerprint. | |
56 * | |
18894
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
57 * @param toVerifyAgainst the fingerprint events to verify against |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
58 */ |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
59 public Fingerprint(List<String> toVerifyAgainst) { |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
60 this.events = toVerifyAgainst; |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
61 index = 0; |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
62 } |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
63 |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
64 /** |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
65 * Creates an object to verify execution matches a given fingerprint. |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
66 * |
18883 | 67 * @param toVerifyAgainst the fingerprint to verify against |
68 */ | |
69 public Fingerprint(Fingerprint toVerifyAgainst) { | |
18894
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
70 this(toVerifyAgainst.events); |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
71 } |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
72 |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
73 public Collection<String> getEvents() { |
203618cfd638
added Fingerprint constructor that takes a list of events
Doug Simon <doug.simon@oracle.com>
parents:
18883
diff
changeset
|
74 return Collections.unmodifiableCollection(events); |
18883 | 75 } |
76 | |
77 /** | |
78 * Starts fingerprint recording or verification for the current thread. At most one fingerprint | |
79 * object can be active for any thread. | |
80 */ | |
81 public Fingerprint open() { | |
82 if (ENABLED) { | |
83 assert current.get() == null; | |
84 current.set(this); | |
85 return this; | |
86 } | |
87 return null; | |
88 } | |
89 | |
90 /** | |
91 * Finishes fingerprint recording or verification for the current thread. | |
92 */ | |
93 public void close() { | |
94 if (ENABLED) { | |
95 assert current.get() == this; | |
96 current.set(null); | |
97 } | |
98 } | |
99 | |
100 private static final int BREAKPOINT_EVENT = Integer.getInteger(ENABLED_PROPERTY_NAME + ".breakpointEvent", -1); | |
101 | |
102 /** | |
103 * Submits an execution event for the purpose of recording or verifying a fingerprint. This must | |
104 * only be called if {@link #ENABLED} is {@code true}. | |
105 */ | |
106 public static void submit(String format, Object... args) { | |
107 assert ENABLED : "fingerprinting must be enabled (-D" + ENABLED_PROPERTY_NAME + "=true)"; | |
108 Fingerprint fingerprint = current.get(); | |
109 if (fingerprint != null) { | |
110 int eventId = fingerprint.nextEventId(); | |
111 if (eventId == BREAKPOINT_EVENT) { | |
112 // Set IDE breakpoint on the following line and set the relevant | |
113 // system property to debug a fingerprint verification error. | |
114 System.console(); | |
115 } | |
116 fingerprint.event(String.format(eventId + ": " + format, args)); | |
117 } | |
118 } | |
119 | |
120 private int nextEventId() { | |
121 return index == -1 ? events.size() : index; | |
122 } | |
123 | |
21558
d563baeca9df
changed uses of Graal terminology to JVMCI (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
21554
diff
changeset
|
124 private static final int MAX_EVENT_TAIL_IN_ERROR_MESSAGE = Integer.getInteger("jvmci.fingerprint.errorEventTailLength", 50); |
18883 | 125 |
126 private String tail() { | |
127 int start = Math.max(index - MAX_EVENT_TAIL_IN_ERROR_MESSAGE, 0); | |
128 return events.subList(start, index).stream().collect(Collectors.joining(String.format("%n"))); | |
129 } | |
130 | |
131 private void event(String entry) { | |
132 if (index == -1) { | |
133 events.add(entry); | |
134 } else { | |
135 if (index > events.size()) { | |
136 throw new InternalError(String.format("%s%nOriginal fingerprint limit reached", tail())); | |
137 } | |
138 String l = events.get(index); | |
139 if (!l.equals(entry)) { | |
140 throw new InternalError(String.format("%s%nFingerprint differs at event %d%nexpected: %s%n actual: %s", tail(), index, l, entry)); | |
141 } | |
142 index++; | |
143 } | |
144 } | |
145 } |