Search in sources :

Example 26 with ThreadInfo

use of java.lang.management.ThreadInfo in project jdk8u_jdk by JetBrains.

the class ThreadInfoCompositeData method createV5ThreadInfo.

public static void createV5ThreadInfo() throws Exception {
    String[] v5ItemNames = new String[NUM_V5_ATTS];
    OpenType[] v5ItemTypes = new OpenType[NUM_V5_ATTS];
    Object[] v5ItemValues = new Object[NUM_V5_ATTS];
    for (int i = 0; i < NUM_V5_ATTS; i++) {
        v5ItemNames[i] = validItemNames[i];
        v5ItemTypes[i] = validItemTypes[i];
        v5ItemValues[i] = values[i];
    }
    CompositeType ct = new CompositeType("MyCompositeType", "CompositeType for JDK 5.0 ThreadInfo", v5ItemNames, v5ItemNames, v5ItemTypes);
    CompositeData cd = new CompositeDataSupport(ct, v5ItemNames, v5ItemValues);
    ThreadInfo info = ThreadInfo.from(cd);
    checkThreadInfo(info);
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 27 with ThreadInfo

use of java.lang.management.ThreadInfo in project symmetric-ds by JumpMind.

the class SnapshotUtil method createSnapshot.

public static File createSnapshot(ISymmetricEngine engine) {
    String dirName = engine.getEngineName().replaceAll(" ", "-") + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    IParameterService parameterService = engine.getParameterService();
    File tmpDir = new File(parameterService.getTempDirectory(), dirName);
    tmpDir.mkdirs();
    File logDir = null;
    String parameterizedLogDir = parameterService.getString("server.log.dir");
    if (isNotBlank(parameterizedLogDir)) {
        logDir = new File(parameterizedLogDir);
    }
    if (logDir != null && logDir.exists()) {
        log.info("Using server.log.dir setting as the location of the log files");
    } else {
        logDir = new File("logs");
        if (!logDir.exists()) {
            Map<File, Layout> matches = findSymmetricLogFile();
            if (matches != null && matches.size() == 1) {
                logDir = matches.keySet().iterator().next().getParentFile();
            }
        }
        if (!logDir.exists()) {
            logDir = new File("../logs");
        }
        if (!logDir.exists()) {
            logDir = new File("target");
        }
        if (logDir.exists()) {
            File[] files = logDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.getName().toLowerCase().endsWith(".log")) {
                        try {
                            FileUtils.copyFileToDirectory(file, tmpDir);
                        } catch (IOException e) {
                            log.warn("Failed to copy " + file.getName() + " to the snapshot directory", e);
                        }
                    }
                }
            }
        }
    }
    FileWriter fwriter = null;
    try {
        fwriter = new FileWriter(new File(tmpDir, "config-export.csv"));
        engine.getDataExtractorService().extractConfigurationStandalone(engine.getNodeService().findIdentity(), fwriter, TableConstants.SYM_NODE, TableConstants.SYM_NODE_SECURITY, TableConstants.SYM_NODE_IDENTITY, TableConstants.SYM_NODE_HOST, TableConstants.SYM_NODE_CHANNEL_CTL, TableConstants.SYM_CONSOLE_USER, TableConstants.SYM_MONITOR_EVENT, TableConstants.SYM_CONSOLE_EVENT);
    } catch (Exception e) {
        log.warn("Failed to export symmetric configuration", e);
    } finally {
        IOUtils.closeQuietly(fwriter);
    }
    File serviceConfFile = new File("conf/sym_service.conf");
    try {
        if (serviceConfFile.exists()) {
            FileUtils.copyFileToDirectory(serviceConfFile, tmpDir);
        }
    } catch (Exception e) {
        log.warn("Failed to copy " + serviceConfFile.getName() + " to the snapshot directory", e);
    }
    TreeSet<Table> tables = new TreeSet<Table>();
    FileOutputStream fos = null;
    try {
        ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
        List<TriggerHistory> triggerHistories = triggerRouterService.getActiveTriggerHistories();
        for (TriggerHistory triggerHistory : triggerHistories) {
            Table table = engine.getDatabasePlatform().getTableFromCache(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), false);
            if (table != null && !table.getName().toUpperCase().startsWith(engine.getSymmetricDialect().getTablePrefix().toUpperCase())) {
                tables.add(table);
            }
        }
        List<Trigger> triggers = triggerRouterService.getTriggers();
        for (Trigger trigger : triggers) {
            Table table = engine.getDatabasePlatform().getTableFromCache(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName(), false);
            if (table != null) {
                tables.add(table);
            }
        }
        fos = new FileOutputStream(new File(tmpDir, "table-definitions.xml"));
        DbExport export = new DbExport(engine.getDatabasePlatform());
        export.setFormat(Format.XML);
        export.setNoData(true);
        export.exportTables(fos, tables.toArray(new Table[tables.size()]));
    } catch (Exception e) {
        log.warn("Failed to export table definitions", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    String tablePrefix = engine.getTablePrefix();
    DbExport export = new DbExport(engine.getDatabasePlatform());
    export.setFormat(Format.CSV);
    export.setNoCreateInfo(true);
    extract(export, new File(tmpDir, "sym_identity.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_IDENTITY));
    extract(export, new File(tmpDir, "sym_node.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE));
    extract(export, new File(tmpDir, "sym_node_security.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_SECURITY));
    extract(export, new File(tmpDir, "sym_node_host.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_HOST));
    extract(export, new File(tmpDir, "sym_trigger_hist.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_TRIGGER_HIST));
    try {
        if (!parameterService.is(ParameterConstants.CLUSTER_LOCKING_ENABLED)) {
            engine.getNodeCommunicationService().persistToTableForSnapshot();
            engine.getClusterService().persistToTableForSnapshot();
        }
    } catch (Exception e) {
        log.warn("Unable to add SYM_NODE_COMMUNICATION to the snapshot.", e);
    }
    extract(export, new File(tmpDir, "sym_lock.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_LOCK));
    extract(export, new File(tmpDir, "sym_node_communication.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_COMMUNICATION));
    extract(export, 10000, "order by create_time desc", new File(tmpDir, "sym_outgoing_batch.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_OUTGOING_BATCH));
    extract(export, 10000, "where status != 'OK' order by create_time", new File(tmpDir, "sym_outgoing_batch_not_ok.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_OUTGOING_BATCH));
    extract(export, 10000, "order by create_time desc", new File(tmpDir, "sym_incoming_batch.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_INCOMING_BATCH));
    extract(export, 10000, "where status != 'OK' order by create_time", new File(tmpDir, "sym_incoming_batch_not_ok.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_INCOMING_BATCH));
    extract(export, 5000, "order by start_id, end_id desc", new File(tmpDir, "sym_data_gap.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_DATA_GAP));
    extract(export, new File(tmpDir, "sym_table_reload_request.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST));
    extract(export, 5000, "order by relative_dir, file_name", new File(tmpDir, "sym_file_snapshot.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_FILE_SNAPSHOT));
    extract(export, new File(tmpDir, "sym_console_event.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_CONSOLE_EVENT));
    extract(export, new File(tmpDir, "sym_monitor_event.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_MONITOR_EVENT));
    extract(export, new File(tmpDir, "sym_extract_request.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_EXTRACT_REQUEST));
    if (engine.getSymmetricDialect() instanceof FirebirdSymmetricDialect) {
        final String[] monTables = { "mon$database", "mon$attachments", "mon$transactions", "mon$statements", "mon$io_stats", "mon$record_stats", "mon$memory_usage", "mon$call_stack", "mon$context_variables" };
        for (String table : monTables) {
            extract(export, new File(tmpDir, "firebird-" + table + ".csv"), table);
        }
    }
    fwriter = null;
    try {
        fwriter = new FileWriter(new File(tmpDir, "threads.txt"));
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        long[] threadIds = threadBean.getAllThreadIds();
        for (long l : threadIds) {
            ThreadInfo info = threadBean.getThreadInfo(l, 100);
            if (info != null) {
                String threadName = info.getThreadName();
                fwriter.append(StringUtils.rightPad(threadName, THREAD_INDENT_SPACE));
                fwriter.append(AppUtils.formatStackTrace(info.getStackTrace(), THREAD_INDENT_SPACE, false));
                fwriter.append("\n");
            }
        }
    } catch (Exception e) {
        log.warn("Failed to export thread information", e);
    } finally {
        IOUtils.closeQuietly(fwriter);
    }
    fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "parameters.properties"));
        Properties effectiveParameters = engine.getParameterService().getAllParameters();
        SortedProperties parameters = new SortedProperties();
        parameters.putAll(effectiveParameters);
        parameters.remove("db.password");
        parameters.store(fos, "parameters.properties");
    } catch (IOException e) {
        log.warn("Failed to export parameter information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "parameters-changed.properties"));
        Properties defaultParameters = new Properties();
        InputStream in = SnapshotUtil.class.getResourceAsStream("/symmetric-default.properties");
        defaultParameters.load(in);
        IOUtils.closeQuietly(in);
        in = SnapshotUtil.class.getResourceAsStream("/symmetric-console-default.properties");
        if (in != null) {
            defaultParameters.load(in);
            IOUtils.closeQuietly(in);
        }
        Properties effectiveParameters = engine.getParameterService().getAllParameters();
        Properties changedParameters = new SortedProperties();
        Map<String, ParameterMetaData> parameters = ParameterConstants.getParameterMetaData();
        for (String key : parameters.keySet()) {
            String defaultValue = defaultParameters.getProperty((String) key);
            String currentValue = effectiveParameters.getProperty((String) key);
            if (defaultValue == null && currentValue != null || (defaultValue != null && !defaultValue.equals(currentValue))) {
                changedParameters.put(key, currentValue == null ? "" : currentValue);
            }
        }
        changedParameters.remove("db.password");
        changedParameters.store(fos, "parameters-changed.properties");
    } catch (Exception e) {
        log.warn("Failed to export parameters-changed information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    writeRuntimeStats(engine, tmpDir);
    writeJobsStats(engine, tmpDir);
    if ("true".equals(System.getProperty(SystemConstants.SYSPROP_STANDALONE_WEB))) {
        writeDirectoryListing(engine, tmpDir);
    }
    fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "system.properties"));
        SortedProperties props = new SortedProperties();
        props.putAll(System.getProperties());
        props.store(fos, "system.properties");
    } catch (Exception e) {
        log.warn("Failed to export thread information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    try {
        File jarFile = new File(getSnapshotDirectory(engine), tmpDir.getName() + ".zip");
        JarBuilder builder = new JarBuilder(tmpDir, jarFile, new File[] { tmpDir }, Version.version());
        builder.build();
        FileUtils.deleteDirectory(tmpDir);
        return jarFile;
    } catch (Exception e) {
        throw new IoException("Failed to package snapshot files into archive", e);
    }
}
Also used : FileWriter(java.io.FileWriter) JarBuilder(org.jumpmind.util.JarBuilder) IParameterService(org.jumpmind.symmetric.service.IParameterService) Properties(java.util.Properties) Trigger(org.jumpmind.symmetric.model.Trigger) ThreadInfo(java.lang.management.ThreadInfo) TreeSet(java.util.TreeSet) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) ThreadMXBean(java.lang.management.ThreadMXBean) Table(org.jumpmind.db.model.Table) FirebirdSymmetricDialect(org.jumpmind.symmetric.db.firebird.FirebirdSymmetricDialect) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.util.Date) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) DbExport(org.jumpmind.symmetric.io.data.DbExport) Layout(org.apache.log4j.Layout) FileOutputStream(java.io.FileOutputStream) IoException(org.jumpmind.exception.IoException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) ParameterMetaData(org.jumpmind.properties.DefaultParameterParser.ParameterMetaData)

Example 28 with ThreadInfo

use of java.lang.management.ThreadInfo in project jdk8u_jdk by JetBrains.

the class AnnotationTypeDeadlockTest method main.

public static void main(String[] args) throws Exception {
    CountDownLatch prepareLatch = new CountDownLatch(2);
    AtomicInteger goLatch = new AtomicInteger(1);
    Task taskA = new Task(prepareLatch, goLatch, AnnA.class);
    Task taskB = new Task(prepareLatch, goLatch, AnnB.class);
    taskA.start();
    taskB.start();
    // wait until both threads start-up
    prepareLatch.await();
    // let them go
    goLatch.set(0);
    // obtain ThreadMXBean
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    // wait for threads to finish or dead-lock
    while (taskA.isAlive() || taskB.isAlive()) {
        // attempt to join threads
        taskA.join(500L);
        taskB.join(500L);
        // detect dead-lock
        long[] deadlockedIds = threadBean.findMonitorDeadlockedThreads();
        if (deadlockedIds != null && deadlockedIds.length > 0) {
            StringBuilder sb = new StringBuilder("deadlock detected:\n\n");
            for (ThreadInfo ti : threadBean.getThreadInfo(deadlockedIds, Integer.MAX_VALUE)) {
                sb.append(ti);
            }
            throw new IllegalStateException(sb.toString());
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 29 with ThreadInfo

use of java.lang.management.ThreadInfo in project tdi-studio-se by Talend.

the class MBeanServer method sampleProfilingData.

/**
     * Samples the profiling data.
     * 
     * @throws JvmCoreException
     */
void sampleProfilingData() throws JvmCoreException {
    if (!checkReachability()) {
        return;
    }
    ThreadMXBean threadMXBean;
    try {
        threadMXBean = (ThreadMXBean) getMXBean(ThreadMXBean.class, ManagementFactory.THREAD_MXBEAN_NAME);
    } catch (IOException e) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.getMBeanFailedMsg, ManagementFactory.THREAD_MXBEAN_NAME), e);
    }
    if (threadMXBean == null) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.getMBeanFailedMsg, ManagementFactory.THREAD_MXBEAN_NAME), null);
    }
    CpuModel cpuModel = (CpuModel) jvm.getCpuProfiler().getCpuModel();
    long samplingTime = System.currentTimeMillis();
    long actualSamplingPeriodInMilliSeconds;
    if (previousSamplingTime == 0) {
        actualSamplingPeriodInMilliSeconds = samplingPeriod;
    } else {
        actualSamplingPeriodInMilliSeconds = samplingTime - previousSamplingTime;
    }
    Set<String> profiledPackages = jvm.getCpuProfiler().getProfiledPackages();
    for (ThreadInfo threadInfo : threadMXBean.dumpAllThreads(true, false)) {
        StackTraceElement[] stackTrace = threadInfo.getStackTrace();
        String threadName = threadInfo.getThreadName();
        if (//$NON-NLS-1$
        stackTrace.length > 0 && !threadName.startsWith("JMX ") && !threadName.startsWith("RMI ")) {
            //$NON-NLS-1$
            ThreadNode<CallTreeNode> callTreeThreadNode = cpuModel.getCallTreeThread(threadName);
            ThreadNode<MethodNode> hotSpotThreadNode = cpuModel.getHotSpotThread(threadName);
            if (callTreeThreadNode == null) {
                callTreeThreadNode = new ThreadNode<CallTreeNode>(threadName);
            }
            if (hotSpotThreadNode == null) {
                hotSpotThreadNode = new ThreadNode<MethodNode>(threadName);
            }
            updateCpuModel(callTreeThreadNode, hotSpotThreadNode, profiledPackages, invertStackTrace(stackTrace), actualSamplingPeriodInMilliSeconds);
            if (callTreeThreadNode.hasChildren()) {
                cpuModel.addCallTreeThread(callTreeThreadNode);
            }
            if (hotSpotThreadNode.hasChildren()) {
                cpuModel.addHotSpotThread(hotSpotThreadNode);
            }
        }
    }
    previousSamplingTime = samplingTime;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) CpuModel(org.talend.designer.runtime.visualization.internal.core.cpu.CpuModel) CallTreeNode(org.talend.designer.runtime.visualization.internal.core.cpu.CallTreeNode) IOException(java.io.IOException) JvmCoreException(org.talend.designer.runtime.visualization.JvmCoreException) ThreadInfo(java.lang.management.ThreadInfo) MethodNode(org.talend.designer.runtime.visualization.internal.core.cpu.MethodNode)

Example 30 with ThreadInfo

use of java.lang.management.ThreadInfo in project tdi-studio-se by Talend.

the class MBeanServer method refreshThreadCache.

/*
     * @see IMBeanServer#refreshThreadCache()
     */
@Override
public void refreshThreadCache() throws JvmCoreException {
    if (!checkReachability()) {
        return;
    }
    ThreadMXBean threadMXBean;
    try {
        threadMXBean = (ThreadMXBean) getMXBean(ThreadMXBean.class, ManagementFactory.THREAD_MXBEAN_NAME);
    } catch (IOException e) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.getMBeanFailedMsg, ManagementFactory.THREAD_MXBEAN_NAME), e);
    }
    if (threadMXBean == null) {
        throw new JvmCoreException(IStatus.ERROR, NLS.bind(Messages.getMBeanFailedMsg, ManagementFactory.THREAD_MXBEAN_NAME), null);
    }
    long[] ids = threadMXBean.findDeadlockedThreads();
    LinkedHashMap<String, ThreadElement> newThreadListElements = new LinkedHashMap<String, ThreadElement>();
    List<ThreadInfo> allThreads = Arrays.asList(threadMXBean.dumpAllThreads(true, false));
    Collections.reverse(allThreads);
    for (ThreadInfo threadInfo : allThreads) {
        String threadName = threadInfo.getThreadName();
        long threadId = threadInfo.getThreadId();
        if (//$NON-NLS-1$
        threadInfo.getStackTrace().length == 0 || threadName.startsWith("RMI ") || threadName.startsWith("JMX ")) {
            //$NON-NLS-1$
            continue;
        }
        boolean isDeadlocked = false;
        if (ids != null) {
            Arrays.sort(ids);
            if (Arrays.binarySearch(ids, threadId) >= 0) {
                isDeadlocked = true;
            }
        }
        ThreadElement oldElement = threadListElements.get(threadName);
        long processCpuTime = threadMXBean.getThreadCpuTime(threadId);
        Long previousCpuTime = previousThreadProcessCpuTime.get(threadId);
        double cpuUsage = 0;
        previousThreadProcessCpuTime.put(threadId, processCpuTime);
        if (previousCpuTime != null) {
            cpuUsage = Math.min((processCpuTime - previousCpuTime) / 10000000d, 100);
        }
        previousThreadProcessCpuTime.put(threadId, processCpuTime);
        if (oldElement == null) {
            newThreadListElements.put(threadName, new ThreadElement(threadInfo, isDeadlocked, cpuUsage));
        } else {
            oldElement.setThreadInfo(threadInfo);
            oldElement.setDeadlocked(isDeadlocked);
            oldElement.setCpuUsage(cpuUsage);
            newThreadListElements.put(threadName, oldElement);
        }
    }
    threadListElements = newThreadListElements;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) IOException(java.io.IOException) IThreadElement(org.talend.designer.runtime.visualization.IThreadElement) JvmCoreException(org.talend.designer.runtime.visualization.JvmCoreException) LinkedHashMap(java.util.LinkedHashMap) ThreadInfo(java.lang.management.ThreadInfo)

Aggregations

ThreadInfo (java.lang.management.ThreadInfo)107 ThreadMXBean (java.lang.management.ThreadMXBean)43 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)6 Map (java.util.Map)6 LockInfo (java.lang.management.LockInfo)5 MonitorInfo (java.lang.management.MonitorInfo)5 Date (java.util.Date)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 PrintStream (java.io.PrintStream)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 LinkedHashSet (java.util.LinkedHashSet)3 TreeMap (java.util.TreeMap)3 ExecutorService (java.util.concurrent.ExecutorService)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2