All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.jfr4jdbc.internal.ResourceMonitor Maven / Gradle / Ivy

There is a newer version: 2.0.0-M3
Show newest version
package dev.jfr4jdbc.internal;

import dev.jfr4jdbc.event.jfr.JfrConnectionResourceEvent;
import jdk.jfr.FlightRecorder;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class ResourceMonitor {

    private static volatile boolean isRecord = true;

    static {
        FlightRecorder.addPeriodicEvent(JfrConnectionResourceEvent.class, () -> {
            if (isRecord) {
                ResourceMonitorManager manager = ResourceMonitorManager.getInstance(ResourceMonitorKind.Connection);
                recordResourceMonitor(manager);
            }
        });
    }

    private final String label;
    private final AtomicInteger usageCount = new AtomicInteger(0);
    private final AtomicInteger waitCount = new AtomicInteger(0);

    public ResourceMonitor(String label) {
        this.label = label;
    }

    static void stopRecording() {
        isRecord = false;
    }

    static void startRecording() {
        isRecord = true;
    }

    public static final void recordResourceMonitor(ResourceMonitorManager manager) {
        List monitors = manager.getMonitors();
        monitors.forEach(m -> {
            JfrConnectionResourceEvent e = new JfrConnectionResourceEvent(m.getLabel(), m.getUsage(), m.getWait());
            e.commit();
        });
    }

    public String getLabel() {
        return this.label;
    }

    public int getUsage() {
        return this.usageCount.get();
    }

    public int getWait() {
        return this.waitCount.get();
    }

    public void waitAssigningResource() {
        this.waitCount.incrementAndGet();
    }

    public void assignedResource() {
        this.waitCount.decrementAndGet();
    }

    public void useResource() {
        this.usageCount.incrementAndGet();
    }

    public void releaseResource() {
        this.usageCount.decrementAndGet();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy