1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "CpuInfoParser.h"
18
19 #include "frameworks/base/core/proto/android/os/cpuinfo.pb.h"
20
21 #include <android-base/file.h>
22 #include <android-base/test_utils.h>
23 #include <gmock/gmock.h>
24 #include <google/protobuf/message_lite.h>
25 #include <gtest/gtest.h>
26 #include <string.h>
27 #include <fcntl.h>
28
29 using namespace android::base;
30 using namespace android::os;
31 using namespace std;
32 using ::testing::StrEq;
33 using ::testing::Test;
34 using ::testing::internal::CaptureStderr;
35 using ::testing::internal::CaptureStdout;
36 using ::testing::internal::GetCapturedStderr;
37 using ::testing::internal::GetCapturedStdout;
38
39 class CpuInfoParserTest : public Test {
40 public:
SetUp()41 virtual void SetUp() override {
42 ASSERT_TRUE(tf.fd != -1);
43 }
44
45 protected:
46 TemporaryFile tf;
47
48 const string kTestPath = GetExecutableDirectory();
49 const string kTestDataPath = kTestPath + "/testdata/";
50 };
51
TEST_F(CpuInfoParserTest,Success)52 TEST_F(CpuInfoParserTest, Success) {
53 const string testFile = kTestDataPath + "cpuinfo.txt";
54 CpuInfoParser parser;
55 CpuInfoProto expected;
56
57 CpuInfoProto::TaskStats* taskStats = expected.mutable_task_stats();
58 taskStats->set_total(2038);
59 taskStats->set_running(1);
60 taskStats->set_sleeping(2033);
61 taskStats->set_stopped(0);
62 taskStats->set_zombie(0);
63
64 CpuInfoProto::MemStats* mem = expected.mutable_mem();
65 mem->set_total(3842668);
66 mem->set_used(3761936);
67 mem->set_free(80732);
68 mem->set_buffers(220188);
69
70 CpuInfoProto::MemStats* swap = expected.mutable_swap();
71 swap->set_total(524284);
72 swap->set_used(25892);
73 swap->set_free(498392);
74 swap->set_cached(1316952);
75
76 CpuInfoProto::CpuUsage* usage = expected.mutable_cpu_usage();
77 usage->set_cpu(400);
78 usage->set_user(17);
79 usage->set_nice(0);
80 usage->set_sys(43);
81 usage->set_idle(338);
82 usage->set_iow(0);
83 usage->set_irq(0);
84 usage->set_sirq(1);
85 usage->set_host(0);
86
87 // This is a special line which is able to be parsed by the CpuInfoParser
88 CpuInfoProto::Task* task1 = expected.add_tasks();
89 task1->set_pid(29438);
90 task1->set_tid(29438);
91 task1->set_user("rootabcdefghij");
92 task1->set_pr("20");
93 task1->set_ni(0);
94 task1->set_cpu(57.9);
95 task1->set_s(CpuInfoProto::Task::STATUS_R);
96 task1->set_virt("14M");
97 task1->set_res("3.8M");
98 task1->set_pcy(CpuInfoProto::Task::POLICY_UNKNOWN);
99 task1->set_cmd("top test");
100 task1->set_name("top");
101
102 CpuInfoProto::Task* task2 = expected.add_tasks();
103 task2->set_pid(916);
104 task2->set_tid(916);
105 task2->set_user("system");
106 task2->set_pr("18");
107 task2->set_ni(-2);
108 task2->set_cpu(1.4);
109 task2->set_s(CpuInfoProto::Task::STATUS_S);
110 task2->set_virt("4.6G");
111 task2->set_res("404M");
112 task2->set_pcy(CpuInfoProto::Task::POLICY_fg);
113 task2->set_cmd("system_server");
114 task2->set_name("system_server");
115
116 CpuInfoProto::Task* task3 = expected.add_tasks();
117 task3->set_pid(28);
118 task3->set_tid(28);
119 task3->set_user("root");
120 task3->set_pr("-2");
121 task3->set_ni(0);
122 task3->set_cpu(1.4);
123 task3->set_s(CpuInfoProto::Task::STATUS_S);
124 task3->set_virt("0");
125 task3->set_res("0");
126 task3->set_pcy(CpuInfoProto::Task::POLICY_bg);
127 task3->set_cmd("rcuc/3");
128 task3->set_name("[rcuc/3]");
129
130 CpuInfoProto::Task* task4 = expected.add_tasks();
131 task4->set_pid(27);
132 task4->set_tid(27);
133 task4->set_user("root");
134 task4->set_pr("RT");
135 task4->set_ni(0);
136 task4->set_cpu(1.4);
137 task4->set_s(CpuInfoProto::Task::STATUS_S);
138 task4->set_virt("0");
139 task4->set_res("0");
140 task4->set_pcy(CpuInfoProto::Task::POLICY_ta);
141 task4->set_cmd("migration/3");
142 task4->set_name("[migration/3]");
143
144 int fd = open(testFile.c_str(), O_RDONLY);
145 ASSERT_TRUE(fd != -1);
146
147 CaptureStdout();
148 ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
149 EXPECT_EQ(GetCapturedStdout(), expected.SerializeAsString());
150 close(fd);
151 }
152