diff options
-rw-r--r-- | simpleperf/environment.cpp | 28 | ||||
-rw-r--r-- | simpleperf/environment.h | 1 | ||||
-rw-r--r-- | simpleperf/environment_test.cpp | 7 | ||||
-rw-r--r-- | simpleperf/utils.cpp | 28 | ||||
-rw-r--r-- | simpleperf/utils.h | 3 | ||||
-rw-r--r-- | simpleperf/utils_test.cpp | 7 |
6 files changed, 38 insertions, 36 deletions
diff --git a/simpleperf/environment.cpp b/simpleperf/environment.cpp index 86766921..38ba2eb9 100644 --- a/simpleperf/environment.cpp +++ b/simpleperf/environment.cpp @@ -92,34 +92,6 @@ std::vector<int> GetOnlineCpus() { return result; } -std::vector<int> GetCpusFromString(const std::string& s) { - std::set<int> cpu_set; - bool have_dash = false; - const char* p = s.c_str(); - char* endp; - int last_cpu; - int cpu; - // Parse line like: 0,1-3, 5, 7-8 - while ((cpu = static_cast<int>(strtol(p, &endp, 10))) != 0 || endp != p) { - if (have_dash && !cpu_set.empty()) { - for (int t = last_cpu + 1; t < cpu; ++t) { - cpu_set.insert(t); - } - } - have_dash = false; - cpu_set.insert(cpu); - last_cpu = cpu; - p = endp; - while (!isdigit(*p) && *p != '\0') { - if (*p == '-') { - have_dash = true; - } - ++p; - } - } - return std::vector<int>(cpu_set.begin(), cpu_set.end()); -} - static std::vector<KernelMmap> GetLoadedModules() { std::vector<KernelMmap> result; FILE* fp = fopen("/proc/modules", "re"); diff --git a/simpleperf/environment.h b/simpleperf/environment.h index 173cdcc6..693be3a2 100644 --- a/simpleperf/environment.h +++ b/simpleperf/environment.h @@ -36,7 +36,6 @@ #include "perf_regs.h" std::vector<int> GetOnlineCpus(); -std::vector<int> GetCpusFromString(const std::string& s); struct KernelMmap { std::string name; diff --git a/simpleperf/environment_test.cpp b/simpleperf/environment_test.cpp index dec9649c..5f799642 100644 --- a/simpleperf/environment_test.cpp +++ b/simpleperf/environment_test.cpp @@ -21,13 +21,6 @@ #include "dso.h" #include "environment.h" -TEST(environment, GetCpusFromString) { - ASSERT_EQ(GetCpusFromString(""), std::vector<int>()); - ASSERT_EQ(GetCpusFromString("0-2"), std::vector<int>({0, 1, 2})); - ASSERT_EQ(GetCpusFromString("0,2-3"), std::vector<int>({0, 2, 3})); - ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::vector<int>({0, 1, 2, 3, 4})); -} - TEST(environment, PrepareVdsoFile) { std::string content; ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &content)); diff --git a/simpleperf/utils.cpp b/simpleperf/utils.cpp index b27fabd6..407c5f08 100644 --- a/simpleperf/utils.cpp +++ b/simpleperf/utils.cpp @@ -410,3 +410,31 @@ std::string GetSimpleperfVersion() { return android::base::StringPrintf("%d.build.%s", SIMPLEPERF_VERSION, android::build::GetBuildNumber().c_str()); } + +std::vector<int> GetCpusFromString(const std::string& s) { + std::set<int> cpu_set; + bool have_dash = false; + const char* p = s.c_str(); + char* endp; + int last_cpu; + int cpu; + // Parse line like: 0,1-3, 5, 7-8 + while ((cpu = static_cast<int>(strtol(p, &endp, 10))) != 0 || endp != p) { + if (have_dash && !cpu_set.empty()) { + for (int t = last_cpu + 1; t < cpu; ++t) { + cpu_set.insert(t); + } + } + have_dash = false; + cpu_set.insert(cpu); + last_cpu = cpu; + p = endp; + while (!isdigit(*p) && *p != '\0') { + if (*p == '-') { + have_dash = true; + } + ++p; + } + } + return std::vector<int>(cpu_set.begin(), cpu_set.end()); +} diff --git a/simpleperf/utils.h b/simpleperf/utils.h index 872e191d..6bb51d3c 100644 --- a/simpleperf/utils.h +++ b/simpleperf/utils.h @@ -21,6 +21,7 @@ #include <time.h> #include <functional> +#include <set> #include <string> #include <vector> @@ -163,6 +164,8 @@ timeval SecondToTimeval(double time_in_sec); std::string GetSimpleperfVersion(); +std::vector<int> GetCpusFromString(const std::string& s); + namespace { // from boost::hash_combine diff --git a/simpleperf/utils_test.cpp b/simpleperf/utils_test.cpp index e9df046e..878f8aae 100644 --- a/simpleperf/utils_test.cpp +++ b/simpleperf/utils_test.cpp @@ -99,3 +99,10 @@ TEST(utils, ArchiveHelper) { ASSERT_FALSE(ArchiveHelper::CreateInstance(GetTestData(ELF_FILE))); ASSERT_FALSE(ArchiveHelper::CreateInstance("/dev/zero")); } + +TEST(utils, GetCpusFromString) { + ASSERT_EQ(GetCpusFromString(""), std::vector<int>()); + ASSERT_EQ(GetCpusFromString("0-2"), std::vector<int>({0, 1, 2})); + ASSERT_EQ(GetCpusFromString("0,2-3"), std::vector<int>({0, 2, 3})); + ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::vector<int>({0, 1, 2, 3, 4})); +} |