Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, May 29, 2013

How to find the culprit when CPU starts to spin?




When your JAVA process started to spin your CPU, you need to immediately issue following two commands and get the invaluable information required to tackle the issue.

1. jstack <pid> > thread-dump.txt
2. ps -C java -L -o pcpu,cpu,nice,state,cputime,pid,tid > thread-usage.txt

After getting those two files, what you can do is,

1. find the thread ID (which belongs to the corresponding PID ) which takes the highest CPU usage by examine thread-usage.txt file.

%CPU CPU  NI S     TIME   PID   TID
..........
  0.0   -   0 S 00:00:00  1519  1602
  0.0   -   0 S 00:00:00  1519  1603
24.8   -   0 R 00:06:19  1519  1604
  2.4   -   0 S 00:00:37  1519  1605
  0.0   -   0 S 00:00:00  1519  1606
..........


2. convert the decimal value (in this case 1604) to hexadecimal - (online converter - http://easycalculation.com/decimal-converter.php)

Hex - 644



3. search for the hexadecimal obtained (in this case 644) in the thread-dump.txt (thread-dump.txt should have that value as a thread id of one thread) and that is the thread which spins.
4. that thread usually has a stack trace, and that's the lead to find the culprit.

In this case the stack trace of the thread that spins is:

"HTTPS-Sender I/O dispatcher-1" prio=10 tid=0x00007fb54c010000 nid=0x644 runnable [0x00007fb534e20000]
   java.lang.Thread.State: RUNNABLE
        at org.apache.http.impl.nio.reactor.IOSessionImpl.getEventMask(IOSessionImpl.java:139)
        - locked <0x00000006cd91fef8> (a org.apache.http.impl.nio.reactor.IOSessionImpl)
        at org.apache.http.nio.reactor.ssl.SSLIOSession.updateEventMask(SSLIOSession.java:300)
        at org.apache.http.nio.reactor.ssl.SSLIOSession.inboundTransport(SSLIOSession.java:402)
        - locked <0x00000006cd471df8> (a org.apache.http.nio.reactor.ssl.SSLIOSession)
        at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:160)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:342)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:320)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:604)
        at java.lang.Thread.run(Thread.java:722)
Hope this helps!

Monday, December 24, 2012

WSO2 Stratos-2.0 - Cloud Controller - Part 1

What is Cloud Controller?

Cloud Controller plays a vital role in Stratos 2.0 and here I list its capabilities and duties.

WSO2 Cloud Controller,

  • is acting as a bridge between application level and Infrastructure as a Service (IaaS) level via Jclouds API.
  • enables your system to scale across multiple IaaS providers.
  • is the central location where the service topology resides.
  • is responsible for sharing the up-to-date service topology among other Stratos 2.0 core services, periodically.
  • supports hot update and deployment of its configuration files.
  • has inbuilt support for AWS EC2 IaaS provider and Openstack Nova IaaS provider.
  • enables you to cloud burst your system across multiple IaaS providers.
  • allows you to plug an implementation of any IaaS provider supports by jclouds, very easily.
  • enables you to spawn new service instances, while associating a public IP automatically, in order to reduce the instance boot-up time.
  • enables you to terminate an already started instance of a particular service cluster.
  • can be configured to cover many scenarios, using its well-thought-out configuration files.

Awaits the next post on Cloud Controller's SOAP Service Interface...

WSO2 Stratos-2.0 - Alpha released!

One rarely get a chance to release a product he is working on, on his Birthday. I'm lucky enough (Usually luck doesn't favour me :-(), to get such a chance.

WSO2 Stratos2 alpha was released on 19th December 2012, ya, that is my birthday.

I was mainly working on WSO2 Stratos-2.0 Cloud Controller, ELB etc. And I will elaborate on Cloud Controller in my future blog posts.

Thursday, January 27, 2011

Using Java Executor Framework...

Java Executor Framework provides a solution using a thread pool to the following problems of using threads directly:

  • Creating a new thread causes some performance overhead.
  • Too many threads can lead to reduced performance, as the CPU needs to switch between these threads.
  • You cannot easily control the number of threads, therefore you may run into out of memory errors due to too many threads.
Here's a code snippet on how you can use it:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestExecutor {
   
    private static final int NTHREDS = 10;

    /**
     * @param args
     */
    public static void main(String[] args) {

        long l=System.currentTimeMillis();
        ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
        for (int i = 0; i < 10000; i++) {
            Runnable worker = new Test(i);
            executor.execute(worker);
        }
        // This will make the executor accept no new threads
        // and finish all existing threads in the queue
        executor.shutdown();
        // Wait until all threads are finish
        while (!executor.isTerminated()) {

        }
        System.out.println("Total time: "+ (System.currentTimeMillis()-l));
    }
public class Test implements Runnable {

    private int id;
   
    public Test(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        int sum=id;
        for(int j=0; j<10000000;j++)
            sum += j;
        System.out.println("My sum = "+sum);
    }
}

You may note that I have used a thread pool with ten threads, you can change the size of the pool according to your need.

You may wonder what actually applying concurrency does to your application's performance. Here's a comparison of CPU usage without and with using threads.



My application without using threads!

Application is running in the time gap

of 40 - 0 seconds in the CPU history

graph. You may note that CPU

utilization is 30% & 100%. i.e. not

effectively utilized.







My application using threads!

Application is running in the time gap

of 25 - 0 seconds in the CPU history

graph. You may note that CPU

utilization is 100% & 100%. Thus the

both CPUs are effectively utilized.





There is another way that you can get the advantage of Java Executor Framework, I will elaborate on that in my next post! :)

Happy Threading!! :D

Tuesday, January 25, 2011

Sorting a hashmap by values?

Hash map is a really useful data structure when it comes to coding. In some instances you may need to sort the keys of the hash map according to the values.

Here's a way (there may be many other ways), how you can retrieve the  list of keys after sorting the hash map according to values.

public ArrayList<String> sortHashMapByValues(HashMap<String, Integer> passedMap) {

        ArrayList<String> sortedRels = new ArrayList<String>();
        ArrayList<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
        Collections.sort(mapValues);

        for(int i : mapValues){
            for ( Iterator it = passedMap.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry e = (Map.Entry) it.next();
                if(((Integer)e.getValue()) == i){
                    sortedRels.add((String)e.getKey());
                    passedMap.remove(e.getKey());
                    break;
                }
            }
        }
        return sortedRels;
    }


Hope this is help someone!

Tuesday, December 21, 2010

Bad smells in code ?? :O


I think it is really important that you identify the "bad smells" in a code that you have written, simply since they are not good to have.

There are number of such "bad smells" which have been recognized and expected, programmers to be aware of. You can check most of them from here

I had a research on the internet for a Eclipse plug-in in order to identify so called "bad smells". Luckily I found this wonderful plug-in called "JDeodorant". 

JDeodorant is capable of recognizing four main types of "bad smells" that can be found in your code namely God Class, Long Method, Type Checking and Feature Envy. I have created a screen cast on "how to use JDeodorant?" and here it is.


Hope this post made your attention towards possible "bad smells" in your code, and hopefully correcting them (Oh I forgot to tell, using JDeodorant you can correct most of these bad smells as well ).

Happy Coding!! :)

Monday, June 14, 2010

Why not create a PDF of your own?

My first project at IFS was about applying PDF/A standards to IFS reports. This project involved a conversion of PDF version. For that I needed a some kind of Java library which will provide me a set of functionality to perform a conversion.

While searching for few hours I found this wonderful Java library called iText. It has both Java and C# .NET versions.

You can use iText to:
  • Serve PDF to a browser
  • Generate dynamic documents from XML files or databases
  • Use PDF's many interactive features
  • Add bookmarks, page numbers, watermarks, etc.
  • Split, concatenate, and manipulate PDF pages
  • Automate filling out of PDF forms
  • Add digital signatures to a PDF file
  • and much more...

You can download iText binaries and source files from here.
 iText API from here.
An excellent tutorial on iText can be founded in here.

Create a small PDF file and enjoy seen it's working, if you like to do more work using this that would make my effort even more success.

Sunday, December 13, 2009

MatrixManipulator 1.0 is released....

I have released Open Office.org extension MatrixManipulator 1.0 today, 13/12/2009.

You can download it from following link:

It still in the implementation phase and I hope to release improved versions soon.

Please put a comment on that extension, on the extension page, because it will immensely help me to improve this.

Hope you get a Graphical User Interface solution for Matlab, up to some extent in the area of matrices.