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 SRC_TRACED_PROBES_SYS_STATS_SYS_STATS_DATA_SOURCE_H_ 18 #define SRC_TRACED_PROBES_SYS_STATS_SYS_STATS_DATA_SOURCE_H_ 19 20 #include <string.h> 21 22 #include <map> 23 #include <memory> 24 #include <string> 25 26 #include "perfetto/ext/base/paged_memory.h" 27 #include "perfetto/ext/base/scoped_file.h" 28 #include "perfetto/ext/base/weak_ptr.h" 29 #include "perfetto/ext/tracing/core/basic_types.h" 30 #include "perfetto/ext/tracing/core/trace_writer.h" 31 #include "perfetto/tracing/core/data_source_config.h" 32 #include "src/traced/probes/common/cpu_freq_info.h" 33 #include "src/traced/probes/probes_data_source.h" 34 35 namespace perfetto { 36 37 namespace base { 38 class TaskRunner; 39 } 40 41 namespace protos { 42 namespace pbzero { 43 class SysStats; 44 } 45 } // namespace protos 46 47 class SysStatsDataSource : public ProbesDataSource { 48 public: 49 static const ProbesDataSource::Descriptor descriptor; 50 51 using OpenFunction = base::ScopedFile (*)(const char*); 52 SysStatsDataSource(base::TaskRunner*, 53 TracingSessionID, 54 std::unique_ptr<TraceWriter> writer, 55 const DataSourceConfig&, 56 std::unique_ptr<CpuFreqInfo> cpu_freq_info, 57 OpenFunction = nullptr); 58 ~SysStatsDataSource() override; 59 60 // ProbesDataSource implementation. 61 void Start() override; 62 void Flush(FlushRequestID, std::function<void()> callback) override; 63 64 base::WeakPtr<SysStatsDataSource> GetWeakPtr() const; 65 set_ns_per_user_hz_for_testing(uint64_t ns)66 void set_ns_per_user_hz_for_testing(uint64_t ns) { ns_per_user_hz_ = ns; } tick_for_testing()67 uint32_t tick_for_testing() const { return tick_; } 68 69 // Virtual for testing 70 virtual base::ScopedDir OpenDevfreqDir(); 71 virtual const char* ReadDevfreqCurFreq(const std::string& name); 72 73 private: 74 struct CStrCmp { operatorCStrCmp75 bool operator()(const char* a, const char* b) const { 76 return strcmp(a, b) < 0; 77 } 78 }; 79 80 static void Tick(base::WeakPtr<SysStatsDataSource>); 81 82 SysStatsDataSource(const SysStatsDataSource&) = delete; 83 SysStatsDataSource& operator=(const SysStatsDataSource&) = delete; 84 void ReadSysStats(); // Virtual for testing. 85 void ReadMeminfo(protos::pbzero::SysStats* sys_stats); 86 void ReadVmstat(protos::pbzero::SysStats* sys_stats); 87 void ReadStat(protos::pbzero::SysStats* sys_stats); 88 void ReadDevfreq(protos::pbzero::SysStats* sys_stats); 89 void ReadCpufreq(protos::pbzero::SysStats* sys_stats); 90 size_t ReadFile(base::ScopedFile*, const char* path); 91 92 base::TaskRunner* const task_runner_; 93 std::unique_ptr<TraceWriter> writer_; 94 base::ScopedFile meminfo_fd_; 95 base::ScopedFile vmstat_fd_; 96 base::ScopedFile stat_fd_; 97 base::PagedMemory read_buf_; 98 TraceWriter::TracePacketHandle cur_packet_; 99 std::map<const char*, int, CStrCmp> meminfo_counters_; 100 std::map<const char*, int, CStrCmp> vmstat_counters_; 101 uint64_t ns_per_user_hz_ = 0; 102 uint32_t tick_ = 0; 103 uint32_t tick_period_ms_ = 0; 104 uint32_t meminfo_ticks_ = 0; 105 uint32_t vmstat_ticks_ = 0; 106 uint32_t stat_ticks_ = 0; 107 uint32_t stat_enabled_fields_ = 0; 108 uint32_t devfreq_ticks_ = 0; 109 uint32_t cpufreq_ticks_ = 0; 110 bool devfreq_error_logged_ = false; 111 112 std::unique_ptr<CpuFreqInfo> cpu_freq_info_; 113 114 base::WeakPtrFactory<SysStatsDataSource> weak_factory_; // Keep last. 115 }; 116 117 } // namespace perfetto 118 119 #endif // SRC_TRACED_PROBES_SYS_STATS_SYS_STATS_DATA_SOURCE_H_ 120