diff options
author | jln@chromium.org <jln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-06 07:02:22 +0000 |
---|---|---|
committer | jln@chromium.org <jln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-06 07:02:22 +0000 |
commit | ddf5b51625c2b564b27c53886111b836492b0403 (patch) | |
tree | 3ffba72fb2091fdad345b6e28dd89b7ad6136900 | |
parent | 73a087f880e0d18557b5c44630e0f4f68541c2b9 (diff) | |
download | chromium_src-ddf5b51625c2b564b27c53886111b836492b0403.zip chromium_src-ddf5b51625c2b564b27c53886111b836492b0403.tar.gz chromium_src-ddf5b51625c2b564b27c53886111b836492b0403.tar.bz2 |
base: add GetNumberOfThreads() to Linux and Android.
NOTRY=true
Review URL: https://chromiumcodereview.appspot.com/12193017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180914 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/process_util.h | 6 | ||||
-rw-r--r-- | base/process_util_linux.cc | 5 | ||||
-rw-r--r-- | base/process_util_unittest.cc | 19 |
3 files changed, 30 insertions, 0 deletions
diff --git a/base/process_util.h b/base/process_util.h index fe21adf..10d3028 100644 --- a/base/process_util.h +++ b/base/process_util.h @@ -197,6 +197,12 @@ BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); // Exposed for testing. BASE_EXPORT int ParseProcStatCPU(const std::string& input); +// Get the number of threads of |process| as available in /proc/<pid>/stat. +// This should be used with care as no synchronization with running threads is +// done. This is mostly useful to guarantee being single-threaded. +// Returns 0 on failure. +BASE_EXPORT int GetNumberOfThreads(ProcessHandle process); + // The maximum allowed value for the OOM score. const int kMaxOomScore = 1000; diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index 06077a8..36f377b 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -46,6 +46,7 @@ enum ProcStatsFields { VM_PGRP = 4, // Process group id. VM_UTIME = 13, // Time scheduled in user mode in clock ticks. VM_STIME = 14, // Time scheduled in kernel mode in clock ticks. + VM_NUMTHREADS = 19, // Number of threads. VM_VSIZE = 22, // Virtual memory size in bytes. VM_RSS = 23, // Resident Set Size in pages. }; @@ -594,6 +595,10 @@ int ParseProcStatCPU(const std::string& input) { return utime + stime; } +int GetNumberOfThreads(ProcessHandle process) { + return ReadProcStatsAndGetFieldAsInt(process, VM_NUMTHREADS); +} + namespace { // The format of /proc/meminfo is: diff --git a/base/process_util_unittest.cc b/base/process_util_unittest.cc index 9897e41..309b87c 100644 --- a/base/process_util_unittest.cc +++ b/base/process_util_unittest.cc @@ -19,6 +19,7 @@ #include "base/test/test_timeouts.h" #include "base/third_party/dynamic_annotations/dynamic_annotations.h" #include "base/threading/platform_thread.h" +#include "base/threading/thread.h" #include "base/utf_string_conversions.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/multiprocess_func_list.h" @@ -944,6 +945,24 @@ TEST_F(ProcessUtilTest, ParseProcStatCPU) { EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); } + +TEST_F(ProcessUtilTest, GetNumberOfThreads) { + const base::ProcessHandle current = base::GetCurrentProcessHandle(); + const int initial_threads = base::GetNumberOfThreads(current); + ASSERT_GT(initial_threads, 0); + const int kNumAdditionalThreads = 10; + { + scoped_ptr<base::Thread> my_threads[kNumAdditionalThreads]; + for (int i = 0; i < kNumAdditionalThreads; ++i) { + my_threads[i].reset(new base::Thread("GetNumberOfThreadsTest")); + my_threads[i]->Start(); + ASSERT_EQ(base::GetNumberOfThreads(current), initial_threads + 1 + i); + } + } + // The Thread destructor will stop them. + ASSERT_EQ(initial_threads, base::GetNumberOfThreads(current)); +} + #endif // defined(OS_LINUX) || defined(OS_ANDROID) // TODO(port): port those unit tests. |