From 5d9a7ba0dc9c24ed4e4efa9cac0e796fd524b308 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 30 May 2014 19:00:03 -0700 Subject: [PATCH] Avoid a system call in 'gettid'. System calls can be pretty slow. This is mako, which has one of our lowest latencies: iterations ns/op BM_unistd_getpid 10000000 209 BM_unistd_gettid 200000000 8 Bug: 15297299 (kernel panic from too many gettid calls) Bug: 15315766 (excessive gettid overhead in liblogd) Change-Id: I49656c0fc5b5d092390264a59e4f2c0d8a8b1aeb --- benchmarks/unistd_benchmark.cpp | 11 ++++++++++ libc/Android.mk | 1 + libc/SYSCALLS.TXT | 1 - libc/arch-arm/syscalls/gettid.S | 14 ------------ libc/arch-arm64/syscalls/gettid.S | 25 --------------------- libc/arch-mips/syscalls/gettid.S | 19 ---------------- libc/arch-mips64/syscalls/gettid.S | 25 --------------------- libc/arch-x86/syscalls/gettid.S | 17 --------------- libc/arch-x86_64/syscalls/gettid.S | 16 -------------- libc/bionic/gettid.cpp | 35 ++++++++++++++++++++++++++++++ 10 files changed, 47 insertions(+), 117 deletions(-) delete mode 100644 libc/arch-arm/syscalls/gettid.S delete mode 100644 libc/arch-arm64/syscalls/gettid.S delete mode 100644 libc/arch-mips/syscalls/gettid.S delete mode 100644 libc/arch-mips64/syscalls/gettid.S delete mode 100644 libc/arch-x86/syscalls/gettid.S delete mode 100644 libc/arch-x86_64/syscalls/gettid.S create mode 100644 libc/bionic/gettid.cpp diff --git a/benchmarks/unistd_benchmark.cpp b/benchmarks/unistd_benchmark.cpp index e839bf8b7..12b788acd 100644 --- a/benchmarks/unistd_benchmark.cpp +++ b/benchmarks/unistd_benchmark.cpp @@ -28,3 +28,14 @@ static void BM_unistd_getpid(int iters) { StopBenchmarkTiming(); } BENCHMARK(BM_unistd_getpid); + +static void BM_unistd_gettid(int iters) { + StartBenchmarkTiming(); + + for (int i = 0; i < iters; ++i) { + gettid(); + } + + StopBenchmarkTiming(); +} +BENCHMARK(BM_unistd_gettid); diff --git a/libc/Android.mk b/libc/Android.mk index c573e3053..4783bd386 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -128,6 +128,7 @@ libc_bionic_src_files := \ bionic/getauxval.cpp \ bionic/getcwd.cpp \ bionic/getpgrp.cpp \ + bionic/gettid.cpp \ bionic/inotify_init.cpp \ bionic/lchown.cpp \ bionic/lfs64_support.cpp \ diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT index 785d19158..584c1ae49 100644 --- a/libc/SYSCALLS.TXT +++ b/libc/SYSCALLS.TXT @@ -38,7 +38,6 @@ uid_t getresuid:getresuid32(uid_t* ruid, uid_t* euid, uid_t* suid) arm,x86 uid_t getresuid:getresuid(uid_t* ruid, uid_t* euid, uid_t* suid) arm64,mips,mips64,x86_64 gid_t getresgid:getresgid32(gid_t* rgid, gid_t* egid, gid_t* sgid) arm,x86 gid_t getresgid:getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid) arm64,mips,mips64,x86_64 -pid_t gettid() all ssize_t readahead(int, off64_t, size_t) all int getgroups:getgroups32(int, gid_t*) arm,x86 int getgroups:getgroups(int, gid_t*) arm64,mips,mips64,x86_64 diff --git a/libc/arch-arm/syscalls/gettid.S b/libc/arch-arm/syscalls/gettid.S deleted file mode 100644 index 3928be122..000000000 --- a/libc/arch-arm/syscalls/gettid.S +++ /dev/null @@ -1,14 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(gettid) - mov ip, r7 - ldr r7, =__NR_gettid - swi #0 - mov r7, ip - cmn r0, #(MAX_ERRNO + 1) - bxls lr - neg r0, r0 - b __set_errno -END(gettid) diff --git a/libc/arch-arm64/syscalls/gettid.S b/libc/arch-arm64/syscalls/gettid.S deleted file mode 100644 index 44ca9132e..000000000 --- a/libc/arch-arm64/syscalls/gettid.S +++ /dev/null @@ -1,25 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(gettid) - stp x29, x30, [sp, #-16]! - .cfi_def_cfa_offset 16 - .cfi_rel_offset x29, 0 - .cfi_rel_offset x30, 8 - mov x29, sp - - mov x8, __NR_gettid - svc #0 - - ldp x29, x30, [sp], #16 - .cfi_def_cfa_offset 0 - .cfi_restore x29 - .cfi_restore x30 - - cmn x0, #(MAX_ERRNO + 1) - cneg x0, x0, hi - b.hi __set_errno - - ret -END(gettid) diff --git a/libc/arch-mips/syscalls/gettid.S b/libc/arch-mips/syscalls/gettid.S deleted file mode 100644 index cfdc57b8d..000000000 --- a/libc/arch-mips/syscalls/gettid.S +++ /dev/null @@ -1,19 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(gettid) - .set noreorder - .cpload t9 - li v0, __NR_gettid - syscall - bnez a3, 1f - move a0, v0 - j ra - nop -1: - la t9,__set_errno - j t9 - nop - .set reorder -END(gettid) diff --git a/libc/arch-mips64/syscalls/gettid.S b/libc/arch-mips64/syscalls/gettid.S deleted file mode 100644 index 30c30c8c1..000000000 --- a/libc/arch-mips64/syscalls/gettid.S +++ /dev/null @@ -1,25 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(gettid) - .set push - .set noreorder - li v0, __NR_gettid - syscall - bnez a3, 1f - move a0, v0 - j ra - nop -1: - move t0, ra - bal 2f - nop -2: - .cpsetup ra, t1, 2b - LA t9,__set_errno - .cpreturn - j t9 - move ra, t0 - .set pop -END(gettid) diff --git a/libc/arch-x86/syscalls/gettid.S b/libc/arch-x86/syscalls/gettid.S deleted file mode 100644 index 1f264b138..000000000 --- a/libc/arch-x86/syscalls/gettid.S +++ /dev/null @@ -1,17 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(gettid) - movl $__NR_gettid, %eax - int $0x80 - cmpl $-MAX_ERRNO, %eax - jb 1f - negl %eax - pushl %eax - call __set_errno - addl $4, %esp - orl $-1, %eax -1: - ret -END(gettid) diff --git a/libc/arch-x86_64/syscalls/gettid.S b/libc/arch-x86_64/syscalls/gettid.S deleted file mode 100644 index 65ecc16bc..000000000 --- a/libc/arch-x86_64/syscalls/gettid.S +++ /dev/null @@ -1,16 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(gettid) - movl $__NR_gettid, %eax - syscall - cmpq $-MAX_ERRNO, %rax - jb 1f - negl %eax - movl %eax, %edi - call __set_errno - orq $-1, %rax -1: - ret -END(gettid) diff --git a/libc/bionic/gettid.cpp b/libc/bionic/gettid.cpp new file mode 100644 index 000000000..f42e36a98 --- /dev/null +++ b/libc/bionic/gettid.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include "pthread_internal.h" + +pid_t gettid() { + return __get_thread()->tid; +}