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 #ifndef SRC_PROFILING_COMMON_PROFILER_GUARDRAILS_H_ 18 #define SRC_PROFILING_COMMON_PROFILER_GUARDRAILS_H_ 19 20 #include <fcntl.h> 21 #include <unistd.h> 22 23 #include <cinttypes> 24 #include <optional> 25 26 #include "perfetto/ext/base/file_utils.h" 27 #include "perfetto/ext/base/scoped_file.h" 28 #include "perfetto/ext/tracing/core/basic_types.h" 29 #include "src/profiling/common/proc_utils.h" 30 31 namespace perfetto { 32 namespace profiling { 33 34 std::optional<uint64_t> GetCputimeSecForCurrentProcess(); 35 // For testing. 36 std::optional<uint64_t> GetCputimeSecForCurrentProcess( 37 base::ScopedFile stat_fd); 38 39 struct GuardrailConfig { 40 uint64_t cpu_guardrail_sec = 0; 41 std::optional<uint64_t> cpu_start_secs; 42 uint32_t memory_guardrail_kb = 0; 43 }; 44 45 class ProfilerCpuGuardrails { 46 public: 47 ProfilerCpuGuardrails(); 48 // Allows to supply custom stat fd for testing. 49 explicit ProfilerCpuGuardrails(base::ScopedFile stat_fd); 50 51 bool IsOverCpuThreshold(const GuardrailConfig& ds); 52 53 private: 54 std::optional<uint64_t> opt_cputime_sec_; 55 }; 56 57 class ProfilerMemoryGuardrails { 58 public: 59 ProfilerMemoryGuardrails(); 60 // Allows to supply custom status fd for testing. 61 explicit ProfilerMemoryGuardrails(base::ScopedFile status_fd); 62 63 bool IsOverMemoryThreshold(const GuardrailConfig& ds); 64 65 private: 66 std::optional<uint32_t> anon_and_swap_; 67 }; 68 69 } // namespace profiling 70 } // namespace perfetto 71 72 #endif // SRC_PROFILING_COMMON_PROFILER_GUARDRAILS_H_ 73