|
| 1 | +#include "cpuinfo.h" |
| 2 | +#include <cstdlib> |
| 3 | +#include <dirent.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <fstream> |
| 6 | +#include <unistd.h> |
| 7 | +#include <unordered_set> |
| 8 | +#include <util/string/ascii.h> |
| 9 | +#include <util/string/builder.h> |
| 10 | +#include <util/system/info.h> |
| 11 | + |
| 12 | +size_t NKikimr::RealNumberOfCpus() { |
| 13 | + // copy-pasted from util/system/info.cpp |
| 14 | +#if defined(_linux_) |
| 15 | + unsigned ret; |
| 16 | + int fd, nread, column; |
| 17 | + char buf[512]; |
| 18 | + static const char matchstr[] = "processor\t:"; |
| 19 | + |
| 20 | + fd = open("/proc/cpuinfo", O_RDONLY); |
| 21 | + |
| 22 | + if (fd == -1) { |
| 23 | + abort(); |
| 24 | + } |
| 25 | + |
| 26 | + column = 0; |
| 27 | + ret = 0; |
| 28 | + |
| 29 | + while (true) { |
| 30 | + nread = read(fd, buf, sizeof(buf)); |
| 31 | + |
| 32 | + if (nread <= 0) { |
| 33 | + break; |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = 0; i < nread; ++i) { |
| 37 | + const char ch = buf[i]; |
| 38 | + |
| 39 | + if (ch == '\n') { |
| 40 | + column = 0; |
| 41 | + } else if (column != -1) { |
| 42 | + if (AsciiToLower(ch) == matchstr[column]) { |
| 43 | + ++column; |
| 44 | + |
| 45 | + if (column == sizeof(matchstr) - 1) { |
| 46 | + column = -1; |
| 47 | + ++ret; |
| 48 | + } |
| 49 | + } else { |
| 50 | + column = -1; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (ret == 0) { |
| 57 | + abort(); |
| 58 | + } |
| 59 | + |
| 60 | + close(fd); |
| 61 | + |
| 62 | + return ret; |
| 63 | +#else |
| 64 | + return NSystemInfo::NumberOfCpus(); |
| 65 | +#endif |
| 66 | +} |
| 67 | + |
| 68 | +double TicksPerSec() { |
| 69 | +#ifdef _SC_CLK_TCK |
| 70 | + return sysconf(_SC_CLK_TCK); |
| 71 | +#else |
| 72 | + return 1; |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | +std::vector<NKikimr::TSystemThreadsMonitor::TSystemThreadPoolInfo> NKikimr::TSystemThreadsMonitor::GetThreadPools(TInstant now) { |
| 77 | + UpdateSystemThreads(getpid()); |
| 78 | + std::vector<TSystemThreadPoolInfo> result; |
| 79 | + result.reserve(NameToThreadIndex.size()); |
| 80 | + double passedSeconds = (now - UpdateTime).SecondsFloat(); |
| 81 | + double ticks = TicksPerSec() * passedSeconds; |
| 82 | + for (const auto& [name, tids] : NameToThreadIndex) { |
| 83 | + TSystemThreadPoolInfo& info = result.emplace_back(); |
| 84 | + info.Name = name; |
| 85 | + info.Threads = tids.size(); |
| 86 | + ui64 majorPageFaults = 0; |
| 87 | + ui64 minorPageFaults = 0; |
| 88 | + ui64 systemTime = 0; |
| 89 | + ui64 userTime = 0; |
| 90 | + std::array<ui16, 256> states{}; |
| 91 | + for (pid_t tid : tids) { |
| 92 | + const TSystemThreadPoolState& state = SystemThreads[tid]; |
| 93 | + majorPageFaults += state.DeltaMajorPageFaults; |
| 94 | + minorPageFaults += state.DeltaMinorPageFaults; |
| 95 | + systemTime += state.DeltaSystemTime; |
| 96 | + userTime += state.DeltaUserTime; |
| 97 | + if (state.State >= 'A' && state.State <= 'z') { |
| 98 | + states[size_t(state.State)]++; |
| 99 | + } |
| 100 | + } |
| 101 | + for (char c = 'A'; c <= 'z'; ++c) { |
| 102 | + if (states[size_t(c)] > 0) { |
| 103 | + info.States.emplace_back(c, states[c]); |
| 104 | + } |
| 105 | + } |
| 106 | + info.MajorPageFaults = double(majorPageFaults) / passedSeconds; |
| 107 | + info.MinorPageFaults = double(minorPageFaults) / passedSeconds; |
| 108 | + info.SystemUsage = double(systemTime) / ticks / info.Threads; |
| 109 | + info.UserUsage = double(userTime) / ticks / info.Threads; |
| 110 | + } |
| 111 | + UpdateTime = now; |
| 112 | + return result; |
| 113 | +} |
| 114 | + |
| 115 | +void NKikimr::TSystemThreadsMonitor::UpdateSystemThreads(pid_t pid) { |
| 116 | +#if defined(_linux_) |
| 117 | + NameToThreadIndex.clear(); |
| 118 | + std::string taskDir = "/proc/" + std::to_string(pid) + "/task"; |
| 119 | + DIR* dir = opendir(taskDir.c_str()); |
| 120 | + if (!dir) { |
| 121 | + SystemThreads.clear(); |
| 122 | + return; |
| 123 | + } |
| 124 | + std::unordered_set<pid_t> currentThreads; |
| 125 | + currentThreads.reserve(SystemThreads.size()); |
| 126 | + for (const auto& [tid, state] : SystemThreads) { |
| 127 | + currentThreads.insert(tid); |
| 128 | + } |
| 129 | + struct dirent* entry; |
| 130 | + while ((entry = readdir(dir)) != nullptr) { |
| 131 | + if (entry->d_name[0] == '.') { |
| 132 | + continue; // skip '.' and '..' |
| 133 | + } |
| 134 | + pid_t tid = atoi(entry->d_name); |
| 135 | + if (tid > 0) { |
| 136 | + UpdateSystemThread(pid, tid); |
| 137 | + currentThreads.erase(tid); |
| 138 | + } |
| 139 | + } |
| 140 | + for (pid_t tid : currentThreads) { |
| 141 | + SystemThreads.erase(tid); |
| 142 | + } |
| 143 | + closedir(dir); |
| 144 | +#else |
| 145 | + Y_UNUSED(pid); |
| 146 | +#endif |
| 147 | +} |
| 148 | + |
| 149 | +void NKikimr::TSystemThreadsMonitor::UpdateSystemThread(pid_t pid, pid_t tid) { |
| 150 | +#if defined(_linux_) |
| 151 | + TSystemThreadPoolState& state = SystemThreads[tid]; |
| 152 | + std::ifstream file("/proc/" + std::to_string(pid) + "/task/" + std::to_string(tid) + "/stat"); |
| 153 | + if (file) { |
| 154 | + std::string line; |
| 155 | + std::getline(file, line); |
| 156 | + size_t name_start = line.find('('); |
| 157 | + size_t name_end = line.find(')', name_start); |
| 158 | + if (name_start == std::string::npos || name_end == std::string::npos) { |
| 159 | + return; |
| 160 | + } |
| 161 | + std::string threadName = line.substr(name_start + 1, name_end - name_start - 1); |
| 162 | + std::istringstream iss(line.substr(name_end + 2)); |
| 163 | + std::string ignore; |
| 164 | + ui64 majorPageFaults; |
| 165 | + ui64 minorPageFaults; |
| 166 | + ui64 systemTime; |
| 167 | + ui64 userTime; |
| 168 | + iss >> state.State >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore |
| 169 | + >> minorPageFaults >> ignore >> majorPageFaults >> ignore >> userTime >> systemTime; |
| 170 | + state.DeltaMajorPageFaults = majorPageFaults - state.MajorPageFaults; |
| 171 | + state.DeltaMinorPageFaults = minorPageFaults - state.MinorPageFaults; |
| 172 | + state.DeltaSystemTime = systemTime - state.SystemTime; |
| 173 | + state.DeltaUserTime = userTime - state.UserTime; |
| 174 | + state.MajorPageFaults = majorPageFaults; |
| 175 | + state.MinorPageFaults = minorPageFaults; |
| 176 | + state.SystemTime = systemTime; |
| 177 | + state.UserTime = userTime; |
| 178 | + //Cerr << threadName << " " << state.SystemTime << " " << state.UserTime << Endl; |
| 179 | + std::string name = GetNormalizedThreadName(threadName); |
| 180 | + NameToThreadIndex[name].push_back(tid); |
| 181 | + } |
| 182 | +#else |
| 183 | + Y_UNUSED(tid); |
| 184 | +#endif |
| 185 | +} |
| 186 | + |
| 187 | +std::string NKikimr::TSystemThreadsMonitor::GetNormalizedThreadName(const std::string& name) { |
| 188 | + std::string result = name; |
| 189 | + while (!result.empty() && (isdigit(result.back()) || result.back() == '-' || result.back() == ' ')) { |
| 190 | + result.resize(result.size() - 1); |
| 191 | + } |
| 192 | + if (result.empty()) { |
| 193 | + return name; |
| 194 | + } |
| 195 | + // some well-known hacks |
| 196 | + if (result == "default-executo") { |
| 197 | + result = "default-executor"; |
| 198 | + } |
| 199 | + if (result == "resolver-execut") { |
| 200 | + result = "resolver-executor"; |
| 201 | + } |
| 202 | + if (result == "grpc_global_tim") { |
| 203 | + result = "grpc_global_timer"; |
| 204 | + } |
| 205 | + if (result == "kikimr.Schedule") { |
| 206 | + result = "kikimr.Scheduler"; |
| 207 | + } |
| 208 | + return result; |
| 209 | +} |
0 commit comments