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 
30 namespace perfetto {
31 
32 class 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, const std::string& proc_status);
55   void WriteThread(int32_t tid, int32_t tgid, const std::string& proc_status);
56   void WriteProcessOrThread(int32_t pid);
57   std::string ReadProcStatusEntry(const std::string& buf, const char* key);
58 
59   protos::pbzero::ProcessTree* GetOrCreatePsTree();
60   void FinalizeCurPsTree();
61 
62   const TracingSessionID session_id_;
63   std::unique_ptr<TraceWriter> writer_;
64   const DataSourceConfig config_;
65   TraceWriter::TracePacketHandle cur_packet_;
66   protos::pbzero::ProcessTree* cur_ps_tree_ = nullptr;
67   bool record_thread_names_ = false;
68 
69   // This set contains PIDs as per the Linux kernel notion of a PID (which is
70   // really a TID). In practice this set will contain all TIDs for all processes
71   // seen, not just the main thread id (aka thread group ID).
72   // TODO(b/76663469): Optimization: use a bitmap.
73   std::set<int32_t> seen_pids_;
74 
75   base::WeakPtrFactory<ProcessStatsDataSource> weak_factory_;  // Keep last.
76 };
77 
78 }  // namespace perfetto
79 
80 #endif  // SRC_TRACED_PROBES_PROCESS_STATS_DATA_SOURCE_H_
81