Search in sources :

Example 6 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class MonitorModule method info.

@Command(value = "info", description = "监听代码/方法执行信息")
public CommandResponse info(final Map<String, String> args) {
    final String classPattern = args.get("class");
    String methodPattern = args.get("method");
    /**
     * 如果 wait 和 count 都没有填写,则默认统计20条
     */
    final int wait = ParameterUtils.getInt(args, "wait", -1);
    /**
     * 条数限定
     */
    final int limits = ParameterUtils.getInt(args, "limits", 100);
    if (StringUtil.isEmpty(classPattern)) {
        return CommandResponse.failure("class must not be empty.");
    }
    if (StringUtil.isEmpty(methodPattern)) {
        methodPattern = "*";
    }
    EventWatcher watcher = null;
    try {
        if (wait > 10 * 60 * 1000) {
            return CommandResponse.failure("wait 最大等待时间不能超过10分钟");
        }
        final CountDownLatch latch = new CountDownLatch(1);
        Queue<Object> traceViews = new ConcurrentLinkedQueue<Object>();
        watcher = new EventWatchBuilder(moduleEventWatcher).onClass(classPattern).includeSubClasses().onBehavior(methodPattern).withInvoke().withCall().onListener(Listeners.of(MonitorListener.class, new Object[] { latch, traceViews, wait != -1 ? -1 : limits })).onClass().onWatch();
        if (wait > 0) {
            latch.await(wait, TimeUnit.SECONDS);
        } else if (limits > 0) {
            latch.await();
        }
        return CommandResponse.success(traceViews);
    } catch (Throwable e) {
        logger.error("SIMULATOR: monitor module err! class={}, method={}, limits={}, wait={}", classPattern, methodPattern, limits, wait, e);
        return CommandResponse.failure(e);
    } finally {
        if (watcher != null) {
            try {
                watcher.onUnWatched();
            } catch (Throwable e) {
                logger.error("SIMULATOR: monitor module unwatched failed! class={}, method={}, limits={}, wait={}", classPattern, methodPattern, limits, wait, e);
            }
        }
    }
}
Also used : EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) ModuleEventWatcher(com.shulie.instrument.simulator.api.resource.ModuleEventWatcher) EventWatchBuilder(com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 7 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class ModuleManagementModule method reload.

@Command(value = "reload", description = "模块重新加载")
public CommandResponse reload(final Map<String, String> args) throws ModuleException {
    String moduleId = args.get("moduleId");
    int total = 0;
    for (final ModuleSpec moduleSpec : searchByModuleId(moduleId)) {
        try {
            moduleManager.unload(moduleSpec.getModuleId());
        } catch (ModuleException me) {
            logger.warn("SIMULATOR: reload to unload module[id={};] occur error={}.", me.getModuleId(), me.getErrorCode(), me);
        }
        try {
            moduleManager.load(new File(moduleSpec.getFile().getAbsolutePath()));
        } catch (ModuleException me) {
            logger.warn("SIMULATOR: reload to load module[id={};] occur error={}.", me.getModuleId(), me.getErrorCode(), me);
        }
    }
    return CommandResponse.success(String.format("total %s module reloaded.", total));
}
Also used : File(java.io.File) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 8 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class ModuleManagementModule method detail.

@Command(value = "detail", description = "模块详情")
public CommandResponse detail(final Map<String, String> param) throws ModuleException {
    final String moduleId = param.get("moduleId");
    try {
        if (StringUtils.isBlank(moduleId)) {
            // 如果参数不对,则认为找不到对应的仿真器模块,返回400
            return CommandResponse.failure("moduleId parameter was required.");
        }
        if (StringUtils.isNotBlank(moduleId)) {
            for (final ModuleSpec moduleSpec : moduleManager.listModuleSpecs()) {
                if (!StringUtils.equals(moduleSpec.getModuleId(), moduleId)) {
                    continue;
                }
                ModuleInf moduleInf = new ModuleInf();
                moduleInf.setModuleId(moduleSpec.getModuleId());
                moduleInf.setSystemModule(moduleSpec.isSystemModule());
                moduleInf.setExportClasses(moduleSpec.getExportClasses());
                moduleInf.setExportExactlyPackages(moduleSpec.getExportExactlyPackages());
                moduleInf.setExportExactlyResources(moduleSpec.getExportExactlyResources());
                moduleInf.setExportPackages(moduleSpec.getExportPackages());
                moduleInf.setExportPrefixPackages(moduleSpec.getExportPrefixPackages());
                moduleInf.setExportPrefixResources(moduleSpec.getExportPrefixResources());
                moduleInf.setExportResources(moduleSpec.getExportResources());
                moduleInf.setExportSuffixPackages(moduleSpec.getExportSuffixPackages());
                moduleInf.setExportSuffixResources(moduleSpec.getExportSuffixResources());
                moduleInf.setAuthor(moduleSpec.getAuthor());
                moduleInf.setVersion(moduleSpec.getVersion());
                moduleInf.setPriority(moduleSpec.getPriority());
                moduleInf.setSupportedModes(moduleSpec.getSupportedModes());
                moduleInf.setActiveOnLoad(moduleSpec.isActiveOnLoad());
                return CommandResponse.success(moduleId);
            }
        }
        return CommandResponse.failure("moduleId arguments is required.");
    } catch (Throwable e) {
        logger.error("SIMULATOR: module management detail err. moduleId:{}", moduleId, e);
        return CommandResponse.failure(e);
    }
}
Also used : ModuleInf(com.shulie.instrument.simulator.module.mgr.model.ModuleInf) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 9 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class MemoryModule method info.

@Command(value = "info", description = "内存信息")
public CommandResponse info(final Map<String, String> args) {
    try {
        List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
        MemoryInfo memoryInfo = new MemoryInfo();
        // heap
        MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        memoryInfo.setHeapMemory(createMemoryEntry(TYPE_HEAP, heapMemoryUsage));
        List<MemoryEntry> heapMemEntries = new ArrayList<MemoryEntry>();
        for (MemoryPoolMXBean poolMXBean : memoryPoolMXBeans) {
            if (MemoryType.HEAP.equals(poolMXBean.getType())) {
                MemoryUsage usage = poolMXBean.getUsage();
                heapMemEntries.add(createMemoryEntry(poolMXBean.getName(), usage));
            }
        }
        memoryInfo.setHeapMemories(heapMemEntries);
        // non-heap
        MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
        List<MemoryEntry> nonheapMemEntries = new ArrayList<MemoryEntry>();
        memoryInfo.setNonheapMemory(createMemoryEntry(TYPE_NON_HEAP, nonHeapMemoryUsage));
        for (MemoryPoolMXBean poolMXBean : memoryPoolMXBeans) {
            if (MemoryType.NON_HEAP.equals(poolMXBean.getType())) {
                MemoryUsage usage = poolMXBean.getUsage();
                nonheapMemEntries.add(createMemoryEntry(poolMXBean.getName(), usage));
            }
        }
        memoryInfo.setNonheapMemories(nonheapMemEntries);
        memoryInfo.setBufferPoolMemories(getBufferPoolMemoryInfo());
        return CommandResponse.success(memoryInfo);
    } catch (Throwable e) {
        return CommandResponse.failure(e);
    }
}
Also used : MemoryInfo(com.shulie.instrument.simulator.module.model.memory.MemoryInfo) MemoryEntry(com.shulie.instrument.simulator.module.model.memory.MemoryEntry) ArrayList(java.util.ArrayList) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 10 with Command

use of com.shulie.instrument.simulator.api.annotation.Command in project LinkAgent by shulieTech.

the class ProfilerModule method profiler.

@Command(value = "profiler", description = "执行 async-profiler")
public CommandResponse<ProfilerModel> profiler(final Map<String, String> args) {
    try {
        final String action = getParameter(args, "action");
        ProfilerAction profilerAction = ProfilerAction.valueOf(action);
        if (ProfilerAction.actions.equals(profilerAction)) {
            return CommandResponse.success(new ProfilerModel(actions()));
        }
        final String actionArg = getParameter(args, "actionArg");
        final AsyncProfiler asyncProfiler = this.profilerInstance(action, actionArg);
        if (ProfilerAction.execute.equals(profilerAction)) {
            if (actionArg == null) {
                return CommandResponse.failure("actionArg can not be empty.");
            }
            String result = execute(asyncProfiler, actionArg);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.start.equals(profilerAction)) {
            // jfr录制,必须在start的时候就指定文件路径
            final String file = getFile(args);
            final String format = getParameter(args, "format", "svg");
            String executeArgs = executeArgs(ProfilerAction.start, args);
            String result = execute(asyncProfiler, executeArgs);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            Long duration = getLongParameter(args, "duration");
            if (duration != null) {
                final String outputFile = outputFile(file, format);
                profilerModel.setOutputFile(outputFile);
                profilerModel.setDuration(duration);
                // 延时执行stop
                ExecutorServiceFactory.getFactory().schedule(new Runnable() {

                    @Override
                    public void run() {
                        // 在异步线程执行,profiler命令已经结束,不能输出到客户端
                        try {
                            logger.info("stopping profiler ...");
                            ProfilerModel model = processStop(asyncProfiler, args, file, format, action, actionArg);
                            logger.info("profiler output file: " + model.getOutputFile());
                            logger.info("stop profiler successfully.");
                        } catch (Throwable e) {
                            logger.error("stop profiler failure", e);
                        }
                    }
                }, duration, TimeUnit.SECONDS);
            }
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.stop.equals(profilerAction)) {
            final String file = getFile(args);
            final String format = getParameter(args, "format");
            ProfilerModel profilerModel = processStop(asyncProfiler, args, file, format, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.resume.equals(profilerAction)) {
            String executeArgs = executeArgs(ProfilerAction.resume, args);
            String result = execute(asyncProfiler, executeArgs);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.list.equals(profilerAction)) {
            String result = asyncProfiler.execute("list");
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.version.equals(profilerAction)) {
            String result = asyncProfiler.execute("version");
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.status.equals(profilerAction)) {
            String result = asyncProfiler.execute("status");
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.dumpCollapsed.equals(profilerAction)) {
            String actionArgs = actionArg;
            if (actionArgs == null) {
                actionArgs = "TOTAL";
            }
            actionArgs = actionArgs.toUpperCase();
            if ("TOTAL".equals(actionArg) || "SAMPLES".equals(actionArgs)) {
                String result = asyncProfiler.dumpCollapsed(Counter.valueOf(actionArgs));
                ProfilerModel profilerModel = createProfilerModel(result, action, actionArgs);
                return CommandResponse.success(profilerModel);
            } else {
                return CommandResponse.failure("ERROR: dumpCollapsed argument should be TOTAL or SAMPLES. ");
            }
        } else if (ProfilerAction.dumpFlat.equals(profilerAction)) {
            int maxMethods = 0;
            if (actionArg != null) {
                maxMethods = Integer.valueOf(actionArg);
            }
            String result = asyncProfiler.dumpFlat(maxMethods);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.dumpTraces.equals(profilerAction)) {
            int maxTraces = 0;
            if (actionArg != null) {
                maxTraces = Integer.valueOf(actionArg);
            }
            String result = asyncProfiler.dumpTraces(maxTraces);
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        } else if (ProfilerAction.getSamples.equals(profilerAction)) {
            String result = "" + asyncProfiler.getSamples() + "\n";
            ProfilerModel profilerModel = createProfilerModel(result, action, actionArg);
            return CommandResponse.success(profilerModel);
        }
        return CommandResponse.failure("ERROR: unsupported action :" + profilerAction);
    } catch (Throwable e) {
        logger.error("AsyncProfiler error", e);
        return CommandResponse.failure(e);
    }
}
Also used : AsyncProfiler(one.profiler.AsyncProfiler) ProfilerModel(com.shulie.instrument.simulator.module.model.profiler.ProfilerModel) Command(com.shulie.instrument.simulator.api.annotation.Command)

Aggregations

Command (com.shulie.instrument.simulator.api.annotation.Command)27 ArrayList (java.util.ArrayList)10 EventWatchBuilder (com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder)5 EventWatcher (com.shulie.instrument.simulator.api.listener.ext.EventWatcher)5 ModuleEventWatcher (com.shulie.instrument.simulator.api.resource.ModuleEventWatcher)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 File (java.io.File)4 Method (java.lang.reflect.Method)3 List (java.util.List)3 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)3 PullLogResponse (com.shulie.instrument.module.log.data.pusher.log.PullLogResponse)2 NameRegexFilter (com.shulie.instrument.simulator.api.filter.NameRegexFilter)2 ModuleInf (com.shulie.instrument.simulator.module.mgr.model.ModuleInf)2 GcInfo (com.shulie.instrument.simulator.module.model.gc.GcInfo)2 MemoryInfo (com.shulie.instrument.simulator.module.model.memory.MemoryInfo)2 HotSpotDiagnosticMXBean (com.sun.management.HotSpotDiagnosticMXBean)2 VMOption (com.sun.management.VMOption)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 JSONObject (com.alibaba.fastjson.JSONObject)1