Skip to content

Commit 7c2c245

Browse files
committed
[GR-48439] Port refactored graphio BGV parsing to compiler folder
PullRequest: graal/17666
2 parents 979124b + 38c92a0 commit 7c2c245

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+11253
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2013, 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.graal.compiler.graph.test.graphio.parsing;
27+
28+
import static org.junit.Assert.assertArrayEquals;
29+
import static org.junit.Assert.assertEquals;
30+
31+
import java.io.EOFException;
32+
import java.io.IOException;
33+
import java.nio.ByteBuffer;
34+
import java.nio.channels.ReadableByteChannel;
35+
import java.nio.charset.StandardCharsets;
36+
import java.security.MessageDigest;
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
40+
import org.junit.Test;
41+
42+
import jdk.graal.compiler.graphio.parsing.BinarySource;
43+
44+
public class BinarySourceTest {
45+
static class Channel implements ReadableByteChannel {
46+
boolean open;
47+
List<byte[]> content = new ArrayList<>();
48+
int pos;
49+
boolean eof;
50+
ByteBuffer current = ByteBuffer.allocate(1000);
51+
52+
void newChunk() {
53+
byte[] chunk = new byte[current.position()];
54+
System.arraycopy(current.array(), 0, chunk, 0, current.position());
55+
content.add(chunk);
56+
current.clear();
57+
}
58+
59+
@Override
60+
public int read(ByteBuffer dst) throws IOException {
61+
if (content.isEmpty()) {
62+
if (eof) {
63+
throw new EOFException();
64+
} else {
65+
eof = true;
66+
return -1;
67+
}
68+
}
69+
byte[] arr = content.get(0);
70+
if (pos < arr.length) {
71+
int l = Math.min(dst.remaining(), arr.length - pos);
72+
dst.put(arr, pos, l);
73+
pos += l;
74+
if (pos >= arr.length) {
75+
content.remove(0);
76+
pos = 0;
77+
}
78+
return l;
79+
} else {
80+
content.remove(0);
81+
pos = 0;
82+
return 0;
83+
}
84+
}
85+
86+
@Override
87+
public boolean isOpen() {
88+
return open;
89+
}
90+
91+
@Override
92+
public void close() throws IOException {
93+
open = false;
94+
}
95+
96+
void updateDigest(MessageDigest digest) {
97+
for (byte[] chunk : content) {
98+
digest.update(chunk);
99+
}
100+
}
101+
102+
}
103+
104+
byte[] slice(byte[] arr, int off, int l) {
105+
byte[] r = new byte[l];
106+
System.arraycopy(arr, off, r, 0, l);
107+
return r;
108+
}
109+
110+
@Test
111+
public void testDigestAcrossFill() throws Exception {
112+
Channel ch = new Channel();
113+
String s = "Whatever";
114+
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
115+
ch.current.putInt(bytes.length).put(slice(bytes, 0, 5));
116+
ch.newChunk();
117+
ch.current.put(slice(bytes, 5, bytes.length - 5));
118+
ch.newChunk();
119+
120+
MessageDigest checkDigest = MessageDigest.getInstance("SHA-1");
121+
ch.updateDigest(checkDigest);
122+
123+
BinarySource src = new BinarySource(null, ch);
124+
src.startDigest();
125+
String read = src.readString();
126+
assertEquals(s, read);
127+
byte[] digest = src.finishDigest();
128+
129+
byte[] toCheck = checkDigest.digest();
130+
assertArrayEquals(toCheck, digest);
131+
}
132+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2013, 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.graal.compiler.graph.test.graphio.parsing;
27+
28+
import static org.junit.Assert.assertTrue;
29+
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.net.URISyntaxException;
33+
import java.net.URL;
34+
import java.nio.channels.FileChannel;
35+
import java.nio.file.StandardOpenOption;
36+
37+
import jdk.graal.compiler.graphio.parsing.BinarySource;
38+
import jdk.graal.compiler.graphio.parsing.DataSource;
39+
40+
public class DataBinarySourceTest extends DataSourceTest {
41+
42+
@Override
43+
protected DataSource createDataSource(URL bigv) throws IOException {
44+
try {
45+
File f = new File(bigv.toURI());
46+
assertTrue("file exists: " + f, f.exists());
47+
FileChannel fch2 = FileChannel.open(f.toPath(), StandardOpenOption.READ);
48+
return new BinarySource(null, fch2);
49+
} catch (URISyntaxException ex) {
50+
throw new AssertionError("Canot convert " + bigv, ex);
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2013, 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.graal.compiler.graph.test.graphio.parsing;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertNotEquals;
30+
import static org.junit.Assert.assertNotNull;
31+
import static org.junit.Assert.assertTrue;
32+
import static org.junit.Assert.fail;
33+
34+
import java.io.BufferedInputStream;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.net.URL;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
41+
import org.junit.Test;
42+
43+
import jdk.graal.compiler.graphio.parsing.BinaryReader;
44+
import jdk.graal.compiler.graphio.parsing.DataSource;
45+
import jdk.graal.compiler.graphio.parsing.ModelBuilder;
46+
import jdk.graal.compiler.graphio.parsing.StreamSource;
47+
import jdk.graal.compiler.graphio.parsing.model.GraphDocument;
48+
import jdk.graal.compiler.graphio.parsing.model.InputGraph;
49+
50+
public class DataSourceTest {
51+
@Test
52+
public void readData30ViaDataSourceInputStream() throws Exception {
53+
URL bigv = DataSourceTest.class.getResource("bigv-3.0.bgv");
54+
assertNotNull("bigv-3.0.bgv found", bigv);
55+
GraphDocument checkDocument = new GraphDocument();
56+
List<String> titles = new ArrayList<>();
57+
ModelBuilder mb = new ModelBuilder(checkDocument, null) {
58+
@Override
59+
public InputGraph startGraph(int dumpId, String format, Object[] args) {
60+
titles.add(ModelBuilder.makeGraphName(dumpId, format, args));
61+
return super.startGraph(dumpId, format, args);
62+
}
63+
};
64+
DataSource scanSource = createDataSource(bigv);
65+
BinaryReader rdr = new BinaryReader(scanSource, mb);
66+
rdr.parse();
67+
assertEquals("Three graphs started", 3, titles.size());
68+
int prev = -1;
69+
for (String t : titles) {
70+
assertEquals("All % are gone", -1, t.indexOf("%"));
71+
int idColon = t.indexOf("id: ");
72+
assertNotEquals("id: found", -1, idColon);
73+
int cnt = Integer.parseInt(t.substring(idColon + 4));
74+
assertTrue("New counter (" + cnt + ") is bigger than " + prev, prev < cnt);
75+
prev = cnt;
76+
}
77+
if (prev < 5) {
78+
fail("Expecting at least 5 counted titles: " + prev);
79+
}
80+
}
81+
82+
protected DataSource createDataSource(URL bigv) throws IOException {
83+
final InputStream is = bigv.openStream();
84+
return new StreamSource(new BufferedInputStream(is));
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2013, 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.graal.compiler.graph.test.graphio.parsing;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertSame;
30+
31+
import java.io.File;
32+
import java.net.URI;
33+
34+
import org.junit.Test;
35+
36+
import jdk.graal.compiler.graphio.parsing.BinaryReader;
37+
import jdk.graal.compiler.graphio.parsing.LocationCache;
38+
import jdk.graal.compiler.graphio.parsing.LocationStackFrame;
39+
import jdk.graal.compiler.graphio.parsing.LocationStratum;
40+
41+
public class StackTraceTest {
42+
@Test
43+
public void testFormatWithFileName() {
44+
BinaryReader.Method m = LocationCache.createMethod("aMethod", "my.Test", null);
45+
LocationStackFrame st = LocationCache.createFrame(m, 56,
46+
LocationCache.fileLineStratum("test.java", 22), null);
47+
String trace = st.toString();
48+
assertEquals("my.Test.aMethod(test.java:22) [bci:56]", trace);
49+
}
50+
51+
@Test
52+
public void testStratumInterned() throws Exception {
53+
LocationStratum l1 = LocationCache.createStratum(new URI("u").toString(), new File(".").getAbsolutePath(), "l", 1, 2, 3);
54+
LocationStratum l2 = LocationCache.createStratum(new URI("u").toString(), new File(".").getAbsolutePath(), "l", 1, 2, 3);
55+
assertSame(l1, l2);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2013, 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.graal.compiler.graph.test.graphio.parsing.model;
27+
28+
import static org.junit.Assert.assertEquals;
29+
30+
import org.junit.Test;
31+
32+
import jdk.graal.compiler.graphio.parsing.model.ChangedEvent;
33+
34+
public class ChangedEventTest {
35+
@Test
36+
public void testBase() {
37+
38+
ChangedEvent<Integer> e = new ChangedEvent<>(5);
39+
final int[] fireCount = new int[1];
40+
41+
e.addListener(s -> {
42+
assertEquals(s.intValue(), 5);
43+
fireCount[0]++;
44+
});
45+
46+
e.fire();
47+
assertEquals(1, fireCount[0]);
48+
49+
e.fire();
50+
assertEquals(2, fireCount[0]);
51+
52+
e.beginAtomic();
53+
54+
e.fire();
55+
assertEquals(2, fireCount[0]);
56+
57+
e.fire();
58+
assertEquals(2, fireCount[0]);
59+
60+
e.fire();
61+
assertEquals(2, fireCount[0]);
62+
63+
e.endAtomic();
64+
assertEquals(3, fireCount[0]);
65+
}
66+
}

0 commit comments

Comments
 (0)