• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_H_
19 
20 #include <functional>
21 
22 #include "perfetto/base/build_config.h"
23 
24 // The POSIX watchdog is only supported on Linux and Android in non-embedder
25 // builds.
26 #if PERFETTO_BUILDFLAG(PERFETTO_WATCHDOG)
27 #include "perfetto/ext/base/watchdog_posix.h"
28 #else
29 #include "perfetto/ext/base/watchdog_noop.h"
30 #endif
31 
32 namespace perfetto {
33 namespace base {
34 
35 // Make the limits more relaxed on desktop, where multi-GB traces are likely.
36 // Multi-GB traces can take bursts of cpu time to write into disk at the end of
37 // the trace.
38 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
39 constexpr uint32_t kWatchdogDefaultCpuLimit = 75;
40 constexpr uint32_t kWatchdogDefaultCpuWindow = 5 * 60 * 1000;  // 5 minutes.
41 #else
42 constexpr uint32_t kWatchdogDefaultCpuLimit = 90;
43 constexpr uint32_t kWatchdogDefaultCpuWindow = 10 * 60 * 1000;  // 10 minutes.
44 #endif
45 
46 // The default memory margin we give to our processes. This is used as as a
47 // constant to put on top of the trace buffers.
48 constexpr uint64_t kWatchdogDefaultMemorySlack = 32 * 1024 * 1024;  // 32 MiB.
49 constexpr uint32_t kWatchdogDefaultMemoryWindow = 30 * 1000;  // 30 seconds.
50 
RunTaskWithWatchdogGuard(const std::function<void ()> & task)51 inline void RunTaskWithWatchdogGuard(const std::function<void()>& task) {
52   // Maximum time a single task can take in a TaskRunner before the
53   // program suicides.
54   constexpr int64_t kWatchdogMillis = 30000;  // 30s
55 
56   Watchdog::Timer handle =
57       base::Watchdog::GetInstance()->CreateFatalTimer(kWatchdogMillis);
58   task();
59 
60   // Suppress unused variable warnings in the client library amalgamated build.
61   (void)kWatchdogDefaultCpuLimit;
62   (void)kWatchdogDefaultCpuWindow;
63   (void)kWatchdogDefaultMemorySlack;
64   (void)kWatchdogDefaultMemoryWindow;
65 }
66 
67 }  // namespace base
68 }  // namespace perfetto
69 
70 #endif  // INCLUDE_PERFETTO_EXT_BASE_WATCHDOG_H_
71