Skip to content

posix: implement sysconf() #56670

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

Closed
Tracked by #51211
cfriedt opened this issue Apr 7, 2023 · 1 comment · Fixed by #67500
Closed
Tracked by #51211

posix: implement sysconf() #56670

cfriedt opened this issue Apr 7, 2023 · 1 comment · Fixed by #67500
Labels
area: POSIX POSIX API Library Enhancement Changes/Updates/Additions to existing features

Comments

@cfriedt
Copy link
Member

cfriedt commented Apr 7, 2023

Is your enhancement proposal related to a problem? Please describe.
The sysconf() utility is actually quite fantastic to have available for any POSIX environment. For the most part, it is used to have a portable way to query standard system limits that do not change after initialization (including those that are compile-time constant).

Describe the solution you'd like
An implementaiton of sysconf(), possibly with a space-optimized version

This is one of those interesting cases where you could provide a "small, medium, and large" configuration, cc @stephanosio.

XS: O(1) time, O(1) space
Completed entirely via macros. Not POSIX-ly correct. Would be compile-time const only. Likely would not even compile if an invalid argument is provided, which would be kind of desireable. Would not work for dynamic queries - e.g. int n; sysconf(n).

// <unistd.h> or somewhere else more appropriate
// ...
#define __z_posix_sysconf_SC_CLK_TCK CONFIG_SYS_CLOCK_TICKS_PER_SEC
#define __z_posix_sysconf_SC_AIO_MAX (-1) // not supported (could be guarded by some Kconfig)
// ...
#define sysconf(x) __z_posix_sysconf ## x

// application
__ASSERT_NO_MSG(sysconf(_SC_CLK_TCK) == CONFIG_SYS_CLOCK_TICKS_PER_SEC);
__ASSERT_NO_MSG(sysconf(_SC_AIO_MAX) == -1);
__ASSERT_NO_MSG(sysconf(42) == 73); // nonsense - will trigger a compile error

Basically free and carefree. This should probably be the default.

S: O(n) time, O(n) space
Linear search through the n enabled options, where it's possible that n < k, where k is the minimum number of supported sysconf keys. Not POSIX-ly correct. Not able to distinguish between an invalid value and a value that is not supported.

M: O(1) time, O(m) space
LUT made of the m maximum sysconf options that Zephyr supports, where m >= k, the minimum number required to be POSIX-ly correct. This method is able to distinguish between invalid and unsupported arguments.

While this is constant is size, it is spatially unstable if the key-space is sparse. Specifically, that would be the case when the libc did not use a contiguous key space (starting at 0, ideally).

It would probably be desireable in this case to have a BUILD_ASSERT() that checks the size of the table (implies the key-space is contiguous).

To give an idea of the space requirements, there are currently 125 required sysconf keys. On 64-bit systems, a long is 8 bytes, so this would be 1000 bytes alone.

L: O(log(m)) time, O(m) space
A binary search through the m maximum sysconf options that Zephyr supports. Same definition as above for k. This method is able to distinguish between invalid and unsupported arguments. It supports a sparse set of keys.

The bsearch() standard library call can be used here. If keys cannot be sorted at build time in a compile-time const way, then there can be a SYS_INIT() call that reorders them in RAM, at the expense of RAM.

Describe alternatives you've considered
Not implementing the sysconf POSIX feature.

Additional context
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
https://man7.org/linux/man-pages/man3/sysconf.3.html

@cfriedt cfriedt added Enhancement Changes/Updates/Additions to existing features area: POSIX POSIX API Library labels Apr 7, 2023
@cfriedt cfriedt added this to the v3.4.0 milestone Apr 7, 2023
@cfriedt cfriedt self-assigned this Apr 7, 2023
@cfriedt
Copy link
Member Author

cfriedt commented Apr 8, 2023

There are many reasons to use sysconf() in POSIX applications. In particular, simply supporting compilation of 3rd party portable libraries.

However, even several internal POSIX function implementations could use it. E.g. the times() function, requested in #51978.

@cfriedt cfriedt modified the milestones: v3.4.0, v3.5.0 Jun 1, 2023
@cfriedt cfriedt removed their assignment Jul 4, 2023
@cfriedt cfriedt removed this from the v3.5.0 milestone Oct 13, 2023
@cfriedt cfriedt changed the title posix: create an implementation for sysconf posix: implement sysconf() Dec 22, 2023
awojasinski added a commit to awojasinski/zephyr that referenced this issue Jan 30, 2024
The patch introduces basic implementation of sysconf() function.
It's based on macro - that means that every function call is resolved
at compile time - and is not fully complient with POSIX standard
(the errno value is not handled and passing invalid name argument
results in compilation error). Treat this commit as a starting point
for proper sysconf() implementation. The one introduced in the patch
could stay as a defult implementation.

sysconf() documentation:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
https://man7.org/linux/man-pages/man3/sysconf.3.html

Fixes zephyrproject-rtos#56670

Signed-off-by: Adam Wojasinski <[email protected]>
carlescufi pushed a commit that referenced this issue Jan 30, 2024
The patch introduces basic implementation of sysconf() function.
It's based on macro - that means that every function call is resolved
at compile time - and is not fully complient with POSIX standard
(the errno value is not handled and passing invalid name argument
results in compilation error). Treat this commit as a starting point
for proper sysconf() implementation. The one introduced in the patch
could stay as a defult implementation.

sysconf() documentation:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
https://man7.org/linux/man-pages/man3/sysconf.3.html

Fixes #56670

Signed-off-by: Adam Wojasinski <[email protected]>
pdgendt pushed a commit to pdgendt/zephyr that referenced this issue Jan 30, 2024
The patch introduces basic implementation of sysconf() function.
It's based on macro - that means that every function call is resolved
at compile time - and is not fully complient with POSIX standard
(the errno value is not handled and passing invalid name argument
results in compilation error). Treat this commit as a starting point
for proper sysconf() implementation. The one introduced in the patch
could stay as a defult implementation.

sysconf() documentation:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
https://man7.org/linux/man-pages/man3/sysconf.3.html

Fixes zephyrproject-rtos#56670

Signed-off-by: Adam Wojasinski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: POSIX POSIX API Library Enhancement Changes/Updates/Additions to existing features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant