Search in sources :

Example 16 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project cassandra by apache.

the class ToolsTester method assertNoUnexpectedThreadsStarted.

public void assertNoUnexpectedThreadsStarted(String[] expectedThreadNames, String[] optionalThreadNames) {
    ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    Set<String> initial = initialThreads.stream().map(ThreadInfo::getThreadName).collect(Collectors.toSet());
    Set<String> current = Arrays.stream(threads.getThreadInfo(threads.getAllThreadIds())).map(ThreadInfo::getThreadName).collect(Collectors.toSet());
    List<Pattern> expected = expectedThreadNames != null ? Arrays.stream(expectedThreadNames).map(Pattern::compile).collect(Collectors.toList()) : Collections.emptyList();
    List<Pattern> optional = optionalThreadNames != null ? Arrays.stream(optionalThreadNames).map(Pattern::compile).collect(Collectors.toList()) : Collections.emptyList();
    current.removeAll(initial);
    List<Pattern> notPresent = expected.stream().filter(threadNamePattern -> !current.stream().anyMatch(threadName -> threadNamePattern.matcher(threadName).matches())).collect(Collectors.toList());
    Set<String> remain = current.stream().filter(threadName -> expected.stream().anyMatch(pattern -> pattern.matcher(threadName).matches())).filter(threadName -> optional.stream().anyMatch(pattern -> pattern.matcher(threadName).matches())).collect(Collectors.toSet());
    if (!current.isEmpty())
        System.err.println("Unexpected thread names: " + remain);
    if (!notPresent.isEmpty())
        System.err.println("Mandatory thread missing: " + notPresent);
    assertTrue("Wrong thread status", remain.isEmpty() && notPresent.isEmpty());
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) ThreadMXBean(java.lang.management.ThreadMXBean) Collectors(java.util.stream.Collectors) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) ThreadInfo(java.lang.management.ThreadInfo) Permission(java.security.Permission) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.fail(org.junit.Assert.fail) ManagementFactory(java.lang.management.ManagementFactory) Pattern(java.util.regex.Pattern) Method(java.lang.reflect.Method) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ThreadMXBean(java.lang.management.ThreadMXBean) Pattern(java.util.regex.Pattern)

Example 17 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project commons by twitter.

the class JvmStats method register.

/**
   * Add a series of system and jvm-level stats to the given registry.
   */
public static void register(MetricRegistry registry) {
    final MetricRegistry stats = registry.scope("jvm");
    final MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
    // memory stats
    final MetricRegistry heapRegistry = stats.scope("heap");
    registerMemoryStats(heapRegistry, new MemoryReporter() {

        @Override
        public MemoryUsage getUsage() {
            return mem.getHeapMemoryUsage();
        }
    });
    final MetricRegistry nonHeapRegistry = stats.scope("nonheap");
    registerMemoryStats(nonHeapRegistry, new MemoryReporter() {

        @Override
        public MemoryUsage getUsage() {
            return mem.getNonHeapMemoryUsage();
        }
    });
    // threads
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    final MetricRegistry threadRegistry = stats.scope("thread");
    threadRegistry.register(new AbstractGauge<Integer>("daemon_count") {

        @Override
        public Integer read() {
            return threads.getDaemonThreadCount();
        }
    });
    threadRegistry.register(new AbstractGauge<Integer>("count") {

        @Override
        public Integer read() {
            return threads.getThreadCount();
        }
    });
    threadRegistry.register(new AbstractGauge<Integer>("peak_count") {

        @Override
        public Integer read() {
            return threads.getPeakThreadCount();
        }
    });
    // class loading bean
    final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    stats.register(new AbstractGauge<Integer>("classes_loaded") {

        @Override
        public Integer read() {
            return classLoadingBean.getLoadedClassCount();
        }
    });
    stats.register(new AbstractGauge<Long>("total_classes_loaded") {

        @Override
        public Long read() {
            return classLoadingBean.getTotalLoadedClassCount();
        }
    });
    stats.register(new AbstractGauge<Long>("classes_unloaded") {

        @Override
        public Long read() {
            return classLoadingBean.getUnloadedClassCount();
        }
    });
    // runtime
    final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    stats.register(new AbstractGauge<Long>("start_time") {

        @Override
        public Long read() {
            return runtime.getStartTime();
        }
    });
    stats.register(new AbstractGauge<Long>("uptime") {

        @Override
        public Long read() {
            return runtime.getUptime();
        }
    });
    //stats.register(new AbstractGauge<String>)
    // os
    final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    stats.register(new AbstractGauge<Integer>("num_cpus") {

        @Override
        public Integer read() {
            return os.getAvailableProcessors();
        }
    });
    if (os instanceof com.sun.management.OperatingSystemMXBean) {
        final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) os;
        // if this is indeed an operating system
        stats.register(new AbstractGauge<Long>("free_physical_memory") {

            @Override
            public Long read() {
                return sunOsMbean.getFreePhysicalMemorySize();
            }
        });
        stats.register(new AbstractGauge<Long>("free_swap") {

            @Override
            public Long read() {
                return sunOsMbean.getFreeSwapSpaceSize();
            }
        });
        stats.register(new AbstractGauge<Long>("process_cpu_time") {

            @Override
            public Long read() {
                return sunOsMbean.getProcessCpuTime();
            }
        });
    }
    if (os instanceof com.sun.management.UnixOperatingSystemMXBean) {
        // it's a unix system... I know this!
        final UnixOperatingSystemMXBean unix = (UnixOperatingSystemMXBean) os;
        stats.register(new AbstractGauge<Long>("fd_count") {

            @Override
            public Long read() {
                return unix.getOpenFileDescriptorCount();
            }
        });
        stats.register(new AbstractGauge<Long>("fd_limit") {

            @Override
            public Long read() {
                return unix.getMaxFileDescriptorCount();
            }
        });
    }
    // mem
    final List<MemoryPoolMXBean> memPool = ManagementFactory.getMemoryPoolMXBeans();
    final MetricRegistry memRegistry = stats.scope("mem");
    final MetricRegistry currentMem = memRegistry.scope("current");
    final MetricRegistry postGCRegistry = memRegistry.scope("postGC");
    for (final MemoryPoolMXBean pool : memPool) {
        String name = normalizeName(pool.getName());
        registerMemoryStats(currentMem.scope(name), new MemoryReporter() {

            @Override
            public MemoryUsage getUsage() {
                return pool.getUsage();
            }
        });
        registerMemoryStats(postGCRegistry.scope(name), new MemoryReporter() {

            @Override
            public MemoryUsage getUsage() {
                return pool.getCollectionUsage();
            }
        });
    }
    currentMem.register(new AbstractGauge<Long>("used") {

        @Override
        public Long read() {
            long sum = 0;
            for (MemoryPoolMXBean pool : memPool) {
                MemoryUsage usage = pool.getUsage();
                if (usage != null) {
                    sum += usage.getUsed();
                }
            }
            return sum;
        }
    });
    AbstractGauge<Long> totalPostGCGauge = new AbstractGauge<Long>("used") {

        @Override
        public Long read() {
            long sum = 0;
            for (MemoryPoolMXBean pool : memPool) {
                MemoryUsage usage = pool.getCollectionUsage();
                if (usage != null) {
                    sum += usage.getUsed();
                }
            }
            return sum;
        }
    };
    postGCRegistry.register(totalPostGCGauge);
    // java 1.7 specific buffer pool gauges
    Multimap<String, AbstractGauge<Long>> java17gauges = buildJava17Gauges();
    if (!java17gauges.isEmpty()) {
        MetricRegistry bufferRegistry = stats.scope("buffer");
        for (String scope : java17gauges.keySet()) {
            MetricRegistry pool = bufferRegistry.scope(scope);
            for (AbstractGauge<Long> gauge : java17gauges.get(scope)) {
                pool.register(gauge);
            }
        }
    }
    // gc
    final List<GarbageCollectorMXBean> gcPool = ManagementFactory.getGarbageCollectorMXBeans();
    MetricRegistry gcRegistry = stats.scope("gc");
    for (final GarbageCollectorMXBean gc : gcPool) {
        String name = normalizeName(gc.getName());
        MetricRegistry scoped = memRegistry.scope(name);
        scoped.register(new AbstractGauge<Long>("cycles") {

            @Override
            public Long read() {
                return gc.getCollectionCount();
            }
        });
        scoped.register(new AbstractGauge<Long>("msec") {

            @Override
            public Long read() {
                return gc.getCollectionTime();
            }
        });
    }
    gcRegistry.register(new AbstractGauge<Long>("cycles") {

        @Override
        public Long read() {
            long sum = 0;
            for (GarbageCollectorMXBean pool : gcPool) {
                long count = pool.getCollectionCount();
                if (count > 0) {
                    sum += count;
                }
            }
            return sum;
        }
    });
    gcRegistry.register(new AbstractGauge<Long>("msec") {

        @Override
        public Long read() {
            long sum = 0;
            for (GarbageCollectorMXBean pool : gcPool) {
                long msec = pool.getCollectionTime();
                if (msec > 0) {
                    sum += msec;
                }
            }
            return sum;
        }
    });
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) ThreadMXBean(java.lang.management.ThreadMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryUsage(java.lang.management.MemoryUsage) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean)

Example 18 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project bazel by bazelbuild.

the class StackTraces method printAll.

/**
   * Prints all stack traces to the given stream.
   *
   * @param out Stream to print to
   */
public static void printAll(PrintStream out) {
    out.println("Starting full thread dump ...\n");
    ThreadMXBean mb = ManagementFactory.getThreadMXBean();
    // ThreadInfo has comprehensive information such as locks.
    ThreadInfo[] threadInfos = mb.dumpAllThreads(true, true);
    // But we can know whether a thread is daemon only from Thread
    Set<Thread> threads = Thread.getAllStackTraces().keySet();
    Map<Long, Thread> threadMap = new HashMap<>();
    for (Thread thread : threads) {
        threadMap.put(thread.getId(), thread);
    }
    // Dump non-daemon threads first
    for (ThreadInfo threadInfo : threadInfos) {
        Thread thread = threadMap.get(threadInfo.getThreadId());
        if (thread != null && !thread.isDaemon()) {
            dumpThreadInfo(threadInfo, thread, out);
        }
    }
    // Dump daemon threads
    for (ThreadInfo threadInfo : threadInfos) {
        Thread thread = threadMap.get(threadInfo.getThreadId());
        if (thread != null && thread.isDaemon()) {
            dumpThreadInfo(threadInfo, thread, out);
        }
    }
    long[] deadlockedThreads = mb.findDeadlockedThreads();
    if (deadlockedThreads != null) {
        out.println("Detected deadlocked threads: " + Arrays.toString(deadlockedThreads));
    }
    long[] monitorDeadlockedThreads = mb.findMonitorDeadlockedThreads();
    if (monitorDeadlockedThreads != null) {
        out.println("Detected monitor deadlocked threads: " + Arrays.toString(monitorDeadlockedThreads));
    }
    out.println("\nDone full thread dump.");
    out.flush();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) HashMap(java.util.HashMap)

Example 19 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project wycheproof by google.

the class DsaTest method testTiming.

/**
   * This test checks for potential of a timing attack. The test generates a number of signatures,
   * selects a fraction of them with a small timing and then compares the values k for the selected
   * signatures with a normal distribution. The test fails if these ks are much smaller than
   * expected. An implementation flaw that can lead to a test failure is to compute the signature
   * with a modular exponentiation with a runtime that depend on the length of the exponent.
   *
   * <p>A failing test simply means that the timing can be used to get information about k. Further
   * analysis is necessary to determine if the bias is exploitable and how many timings are
   * necessary for an attack. A passing test does not mean that the implementation is secure against
   * timing attacks. The test only catches relatively big timing differences. It requires high
   * confidence to fail. Noise on the test machine can prevent that a relation between timing and k
   * can be detected.
   *
   * <p>Claims of what is exploitable: http://www.hpl.hp.com/techreports/1999/HPL-1999-90.pdf 30
   * signatures are sufficient to find the private key if the attacker knows 8 bits of each k.
   * http://eprint.iacr.org/2004/277.pdf 27 signatures are sufficient if 8 bits of each k is known.
   * Our own old experiments (using 1GB memory on a Pentium-4? CPU): 2^11 signatures are sufficient
   * with a 3 bit leakage. 2^15 signatures are sufficient with a 2 bit leakage. 2^24 signatures are
   * sufficient with a 1 bit leakage. Estimate for biased generation in the NIST standard: e.g. 2^22
   * signatures, 2^40 memory, 2^64 time
   *
   * <p><b>Sample output for the SUN provider:</b> <code>
   * count:50000 cutoff:4629300 relative average:0.9992225872624547 sigmas:0.3010906585642381
   * count:25000 cutoff:733961 relative average:0.976146066585879 sigmas:6.532668708070148
   * count:12500 cutoff:688305 relative average:0.9070352192339134 sigmas:18.00255238454385
   * count:6251 cutoff:673971 relative average:0.7747148791368986 sigmas:30.850903417893825
   * count:3125 cutoff:667045 relative average:0.5901994097874541 sigmas:39.67877152897901
   * count:1563 cutoff:662088 relative average:0.4060286694971057 sigmas:40.67294313795137
   * count:782 cutoff:657921 relative average:0.2577955312387898 sigmas:35.94906247333319
   * count:391 cutoff:653608 relative average:0.1453438859272699 sigmas:29.271192100879457
   * count:196 cutoff:649280 relative average:0.08035497211567771 sigmas:22.300206785132406
   * count:98 cutoff:645122 relative average:0.05063589092661368 sigmas:16.27820353139225
   * count:49 cutoff:641582 relative average:0.018255560447883384 sigmas:11.903018745467488
   * count:25 cutoff:638235 relative average:0.009082660721102722 sigmas:8.581595888660086
   * count:13 cutoff:633975 relative average:0.0067892346039088326 sigmas:6.20259924188633
   * </code>
   *
   * <p><b>What this shows:</b> The first line uses all 50'000 signatures. The average k of these
   * signatures is close to the expected value q/2. Being more selective gives us signatures with a
   * more biased k. For example, the 196 signatures with the fastest timing have about a 3-bit bias.
   * From this we expect that 2^19 signatures and timings are sufficient to find the private key.
   *
   * <p>A list of problems caught by this test:
   * <ul>
   * <li> CVE-2016-5548 OpenJDK8's DSA is vulnerable to timing attacks.
   * <li> CVE-2016-1000341 BouncyCastle before v 1.56 is vulnernerable to timing attacks.
   * </ul>
   */
@SlowTest(providers = { ProviderType.BOUNCY_CASTLE, ProviderType.OPENJDK, ProviderType.SPONGY_CASTLE })
@SuppressWarnings("InsecureCryptoUsage")
public void testTiming() throws Exception {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    if (!bean.isCurrentThreadCpuTimeSupported()) {
        System.out.println("getCurrentThreadCpuTime is not supported. Skipping");
        return;
    }
    String hashAlgorithm = "SHA-1";
    String message = "Hello";
    byte[] messageBytes = message.getBytes("UTF-8");
    byte[] digest = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
    BigInteger h = new BigInteger(1, digest);
    KeyPairGenerator generator = java.security.KeyPairGenerator.getInstance("DSA");
    generator.initialize(1024);
    KeyPair keyPair = generator.generateKeyPair();
    DSAPrivateKey priv = (DSAPrivateKey) keyPair.getPrivate();
    Signature signer = Signature.getInstance("SHA1WITHDSA");
    signer.initSign(priv);
    // The timings below are quite noisy. Thus we need a large number of samples.
    int samples = 50000;
    long[] timing = new long[samples];
    BigInteger[] k = new BigInteger[samples];
    for (int i = 0; i < samples; i++) {
        long start = bean.getCurrentThreadCpuTime();
        signer.update(messageBytes);
        byte[] signature = signer.sign();
        timing[i] = bean.getCurrentThreadCpuTime() - start;
        k[i] = extractK(signature, h, priv, false);
    }
    long[] sorted = Arrays.copyOf(timing, timing.length);
    Arrays.sort(sorted);
    // Here we are only interested in roughly the 8 most significant bits of the ks.
    // Hence, using double is sufficiently precise.
    double q = priv.getParams().getQ().doubleValue();
    double expectedAverage = q / 2;
    double maxSigmas = 0;
    System.out.println("testTiming: SHA1WITHDSA");
    for (int idx = samples - 1; idx > 10; idx /= 2) {
        long cutoff = sorted[idx];
        int count = 0;
        double total = 0;
        for (int i = 0; i < samples; i++) {
            if (timing[i] <= cutoff) {
                total += k[i].doubleValue();
                count += 1;
            }
        }
        double expectedStdDev = q / Math.sqrt(12 * count);
        double average = total / count;
        // Number of standard deviations that the average is away from
        // the expected value:
        double sigmas = (expectedAverage - average) / expectedStdDev;
        if (sigmas > maxSigmas) {
            maxSigmas = sigmas;
        }
        System.out.println("count:" + count + " cutoff:" + cutoff + " relative average:" + (average / expectedAverage) + " sigmas:" + sigmas);
    }
    // than 10^{-10}.
    if (maxSigmas >= 7) {
        fail("Signatures with short timing have a biased k");
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator) Signature(java.security.Signature) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) SlowTest(com.google.security.wycheproof.WycheproofRunner.SlowTest)

Example 20 with ThreadMXBean

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

the class ThreadDumpGenerator method getAllThreads.

public static ThreadInfo[] getAllThreads() {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    if (threadMXBean.isObjectMonitorUsageSupported() && threadMXBean.isSynchronizerUsageSupported()) {
        return threadMXBean.dumpAllThreads(true, true);
    }
    long[] allThreadIds = threadMXBean.getAllThreadIds();
    return getThreadInfos(threadMXBean, allThreadIds);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)87 ThreadInfo (java.lang.management.ThreadInfo)43 HashMap (java.util.HashMap)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 RuntimeMXBean (java.lang.management.RuntimeMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 Date (java.util.Date)4 MonitorInfo (java.lang.management.MonitorInfo)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)2 FileOutputStream (java.io.FileOutputStream)2