• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020, 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 #include "ProcPidDir.h"
18 
19 #include "UidProcStatsCollector.h"
20 
21 #include <android-base/file.h>
22 #include <android-base/result.h>
23 
24 #include <errno.h>
25 
26 namespace android {
27 namespace automotive {
28 namespace watchdog {
29 namespace testing {
30 
31 using ::android::base::Error;
32 using ::android::base::Result;
33 using ::android::base::WriteStringToFile;
34 
35 namespace {
36 
makeDir(std::string path)37 Result<void> makeDir(std::string path) {
38     if (mkdir(path.c_str(), 0700) && errno != EEXIST) {
39         return Error() << "Could not mkdir " << path << ": " << strerror(errno);
40     }
41     return {};
42 }
43 
44 }  // namespace
45 
populateProcPidDir(const std::string & procDirPath,const std::unordered_map<pid_t,std::vector<pid_t>> & pidToTids,const std::unordered_map<pid_t,std::string> & processStat,const std::unordered_map<pid_t,std::string> & processStatus,const std::unordered_map<pid_t,std::string> & threadStat,const std::unordered_map<pid_t,std::string> & threadTimeInState)46 Result<void> populateProcPidDir(const std::string& procDirPath,
47                                 const std::unordered_map<pid_t, std::vector<pid_t>>& pidToTids,
48                                 const std::unordered_map<pid_t, std::string>& processStat,
49                                 const std::unordered_map<pid_t, std::string>& processStatus,
50                                 const std::unordered_map<pid_t, std::string>& threadStat,
51                                 const std::unordered_map<pid_t, std::string>& threadTimeInState) {
52     for (const auto& it : pidToTids) {
53         // 1. Create /proc/PID dir.
54         const auto& pidDirRes = makeDir(StringPrintf("%s/%" PRIu32, procDirPath.c_str(), it.first));
55         if (!pidDirRes.ok()) {
56             return Error() << "Failed to create top-level per-process directory: "
57                            << pidDirRes.error();
58         }
59 
60         // 2. Create /proc/PID/stat file.
61         uint32_t pid = it.first;
62         if (processStat.find(pid) != processStat.end()) {
63             std::string path = StringPrintf((procDirPath + kStatFileFormat).c_str(), pid);
64             if (!WriteStringToFile(processStat.at(pid), path)) {
65                 return Error() << "Failed to write pid stat file " << path;
66             }
67         }
68 
69         // 3. Create /proc/PID/status file.
70         if (processStatus.find(pid) != processStatus.end()) {
71             std::string path = StringPrintf((procDirPath + kStatusFileFormat).c_str(), pid);
72             if (!WriteStringToFile(processStatus.at(pid), path)) {
73                 return Error() << "Failed to write pid status file " << path;
74             }
75         }
76 
77         // 4. Create /proc/PID/task dir.
78         const auto& taskDirRes = makeDir(StringPrintf((procDirPath + kTaskDirFormat).c_str(), pid));
79         if (!taskDirRes.ok()) {
80             return Error() << "Failed to create task directory: " << taskDirRes.error();
81         }
82 
83         // 5. Create /proc/PID/task/TID dirs, /proc/PID/task/TID/stat and
84         //    /proc/PID/task/TID/time_in_state files.
85         for (const auto& tid : it.second) {
86             const auto& tidDirRes = makeDir(
87                     StringPrintf((procDirPath + kTaskDirFormat + "/%" PRIu32).c_str(), pid, tid));
88             if (!tidDirRes.ok()) {
89                 return Error() << "Failed to create per-thread directory: " << tidDirRes.error();
90             }
91             if (threadStat.find(tid) != threadStat.end()) {
92                 std::string path =
93                         StringPrintf((procDirPath + kTaskDirFormat + kStatFileFormat).c_str(), pid,
94                                      tid);
95                 if (!WriteStringToFile(threadStat.at(tid), path)) {
96                     return Error() << "Failed to write thread stat file " << path;
97                 }
98             }
99             if (threadTimeInState.find(tid) != threadTimeInState.end()) {
100                 std::string path =
101                         StringPrintf((procDirPath + kTaskDirFormat + kTimeInStateFormat).c_str(),
102                                      pid, tid);
103                 if (!WriteStringToFile(threadTimeInState.at(tid), path)) {
104                     return Error() << "Failed to write thread time_in_state file " << path;
105                 }
106             }
107         }
108     }
109 
110     return {};
111 }
112 
113 }  // namespace testing
114 }  // namespace watchdog
115 }  // namespace automotive
116 }  // namespace android
117