-
Notifications
You must be signed in to change notification settings - Fork 93
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 #38 from AlejandroRivera/feature/cpu-monitoring
Added a new Profiler for CPU/JVM CPU monitoring using (Sun/Oracle) Java7+ JMX Bean
- Loading branch information
Showing
15 changed files
with
250 additions
and
51 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
105 changes: 105 additions & 0 deletions
105
src/main/java/com/etsy/statsd/profiler/profilers/CPULoadProfiler.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,105 @@ | ||
package com.etsy.statsd.profiler.profilers; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import com.etsy.statsd.profiler.Arguments; | ||
import com.etsy.statsd.profiler.Profiler; | ||
import com.etsy.statsd.profiler.reporter.Reporter; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.management.Attribute; | ||
import javax.management.AttributeList; | ||
import javax.management.InstanceNotFoundException; | ||
import javax.management.MBeanServer; | ||
import javax.management.MalformedObjectNameException; | ||
import javax.management.ObjectName; | ||
import javax.management.ReflectionException; | ||
|
||
/** | ||
* This profiler retrieves CPU values for the JVM and System from the "OperatingSystem" JMX Bean. | ||
* <p> | ||
* This profiler relies on a JMX bean that might not be available in all JVM implementations. | ||
* We know for sure it's available in Sun/Oracle's JRE 7+, but there are no guarantees it | ||
* will remain there for the foreseeable future. | ||
* | ||
* @see <a href="http://stackoverflow.com/questions/3044841/cpu-usage-mbean-on-sun-jvm">StackOverflow post</a> | ||
* | ||
* @author Alejandro Rivera | ||
*/ | ||
public class CPULoadProfiler extends Profiler { | ||
|
||
public static final long PERIOD = 10; | ||
private static final Map<String, String> ATTRIBUTES_MAP = ImmutableMap.of("ProcessCpuLoad", "cpu.jvm", | ||
"SystemCpuLoad", "cpu.system"); | ||
|
||
private AttributeList list; | ||
|
||
public CPULoadProfiler(Reporter reporter, Arguments arguments) { | ||
super(reporter, arguments); | ||
try { | ||
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); | ||
ObjectName os = ObjectName.getInstance("java.lang:type=OperatingSystem"); | ||
list = mbs.getAttributes(os, ATTRIBUTES_MAP.keySet().toArray(new String[ATTRIBUTES_MAP.size()])); | ||
} catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException e) { | ||
list = null; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Profile memory usage and GC statistics | ||
*/ | ||
@Override | ||
public void profile() { | ||
recordStats(); | ||
} | ||
|
||
@Override | ||
public void flushData() { | ||
recordStats(); | ||
} | ||
|
||
@Override | ||
public long getPeriod() { | ||
return PERIOD; | ||
} | ||
|
||
@Override | ||
public TimeUnit getTimeUnit() { | ||
return TimeUnit.SECONDS; | ||
} | ||
|
||
@Override | ||
protected void handleArguments(Arguments arguments) { /* No arguments needed */ } | ||
|
||
/** | ||
* Records all memory statistics | ||
*/ | ||
private void recordStats() { | ||
if (list == null) { | ||
return; | ||
} | ||
|
||
Attribute att; | ||
Double value; | ||
String metric; | ||
for (Object o : list) { | ||
att = (Attribute) o; | ||
value = (Double) att.getValue(); | ||
|
||
if (value == null || value == -1.0) { | ||
continue; | ||
} | ||
|
||
metric = ATTRIBUTES_MAP.get(att.getName()); | ||
if (metric == null) { | ||
continue; | ||
} | ||
|
||
value = ((int) (value * 1000)) / 10.0d; // 0-100 with 1-decimal precision | ||
recordGaugeValue(metric, value); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.