-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix] [broker] incorrect CPU usage collected by pulsar #17820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0de66ff
04271fc
cc575d3
e7e4cbd
e4165c7
d035064
95f5283
c8977ae
cc11ad4
db916d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ public class LinuxInfoUtils { | |
| private static final String CGROUPS_CPU_USAGE_PATH = "/sys/fs/cgroup/cpu/cpuacct.usage"; | ||
| private static final String CGROUPS_CPU_LIMIT_QUOTA_PATH = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"; | ||
| private static final String CGROUPS_CPU_LIMIT_PERIOD_PATH = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"; | ||
| private static final String CGROUPS_CPU_CPUSET_CPUS = "/sys/fs/cgroup/cpuset/cpuset.cpus"; | ||
|
|
||
| // proc states | ||
| private static final String PROC_STAT_PATH = "/proc/stat"; | ||
|
|
@@ -105,6 +106,26 @@ public static boolean isCGroupEnabled() { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get total cpu count. | ||
| * @return Total cpu count | ||
| */ | ||
| public static int getTotalCpuCount() throws IOException { | ||
| int totalCpuCount = 0; | ||
| String[] ranges = readTrimStringFromFile(Paths.get(CGROUPS_CPU_CPUSET_CPUS)).split(","); | ||
| for (String range : ranges) { | ||
| if (!range.contains("-")) { | ||
| totalCpuCount++; | ||
| } else { | ||
| int dashIndex = range.indexOf('-'); | ||
| int left = Integer.parseInt(range.substring(0, dashIndex)); | ||
| int right = Integer.parseInt(range.substring(dashIndex + 1)); | ||
| totalCpuCount += right - left + 1; | ||
| } | ||
| } | ||
| return totalCpuCount; | ||
| } | ||
|
|
||
| /** | ||
| * Get total cpu limit. | ||
| * @param isCGroupsEnabled Whether CGroup is enabled | ||
|
|
@@ -126,6 +147,10 @@ public static double getTotalCpuLimit(boolean isCGroupsEnabled) { | |
| if (quota > 0) { | ||
| return 100.0 * quota / period; | ||
| } | ||
| int totalCpuCount = getTotalCpuCount(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, java 17 tracks container-aware resource usage. I think we just need to read jvm cpu limit and load if the jvm already provides the container-aware cpu limit and load. Reference: |
||
| if (totalCpuCount > 0) { | ||
| return 100 * totalCpuCount; | ||
| } | ||
| } catch (Exception e) { | ||
| log.warn("[LinuxInfo] Failed to read CPU quotas from cgroup", e); | ||
| // Fallback to availableProcessors | ||
|
|
@@ -300,11 +325,11 @@ private static Path getReplacedNICPath(String template, String nic) { | |
| return Paths.get(String.format(template, nic)); | ||
| } | ||
|
|
||
| private static String readTrimStringFromFile(Path path) throws IOException { | ||
| public static String readTrimStringFromFile(Path path) throws IOException { | ||
| return new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim(); | ||
| } | ||
|
|
||
| private static long readLongFromFile(Path path) throws IOException { | ||
| public static long readLongFromFile(Path path) throws IOException { | ||
| return Long.parseLong(readTrimStringFromFile(path)); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.