@@ -42,21 +42,8 @@ public class OshiPerformanceCounter implements PerformanceCounter {
4242 private long prevProcessBytes ;
4343 private long prevTotalProcessorMillis ;
4444
45- private final OSProcess processInfo ;
46- private final CentralProcessor processor ;
47-
48- public OshiPerformanceCounter () {
49- SystemInfo systemInfo = new SystemInfo ();
50- OperatingSystem osInfo = systemInfo .getOperatingSystem ();
51- processInfo = osInfo .getProcess (osInfo .getProcessId ());
52- processor = systemInfo .getHardware ().getProcessor ();
53-
54- // grab values early, so that we will have something to report the first time report() is called
55- updateAttributes (processInfo );
56- prevCollectionTimeMillis = System .currentTimeMillis ();
57- prevProcessBytes = getProcessBytes (processInfo );
58- prevTotalProcessorMillis = getTotalProcessorMillis (processor );
59- }
45+ private volatile OSProcess processInfo ;
46+ private volatile CentralProcessor processor ;
6047
6148 @ Override
6249 public String getId () {
@@ -65,21 +52,32 @@ public String getId() {
6552
6653 @ Override
6754 public void report (TelemetryClient telemetryClient ) {
55+ if (processInfo == null || processor == null ) {
56+ // lazy initializing these because they add to slowness during startup
57+ SystemInfo systemInfo = new SystemInfo ();
58+ OperatingSystem osInfo = systemInfo .getOperatingSystem ();
59+ processInfo = osInfo .getProcess (osInfo .getProcessId ());
60+ processor = systemInfo .getHardware ().getProcessor ();
61+ }
62+
63+
6864 long currCollectionTimeMillis = System .currentTimeMillis ();
6965 updateAttributes (processInfo );
7066 long currProcessBytes = getProcessBytes (processInfo );
7167 long currTotalProcessorMillis = getTotalProcessorMillis (processor );
7268
73- double elapsedMillis = currCollectionTimeMillis - prevCollectionTimeMillis ;
74- double elapsedSeconds = elapsedMillis / MILLIS_IN_SECOND ;
75- double processBytes = (currProcessBytes - prevProcessBytes ) / elapsedSeconds ;
76- send (telemetryClient , processBytes , Constants .PROCESS_IO_PC_METRIC_NAME );
77- logger .trace ("Sent performance counter for '{}': '{}'" , Constants .PROCESS_IO_PC_METRIC_NAME , processBytes );
78-
79- double processorLoad = (currTotalProcessorMillis - prevTotalProcessorMillis ) / (elapsedMillis * processor .getLogicalProcessorCount ());
80- double processorPercentage = 100 * processorLoad ;
81- send (telemetryClient , processorPercentage , Constants .TOTAL_CPU_PC_METRIC_NAME );
82- logger .trace ("Sent performance counter for '{}': '{}'" , Constants .TOTAL_CPU_PC_METRIC_NAME , processorPercentage );
69+ if (prevCollectionTimeMillis != 0 ) {
70+ double elapsedMillis = currCollectionTimeMillis - prevCollectionTimeMillis ;
71+ double elapsedSeconds = elapsedMillis / MILLIS_IN_SECOND ;
72+ double processBytes = (currProcessBytes - prevProcessBytes ) / elapsedSeconds ;
73+ send (telemetryClient , processBytes , Constants .PROCESS_IO_PC_METRIC_NAME );
74+ logger .trace ("Sent performance counter for '{}': '{}'" , Constants .PROCESS_IO_PC_METRIC_NAME , processBytes );
75+
76+ double processorLoad = (currTotalProcessorMillis - prevTotalProcessorMillis ) / (elapsedMillis * processor .getLogicalProcessorCount ());
77+ double processorPercentage = 100 * processorLoad ;
78+ send (telemetryClient , processorPercentage , Constants .TOTAL_CPU_PC_METRIC_NAME );
79+ logger .trace ("Sent performance counter for '{}': '{}'" , Constants .TOTAL_CPU_PC_METRIC_NAME , processorPercentage );
80+ }
8381
8482 prevCollectionTimeMillis = currCollectionTimeMillis ;
8583 prevProcessBytes = currProcessBytes ;
0 commit comments