comparison graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Fingerprint.java @ 18883:fe7a58f50fe4

added Fingerprint class
author Doug Simon <doug.simon@oracle.com>
date Tue, 20 Jan 2015 16:22:11 +0100
parents
children 203618cfd638
comparison
equal deleted inserted replaced
18882:ceeaa3d58f1c 18883:fe7a58f50fe4
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 */
23 package com.oracle.graal.debug;
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
33 private static final String ENABLED_PROPERTY_NAME = "graal.fingerprint";
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 *
57 * @param toVerifyAgainst the fingerprint to verify against
58 */
59 public Fingerprint(Fingerprint toVerifyAgainst) {
60 this.events = toVerifyAgainst.events;
61 index = 0;
62 }
63
64 /**
65 * Starts fingerprint recording or verification for the current thread. At most one fingerprint
66 * object can be active for any thread.
67 */
68 public Fingerprint open() {
69 if (ENABLED) {
70 assert current.get() == null;
71 current.set(this);
72 return this;
73 }
74 return null;
75 }
76
77 /**
78 * Finishes fingerprint recording or verification for the current thread.
79 */
80 public void close() {
81 if (ENABLED) {
82 assert current.get() == this;
83 current.set(null);
84 }
85 }
86
87 private static final int BREAKPOINT_EVENT = Integer.getInteger(ENABLED_PROPERTY_NAME + ".breakpointEvent", -1);
88
89 /**
90 * Submits an execution event for the purpose of recording or verifying a fingerprint. This must
91 * only be called if {@link #ENABLED} is {@code true}.
92 */
93 public static void submit(String format, Object... args) {
94 assert ENABLED : "fingerprinting must be enabled (-D" + ENABLED_PROPERTY_NAME + "=true)";
95 Fingerprint fingerprint = current.get();
96 if (fingerprint != null) {
97 int eventId = fingerprint.nextEventId();
98 if (eventId == BREAKPOINT_EVENT) {
99 // Set IDE breakpoint on the following line and set the relevant
100 // system property to debug a fingerprint verification error.
101 System.console();
102 }
103 fingerprint.event(String.format(eventId + ": " + format, args));
104 }
105 }
106
107 private int nextEventId() {
108 return index == -1 ? events.size() : index;
109 }
110
111 private static final int MAX_EVENT_TAIL_IN_ERROR_MESSAGE = Integer.getInteger("graal.fingerprint.errorEventTailLength", 50);
112
113 private String tail() {
114 int start = Math.max(index - MAX_EVENT_TAIL_IN_ERROR_MESSAGE, 0);
115 return events.subList(start, index).stream().collect(Collectors.joining(String.format("%n")));
116 }
117
118 private void event(String entry) {
119 if (index == -1) {
120 events.add(entry);
121 } else {
122 if (index > events.size()) {
123 throw new InternalError(String.format("%s%nOriginal fingerprint limit reached", tail()));
124 }
125 String l = events.get(index);
126 if (!l.equals(entry)) {
127 throw new InternalError(String.format("%s%nFingerprint differs at event %d%nexpected: %s%n actual: %s", tail(), index, l, entry));
128 }
129 index++;
130 }
131 }
132 }