process_stats_data_source.h revision 567a5111ff3fad730c1b9c8b1a77a09f187a7385
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_PROCESS_STATS_DATA_SOURCE_H_
18#define SRC_TRACED_PROBES_PROCESS_STATS_DATA_SOURCE_H_
19
20#include <memory>
21#include <set>
22#include <vector>
23
24#include "perfetto/base/weak_ptr.h"
25#include "perfetto/trace/ps/process_tree.pbzero.h"
26#include "perfetto/tracing/core/basic_types.h"
27#include "perfetto/tracing/core/data_source_config.h"
28#include "perfetto/tracing/core/trace_writer.h"
29
30namespace perfetto {
31
32class ProcessStatsDataSource {
33 public:
34  ProcessStatsDataSource(TracingSessionID,
35                         std::unique_ptr<TraceWriter> writer,
36                         const DataSourceConfig&);
37  virtual ~ProcessStatsDataSource();
38
39  TracingSessionID session_id() const { return session_id_; }
40  const DataSourceConfig& config() const { return config_; }
41
42  base::WeakPtr<ProcessStatsDataSource> GetWeakPtr() const;
43  void WriteAllProcesses();
44  void OnPids(const std::vector<int32_t>& pids);
45  void Flush();
46
47  // Virtual for testing.
48  virtual std::string ReadProcPidFile(int32_t pid, const std::string& file);
49
50 private:
51  ProcessStatsDataSource(const ProcessStatsDataSource&) = delete;
52  ProcessStatsDataSource& operator=(const ProcessStatsDataSource&) = delete;
53
54  void WriteProcess(int32_t pid,
55                    const std::string& proc_status,
56                    protos::pbzero::ProcessTree*);
57  void WriteThread(int32_t tid,
58                   int32_t tgid,
59                   const std::string& proc_status,
60                   protos::pbzero::ProcessTree*);
61  void WriteProcessOrThread(int32_t pid, protos::pbzero::ProcessTree*);
62  std::string ReadProcStatusEntry(const std::string& buf, const char* key);
63
64  const TracingSessionID session_id_;
65  std::unique_ptr<TraceWriter> writer_;
66  const DataSourceConfig config_;
67
68  // This set contains PIDs as per the Linux kernel notion of a PID (which is
69  // really a TID). In practice this set will contain all TIDs for all processes
70  // seen, not just the main thread id (aka thread group ID).
71  // TODO(b/76663469): Optimization: use a bitmap.
72  std::set<int32_t> seen_pids_;
73
74  base::WeakPtrFactory<ProcessStatsDataSource> weak_factory_;  // Keep last.
75};
76
77}  // namespace perfetto
78
79#endif  // SRC_TRACED_PROBES_PROCESS_STATS_DATA_SOURCE_H_
80