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

From: Denys Vlasenko
Date: Mon May 16 2011 - 11:43:17 EST


On Sat, May 14, 2011 at 1:24 AM, Andi Kleen <andi@xxxxxxxxxxxxxx> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/