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