|
24 | 24 | import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem; |
25 | 25 | import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient; |
26 | 26 | import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryUtil; |
| 27 | +import java.util.Map; |
27 | 28 | import java.util.concurrent.atomic.AtomicBoolean; |
28 | 29 | import org.slf4j.Logger; |
29 | 30 | import org.slf4j.LoggerFactory; |
|
32 | 33 | import oshi.hardware.CentralProcessor.TickType; |
33 | 34 | import oshi.software.os.OSProcess; |
34 | 35 | import oshi.software.os.OperatingSystem; |
| 36 | +import oshi.software.os.linux.LinuxOSProcess; |
| 37 | +import oshi.util.FileUtil; |
| 38 | +import oshi.util.ParseUtil; |
| 39 | +import oshi.util.platform.linux.ProcPath; |
35 | 40 |
|
36 | 41 | public class OshiPerformanceCounter implements PerformanceCounter { |
37 | 42 |
|
@@ -79,8 +84,12 @@ public void report(TelemetryClient telemetryClient) { |
79 | 84 | long currCollectionTimeMillis = System.currentTimeMillis(); |
80 | 85 | long currProcessBytes = 0L; |
81 | 86 | if (processInfo != null) { |
82 | | - updateAttributes(processInfo); |
83 | | - currProcessBytes = getProcessBytes(processInfo); |
| 87 | + if (processInfo instanceof LinuxOSProcess) { |
| 88 | + currProcessBytes = getProcessBytesLinux(processInfo.getProcessID()); |
| 89 | + } else { |
| 90 | + updateAttributes(processInfo); |
| 91 | + currProcessBytes = getProcessBytes(processInfo); |
| 92 | + } |
84 | 93 | } |
85 | 94 |
|
86 | 95 | long currTotalProcessorMillis = getTotalProcessorMillis(processor); |
@@ -124,6 +133,21 @@ private static long getProcessBytes(OSProcess processInfo) { |
124 | 133 | return processInfo.getBytesRead() + processInfo.getBytesWritten(); |
125 | 134 | } |
126 | 135 |
|
| 136 | + // oshi.software.os.linux.LinuxOSProcess.updateAttributes() captures too much stuff each time, |
| 137 | + // including groupId which calls "getent group", which having the machine connected to active |
| 138 | + // directory, scans its entire structure |
| 139 | + // (https://portal.microsofticm.com/imp/v3/incidents/details/289056966/home) |
| 140 | + // |
| 141 | + // so this method copies what LinuxOSProcess.updateAttributes() does for just the metrics that we |
| 142 | + // use for calculating I/O bytes |
| 143 | + private static long getProcessBytesLinux(int processId) { |
| 144 | + Map<String, String> io = |
| 145 | + FileUtil.getKeyValueMapFromFile(String.format(ProcPath.PID_IO, processId), ":"); |
| 146 | + long bytesRead = ParseUtil.parseLongOrDefault(io.getOrDefault("read_bytes", ""), 0L); |
| 147 | + long bytesWritten = ParseUtil.parseLongOrDefault(io.getOrDefault("write_bytes", ""), 0L); |
| 148 | + return bytesRead + bytesWritten; |
| 149 | + } |
| 150 | + |
127 | 151 | private static long getTotalProcessorMillis(CentralProcessor processor) { |
128 | 152 | long[] systemCpuLoadTicks = processor.getSystemCpuLoadTicks(); |
129 | 153 | return systemCpuLoadTicks[TickType.USER.getIndex()] |
|
0 commit comments