Skip to content

Commit 29eb3da

Browse files
alexd65536qrort
authored andcommitted
support cgroups v2 in process stats (#17232)
1 parent 271d367 commit 29eb3da

File tree

1 file changed

+72
-26
lines changed

1 file changed

+72
-26
lines changed

ydb/library/actors/core/process_stats.cpp

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ namespace NActors {
4747
}
4848

4949
bool TProcStat::Fill(pid_t pid) {
50-
try {
51-
TString strPid(ToString(pid));
52-
TString line;
53-
TVector<TString> fields;
50+
TString strPid(ToString(pid));
51+
TString line;
5452

53+
try {
5554
TFileInput proc("/proc/" + strPid + "/status");
5655
while (proc.ReadLine(line)) {
5756
if (ExtractVal(line, "VmRSS:", Rss))
@@ -63,8 +62,21 @@ namespace NActors {
6362
}
6463
ConvertFromKb(Rss);
6564

66-
float tickPerMillisec = TicksPerMillisec();
65+
} catch (...) {
66+
}
67+
68+
Vsize = 0;
69+
Utime = 0;
70+
Stime = 0;
71+
MinFlt = 0;
72+
MajFlt = 0;
73+
SystemUptime = {};
74+
Uptime = {};
75+
NumThreads = 0;
76+
77+
float ticksPerMillisec = TicksPerMillisec();
6778

79+
try {
6880
TFileInput procStat("/proc/" + strPid + "/stat");
6981
if (procStat.ReadLine(line)) {
7082
sscanf(line.data(),
@@ -74,14 +86,20 @@ namespace NActors {
7486
&Pid, &State, &Ppid, &Pgrp, &Session, &TtyNr, &TPgid, &Flags, &MinFlt, &CMinFlt,
7587
&MajFlt, &CMajFlt, &Utime, &Stime, &CUtime, &CStime, &Priority, &Nice, &NumThreads,
7688
&ItRealValue, &StartTime, &Vsize, &RssPages, &RssLim);
77-
Utime /= tickPerMillisec;
78-
Stime /= tickPerMillisec;
79-
CUtime /= tickPerMillisec;
80-
CStime /= tickPerMillisec;
81-
SystemUptime = ::Uptime();
82-
Uptime = SystemUptime - TDuration::MilliSeconds(StartTime / TicksPerMillisec());
89+
Utime /= ticksPerMillisec;
90+
Stime /= ticksPerMillisec;
91+
CUtime /= ticksPerMillisec;
92+
CStime /= ticksPerMillisec;
8393
}
94+
SystemUptime = ::Uptime();
95+
Uptime = SystemUptime - TDuration::MilliSeconds(StartTime / ticksPerMillisec);
96+
} catch (...) {
97+
}
98+
99+
FileRss = 0;
100+
AnonRss = 0;
84101

102+
try {
85103
TFileInput statm("/proc/" + strPid + "/statm");
86104
if (statm.ReadLine(line)) {
87105
TVector<TString> fields;
@@ -96,30 +114,58 @@ namespace NActors {
96114
AnonRss = (resident - shared) * PageSize;
97115
}
98116
}
117+
} catch (...) {
118+
}
119+
120+
CGroupMemLim = 0;
121+
122+
try {
123+
bool isV2 = NFs::Exists("/sys/fs/cgroup/cgroup.controllers");
99124

100125
TFileInput cgroup("/proc/" + strPid + "/cgroup");
101126
TString memoryCGroup;
102127
while (cgroup.ReadLine(line)) {
128+
TVector<TString> fields;
103129
StringSplitter(line).Split(':').Collect(&fields);
104-
if (fields.size() > 2 && fields[1] == "memory") {
105-
memoryCGroup = fields[2];
106-
break;
130+
if (fields.size() <= 2) {
131+
continue;
132+
}
133+
if (isV2) {
134+
if (fields[0] == "0") {
135+
memoryCGroup = fields[2];
136+
break;
137+
}
138+
} else {
139+
if (fields[1] == "memory") {
140+
memoryCGroup = fields[2];
141+
break;
142+
}
107143
}
108144
}
109145

110-
TString cgroupFileName = "/sys/fs/cgroup/memory" + memoryCGroup + "/memory.limit_in_bytes";
111-
if (!NFs::Exists(cgroupFileName)) {
112-
// fallback for mk8s
113-
cgroupFileName = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
114-
}
115-
TFileInput limit(cgroupFileName);
116-
if (limit.ReadLine(line) > 0) {
117-
CGroupMemLim = FromString<ui64>(line);
118-
if (CGroupMemLim > (1ULL << 40)) {
119-
CGroupMemLim = 0;
146+
if (!memoryCGroup.empty() && memoryCGroup != "/") {
147+
TString cgroupFileName;
148+
if (isV2) {
149+
cgroupFileName = "/sys/fs/cgroup" + memoryCGroup + "/memory.max";
150+
} else {
151+
cgroupFileName = "/sys/fs/cgroup/memory" + memoryCGroup + "/memory.limit_in_bytes";
152+
// fallback for mk8s
153+
if (!NFs::Exists(cgroupFileName)) {
154+
cgroupFileName = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
155+
}
156+
}
157+
TFileInput limit(cgroupFileName);
158+
if (limit.ReadLine(line) && line != "max") {
159+
CGroupMemLim = FromString<ui64>(line);
160+
if (CGroupMemLim > (1ULL << 40)) {
161+
CGroupMemLim = 0;
162+
}
120163
}
121164
}
165+
} catch (...) {
166+
}
122167

168+
try {
123169
TFileInput memInfo("/proc/meminfo");
124170
while (memInfo.ReadLine(line)) {
125171
if (ExtractVal(line, "MemTotal:", MemTotal))
@@ -130,8 +176,8 @@ namespace NActors {
130176
ConvertFromKb(MemTotal);
131177
ConvertFromKb(MemAvailable);
132178
} catch (...) {
133-
return false;
134179
}
180+
135181
return true;
136182
}
137183

@@ -233,7 +279,7 @@ namespace {
233279
*MajorPageFaults = procStat.MajFlt;
234280
*UptimeSeconds = procStat.Uptime.Seconds();
235281
*NumThreads = procStat.NumThreads;
236-
*SystemUptimeSeconds = procStat.Uptime.Seconds();
282+
*SystemUptimeSeconds = procStat.SystemUptime.Seconds();
237283
}
238284

239285
private:

0 commit comments

Comments
 (0)