Tuesday 24 April 2018

Reporting Java system properties default values

In many cases we might want to check what are the default value for a certain Java property.
This helps in tuning Java applications and also in predicting application behavior when based on those default values.

Java sets many properties by default unless you chose to provide other values.
To report all Java properties for a given JVM we use the below form:

java -XX:+PrintFlagsFinal -version

This will list all the applicable properties to that JVM.
To find a certain property on Windows, we can use the findstr command which works like grep:

C:\Users\sherif>java -XX:+PrintFlagsFinal -version|findstr /I heapsize
    uintx ErgoHeapSizeLimit                         = 0
          {product}
    uintx HeapSizePerGCThread                       = 87241520
          {product}
    uintx InitialHeapSize                          := 100663296
          {product}
    uintx LargePageHeapSizeThreshold                = 134217728
          {product}
    uintx MaxHeapSize                              := 1591738368
          {product}
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

C:\Users\sherif>


This shows that the default max heap size is set to be 1.5GB by default for the 64-bit JVM if we didn't specify a heap size.

Another useful option for Java is the showSettings:properties option, this will show system related settings like the OS type, CPU architecture, and boot class path.
The form looks like this:

java -XshowSettings:properties -version

The output is printed on Windows standard error thus normal piping wouldn't work with it.

Saturday 7 April 2018

How to grep for utf8 characters?

UTF8 offers a lot of flexibility to represent literals and shapes (emoicons) that come from different languages other than Latin.
You can check the page: http://www.utf8-chartable.de/unicode-utf8-table.pl which offers a table form for UTF8.

Lets say you want to grep for all lines that contain Arabic characters in a file and you don't know Arabic and can't even read it.
An easy way would be to use Arabic character UTF8 representation if grep is able to understand it.

grep does offer this facility using the -P option.
grep man:
-P, --perl-regexp
              Interpret  PATTERN  as a Perl regular expression.  This is highly experimental and grep -P may warn of unimplemented
              features.

But a better approach is to use the very useful printf command.
printf is meant to replace the good old echo.
printf offers very strong set of escaping and formatting features much superior to any thing echo can offer.

Below is how the you can grep of the arabic character range:

[sherif@thingol ~]$ LANG=c grep [`printf "\xd8\x80"`-`printf "\xdb\xbf"`] mixed_text |cat
س
ي
ا
ر
ا
ت
م
م
ي
ز
ة
ف
ي
س
و
ق
ل
م
س
تع
م
ل
[sherif@thingol ~]$ LANG=c grep -v [`printf "\xd8\x80"`-`printf "\xdb\xbf"`] mixed_text |cat
A
GH
a
rt

Test
[sherif@thingol ~]$



Using the LANG environment variable ensures we are able to display arabic characters.
We use the printf command to represent the arabic characters using their hex values from the unicode table.
We could also use the unicode points using:

[sherif@thingol ~]$ printf "\u0644 \u0628 \n"
ل ب
[sherif@thingol ~]$


This allows to do any kind of string manipulation for unicode characters.

Enjoy playing with unicode. 

Monday 2 April 2018

Basic Java HotSpot Thread Dump State Analyser

I have been doing a lot of thread dump analysis in the last few months and have been trying to find a tool that would help me do my analysis quickly and precisely.
There is a lot of tools that could be used to do the job for you among those are:
* TDA: https://github.com/irockel/tda  & https://github.com/mkbrv/tda
* IBM JCA: https://www.ibm.com/developerworks/community/groups/service/html/communityview
* Samurai: https://github.com/yusuke/samurai

My favorite tool is Samurai, it offers a very nice view of thread states, with very obvious color codes and offers very reliable lock detection.
Samurai also allows you to follow locked objects from a thread waiting on a lock to the locking thread.

Unfortunately Samurai is not being maintained any more, although the code is available opensource in the above Github repo.
Samurai although still very useful, but it is slow, and at times it gets quite challenging to analyze large dumps with several hundred threads.
Also Samurai only color codes the threads bases on the Thread State class state, while the other tools rely more on the thread status reported based on thread execution and ignores the Thread State class.
This makes analysis more confusing and contradicting at times specially some tools like IBM JCA is not even reporting the State altogether and only relies on execution status.

I wanted to build a tool that allows me more control on what I wanted to see and still be simple to maintain on my side and is quick enough to analyze big dumps.

I started to develop 3 scripts that would help me create a table format for thread states from a set of thread dumps.
The first script would extract the dumps from a log file and write each dump in a single file, then a second awk script parses the thread dump to a thread name, a native id and a status that is a combination of both execution and thread state.
The 3rd script then translate this output to an HTML table.

The code is available on github: https://github.com/sherif-abdelfattah/Thread_dump_analyser

As of now (April 1st 2018), the current out put of the scripts looks like this:



The scripts will run only on Linux, they are mainly using bash and awk.
The scripts are designed to parse Java hotspot thread dumps created for Oracle Java.
I am planning to add more features and enhancements on those scripts once I have a chance to do more coding.

Hope that those scripts could be found useful.