• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Virtual for testing
68   virtual base::ScopedDir OpenDevfreqDir();
69   virtual const char* ReadDevfreqCurFreq(const std::string& name);
70 
71  private:
72   struct CStrCmp {
operatorCStrCmp73     bool operator()(const char* a, const char* b) const {
74       return strcmp(a, b) < 0;
75     }
76   };
77 
78   static void Tick(base::WeakPtr<SysStatsDataSource>);
79 
80   SysStatsDataSource(const SysStatsDataSource&) = delete;
81   SysStatsDataSource& operator=(const SysStatsDataSource&) = delete;
82   void ReadSysStats();  // Virtual for testing.
83   void ReadMeminfo(protos::pbzero::SysStats* sys_stats);
84   void ReadVmstat(protos::pbzero::SysStats* sys_stats);
85   void ReadStat(protos::pbzero::SysStats* sys_stats);
86   void ReadDevfreq(protos::pbzero::SysStats* sys_stats);
87   size_t ReadFile(base::ScopedFile*, const char* path);
88 
89   base::TaskRunner* const task_runner_;
90   std::unique_ptr<TraceWriter> writer_;
91   base::ScopedFile meminfo_fd_;
92   base::ScopedFile vmstat_fd_;
93   base::ScopedFile stat_fd_;
94   base::PagedMemory read_buf_;
95   TraceWriter::TracePacketHandle cur_packet_;
96   std::map<const char*, int, CStrCmp> meminfo_counters_;
97   std::map<const char*, int, CStrCmp> vmstat_counters_;
98   uint64_t ns_per_user_hz_ = 0;
99   uint32_t tick_ = 0;
100   uint32_t tick_period_ms_ = 0;
101   uint32_t meminfo_ticks_ = 0;
102   uint32_t vmstat_ticks_ = 0;
103   uint32_t stat_ticks_ = 0;
104   uint32_t stat_enabled_fields_ = 0;
105   uint32_t devfreq_ticks_ = 0;
106   bool devfreq_error_logged_ = false;
107 
108   base::WeakPtrFactory<SysStatsDataSource> weak_factory_;  // Keep last.
109 };
110 
111 }  // namespace perfetto
112 
113 #endif  // SRC_TRACED_PROBES_SYS_STATS_SYS_STATS_DATA_SOURCE_H_
114