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/probes_data_source.h" 33 34 namespace perfetto { 35 36 namespace base { 37 class TaskRunner; 38 } 39 40 namespace protos { 41 namespace pbzero { 42 class SysStats; 43 } 44 } // namespace protos 45 46 class SysStatsDataSource : public ProbesDataSource { 47 public: 48 static const ProbesDataSource::Descriptor descriptor; 49 50 using OpenFunction = base::ScopedFile (*)(const char*); 51 SysStatsDataSource(base::TaskRunner*, 52 TracingSessionID, 53 std::unique_ptr<TraceWriter> writer, 54 const DataSourceConfig&, 55 OpenFunction = nullptr); 56 ~SysStatsDataSource() override; 57 58 // ProbesDataSource implementation. 59 void Start() override; 60 void Flush(FlushRequestID, std::function<void()> callback) override; 61 62 base::WeakPtr<SysStatsDataSource> GetWeakPtr() const; 63 set_ns_per_user_hz_for_testing(uint64_t ns)64 void set_ns_per_user_hz_for_testing(uint64_t ns) { ns_per_user_hz_ = ns; } tick_for_testing()65 uint32_t tick_for_testing() const { return tick_; } 66 67 private: 68 struct CStrCmp { operatorCStrCmp69 bool operator()(const char* a, const char* b) const { 70 return strcmp(a, b) < 0; 71 } 72 }; 73 74 static void Tick(base::WeakPtr<SysStatsDataSource>); 75 76 SysStatsDataSource(const SysStatsDataSource&) = delete; 77 SysStatsDataSource& operator=(const SysStatsDataSource&) = delete; 78 void ReadSysStats(); // Virtual for testing. 79 void ReadMeminfo(protos::pbzero::SysStats* sys_stats); 80 void ReadVmstat(protos::pbzero::SysStats* sys_stats); 81 void ReadStat(protos::pbzero::SysStats* sys_stats); 82 size_t ReadFile(base::ScopedFile*, const char* path); 83 84 base::TaskRunner* const task_runner_; 85 std::unique_ptr<TraceWriter> writer_; 86 base::ScopedFile meminfo_fd_; 87 base::ScopedFile vmstat_fd_; 88 base::ScopedFile stat_fd_; 89 base::PagedMemory read_buf_; 90 TraceWriter::TracePacketHandle cur_packet_; 91 std::map<const char*, int, CStrCmp> meminfo_counters_; 92 std::map<const char*, int, CStrCmp> vmstat_counters_; 93 uint64_t ns_per_user_hz_ = 0; 94 uint32_t tick_ = 0; 95 uint32_t tick_period_ms_ = 0; 96 uint32_t meminfo_ticks_ = 0; 97 uint32_t vmstat_ticks_ = 0; 98 uint32_t stat_ticks_ = 0; 99 uint32_t stat_enabled_fields_ = 0; 100 101 base::WeakPtrFactory<SysStatsDataSource> weak_factory_; // Keep last. 102 }; 103 104 } // namespace perfetto 105 106 #endif // SRC_TRACED_PROBES_SYS_STATS_SYS_STATS_DATA_SOURCE_H_ 107