| Andy Hung | afc51db | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #define LOG_TAG "Process" |
| 18 | #include <utils/Log.h> |
| 19 | #include <mediautils/Process.h> |
| 20 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <android-base/strings.h> |
| 23 | #include <cstdlib> |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | void processLine(std::string_view s, std::map<std::string, double>& m) { |
| 28 | if (s.empty()) return; |
| 29 | |
| 30 | const size_t colon_pos = s.find(':'); |
| 31 | if (colon_pos == std::string_view::npos) return; |
| 32 | |
| 33 | const size_t space_pos = s.find(' '); |
| 34 | if (space_pos == 0 || space_pos == std::string_view::npos || space_pos > colon_pos) return; |
| 35 | std::string key(s.data(), s.data() + space_pos); |
| 36 | |
| 37 | const size_t value_pos = s.find_first_not_of(' ', colon_pos + 1); |
| 38 | if (value_pos == std::string_view::npos) return; |
| 39 | |
| 40 | const double value = strtod(s.data() + value_pos, nullptr /* end */); |
| 41 | m[std::move(key)] = value; |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | namespace android::mediautils { |
| 47 | |
| 48 | std::string getThreadSchedAsString(pid_t tid) { |
| 49 | const pid_t pid = getpid(); |
| 50 | const std::string path = std::string("/proc/").append(std::to_string(pid)) |
| 51 | .append("/task/").append(std::to_string(tid)).append("/sched"); |
| 52 | std::string sched; |
| 53 | (void)android::base::ReadFileToString(path.c_str(), &sched); |
| 54 | return sched; |
| 55 | } |
| 56 | |
| 57 | std::map<std::string, double> parseThreadSchedString(const std::string& schedString) { |
| 58 | std::map<std::string, double> m; |
| 59 | if (schedString.empty()) return m; |
| 60 | std::vector<std::string> stringlist = android::base::Split(schedString, "\n"); |
| 61 | |
| 62 | // OK we use values not strings... m["summary"] = stringlist[0]; |
| 63 | for (size_t i = 2; i < stringlist.size(); ++i) { |
| 64 | processLine(stringlist[i], m); |
| 65 | } |
| 66 | return m; |
| 67 | } |
| 68 | |
| 69 | } // namespace android::mediautils |