This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 4/5] Add a sysconf syscall


On Sat, May 14, 2011 at 1:24 AM, Andi Kleen <andi@firstfloor.org> wrote:
> +/*
> + * POSIX sysconf subset. Some programs need this in relatively fast paths
> + * and /proc is too slow for them.
> + *
> + * Note this is only a subset of the values supported by POSIX.
> + * We assume the C library handles the others.
> + */
> +SYSCALL_DEFINE1(sysconf, int, name)
> +{
> + ? ? ? switch (name) {
> + ? ? ? case _SC_ARG_MAX:
> + ? ? ? ? ? ? ? return rlimit_or(RLIMIT_STACK, ARG_MAX*ARG_MAX_FACTOR) /
> + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ARG_MAX_FACTOR;
> +
> + ? ? ? case _SC_CHILD_MAX:
> + ? ? ? ? ? ? ? return rlimit_or(RLIMIT_NPROC, max_threads);
> +
> + ? ? ? case _SC_CLK_TCK:
> + ? ? ? ? ? ? ? return HZ;
> +
> + ? ? ? case _SC_SEM_NSEMS_MAX:
> + ? ? ? ? ? ? ? return current->nsproxy->ipc_ns->sem_ctls[1];
> +
> + ? ? ? case _SC_OPEN_MAX:
> + ? ? ? ? ? ? ? return rlimit_or(RLIMIT_NOFILE, sysctl_nr_open);
> +
> + ? ? ? case _SC_SIGQUEUE_MAX:
> + ? ? ? ? ? ? ? /* or fallback based on memory? */
> + ? ? ? ? ? ? ? return rlimit_or(RLIMIT_SIGPENDING, INT_MAX);
> +
> + ? ? ? case _SC_UIO_MAXIOV:
> + ? ? ? ? ? ? ? return UIO_MAXIOV;
> +
> + ? ? ? case _SC_PAGESIZE:
> + ? ? ? ? ? ? ? return PAGE_SIZE;
> +
> + ? ? ? case _SC_SYMLOOP_MAX:
> + ? ? ? ? ? ? ? return SYMLOOP_MAX;
> +
> + ? ? ? case _SC_PHYS_PAGES:
> + ? ? ? ? ? ? ? return totalram_pages;
> +
> + ? ? ? case _SC_AVPHYS_PAGES:
> + ? ? ? ? ? ? ? return nr_free_pages();
> +
> + ? ? ? case _SC_NPROCESSORS_CONF:
> + ? ? ? ? ? ? ? return num_possible_cpus();
> +
> + ? ? ? case _SC_NPROCESSORS_ONLN:
> + ? ? ? ? ? ? ? return num_online_cpus();
> +
> + ? ? ? case _SC_NGROUPS_MAX:
> + ? ? ? ? ? ? ? return NGROUPS_MAX;
> +
> + ? ? ? default:
> + ? ? ? ? ? ? ? return -EINVAL;
> + ? ? ? }
> +}

...and libc will start making many such calls in a row in order to retrieve
a dozen of such values.

It's rather inefficient to return just one word.
Try to return more data per call.

Pass a pointer to the result struct and its length.
Future-proof API, such as using generously wide data types,
passing "version of struct" input parameter to facilitate incompatible
future changes, etc.

Pass a bit flag variable which says what data you want to have:
all-zeros means "give me only the cheap stuff which needs no calculations",
such as NGROUPS_MAX.
If SYSCONF_NPROCESSORS_ONLN bit is set, also return num_online_cpus().
If SYSCONF_AVPHYS_PAGES bit is set, return nr_free_pages().
Etc.

Maybe it makes sense to pass bits in the struct rather than in a parameter,
to not get caught by "we ran out of bits in the word!" problem later.

-- 
vda


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]