blob: 3adc261dfbc9c0d9d43b1ffae05f25e2c02d3315 [file] [log] [blame]
Florian Mayerb4334002018-02-01 11:10:36 +00001/*
2 * Copyright (C) 2018 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/watchdog.h"
18
19#include "gtest/gtest.h"
Lalit Maganti44ff2a72018-02-27 16:12:24 +000020#include "perfetto/base/logging.h"
21#include "perfetto/base/page_allocator.h"
Florian Mayerb4334002018-02-01 11:10:36 +000022
23#include <time.h>
Lalit Maganti44ff2a72018-02-27 16:12:24 +000024#include <map>
Florian Mayerb4334002018-02-01 11:10:36 +000025
26namespace perfetto {
27namespace base {
28namespace {
29
Lalit Maganti44ff2a72018-02-27 16:12:24 +000030class TestWatchdog : public Watchdog {
31 public:
Florian Mayer22e4b392018-03-08 10:20:11 +000032 explicit TestWatchdog(uint32_t polling_interval_ms)
33 : Watchdog(polling_interval_ms) {}
Lalit Maganti44ff2a72018-02-27 16:12:24 +000034 ~TestWatchdog() override {}
35 TestWatchdog(TestWatchdog&& other) noexcept = default;
36};
37
38TEST(WatchdogTest, TimerCrash) {
39 // Create a timer for 20 ms and don't release wihin the time.
Florian Mayerb4334002018-02-01 11:10:36 +000040 EXPECT_DEATH(
41 {
Lalit Maganti44ff2a72018-02-27 16:12:24 +000042 TestWatchdog watchdog(100);
43 auto handle = watchdog.CreateFatalTimer(20);
44 usleep(200 * 1000);
Florian Mayerb4334002018-02-01 11:10:36 +000045 },
46 "");
47}
48
Lalit Maganti44ff2a72018-02-27 16:12:24 +000049TEST(WatchdogTest, CrashEvenWhenMove) {
50 std::map<int, Watchdog::Timer> timers;
51 EXPECT_DEATH(
52 {
53 TestWatchdog watchdog(100);
54 timers.emplace(0, watchdog.CreateFatalTimer(20));
55 usleep(200 * 1000);
56 },
57 "");
58}
59
60TEST(WatchdogTest, CrashMemory) {
61 EXPECT_DEATH(
62 {
63 // Allocate 8MB of data and use it to increase RSS.
64 const size_t kSize = 8 * 1024 * 1024;
65 auto void_ptr = PageAllocator::Allocate(kSize);
66 volatile uint8_t* ptr = static_cast<volatile uint8_t*>(void_ptr.get());
67 for (size_t i = 0; i < kSize; i += sizeof(size_t)) {
68 *reinterpret_cast<volatile size_t*>(&ptr[i]) = i;
69 }
70
71 TestWatchdog watchdog(5);
72 watchdog.SetMemoryLimit(8 * 1024 * 1024, 25);
Lalit Magantie419ccb2018-03-06 11:48:03 +000073 watchdog.Start();
Lalit Maganti44ff2a72018-02-27 16:12:24 +000074
75 // Sleep so that the watchdog has some time to pick it up.
76 usleep(1000 * 1000);
77 },
78 "");
79}
80
81TEST(WatchdogTest, CrashCpu) {
82 EXPECT_DEATH(
83 {
84 TestWatchdog watchdog(1);
85 watchdog.SetCpuLimit(10, 25);
Lalit Magantie419ccb2018-03-06 11:48:03 +000086 watchdog.Start();
Lalit Maganti44ff2a72018-02-27 16:12:24 +000087 volatile int x = 0;
88 while (true) {
89 x++;
90 }
91 },
92 "");
Florian Mayerb4334002018-02-01 11:10:36 +000093}
94
95} // namespace
96} // namespace base
97} // namespace perfetto