<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-6567270109709132115</atom:id><lastBuildDate>Sun, 01 Sep 2024 17:45:03 +0000</lastBuildDate><category>java profiling monitoring btrace performance</category><title>JarQuad</title><description>Mostly about Java, performance and other annoyances.</description><link>http://jarquad.blogspot.com/</link><managingEditor>noreply@blogger.com (Enthusiast)</managingEditor><generator>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6567270109709132115.post-2683635879178418815</guid><pubDate>Tue, 22 Oct 2013 20:34:00 +0000</pubDate><atom:updated>2013-10-22T22:34:31.920+02:00</atom:updated><title>Squeezing JMX through firewall</title><description>Getting JMX connection working through a firewall seems to be a recurring topic and a great source of confusion.&lt;br /&gt;
&lt;br /&gt;
Typically, you use something like this:&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; font-size: 14px; line-height: 18px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=&amp;lt;some port&amp;gt;
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false 
-Djava.rmi.server.hostname=localhost&lt;/code&gt;&lt;/pre&gt;
to start the application with the JMX enabled for remote connection and then configure port forwarding for &lt;i&gt;&lt;some port=&quot;&quot;&gt; &lt;/some&gt;&lt;/i&gt;to let the traffic pass through the firewall.&lt;br /&gt;
&lt;br /&gt;
And it just does not work. Whatever you do you can not get that connection working. It&#39;s really annoying, frustrating and infuriating. And you did everything right ...&lt;br /&gt;
&lt;br /&gt;
The dark secret is that there is a second port in play. The port for the RMI registry which is, by default, generated randomly each time you start the application. Without this port the JMX can not locate the stub required for proper remoting and fails. This makes JMX practically unusable in environments with firewalls between the monitored application and the monitoring tool.&lt;br /&gt;
&lt;br /&gt;
This was fixed in &lt;b&gt;JDK7u4&lt;/b&gt;&amp;nbsp;(and later) where you can use the following system property&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; font-size: 14px; line-height: 18px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;-Dcom.sun.management.jmxremote.rmi.port=&amp;lt;rmi registry port&amp;gt;&lt;/code&gt;&lt;/pre&gt;
to define the RMI registry port to be used. After that the only step left is to define the port forwarding for the RMI registry port and you are done. You can access your JMX enabled system through firewall. Yippie!&lt;br /&gt;
&lt;br /&gt;
-JB-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://jarquad.blogspot.com/2013/10/squeezing-jmx-through-firewall.html</link><author>noreply@blogger.com (Enthusiast)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6567270109709132115.post-2826748224217157518</guid><pubDate>Sun, 25 Jan 2009 13:02:00 +0000</pubDate><atom:updated>2013-10-22T22:16:36.970+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">java profiling monitoring btrace performance</category><title>Socket Monitoring via BTrace</title><description>A few days ago I came across an interesting article about various approaches to monitoring Java socket traffic: &lt;a href=&quot;http://www.javaspecialists.eu/archive/Issue169.html&quot;&gt;[JavaSpecialists 169] - Monitoring Sockets&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
What brought this article to my attention (except of the well known name of &lt;span style=&quot;font-style: italic;&quot;&gt;Dr. Heinz M.Kabutz&lt;/span&gt;) was the fact that monitoring Java sockets would be the right task for &lt;a href=&quot;http://btrace.dev.java.net/&quot;&gt;BTrace&lt;/a&gt; - a tool I happen to be involved with.&lt;br /&gt;
&lt;br /&gt;
Indeed, &lt;span style=&quot;font-style: italic;&quot;&gt;Dr.Kabutz&lt;/span&gt; does mention &lt;a href=&quot;http://btrace.dev.java.net/&quot;&gt;BTrace&lt;/a&gt; among the various tools he would use to achieve the objective. Unfortunately, without an example. I took this as a great opportunity to show-off :) and give the audience a working example of monitoring Java sockets using only &lt;a href=&quot;http://btrace.dev.java.net/&quot;&gt;BTrace&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;Java&quot; name=&quot;code&quot;&gt;
package socket;

import com.sun.btrace.AnyType;
import com.sun.btrace.aggregation.Aggregation;
import com.sun.btrace.aggregation.AggregationFunction;
import com.sun.btrace.annotations.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class SocketMonitor {

  private static Map&lt;object socket=&quot;&amp;quot;&amp;quot;&quot;&gt; streamMap = newWeakMap();&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * A helper aggregation instance summing Data-In and Data-Out per socket&lt;br /&gt;   */&lt;br /&gt;  private static Aggregation socketDataSum = newAggregation(AggregationFunction.SUM);&lt;br /&gt;&lt;br /&gt;  @Property&lt;br /&gt;  /**&lt;br /&gt;   * dataIn will get published as a JMX property of the btrace/SocketMonitor bean&lt;br /&gt;   */&lt;br /&gt;  private static AtomicLong dataIn = newAtomicLong(0L);&lt;br /&gt;  @Property&lt;br /&gt;  /**&lt;br /&gt;   * dataOut will get published as a JMX property of the btrace/SocketMonitor bean&lt;br /&gt;   */&lt;br /&gt;  private static AtomicLong dataOut = newAtomicLong(0L);&lt;br /&gt;&lt;br /&gt;  @TLS&lt;br /&gt;  /**&lt;br /&gt;   * A thread safe helper variable to keep the instance of a Socket&lt;br /&gt;   * that getInput/OutputStream method is applied to&lt;br /&gt;   */&lt;br /&gt;  private static Socket currentSocket;&lt;br /&gt;  @TLS&lt;br /&gt;  /**&lt;br /&gt;   * A thread safe helper variable to keep the instance of an InputStream&lt;br /&gt;   * that read method is applied to&lt;br /&gt;   */&lt;br /&gt;  private static InputStream currentInputStream;&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Intercept the entry to the getInput/OutputStream method call&lt;br /&gt;   * Store the Socket instance in currentSocket variable&lt;br /&gt;   */&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.net.Socket&amp;quot;, method = &amp;quot;/get(Input|Output)Stream/&amp;quot;, location = @Location(Kind.ENTRY))&lt;br /&gt;  public static void onGetStreamEntry(Socket self) {&lt;br /&gt;      currentSocket = self;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Intercept the normal exit of the getInputStream method call&lt;br /&gt;   * Upon the exit the instance of the created InputStream is known&lt;br /&gt;   * so we can bind it with the Socket instance used to obtain the stream&lt;br /&gt;   */&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.net.Socket&amp;quot;, method = &amp;quot;getInputStream&amp;quot;, location = @Location(Kind.RETURN))&lt;br /&gt;  public static void onInputStream(InputStream stream) {&lt;br /&gt;      put(streamMap, stream, currentSocket);&lt;br /&gt;      currentSocket = null;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Intercept the normal exit of the getOutputStream method call&lt;br /&gt;   * Upon the exit the instance of the created OutputStream is known&lt;br /&gt;   * so we can bind it with the Socket instance used to obtain the stream&lt;br /&gt;   */&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.net.Socket&amp;quot;, method = &amp;quot;getOutputStream&amp;quot;, location = @Location(Kind.RETURN))&lt;br /&gt;  public static void onOutputStream(OutputStream stream) {&lt;br /&gt;      put(streamMap, stream, currentSocket);&lt;br /&gt;      currentSocket = null;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Store the InputStream instance used in the read method call&lt;br /&gt;   */&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.io.InputStream&amp;quot;, method = &amp;quot;read&amp;quot;, location = @Location(Kind.ENTRY))&lt;br /&gt;  public static void onRead(AnyType[] args) {&lt;br /&gt;      InputStream self = (InputStream) args[0];&lt;br /&gt;      if (containsKey(streamMap, self)) {&lt;br /&gt;          currentInputStream = self;&lt;br /&gt;      } else {&lt;br /&gt;          currentInputStream = null;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Use the stored InputStream instance to get hold of the defining Socket instance.&lt;br /&gt;   * Then use the byte count available as the result of the method call to update&lt;br /&gt;   * the aggregation and total values&lt;br /&gt;   */&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.io.InputStream&amp;quot;, method = &amp;quot;read&amp;quot;, location = @Location(Kind.RETURN))&lt;br /&gt;  public static void countReadData(int count) {&lt;br /&gt;      if (count &amp;gt; -1 &amp;amp;&amp;amp; currentInputStream != null) {&lt;br /&gt;          Socket sck = get(streamMap, currentInputStream);&lt;br /&gt;          addAndGet(dataIn, count);&lt;br /&gt;          addToAggregation(socketDataSum, newAggregationKey(str(sck), &amp;quot;Input&amp;quot;), count);&lt;br /&gt;          currentInputStream = null;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * The following three methods intercept and process three different forms of the write method call.&lt;br /&gt;   * The separation is necessary to be able to get hold of strongly typed parameters wchich&lt;br /&gt;   * we can use in oreder to extract valuable information&lt;br /&gt;   */&lt;br /&gt;&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.io.OutputStream&amp;quot;, method = &amp;quot;write&amp;quot;, location = @Location(Kind.ENTRY))&lt;br /&gt;  public static void onWrite(Object self, int byte) {&lt;br /&gt;      if (containsKey(streamMap, self)) {&lt;br /&gt;          Socket sck = (Socket) get(streamMap, self);&lt;br /&gt;          addAndGet(dataOut, 1L);&lt;br /&gt;          addToAggregation(socketDataSum, newAggregationKey(str(sck), &amp;quot;Output&amp;quot;), 1);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.io.OutputStream&amp;quot;, method = &amp;quot;write&amp;quot;, location = @Location(Kind.ENTRY))&lt;br /&gt;  public static void onWrite(Object self, byte[] data) {&lt;br /&gt;      if (containsKey(streamMap, self)) {&lt;br /&gt;          Socket sck = (Socket) get(streamMap, self);&lt;br /&gt;          addAndGet(dataOut, data.length);&lt;br /&gt;          addToAggregation(socketDataSum, newAggregationKey(str(sck), &amp;quot;Output&amp;quot;), data.length);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @OnMethod(clazz = &amp;quot;+java.io.OutputStream&amp;quot;, method = &amp;quot;write&amp;quot;, location = @Location(Kind.ENTRY))&lt;br /&gt;  public static void onWrite(Object self, byte[] data, int offset, int length) {&lt;br /&gt;      if (containsKey(streamMap, self)) {&lt;br /&gt;          Socket sck = (Socket) get(streamMap, self);&lt;br /&gt;          addAndGet(dataOut, length);&lt;br /&gt;          addToAggregation(socketDataSum, newAggregationKey(str(sck), &amp;quot;Output&amp;quot;), length);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * BTrace event handler - when the BTrace engine receives this event&lt;br /&gt;   * it will dump the aggregation data to the client&lt;br /&gt;   */&lt;br /&gt;  @OnEvent(&amp;quot;dump_stats&amp;quot;)&lt;br /&gt;  public static void dumpData() {&lt;br /&gt;      printAggregation(&amp;quot;Summary of the data read by socket&amp;quot;, socketDataSum);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * BTrace event handler - when received the aggregation data as well as the totals&lt;br /&gt;   * are reset&lt;br /&gt;   */&lt;br /&gt;  @OnEvent(&amp;quot;clear_stats&amp;quot;)&lt;br /&gt;  public static void reset() {&lt;br /&gt;      println(&amp;quot;Resetting collected data...&amp;quot;);&lt;br /&gt;      clearAggregation(socketDataSum);&lt;br /&gt;      set(dataIn, 0L);&lt;br /&gt;      set(dataOut, 0L);&lt;br /&gt;      println(&amp;quot;Data reset&amp;quot;);&lt;br /&gt;  }&lt;br /&gt;}&lt;/object&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
You can see that with not that much coding you can completely monitor the Java socket subsystem. The code does take advantage of the built-in aggregations in BTrace to summarize the data traffic according to the socket and the direction data moves. &lt;br /&gt;
&lt;br /&gt;
Also, exposing the measured data as a property of JMX bean is just the matter of annotating the accumulator variable with &lt;span style=&quot;font-weight: bold;&quot;&gt;@Property&lt;/span&gt; annotation. Unfortunately, this doesn&#39;t work from the aggregations - yet.&lt;br /&gt;
&lt;br /&gt;
I hope you enjoyed this small excursion to BTrace scripting and I hope I will be able to feed you with more of the interesting stuff in the time to come.</description><link>http://jarquad.blogspot.com/2009/01/socket-monitoring-via-btrace.html</link><author>noreply@blogger.com (Enthusiast)</author><thr:total>0</thr:total></item></channel></rss>