Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.os: add sysconf(), getDefaultPageSize() with smoke tests #16638

Closed
wants to merge 3 commits into from

Conversation

matu3ba
Copy link
Contributor

@matu3ba matu3ba commented Jul 31, 2023

sysconf() abstracts posix sysconf for comptime- and runtime probing.

getDefaultPageSize() reads the Kernel provided default page size,
which remains constant from program start to end.

Reproduce added abi bits with:
git clone --depth=1 https://github.com/DragonFlyBSD/DragonFlyBSD
git clone --depth=1 https://git.FreeBSD.org/src.git freebsd
git clone --depth=1 https://fuchsia.googlesource.com/fuchsia
git clone --depth=1 https://github.com/haiku/haiku
git clone --depth=1 https://github.com/Stichting-MINIX-Research-Foundation/minix/
git clone --depth=1 https://github.com/NetBSD/src netbsd
git clone --depth=1 https://github.com/openbsd/src openbsd
git clone --depth=1 https://github.com/kofemann/opensolaris
git clone --depth=1 https://github.com/apple-open-source-mirror/Libc
rg 'define.*_SC_PAGE_SIZE' -B 1 --vimgrep

Closes #11308.

@matu3ba
Copy link
Contributor Author

matu3ba commented Aug 1, 2023

Looking good: As expected, besides the c symbol includes tests fail with error: 'test.getPageSize smoke test' failed: expected 4096, found 100. I'll fix up things later today.

REVIEW QUESTION: Should Kernel abi things reference the official repo + tagged commit? Personally, I'd prefer linking to a git reference, if an official one exists.

List of TODOs:

  • better name
  • explain usage
  • add this info to the commit
  • windows NtQuerySystemInformation since its already used and looks relative risk free
  • add sysconf numbers for _SC_PAGESIZE as _SC.PAGESIZE
  • make an issue to clarify if and how allocation of page sizes the kernel supports should be supported
  • followup: prepare large tables api including comment on necessary windows capabilities

Common alias for _SC_PAGE_SIZE is _SC_PAGESIZE

#!/usr/bin/env sh
git clone --depth=1 https://github.com/DragonFlyBSD/DragonFlyBSD
git clone --depth=1 https://github.com/freebsd/freebsd-src
git clone --depth=1 https://fuchsia.googlesource.com/fuchsia
git clone --depth=1 https://github.com/haiku/haiku
git clone --depth=1 https://github.com/hermitcore/rusty-hermit
git clone --depth=1 https://github.com/Stichting-MINIX-Research-Foundation/minix/
git clone --depth=1 https://github.com/NetBSD/src netbsd
git clone --depth=1 https://github.com/openbsd/src openbsd
git clone --depth=1 https://github.com/kofemann/opensolaris
rg 'define.*_SC_PAGE_SIZE' -B 1 --vimgrep

# Cleaned up output
DragonFlyBSD/include/unistd.h:240:9:#define	_SC_PAGESIZE		47
DragonFlyBSD/include/unistd.h:305:24:#define	_SC_PAGE_SIZE		_SC_PAGESIZE
freebsd-src/include/unistd.h:213#define	_SC_PAGESIZE		47
freebsd-src/include/unistd.h:278#define	_SC_PAGE_SIZE		_SC_PAGESIZE
fuchsia/zircon/third_party/ulib/musl/include/unistd.h:281:9:#define _SC_PAGE_SIZE 30
fuchsia/zircon/third_party/ulib/musl/include/unistd.h:282:9:#define _SC_PAGESIZE 30 /* !! */
haiku/headers/posix/unistd.h:187:9:#define _SC_PAGE_SIZE			27
haiku/headers/posix/unistd.h:188:9:#define _SC_PAGESIZE			_SC_PAGE_SIZE
minix/sys/sys/unistd.h:248:9:#define	_SC_PAGESIZE		28
minix/sys/sys/unistd.h:249:24:#define	_SC_PAGE_SIZE		_SC_PAGESIZE	/* 1170 compatibility */
netbsd/sys/sys/unistd.h:257:#define	_SC_PAGESIZE		28
netbsd/sys/sys/unistd.h:258:#define	_SC_PAGE_SIZE		_SC_PAGESIZE	/* 1170 compatibility */
openbsd/include/unistd.h:180:#define	_SC_PAGESIZE		28
openbsd/include/unistd.h:180:#define	_SC_PAGE_SIZE		_SC_PAGESIZE	/* 1170 compatibility */
opensolaris/usr/src/ucbhead/unistd.h:84#define	_SC_PAGESIZE	11
opensolaris/usr/src/uts/common/sys/unistd.h:182:25:#define	_SC_PAGE_SIZE			_SC_PAGESIZE

Left out: Apple Libc. Hermit has no reliable page size and neither an api for it. See hermit-os/kernel#815.

lib/std/os.zig Outdated Show resolved Hide resolved
lib/std/os.zig Outdated Show resolved Hide resolved
@matu3ba matu3ba force-pushed the learn_page_size branch 2 times, most recently from 8c57e1c to 99fbe13 Compare August 2, 2023 21:28
@matu3ba matu3ba changed the title std.os: add getPageSize() with smoke tests std.os: add getDefaultPageSize() with smoke tests Aug 2, 2023
@matu3ba matu3ba marked this pull request as ready for review August 2, 2023 21:32
@matu3ba matu3ba marked this pull request as draft August 2, 2023 22:36
@matu3ba matu3ba changed the title std.os: add getDefaultPageSize() with smoke tests std.os: add sysconf(), getDefaultPageSize() with smoke tests Aug 20, 2023
@matu3ba matu3ba force-pushed the learn_page_size branch 2 times, most recently from 9881625 to 7eabb78 Compare August 21, 2023 18:43
@matu3ba

This comment was marked as resolved.

Jan Philipp Hafer added 3 commits August 23, 2023 01:38
sysconf() abstracts posix sysconf for comptime- and runtime probing.

getDefaultPageSize() reads the Kernel provided default page size,
which remains constant from program start to end.

Reproduce added abi bits with:
git clone --depth=1 https://github.com/DragonFlyBSD/DragonFlyBSD
git clone --depth=1 https://git.FreeBSD.org/src.git freebsd
git clone --depth=1 https://fuchsia.googlesource.com/fuchsia
git clone --depth=1 https://github.com/haiku/haiku
git clone --depth=1 https://github.com/Stichting-MINIX-Research-Foundation/minix/
git clone --depth=1 https://github.com/NetBSD/src netbsd
git clone --depth=1 https://github.com/openbsd/src openbsd
git clone --depth=1 https://github.com/kofemann/opensolaris
git clone --depth=1 https://github.com/apple-open-source-mirror/Libc
rg 'define.*_SC_PAGE_SIZE' -B 1 --vimgrep

Closes ziglang#11308.
@matu3ba matu3ba marked this pull request as ready for review August 23, 2023 00:29
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this. A few requests-

@@ -233,6 +233,9 @@ pub const mach_header_64 = macho.mach_header_64;
pub const mach_header = macho.mach_header;

pub const _errno = __error;
pub const _SC = struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No underscore, please.

Comment on lines +4340 to +4342
/// Returns page size assigned by the Kernel to the process. This size is
/// constant during process lifetime, but other page sizes might be usable.
pub fn getDefaultPageSize() error{UnknownPageSize}!usize {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not belong in std.os (reminder: #5019)

I think it should go into std.heap.pageSize.

}
return sbi.PageSize;
},
// zig fmt: off
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to turn off formatting

@@ -1,4 +1,4 @@
const std = @import("../std.zig");
const std = @import("std");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be left alone

test "getDefaultPageSize smoke test" {
const page_size = try os.getDefaultPageSize();
switch (page_size) {
// zig fmt: off
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not turn off code formatting

@andrewrk andrewrk closed this Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add an API to the standard library to learn the system page size
5 participants