From 318fe6941a95397d4e7243132319f3519f14c78a Mon Sep 17 00:00:00 2001 From: Peter Dillinger Date: Tue, 24 Aug 2021 17:45:01 -0700 Subject: [PATCH] Add port::GetProcessID() (#8693) Summary: Useful in some places for object uniqueness across processes. Currently used for generating a host-wide identifier of Cache objects but expected to be used soon in some unique id generation code. `int64_t` is chosen for return type because POSIX uses signed integer type, usually `int`, for `pid_t` and Windows uses `DWORD`, which is `uint32_t`. Future work: avoid copy-pasted declarations in port_*.h, perhaps with port_common.h always included from port.h Pull Request resolved: https://github.com/facebook/rocksdb/pull/8693 Test Plan: manual for now Reviewed By: ajkr, anand1976 Differential Revision: D30492876 Pulled By: pdillinger fbshipit-source-id: 39fc2788623cc9f4787866bdb67a4d183dde7eef --- db/internal_stats.cc | 4 +++- port/port_posix.cc | 2 ++ port/port_posix.h | 2 ++ port/win/port_win.cc | 2 ++ port/win/port_win.h | 2 ++ 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/db/internal_stats.cc b/db/internal_stats.cc index a91883975..abe4b6607 100644 --- a/db/internal_stats.cc +++ b/db/internal_stats.cc @@ -23,6 +23,7 @@ #include "cache/cache_entry_stats.h" #include "db/column_family.h" #include "db/db_impl/db_impl.h" +#include "port/port.h" #include "rocksdb/system_clock.h" #include "rocksdb/table.h" #include "table/block_based/cachable_entry.h" @@ -602,7 +603,8 @@ void InternalStats::CacheEntryRoleStats::BeginCollection( ++collection_count; role_map_ = CopyCacheDeleterRoleMap(); std::ostringstream str; - str << cache->Name() << "@" << static_cast(cache); + str << cache->Name() << "@" << static_cast(cache) << "#" + << port::GetProcessID(); cache_id = str.str(); cache_capacity = cache->GetCapacity(); } diff --git a/port/port_posix.cc b/port/port_posix.cc index 112984de2..57f19e33d 100644 --- a/port/port_posix.cc +++ b/port/port_posix.cc @@ -263,6 +263,8 @@ void SetCpuPriority(ThreadId id, CpuPriority priority) { #endif } +int64_t GetProcessID() { return getpid(); } + } // namespace port } // namespace ROCKSDB_NAMESPACE diff --git a/port/port_posix.h b/port/port_posix.h index 90f131e13..fa23f9c33 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -219,5 +219,7 @@ using ThreadId = pid_t; extern void SetCpuPriority(ThreadId id, CpuPriority priority); +int64_t GetProcessID(); + } // namespace port } // namespace ROCKSDB_NAMESPACE diff --git a/port/win/port_win.cc b/port/win/port_win.cc index bd7b55f42..b28a44895 100644 --- a/port/win/port_win.cc +++ b/port/win/port_win.cc @@ -277,6 +277,8 @@ void SetCpuPriority(ThreadId id, CpuPriority priority) { (void)priority; } +int64_t GetProcessID() { return GetCurrentProcessId(); } + } // namespace port } // namespace ROCKSDB_NAMESPACE diff --git a/port/win/port_win.h b/port/win/port_win.h index a6a6de278..7de9b61c5 100644 --- a/port/win/port_win.h +++ b/port/win/port_win.h @@ -347,6 +347,8 @@ using ThreadId = int; extern void SetCpuPriority(ThreadId id, CpuPriority priority); +int64_t GetProcessID(); + } // namespace port