Skip to content

Commit 762c351

Browse files
Andrew Leonardtstuefe
Andrew Leonard
authored andcommitted
8273092: Sort classlist in JDK image
Reviewed-by: aph Backport-of: 1996f649a3a30b7ac4b547a762417f807f5fa414
1 parent fe9b7c6 commit 762c351

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

make/GenerateLinkOptData.gmk

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# This code is free software; you can redistribute it and/or modify it
@@ -88,7 +88,10 @@ $(CLASSLIST_FILE): $(INTERIM_IMAGE_DIR)/bin/java$(EXECUTABLE_SUFFIX) $(CLASSLIST
8888
$(CAT) $(LINK_OPT_DIR)/stderr $(JLI_TRACE_FILE) ; \
8989
exit $$exitcode \
9090
)
91-
$(GREP) -v HelloClasslist $@.raw.2 > $@
91+
$(GREP) -v HelloClasslist $@.raw.2 > $@.raw.3
92+
$(FIXPATH) $(INTERIM_IMAGE_DIR)/bin/java \
93+
-cp $(SUPPORT_OUTPUTDIR)/classlist.jar \
94+
build.tools.classlist.SortClasslist $@.raw.3 > $@
9295

9396
# The jli trace is created by the same recipe as classlist. By declaring these
9497
# dependencies, make will correctly rebuild both jli trace and classlist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2021,2023 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+
/**
27+
* This application is meant to be run to create a classlist file representing
28+
* common use.
29+
*
30+
* The classlist is produced by adding -XX:DumpLoadedClassList=classlist
31+
*/
32+
package build.tools.classlist;
33+
34+
import java.io.FileInputStream;
35+
import java.io.FileNotFoundException;
36+
import java.util.ArrayList;
37+
import java.util.Collections;
38+
import java.util.regex.Pattern;
39+
import java.util.regex.Matcher;
40+
import java.util.Scanner;
41+
42+
/**
43+
* The classlist generated by build.tools.classlist.HelloClasslist
44+
* may have non-deterministic contents, affected by Java thread execution order.
45+
* SortClasslist sorts the file to make the JDK image's contents more deterministic.
46+
*/
47+
public class SortClasslist {
48+
public static void main(String args[]) throws FileNotFoundException {
49+
ArrayList<String> classes = new ArrayList<>();
50+
ArrayList<String> lambdas = new ArrayList<>();
51+
52+
FileInputStream fis = new FileInputStream(args[0]);
53+
Scanner scanner = new Scanner(fis);
54+
while (scanner.hasNextLine()) {
55+
String line = scanner.nextLine();
56+
if (line.startsWith("#")) {
57+
// Comments -- print them first without sorting. These appear only at the top
58+
// of the file.
59+
System.out.println(line);
60+
} else if (line.startsWith("@")) {
61+
// @lambda-form-invoker, @lambda-proxy, etc.
62+
lambdas.add(line);
63+
} else {
64+
// Class name line
65+
classes.add(line);
66+
}
67+
}
68+
69+
Collections.sort(classes);
70+
Collections.sort(lambdas);
71+
72+
for (String s : classes) {
73+
System.out.println(s);
74+
}
75+
for (String s : lambdas) {
76+
System.out.println(s);
77+
}
78+
}
79+
}

make/scripts/compare.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ compare_general_files() {
357357
"
358358
$CAT $OTHER_DIR/$f | eval "$SVG_FILTER" > $OTHER_FILE
359359
$CAT $THIS_DIR/$f | eval "$SVG_FILTER" > $THIS_FILE
360-
elif [[ "$f" = *"/lib/classlist" ]] || [ "$SUFFIX" = "jar_contents" ]; then
361-
# The classlist files may have some lines in random order
360+
elif [ "$SUFFIX" = "jar_contents" ]; then
361+
# The jar_contents files may have some lines in random order
362362
OTHER_FILE=$WORK_DIR/$f.other
363363
THIS_FILE=$WORK_DIR/$f.this
364364
$MKDIR -p $(dirname $OTHER_FILE) $(dirname $THIS_FILE)

0 commit comments

Comments
 (0)