Sunday, April 22, 2012

JVM Command Line Options for Low Latency Applications


In this article, I elaborate Java Hotspot JVM command line options for low latency applications.

Garbage Collection (GC)

When tuning JVM garbage collection following principles should be taken into consideration:

  • Minor GC Reclaim Principle: Maximize the number of objects reclaimed in each minor GC.  This will reduce number of full garbage collections and its frequency.
  • GC Maximize Memory Principle: The larger the java heap size, the better garbage collector and application performance.
  • Constraints: Tune JVM garbage collector based on two of following performance attributes: throughput, footprint and latency.

Minimum JVM command line options for GC are as follows:

-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails
-Xloggc:

Calendar date and time stamp of GC activities to link application’s own logs.

-XX:PrintGCDateStamps

To monitor and analyse pause events arising from VM safe point operations for low latency applications:

-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCApplicationConcurrentTime
-XX:+PrintSafepointStatistics

Note, safe point operations can happen due to many reasons, not only GC (for example, bias unlocking/revoking, deoptimization, thread stopping/resuming, exit).  So analysing reasons behind stop world pause times is very important.

Java Heap Size

Amount of data in java heap after a full garbage collection when application in a steady state is the size of live data. Live data size determines size of heap sections as follows:


  • Overall Heap size (-Xms and -Xmx) should be 3 or 4 times of live data size
  • Permanent Generation (-XX:PermSize, -XX:MaxPermSize) size should be  1.2 to 1.5x times of permanent generation space occupancy.
  • Young generation (-Xmn) size should be bigger than 1.5 times of live data size and not less than 10 % heap size.  Size of young generation space should be optimum to reduce frequency and duration of minor GC. For example, if minor GC happens very frequently size of young generation should be increased. Whereas, if duration of minor GC is so high, size of young generation should be decreased.
  • Old generation (Heap size - Young generation) size should be 2 to 3 times of live data size.
Initial heap size (-Xms) and maximum heap size (-Xmx) should be same for low latency applications in order to avoid dynamic sizing of heap. This is true for young generation too therefore size of young generation should be specified with flag –Xmn (max and initial size).

In Concurrent Garbage collector (CMS) (-XX:+UseConcMarkSweepGC), special attention should be given to survivor ratio (-XX:SurvivorRatio) and tenuring threshold.

Survivor ratio (-XX:SurvivorRatio) is the ratio  of eden space to survivor spaces. It must be greater than 0 and it is used to determine size of survivor and eden space compare to overall young generation size such as:

survivor space size = -Xmn/(-XX:SurvivorRatio= + 2)

For example if -XX:SurvivorRatio=6 and –Xmn=512m is specified, survivor size will be 64m and eden size will be 384m.

As explained above, since Survivor ratio (-XX:SurvivorRatio) determines size of eden space, so the smaller eden space, the higher frequency of minor GC, which leads short minor GC duration time and more objects are promoted to survivor space. When objects promoted from eden space to survivor spaces, age of objects are increased too.  Objects older than an age (tenuring threshold) are promoted to old generation. Therefore, survivor ratio (-XX:SurvivorRatio) affects tenuring of objects.

Tenuring threshold is calculated by JVM dynamically by analysing aging activity of objects. But -XX:MaxTenuringThreshold  (between 0 and 31 in java 6) can be used to specify tenuring threshold. Objects older than this value are promoted to old generation space.  Hence, an optimum tenuring threshold must be used to prevent promoting objects to old generation.

Tuniring activity can be monitored by enabling -XX:+PrintTenuringDistribution which prints out statistics for each age:

Desired survivor size 8388608 bytes, new threshold 1 (max 15)
- age 1: 16690480 bytes, 16690480 total

In this output it should be analysed in a way that if the number of bytes surviving at each object age decreases as the object age increases and whether the tenuring threshold calculated by the JVM is equal to or stays close to the value set for the max tenuring threshold.  For example, similar to previous example, if internal threshold is less then max tenuring threshold and desired survivor space less than aged byte consistently, then survivor space should be increased.

Another point in CMS garbage collector is also when to initialize the cycle. Following command line options are used to control when CMS garbage collection should be started based on occupancy of old generation space:

-XX:CMSInitiatingOccupancyFraction=
-XX:+UseCMSInitiatingOccupancyOnly

An optimum value of -XX:+UseCMSInitiatingOccupancyOnly should be used minimize frequency of CMS and duration and risk of stop the all world garbage collection. If a high value of -XX:+UseCMSInitiatingOccupancyOnly is used, CMS duration would be longer, also there is a chance that old generation will be full before CMS reclaim space, if application generates more objects than reclaimed.

Explicit Garbage Collections

Unless it is required specifically in the application, garbage collection arising by System.gc() should be disabled or executed as concurrent GC with following options:

-XX:+DisableExplicitGC
-XX:+ExplicitGCInvokesConcurrent
-XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses

Aggressive Optimization Techniques

Many new and slightly risky optimization techniques can be enabled by -XX:+AggressiveOpts options. This option is not recommended for application in which, stability is important, as this feature enable new optimization techniques which are not fully proven stable yet.

Biased Locking

Hotspot Java 6 has a new default feature which bias locking for towards last thread holding the lock. This approach is true for many applications. But in some cases, that can not be true. For example, a resource locked by producer and consumer threads.  Since revoking a  biased locking requires a full stop the world safe operation, it is better to disable that future for that sort of application with -XX:-UseBiasedLocking option. Statistics regarding to stop the world time and activities can be monitored by following options

-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCApplicationConcurrentTime
-XX:+PrintSafepointStatistics

Example JVM parameters:

$JAVA_HOME/bin/java –Xmx10G –Xms10G -Xmn1200m -XX:SurvivorRatio=4 -XX:PermSize=128m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC -XX:MaxGCPauseMillis=1000 -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=90 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -XX:+PrintSafepointStatistics -XX:+AggressiveOpts -XX:+UseCompressedOops -XX:+OptimizeStringConcat -XX:+UseStringCache -XX:+UseFastAccessorMethods

Commands to analyse heap

Getting histogram of objects in JVM heap

PID= |Process id of a java process|
FILE_NAME_HIST=heapHist.dump
FILE_NAME_HIST_LIVE=heapHist_Live.dump 

%% All objects 
$JAVA_HOME/bin/jmap -histo $pid > $FILE_NAME_HIST
%% Live objects 
$JAVA_HOME/bin/jmap -histo:live $pid > $FILE_NAME_HIST_LIVE 

Getting a memory dump of a java process:

pid= |Process id of a java process| 
FILE_NAME_LIVE=heapDump_Live_$pid.dump
FILE_NAME=heapDump_$pid.dump 

%% dump everything in heap to $FILE_NAME 
$JAVA_HOME/bin/jmap  -F -dump:format=b,file=$FILE_NAME $pid 
%%For only Live objects, we need to do following. Note this will force a full GC first, therefore first try above dump. 
$JAVA_HOME/bin/jmap  -F -dump:live,format=b,file=$FILE_NAME_LIVE $pid 

Starting jstatd (to connect a remote java process via visualVM)

# start jstatd (with same user name/functional id)
# an example of tools.policy file is provided below
jstatd  -J-Djava.security.policy=tools.policy  &

# example of tools.policy file
grant codebase "file:${java.home}/../lib/tools.jar" {
   permission java.security.AllPermission;
};

Sunday, March 11, 2012

Performance Monitoring on Operating System Level

It is that time of year again in my office. We have to fill up forms for this year’s goals. I can not say that I do enjoy filling up these forms. But it is one of these things you don’t like doing it, but you have to do it, because it is part of your job.

Some of our goals must be in align with business’s direction. One of them is to make our application faster. So, performance and performance related tasks are the main project in my work for this year. We  aim at boasting our performance to single digit millisecond, if not a nanosecond.

In software development, performance enhancement is not focused in first release or iterations, unless performance is a vital feature of application. Of course, as part of best practices, generally during design stage, performance side is considered. But main goal is to ship the product with a reasonable performance, initially. Once application is released, along with other new features, performance of the application is improved (provided that application is a success and some people are still using it.J). At the moment, in my work, we are in performance tuning stage for our application.

First step in performance enhancement is to gather performance related measurements, so you will have some quantitative data to determine which component of the application needs to be addressed. These initial data will be used also as benchmark for performance enhancement. The measurements are collected by profiling application, looking at OS level data and benchmarking.

OS level data provides you first-hand knowledge of utilization of your resources. In the first place, you may resolve some of these issues by increasing your resources (CPU, memory, disk, network) within given budget. In my current company, a new hardware or resource is not bought unless, either it is broken or a given utilization threshold for that resource is not met. For example, to get a new server with a better CPU specification, its CPU utilization must be more than 40% generally in a normal trading day. Fair enough! No need to waste money on new toys, if applications do not already utilise these resources. So, in performance analysing, first step is to measure how an application utilise resources such as CPU, memory, network, disk.

In this article, I will list commonly used tools for monitoring performance related data in OS (Windows and Linux). I am not going to dive into how to use these tools, as there are many related online/offline resources (man, docs …). If you want to get into more detail, I can also recommend a new book, Java Performance by C. Hunt and B. John, of which this and upcoming performance related text will be based.

CPU Utilization
Monitoring OS system allow us to see how an application utilize CPU cycles. For example, 
if a multithreading application causes saturation of CPU resources, that issue needs to be resolved before considering the increase of CPUs number. 

When monitoring CPU utilization, two measurements have to be collected: User and Kernel or System (sys) CPU utilization. Impact of an application will be displayed as user utilization in tools.

Windows
To monitor performance of CPU in Windows, there are three tools. Task Manager, perfmon, typeperf.

Task Manager is widely recognized and one click away on the Desktop. In performance tab of Task Manager, you can see CPU related graphs along with memory measurements. In order to display kernel utilization, you need to enable it by View à Show Kernel Times. By doing so, kernel utilization measurement will be shown in red line in charts. User utilization will be difference between these two charts.

Task Manager does not provide low level measurements. For that one perfmon tool is needed.This is a very advance tool and you can display many measurements in graph by selecting it from Add Counters … window. For example to display user and kernel time of a processor, you need to select % User Time and % Privileged Time in Processor item.

Perfmon is a graphical interface. To automate monitoring in a batch script, there is a command line command typeperf which is a command line interface of perfmon. For example to show user, kernel and total CPU utilization with 5 sec interval, following command is used.
typeperf –si 5 "\Processor(_Total)\% User Time" "\Processor(_Total)% Privileged Time" "\Processor(_Total)% Processor Time”

Linux
For linux type operating system, there are two main tools to display CPU utilizations (and other measurements too): vmstat, top.
In vmstat, CPU related measurements are shown under cs, sy, id and wa column of cpu header:
  • us: Time spent (%) running non-kernel code. (user time, including nice time)
  • sy: Time spent (%) running kernel code. (system time)
  • id: Time spent (%) idle. Prior to Linux 2.5.41, this includes IO-wait time.
  • wa: Time spent (%) waiting for IO.

By using top command, similar CPU related measurements in top of the screen can be seen.

CPU Schedule Run Queue
CPU schedule run queues hold light-weight processes which are ready to be run but are waiting for a CPU to be executed. Size of queue increases when there are more lightweight process are ready to be executed than system can handle. So, if queue size is an indication of performance issue in system. Generally, if size of a queue is 3 or 4 times bigger than the number of processes (in java this is Runtime.availableProcessprs()), then it can be assumed that system is not good enough to handle lightweight processes. As a solution, to this issue, either increase the CPU number or optimize the source code of applications to reduce CPU cycles.

In Windows, following typeperf command will show run queues:
typeperf -si 5 ”\System\Processor Queue Length”
In vmstat, first column r shows the actual number of lightweight processes.

Memory Utilization
In memory utilization, paging or swapping, locking, voluntary and involuntary context switching activities should be monitored.

If a system’s main memory is not enough to process a request, then a disk space from memory (swap space) is used to dump the content of the memory. Therefore, application causing so much swapping or paging will come to a slowdown. For example, for a Java application, if a part of heap memory is paged, then during garbage collector phrase, this part has to be taken in to memory. That would increase garbage collector time (if no concurrent garbage collector is used, that means application stop longer time).

In Windows following command show available memory and paging (per second) activities:
typeperf -si 5 ”\Memory\Available Mbytes” ”\Memory\Pages/sec”

In Linux, again top and vmstat commands are our best friend again. In vmstat, memory and si and so columns shows memory related measurements:
  • swpd: the amount of virtual memory used.
  • free: the amount of idle memory.
  • buff: the amount of memory used as buffers.
  • cache: the amount of memory used as cache.
  • inact: the amount of inactive memory. (-a option)
  • active: the amount of active memory. (-a option)
  • si: Amount of memory swapped in from disk (/s).
  • so: Amount of memory swapped to disk (/s).
In the case of less memory size and the rise of paging, increase of RAM should be considered 
as an appropriate step. Please, note that paging can happen frequently when you launch 
an application.

To monitoring locking, voluntary and involuntary context switching activities in linux a package called pidstat has to be installed.

Network Utilization
A system with heavy network communication, must utilise well its network resources (network bandwidth or network IO), otherwise that will degrade the performance of applications.

In Linux, we can use netstat to display network communications. But this tool does not provide total utilization of resources. A manual estimation has to be done with according to capacity of network resource and current network activities reported by netstat. Following formula can be used for utilization:

Network Utilization= Bytes Total/Sec/(Current Bandwith/8)*100

Note: Current bandwith in bits, therefore we convert it to byte by diving 8.

Similar estimation can be done in Windows by using following command:

typeperf -si 5 "\Network Interface(*)\Bytes Total/sec"

Disk Utilization
Apart from network, disk IO is another factor for performance. An application has significant IO interaction, such as a database, must consider disk IO utilizations.

In Linux iostat command is used for monitoring disk IO activities. For example when iostat is used with extended statistic argument (x) it will provide utilization for each devices.

SAR
These tools described above provide information related to current state of system. But if we are interested in historical performance data, sar command can be useful. This command in Linux, provides measurements data for an extended period of time (e.x : last 10 days).

CPU/Cache Utilization
To gather CPU and cache (L1, L2, L3) related statistics (such as load, stores, misses, number of cycles, instruction)  perf  and likwid.  For example to find detailed L1 cache statistics of a java process, following command can be used:

$ perf stat -d -e L1-dcache-loads,L1-dcache-load-misses,L1-dcache-stores,L1-dcache-store-misses,cache-references,cache-misses,cycles,instructions java myProcess




Sunday, January 01, 2012

Affordable House prices in London

What is the biggest issue in London? I think, it is affordable housing issue for working class people. To get a secure, habitable, accessible (close to transports) house in London is a big issue. Although, Compare to many cities in the world, of course London is maybe the more secure and accessible (transport infra-structure is good) city, but perception among Londoner is not that much optimistic, as far as I see, from my experiences. In this text, i will explore the state of housing in London in terms of affordability for local, common people and try to answer question, if house prices in London will decrease or increase in 2012, given supply and demand parameters. And my conclusion is, it will decrease, but it won’t crash, yet. I will try to establish it in terms of demand/supply factors.

Foremost, I am not expert on real-estate properties. But last one year, I am doing my own research to find a suitable house in London, therefore, this text will be based on my own research and personal experiences. So, don’t forget to do your own research. I don’t work for any real estate related company or any whatsoever relation. I know, statistics, is white lies, but in order to justify my conclusion, I will use some statistics. Most of statistics in this text will be based on following publication from Local London Authority and Land Registry:

http://www.london.gov.uk/sites/default/files/Housing%20in%20London%20Dec11.pdf

http://www.landreg.gov.uk/upload/documents/HPI_Report_Nov_11_ws13pm4.pdf

I think housing issues is a timer bomb, waiting to explode. And at the moment, I cannot see a sound policy to address it, neither from local and governmental authorities agencies. They hope the issue resolves itself. Would it be resolved itself for common people in a free market capitalist economy, where rich foreigner can buy properties or it is open to migration?

In Maslow's hierarchy of needs, sheltering is at the bottom, among side of breathing, food, water, sleep, sex, excretion. People need a shelter, a home. That is very basic. We need a safe place to nurture. We either need to rent or buy house to live in. In some worst cases, some of us are forced to streets or squatting. Actually, because of current economic crisis, homeless people in London streets increase (around 4000 people in London streets, increased almost 33 % since economic crisis started in 2008).

According to statistics, in 2011, in London, 53 % of people own their house, from high of 57 % in 2001. Around 25% people in private or social rental and private rental is increasing.

So let’s we assume that you are one of these lucky person in London who has a job and afford to rent a place at least. Actually, most of these people prefer to buy a house rather than renting. Statistically, 85% of Londoners would prefer to buy a house. So, these people somehow have to save money to buy a house, if they are in rent.

Average house price in London in July 2011 was £347,000 which includes very expensive houses in exclusive places of London. In that text, since I am considering affordable house for Londoners, I assume average house price in London £250,000, which is actually of mean of house prices and also what I have observed since last year.

For a first time buyer, you need to provide at least %20 deposit for mortgage which is be around £50K. In addition, you need at least £5K for other services (mortgage, legal, surveyor, and I am not including %3 stamp duty tax for houses less than £250K). For someone, who would be saving £500 per month, it would take at least 10 years to make a 20% deposit which leads people to not save but spend, as it seems very long term investment. Given that people in UK are bombard to spend by capitalist system, rather than save, most people do not save, but enjoy today, rather than thinking tomorrow.

So, I think, most of first time buyer cannot afford to buy house because of restriction on mortgage. Therefore, in terms of first time buyer, demand is low.

There is also another type of buyers who prefers to work in London, but who do not prefer to live in London. They prefer to commute every day 2-3 hours than living in London. For example, some of my colleagues and friends, commutes from Brighton and Oxford (2-3 hours journey everyday), rather than to live in London. So, another low demand factor.

But, we have also foreigner factors in London in terms of demand. This factor, foreigner investing in London, buying houses in London, is one of the biggest factors in increase house prices in the last decade. Rich foreigner sees London as a safe place to invest. They think, they will make good profit. And actually they do. In exclusive neighbourhood in zone 1, house prices has increased almost 9% last year, but overall increase all over London was %1. So, simply, some rich people will invest in zone 1, but I wonder if they will invest in houses, in zone 2 or zone 3? For example Stratford which is going to host 2012 Olympics.

I doubt it. I don’t think, any rich foreigner will invest it in zones apart from Zone 1 or Zone 2. I was walking in Stratford last week, I noticed many empty flats. It seems not much demand for these new small flats which is built Olympics pipe dreams. For example, house prices in Newham (which holds Olympics and zone 3) has reduced 0.3% in the last year. Besides that, I don’t think, rich foreigner will see London as safe place while EU is in credit crisis. Remember the attitude of Chinese government regarding to buying EU zone debts? They refused it. They are not investing in EU as they do not have faith. Sure, UK and EU is not exactly same, but they have strong trading ties. I think, these rich people would invest more in emerging markets, such as Brazil, Turkey and Poland or Zone 1 in London. So rich foreigner investment won’t effect houses prices for common people who cannot afford to buy a house in Zone 1 or 2, they are already priced out in these zones.

Now let’s have a look at possible seller. Let starts with local Londoners, who bought house very long time ago and would like to sell it and leave the city. Actually, in 2011, net migration is negative. For example, between mid 2009 and 2010 11,200 more people moved out London than moving in. And I think, in 2011, that number has increased due to economic crisis. So that would increase housing supply.

Other type of people who would like to sell house are those who bought big houses before boom, and would like to sell now to make a profit and get a smaller or bigger places. That’s very common. Lately, a friend of mine sold his house to get a bigger place to settle down and have a family. And I read from many websites, people sells big houses to make profit and buy smaller and more affordable inner central places and invest house abroad. Since, these buys and sells kinda offset each other, in terms of demand and supply; they are negligible, if not increase the supply.

There is an emotional factor during selling and buying house prices. Many house owners feel that their house still worth as much as peak of 2007. For example, one of the house owners got very emotional once I did not offer as much as she thought so. After my discussion and email about my reasoning why it does not worth, she sliced her prices £15K (5% lesser), but it was still so high for that house. Statistically, in UK, average asking price of house is £236,597, but selling price is £168,205 according to land registry. That means almost 30% higher asking price and a very good negotiation (middle-eastern or Asian way) has to be done, as house owner expect very high.

Let’s have a look at also housing supply in London. In last 3-4 years, in average, around 25000 new house is delivered. A total of 40,870 affordable homes were delivered in the three years 2008/09 to 2010/11. As future, there are 172,000 homes in London with outstanding planning permission, of which just over a third are under construction. Most of these houses (55000) will be built in Greenwich, Tower Hamlets and Newham. Camden and Richmond and Kingston Upon Thames will have smallest new home, 550 and 1,200 home respectively . It is estimated that London population have grown by 71,600 between 2009 and 2010. That growth is attributed to mostly natural changes (birth, dead) . Therefore, delivered new houses (25000) are a bit short of demand due to population growth (71000) in long term, but for short term demand-supply is equal, if supply is not less.

As I mentioned above, most of the new house (almost third) will be built in Greenwich, Tower Hamlets and Newham area of London. These are the most deprived places of London. With Olympics 2012 and Royal Docks Enterprise Zone, Cross Rail and several private initiatives(for example Westfield shopping centre in Stratford), east London is getting attention. But Londoners emotional attitude towards these areas are very distant. West and North areas are still very popular. In that sense, to get people attention to the east, house prices must be lower; this is already lower compare to North and West London, in fact.

Although house prices in London have increased 1%, when you consider inflation, house prices are actually is going down. Royal Institution of Chartered Surveyors reckons that house prices will fail %3 in 2012. Given that house prices is still 40% higher than 2006, it will go further, I think. But the question is how much and longer? I don’t think, house prices would go so much down, as that would be disaster for so many people for whom bought house in peak times. According to Council of Mortgage Lenders, around 13% mortgages approved after 2005 is in negative equity, while house prices are down around 10% meanwhile. Therefore, government will take some action to make sure, house prices to stabilise otherwise they will have an issue in election.

So, government has a dilemma. House prices cannot go much down, in that case it will scare foreign investor and also many public will be in negative equity. Therefore, I reckon, government will use inflation to wipe out its debt and public dept. With higher inflation, house prices will decrease in real terms, like in 2011. Government will also control supply chain to make sure there is not much new houses.

Overall, I think, house prices will decrease in 2012, especially when you consider inflation. The factors increase house prices are: Low level of new houses compare to population growth, high renting prices and basic needs (people need a shelter). On other hand, I can count many factors which will decrease house prices: Already overprices, low level of mortgage approval for first time buyer, reluctant foreign investor, poor housing and habitation. But, I think, house prices won’t crash due to historical low level interest rate. Once interest rate starts soaring and people could not pay their mortgage, and repossessions start, supply will be higher and then we may see property crash. But policy makers won’t sit idle, they will come up with something, as public are very emotional with their house and many people see them also as a retirement investment.