|
20 | 20 | import com.alipay.lookout.api.Id;
|
21 | 21 | import com.alipay.lookout.api.Registry;
|
22 | 22 | import com.alipay.lookout.api.composite.MixinMetric;
|
| 23 | +import com.alipay.lookout.common.log.LookoutLoggerFactory; |
23 | 24 | import com.alipay.lookout.spi.MetricsImporter;
|
| 25 | +import org.slf4j.Logger; |
24 | 26 |
|
25 | 27 | import java.lang.management.ManagementFactory;
|
| 28 | +import java.lang.management.MemoryPoolMXBean; |
| 29 | +import java.util.List; |
26 | 30 |
|
27 | 31 | /**
|
28 | 32 | * Created by [email protected] on 2017/2/16.
|
29 | 33 | */
|
30 | 34 | public class JvmMemoryMetricsImporter implements MetricsImporter {
|
| 35 | + private static final Logger LOGGER = LookoutLoggerFactory |
| 36 | + .getLogger(JvmMemoryMetricsImporter.class); |
| 37 | + private static final String CODE_CACHE_NAME = "Code Cache"; |
| 38 | + |
31 | 39 | @Override
|
32 | 40 | public void register(Registry registry) {
|
33 | 41 | Id id = registry.createId("jvm.memory");
|
34 | 42 | MixinMetric mixin = registry.mixinMetric(id);
|
35 | 43 | heapImport(mixin);
|
36 | 44 | nonheapImport(mixin);
|
| 45 | + codeCacheImport(mixin); |
37 | 46 | }
|
38 | 47 |
|
39 | 48 | private void nonheapImport(MixinMetric mixin) {
|
@@ -90,4 +99,48 @@ public Long value() {
|
90 | 99 | }
|
91 | 100 | });
|
92 | 101 | }
|
| 102 | + |
| 103 | + private void codeCacheImport(MixinMetric mixin) { |
| 104 | + final MemoryPoolMXBean codeCacheMXBean = getCodeCacheMXBean(); |
| 105 | + if (codeCacheMXBean == null) { |
| 106 | + LOGGER.info("can't get code cache MemoryPoolMXBean, won't report code cache usage."); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + mixin.gauge("codecache.init", new Gauge<Long>() { |
| 111 | + @Override |
| 112 | + public Long value() { |
| 113 | + return codeCacheMXBean.getUsage().getInit(); |
| 114 | + } |
| 115 | + }); |
| 116 | + mixin.gauge("codecache.used", new Gauge<Long>() { |
| 117 | + @Override |
| 118 | + public Long value() { |
| 119 | + return codeCacheMXBean.getUsage().getUsed(); |
| 120 | + } |
| 121 | + }); |
| 122 | + mixin.gauge("codecache.committed", new Gauge<Long>() { |
| 123 | + @Override |
| 124 | + public Long value() { |
| 125 | + return codeCacheMXBean.getUsage().getCommitted(); |
| 126 | + } |
| 127 | + }); |
| 128 | + mixin.gauge("codecache.max", new Gauge<Long>() { |
| 129 | + @Override |
| 130 | + public Long value() { |
| 131 | + return codeCacheMXBean.getUsage().getMax(); |
| 132 | + } |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + private MemoryPoolMXBean getCodeCacheMXBean() { |
| 137 | + List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); |
| 138 | + for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) { |
| 139 | + if (CODE_CACHE_NAME.equals(memoryPoolMXBean.getName())) { |
| 140 | + return memoryPoolMXBean; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
93 | 146 | }
|
0 commit comments