Skip to content

Commit 3750ce3

Browse files
committed
[GR-54128] Re-import libsvm_container code and add change detection annotations
PullRequest: graal/18000
2 parents 2f1bffd + f912c53 commit 3750ce3

File tree

30 files changed

+2055
-1019
lines changed

30 files changed

+2055
-1019
lines changed

substratevm/ci/ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117

118118
// START MAIN BUILD DEFINITION
119119
local task_dict = {
120-
"style-fullbuild": mxgate("fullbuild,style,nativeimagehelp") + eclipse + jdt + maven + mx_build_exploded + gdb("10.2") + platform_spec(no_jobs) + platform_spec({
120+
"style-fullbuild": mxgate("fullbuild,style,nativeimagehelp,check_libcontainer_annotations") + eclipse + jdt + maven + mx_build_exploded + gdb("10.2") + platform_spec(no_jobs) + platform_spec({
121121
// We could run the style gate on JDK 22 as well, and use old JDKs for running tools like StopBugs etc.,
122122
// but since we support JDK 21 anyways, there is not good reason to do so.
123123
"linux:amd64:jdk21": gate + t("30:00"),

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#
2525

2626
import os
27+
import pathlib
2728
import re
29+
import shutil
2830
import tempfile
2931
from glob import glob
3032
from contextlib import contextmanager
@@ -53,8 +55,6 @@
5355

5456
import sys
5557

56-
57-
5858
suite = mx.suite('substratevm')
5959
svmSuites = [suite]
6060

@@ -206,6 +206,7 @@ def __getattr__(self, name):
206206
'hellomodule',
207207
'condconfig',
208208
'truffle_unittests',
209+
'check_libcontainer_annotations'
209210
])
210211

211212
def vm_native_image_path(config=None):
@@ -449,6 +450,10 @@ def help_stdout_check(output):
449450

450451
mx.log('mx native-image --help output check detected no errors.')
451452

453+
with Task('Check ContainerLibrary annotations', tasks, tags=[GraalTags.check_libcontainer_annotations]) as t:
454+
if t:
455+
mx.command_function("check-libcontainer-annotations")([])
456+
452457
with Task('module build demo', tasks, tags=[GraalTags.hellomodule]) as t:
453458
if t:
454459
hellomodule(args.extra_image_builder_arguments)
@@ -2213,3 +2218,77 @@ def javac_image(args):
22132218
def musl_helloworld(args, config=None):
22142219
final_args = ['--static', '--libc=musl'] + args
22152220
run_helloworld_command(final_args, config, 'muslhelloworld')
2221+
2222+
2223+
def _get_libcontainer_files():
2224+
paths = []
2225+
libcontainer_project = mx.project("com.oracle.svm.native.libcontainer")
2226+
libcontainer_dir = libcontainer_project.dir
2227+
for src_dir in libcontainer_project.source_dirs():
2228+
for path, _, files in os.walk(src_dir):
2229+
for name in files:
2230+
abs_path = pathlib.PurePath(path, name)
2231+
rel_path = abs_path.relative_to(libcontainer_dir)
2232+
src_svm = pathlib.PurePath("src", "svm")
2233+
if src_svm in rel_path.parents:
2234+
# replace "svm" with "hotspot"
2235+
stripped_path = rel_path.relative_to(src_svm)
2236+
if not stripped_path.as_posix().startswith("svm_container"):
2237+
hotspot_path = pathlib.PurePath("src", "hotspot") / stripped_path
2238+
paths.append(hotspot_path.as_posix())
2239+
else:
2240+
paths.append(rel_path.as_posix())
2241+
return libcontainer_dir, paths
2242+
2243+
2244+
@mx.command(suite, 'check-libcontainer-annotations')
2245+
def check_libcontainer_annotations(args):
2246+
"""Verifies that files from libcontainer that are copied from hotspot have a @BasedOnJDKFile annotation in ContainerLibrary."""
2247+
2248+
# collect paths to check
2249+
2250+
libcontainer_dir, paths = _get_libcontainer_files()
2251+
2252+
java_project = mx.project("com.oracle.svm.core")
2253+
container_library = pathlib.Path(java_project.dir, "src/com/oracle/svm/core/container/ContainerLibrary.java")
2254+
with open(container_library, "r") as fp:
2255+
annotation_lines = [x for x in fp.readlines() if "@BasedOnJDKFile" in x]
2256+
2257+
# check all files are in an annotation
2258+
for f in paths:
2259+
if not any((a for a in annotation_lines if f in a)):
2260+
mx.abort(f"file {f} not found in any annotation in {container_library}")
2261+
2262+
# check all annotations refer to a file
2263+
for a in annotation_lines:
2264+
if not any((f for f in paths if f in a)):
2265+
mx.abort(f"annotation {a} does not match any files in {libcontainer_dir}")
2266+
2267+
2268+
reimport_libcontainer_files_cmd = "reimport-libcontainer-files"
2269+
2270+
2271+
@mx.command(suite, reimport_libcontainer_files_cmd)
2272+
def reimport_libcontainer_files(args):
2273+
parser = ArgumentParser(prog=f"mx {reimport_libcontainer_files_cmd}")
2274+
parser.add_argument("--jdk-repo", required=True, help="Path to the OpenJDK repo to import the files from.")
2275+
parsed_args = parser.parse_args(args)
2276+
2277+
libcontainer_dir, paths = _get_libcontainer_files()
2278+
2279+
libcontainer_path = pathlib.Path(libcontainer_dir)
2280+
jdk_path = pathlib.Path(parsed_args.jdk_repo)
2281+
2282+
missing = []
2283+
2284+
for path in paths:
2285+
jdk_file = jdk_path / path
2286+
svm_file = libcontainer_path / path
2287+
if jdk_file.is_file():
2288+
if mx.ask_yes_no(f"Should I update {path}"):
2289+
shutil.copyfile(jdk_file, svm_file)
2290+
else:
2291+
missing.append(jdk_file)
2292+
mx.warn(f"File not found: {jdk_file}")
2293+
if mx.ask_yes_no(f"Should I delete {path}"):
2294+
svm_file.unlink()

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/container/ContainerLibrary.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,62 @@
3333
import org.graalvm.word.UnsignedWord;
3434

3535
import com.oracle.svm.core.SubstrateOptions;
36+
import com.oracle.svm.core.util.BasedOnJDKFile;
3637

38+
/**
39+
* Provides Java-level access to the native {@code libsvm_container} implementation.
40+
*
41+
* The native code is base on the container implementation in the JDK. The {@link BasedOnJDKFile}
42+
* annotations below allow us to track upstream changes. Note that the referenced revisions/tags do
43+
* not necessarily denote the date when the file was last imported (although often that is the
44+
* case), but rather the last time upstream changes where reviewed. If there are changes that are
45+
* irrelevant for SVM, we might omit updating our copies. That said, full updates are done
46+
* regularly. See also the README file in
47+
* {@code substratevm/src/com.oracle.svm.native.libcontainer/README.md}.
48+
*/
3749
@CContext(ContainerLibraryDirectives.class)
3850
@CLibrary(value = "svm_container", requireStatic = true, dependsOn = "m")
51+
// The following annotations are for files in `src/hotspot`, which are copied from the JDK
52+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/java.base/share/native/include/jni.h")
53+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/java.base/unix/native/include/jni_md.h")
54+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupSubsystem_linux.cpp")
55+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupSubsystem_linux.hpp")
56+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupUtil_linux.cpp")
57+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupUtil_linux.hpp")
58+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp")
59+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp")
60+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp")
61+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp")
62+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/osContainer_linux.cpp")
63+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/osContainer_linux.hpp")
64+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/os_linux.cpp")
65+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/os_linux.hpp")
66+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/linux/os_linux.inline.hpp")
67+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/posix/include/jvm_md.h")
68+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/posix/os_posix.cpp")
69+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/posix/os_posix.hpp")
70+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/os/posix/os_posix.inline.hpp")
71+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/memory/allocation.hpp")
72+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/memory/allocation.inline.hpp")
73+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/memory/allStatic.hpp")
74+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/nmt/memflags.hpp")
75+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/runtime/os.cpp")
76+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/runtime/os.hpp")
77+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/runtime/os.inline.hpp")
78+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/checkedCast.hpp")
79+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/compilerWarnings_gcc.hpp")
80+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/compilerWarnings.hpp")
81+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/globalDefinitions_gcc.hpp")
82+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/globalDefinitions.hpp")
83+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/macros.hpp")
84+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/ostream.cpp")
85+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/ostream.hpp")
86+
// The following annotations are for files in `src/svm`, which are completely customized for SVM
87+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/logging/log.hpp")
88+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/memory/allocation.cpp")
89+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/runtime/globals.hpp")
90+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/debug.cpp")
91+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+5/src/hotspot/share/utilities/debug.hpp")
3992
class ContainerLibrary {
4093
static final int VERSION = 240100;
4194

substratevm/src/com.oracle.svm.native.libcontainer/README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Native cgroup support for SVM
22

33
This contains `libsvm_container`, the native cgroup support for SVM (libsvm_container).
4-
The C code is ported from the OpenJDK and currently based on:
5-
https://github.com/openjdk/jdk/tree/9049402a1b9394095b04287eef1f2d46c4da60e9/src/hotspot
4+
The C code is ported from the OpenJDK and update regularly (see "Updating" below).
65

76
## Building
87

@@ -28,12 +27,25 @@ custom. They only provide the minimal required functionality and are specific to
2827

2928
## Updating
3029

31-
While the code in here is completely independent and does not need to be in sync with the OpenJDK,
32-
it should be updated regularly to profit from upstream fixes and improvements. To do so, replace
33-
the files in [`src/hotspot`](./src/hotspot) with those from the OpenJDK. Then reapply all the
34-
changes (`#ifdef` guards) using the diff tool of your choice. Finally, adopt the files in
35-
[`src/svm`](./src/svm) to provide new functionality, if needed. Don't forget to update the import
36-
revision mention in this file.
30+
While the code in `libsvm_container` is completely independent and does not need to be in sync with
31+
the OpenJDK, it should be updated regularly to profit from upstream fixes and improvements. To keep
32+
track of this, `ContainerLibrary.java` contains `@BasedOnJDKFile` annotations for each imported file,
33+
which links to the source version in the JDK. With this information, all upstream changes can be
34+
detected. Note that strictly speaking, the referenced version in the annotation does not necessarily
35+
mean that the file was imported from that revision. Rather that all changes have been reviewed. If
36+
there are changes that are irrelevant for `libsvm_container`, we might keep the file as is and still
37+
bump the version. That said, we plan to do full reimports regularly, at least once every for every
38+
release.
39+
40+
To help keeping the `@BasedOnJDKFile` annotations up to date, the
41+
`mx gate --tags check_libcontainer_annotations` command ensures that the actual files and
42+
annotations are in sync.
43+
44+
To do a full reimport, replace the files in [`src/hotspot`](./src/hotspot) with those from the OpenJDK.
45+
The `mx reimport-libcontainer-files --jdk-repo path/to/jdk` can help with that. Then reapply all the
46+
changes (`#ifdef` guards) using the diff tool of your choice. Then, adopt the files in
47+
[`src/svm`](./src/svm) to provide new functionality, if needed. Finally, update the `@BasedOnJDKFile`
48+
annotations in `ContainerLibrary.java` to reflect the import revision.
3749

3850
## Local Testing
3951

0 commit comments

Comments
 (0)