• 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_MEMORY_PROC_UTILS_H_
18 #define SRC_PROFILING_MEMORY_PROC_UTILS_H_
19 
20 #include <sys/types.h>
21 #include <set>
22 #include <vector>
23 
24 #include "perfetto/base/scoped_file.h"
25 
26 namespace perfetto {
27 namespace profiling {
28 
29 template <typename Fn>
ForEachPid(Fn callback)30 void ForEachPid(Fn callback) {
31   base::ScopedDir proc_dir(opendir("/proc"));
32   if (!proc_dir) {
33     PERFETTO_DFATAL("Failed to open /proc");
34     return;
35   }
36   struct dirent* entry;
37   while ((entry = readdir(*proc_dir))) {
38     char* end;
39     long int pid = strtol(entry->d_name, &end, 10);
40     if (*end != '\0')
41       continue;
42     callback(static_cast<pid_t>(pid));
43   }
44 }
45 
46 bool NormalizeCmdLine(char* cmdline, size_t size, std::string* name);
47 std::vector<std::string> NormalizeCmdlines(
48     const std::vector<std::string>& cmdlines);
49 
50 void FindAllProfilablePids(std::set<pid_t>* pids);
51 void FindPidsForCmdlines(const std::vector<std::string>& cmdlines,
52                          std::set<pid_t>* pids);
53 bool GetCmdlineForPID(pid_t pid, std::string* name);
54 
55 }  // namespace profiling
56 }  // namespace perfetto
57 
58 #endif  // SRC_PROFILING_MEMORY_PROC_UTILS_H_
59