Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26584 Backport of fixes to 7.6.3
- Loading branch information
Showing
9 changed files
with
310 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...ss-services/src/main/java/org/gradle/process/internal/health/memory/CGroupMemoryInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.gradle.process.internal.health.memory; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.io.Files; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
|
||
public class CGroupMemoryInfo implements OsMemoryInfo { | ||
private static final String CGROUP_MEM_USAGE_FILE = "/sys/fs/cgroup/memory/memory.usage_in_bytes"; | ||
private static final String CGROUP_MEM_TOTAL_FILE = "/sys/fs/cgroup/memory/memory.limit_in_bytes"; | ||
|
||
@Override | ||
public OsMemoryStatus getOsSnapshot() { | ||
String memUsageString = readStringFromFile(new File(CGROUP_MEM_USAGE_FILE)); | ||
String memTotalString = readStringFromFile(new File(CGROUP_MEM_TOTAL_FILE)); | ||
|
||
return getOsSnapshotFromCgroup(memUsageString, memTotalString); | ||
} | ||
|
||
private String readStringFromFile(File file) { | ||
try { | ||
return Files.asCharSource(file, Charset.defaultCharset()).readFirstLine(); | ||
} catch (IOException e) { | ||
throw new UnsupportedOperationException("Unable to read system memory from " + file.getAbsoluteFile(), e); | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
OsMemoryStatusSnapshot getOsSnapshotFromCgroup(String memUsageString, String memTotalString) { | ||
long memUsage; | ||
long memTotal; | ||
long memAvailable; | ||
|
||
try { | ||
memUsage = Long.parseLong(memUsageString); | ||
memTotal = Long.parseLong(memTotalString); | ||
memAvailable = Math.max(0, memTotal - memUsage); | ||
} catch (NumberFormatException e) { | ||
throw new UnsupportedOperationException("Unable to read system memory", e); | ||
} | ||
|
||
return new OsMemoryStatusSnapshot(memTotal, memAvailable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ces/src/test/groovy/org/gradle/process/internal/health/memory/CGroupMemoryInfoTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.gradle.process.internal.health.memory | ||
|
||
import spock.lang.Specification | ||
|
||
class CGroupMemoryInfoTest extends Specification { | ||
private static final long MB_IN_BYTES = 1024 * 1024 * 1024 | ||
|
||
def "parses memory from cgroup values"() { | ||
def snapshot = new CGroupMemoryInfo().getOsSnapshotFromCgroup(mbsToBytesAsString(800), mbsToBytesAsString(1024)) | ||
|
||
expect: | ||
snapshot.totalPhysicalMemory == mbsToBytes(1024) | ||
snapshot.freePhysicalMemory == mbsToBytes(224) | ||
} | ||
|
||
def "negative free memory returns zero"() { | ||
def snapshot = new CGroupMemoryInfo().getOsSnapshotFromCgroup(mbsToBytesAsString(1024), mbsToBytesAsString(512)) | ||
|
||
expect: | ||
snapshot.totalPhysicalMemory == mbsToBytes(512) | ||
snapshot.freePhysicalMemory == 0 | ||
} | ||
|
||
def "throws unsupported operation exception when non-numeric values are provided"() { | ||
when: | ||
new CGroupMemoryInfo().getOsSnapshotFromCgroup("foo", "bar") | ||
|
||
then: | ||
thrown(UnsupportedOperationException) | ||
} | ||
|
||
long mbsToBytes(int mbs) { | ||
return mbs * MB_IN_BYTES | ||
} | ||
|
||
String mbsToBytesAsString(int mbs) { | ||
return mbsToBytes(mbs) as String | ||
} | ||
} |
Oops, something went wrong.