• 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_PROFILING_COMMON_PROC_UTILS_H_
18 #define SRC_PROFILING_COMMON_PROC_UTILS_H_
19 
20 #include <inttypes.h>
21 #include <sys/types.h>
22 
23 #include <set>
24 #include <vector>
25 
26 #include "perfetto/ext/base/optional.h"
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 base::Optional<std::vector<std::string>> NormalizeCmdlines(
57     const std::vector<std::string>& cmdlines);
58 
59 void FindAllProfilablePids(std::set<pid_t>* pids);
60 void FindPidsForCmdlines(const std::vector<std::string>& cmdlines,
61                          std::set<pid_t>* pids);
62 bool GetCmdlineForPID(pid_t pid, std::string* name);
63 
64 base::Optional<std::string> ReadStatus(pid_t pid);
65 base::Optional<uint32_t> GetRssAnonAndSwap(const std::string&);
66 // Filters the list of pids (in-place), keeping only the
67 // entries satisfying the minimum size criteria for anonymous memory.
68 void RemoveUnderAnonThreshold(uint32_t min_size_kb, std::set<pid_t>* pids);
69 
70 base::Optional<Uids> GetUids(const std::string&);
71 
72 }  // namespace profiling
73 }  // namespace perfetto
74 
75 #endif  // SRC_PROFILING_COMMON_PROC_UTILS_H_
76