15 package com.cloudera.impala.common;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20 import java.io.Writer;
21 import java.lang.management.ManagementFactory;
22 import java.lang.management.MemoryPoolMXBean;
23 import java.lang.management.MemoryUsage;
24 import java.util.ArrayList;
26 import org.apache.thrift.TBase;
27 import org.apache.thrift.TSerializer;
28 import org.apache.thrift.TDeserializer;
29 import org.apache.thrift.TException;
30 import org.apache.thrift.protocol.TBinaryProtocol;
31 import org.apache.thrift.protocol.TProtocolFactory;
33 import com.cloudera.impala.thrift.TGetJvmMetricsRequest;
34 import com.cloudera.impala.thrift.TGetJvmMetricsResponse;
35 import com.cloudera.impala.thrift.TJvmMemoryPool;
42 new TBinaryProtocol.Factory();
50 Writer output =
new StringWriter();
52 output.write(String.format(
"%s: %s", t.getClass().getSimpleName(),
56 while ((cause = cause.getCause()) != null) {
57 output.write(String.format(
"\nCAUSED BY: %s: %s",
58 cause.getClass().getSimpleName(), cause.getMessage()));
60 }
catch (IOException e) {
63 return output.toString();
70 Writer output =
new StringWriter();
71 t.printStackTrace(
new PrintWriter(output));
72 return output.toString();
78 public static <T extends TBase<?, ?>, F extends TProtocolFactory>
82 TDeserializer deserializer =
new TDeserializer(protocolFactory);
84 deserializer.deserialize(result, thriftData);
85 }
catch (TException e) {
96 TGetJvmMetricsRequest request =
new TGetJvmMetricsRequest();
99 TGetJvmMetricsResponse jvmMetrics =
new TGetJvmMetricsResponse();
100 jvmMetrics.setMemory_pools(
new ArrayList<TJvmMemoryPool>());
101 TJvmMemoryPool totalUsage =
new TJvmMemoryPool();
103 request.getMemory_pool() != null && request.getMemory_pool().equals(
"total");
105 if (request.get_all || is_total) {
106 totalUsage.setName(
"total");
107 jvmMetrics.getMemory_pools().add(totalUsage);
109 for (MemoryPoolMXBean memBean: ManagementFactory.getMemoryPoolMXBeans()) {
110 if (request.get_all || is_total ||
111 memBean.getName().equals(request.getMemory_pool())) {
112 TJvmMemoryPool usage =
new TJvmMemoryPool();
113 MemoryUsage beanUsage = memBean.getUsage();
114 usage.setCommitted(beanUsage.getCommitted());
115 usage.setInit(beanUsage.getInit());
116 usage.setMax(beanUsage.getMax());
117 usage.setUsed(beanUsage.getUsed());
118 usage.setName(memBean.getName());
120 totalUsage.committed += beanUsage.getCommitted();
121 totalUsage.init += beanUsage.getInit();
122 totalUsage.max += beanUsage.getMax();
123 totalUsage.used += beanUsage.getUsed();
125 MemoryUsage peakUsage = memBean.getPeakUsage();
126 usage.setPeak_committed(peakUsage.getCommitted());
127 usage.setPeak_init(peakUsage.getInit());
128 usage.setPeak_max(peakUsage.getMax());
129 usage.setPeak_used(peakUsage.getUsed());
131 totalUsage.peak_committed += peakUsage.getCommitted();
132 totalUsage.peak_init += peakUsage.getInit();
133 totalUsage.peak_max += peakUsage.getMax();
134 totalUsage.peak_used += peakUsage.getUsed();
137 jvmMetrics.getMemory_pools().add(usage);
138 if (!request.get_all)
break;
142 TSerializer serializer =
new TSerializer(protocolFactory_);
144 return serializer.serialize(jvmMetrics);
145 }
catch (TException e) {
154 StringBuilder sb =
new StringBuilder();
155 sb.append(
"Java Version Info: ");
156 sb.append(System.getProperty(
"java.runtime.name"));
158 sb.append(System.getProperty(
"java.runtime.version"));
160 return sb.toString();
static String getJavaVersion()
static< TextendsTBase<?,?> F extends TProtocolFactory void deserializeThrift(F protocolFactory, T result, byte[] thriftData)
static String throwableToString(Throwable t)
static final TBinaryProtocol.Factory protocolFactory_
static byte[] getJvmMetrics(byte[] argument)
static String throwableToStackTrace(Throwable t)