diff options
author | Yi Jin <jinyithu@google.com> | 2017-11-20 19:34:15 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-11-20 19:34:15 +0000 |
commit | 0dc7e54400da07e98cc05e8531465df7501e81a5 (patch) | |
tree | 2eba56d47ba9ba8be3ec3b11d74d477ac96ee641 /cmds/incident_helper | |
parent | 3fbc4f3845ae899867ad69af409492043c5a2b57 (diff) | |
parent | 0eb223496c3dee6bd0a33ea4fa664fb92ef557cc (diff) |
Merge "Implement Cpu Freq Section"
Diffstat (limited to 'cmds/incident_helper')
-rw-r--r-- | cmds/incident_helper/src/main.cpp | 3 | ||||
-rw-r--r-- | cmds/incident_helper/src/parsers/CpuFreqParser.cpp | 90 | ||||
-rw-r--r-- | cmds/incident_helper/src/parsers/CpuFreqParser.h | 35 | ||||
-rw-r--r-- | cmds/incident_helper/testdata/cpufreq.txt | 6 | ||||
-rw-r--r-- | cmds/incident_helper/tests/CpuFreqParser_test.cpp | 130 | ||||
-rw-r--r-- | cmds/incident_helper/tests/CpuInfoParser_test.cpp | 2 |
6 files changed, 265 insertions, 1 deletions
diff --git a/cmds/incident_helper/src/main.cpp b/cmds/incident_helper/src/main.cpp index 8239d8ec92ed..c8a0883d493c 100644 --- a/cmds/incident_helper/src/main.cpp +++ b/cmds/incident_helper/src/main.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "incident_helper" +#include "parsers/CpuFreqParser.h" #include "parsers/CpuInfoParser.h" #include "parsers/KernelWakesParser.h" #include "parsers/PageTypeInfoParser.h" @@ -60,6 +61,8 @@ static TextParserBase* selectParser(int section) { return new KernelWakesParser(); case 2003: return new CpuInfoParser(); + case 2004: + return new CpuFreqParser(); default: return NULL; } diff --git a/cmds/incident_helper/src/parsers/CpuFreqParser.cpp b/cmds/incident_helper/src/parsers/CpuFreqParser.cpp new file mode 100644 index 000000000000..02f1ce7cc0fc --- /dev/null +++ b/cmds/incident_helper/src/parsers/CpuFreqParser.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "incident_helper" + +#include <android/util/ProtoOutputStream.h> +#include <unistd.h> + +#include "frameworks/base/core/proto/android/os/cpufreq.proto.h" +#include "ih_util.h" +#include "CpuFreqParser.h" + +using namespace android::os; + +status_t +CpuFreqParser::Parse(const int in, const int out) const +{ + Reader reader(in); + string line; + + // parse header + reader.readLine(&line); + header_t header = parseHeader(line, TAB_DELIMITER); + if (header.size() < 1) { + fprintf(stderr, "Bad header: %s\n", line.c_str()); + return BAD_VALUE; + } + const int numCpus = (int)header.size() - 1; + vector<pair<int, long long>> cpucores[numCpus]; + + // parse freq and time + while (reader.readLine(&line)) { + if (line.empty()) continue; + + record_t record = parseRecord(line, TAB_DELIMITER); + if (record.size() != header.size()) { + fprintf(stderr, "Bad line: %s\n", line.c_str()); + continue; + } + + int freq = toInt(record[0]); + for (int i=0; i<numCpus; i++) { + if (strcmp(record[i+1].c_str(), "N/A") == 0) { + continue; + } + cpucores[i].push_back(make_pair(freq, toLongLong(record[i+1]))); + } + } + + ProtoOutputStream proto; + + long jiffyHz = sysconf(_SC_CLK_TCK); + proto.write(CpuFreq::JIFFY_HZ, (int)jiffyHz); + + for (int i=0; i<numCpus; i++) { + long long token = proto.start(CpuFreq::CPU_FREQS); + proto.write(CpuFreqStats::CPU_NAME, header[i+1]); + for (vector<pair<int, long long>>::iterator it = cpucores[i].begin(); it != cpucores[i].end(); it++) { + long long stateToken = proto.start(CpuFreqStats::TIMES); + proto.write(CpuFreqStats::TimeInState::STATE_KHZ, it->first); + proto.write(CpuFreqStats::TimeInState::TIME_JIFFY, it->second); + proto.end(stateToken); + } + proto.end(token); + } + + if (!reader.ok(&line)) { + fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str()); + return -1; + } + + if (!proto.flush(out)) { + fprintf(stderr, "[%s]Error writing proto back\n", this->name.string()); + return -1; + } + fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.string(), proto.size()); + return NO_ERROR; +} diff --git a/cmds/incident_helper/src/parsers/CpuFreqParser.h b/cmds/incident_helper/src/parsers/CpuFreqParser.h new file mode 100644 index 000000000000..470d56834781 --- /dev/null +++ b/cmds/incident_helper/src/parsers/CpuFreqParser.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CPU_FREQ_PARSER_H +#define CPU_FREQ_PARSER_H + +#include "TextParserBase.h" + +using namespace android; + +/** + * Cpu frequency parser, parses text in /sys/devices/system/cpu/cpufreq/all_time_in_state + */ +class CpuFreqParser : public TextParserBase { +public: + CpuFreqParser() : TextParserBase(String8("CpuFreqParser")) {}; + ~CpuFreqParser() {}; + + virtual status_t Parse(const int in, const int out) const; +}; + +#endif // CPU_FREQ_PARSER_H diff --git a/cmds/incident_helper/testdata/cpufreq.txt b/cmds/incident_helper/testdata/cpufreq.txt new file mode 100644 index 000000000000..6472839e455e --- /dev/null +++ b/cmds/incident_helper/testdata/cpufreq.txt @@ -0,0 +1,6 @@ +freq cpu0 cpu1 cpu2 cpu3 +307200 23860761 23860761 23890935 23890935 +384000 83124 83124 29383 29383 +748800 N/A N/A 10547 10547 +768000 22652 22652 N/A N/A +825600 N/A N/A 13173 13173 diff --git a/cmds/incident_helper/tests/CpuFreqParser_test.cpp b/cmds/incident_helper/tests/CpuFreqParser_test.cpp new file mode 100644 index 000000000000..1c2f9e59308d --- /dev/null +++ b/cmds/incident_helper/tests/CpuFreqParser_test.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CpuFreqParser.h" + +#include "frameworks/base/core/proto/android/os/cpufreq.pb.h" + +#include <android-base/file.h> +#include <android-base/test_utils.h> +#include <gmock/gmock.h> +#include <google/protobuf/message.h> +#include <gtest/gtest.h> +#include <string.h> +#include <fcntl.h> + +using namespace android::base; +using namespace android::os; +using namespace std; +using ::testing::StrEq; +using ::testing::Test; +using ::testing::internal::CaptureStderr; +using ::testing::internal::CaptureStdout; +using ::testing::internal::GetCapturedStderr; +using ::testing::internal::GetCapturedStdout; + +class CpuFreqParserTest : public Test { +public: + virtual void SetUp() override { + ASSERT_TRUE(tf.fd != -1); + } + + string getSerializedString(::google::protobuf::Message& message) { + string expectedStr; + message.SerializeToFileDescriptor(tf.fd); + ReadFileToString(tf.path, &expectedStr); + return expectedStr; + } + +protected: + TemporaryFile tf; + + const string kTestPath = GetExecutableDirectory(); + const string kTestDataPath = kTestPath + "/testdata/"; +}; + +TEST_F(CpuFreqParserTest, Success) { + const string testFile = kTestDataPath + "cpufreq.txt"; + CpuFreqParser parser; + CpuFreq expected; + + long jiffyHz = sysconf(_SC_CLK_TCK); + expected.set_jiffy_hz(jiffyHz); + + CpuFreqStats::TimeInState* state; + + CpuFreqStats* cpu0 = expected.add_cpu_freqs(); + cpu0->set_cpu_name("cpu0"); + state = cpu0->add_times(); + state->set_state_khz(307200); + state->set_time_jiffy(23860761); + state = cpu0->add_times(); + state->set_state_khz(384000); + state->set_time_jiffy(83124); + state = cpu0->add_times(); + state->set_state_khz(768000); + state->set_time_jiffy(22652); + + CpuFreqStats* cpu1 = expected.add_cpu_freqs(); + cpu1->set_cpu_name("cpu1"); + state = cpu1->add_times(); + state->set_state_khz(307200); + state->set_time_jiffy(23860761); + state = cpu1->add_times(); + state->set_state_khz(384000); + state->set_time_jiffy(83124); + state = cpu1->add_times(); + state->set_state_khz(768000); + state->set_time_jiffy(22652); + + CpuFreqStats* cpu2 = expected.add_cpu_freqs(); + cpu2->set_cpu_name("cpu2"); + state = cpu2->add_times(); + state->set_state_khz(307200); + state->set_time_jiffy(23890935); + state = cpu2->add_times(); + state->set_state_khz(384000); + state->set_time_jiffy(29383); + state = cpu2->add_times(); + state->set_state_khz(748800); + state->set_time_jiffy(10547); + state = cpu2->add_times(); + state->set_state_khz(825600); + state->set_time_jiffy(13173); + + CpuFreqStats* cpu3 = expected.add_cpu_freqs(); + cpu3->set_cpu_name("cpu3"); + state = cpu3->add_times(); + state->set_state_khz(307200); + state->set_time_jiffy(23890935); + state = cpu3->add_times(); + state->set_state_khz(384000); + state->set_time_jiffy(29383); + state = cpu3->add_times(); + state->set_state_khz(748800); + state->set_time_jiffy(10547); + state = cpu3->add_times(); + state->set_state_khz(825600); + state->set_time_jiffy(13173); + + int fd = open(testFile.c_str(), O_RDONLY); + ASSERT_TRUE(fd != -1); + + CaptureStdout(); + ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO)); + EXPECT_EQ(GetCapturedStdout(), getSerializedString(expected)); + close(fd); +} diff --git a/cmds/incident_helper/tests/CpuInfoParser_test.cpp b/cmds/incident_helper/tests/CpuInfoParser_test.cpp index 57ad15cf3910..bbc14bca3efc 100644 --- a/cmds/incident_helper/tests/CpuInfoParser_test.cpp +++ b/cmds/incident_helper/tests/CpuInfoParser_test.cpp @@ -56,7 +56,7 @@ protected: const string kTestDataPath = kTestPath + "/testdata/"; }; -TEST_F(CpuInfoParserTest, HasSwapInfo) { +TEST_F(CpuInfoParserTest, Success) { const string testFile = kTestDataPath + "cpuinfo.txt"; CpuInfoParser parser; CpuInfo expected; |