xref: /aosp_15_r20/external/perfetto/src/profiling/common/proc_utils.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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_PROFILING_COMMON_PROC_UTILS_H_
18 #define SRC_PROFILING_COMMON_PROC_UTILS_H_
19 
20 #include <sys/types.h>
21 
22 #include <cinttypes>
23 #include <optional>
24 #include <set>
25 #include <vector>
26 
27 #include "perfetto/ext/base/scoped_file.h"
28 
29 namespace perfetto {
30 namespace profiling {
31 
32 struct Uids {
33   uint64_t real;
34   uint64_t effective;
35   uint64_t saved_set;
36   uint64_t filesystem;
37 };
38 
39 template <typename Fn>
ForEachPid(Fn callback)40 void ForEachPid(Fn callback) {
41   base::ScopedDir proc_dir(opendir("/proc"));
42   if (!proc_dir) {
43     PERFETTO_DFATAL_OR_ELOG("Failed to open /proc");
44     return;
45   }
46   struct dirent* entry;
47   while ((entry = readdir(*proc_dir))) {
48     char* end;
49     long int pid = strtol(entry->d_name, &end, 10);
50     if (*end != '\0')
51       continue;
52     callback(static_cast<pid_t>(pid));
53   }
54 }
55 
56 std::optional<std::string> ReadStatus(pid_t pid);
57 std::optional<uint32_t> GetRssAnonAndSwap(const std::string&);
58 // Filters the list of pids (in-place), keeping only the
59 // entries satisfying the minimum size criteria for anonymous memory.
60 void RemoveUnderAnonThreshold(uint32_t min_size_kb, std::set<pid_t>* pids);
61 
62 std::optional<Uids> GetUids(const std::string&);
63 
64 void FindAllProfilablePids(std::set<pid_t>* pids);
65 
66 // TODO(rsavitski): we're changing how the profilers treat proc cmdlines, the
67 // newer semantics are implemented in proc_cmdline.h. Wrappers around those
68 // implementations are placed in the "glob_aware" namespace here, until we
69 // migrate to one implementation for all profilers.
70 ssize_t NormalizeCmdLine(char** cmdline_ptr, size_t size);
71 std::optional<std::vector<std::string>> NormalizeCmdlines(
72     const std::vector<std::string>& cmdlines);
73 void FindPidsForCmdlines(const std::vector<std::string>& cmdlines,
74                          std::set<pid_t>* pids);
75 bool GetCmdlineForPID(pid_t pid, std::string* name);
76 
77 namespace glob_aware {
78 void FindPidsForCmdlinePatterns(const std::vector<std::string>& cmdlines,
79                                 std::set<pid_t>* pids);
80 }  // namespace glob_aware
81 
82 }  // namespace profiling
83 }  // namespace perfetto
84 
85 #endif  // SRC_PROFILING_COMMON_PROC_UTILS_H_
86