Search in sources :

Example 1 with MemoryPool

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;
}
Also used : GcCollectionEvent(com.microsoft.gcmonitor.GcCollectionEvent) MemoryUsage(java.lang.management.MemoryUsage) GarbageCollector(com.microsoft.gcmonitor.garbagecollectors.GarbageCollector) MemoryPool(com.microsoft.gcmonitor.memorypools.MemoryPool)

Example 2 with MemoryPool

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);
            }
        }
    }
}
Also used : MemoryUsage(java.lang.management.MemoryUsage) MemoryPool(com.microsoft.gcmonitor.memorypools.MemoryPool)

Example 3 with MemoryPool

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);
}
Also used : HashMap(java.util.HashMap) CompositeData(javax.management.openmbean.CompositeData) MemoryUsage(java.lang.management.MemoryUsage) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) MemoryPool(com.microsoft.gcmonitor.memorypools.MemoryPool)

Example 4 with MemoryPool

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())));
        }
    });
}
Also used : StringJoiner(java.util.StringJoiner) MemoryPool(com.microsoft.gcmonitor.memorypools.MemoryPool)

Example 5 with MemoryPool

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);
}
Also used : HashMap(java.util.HashMap) TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) MemoryUsage(java.lang.management.MemoryUsage) TelemetryEventData(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData) MemoryPool(com.microsoft.gcmonitor.memorypools.MemoryPool)

Aggregations

MemoryPool (com.microsoft.gcmonitor.memorypools.MemoryPool)5 MemoryUsage (java.lang.management.MemoryUsage)4 HashMap (java.util.HashMap)2 TelemetryEventData (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData)1 TelemetryItem (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem)1 GcCollectionEvent (com.microsoft.gcmonitor.GcCollectionEvent)1 GarbageCollector (com.microsoft.gcmonitor.garbagecollectors.GarbageCollector)1 List (java.util.List)1 Map (java.util.Map)1 StringJoiner (java.util.StringJoiner)1 CompositeData (javax.management.openmbean.CompositeData)1