1 /*
2  * Copyright (C) 2020 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 "perfetto/base/build_config.h"
18 
19 #if PERFETTO_BUILDFLAG(PERFETTO_WATCHDOG)
20 
21 #include "perfetto/ext/base/watchdog_posix.h"
22 
23 #include <stdio.h>
24 
25 #include "perfetto/ext/base/file_utils.h"
26 #include "perfetto/ext/base/temp_file.h"
27 
28 #include "test/gtest_and_gmock.h"
29 
30 namespace perfetto {
31 namespace base {
32 namespace {
33 
TEST(WatchdogPosixTest,ParseProcStat)34 TEST(WatchdogPosixTest, ParseProcStat) {
35   constexpr const char stat[] =
36       "2965981 (zsh) S 2965977 2965981 2965981 34822 2966607 4194304 6632 6697 "
37       "0 0 11 6 4 1 20 0 1 0 227163466 15839232 2311 18446744073709551615 "
38       "94823961161728 94823961762781 140722993535472 0 0 0 2 3686400 134295555 "
39       "0 0 0 17 2 0 0 0 0 0 94823961905904 94823961935208 94823993954304 "
40       "140722993543678 140722993543691 140722993543691 140722993545195 0";
41   TempFile f = TempFile::CreateUnlinked();
42   WriteAll(f.fd(), stat, sizeof(stat));
43   ASSERT_NE(lseek(f.fd(), 0, SEEK_SET), -1);
44   ProcStat ps;
45   ASSERT_TRUE(ReadProcStat(f.fd(), &ps));
46   EXPECT_EQ(ps.utime, 11u);
47   EXPECT_EQ(ps.stime, 6u);
48   EXPECT_EQ(ps.rss_pages, 2311);
49 }
50 
51 }  // namespace
52 }  // namespace base
53 }  // namespace perfetto
54 
55 #endif  // PERFETTO_BUILDFLAG(PERFETTO_WATCHDOG)
56