Search in sources :

Example 16 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project h2o-3 by h2oai.

the class H2O method main.

// --------------------------------------------------------------------------
public static void main(String[] args) {
    long time0 = System.currentTimeMillis();
    if (checkUnsupportedJava())
        throw new RuntimeException("Unsupported Java version");
    // Record system start-time.
    if (!START_TIME_MILLIS.compareAndSet(0L, System.currentTimeMillis()))
        // Already started
        return;
    // Copy all ai.h2o.* system properties to the tail of the command line,
    // effectively overwriting the earlier args.
    ArrayList<String> args2 = new ArrayList<>(Arrays.asList(args));
    for (Object p : System.getProperties().keySet()) {
        String s = (String) p;
        if (s.startsWith("ai.h2o.")) {
            args2.add("-" + s.substring(7));
            // hack: Junits expect properties, throw out dummy prop for ga_opt_out
            if (!s.substring(7).equals("ga_opt_out"))
                args2.add(System.getProperty(s));
        }
    }
    // Parse args
    String[] arguments = args2.toArray(args);
    parseArguments(arguments);
    // Get ice path before loading Log or Persist class
    long time1 = System.currentTimeMillis();
    String ice = DEFAULT_ICE_ROOT();
    if (ARGS.ice_root != null)
        ice = ARGS.ice_root.replace("\\", "/");
    try {
        ICE_ROOT = new URI(ice);
    } catch (URISyntaxException ex) {
        throw new RuntimeException("Invalid ice_root: " + ice + ", " + ex.getMessage());
    }
    // Always print version, whether asked-for or not!
    long time2 = System.currentTimeMillis();
    printAndLogVersion(arguments);
    if (ARGS.version) {
        Log.flushStdout();
        exit(0);
    }
    // Print help & exit
    if (ARGS.help) {
        printHelp();
        exit(0);
    }
    // Validate arguments
    validateArguments();
    Log.info("X-h2o-cluster-id: " + H2O.CLUSTER_ID);
    Log.info("User name: '" + H2O.ARGS.user_name + "'");
    // Register with GA or not
    long time3 = System.currentTimeMillis();
    // fetching this list takes ~100ms
    List<String> gaidList;
    if ((new File(".h2o_no_collect")).exists() || (new File(System.getProperty("user.home") + File.separator + ".h2o_no_collect")).exists() || ARGS.ga_opt_out || (gaidList = JarHash.getResourcesList("gaid")).contains("CRAN") || H2O.ABV.projectVersion().split("\\.")[3].equals("99999")) {
        // dev build has minor version 99999
        GA = null;
        Log.info("Opted out of sending usage metrics.");
    } else {
        try {
            GA = new GoogleAnalytics("UA-56665317-1", "H2O", ABV.projectVersion());
            DefaultRequest defReq = GA.getDefaultRequest();
            String gaid = null;
            if (gaidList.size() > 0) {
                if (gaidList.size() > 1)
                    Log.debug("More than once resource seen in gaid dir.");
                for (String str : gaidList) {
                    if (str.matches("........-....-....-....-............") && !str.equals("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")) {
                        gaid = str;
                        break;
                    }
                }
            }
            if (gaid == null) {
                // No UUID, create one
                gaid = defReq.clientId();
                gaid = gaid.replaceFirst("........-", "ANONYMOU-");
            }
            defReq.customDimension(CLIENT_ID_GA_CUST_DIM, gaid);
            GA.setDefaultRequest(defReq);
        } catch (Throwable t) {
            Log.POST(11, t.toString());
            StackTraceElement[] stes = t.getStackTrace();
            for (StackTraceElement ste : stes) Log.POST(11, ste.toString());
        }
    }
    // Epic Hunt for the correct self InetAddress
    long time4 = System.currentTimeMillis();
    Log.info("IPv6 stack selected: " + IS_IPV6);
    SELF_ADDRESS = NetworkInit.findInetAddressForSelf();
    // to enable IPv6 preference.
    if (!IS_IPV6 && SELF_ADDRESS instanceof Inet6Address) {
        Log.err("IPv4 network stack specified but IPv6 address found: " + SELF_ADDRESS + "\n" + "Please specify JVM flags -Djava.net.preferIPv6Addresses=true and -Djava.net.preferIPv4Addresses=false to select IPv6 stack");
        H2O.exit(-1);
    }
    if (IS_IPV6 && SELF_ADDRESS instanceof Inet4Address) {
        Log.err("IPv6 network stack specified but IPv4 address found: " + SELF_ADDRESS);
        H2O.exit(-1);
    }
    // Start the local node.  Needed before starting logging.
    long time5 = System.currentTimeMillis();
    startLocalNode();
    // Allow extensions to perform initialization that requires the network.
    long time6 = System.currentTimeMillis();
    for (AbstractH2OExtension ext : extensions) {
        ext.onLocalNodeStarted();
    }
    try {
        String logDir = Log.getLogDir();
        Log.info("Log dir: '" + logDir + "'");
    } catch (Exception e) {
        Log.info("Log dir: (Log4j configuration inherited)");
    }
    Log.info("Cur dir: '" + System.getProperty("user.dir") + "'");
    //Print extra debug info now that logs are setup
    long time7 = System.currentTimeMillis();
    RuntimeMXBean rtBean = ManagementFactory.getRuntimeMXBean();
    Log.debug("H2O launch parameters: " + ARGS.toString());
    Log.debug("Boot class path: " + rtBean.getBootClassPath());
    Log.debug("Java class path: " + rtBean.getClassPath());
    Log.debug("Java library path: " + rtBean.getLibraryPath());
    // Load up from disk and initialize the persistence layer
    long time8 = System.currentTimeMillis();
    initializePersistence();
    // Initialize NPS
    {
        String flow_dir;
        if (ARGS.flow_dir != null) {
            flow_dir = ARGS.flow_dir;
        } else {
            flow_dir = DEFAULT_FLOW_DIR();
        }
        if (flow_dir != null) {
            flow_dir = flow_dir.replace("\\", "/");
            Log.info("Flow dir: '" + flow_dir + "'");
        } else {
            Log.info("Flow dir is undefined; saving flows not available");
        }
        NPS = new NodePersistentStorage(flow_dir);
    }
    // Start network services, including heartbeats
    long time9 = System.currentTimeMillis();
    // start server services
    startNetworkServices();
    Log.trace("Network services started");
    // The "Cloud of size N formed" message printed out by doHeartbeat is the trigger
    // for users of H2O to know that it's OK to start sending REST API requests.
    long time10 = System.currentTimeMillis();
    Paxos.doHeartbeat(SELF);
    assert SELF._heartbeat._cloud_hash != 0 || ARGS.client;
    // Start the heartbeat thread, to publish the Clouds' existence to other
    // Clouds. This will typically trigger a round of Paxos voting so we can
    // join an existing Cloud.
    new HeartBeatThread().start();
    long time11 = System.currentTimeMillis();
    if (GA != null)
        startGAStartupReport();
    // Log registered parsers
    Log.info("Registered parsers: " + Arrays.toString(ParserService.INSTANCE.getAllProviderNames(true)));
    long time12 = System.currentTimeMillis();
    Log.debug("Timing within H2O.main():");
    Log.debug("    Args parsing & validation: " + (time1 - time0) + "ms");
    Log.debug("    Get ICE root: " + (time2 - time1) + "ms");
    Log.debug("    Print log version: " + (time3 - time2) + "ms");
    Log.debug("    Register GA: " + (time4 - time3) + "ms");
    Log.debug("    Detect network address: " + (time5 - time4) + "ms");
    Log.debug("    Start local node: " + (time6 - time5) + "ms");
    Log.debug("    Extensions onLocalNodeStarted(): " + (time7 - time6) + "ms");
    Log.debug("    RuntimeMxBean: " + (time8 - time7) + "ms");
    Log.debug("    Initialize persistence layer: " + (time9 - time8) + "ms");
    Log.debug("    Start network services: " + (time10 - time9) + "ms");
    Log.debug("    Cloud up: " + (time11 - time10) + "ms");
    Log.debug("    Start GA: " + (time12 - time11) + "ms");
}
Also used : GoogleAnalytics(com.brsanthu.googleanalytics.GoogleAnalytics) DefaultRequest(com.brsanthu.googleanalytics.DefaultRequest) RuntimeMXBean(java.lang.management.RuntimeMXBean) H2OFailException(water.exceptions.H2OFailException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 17 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project hazelcast by hazelcast.

the class PhoneHomeTest method testPhoneHomeParameters.

@Test
public void testPhoneHomeParameters() throws Exception {
    Node node1 = TestUtil.getNode(hz1);
    PhoneHome phoneHome = new PhoneHome();
    sleepAtLeastMillis(1);
    Map<String, String> parameters = phoneHome.phoneHome(node1, "test_version", false);
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
    assertEquals(parameters.get("version"), "test_version");
    assertEquals(parameters.get("m"), node1.getLocalMember().getUuid());
    assertEquals(parameters.get("e"), "false");
    assertEquals(parameters.get("l"), "NULL");
    assertEquals(parameters.get("p"), "source");
    assertEquals(parameters.get("crsz"), "A");
    assertEquals(parameters.get("cssz"), "A");
    assertEquals(parameters.get("hdgb"), "0");
    assertEquals(parameters.get("ccpp"), "0");
    assertEquals(parameters.get("cdn"), "0");
    assertEquals(parameters.get("cjv"), "0");
    assertEquals(parameters.get("cnjs"), "0");
    assertEquals(parameters.get("cpy"), "0");
    assertEquals(parameters.get("jetv"), "");
    assertFalse(Integer.parseInt(parameters.get("cuptm")) < 0);
    assertNotEquals(parameters.get("nuptm"), "0");
    assertNotEquals(parameters.get("nuptm"), parameters.get("cuptm"));
    assertEquals(parameters.get("osn"), osMxBean.getName());
    assertEquals(parameters.get("osa"), osMxBean.getArch());
    assertEquals(parameters.get("osv"), osMxBean.getVersion());
    assertEquals(parameters.get("jvmn"), runtimeMxBean.getVmName());
    assertEquals(parameters.get("jvmv"), System.getProperty("java.version"));
}
Also used : Node(com.hazelcast.instance.Node) RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project uavstack by uavorg.

the class JVMToolHelper method getProcessCpuUtilization.

/**
 * obtain current process cpu utilization if jdk version is 1.6 by-hongqiangwei
 */
@SuppressWarnings("restriction")
public static double getProcessCpuUtilization() {
    com.sun.management.OperatingSystemMXBean osMBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    long processCpuTime1 = osMBean.getProcessCpuTime();
    long runtime1 = runtimeMXBean.getUptime();
    ThreadHelper.suspend(50);
    long processCpuTime2 = osMBean.getProcessCpuTime();
    long runtime2 = runtimeMXBean.getUptime();
    long deltaProcessTime = processCpuTime2 - processCpuTime1;
    long deltaRunTime = (runtime2 - runtime1) * 1000000L;
    int cpuNumber = Runtime.getRuntime().availableProcessors();
    double cpuUtilization = (double) deltaProcessTime / (deltaRunTime * cpuNumber);
    return cpuUtilization;
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 19 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project pentaho-kettle by pentaho.

the class GetStatusServlet method getSystemInfo.

private static void getSystemInfo(SlaveServerStatus serverStatus) {
    OperatingSystemMXBean operatingSystemMXBean = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
    ThreadMXBean threadMXBean = java.lang.management.ManagementFactory.getThreadMXBean();
    RuntimeMXBean runtimeMXBean = java.lang.management.ManagementFactory.getRuntimeMXBean();
    int cores = Runtime.getRuntime().availableProcessors();
    long freeMemory = Runtime.getRuntime().freeMemory();
    long totalMemory = Runtime.getRuntime().totalMemory();
    String osArch = operatingSystemMXBean.getArch();
    String osName = operatingSystemMXBean.getName();
    String osVersion = operatingSystemMXBean.getVersion();
    double loadAvg = operatingSystemMXBean.getSystemLoadAverage();
    int threadCount = threadMXBean.getThreadCount();
    long allThreadsCpuTime = 0L;
    long[] threadIds = threadMXBean.getAllThreadIds();
    for (int i = 0; i < threadIds.length; i++) {
        allThreadsCpuTime += threadMXBean.getThreadCpuTime(threadIds[i]);
    }
    long uptime = runtimeMXBean.getUptime();
    serverStatus.setCpuCores(cores);
    serverStatus.setCpuProcessTime(allThreadsCpuTime);
    serverStatus.setUptime(uptime);
    serverStatus.setThreadCount(threadCount);
    serverStatus.setLoadAvg(loadAvg);
    serverStatus.setOsName(osName);
    serverStatus.setOsVersion(osVersion);
    serverStatus.setOsArchitecture(osArch);
    serverStatus.setMemoryFree(freeMemory);
    serverStatus.setMemoryTotal(totalMemory);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 20 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project nifi by apache.

the class SystemDiagnosticsFactory method create.

public SystemDiagnostics create(final FlowFileRepository flowFileRepo, final ContentRepository contentRepo, ProvenanceRepository provenanceRepository) {
    final SystemDiagnostics systemDiagnostics = new SystemDiagnostics();
    final MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
    final MemoryUsage heap = memory.getHeapMemoryUsage();
    final MemoryUsage nonHeap = memory.getNonHeapMemoryUsage();
    final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    final List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
    final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    systemDiagnostics.setDaemonThreads(threads.getDaemonThreadCount());
    systemDiagnostics.setTotalThreads(threads.getThreadCount());
    systemDiagnostics.setTotalHeap(heap.getCommitted());
    systemDiagnostics.setUsedHeap(heap.getUsed());
    systemDiagnostics.setMaxHeap(heap.getMax());
    systemDiagnostics.setTotalNonHeap(nonHeap.getCommitted());
    systemDiagnostics.setUsedNonHeap(nonHeap.getUsed());
    systemDiagnostics.setMaxNonHeap(nonHeap.getMax());
    systemDiagnostics.setUptime(runtime.getUptime());
    systemDiagnostics.setAvailableProcessors(os.getAvailableProcessors());
    final double systemLoad = os.getSystemLoadAverage();
    if (systemLoad >= 0) {
        systemDiagnostics.setProcessorLoadAverage(systemLoad);
    } else {
        systemDiagnostics.setProcessorLoadAverage(-1.0);
    }
    // get the database disk usage
    final StorageUsage flowFileRepoStorageUsage = new StorageUsage();
    flowFileRepoStorageUsage.setIdentifier("FlowFile Repository");
    try {
        flowFileRepoStorageUsage.setFreeSpace(flowFileRepo.getUsableStorageSpace());
        flowFileRepoStorageUsage.setTotalSpace(flowFileRepo.getStorageCapacity());
    } catch (final IOException ioe) {
        flowFileRepoStorageUsage.setFreeSpace(0L);
        flowFileRepoStorageUsage.setTotalSpace(-1L);
        logger.warn("Unable to determine FlowFile Repository usage due to {}", ioe.toString());
        if (logger.isDebugEnabled()) {
            logger.warn("", ioe);
        }
    }
    systemDiagnostics.setFlowFileRepositoryStorageUsage(flowFileRepoStorageUsage);
    // get the file repository disk usage
    final Set<String> containerNames = contentRepo.getContainerNames();
    final Map<String, StorageUsage> fileRepositoryUsage = new LinkedHashMap<>(containerNames.size());
    for (final String containerName : containerNames) {
        long containerCapacity = -1L;
        long containerFree = 0L;
        try {
            containerFree = contentRepo.getContainerUsableSpace(containerName);
            containerCapacity = contentRepo.getContainerCapacity(containerName);
        } catch (final IOException ioe) {
            logger.warn("Unable to determine Content Repository usage for container {} due to {}", containerName, ioe.toString());
            if (logger.isDebugEnabled()) {
                logger.warn("", ioe);
            }
        }
        final StorageUsage storageUsage = new StorageUsage();
        storageUsage.setIdentifier(containerName);
        storageUsage.setFreeSpace(containerFree);
        storageUsage.setTotalSpace(containerCapacity);
        fileRepositoryUsage.put(containerName, storageUsage);
    }
    systemDiagnostics.setContentRepositoryStorageUsage(fileRepositoryUsage);
    // get provenance repository disk usage
    final Set<String> provContainerNames = provenanceRepository.getContainerNames();
    final Map<String, StorageUsage> provRepositoryUsage = new LinkedHashMap<>(provContainerNames.size());
    for (final String containerName : provContainerNames) {
        long containerCapacity = -1L;
        long containerFree = 0L;
        try {
            containerFree = provenanceRepository.getContainerUsableSpace(containerName);
            containerCapacity = provenanceRepository.getContainerCapacity(containerName);
        } catch (final IOException ioe) {
            logger.warn("Unable to determine Provenance Repository usage for container {} due to {}", containerName, ioe.toString());
            if (logger.isDebugEnabled()) {
                logger.warn("", ioe);
            }
        }
        final StorageUsage storageUsage = new StorageUsage();
        storageUsage.setIdentifier(containerName);
        storageUsage.setFreeSpace(containerFree);
        storageUsage.setTotalSpace(containerCapacity);
        provRepositoryUsage.put(containerName, storageUsage);
    }
    systemDiagnostics.setProvenanceRepositoryStorageUsage(provRepositoryUsage);
    // get the garbage collection statistics
    final Map<String, GarbageCollection> garbageCollection = new LinkedHashMap<>(garbageCollectors.size());
    for (final GarbageCollectorMXBean garbageCollector : garbageCollectors) {
        final GarbageCollection garbageCollectionEntry = new GarbageCollection();
        garbageCollectionEntry.setCollectionCount(garbageCollector.getCollectionCount());
        garbageCollectionEntry.setCollectionTime(garbageCollector.getCollectionTime());
        garbageCollection.put(garbageCollector.getName(), garbageCollectionEntry);
    }
    systemDiagnostics.setGarbageCollection(garbageCollection);
    // This information is available only for *nix systems.
    final OperatingSystemMXBean osStats = ManagementFactory.getOperatingSystemMXBean();
    try {
        final Class<?> unixOsMxBeanClass = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
        if (unixOsMxBeanClass.isAssignableFrom(osStats.getClass())) {
            final Method totalPhysicalMemory = unixOsMxBeanClass.getMethod("getTotalPhysicalMemorySize");
            totalPhysicalMemory.setAccessible(true);
            final Long ramBytes = (Long) totalPhysicalMemory.invoke(osStats);
            systemDiagnostics.setTotalPhysicalMemory(ramBytes);
            final Method maxFileDescriptors = unixOsMxBeanClass.getMethod("getMaxFileDescriptorCount");
            maxFileDescriptors.setAccessible(true);
            final Long maxOpenFileDescriptors = (Long) maxFileDescriptors.invoke(osStats);
            systemDiagnostics.setMaxOpenFileHandles(maxOpenFileDescriptors);
            final Method openFileDescriptors = unixOsMxBeanClass.getMethod("getOpenFileDescriptorCount");
            openFileDescriptors.setAccessible(true);
            final Long openDescriptorCount = (Long) openFileDescriptors.invoke(osStats);
            systemDiagnostics.setOpenFileHandles(openDescriptorCount);
        }
    } catch (final Throwable t) {
    // Ignore. This will throw either ClassNotFound or NoClassDefFoundError if unavailable in this JVM.
    }
    // set the creation timestamp
    systemDiagnostics.setCreationTimestamp(new Date().getTime());
    return systemDiagnostics;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) IOException(java.io.IOException) Method(java.lang.reflect.Method) MemoryUsage(java.lang.management.MemoryUsage) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) MemoryMXBean(java.lang.management.MemoryMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Aggregations

RuntimeMXBean (java.lang.management.RuntimeMXBean)164 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)27 IOException (java.io.IOException)17 MemoryMXBean (java.lang.management.MemoryMXBean)16 ThreadMXBean (java.lang.management.ThreadMXBean)13 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)11 Date (java.util.Date)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 MemoryUsage (java.lang.management.MemoryUsage)8 Method (java.lang.reflect.Method)8 Map (java.util.Map)8 Properties (java.util.Properties)6 InputStream (java.io.InputStream)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 Iterator (java.util.Iterator)5 File (java.io.File)4 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)4 Field (java.lang.reflect.Field)4 UnknownHostException (java.net.UnknownHostException)4