From cbdd7c9b4cf531e9d9830fa80ca8de13fd834a30 Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Wed, 14 Feb 2018 17:16:14 +0100 Subject: [PATCH] common: make ceph_clock_now() inlineable. The `ceph_clock_now()` is a widely spread but thin routine. All it does is wrap `clock_gettime` or `gettimeofday` with accompanying conversion to `utime_t`. Unfortunately, as it is defined outside of header, compilers are enforced to generate a full-blown function. The overhead is related not only the well visible stack smashing protection but also to enforcing callers to go through PLT each time. Taking into account the time getters are usually *user-space syscalls* (leveraging e.g. the VDSO mechanism), eradicating even small boilerplate might be beneficial. ``` 0000000000000000 : 0: 48 83 ec 28 sub $0x28,%rsp 4: 31 ff xor %edi,%edi 6: 48 89 e6 mov %rsp,%rsi 9: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 10: 00 00 12: 48 89 44 24 18 mov %rax,0x18(%rsp) 17: 31 c0 xor %eax,%eax 19: e8 00 00 00 00 callq 1e 1e: 8b 44 24 08 mov 0x8(%rsp),%eax 22: 48 c1 e0 20 shl $0x20,%rax 26: 48 89 c2 mov %rax,%rdx 29: 8b 04 24 mov (%rsp),%eax 2c: 48 09 d0 or %rdx,%rax 2f: 48 8b 4c 24 18 mov 0x18(%rsp),%rcx 34: 64 48 33 0c 25 28 00 xor %fs:0x28,%rcx 3b: 00 00 3d: 75 05 jne 44 3f: 48 83 c4 28 add $0x28,%rsp 43: c3 retq 44: e8 00 00 00 00 callq 49 ``` Signed-off-by: Radoslaw Zarzynski --- src/CMakeLists.txt | 1 - src/common/Clock.cc | 30 ------------------------------ src/common/Clock.h | 14 +++++++++++++- src/include/utime.h | 2 ++ 4 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 src/common/Clock.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9651c75041f8..1a99c9a85fce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -418,7 +418,6 @@ set(libcommon_files common/escape.cc common/url_escape.cc common/io_priority.cc - common/Clock.cc common/ceph_time.cc common/mempool.cc common/Throttle.cc diff --git a/src/common/Clock.cc b/src/common/Clock.cc deleted file mode 100644 index 9412facf3359..000000000000 --- a/src/common/Clock.cc +++ /dev/null @@ -1,30 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab -/* - * Ceph - scalable distributed file system - * - * Copyright (C) 2004-2006 Sage Weil - * - * This is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software - * Foundation. See file COPYING. - * - */ - - -#include "common/Clock.h" - -utime_t ceph_clock_now() -{ -#if defined(__linux__) - struct timespec tp; - clock_gettime(CLOCK_REALTIME, &tp); - utime_t n(tp); -#else - struct timeval tv; - gettimeofday(&tv, nullptr); - utime_t n(&tv); -#endif - return n; -} diff --git a/src/common/Clock.h b/src/common/Clock.h index 9f3238f436e7..b47954ad1ce7 100644 --- a/src/common/Clock.h +++ b/src/common/Clock.h @@ -19,6 +19,18 @@ #include -utime_t ceph_clock_now(); +static inline utime_t ceph_clock_now() +{ +#if defined(__linux__) + struct timespec tp; + clock_gettime(CLOCK_REALTIME, &tp); + utime_t n(tp); +#else + struct timeval tv; + gettimeofday(&tv, nullptr); + utime_t n(&tv); +#endif + return n; +} #endif diff --git a/src/include/utime.h b/src/include/utime.h index 63b468316166..05418ff3b1f4 100644 --- a/src/include/utime.h +++ b/src/include/utime.h @@ -62,6 +62,8 @@ public: } utime_t(const struct timespec v) { + // NOTE: this is used by ceph_clock_now() so should be kept + // as thin as possible. tv.tv_sec = v.tv_sec; tv.tv_nsec = v.tv_nsec; } -- 2.47.3