Skip to content

Commit

Permalink
add a getpid benchmark and have ProcessId::current use get_cached_pid…
Browse files Browse the repository at this point in the history
… on unix

Summary:
getpid() is an unconditional syscall on Linux/glibc. Therefore, it
costs about 250 nanoseconds. There are a couple places in EdenFS where
getpid() is an inner-ish loop so use folly::get_cached_pid() instead.

Reviewed By: kmancini

Differential Revision: D42044221

fbshipit-source-id: e9c8f8a00febeaf6c9a0a3481879040d739038f5
  • Loading branch information
chadaustin authored and facebook-github-bot committed Jul 14, 2023
1 parent 0ac3abb commit abddc1b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions eden/common/os/ProcessId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
#include <ostream>
#include <type_traits>

#include <folly/system/Pid.h>

#ifdef _WIN32
#include <windows.h> // @manual
// windows.h has to come first. Don't alphabetize, clang-format.
#include <processthreadsapi.h> // @manual
#else
#include <unistd.h> // @manual
#endif

namespace facebook::eden {
Expand Down Expand Up @@ -48,9 +48,14 @@ void ProcessId::assertValid() {

ProcessId ProcessId::current() noexcept {
#ifdef _WIN32
// On Windows, there's no need to cache GetCurrentProcessId(). No syscall is
// required: process ID is stored in the PEB which is reachable from the TEB.
return ProcessId{GetCurrentProcessId()};
#else
return ProcessId{static_cast<uint32_t>(getpid())};
// On Linux/glibc, getpid() is an unconditional syscall, and costs about 250
// nanoseconds. On macOS, it only costs a couple nanoseconds, but caching
// doesn't hurt.
return ProcessId{static_cast<uint32_t>(folly::get_cached_pid())};
#endif
}

Expand Down

0 comments on commit abddc1b

Please sign in to comment.