Example usage for java.lang.management ManagementFactory getGarbageCollectorMXBeans

List of usage examples for java.lang.management ManagementFactory getGarbageCollectorMXBeans

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory getGarbageCollectorMXBeans.

Prototype

public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() 

Source Link

Document

Returns a list of GarbageCollectorMXBean objects in the Java virtual machine.

Usage

From source file:edu.uci.ics.hyracks.control.nc.NodeControllerService.java

public NodeControllerService(NCConfig ncConfig) throws Exception {
    this.ncConfig = ncConfig;
    id = ncConfig.nodeId;/*w  w  w . jav  a2s.c om*/
    NodeControllerIPCI ipci = new NodeControllerIPCI();
    ipc = new IPCSystem(new InetSocketAddress(ncConfig.clusterNetIPAddress, ncConfig.clusterNetPort), ipci,
            new CCNCFunctions.SerializerDeserializer());

    this.ctx = new RootHyracksContext(this, new IOManager(getDevices(ncConfig.ioDevices)));
    if (id == null) {
        throw new Exception("id not set");
    }
    partitionManager = new PartitionManager(this);
    netManager = new NetworkManager(ncConfig.dataIPAddress, ncConfig.dataPort, partitionManager,
            ncConfig.nNetThreads, ncConfig.nNetBuffers, ncConfig.dataPublicIPAddress, ncConfig.dataPublicPort);

    lccm = new LifeCycleComponentManager();
    queue = new WorkQueue();
    jobletMap = new Hashtable<JobId, Joblet>();
    timer = new Timer(true);
    serverCtx = new ServerContext(ServerContext.ServerType.NODE_CONTROLLER,
            new File(new File(NodeControllerService.class.getName()), id));
    memoryMXBean = ManagementFactory.getMemoryMXBean();
    gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
    threadMXBean = ManagementFactory.getThreadMXBean();
    runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    osMXBean = ManagementFactory.getOperatingSystemMXBean();
    registrationPending = true;
    getNodeControllerInfosAcceptor = new MutableObject<FutureValue<Map<String, NodeControllerInfo>>>();
    memoryManager = new MemoryManager(
            (long) (memoryMXBean.getHeapMemoryUsage().getMax() * MEMORY_FUDGE_FACTOR));
    ioCounter = new IOCounterFactory().getIOCounter();
}

From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java

@Override
@SuppressWarnings("unchecked")
public <T> T code(@javax.annotation.Nonnull final UncheckedSupplier<T> fn, final int maxLog,
        final int framesNo) {
    try {/* w  ww. jav  a2s.co m*/
        final StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[framesNo];
        final String sourceCode = CodeUtil.getInnerText(callingFrame);
        @javax.annotation.Nonnull
        final SysOutInterceptor.LoggedResult<TimedResult<Object>> result = SysOutInterceptor.withOutput(() -> {
            long priorGcMs = ManagementFactory.getGarbageCollectorMXBeans().stream()
                    .mapToLong(x -> x.getCollectionTime()).sum();
            final long start = System.nanoTime();
            try {
                @Nullable
                Object result1 = null;
                try {
                    result1 = fn.get();
                } catch (@javax.annotation.Nonnull final RuntimeException e) {
                    throw e;
                } catch (@javax.annotation.Nonnull final Exception e) {
                    throw new RuntimeException(e);
                }
                long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream()
                        .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs;
                return new TimedResult<Object>(result1, System.nanoTime() - start, gcTime);
            } catch (@javax.annotation.Nonnull final Throwable e) {
                long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream()
                        .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs;
                return new TimedResult<Object>(e, System.nanoTime() - start, gcTime);
            }
        });
        out(anchor(anchorId()) + "Code from [%s:%s](%s#L%s) executed in %.2f seconds (%.3f gc): ",
                callingFrame.getFileName(), callingFrame.getLineNumber(),
                linkTo(CodeUtil.findFile(callingFrame)), callingFrame.getLineNumber(), result.obj.seconds(),
                result.obj.gc_seconds());
        String text = sourceCode.replaceAll("\n", "\n  ");
        out("```java");
        out("  " + text);
        out("```");

        if (!result.log.isEmpty()) {
            String summary = summarize(result.log, maxLog).replaceAll("\n", "\n    ").replaceAll("    ~", "");
            out(anchor(anchorId()) + "Logging: ");
            out("```");
            out("    " + summary);
            out("```");
        }
        out("");

        final Object eval = result.obj.result;
        if (null != eval) {
            out(anchor(anchorId()) + "Returns: \n");
            String str;
            boolean escape;
            if (eval instanceof Throwable) {
                @javax.annotation.Nonnull
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                ((Throwable) eval).printStackTrace(new PrintStream(out));
                str = new String(out.toByteArray(), "UTF-8");
                escape = true;//
            } else if (eval instanceof Component) {
                str = image(Util.toImage((Component) eval), "Result");
                escape = false;
            } else if (eval instanceof BufferedImage) {
                str = image((BufferedImage) eval, "Result");
                escape = false;
            } else if (eval instanceof TableOutput) {
                str = ((TableOutput) eval).toTextTable();
                escape = false;
            } else {
                str = eval.toString();
                escape = true;
            }
            @javax.annotation.Nonnull
            String fmt = escape
                    ? "    " + summarize(str, maxLog).replaceAll("\n", "\n    ").replaceAll("    ~", "")
                    : str;
            if (escape) {
                out("```");
                out(fmt);
                out("```");
            } else {
                out(fmt);
            }
            out("\n\n");
            if (eval instanceof RuntimeException) {
                throw ((RuntimeException) result.obj.result);
            }
            if (eval instanceof Throwable) {
                throw new RuntimeException((Throwable) result.obj.result);
            }
        }
        return (T) eval;
    } catch (@javax.annotation.Nonnull final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jenkinsci.remoting.engine.HandlerLoopbackLoadStress.java

public HandlerLoopbackLoadStress(Config config)
        throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException,
        UnrecoverableKeyException, KeyManagementException, OperatorCreationException {
    this.config = config;
    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
    gen.initialize(2048); // maximum supported by JVM with export restrictions
    keyPair = gen.generateKeyPair();/* w w  w  .  j a  v a 2s. co  m*/

    Date now = new Date();
    Date firstDate = new Date(now.getTime() + TimeUnit.DAYS.toMillis(10));
    Date lastDate = new Date(now.getTime() + TimeUnit.DAYS.toMillis(-10));

    SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo
            .getInstance(keyPair.getPublic().getEncoded());

    X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    X500Name subject = nameBuilder.addRDN(BCStyle.CN, getClass().getSimpleName()).addRDN(BCStyle.C, "US")
            .build();

    X509v3CertificateBuilder certGen = new X509v3CertificateBuilder(subject, BigInteger.ONE, firstDate,
            lastDate, subject, subjectPublicKeyInfo);

    JcaX509ExtensionUtils instance = new JcaX509ExtensionUtils();

    certGen.addExtension(X509Extension.subjectKeyIdentifier, false,
            instance.createSubjectKeyIdentifier(subjectPublicKeyInfo));

    ContentSigner signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider(BOUNCY_CASTLE_PROVIDER)
            .build(keyPair.getPrivate());

    certificate = new JcaX509CertificateConverter().setProvider(BOUNCY_CASTLE_PROVIDER)
            .getCertificate(certGen.build(signer));

    char[] password = "password".toCharArray();

    KeyStore store = KeyStore.getInstance("jks");
    store.load(null, password);
    store.setKeyEntry("alias", keyPair.getPrivate(), password, new Certificate[] { certificate });

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(store, password);

    context = SSLContext.getInstance("TLS");
    context.init(kmf.getKeyManagers(), new TrustManager[] { new BlindTrustX509ExtendedTrustManager() }, null);

    mainHub = IOHub.create(executorService);
    // on windows there is a bug whereby you cannot mix ServerSockets and Sockets on the same selector
    acceptorHub = File.pathSeparatorChar == 59 ? IOHub.create(executorService) : mainHub;
    legacyHub = new NioChannelHub(executorService);
    executorService.submit(legacyHub);
    serverSocketChannel = ServerSocketChannel.open();

    JnlpProtocolHandler handler = null;
    for (JnlpProtocolHandler h : new JnlpProtocolHandlerFactory(executorService).withNioChannelHub(legacyHub)
            .withIOHub(mainHub).withSSLContext(context).withPreferNonBlockingIO(!config.bio)
            .withClientDatabase(new JnlpClientDatabase() {
                @Override
                public boolean exists(String clientName) {
                    return true;
                }

                @Override
                public String getSecretOf(@Nonnull String clientName) {
                    return secretFor(clientName);
                }
            }).withSSLClientAuthRequired(false).handlers()) {
        if (config.name.equals(h.getName())) {
            handler = h;
            break;
        }
    }
    if (handler == null) {
        throw new RuntimeException("Unknown handler: " + config.name);
    }
    this.handler = handler;

    acceptor = new Acceptor(serverSocketChannel);
    runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    _getProcessCpuTime = _getProcessCpuTime(operatingSystemMXBean);
    garbageCollectorMXBeans = new ArrayList<GarbageCollectorMXBean>(
            ManagementFactory.getGarbageCollectorMXBeans());
    Collections.sort(garbageCollectorMXBeans, new Comparator<GarbageCollectorMXBean>() {
        @Override
        public int compare(GarbageCollectorMXBean o1, GarbageCollectorMXBean o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    stats = new Stats();
}

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 w w .ja v  a 2  s . com
        }

        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.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;/* ww w  .j  av  a2s. com*/
        }

        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;
}

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;
        }// ww  w  . j  ava 2 s  .  c  om
        long time = gc.getCollectionTime();
        if (time >= 0) {
            garbageCollectionTime += time;
        }
    }
    LOG.info("Total Garbage Collections: " + totalGarbageCollections + ", Total Garbage Collection Time (ms): "
            + garbageCollectionTime);
}

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

public TaskManager(ExecutionMode executionMode, JobManagerProtocol jobManager,
        InputSplitProviderProtocol splitProvider, ChannelLookupProtocol channelLookup,
        AccumulatorProtocol accumulators, InetSocketAddress jobManagerAddress,
        InetAddress taskManagerBindAddress) throws Exception {
    if (executionMode == null || jobManager == null || splitProvider == null || channelLookup == null
            || accumulators == null) {/*from   ww w  . ja va2s. c  o m*/
        throw new NullPointerException();
    }

    LOG.info("TaskManager execution mode: " + executionMode);

    this.executionMode = executionMode;
    this.jobManager = jobManager;
    this.lookupService = channelLookup;
    this.globalInputSplitProvider = splitProvider;
    this.accumulatorProtocolProxy = accumulators;

    // initialize the number of slots
    {
        int slots = GlobalConfiguration.getInteger(ConfigConstants.TASK_MANAGER_NUM_TASK_SLOTS, -1);
        if (slots == -1) {
            slots = 1;
            LOG.info("Number of task slots not configured. Creating one task slot.");
        } else if (slots <= 0) {
            throw new Exception("Illegal value for the number of task slots: " + slots);
        } else {
            LOG.info("Creating " + slots + " task slot(s).");
        }
        this.numberOfSlots = slots;
    }

    int ipcPort = GlobalConfiguration.getInteger(ConfigConstants.TASK_MANAGER_IPC_PORT_KEY, -1);
    int dataPort = GlobalConfiguration.getInteger(ConfigConstants.TASK_MANAGER_DATA_PORT_KEY, -1);
    if (ipcPort == -1) {
        ipcPort = getAvailablePort();
    }
    if (dataPort == -1) {
        dataPort = getAvailablePort();
    }

    this.localInstanceConnectionInfo = new InstanceConnectionInfo(taskManagerBindAddress, ipcPort, dataPort);
    LOG.info("TaskManager connection information:" + this.localInstanceConnectionInfo);

    // Start local RPC server, give it the number of threads as we have slots
    try {
        // some magic number for the handler threads
        final int numHandlers = Math.min(numberOfSlots, 2 * Hardware.getNumberCPUCores());

        this.taskManagerServer = RPC.getServer(this, taskManagerBindAddress.getHostAddress(), ipcPort,
                numHandlers);
        this.taskManagerServer.start();
    } catch (IOException e) {
        LOG.error("Failed to start TaskManager server. " + e.getMessage(), e);
        throw new Exception("Failed to start taskmanager server. " + e.getMessage(), e);
    }

    // Load profiler if it should be used
    if (GlobalConfiguration.getBoolean(ProfilingUtils.ENABLE_PROFILING_KEY, false)) {

        final String profilerClassName = GlobalConfiguration.getString(ProfilingUtils.TASKMANAGER_CLASSNAME_KEY,
                "org.apache.flink.runtime.profiling.impl.TaskManagerProfilerImpl");

        this.profiler = ProfilingUtils.loadTaskManagerProfiler(profilerClassName,
                jobManagerAddress.getAddress(), this.localInstanceConnectionInfo);

        if (this.profiler == null) {
            LOG.error("Cannot find class name for the profiler.");
        } else {
            LOG.info("Profiling of jobs is enabled.");
        }
    } else {
        this.profiler = null;
        LOG.info("Profiling of jobs is disabled.");
    }

    // Get the directory for storing temporary files
    final String[] tmpDirPaths = GlobalConfiguration
            .getString(ConfigConstants.TASK_MANAGER_TMP_DIR_KEY, ConfigConstants.DEFAULT_TASK_MANAGER_TMP_PATH)
            .split(",|" + File.pathSeparator);

    checkTempDirs(tmpDirPaths);

    int numBuffers = GlobalConfiguration.getInteger(ConfigConstants.TASK_MANAGER_NETWORK_NUM_BUFFERS_KEY,
            ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_NUM_BUFFERS);

    int bufferSize = GlobalConfiguration.getInteger(ConfigConstants.TASK_MANAGER_NETWORK_BUFFER_SIZE_KEY,
            ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_BUFFER_SIZE);

    // Initialize the channel manager
    try {
        NetworkConnectionManager networkConnectionManager = null;

        switch (executionMode) {
        case LOCAL:
            networkConnectionManager = new LocalConnectionManager();
            break;
        case CLUSTER:
            int numInThreads = GlobalConfiguration.getInteger(
                    ConfigConstants.TASK_MANAGER_NET_NUM_IN_THREADS_KEY,
                    ConfigConstants.DEFAULT_TASK_MANAGER_NET_NUM_IN_THREADS);

            int numOutThreads = GlobalConfiguration.getInteger(
                    ConfigConstants.TASK_MANAGER_NET_NUM_OUT_THREADS_KEY,
                    ConfigConstants.DEFAULT_TASK_MANAGER_NET_NUM_OUT_THREADS);

            int lowWaterMark = GlobalConfiguration.getInteger(
                    ConfigConstants.TASK_MANAGER_NET_NETTY_LOW_WATER_MARK,
                    ConfigConstants.DEFAULT_TASK_MANAGER_NET_NETTY_LOW_WATER_MARK);

            int highWaterMark = GlobalConfiguration.getInteger(
                    ConfigConstants.TASK_MANAGER_NET_NETTY_HIGH_WATER_MARK,
                    ConfigConstants.DEFAULT_TASK_MANAGER_NET_NETTY_HIGH_WATER_MARK);

            networkConnectionManager = new NettyConnectionManager(localInstanceConnectionInfo.address(),
                    localInstanceConnectionInfo.dataPort(), bufferSize, numInThreads, numOutThreads,
                    lowWaterMark, highWaterMark);
            break;
        }

        channelManager = new ChannelManager(lookupService, localInstanceConnectionInfo, numBuffers, bufferSize,
                networkConnectionManager);
    } catch (IOException ioe) {
        LOG.error(StringUtils.stringifyException(ioe));
        throw new Exception("Failed to instantiate ChannelManager.", ioe);
    }

    // initialize the memory manager
    {
        // Check whether the memory size has been explicitly configured.
        final long configuredMemorySize = GlobalConfiguration
                .getInteger(ConfigConstants.TASK_MANAGER_MEMORY_SIZE_KEY, -1);
        final long memorySize;

        if (configuredMemorySize == -1) {
            // no manually configured memory. take a relative fraction of the free heap space
            float fraction = GlobalConfiguration.getFloat(ConfigConstants.TASK_MANAGER_MEMORY_FRACTION_KEY,
                    ConfigConstants.DEFAULT_MEMORY_MANAGER_MEMORY_FRACTION);
            memorySize = (long) (EnvironmentInformation.getSizeOfFreeHeapMemoryWithDefrag() * fraction);
            LOG.info("Using " + fraction + " of the free heap space for managed memory.");
        } else if (configuredMemorySize <= 0) {
            throw new Exception("Invalid value for Memory Manager memory size: " + configuredMemorySize);
        } else {
            memorySize = configuredMemorySize << 20;
        }

        final int pageSize = GlobalConfiguration.getInteger(
                ConfigConstants.TASK_MANAGER_NETWORK_BUFFER_SIZE_KEY,
                ConfigConstants.DEFAULT_TASK_MANAGER_NETWORK_BUFFER_SIZE);

        // Initialize the memory manager
        LOG.info("Initializing memory manager with " + (memorySize >>> 20) + " megabytes of memory. "
                + "Page size is " + pageSize + " bytes.");

        try {
            @SuppressWarnings("unused")
            final boolean lazyAllocation = GlobalConfiguration.getBoolean(
                    ConfigConstants.TASK_MANAGER_MEMORY_LAZY_ALLOCATION_KEY,
                    ConfigConstants.DEFAULT_TASK_MANAGER_MEMORY_LAZY_ALLOCATION);

            this.memoryManager = new DefaultMemoryManager(memorySize, this.numberOfSlots, pageSize);
        } catch (Throwable t) {
            LOG.error(
                    "Unable to initialize memory manager with " + (memorySize >>> 20) + " megabytes of memory.",
                    t);
            throw new Exception("Unable to initialize memory manager.", t);
        }
    }

    this.hardwareDescription = HardwareDescription.extractFromSystem(this.memoryManager.getMemorySize());

    // Determine the port of the BLOB server and register it with the library cache manager
    {
        final int blobPort = this.jobManager.getBlobServerPort();

        if (blobPort == -1) {
            LOG.warn("Unable to determine BLOB server address: User library download will not be available");
            this.libraryCacheManager = new FallbackLibraryCacheManager();
        } else {
            final InetSocketAddress blobServerAddress = new InetSocketAddress(jobManagerAddress.getAddress(),
                    blobPort);
            LOG.info("Determined BLOB server address to be " + blobServerAddress);

            this.libraryCacheManager = new BlobLibraryCacheManager(new BlobCache(blobServerAddress),
                    GlobalConfiguration.getConfiguration());
        }
    }
    this.ioManager = new IOManagerAsync(tmpDirPaths);

    // start the heart beats
    {
        final long interval = GlobalConfiguration.getInteger(
                ConfigConstants.TASK_MANAGER_HEARTBEAT_INTERVAL_KEY,
                ConfigConstants.DEFAULT_TASK_MANAGER_HEARTBEAT_INTERVAL);

        this.heartbeatThread = new Thread() {
            @Override
            public void run() {
                registerAndRunHeartbeatLoop(interval, MAX_LOST_HEART_BEATS);
            }
        };
        this.heartbeatThread.setName("Heartbeat Thread");
        this.heartbeatThread.start();
    }

    // --------------------------------------------------------------------
    // Memory Usage
    // --------------------------------------------------------------------

    final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    final List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();

    LOG.info(getMemoryUsageStatsAsString(memoryMXBean));

    boolean startMemoryUsageLogThread = GlobalConfiguration.getBoolean(
            ConfigConstants.TASK_MANAGER_DEBUG_MEMORY_USAGE_START_LOG_THREAD,
            ConfigConstants.DEFAULT_TASK_MANAGER_DEBUG_MEMORY_USAGE_START_LOG_THREAD);

    if (startMemoryUsageLogThread) {
        final int logIntervalMs = GlobalConfiguration.getInteger(
                ConfigConstants.TASK_MANAGER_DEBUG_MEMORY_USAGE_LOG_INTERVAL_MS,
                ConfigConstants.DEFAULT_TASK_MANAGER_DEBUG_MEMORY_USAGE_LOG_INTERVAL_MS);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (!isShutDown()) {
                        Thread.sleep(logIntervalMs);

                        LOG.info(getMemoryUsageStatsAsString(memoryMXBean));
                        LOG.info(getGarbageCollectorStatsAsString(gcMXBeans));
                    }
                } catch (InterruptedException e) {
                    LOG.warn("Unexpected interruption of memory usage logger thread.");
                }
            }
        }).start();
    }
}

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

private Tracer() {
    fileLoggingEnabled = logFile != null;
    enabled = fileLoggingEnabled;//from   w w  w  .  ja  v  a2s . c  o m

    if (enabled) {
        elapsedTimeKeeper = new ElapsedNormalizedTimeKeeper();
        processCpuTimeKeeper = new ProcessNormalizedTimeKeeper();
        threadCpuTimeKeeper = new ThreadNormalizedTimeKeeper();

        if (fileLoggingEnabled) {
            // Allow a system property to override the default output format
            Format format = Format.HTML;
            if (defaultFormatString != null) {
                for (Format value : Format.values()) {
                    if (value.name().toLowerCase().equals(defaultFormatString.toLowerCase())) {
                        format = value;
                        break;
                    }
                }
            }
            outputFormat = format;
            eventsToWrite = openDefaultLogWriter();

            shutDownSentinel = new TraceEvent();
            flushSentinel = new TraceEvent();
            shutDownLatch = new CountDownLatch(1);
        }

        if (logGcTime) {
            gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
            lastGcTimes = new ConcurrentHashMap<String, Long>();
        }

        pendingEvents = initPendingEvents();
    } else {
        elapsedTimeKeeper = null;
        processCpuTimeKeeper = null;
        threadCpuTimeKeeper = null;
    }
}

From source file:org.apache.giraph.graph.GraphTaskManager.java

/**
 * Install GC monitoring. This method intercepts all GC, log the gc, and
 * notifies an out-of-core engine (if any is used) about the GC.
 *//*w ww.ja v  a 2  s .  c  om*/
private void installGCMonitoring() {
    List<GarbageCollectorMXBean> mxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    final OutOfCoreEngine oocEngine = serviceWorker.getServerData().getOocEngine();
    for (GarbageCollectorMXBean gcBean : mxBeans) {
        NotificationEmitter emitter = (NotificationEmitter) gcBean;
        NotificationListener listener = new NotificationListener() {
            @Override
            public void handleNotification(Notification notification, Object handle) {
                if (notification.getType()
                        .equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
                    GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo
                            .from((CompositeData) notification.getUserData());

                    if (LOG.isInfoEnabled()) {
                        LOG.info("installGCMonitoring: name = " + info.getGcName() + ", action = "
                                + info.getGcAction() + ", cause = " + info.getGcCause() + ", duration = "
                                + info.getGcInfo().getDuration() + "ms");
                    }
                    gcTimeMetric.inc(info.getGcInfo().getDuration());
                    if (oocEngine != null) {
                        oocEngine.gcCompleted(info);
                    }
                }
            }
        };
        //Add the listener
        emitter.addNotificationListener(listener, null, null);
    }
}

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  w  w .java  2 s.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..");
}