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/base/paged_memory.h" 27 #include "perfetto/base/scoped_file.h" 28 #include "perfetto/base/weak_ptr.h" 29 #include "perfetto/tracing/core/basic_types.h" 30 #include "perfetto/tracing/core/data_source_config.h" 31 #include "perfetto/tracing/core/trace_writer.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 constexpr int kTypeId = 4; 49 using OpenFunction = base::ScopedFile (*)(const char*); 50 SysStatsDataSource(base::TaskRunner*, 51 TracingSessionID, 52 std::unique_ptr<TraceWriter> writer, 53 const DataSourceConfig&, 54 OpenFunction = nullptr); 55 ~SysStatsDataSource() override; 56 57 // ProbesDataSource implementation. 58 void Start() override; 59 void Flush(FlushRequestID, std::function<void()> callback) override; 60 61 base::WeakPtr<SysStatsDataSource> GetWeakPtr() const; 62 set_ns_per_user_hz_for_testing(uint64_t ns)63 void set_ns_per_user_hz_for_testing(uint64_t ns) { ns_per_user_hz_ = ns; } tick_for_testing()64 uint32_t tick_for_testing() const { return tick_; } 65 66 private: 67 struct CStrCmp { operatorCStrCmp68 bool operator()(const char* a, const char* b) const { 69 return strcmp(a, b) < 0; 70 } 71 }; 72 73 static void Tick(base::WeakPtr<SysStatsDataSource>); 74 75 SysStatsDataSource(const SysStatsDataSource&) = delete; 76 SysStatsDataSource& operator=(const SysStatsDataSource&) = delete; 77 void ReadSysStats(); // Virtual for testing. 78 void ReadMeminfo(protos::pbzero::SysStats* sys_stats); 79 void ReadVmstat(protos::pbzero::SysStats* sys_stats); 80 void ReadStat(protos::pbzero::SysStats* sys_stats); 81 size_t ReadFile(base::ScopedFile*, const char* path); 82 83 base::TaskRunner* const task_runner_; 84 std::unique_ptr<TraceWriter> writer_; 85 base::ScopedFile meminfo_fd_; 86 base::ScopedFile vmstat_fd_; 87 base::ScopedFile stat_fd_; 88 base::PagedMemory read_buf_; 89 TraceWriter::TracePacketHandle cur_packet_; 90 std::map<const char*, int, CStrCmp> meminfo_counters_; 91 std::map<const char*, int, CStrCmp> vmstat_counters_; 92 uint64_t ns_per_user_hz_ = 0; 93 uint32_t tick_ = 0; 94 uint32_t tick_period_ms_ = 0; 95 uint32_t meminfo_ticks_ = 0; 96 uint32_t vmstat_ticks_ = 0; 97 uint32_t stat_ticks_ = 0; 98 uint32_t stat_enabled_fields_ = 0; 99 100 base::WeakPtrFactory<SysStatsDataSource> weak_factory_; // Keep last. 101 }; 102 103 } // namespace perfetto 104 105 #endif // SRC_TRACED_PROBES_SYS_STATS_SYS_STATS_DATA_SOURCE_H_ 106