Happy

2009 September 2
by Dragos

My compilers teacher was Preston Briggs’ teacher.
I’m happy and proud that I managed to get here.

The Digg effect, on my blog

2009 July 3
tags:
by Dragos

My blog was stumbled a few days ago, and this is the result:

digg_effect

I always heard about such spikes in visitors / page views, but I never thought it would happen to me :) . Five times more visitors than the average, in one day.

The new Firefox 3.5 icon

2009 July 1
tags:
by Dragos

I simply love it :)

Firefox 3.5 icon

Firefox 3.5 icon

Add figure or table that spans multiple columns

2009 June 21
tags:
by Dragos

If your Latex document has two or more columns, if you add a figure or table, it will span one column and then overlap with the text from the second one. To fix this and let the object take up space in both columns, just add a star after the object name, like this:

\begin{table*}
\centering
\caption{Table Caption}
\begin{tabular} …
\end{tabular}
\end{table*}

or, for a figure:

\begin{figure*}[!t]
\centering
\includegraphics[width=7in]{filename}
\caption{ Figure Caption}
\label{label}
\end{figure*}

How to get CPU usage in Linux from Java

2009 May 15
tags: ,
by Dragos

Technorati Profile

The easiest method to get the CPU usage from a Java class in Linux is to parse the output of the top command. Check out the following code.

import java.io.*;
public class SystemData
{
    static String cmdTop = "top -n 2 -b -d 0.2";
    // returns user cpu usage 
    public static double getCpu()
    {
        double cpu = -1;
        try
        {
            // start up the command in child process
            String cmd = cmdTop;
            Process child = Runtime.getRuntime().exec(cmd);

            // hook up child process output to parent
            InputStream lsOut = child.getInputStream();
            InputStreamReader r = new InputStreamReader(lsOut);
            BufferedReader in = new BufferedReader(r);

            // read the child process' output
            String line;
            int emptyLines = 0;
            while(emptyLines<3)
            {
                line = in.readLine();
                if (line.length()<1) emptyLines++;
            }
            in.readLine();
            in.readLine();
            line = in.readLine();
            System.out.println("Parsing line "+ line);
            String delims = "%";
            String[] parts = line.split(delims);
            System.out.println("Parsing fragment " + parts[0]);
            delims =" ";

            parts = parts[0].split(delims);
            cpu = Double.parseDouble(parts[parts.length-1]);
        }
        catch (Exception e)
        { // exception thrown
            System.out.println("Command failed!");
        }
        return cpu;
    }
}

The parameters from the top command line mean: -b = batch mode, -d 0.2 = take the readings at 0.2 seconds intervals, -n 2 = take 2 readings (the first reading is not correct, from my experience, so we take two).
This code fragment only gets the user CPU percentage. To get the whole CPU percentage, you have to add this value with the system CPU. These values are easy to obtain by changing a few numeric values in the above code. This is left as an exercise for the user. Note that although in general the system CPU and nice CPU percentages are very small, this is not always the case and it is safer to add these values together than to approximate the CPU usage as user CPU only.