Example usage for java.lang.management GarbageCollectorMXBean getCollectionTime

List of usage examples for java.lang.management GarbageCollectorMXBean getCollectionTime

Introduction

In this page you can find the example usage for java.lang.management GarbageCollectorMXBean getCollectionTime.

Prototype

public long getCollectionTime();

Source Link

Document

Returns the approximate accumulated collection elapsed time in milliseconds.

Usage

From source file:alluxio.util.JvmPauseMonitor.java

private String formatLogString(long extraSleepTime, Map<String, GarbageCollectorMXBean> gcMXBeanMapBeforeSleep,
        Map<String, GarbageCollectorMXBean> gcMXBeanMapAfterSleep) {
    List<String> beanDiffs = Lists.newArrayList();
    GarbageCollectorMXBean oldBean;
    GarbageCollectorMXBean newBean;
    Set<String> nameSet = Sets.intersection(gcMXBeanMapBeforeSleep.keySet(), gcMXBeanMapAfterSleep.keySet());
    for (String name : nameSet) {
        oldBean = gcMXBeanMapBeforeSleep.get(name);
        newBean = gcMXBeanMapAfterSleep.get(name);
        if (oldBean == null) {
            beanDiffs.add("new GCBean created name= '" + newBean.getName() + " count="
                    + newBean.getCollectionCount() + " time=" + newBean.getCollectionTime() + "ms");
        } else if (newBean == null) {
            beanDiffs.add("old GCBean canceled name= '" + oldBean.getName() + " count="
                    + oldBean.getCollectionCount() + " time=" + oldBean.getCollectionTime() + "ms");
        } else {/*from  w  ww  .  jav a  2 s  .  co  m*/
            if (oldBean.getCollectionTime() != newBean.getCollectionTime()
                    || oldBean.getCollectionCount() != newBean.getCollectionCount()) {
                beanDiffs.add("GC name= '" + newBean.getName() + " count=" + newBean.getCollectionCount()
                        + " time=" + newBean.getCollectionTime() + "ms");
            }
        }
    }
    StringBuilder ret = new StringBuilder().append("JVM paused ").append(extraSleepTime).append("ms\n");
    if (beanDiffs.isEmpty()) {
        ret.append("No GCs detected ");
    } else {
        ret.append("GC list:\n" + Joiner.on("\n").join(beanDiffs));
    }
    ret.append("\n").append(getMemoryInfo());
    return ret.toString();
}

From source file:com.nextdoor.bender.handler.BaseHandler.java

private void getGCStats() {
    long currentGcCount = 0;
    long currentGcDuration = 0;

    for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
        long count = gc.getCollectionCount();

        if (count >= 0) {
            currentGcCount += count;/*from w  ww  .  j a  va2  s .  c o m*/
        }

        long time = gc.getCollectionTime();

        if (time >= 0) {
            currentGcDuration += time;
        }
    }

    logger.trace("number of GCs: " + (currentGcCount - lastGcCount) + " and time spent in GCs: "
            + (currentGcDuration - lastGcDuration) + "ms");

    lastGcCount = currentGcCount;
    lastGcDuration = currentGcDuration;
}

From source file:org.apache.hadoop.mapred.CollectiveMapper.java

protected void logGCTime() {
    long totalGarbageCollections = 0;
    long garbageCollectionTime = 0;
    for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
        long count = gc.getCollectionCount();
        if (count >= 0) {
            totalGarbageCollections += count;
        }/*w ww  .  jav  a  2  s . c o  m*/
        long time = gc.getCollectionTime();
        if (time >= 0) {
            garbageCollectionTime += time;
        }
    }
    LOG.info("Total Garbage Collections: " + totalGarbageCollections + ", Total Garbage Collection Time (ms): "
            + garbageCollectionTime);
}

From source file:com.google.dart.compiler.metrics.Tracer.java

void addGcEvents(TraceEvent refEvent) {
    // we're not sending GC events to the dartboard, so we only record them
    // to file//from www. j  a  v  a 2  s  .c  o m
    if (!fileLoggingEnabled) {
        return;
    }

    for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
        String gcName = gcMXBean.getName();
        Long lastGcTime = lastGcTimes.get(gcName);
        long currGcTime = gcMXBean.getCollectionTime();
        if (lastGcTime == null) {
            lastGcTime = 0L;
        }
        if (currGcTime > lastGcTime) {
            // create a new event
            long gcDurationNanos = (currGcTime - lastGcTime) * 1000000L;
            TraceEvent gcEvent = new GcEvent(refEvent, gcName, gcMXBean.getCollectionCount(), gcDurationNanos);

            eventsToWrite.add(gcEvent);
            lastGcTimes.put(gcName, currGcTime);
        }
    }
}

From source file:org.apache.flink.runtime.taskmanager.TaskManager.java

private String getGarbageCollectorStatsAsString(List<GarbageCollectorMXBean> gcMXBeans) {
    StringBuilder str = new StringBuilder();
    str.append("Garbage collector stats: ");

    for (int i = 0; i < gcMXBeans.size(); i++) {
        GarbageCollectorMXBean bean = gcMXBeans.get(i);

        String msg = String.format("[%s, GC TIME (ms): %d, GC COUNT: %d]", bean.getName(),
                bean.getCollectionTime(), bean.getCollectionCount());
        str.append(msg);// ww w  . j a  v a 2 s  .  c  om
        str.append(i < gcMXBeans.size() - 1 ? ", " : "");
    }

    return str.toString();
}

From source file:com.datatorrent.stram.engine.StreamingContainer.java

public void heartbeatLoop() throws Exception {
    umbilical.log(containerId, "[" + containerId + "] Entering heartbeat loop..");
    logger.debug("Entering heartbeat loop (interval is {} ms)", this.heartbeatIntervalMillis);
    final YarnConfiguration conf = new YarnConfiguration();
    long tokenLifeTime = (long) (containerContext.getValue(LogicalPlan.TOKEN_REFRESH_ANTICIPATORY_FACTOR)
            * containerContext.getValue(LogicalPlan.HDFS_TOKEN_LIFE_TIME));
    long expiryTime = System.currentTimeMillis();
    final Credentials credentials = UserGroupInformation.getCurrentUser().getCredentials();
    Iterator<Token<?>> iter = credentials.getAllTokens().iterator();
    while (iter.hasNext()) {
        Token<?> token = iter.next();
        logger.debug("token: {}", token);
    }/*from   w ww .  j a  va 2s. co  m*/
    String hdfsKeyTabFile = containerContext.getValue(LogicalPlan.KEY_TAB_FILE);
    while (!exitHeartbeatLoop) {

        if (UserGroupInformation.isSecurityEnabled() && System.currentTimeMillis() >= expiryTime
                && hdfsKeyTabFile != null) {
            expiryTime = StramUserLogin.refreshTokens(tokenLifeTime, FileUtils.getTempDirectoryPath(),
                    containerId, conf, hdfsKeyTabFile, credentials, null, false);
        }
        synchronized (this.heartbeatTrigger) {
            try {
                this.heartbeatTrigger.wait(heartbeatIntervalMillis);
            } catch (InterruptedException e1) {
                logger.warn("Interrupted in heartbeat loop, exiting..");
                break;
            }
        }

        long currentTime = System.currentTimeMillis();
        ContainerHeartbeat msg = new ContainerHeartbeat();
        msg.jvmName = jvmName;
        if (this.bufferServerAddress != null) {
            msg.bufferServerHost = this.bufferServerAddress.getHostName();
            msg.bufferServerPort = this.bufferServerAddress.getPort();
            if (bufferServer != null && !eventloop.isActive()) {
                logger.warn("Requesting restart due to terminated event loop");
                msg.restartRequested = true;
            }
        }
        msg.memoryMBFree = ((int) (Runtime.getRuntime().freeMemory() / (1024 * 1024)));
        garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean bean : garbageCollectorMXBeans) {
            msg.gcCollectionTime += bean.getCollectionTime();
            msg.gcCollectionCount += bean.getCollectionCount();
        }

        ContainerHeartbeatResponse rsp;
        do {
            ContainerStats stats = new ContainerStats(containerId);
            // gather heartbeat info for all operators
            for (Map.Entry<Integer, Node<?>> e : nodes.entrySet()) {
                OperatorHeartbeat hb = new OperatorHeartbeat();
                hb.setNodeId(e.getKey());
                hb.setGeneratedTms(currentTime);
                hb.setIntervalMs(heartbeatIntervalMillis);
                if (e.getValue().commandResponse.size() > 0) {
                    BlockingQueue<StatsListener.OperatorResponse> commandResponse = e
                            .getValue().commandResponse;
                    ArrayList<StatsListener.OperatorResponse> response = new ArrayList<StatsListener.OperatorResponse>();
                    for (int i = 0; i < commandResponse.size(); i++) {
                        response.add(commandResponse.poll());
                    }
                    hb.requestResponse = response;
                }
                OperatorContext context = e.getValue().context;
                context.drainStats(hb.getOperatorStatsContainer());

                if (context.getThread() == null || context.getThread().getState() != Thread.State.TERMINATED) {
                    hb.setState(DeployState.ACTIVE);
                } else if (failedNodes.contains(hb.nodeId)) {
                    hb.setState(DeployState.FAILED);
                } else {
                    logger.debug("Reporting SHUTDOWN state because thread is {} and failedNodes is {}",
                            context.getThread(), failedNodes);
                    hb.setState(DeployState.SHUTDOWN);
                }

                stats.addNodeStats(hb);
            }

            /**
             * Container stats published for whoever is interested in listening.
             * Currently interested candidates are TupleRecorderCollection and BufferServerStatsSubscriber
             */
            eventBus.publish(new ContainerStatsEvent(stats));

            msg.setContainerStats(stats);

            // heartbeat call and follow-up processing
            //logger.debug("Sending heartbeat for {} operators.", msg.getContainerStats().size());
            msg.sentTms = System.currentTimeMillis();
            rsp = umbilical.processHeartbeat(msg);
            processHeartbeatResponse(rsp);
            if (rsp.hasPendingRequests) {
                logger.info("Waiting for pending request.");
                synchronized (this.heartbeatTrigger) {
                    try {
                        this.heartbeatTrigger.wait(500);
                    } catch (InterruptedException ie) {
                        logger.warn("Interrupted in heartbeat loop", ie);
                        break;
                    }
                }
            }
        } while (rsp.hasPendingRequests);

    }
    logger.debug("Exiting hearbeat loop");
    umbilical.log(containerId, "[" + containerId + "] Exiting heartbeat loop..");
}

From source file:net.centro.rtb.monitoringcenter.metrics.system.jvm.GarbageCollectorMetricSet.java

GarbageCollectorMetricSet() {
    this.garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();

    this.minorGcTimer = new Timer();
    this.majorGcTimer = new Timer();

    // Determine the location of the gc log file (note that there's not support for rolling gc logs)
    String gcLogFilePath = null;/*from w w  w .  ja v a2 s  .  c  om*/
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    List<String> inputArguments = runtimeMXBean.getInputArguments();
    for (String argument : inputArguments) {
        if (argument.startsWith(LOG_GC_JVM_PARAM)) {
            gcLogFilePath = argument.substring(LOG_GC_JVM_PARAM.length());
            break;
        }
    }

    if (gcLogFilePath != null && !gcLogFilePath.trim().isEmpty()) {
        final File gcLogFile = new File(gcLogFilePath);
        if (gcLogFile.exists()) {
            this.fullCollectionsCounter = new AtomicLong();

            this.gcLogTailer = Tailer.create(gcLogFile, new TailerListenerAdapter() {
                @Override
                public void handle(String line) {
                    if (line != null && line.contains(FULL_GC_LOG_STRING)) {
                        fullCollectionsCounter.incrementAndGet();
                    }
                }
            }, GC_LOG_FILE_TAIL_DELAY_IN_MILLIS);
        }
    }

    // Attach a listener to the GarbageCollectorMXBeans
    this.gcEventListener = new NotificationListener() {
        @Override
        public void handleNotification(Notification notification, Object handback) {
            String notificationType = notification.getType();
            if (notificationType.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
                CompositeData compositeData = CompositeData.class.cast(notification.getUserData());
                GarbageCollectionNotificationInfo gcNotificationInfo = GarbageCollectionNotificationInfo
                        .from(compositeData);

                if (GC_NOTIFICATION_MINOR_GC_ACTION_STRING.equals(gcNotificationInfo.getGcAction())) {
                    minorGcTimer.update(gcNotificationInfo.getGcInfo().getDuration(), TimeUnit.MILLISECONDS);
                } else if (GC_NOTIFICATION_MAJOR_GC_ACTION_STRING.equals(gcNotificationInfo.getGcAction())) {
                    majorGcTimer.update(gcNotificationInfo.getGcInfo().getDuration(), TimeUnit.MILLISECONDS);
                }
            }
        }
    };

    for (final GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) {
        if (NotificationEmitter.class.isInstance(garbageCollectorMXBean)) {
            NotificationEmitter emitter = NotificationEmitter.class.cast(garbageCollectorMXBean);
            emitter.addNotificationListener(gcEventListener, null, null);
        }
    }

    // Set up metrics
    Map<String, Metric> metricsByNames = new HashMap<>();

    if (fullCollectionsCounter != null) {
        this.fullCollectionsGauge = new Gauge<Long>() {
            @Override
            public Long getValue() {
                return fullCollectionsCounter.get();
            }
        };
        metricsByNames.put("fullCollections", fullCollectionsGauge);
    }

    metricsByNames.put("majorGcTimer", majorGcTimer);
    metricsByNames.put("minorGcTimer", minorGcTimer);

    List<GarbageCollectorStatus> garbageCollectorStatuses = new ArrayList<>();
    for (final GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) {
        final String garbageCollectorName = garbageCollectorMXBean.getName();
        final String garbageCollectorNamespace = MetricNamingUtil.join("collectors",
                MetricNamingUtil.sanitize(garbageCollectorName));

        final Gauge<Long> collectionsGauge;
        if (garbageCollectorMXBean.getCollectionCount() >= 0) {
            collectionsGauge = new Gauge<Long>() {
                @Override
                public Long getValue() {
                    return garbageCollectorMXBean.getCollectionCount();
                }
            };
            metricsByNames.put(MetricNamingUtil.join(garbageCollectorNamespace, "collections"),
                    collectionsGauge);
        } else {
            collectionsGauge = null;
        }

        final Gauge<Long> totalCollectionDurationInMillisGauge;
        if (garbageCollectorMXBean.getCollectionTime() >= 0) {
            totalCollectionDurationInMillisGauge = new Gauge<Long>() {
                @Override
                public Long getValue() {
                    return garbageCollectorMXBean.getCollectionTime();
                }
            };
            metricsByNames.put(
                    MetricNamingUtil.join(garbageCollectorNamespace, "totalCollectionDurationInMillis"),
                    totalCollectionDurationInMillisGauge);
        } else {
            totalCollectionDurationInMillisGauge = null;
        }

        garbageCollectorStatuses.add(new GarbageCollectorStatus() {
            @Override
            public String getName() {
                return garbageCollectorName;
            }

            @Override
            public Gauge<Long> getCollectionsGauge() {
                return collectionsGauge;
            }

            @Override
            public Gauge<Long> getTotalCollectionDurationInMillisGauge() {
                return totalCollectionDurationInMillisGauge;
            }
        });
    }
    this.garbageCollectorStatuses = garbageCollectorStatuses;

    this.metricsByNames = metricsByNames;
}

From source file:org.apache.accumulo.server.tabletserver.ScanRunState.java

private synchronized static void logGCInfo(AccumuloConfiguration conf) {
    List<GarbageCollectorMXBean> gcmBeans = ManagementFactory.getGarbageCollectorMXBeans();
    Runtime rt = Runtime.getRuntime();

    StringBuilder sb = new StringBuilder("gc");

    boolean sawChange = false;

    long maxIncreaseInCollectionTime = 0;

    for (GarbageCollectorMXBean gcBean : gcmBeans) {
        Long prevTime = prevGcTime.get(gcBean.getName());
        long pt = 0;
        if (prevTime != null) {
            pt = prevTime;/*from   w w w .j a  v a 2s.  c  o m*/
        }

        long time = gcBean.getCollectionTime();

        if (time - pt != 0) {
            sawChange = true;
        }

        long increaseInCollectionTime = time - pt;
        sb.append(String.format(" %s=%,.2f(+%,.2f) secs", gcBean.getName(), time / 1000.0,
                increaseInCollectionTime / 1000.0));
        maxIncreaseInCollectionTime = Math.max(increaseInCollectionTime, maxIncreaseInCollectionTime);
        prevGcTime.put(gcBean.getName(), time);
    }

    long mem = rt.freeMemory();
    if (maxIncreaseInCollectionTime == 0) {
        gcTimeIncreasedCount = 0;
    } else {
        gcTimeIncreasedCount++;
        if (gcTimeIncreasedCount > 3 && mem < rt.maxMemory() * 0.05) {
            log.warn("Running low on memory");
            gcTimeIncreasedCount = 0;
        }
    }

    if (mem > lastMemorySize) {
        sawChange = true;
    }

    String sign = "+";
    if (mem - lastMemorySize <= 0) {
        sign = "";
    }

    sb.append(String.format(" freemem=%,d(%s%,d) totalmem=%,d", mem, sign, (mem - lastMemorySize),
            rt.totalMemory()));

    if (sawChange) {
        log.debug(sb.toString());
    }

    final long keepAliveTimeout = conf.getTimeInMillis(Property.INSTANCE_ZK_TIMEOUT);
    if (maxIncreaseInCollectionTime > keepAliveTimeout) {
        Halt.halt("Garbage collection may be interfering with lock keep-alive.  Halting.", -1);
    }

    lastMemorySize = mem;
}