From 2158006c9f6b9fc66658b0369a27724e6af72304 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Tue, 6 Oct 2026 11:52:50 +0200 Subject: [PATCH] Add focused POSIX operation benchmarks --- .../python/micro/posix-lseek.py | 71 ++++++++++++++ .../python/micro/posix-open-close.py | 69 +++++++++++++ .../python/micro/posix-operations.py | 97 +++++++++++++++++++ .../python/micro/posix-read.py | 76 +++++++++++++++ .../python/micro/posix-scandir.py | 70 +++++++++++++ .../python/micro/posix-stat.py | 73 ++++++++++++++ .../python/micro/posix-write.py | 76 +++++++++++++++ mx.graalpython/mx_graalpython.py | 8 ++ mx.graalpython/mx_graalpython_bench_param.py | 14 +++ 9 files changed, 554 insertions(+) create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-lseek.py create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-open-close.py create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-operations.py create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-read.py create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-scandir.py create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-stat.py create mode 100644 graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-write.py diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-lseek.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-lseek.py new file mode 100644 index 0000000000..5b22223da4 --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-lseek.py @@ -0,0 +1,71 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Reposition an open descriptor within a cached file.""" + +import os +import tempfile + +_DIRECTORY = None +_FD = None + + +def __setup__(num=40000): + global _DIRECTORY, _FD + _DIRECTORY = tempfile.TemporaryDirectory(prefix="graalpy-posix-lseek-") + path = os.path.join(_DIRECTORY.name, "data") + _FD = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) + os.ftruncate(_FD, 4 * 1024 * 1024) + + +def __benchmark__(num=40000): + total = 0 + for i in range(num): + total += os.lseek(_FD, (i & 1023) * 4096, os.SEEK_SET) + return total + + +def __teardown__(): + global _DIRECTORY, _FD + if _FD is not None: + os.close(_FD) + _FD = None + if _DIRECTORY is not None: + _DIRECTORY.cleanup() + _DIRECTORY = None diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-open-close.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-open-close.py new file mode 100644 index 0000000000..229e6b633b --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-open-close.py @@ -0,0 +1,69 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Open and close an existing cached file by pathname.""" + +import os +import tempfile + +_DIRECTORY = None +_PATH = None + + +def __setup__(num=20000): + global _DIRECTORY, _PATH + _DIRECTORY = tempfile.TemporaryDirectory(prefix="graalpy-posix-open-") + _PATH = os.path.join(_DIRECTORY.name, "data") + with open(_PATH, "wb") as stream: + stream.write(b"x") + + +def __benchmark__(num=20000): + for _ in range(num): + fd = os.open(_PATH, os.O_RDONLY) + os.close(fd) + return num + + +def __teardown__(): + global _DIRECTORY, _PATH + _PATH = None + if _DIRECTORY is not None: + _DIRECTORY.cleanup() + _DIRECTORY = None diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-operations.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-operations.py new file mode 100644 index 0000000000..a34b293f25 --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-operations.py @@ -0,0 +1,97 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Benchmark POSIX file and directory operations; also used for native-runtime PGO training.""" + +import os +import tempfile + + +def __benchmark__(num=2000): + # Repeated runtime calls make the native successors of PreInitPosixSupport's phase checks hot. + # Both path types also exercise the backend selection used by string conversion nodes. + data = b"posix-pgo" + with tempfile.TemporaryDirectory(prefix="graalpy-pgo-posix-") as directory: + filename = os.path.join(directory, "data") + with open(filename, "wb") as stream: + stream.write(data) + os.mkdir(os.path.join(directory, "subdir")) + paths = (filename, os.fsencode(filename)) + directories = (directory, os.fsencode(directory)) + statvfs = getattr(os, "statvfs", None) + fstatvfs = getattr(os, "fstatvfs", None) + for iteration in range(num): + path = paths[iteration % 2] + directory_path = directories[iteration % 2] + fd = os.open(path, os.O_RDWR) + try: + os.set_inheritable(fd, False) + os.lseek(fd, 0, os.SEEK_SET) + read_data = os.read(fd, len(data)) + assert read_data == data + os.lseek(fd, 0, os.SEEK_SET) + written = os.write(fd, data) + fd_stat = os.fstat(fd) + path_stat = os.stat(path) + link_stat = os.lstat(path) + assert written == fd_stat.st_size == path_stat.st_size == link_stat.st_size == len(data) + if statvfs is not None: + statvfs(path) + if fstatvfs is not None: + fstatvfs(fd) + finally: + os.close(fd) + + with os.scandir(directory_path) as entries: + for entry in entries: + assert entry.name + entry.stat() + entry.is_file() + entry.is_dir() + names = os.listdir(directory_path) + assert len(names) == 2 + if os.listdir in os.supports_fd: + directory_fd = os.open(directory_path, os.O_RDONLY) + try: + # listdir(fd) rewinds the directory stream before closing it. + names = os.listdir(directory_fd) + assert len(names) == 2 + finally: + os.close(directory_fd) + return num diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-read.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-read.py new file mode 100644 index 0000000000..48e2310db8 --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-read.py @@ -0,0 +1,76 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Read a cached file in 4 KiB chunks with an already-open descriptor.""" + +import os +import tempfile + +_DIRECTORY = None +_FD = None +_CHUNK_SIZE = 4096 + + +def __setup__(num=16384): + global _DIRECTORY, _FD + _DIRECTORY = tempfile.TemporaryDirectory(prefix="graalpy-posix-read-") + path = os.path.join(_DIRECTORY.name, "data") + _FD = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) + os.write(_FD, b"x" * (max(num, 1) * _CHUNK_SIZE)) + + +def __benchmark__(num=16384): + os.lseek(_FD, 0, os.SEEK_SET) + total = 0 + for _ in range(num): + data = os.read(_FD, _CHUNK_SIZE) + if len(data) != _CHUNK_SIZE: + raise AssertionError("unexpected end of file") + total += data[0] + return total + + +def __teardown__(): + global _DIRECTORY, _FD + if _FD is not None: + os.close(_FD) + _FD = None + if _DIRECTORY is not None: + _DIRECTORY.cleanup() + _DIRECTORY = None diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-scandir.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-scandir.py new file mode 100644 index 0000000000..0ac4027be0 --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-scandir.py @@ -0,0 +1,70 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Iterate directory entries and materialize their names.""" + +import os +import tempfile + +_DIRECTORY = None + + +def __setup__(num=1200): + global _DIRECTORY + _DIRECTORY = tempfile.TemporaryDirectory(prefix="graalpy-posix-scandir-") + for i in range(64): + path = os.path.join(_DIRECTORY.name, "file-%02d" % i) + with open(path, "wb"): + pass + + +def __benchmark__(num=1200): + total = 0 + for _ in range(num): + with os.scandir(_DIRECTORY.name) as entries: + for entry in entries: + total += len(entry.name) + return total + + +def __teardown__(): + global _DIRECTORY + if _DIRECTORY is not None: + _DIRECTORY.cleanup() + _DIRECTORY = None diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-stat.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-stat.py new file mode 100644 index 0000000000..7986b2e249 --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-stat.py @@ -0,0 +1,73 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Look up metadata for existing paths, as import and pathlib code commonly do.""" + +import os +import tempfile + +_DIRECTORY = None +_PATHS = None + + +def __setup__(num=40000): + global _DIRECTORY, _PATHS + _DIRECTORY = tempfile.TemporaryDirectory(prefix="graalpy-posix-stat-") + paths = [] + for i in range(16): + path = os.path.join(_DIRECTORY.name, "file-%02d" % i) + with open(path, "wb") as stream: + stream.write(b"x") + paths.append(path) + _PATHS = tuple(paths) + + +def __benchmark__(num=40000): + total = 0 + for i in range(num): + total += os.stat(_PATHS[i & 15]).st_size + return total + + +def __teardown__(): + global _DIRECTORY, _PATHS + _PATHS = None + if _DIRECTORY is not None: + _DIRECTORY.cleanup() + _DIRECTORY = None diff --git a/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-write.py b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-write.py new file mode 100644 index 0000000000..86fa17e8c1 --- /dev/null +++ b/graalpython/com.oracle.graal.python.benchmarks/python/micro/posix-write.py @@ -0,0 +1,76 @@ +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# The Universal Permissive License (UPL), Version 1.0 +# +# Subject to the condition set forth below, permission is hereby granted to any +# person obtaining a copy of this software, associated documentation and/or +# data (collectively the "Software"), free of charge and under any and all +# copyright rights in the Software, and any and all patent rights owned or +# freely licensable by each licensor hereunder covering either (i) the +# unmodified Software as contributed to or provided by such licensor, or (ii) +# the Larger Works (as defined below), to deal in both +# +# (a) the Software, and +# +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if +# one is included with the Software each a "Larger Work" to which the Software +# is contributed by such licensors), +# +# without restriction, including without limitation the rights to copy, create +# derivative works of, display, perform, and distribute the Software and make, +# use, sell, offer for sale, import, export, have made, and have sold the +# Software and the Larger Work(s), and to sublicense the foregoing rights on +# either these or other terms. +# +# This license is subject to the following condition: +# +# The above copyright notice and either this complete permission notice or at a +# minimum a reference to the UPL must be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Overwrite a cached file in 4 KiB chunks with an already-open descriptor.""" + +import os +import tempfile + +_DIRECTORY = None +_FD = None +_DATA = b"x" * 4096 + + +def __setup__(num=16384): + global _DIRECTORY, _FD + _DIRECTORY = tempfile.TemporaryDirectory(prefix="graalpy-posix-write-") + path = os.path.join(_DIRECTORY.name, "data") + _FD = os.open(path, os.O_CREAT | os.O_RDWR, 0o600) + os.ftruncate(_FD, max(num, 1) * len(_DATA)) + + +def __benchmark__(num=16384): + os.lseek(_FD, 0, os.SEEK_SET) + total = 0 + for _ in range(num): + written = os.write(_FD, _DATA) + if written != len(_DATA): + raise AssertionError("short write") + total += written + return total + + +def __teardown__(): + global _DIRECTORY, _FD + if _FD is not None: + os.close(_FD) + _FD = None + if _DIRECTORY is not None: + _DIRECTORY.cleanup() + _DIRECTORY = None diff --git a/mx.graalpython/mx_graalpython.py b/mx.graalpython/mx_graalpython.py index 79fa732f32..af7abee1e7 100644 --- a/mx.graalpython/mx_graalpython.py +++ b/mx.graalpython/mx_graalpython.py @@ -675,6 +675,14 @@ def graalpy_native_pgo_build_and_test(args=None): GRAALPY_HOME=instrumented_home, ): graalpytest(["--python", instrumented_launcher, "test_venv.py"]) + mx.log(mx.colorize("[PGO] Training native POSIX runtime paths", color="yellow")) + mx.command_function('benchmark')([ + "micro-small:posix-operations", "--", + "--python-vm", "graalpython", "--python-vm-config", "custom", + "--experimental-options", "--python.PosixModuleBackend=native", + # Keep the workload in the host interpreter so its AOT paths are profiled. + "--engine.Compilation=false", + ]) mx.command_function('benchmark')(["meso-small:*", "--", "--python-vm", "graalpython", "--python-vm-config", 'custom']) iprof_path = Path(SUITE.dir) / f'default.iprof' lcov_path = Path(SUITE.dir) / f'default.lcov' diff --git a/mx.graalpython/mx_graalpython_bench_param.py b/mx.graalpython/mx_graalpython_bench_param.py index 2ef13cce7d..76de9ca9d6 100644 --- a/mx.graalpython/mx_graalpython_bench_param.py +++ b/mx.graalpython/mx_graalpython_bench_param.py @@ -107,6 +107,13 @@ 'call-classmethod-sized': ITER_10 + ['500_000_000'], 'mmap-anonymous-sized': ITER_10 + ['20_000'], 'mmap-file': ITER_10 + ['1000'], + 'posix-operations': ITER_10 + WARMUP_2 + ['2000'], + 'posix-read': ITER_10 + WARMUP_2 + ['16384'], + 'posix-write': ITER_10 + WARMUP_2 + ['16384'], + 'posix-stat': ITER_10 + WARMUP_2 + ['40000'], + 'posix-open-close': ITER_10 + WARMUP_2 + ['20000'], + 'posix-lseek': ITER_10 + WARMUP_2 + ['40000'], + 'posix-scandir': ITER_10 + WARMUP_2 + ['1200'], 'generate-functions-sized': ITER_15 + ['500_000_000'], 'try-except-simple': ITER_10 + ['500_000_000'], 'try-except-store-simple': ITER_10 + ['500_000_000'], @@ -134,6 +141,13 @@ # For benchmarking the interpreter with --engine.Compilation=false MICRO_BENCHMARKS_SMALL = { + 'posix-operations': ITER_6 + WARMUP_2 + ['2000'], + 'posix-read': ITER_6 + WARMUP_2 + ['16384'], + 'posix-write': ITER_6 + WARMUP_2 + ['16384'], + 'posix-stat': ITER_6 + WARMUP_2 + ['40000'], + 'posix-open-close': ITER_6 + WARMUP_2 + ['20000'], + 'posix-lseek': ITER_6 + WARMUP_2 + ['40000'], + 'posix-scandir': ITER_6 + WARMUP_2 + ['1200'], 'nano-arith': ITER_6 + WARMUP_2, 'nano-loop': ITER_6 + WARMUP_2, 'nano-if': ITER_6 + WARMUP_2,