1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef ATRACE_PROCESS_DUMP_H_ 6 #define ATRACE_PROCESS_DUMP_H_ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <unistd.h> 11 12 #include <memory> 13 #include <set> 14 #include <string> 15 16 #include "logging.h" 17 #include "process_info.h" 18 #include "time_utils.h" 19 20 // Program that collects processes, thread names, per-process memory stats and 21 // other minor metrics from /proc filesystem. It's aimed to extend systrace 22 // with more actionable number to hit performance issues. 23 class AtraceProcessDump { 24 public: 25 enum FullDumpMode { 26 kDisabled, 27 kAllProcesses, 28 kAllJavaApps, 29 kOnlyWhitelisted, 30 }; 31 32 AtraceProcessDump(); 33 ~AtraceProcessDump(); 34 35 void RunAndPrintJson(FILE* stream); 36 void Stop(); 37 38 void SetDumpInterval(int interval_ms); 39 40 // Negative number or zero means unlimited number of dumps. 41 void set_dump_count(int count) { dump_count_ = count; } 42 43 void set_full_dump_mode(FullDumpMode mode) { full_dump_mode_ = mode; } 44 void set_full_dump_whitelist(const std::set<std::string> &whitelist) { 45 CHECK(full_dump_mode_ == FullDumpMode::kOnlyWhitelisted); 46 full_dump_whitelist_ = whitelist; 47 } 48 void enable_graphics_stats() { graphics_stats_ = true; } 49 void enable_print_smaps() { print_smaps_ = true; } 50 51 private: 52 AtraceProcessDump(const AtraceProcessDump&) = delete; 53 void operator=(const AtraceProcessDump&) = delete; 54 55 using ProcessMap = std::map<int, std::unique_ptr<ProcessInfo>>; 56 using ProcessSnapshotMap = std::map<int, std::unique_ptr<ProcessSnapshot>>; 57 58 void TakeGlobalSnapshot(); 59 void TakeAndSerializeMemInfo(); 60 bool UpdatePersistentProcessInfo(int pid); 61 bool ShouldTakeFullDump(const ProcessInfo* process); 62 void SerializeSnapshot(); 63 void SerializePersistentProcessInfo(); 64 void Cleanup(); 65 66 int self_pid_; 67 int dump_count_; 68 bool graphics_stats_ = false; 69 bool print_smaps_ = false; 70 FullDumpMode full_dump_mode_ = FullDumpMode::kDisabled; 71 std::set<std::string> full_dump_whitelist_; 72 73 FILE* out_; 74 ProcessMap processes_; 75 ProcessSnapshotMap snapshot_; 76 uint64_t snapshot_timestamp_; 77 std::set<int> full_dump_whitelisted_pids_; 78 std::unique_ptr<time_utils::PeriodicTimer> snapshot_timer_; 79 int dump_interval_in_timer_ticks_; 80 }; 81 82 #endif // ATRACE_PROCESS_DUMP_H_ 83