| 199 | } |
| 200 | |
| 201 | bool ThreadMonitor::read_thread_stat(pid_t tid, long long& utime, long long& stime) { |
| 202 | std::string stat_path = "/proc/self/task/" + std::to_string(tid) + "/stat"; |
| 203 | std::ifstream stat_file(stat_path); |
| 204 | if (!stat_file.is_open()) { |
| 205 | dout(20) << __func__ << "Could not open " << stat_path << dendl; |
| 206 | return false; |
| 207 | } |
| 208 | std::string line; |
| 209 | std::getline(stat_file, line); |
| 210 | size_t start = line.find('('); |
| 211 | size_t end = line.rfind(')'); |
| 212 | if (start == std::string::npos || end == std::string::npos) { |
| 213 | dout(20) << __func__ << "Malformed stat file for TID " << tid << dendl; |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | std::string remainder = line.substr(end + 2); // +2 to skip ") " |
| 218 | std::stringstream ss(remainder); |
| 219 | std::string val; |
| 220 | |
| 221 | // skip 11 fields before utime/stime: |
| 222 | // state ppid pgrp session tty_nr tpgid flags minflt cminflt majflt cmajflt |
| 223 | for (int i = 0; i < 11; ++i) { |
| 224 | ss >> val; |
| 225 | } |
| 226 | ss >> utime >> stime; |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool ThreadMonitor::read_process_statm(long long& rss_pages) { |
| 231 | std::string statm_path = "/proc/self/statm"; |