Skip to content

Commit d09276d

Browse files
committed
YARN-11687. Update CGroupsResourceCalculator to track usages using cgroupv2
- unify common code between v1 and v2
1 parent 72fcc8e commit d09276d

File tree

3 files changed

+78
-68
lines changed

3 files changed

+78
-68
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources;
20+
21+
import org.apache.hadoop.classification.VisibleForTesting;
22+
import org.apache.hadoop.util.CpuTimeTracker;
23+
import org.apache.hadoop.util.SysInfoLinux;
24+
import org.apache.hadoop.yarn.util.Clock;
25+
import org.apache.hadoop.yarn.util.ResourceCalculatorProcessTree;
26+
27+
/**
28+
* Common code base for the CGroupsResourceCalculator implementations.
29+
*/
30+
public abstract class AbstractCGroupsResourceCalculator extends ResourceCalculatorProcessTree {
31+
protected final String pid;
32+
protected Clock clock;
33+
34+
@VisibleForTesting
35+
long jiffyLengthMs = SysInfoLinux.JIFFY_LENGTH_IN_MILLIS;
36+
@VisibleForTesting
37+
CpuTimeTracker cpuTimeTracker;
38+
@VisibleForTesting
39+
CGroupsHandler cGroupsHandler;
40+
41+
/**
42+
* Create resource calculator for the container that has the specified pid.
43+
* @param pid A pid from the cgroup or null for all containers
44+
*/
45+
public AbstractCGroupsResourceCalculator(String pid) {
46+
super(pid);
47+
this.pid = pid;
48+
}
49+
50+
@Override
51+
public String getProcessTreeDump() {
52+
// We do not have a process tree in cgroups return just the pid for tracking
53+
return pid;
54+
}
55+
56+
@Override
57+
public boolean checkPidPgrpidForMatch() {
58+
// We do not have a process tree in cgroups returning default ok
59+
return true;
60+
}
61+
62+
@Override
63+
public float getCpuUsagePercent() {
64+
return cpuTimeTracker.getCpuTrackerUsagePercent();
65+
}
66+
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/CGroupsResourceCalculator.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* If the real virtual memory is required please use the legacy procfs based
5959
* resource calculator or CombinedResourceCalculator.
6060
*/
61-
public class CGroupsResourceCalculator extends ResourceCalculatorProcessTree {
61+
public class CGroupsResourceCalculator extends AbstractCGroupsResourceCalculator {
6262
enum Result {
6363
Continue,
6464
Exit
@@ -76,9 +76,7 @@ enum Result {
7676
private static final Pattern CGROUP_FILE_FORMAT = Pattern.compile(
7777
"^(\\d+):([^:]+):/(.*)$");
7878
private final String procfsDir;
79-
private CGroupsHandler cGroupsHandler;
8079

81-
private String pid;
8280
private File cpuStat;
8381
private File memStat;
8482
private File memswStat;
@@ -87,10 +85,6 @@ enum Result {
8785
private long processPhysicalMemory;
8886
private long processVirtualMemory;
8987

90-
private final long jiffyLengthMs;
91-
private final CpuTimeTracker cpuTimeTracker;
92-
private Clock clock;
93-
9488
/**
9589
* Create resource calculator for all Yarn containers.
9690
*/
@@ -122,10 +116,9 @@ public CGroupsResourceCalculator(String pid) {
122116
CGroupsHandler cGroupsHandler,
123117
Clock clock,
124118
long jiffyLengthMs0) {
125-
super(pid);
119+
super(pid != null && pid.equals("0") ? "1" : pid);
126120
this.procfsDir = procfsDir;
127121
this.cGroupsHandler = cGroupsHandler;
128-
this.pid = pid != null && pid.equals("0") ? "1" : pid;
129122
this.jiffyLengthMs = jiffyLengthMs0;
130123
this.cpuTimeTracker =
131124
new CpuTimeTracker(this.jiffyLengthMs);
@@ -143,12 +136,6 @@ public void initialize() throws YarnException {
143136
setCGroupFilePaths();
144137
}
145138

146-
@Override
147-
public float getCpuUsagePercent() {
148-
LOG.debug("Process {} jiffies:{}", pid, processTotalJiffies);
149-
return cpuTimeTracker.getCpuTrackerUsagePercent();
150-
}
151-
152139
@Override
153140
public long getCumulativeCpuTime() {
154141
if (jiffyLengthMs < 0) {
@@ -191,18 +178,6 @@ public void updateProcessTree() {
191178
}
192179
}
193180

194-
@Override
195-
public String getProcessTreeDump() {
196-
// We do not have a process tree in cgroups return just the pid for tracking
197-
return pid;
198-
}
199-
200-
@Override
201-
public boolean checkPidPgrpidForMatch() {
202-
// We do not have a process tree in cgroups returning default ok
203-
return true;
204-
}
205-
206181
/**
207182
* Checks if the CGroupsResourceCalculator is available on this system.
208183
* This assumes that Linux container executor is already initialized.

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/CGroupsV2ResourceCalculator.java

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@
1818

1919
package org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.resources;
2020

21-
import org.apache.commons.io.FileUtils;
22-
import org.apache.commons.lang3.StringUtils;
23-
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
27-
import org.apache.hadoop.classification.VisibleForTesting;
28-
import org.apache.hadoop.util.CpuTimeTracker;
29-
import org.apache.hadoop.util.SysInfoLinux;
30-
import org.apache.hadoop.yarn.exceptions.YarnException;
31-
import org.apache.hadoop.yarn.util.Clock;
32-
import org.apache.hadoop.yarn.util.ResourceCalculatorProcessTree;
33-
import org.apache.hadoop.yarn.util.SystemClock;
34-
3521
import java.io.IOException;
3622
import java.math.BigInteger;
3723
import java.nio.charset.StandardCharsets;
@@ -44,34 +30,34 @@
4430
import java.util.stream.Collectors;
4531
import java.util.stream.Stream;
4632

33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
36+
import org.apache.commons.io.FileUtils;
37+
import org.apache.commons.lang3.StringUtils;
38+
import org.apache.hadoop.classification.VisibleForTesting;
39+
import org.apache.hadoop.util.CpuTimeTracker;
40+
import org.apache.hadoop.yarn.exceptions.YarnException;
41+
4742
/**
4843
* A CGroupV2 file-system based Resource calculator without the process tree features.
4944
*
5045
* The feature only works if cluster runs in pure V2 version, because when we read the
5146
* /proc/{pid}/cgroup file currently we can not handle multiple lines.
5247
*/
53-
public class CGroupsV2ResourceCalculator extends ResourceCalculatorProcessTree {
48+
public class CGroupsV2ResourceCalculator extends AbstractCGroupsResourceCalculator {
5449
private static final Logger LOG = LoggerFactory.getLogger(CGroupsV2ResourceCalculator.class);
5550
private final Map<String, String> stats = new ConcurrentHashMap<>();
56-
private final Clock clock = SystemClock.getInstance();
57-
private final String pid;
5851

59-
@VisibleForTesting
60-
long jiffyLengthMs = SysInfoLinux.JIFFY_LENGTH_IN_MILLIS;
6152
@VisibleForTesting
6253
String root = "/";
63-
@VisibleForTesting
64-
CpuTimeTracker cpuTimeTracker;
65-
@VisibleForTesting
66-
CGroupsHandler cGroupsHandler;
6754

6855
/**
6956
* Create resource calculator for the container that has the specified pid.
7057
* @param pid A pid from the cgroup or null for all containers
7158
*/
7259
public CGroupsV2ResourceCalculator(String pid) {
7360
super(pid);
74-
this.pid = pid;
7561
}
7662

7763
@Override
@@ -80,11 +66,6 @@ public void initialize() throws YarnException {
8066
cGroupsHandler = ResourceHandlerModule.getCGroupsHandler();
8167
}
8268

83-
@Override
84-
public float getCpuUsagePercent() {
85-
return cpuTimeTracker.getCpuTrackerUsagePercent();
86-
}
87-
8869
@Override
8970
public long getCumulativeCpuTime() {
9071
// https://docs.kernel.org/admin-guide/cgroup-v2.html#cpu-interface-files
@@ -109,18 +90,6 @@ public long getVirtualMemorySize(int olderThanAge) {
10990
: getStat("memory.stat#vmalloc");
11091
}
11192

112-
@Override
113-
public String getProcessTreeDump() {
114-
// We do not have a process tree in cgroups return just the pid for tracking
115-
return pid;
116-
}
117-
118-
@Override
119-
public boolean checkPidPgrpidForMatch() {
120-
// We do not have a process tree in cgroups returning default ok
121-
return true;
122-
}
123-
12493
@Override
12594
public void updateProcessTree() {
12695
try (Stream<Path> cGroupFiles = Files.list(getCGroupPath())){

0 commit comments

Comments
 (0)