From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9426 Path: news.gmane.org!not-for-mail From: Nathan Zadoks Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] add sched_getcpu, with vDSO support Date: Tue, 1 Mar 2016 14:45:40 +0100 Message-ID: <1456839940-10330-1-git-send-email-nathan@nathan7.eu> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1456839976 3446 80.91.229.3 (1 Mar 2016 13:46:16 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Mar 2016 13:46:16 +0000 (UTC) Cc: Nathan Zadoks To: musl@lists.openwall.com Original-X-From: musl-return-9439-gllmg-musl=m.gmane.org@lists.openwall.com Tue Mar 01 14:46:07 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1aakck-0006WW-Ar for gllmg-musl@m.gmane.org; Tue, 01 Mar 2016 14:46:02 +0100 Original-Received: (qmail 32194 invoked by uid 550); 1 Mar 2016 13:46:00 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 32139 invoked from network); 1 Mar 2016 13:45:56 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nathan7.eu; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qaRGX4/NVjE7KjjHy3hjCXSkN1OMAfYmP5aVU/F0Vmo=; b=d+8IagbajIwAZ3lkgtD1mIMbqo0rkHvBKFwrmK7uvqaBZs9Hbm666TWcYHQMXUiVdq vJDVP4KnRztAqVdoE+jDFqTylN4oOPgEafOlxwMSELmTV01c/xfVXDeH/LufiC7pHIo5 Wara3HaZNeDxNW5dJdgCTmWHmViESXfYAkmOQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qaRGX4/NVjE7KjjHy3hjCXSkN1OMAfYmP5aVU/F0Vmo=; b=frR710e1fEwMPP9IGE6VtEduQwNcWrp3gjD4V7H+ym6cawc9eAIYhtFDBSnX9dESdc rAOapPCxAqSyhCquOqxrSDf11WGW7QkP7KyQV0ZZ3DQt9IBAhT0uC32IhA0hc1uDpkZY moMBMWCCA39KYozRCq5jbSL/TrqfEaI7S/BEKm10P44EFvFG1bwskwnWOIXP8KncnrGY 20bRPncVItbfMwwa9JMAmEbCWX29DLG7/p/al31n2b99GZl30eyRzPKnDqTlcr5NwCk7 PTX68jvT/1l9TUBk95tHb5gNOJo9ZxzHXe0zHA7v0xiROYIrNgqZeext5IXp9zs7cVKL CijA== X-Gm-Message-State: AD7BkJLVPF32t6pV0jYc6a9NjKl23Sa4fcM4idRxWcnsRx6y3lJh78rFVfGLUBS0FGuHtw== X-Received: by 10.28.148.16 with SMTP id w16mr3967500wmd.90.1456839944768; Tue, 01 Mar 2016 05:45:44 -0800 (PST) X-Mailer: git-send-email 2.7.1 In-Reply-To: Xref: news.gmane.org gmane.linux.lib.musl.general:9426 Archived-At: This is a GNU extension, but a fairly minor one, for a system call that otherwise has no libc wrapper. Adding it was discussed previously, without any objections: http://www.openwall.com/lists/musl/2015/05/08/24 --- I've done some cleanup and added vDSO support, since it seems to make a significant perf difference. (about 14ns/vDSO call, vs about 102ns/syscall) I've only added vDSO support for x86_64, since it doesn't exist on i386, and I don't see any vDSO handling at all on x32. The vDSO function is also available on PowerPC, but I don't have any machines for testing that on, so I've left that out too. --- arch/x86_64/syscall_arch.h | 2 ++ include/sched.h | 1 + src/sched/sched_getcpu.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/sched/sched_getcpu.c diff --git a/arch/x86_64/syscall_arch.h b/arch/x86_64/syscall_arch.h index a7a7b5a..54e05ff 100644 --- a/arch/x86_64/syscall_arch.h +++ b/arch/x86_64/syscall_arch.h @@ -64,3 +64,5 @@ static __inline long __syscall6(long n, long a1, long a2, long a3, long a4, long #define VDSO_USEFUL #define VDSO_CGT_SYM "__vdso_clock_gettime" #define VDSO_CGT_VER "LINUX_2.6" +#define VDSO_GETCPU_SYM "__vdso_getcpu" +#define VDSO_GETCPU_VER "LINUX_2.6" diff --git a/include/sched.h b/include/sched.h index 3e34a72..7e88f09 100644 --- a/include/sched.h +++ b/include/sched.h @@ -76,6 +76,7 @@ void free(void *); typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t; int __sched_cpucount(size_t, const cpu_set_t *); +int sched_getcpu(void); int sched_getaffinity(pid_t, size_t, cpu_set_t *); int sched_setaffinity(pid_t, size_t, const cpu_set_t *); diff --git a/src/sched/sched_getcpu.c b/src/sched/sched_getcpu.c new file mode 100644 index 0000000..ed5e331 --- /dev/null +++ b/src/sched/sched_getcpu.c @@ -0,0 +1,45 @@ +#define _GNU_SOURCE +#include +#include +#include +#include "syscall.h" +#include "atomic.h" + +#ifdef VDSO_GETCPU_SYM + +void *__vdsosym(const char *, const char *); + +static void *volatile vdso_func; + +typedef long (*getcpu_f)(unsigned *, unsigned *, void *); + +long getcpu_init(unsigned *cpu, unsigned *node, void *unused) +{ + void *p = __vdsosym(VDSO_GETCPU_VER, VDSO_GETCPU_SYM); + getcpu_f f = (getcpu_f)p; + a_cas_p(&vdso_func, (void *)getcpu_init, p); + return f ? f(cpu, node, unused) : -ENOSYS; +} + +static void *volatile vdso_func = (void *)getcpu_init; + +#endif + +int sched_getcpu(void) +{ + int r; + unsigned cpu; + +#ifdef VDSO_CGT_SYM + getcpu_f f = (getcpu_f)vdso_func; + if (f) { + r = f(&cpu, NULL, NULL); + if (!r) return cpu; + if (r != -ENOSYS) return __syscall_ret(r); + } +#endif + + r = __syscall(SYS_getcpu, &cpu, NULL, NULL); + if (!r) return cpu; + return __syscall_ret(r); +} -- 2.7.1