Bug 1721110 - baseprofiler::profiler_current_process_id() now returns BaseProfilerProcessId - r=florian
authorGerald Squelart <gsquelart@mozilla.com>
Wed, 21 Jul 2021 04:16:58 +0000
changeset 586271 2ac1a3d1198e28955e5a24dc09e9d80172730d7b
parent 586270 b5ffc719210c96d2477240b20cb225892a85df18
child 586272 5a2c9d6565f9b70c2d8835048c381b83e3935368
push id38629
push userarchaeopteryx@coole-files.de
push dateWed, 21 Jul 2021 09:23:53 +0000
treeherdermozilla-central@d716918916ac [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian
bugs1721110
milestone92.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1721110 - baseprofiler::profiler_current_process_id() now returns BaseProfilerProcessId - r=florian Differential Revision: https://phabricator.services.mozilla.com/D120222
mozglue/baseprofiler/core/ProfiledThreadData.cpp
mozglue/baseprofiler/core/platform-linux-android.cpp
mozglue/baseprofiler/core/platform-macos.cpp
mozglue/baseprofiler/core/platform-win32.cpp
mozglue/baseprofiler/core/platform.cpp
mozglue/baseprofiler/core/platform.h
mozglue/baseprofiler/core/shared-libraries-linux.cc
mozglue/baseprofiler/lul/LulMain.cpp
mozglue/baseprofiler/public/BaseProfilerUtils.h
mozglue/tests/TestBaseProfiler.cpp
--- a/mozglue/baseprofiler/core/ProfiledThreadData.cpp
+++ b/mozglue/baseprofiler/core/ProfiledThreadData.cpp
@@ -169,18 +169,18 @@ int StreamSamplesAndMarkers(
     {
       aBuffer.StreamMarkersToJSON(aWriter, aThreadId, aProcessStartTime,
                                   aSinceTime, aUniqueStacks);
     }
     aWriter.EndArray();
   }
   aWriter.EndObject();
 
-  aWriter.IntProperty("pid",
-                      static_cast<int64_t>(profiler_current_process_id()));
+  aWriter.IntProperty(
+      "pid", static_cast<int64_t>(profiler_current_process_id().ToNumber()));
   aWriter.IntProperty(
       "tid",
       static_cast<int64_t>(aThreadId != 0 ? aThreadId : processedThreadId));
 
   return processedThreadId;
 }
 
 }  // namespace baseprofiler
--- a/mozglue/baseprofiler/core/platform-linux-android.cpp
+++ b/mozglue/baseprofiler/core/platform-linux-android.cpp
@@ -69,17 +69,19 @@
 #include <string.h>
 #include <list>
 
 using namespace mozilla;
 
 namespace mozilla {
 namespace baseprofiler {
 
-int profiler_current_process_id() { return getpid(); }
+BaseProfilerProcessId profiler_current_process_id() {
+  return BaseProfilerProcessId::FromNumber(getpid());
+}
 
 int profiler_current_thread_id() {
 #if defined(GP_OS_linux)
   // glibc doesn't provide a wrapper for gettid() until 2.30
   return static_cast<int>(static_cast<pid_t>(syscall(SYS_gettid)));
 #elif defined(GP_OS_android)
   return gettid();
 #elif defined(GP_OS_freebsd)
@@ -327,17 +329,17 @@ void Sampler::SuspendAndSampleAndResumeT
   // Suspend the samplee thread and get its context.
 
   SigHandlerCoordinator coord;  // on sampler thread's stack
   sSigHandlerCoordinator = &coord;
 
   // Send message 1 to the samplee (the thread to be sampled), by
   // signalling at it.
   // This could fail if the thread doesn't exist anymore.
-  int r = tgkill(mMyPid, sampleeTid, SIGPROF);
+  int r = tgkill(mMyPid.ToNumber(), sampleeTid, SIGPROF);
   if (r == 0) {
     // Wait for message 2 from the samplee, indicating that the context
     // is available and that the thread is suspended.
     while (true) {
       r = sem_wait(&sSigHandlerCoordinator->mMessage2);
       if (r == -1 && errno == EINTR) {
         // Interrupted by a signal.  Try again.
         continue;
--- a/mozglue/baseprofiler/core/platform-macos.cpp
+++ b/mozglue/baseprofiler/core/platform-macos.cpp
@@ -31,17 +31,19 @@
 #include <errno.h>
 #include <math.h>
 
 // this port is based off of v8 svn revision 9837
 
 namespace mozilla {
 namespace baseprofiler {
 
-int profiler_current_process_id() { return getpid(); }
+BaseProfilerProcessId profiler_current_process_id() {
+  return BaseProfilerProcessId::FromNumber(getpid());
+}
 
 int profiler_current_thread_id() {
   uint64_t tid;
   pthread_threadid_np(nullptr, &tid);
   // Cast the uint64_t value to an int.
   // In theory, this risks truncating the value. It's unknown if such large
   // values occur in reality.
   // It may be worth changing our cross-platform tid type to 64 bits.
--- a/mozglue/baseprofiler/core/platform-win32.cpp
+++ b/mozglue/baseprofiler/core/platform-win32.cpp
@@ -34,17 +34,19 @@
 
 #include "nsWindowsDllInterceptor.h"
 #include "mozilla/StackWalk_windows.h"
 #include "mozilla/WindowsVersion.h"
 
 namespace mozilla {
 namespace baseprofiler {
 
-int profiler_current_process_id() { return _getpid(); }
+BaseProfilerProcessId profiler_current_process_id() {
+  return BaseProfilerProcessId::FromNumber(_getpid());
+}
 
 int profiler_current_thread_id() {
   DWORD threadId = GetCurrentThreadId();
   MOZ_ASSERT(threadId <= INT32_MAX, "native thread ID is > INT32_MAX");
   return int(threadId);
 }
 
 static int64_t MicrosecondsSince1970() {
--- a/mozglue/baseprofiler/core/platform.cpp
+++ b/mozglue/baseprofiler/core/platform.cpp
@@ -684,17 +684,18 @@ class ActivePS {
 
       // Crude, non UTF-8 compatible, case insensitive substring search
       if (name.find(filter) != std::string::npos) {
         return true;
       }
 
       // If the filter starts with pid:, check for a pid match
       if (filter.find("pid:") == 0) {
-        std::string mypid = std::to_string(profiler_current_process_id());
+        std::string mypid =
+            std::to_string(profiler_current_process_id().ToNumber());
         if (filter.compare(4, std::string::npos, mypid) == 0) {
           return true;
         }
       }
     }
 
     return false;
   }
@@ -2146,17 +2147,17 @@ class Sampler {
 
  private:
 #if defined(GP_OS_linux) || defined(GP_OS_android) || defined(GP_OS_freebsd)
   // Used to restore the SIGPROF handler when ours is removed.
   struct sigaction mOldSigprofHandler;
 
   // This process' ID. Needed as an argument for tgkill in
   // SuspendAndSampleAndResumeThread.
-  int mMyPid;
+  BaseProfilerProcessId mMyPid;
 
   // The sampler thread's ID.  Used to assert that it is not sampling itself,
   // which would lead to deadlock.
   int mSamplerTid;
 
  public:
   // This is the one-and-only variable used to communicate between the sampler
   // thread and the samplee thread's signal handler. It's static because the
--- a/mozglue/baseprofiler/core/platform.h
+++ b/mozglue/baseprofiler/core/platform.h
@@ -46,47 +46,53 @@ namespace baseprofiler {
 bool LogTest(int aLevelToTest);
 void PrintToConsole(const char* aFmt, ...) MOZ_FORMAT_PRINTF(1, 2);
 }  // namespace baseprofiler
 }  // namespace mozilla
 
 // These are for MOZ_BASE_PROFILER_LOGGING and above. It's the default logging
 // level for the profiler, and should be used sparingly.
 #define LOG_TEST ::mozilla::baseprofiler::LogTest(3)
-#define LOG(arg, ...)                                           \
-  do {                                                          \
-    if (LOG_TEST) {                                             \
-      ::mozilla::baseprofiler::PrintToConsole(                  \
-          "[I %d/%d] " arg "\n", profiler_current_process_id(), \
-          profiler_current_thread_id(), ##__VA_ARGS__);         \
-    }                                                           \
+#define LOG(arg, ...)                                                \
+  do {                                                               \
+    if (LOG_TEST) {                                                  \
+      ::mozilla::baseprofiler::PrintToConsole(                       \
+          "[I %d/%d] " arg "\n",                                     \
+          int(::mozilla::baseprofiler::profiler_current_process_id() \
+                  .ToNumber()),                                      \
+          profiler_current_thread_id(), ##__VA_ARGS__);              \
+    }                                                                \
   } while (0)
 
 // These are for MOZ_BASE_PROFILER_DEBUG_LOGGING. It should be used for logging
 // that is somewhat more verbose than LOG.
 #define DEBUG_LOG_TEST ::mozilla::baseprofiler::LogTest(4)
-#define DEBUG_LOG(arg, ...)                                     \
-  do {                                                          \
-    if (DEBUG_LOG_TEST) {                                       \
-      ::mozilla::baseprofiler::PrintToConsole(                  \
-          "[D %d/%d] " arg "\n", profiler_current_process_id(), \
-          profiler_current_thread_id(), ##__VA_ARGS__);         \
-    }                                                           \
+#define DEBUG_LOG(arg, ...)                                          \
+  do {                                                               \
+    if (DEBUG_LOG_TEST) {                                            \
+      ::mozilla::baseprofiler::PrintToConsole(                       \
+          "[D %d/%d] " arg "\n",                                     \
+          int(::mozilla::baseprofiler::profiler_current_process_id() \
+                  .ToNumber()),                                      \
+          profiler_current_thread_id(), ##__VA_ARGS__);              \
+    }                                                                \
   } while (0)
 
 // These are for MOZ_BASE_PROFILER_VERBOSE_LOGGING. It should be used for
 // logging that is somewhat more verbose than DEBUG_LOG.
 #define VERBOSE_LOG_TEST ::mozilla::baseprofiler::LogTest(5)
-#define VERBOSE_LOG(arg, ...)                                   \
-  do {                                                          \
-    if (VERBOSE_LOG_TEST) {                                     \
-      ::mozilla::baseprofiler::PrintToConsole(                  \
-          "[V %d/%d] " arg "\n", profiler_current_process_id(), \
-          profiler_current_thread_id(), ##__VA_ARGS__);         \
-    }                                                           \
+#define VERBOSE_LOG(arg, ...)                                        \
+  do {                                                               \
+    if (VERBOSE_LOG_TEST) {                                          \
+      ::mozilla::baseprofiler::PrintToConsole(                       \
+          "[V %d/%d] " arg "\n",                                     \
+          int(::mozilla::baseprofiler::profiler_current_process_id() \
+                  .ToNumber()),                                      \
+          profiler_current_thread_id(), ##__VA_ARGS__);              \
+    }                                                                \
   } while (0)
 
 namespace mozilla {
 
 class JSONWriter;
 
 namespace baseprofiler {
 
--- a/mozglue/baseprofiler/core/shared-libraries-linux.cc
+++ b/mozglue/baseprofiler/core/shared-libraries-linux.cc
@@ -747,17 +747,17 @@ SharedLibraryInfo SharedLibraryInfo::Get
     // xpcshell when running tests), it won't be available and we should
     // not call it.
     return info;
   }
 #endif
 
 #if defined(GP_OS_linux) || defined(GP_OS_android)
   // Read info from /proc/self/maps. We ignore most of it.
-  pid_t pid = mozilla::baseprofiler::profiler_current_process_id();
+  pid_t pid = mozilla::baseprofiler::profiler_current_process_id().ToNumber();
   char path[PATH_MAX];
   SprintfLiteral(path, "/proc/%d/maps", pid);
   std::ifstream maps(path);
   std::string line;
   while (std::getline(maps, line)) {
     int ret;
     unsigned long start;
     unsigned long end;
--- a/mozglue/baseprofiler/lul/LulMain.cpp
+++ b/mozglue/baseprofiler/lul/LulMain.cpp
@@ -680,24 +680,24 @@ class PriMap {
   // a logging sink, for debugging.
   void (*mLog)(const char*);
 };
 
 ////////////////////////////////////////////////////////////////
 // LUL                                                        //
 ////////////////////////////////////////////////////////////////
 
-#define LUL_LOG(_str)                                           \
-  do {                                                          \
-    char buf[200];                                              \
-    SprintfLiteral(buf, "LUL: pid %d tid %d lul-obj %p: %s",    \
-                   profiler_current_process_id(),               \
-                   profiler_current_thread_id(), this, (_str)); \
-    buf[sizeof(buf) - 1] = 0;                                   \
-    mLog(buf);                                                  \
+#define LUL_LOG(_str)                                             \
+  do {                                                            \
+    char buf[200];                                                \
+    SprintfLiteral(buf, "LUL: pid %d tid %d lul-obj %p: %s",      \
+                   int(profiler_current_process_id().ToNumber()), \
+                   profiler_current_thread_id(), this, (_str));   \
+    buf[sizeof(buf) - 1] = 0;                                     \
+    mLog(buf);                                                    \
   } while (0)
 
 LUL::LUL(void (*aLog)(const char*))
     : mLog(aLog),
       mAdminMode(true),
       mAdminThreadId(profiler_current_thread_id()),
       mPriMap(new PriMap(aLog)),
       mSegArray(new SegArray()),
--- a/mozglue/baseprofiler/public/BaseProfilerUtils.h
+++ b/mozglue/baseprofiler/public/BaseProfilerUtils.h
@@ -109,31 +109,33 @@ static_assert(std::is_copy_assignable_v<
 static_assert(std::is_move_assignable_v<BaseProfilerThreadId>);
 
 }  // namespace mozilla::baseprofiler
 
 #ifndef MOZ_GECKO_PROFILER
 
 namespace mozilla::baseprofiler {
 
-[[nodiscard]] inline int profiler_current_process_id() { return 0; }
+[[nodiscard]] inline BaseProfilerProcessId profiler_current_process_id() {
+  return BaseProfilerProcessId{};
+}
 [[nodiscard]] inline int profiler_current_thread_id() { return 0; }
 [[nodiscard]] inline int profiler_main_thread_id() { return 0; }
 [[nodiscard]] inline bool profiler_is_main_thread() { return false; }
 
 }  // namespace mozilla::baseprofiler
 
 #else  // !MOZ_GECKO_PROFILER
 
 #  include "mozilla/Types.h"
 
 namespace mozilla::baseprofiler {
 
 // Get the current process's ID.
-[[nodiscard]] MFBT_API int profiler_current_process_id();
+[[nodiscard]] MFBT_API BaseProfilerProcessId profiler_current_process_id();
 
 // Get the current thread's ID.
 [[nodiscard]] MFBT_API int profiler_current_thread_id();
 
 namespace detail {
 // Statically initialized to 0, then set once from profiler_init(), which should
 // be called from the main thread before any other use of the profiler.
 extern MFBT_DATA int scProfilerMainThreadId;
--- a/mozglue/tests/TestBaseProfiler.cpp
+++ b/mozglue/tests/TestBaseProfiler.cpp
@@ -104,16 +104,28 @@ void TestProfilerUtils() {
     MOZ_RELEASE_ASSERT(pid2.IsSpecified());
     MOZ_RELEASE_ASSERT(pid2.ToNumber() == 123);
 
     // No conversions to/from numbers.
     static_assert(!std::is_constructible_v<BaseProfilerProcessId, Number>);
     static_assert(!std::is_assignable_v<BaseProfilerProcessId, Number>);
     static_assert(!std::is_constructible_v<Number, BaseProfilerProcessId>);
     static_assert(!std::is_assignable_v<Number, BaseProfilerProcessId>);
+
+    static_assert(
+        std::is_same_v<
+            decltype(mozilla::baseprofiler::profiler_current_process_id()),
+            BaseProfilerProcessId>);
+#ifdef MOZ_GECKO_PROFILER
+    MOZ_RELEASE_ASSERT(
+        mozilla::baseprofiler::profiler_current_process_id().IsSpecified());
+#else
+    MOZ_RELEASE_ASSERT(
+        !mozilla::baseprofiler::profiler_current_process_id().IsSpecified());
+#endif
   }
 
   {
     using mozilla::baseprofiler::BaseProfilerThreadId;
     using Number = BaseProfilerThreadId::NumberType;
     static constexpr Number scMaxNumber = std::numeric_limits<Number>::max();
 
     static_assert(
@@ -3740,17 +3752,17 @@ MOZ_NEVER_INLINE unsigned long long Fibo
                               MarkerTiming::IntervalUntilNowFrom(start),
                               std::to_string(DEPTH));
   }
   return f2 + f1;
 }
 
 void TestProfiler() {
   printf("TestProfiler starting -- pid: %d, tid: %d\n",
-         baseprofiler::profiler_current_process_id(),
+         int(baseprofiler::profiler_current_process_id().ToNumber()),
          baseprofiler::profiler_current_thread_id());
   // ::SleepMilli(10000);
 
   TestProfilerDependencies();
 
   {
     printf("profiler_init()...\n");
     AUTO_BASE_PROFILER_INIT;
@@ -4495,17 +4507,17 @@ void TestPredefinedMarkers() {
 
   PrintMarkers(buffer);
 
   printf("TestPredefinedMarkers done\n");
 }
 
 void TestProfilerMarkers() {
   printf("TestProfilerMarkers -- pid: %d, tid: %d\n",
-         mozilla::baseprofiler::profiler_current_process_id(),
+         int(mozilla::baseprofiler::profiler_current_process_id().ToNumber()),
          mozilla::baseprofiler::profiler_current_thread_id());
   // ::SleepMilli(10000);
 
   TestUniqueJSONStrings();
   TestMarkerCategory();
   TestMarkerThreadId();
   TestMarkerNoPayload();
   TestUserMarker();
@@ -4575,17 +4587,17 @@ void TestProfilerMarkers() {
 #if defined(XP_WIN)
 int wmain()
 #else
 int main()
 #endif  // defined(XP_WIN)
 {
 #ifdef MOZ_GECKO_PROFILER
   printf("BaseTestProfiler -- pid: %d, tid: %d\n",
-         baseprofiler::profiler_current_process_id(),
+         int(baseprofiler::profiler_current_process_id().ToNumber()),
          baseprofiler::profiler_current_thread_id());
   // ::SleepMilli(10000);
 #endif  // MOZ_GECKO_PROFILER
 
   TestProfilerUtils();
   // Note that there are two `TestProfiler{,Markers}` functions above, depending
   // on whether MOZ_GECKO_PROFILER is #defined.
   TestProfiler();