From 87668897f614f79997cf08ec8e9142ce35ab191e Mon Sep 17 00:00:00 2001 From: Eric Rahm Date: Fri, 22 Aug 2014 16:35:29 -0700 Subject: [PATCH] Bug 1056962 - Part 1: Move GetThreadName to LinuxUtils. r=jld --- xpcom/base/LinuxUtils.cpp | 50 +++++++++++++++++++++++++++++ xpcom/base/LinuxUtils.h | 34 ++++++++++++++++++++ xpcom/base/SystemMemoryReporter.cpp | 36 ++------------------- xpcom/base/moz.build | 2 ++ 4 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 xpcom/base/LinuxUtils.cpp create mode 100644 xpcom/base/LinuxUtils.h diff --git a/xpcom/base/LinuxUtils.cpp b/xpcom/base/LinuxUtils.cpp new file mode 100644 index 000000000000..331c82be979b --- /dev/null +++ b/xpcom/base/LinuxUtils.cpp @@ -0,0 +1,50 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "LinuxUtils.h" + +#if defined(XP_LINUX) + +#include +#include + +#include "nsPrintfCString.h" + +namespace mozilla { + +void +LinuxUtils::GetThreadName(pid_t aTid, nsACString& aName) +{ + aName.Truncate(); + if (aTid <= 0) { + return; + } + + const size_t kBuffSize = 16; // 15 chars max + '\n' + char buf[kBuffSize]; + nsPrintfCString path("/proc/%d/comm", aTid); + FILE* fp = fopen(path.get(), "r"); + if (!fp) { + // The fopen could also fail if the thread exited before we got here. + return; + } + + size_t len = fread(buf, 1, kBuffSize, fp); + fclose(fp); + + // No need to strip the '\n', since isspace() includes it. + while (len > 0 && + (isspace(buf[len - 1]) || isdigit(buf[len - 1]) || + buf[len - 1] == '#' || buf[len - 1] == '_')) { + --len; + } + + aName.Assign(buf, len); +} + +} + +#endif // XP_LINUX diff --git a/xpcom/base/LinuxUtils.h b/xpcom/base/LinuxUtils.h new file mode 100644 index 000000000000..e82c15e08c84 --- /dev/null +++ b/xpcom/base/LinuxUtils.h @@ -0,0 +1,34 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_LinuxUtils_h +#define mozilla_LinuxUtils_h + +#if defined(XP_LINUX) + +#include +#include "nsString.h" + +namespace mozilla { + +class LinuxUtils +{ +public: + // Obtain the name of a thread, omitting any numeric suffix added by a + // thread pool library (as in, e.g., "Binder_2" or "mozStorage #1"). + // The empty string is returned on error. + // + // Note: if this is ever needed on kernels older than 2.6.33 (early 2010), + // it will have to parse /proc//status instead, because + // /proc//comm didn't exist before then. + static void GetThreadName(pid_t aTid, nsACString& aName); +}; + +} + +#endif // XP_LINUX + +#endif diff --git a/xpcom/base/SystemMemoryReporter.cpp b/xpcom/base/SystemMemoryReporter.cpp index 41516bbd1625..84e966ad0758 100644 --- a/xpcom/base/SystemMemoryReporter.cpp +++ b/xpcom/base/SystemMemoryReporter.cpp @@ -7,6 +7,7 @@ #include "mozilla/SystemMemoryReporter.h" #include "mozilla/Attributes.h" +#include "mozilla/LinuxUtils.h" #include "mozilla/PodOperations.h" #include "mozilla/Preferences.h" #include "mozilla/TaggedAnonymousMemory.h" @@ -349,37 +350,6 @@ private: return NS_OK; } - // Obtain the name of a thread, omitting any numeric suffix added by a - // thread pool library (as in, e.g., "Binder_2" or "mozStorage #1"). - // The empty string is returned on error. - // - // Note: if this is ever needed on kernels older than 2.6.33 (early 2010), - // it will have to parse /proc//status instead, because - // /proc//comm didn't exist before then. - void GetThreadName(pid_t aTid, nsACString& aName) - { - aName.Truncate(); - if (aTid <= 0) { - return; - } - char buf[16]; // 15 chars max + '\n' - nsPrintfCString path("/proc/%d/comm", aTid); - FILE* fp = fopen(path.get(), "r"); - if (!fp) { - // The fopen could also fail if the thread exited before we got here. - return; - } - size_t len = fread(buf, 1, sizeof(buf), fp); - fclose(fp); - // No need to strip the '\n', since isspace() includes it. - while (len > 0 && - (isspace(buf[len - 1]) || isdigit(buf[len - 1]) || - buf[len - 1] == '#' || buf[len - 1] == '_')) { - --len; - } - aName.Assign(buf, len); - } - nsresult ParseMappings(FILE* aFile, const nsACString& aProcessName, nsIHandleReportCallback* aHandleReport, @@ -504,7 +474,7 @@ private: // "[stack:" entries from reaching the IsAnonymous case below.) pid_t tid = atoi(absPath.get() + 7); nsAutoCString threadName, escapedThreadName; - GetThreadName(tid, threadName); + LinuxUtils::GetThreadName(tid, threadName); if (threadName.IsEmpty()) { threadName.AssignLiteral(""); } @@ -1037,7 +1007,7 @@ private: // Attempt to map the pid to a more useful name. nsAutoCString procName; - GetThreadName(atoi(pid), procName); + LinuxUtils::GetThreadName(atoi(pid), procName); if (procName.IsEmpty()) { procName.Append("pid="); diff --git a/xpcom/base/moz.build b/xpcom/base/moz.build index bcb5d1d23154..e239e0c0d8db 100644 --- a/xpcom/base/moz.build +++ b/xpcom/base/moz.build @@ -74,6 +74,7 @@ EXPORTS.mozilla += [ 'ClearOnShutdown.h', 'CycleCollectedJSRuntime.h', 'Debug.h', + 'LinuxUtils.h', 'nsMemoryInfoDumper.h', 'StackWalk.h', 'StaticMutex.h', @@ -128,6 +129,7 @@ if CONFIG['OS_TARGET'] != 'WINNT' or \ if CONFIG['OS_ARCH'] == 'Linux': SOURCES += [ + 'LinuxUtils.cpp', 'SystemMemoryReporter.cpp', ]