Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JniUtil.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.common;
16 
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;
25 
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;
32 
33 import com.cloudera.impala.thrift.TGetJvmMetricsRequest;
34 import com.cloudera.impala.thrift.TGetJvmMetricsResponse;
35 import com.cloudera.impala.thrift.TJvmMemoryPool;
36 
40 public class JniUtil {
41  private final static TBinaryProtocol.Factory protocolFactory_ =
42  new TBinaryProtocol.Factory();
43 
49  public static String throwableToString(Throwable t) {
50  Writer output = new StringWriter();
51  try {
52  output.write(String.format("%s: %s", t.getClass().getSimpleName(),
53  t.getMessage()));
54  // Follow the chain of exception causes and print them as well.
55  Throwable cause = t;
56  while ((cause = cause.getCause()) != null) {
57  output.write(String.format("\nCAUSED BY: %s: %s",
58  cause.getClass().getSimpleName(), cause.getMessage()));
59  }
60  } catch (IOException e) {
61  throw new Error(e);
62  }
63  return output.toString();
64  }
65 
69  public static String throwableToStackTrace(Throwable t) {
70  Writer output = new StringWriter();
71  t.printStackTrace(new PrintWriter(output));
72  return output.toString();
73  }
74 
78  public static <T extends TBase<?, ?>, F extends TProtocolFactory>
79  void deserializeThrift(F protocolFactory, T result, byte[] thriftData)
80  throws ImpalaException {
81  // TODO: avoid creating deserializer for each query?
82  TDeserializer deserializer = new TDeserializer(protocolFactory);
83  try {
84  deserializer.deserialize(result, thriftData);
85  } catch (TException e) {
86  throw new InternalException(e.getMessage());
87  }
88  }
89 
95  public static byte[] getJvmMetrics(byte[] argument) throws ImpalaException {
96  TGetJvmMetricsRequest request = new TGetJvmMetricsRequest();
97  JniUtil.deserializeThrift(protocolFactory_, request, argument);
98 
99  TGetJvmMetricsResponse jvmMetrics = new TGetJvmMetricsResponse();
100  jvmMetrics.setMemory_pools(new ArrayList<TJvmMemoryPool>());
101  TJvmMemoryPool totalUsage = new TJvmMemoryPool();
102  boolean is_total =
103  request.getMemory_pool() != null && request.getMemory_pool().equals("total");
104 
105  if (request.get_all || is_total) {
106  totalUsage.setName("total");
107  jvmMetrics.getMemory_pools().add(totalUsage);
108  }
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());
119 
120  totalUsage.committed += beanUsage.getCommitted();
121  totalUsage.init += beanUsage.getInit();
122  totalUsage.max += beanUsage.getMax();
123  totalUsage.used += beanUsage.getUsed();
124 
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());
130 
131  totalUsage.peak_committed += peakUsage.getCommitted();
132  totalUsage.peak_init += peakUsage.getInit();
133  totalUsage.peak_max += peakUsage.getMax();
134  totalUsage.peak_used += peakUsage.getUsed();
135 
136  if (!is_total) {
137  jvmMetrics.getMemory_pools().add(usage);
138  if (!request.get_all) break;
139  }
140  }
141  }
142  TSerializer serializer = new TSerializer(protocolFactory_);
143  try {
144  return serializer.serialize(jvmMetrics);
145  } catch (TException e) {
146  throw new InternalException(e.getMessage());
147  }
148  }
149 
153  public static String getJavaVersion() {
154  StringBuilder sb = new StringBuilder();
155  sb.append("Java Version Info: ");
156  sb.append(System.getProperty("java.runtime.name"));
157  sb.append(" (");
158  sb.append(System.getProperty("java.runtime.version"));
159  sb.append(")");
160  return sb.toString();
161  }
162 }
static< TextendsTBase<?,?> F extends TProtocolFactory void deserializeThrift(F protocolFactory, T result, byte[] thriftData)
Definition: JniUtil.java:79
static String throwableToString(Throwable t)
Definition: JniUtil.java:49
static final TBinaryProtocol.Factory protocolFactory_
Definition: JniUtil.java:41
static byte[] getJvmMetrics(byte[] argument)
Definition: JniUtil.java:95
static String throwableToStackTrace(Throwable t)
Definition: JniUtil.java:69