Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 1 | /* |
| 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 Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 20 | #include "perfetto/base/logging.h" |
| 21 | #include "perfetto/base/page_allocator.h" |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 22 | |
| 23 | #include <time.h> |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 24 | #include <map> |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 25 | |
| 26 | namespace perfetto { |
| 27 | namespace base { |
| 28 | namespace { |
| 29 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 30 | class TestWatchdog : public Watchdog { |
| 31 | public: |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 32 | explicit TestWatchdog(uint32_t polling_interval_ms) |
| 33 | : Watchdog(polling_interval_ms) {} |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 34 | ~TestWatchdog() override {} |
| 35 | TestWatchdog(TestWatchdog&& other) noexcept = default; |
| 36 | }; |
| 37 | |
| 38 | TEST(WatchdogTest, TimerCrash) { |
| 39 | // Create a timer for 20 ms and don't release wihin the time. |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 40 | EXPECT_DEATH( |
| 41 | { |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 42 | TestWatchdog watchdog(100); |
| 43 | auto handle = watchdog.CreateFatalTimer(20); |
| 44 | usleep(200 * 1000); |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 45 | }, |
| 46 | ""); |
| 47 | } |
| 48 | |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 49 | TEST(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 | |
| 60 | TEST(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 Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 73 | watchdog.Start(); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 74 | |
| 75 | // Sleep so that the watchdog has some time to pick it up. |
| 76 | usleep(1000 * 1000); |
| 77 | }, |
| 78 | ""); |
| 79 | } |
| 80 | |
| 81 | TEST(WatchdogTest, CrashCpu) { |
| 82 | EXPECT_DEATH( |
| 83 | { |
| 84 | TestWatchdog watchdog(1); |
| 85 | watchdog.SetCpuLimit(10, 25); |
Lalit Maganti | e419ccb | 2018-03-06 11:48:03 +0000 | [diff] [blame] | 86 | watchdog.Start(); |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 87 | volatile int x = 0; |
| 88 | while (true) { |
| 89 | x++; |
| 90 | } |
| 91 | }, |
| 92 | ""); |
Florian Mayer | b433400 | 2018-02-01 11:10:36 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | } // namespace base |
| 97 | } // namespace perfetto |