|
| 1 | +package com.alibaba.chaosblade.exec.plugin.jvm.gc; |
| 2 | + |
| 3 | +import com.alibaba.chaosblade.exec.common.aop.EnhancerModel; |
| 4 | +import com.alibaba.chaosblade.exec.common.util.ConfigUtil; |
| 5 | +import com.alibaba.chaosblade.exec.plugin.jvm.JvmConstant; |
| 6 | +import com.alibaba.chaosblade.exec.plugin.jvm.StoppableActionExecutor; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import javax.management.*; |
| 11 | +import java.lang.management.ManagementFactory; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | +import java.util.concurrent.ScheduledExecutorService; |
| 15 | +import java.util.concurrent.ThreadFactory; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 18 | +import java.util.concurrent.atomic.AtomicInteger; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author shizhi.zhu@qunar.com |
| 22 | + */ |
| 23 | +public class FullGCExecutor implements StoppableActionExecutor { |
| 24 | + |
| 25 | + private static final Logger LOGGER = LoggerFactory.getLogger(FullGCExecutor.class); |
| 26 | + |
| 27 | + private volatile ScheduledExecutorService scheduledExecutorService; |
| 28 | + private AtomicInteger fullGCCounter = new AtomicInteger(); |
| 29 | + private final AtomicBoolean started = new AtomicBoolean(false); |
| 30 | + |
| 31 | + private final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); |
| 32 | + |
| 33 | + @Override |
| 34 | + public synchronized void run(EnhancerModel enhancerModel) throws Exception { |
| 35 | + if (started.compareAndSet(false, true)) { |
| 36 | + final int interval = ConfigUtil.getActionFlag(enhancerModel, JvmConstant.FLAG_FULL_GC_INTERVAL, 0); |
| 37 | + final int totalCount = ConfigUtil.getActionFlag(enhancerModel, JvmConstant.FLAG_FULL_GC_TOTAL_COUNT, 0); |
| 38 | + scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { |
| 39 | + @Override |
| 40 | + public Thread newThread(Runnable r) { |
| 41 | + return new Thread(r, "chaosblade-fgc-thread"); |
| 42 | + } |
| 43 | + }); |
| 44 | + scheduledExecutorService.scheduleAtFixedRate(new Runnable() { |
| 45 | + @Override |
| 46 | + public void run() { |
| 47 | + try { |
| 48 | + if (totalCount > 0 && fullGCCounter.get() >= totalCount) { |
| 49 | + doStop(); |
| 50 | + return; |
| 51 | + } |
| 52 | + doGc(); |
| 53 | + } catch (Exception e) { |
| 54 | + LOGGER.error("do gcClassHistogram error", e); |
| 55 | + } |
| 56 | + } |
| 57 | + }, 0, interval, TimeUnit.MILLISECONDS); |
| 58 | + } else { |
| 59 | + LOGGER.warn("another executor is running now"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public synchronized void stop(EnhancerModel enhancerModel) throws Exception { |
| 65 | + doStop(); |
| 66 | + } |
| 67 | + |
| 68 | + private void doStop() { |
| 69 | + if (started.compareAndSet(true, false)) { |
| 70 | + if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) { |
| 71 | + try { |
| 72 | + scheduledExecutorService.shutdownNow(); |
| 73 | + } catch (Exception e) { |
| 74 | + LOGGER.error("shutdown executor error", e); |
| 75 | + } |
| 76 | + scheduledExecutorService = null; |
| 77 | + fullGCCounter = new AtomicInteger(); |
| 78 | + LOGGER.info("jvm full gc stopped"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void doGc() throws MalformedObjectNameException, IntrospectionException, ReflectionException, InstanceNotFoundException, MBeanException { |
| 84 | + if (jvmBefore8()) { |
| 85 | + doGCHistogramInSeparateProcess(); |
| 86 | + } else { |
| 87 | + doGcHistogramInMbean(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void doGcHistogramInMbean() throws MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, ReflectionException, MBeanException { |
| 92 | + Set<ObjectName> objectNames = mbeanServer.queryNames(new ObjectName("com.sun.management:type=DiagnosticCommand"), null); |
| 93 | + if (objectNames == null || objectNames.isEmpty()) { |
| 94 | + LOGGER.warn("no mBean found, exit"); |
| 95 | + return; |
| 96 | + } |
| 97 | + for (ObjectName name : objectNames) { |
| 98 | + MBeanInfo mBeanInfo = mbeanServer.getMBeanInfo(name); |
| 99 | + MBeanOperationInfo[] operations = mBeanInfo.getOperations(); |
| 100 | + for (MBeanOperationInfo op : operations) { |
| 101 | + if (op.getName().equals("gcClassHistogram")) { |
| 102 | + String[] emptyStringArgs = {}; |
| 103 | + Object[] dcmdArgs = {emptyStringArgs}; |
| 104 | + String[] signature = {String[].class.getName()}; |
| 105 | + Object invoke = mbeanServer.invoke(name, op.getName(), dcmdArgs, signature); |
| 106 | + int count = fullGCCounter.getAndIncrement(); |
| 107 | + LOGGER.debug("do gc class histogram count:{} \n, {}", count, invoke); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void doGCHistogramInSeparateProcess() { |
| 114 | + int pid = getPid(); |
| 115 | + String cmd = null; |
| 116 | + try { |
| 117 | + String javaHome = System.getenv("JAVA_HOME"); |
| 118 | + if (javaHome.endsWith("/")) { |
| 119 | + javaHome = javaHome.substring(0, javaHome.length() - 1); |
| 120 | + } |
| 121 | + cmd = javaHome + "/bin/jmap"; |
| 122 | + ProcessBuilder pb = new ProcessBuilder(cmd, "-histo:live", String.valueOf(pid)); |
| 123 | + Process start = pb.start(); |
| 124 | + int count = fullGCCounter.getAndIncrement(); |
| 125 | + start.waitFor(); |
| 126 | + LOGGER.debug("do gc class histogram in separate process count:{} cmd:{}", count, cmd); |
| 127 | + } catch (Exception e) { |
| 128 | + LOGGER.error("exec jmap error, cmd:{}", cmd, e); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static int getPid() { |
| 133 | + byte pid = 0; |
| 134 | + try { |
| 135 | + String name = ManagementFactory.getRuntimeMXBean().getName(); |
| 136 | + return Integer.parseInt(name.substring(0, name.indexOf('@'))); |
| 137 | + } catch (Throwable e) { |
| 138 | + return pid; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private boolean jvmBefore8() { |
| 143 | + String javaVersion = System.getProperty("java.version"); |
| 144 | + String[] versions = javaVersion.split("\\."); |
| 145 | + if (Integer.parseInt(versions[1]) >= 8) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + return true; |
| 149 | + } |
| 150 | +} |
0 commit comments