use of com.microsoft.gcmonitor.memorypools.MemoryPool in project ApplicationInsights-Java by microsoft.
the class GcEventMonitorTest method mockGcEvent.
private static GcCollectionEvent mockGcEvent() {
GcCollectionEvent event = Mockito.mock(GcCollectionEvent.class);
GarbageCollector collector = Mockito.mock(GarbageCollector.class);
MemoryPool tenuredPool = Mockito.mock(MemoryPool.class);
Mockito.when(collector.isTenuredCollector()).thenReturn(true);
Mockito.when(event.getCollector()).thenReturn(collector);
Mockito.when(event.getTenuredPool()).thenReturn(Optional.of(tenuredPool));
Mockito.when(event.getMemoryUsageAfterGc(Mockito.eq(tenuredPool))).thenReturn(new MemoryUsage(1, 9, 10, 10));
return event;
}
use of com.microsoft.gcmonitor.memorypools.MemoryPool in project ApplicationInsights-Java by microsoft.
the class GcEventMonitor method sendTenuredFillPercentageToAlerting.
/**
* Calculate the tenured fill percentage and forward the data to the alerting subsystem.
*/
private static void sendTenuredFillPercentageToAlerting(AlertingSubsystem alertingSubsystem, GcCollectionEvent event) {
if (event.getCollector().isTenuredCollector()) {
Optional<MemoryPool> tenuredPool = event.getTenuredPool();
if (tenuredPool.isPresent()) {
MemoryUsage tenuredUsage = event.getMemoryUsageAfterGc(tenuredPool.get());
long currentLevel = tenuredUsage.getUsed();
long max = tenuredUsage.getMax();
if (max > 0) {
double percentage = 100.0 * (double) currentLevel / (double) max;
alertingSubsystem.track(AlertMetricType.MEMORY, percentage);
}
}
}
}
use of com.microsoft.gcmonitor.memorypools.MemoryPool in project ApplicationInsights-Java by microsoft.
the class GcCollectionSample method groupMemoryUsageByPoolName.
private static Map<MemoryPool, MemoryUsage> groupMemoryUsageByPoolName(Object map, MemoryManagement memoryManagement) {
@SuppressWarnings("unchecked") Map<List<String>, CompositeData> byName = (Map<List<String>, CompositeData>) map;
Map<MemoryPool, MemoryUsage> byIdentifier = new HashMap<>();
try {
for (Map.Entry<List<String>, CompositeData> pool : byName.entrySet()) {
Iterator<?> pair = pool.getValue().values().iterator();
Optional<MemoryPool> name = memoryManagement.getPool((String) pair.next());
if (name.isPresent()) {
MemoryUsage usage = MemoryUsage.from((CompositeData) pair.next());
byIdentifier.put(name.get(), usage);
}
}
} catch (RuntimeException e) {
LOGGER.error("Failed to group pool data", e);
}
return Collections.unmodifiableMap(byIdentifier);
}
use of com.microsoft.gcmonitor.memorypools.MemoryPool in project ApplicationInsights-Java by microsoft.
the class VariousCollectorsTest method print.
@SuppressWarnings("SystemOut")
private static void print(List<GcCollectionEvent> events) {
System.out.println("Obtained: " + events.size());
events.forEach(event -> {
Optional<MemoryPool> tenuredPool = event.getTenuredPool();
if (tenuredPool.isPresent()) {
System.out.println(new StringJoiner(",").add(Long.toString(event.getId())).add(event.getGcCause()).add(Long.toString(event.getDuration())).add(Long.toString(event.getEndTime())).add(event.getCollector().toString()).add(event.getGcAction()).add(Long.toString(event.getMemoryUsageBeforeGc(tenuredPool.get()).getUsed())).add(Long.toString(event.getMemoryUsageAfterGc(tenuredPool.get()).getUsed())));
}
});
}
use of com.microsoft.gcmonitor.memorypools.MemoryPool in project ApplicationInsights-Java by microsoft.
the class GcEventMonitor method emitGcEvent.
/**
* If gc reporting is enabled, send gc event to Application Insights.
*/
private static void emitGcEvent(TelemetryClient telemetryClient, GcEventMonitorConfiguration gcEventMonitorConfiguration, GcCollectionEvent event) {
boolean reportEvent = gcEventMonitorConfiguration.reportingLevel == GcReportingLevel.ALL;
reportEvent |= gcEventMonitorConfiguration.reportingLevel == GcReportingLevel.TENURED_ONLY && event.getCollector().isTenuredCollector();
if (!reportEvent) {
return;
}
TelemetryItem telemetry = new TelemetryItem();
TelemetryEventData data = new TelemetryEventData();
TelemetryClient.getActive().initEventTelemetry(telemetry, data);
data.setName("GcEvent");
Map<String, String> properties = new HashMap<>();
properties.put("collector", event.getCollector().getName());
properties.put("type", event.getGcCause());
properties.put("action", event.getGcAction());
properties.put("jvm_instance_id", JVM_INSTANCE_UID);
data.setProperties(properties);
Map<String, Double> measurements = new HashMap<>();
measurements.put("id", (double) event.getId());
measurements.put("duration_ms", (double) event.getDuration());
measurements.put("end_time_ms", (double) event.getEndTime());
measurements.put("thread_count", (double) event.getGcThreadCount());
measurements.put("collection_count", (double) event.getCollector().getCollectionCount());
measurements.put("cumulative_collector_time_sec", (double) event.getCollector().getCollectionTime());
addMemoryUsage("young", "before", measurements, event.getMemoryUsageBeforeGc(event.getYoungPools()));
addMemoryUsage("young", "after", measurements, event.getMemoryUsageAfterGc(event.getYoungPools()));
Optional<MemoryPool> tenuredPool = event.getTenuredPool();
if (tenuredPool.isPresent()) {
MemoryUsage beforeOg = event.getMemoryUsageBeforeGc(tenuredPool.get());
addMemoryUsage("tenured", "before", measurements, beforeOg);
MemoryUsage afterOg = event.getMemoryUsageAfterGc(tenuredPool.get());
addMemoryUsage("tenured", "after", measurements, afterOg);
}
data.setMeasurements(measurements);
telemetry.setTime(FormattedTime.offSetDateTimeFromNow());
telemetryClient.trackAsync(telemetry);
}
Aggregations