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