Wednesday, August 5, 2015

JMH result on UDOO

Just for fun. As a follow-up on Java 8 on UDOO. Here are the result of running JMH on UDOO.

OpenJDK 1.8.0_51 (Zero VM in interpreted mode only.  No Cacao for the default build):

Benchmark                Mode  Cnt        Score      Error  Units
MyBenchmark.testMethod  thrpt  200  2593688.336 ? 4998.196  ops/s


Oracle JVM 1.8.0_51:

Benchmark                Mode  Cnt         Score       Error  Units
MyBenchmark.testMethod  thrpt  200  47042989.345 ? 97733.584  ops/s


That is 18x improvement on the througput.

Java 8 on UDOO

The performance of OpenJDK 8 on UDOO (Archlinux) is not that great when compared with Beaglebone Black:

$ uname -a
Linux maggie 4.1.3-1-ARCH #1 SMP Wed Jul 22 18:44:39 MDT 2015 armv7l GNU/Linux

$ java -version
openjdk version "1.8.0_51"
OpenJDK Runtime Environment (build 1.8.0_51-b16)
OpenJDK Zero VM (build 25.51-b03, interpreted mode)

$ time java -XX:+TieredCompilation -XX:+AggressiveOpts fastaredux 25000000 > /dev/null 2>&1

real    6m52.692s
user    6m52.095s
sys     0m0.480s


Time to download  the Oracle JDK 8 for ARM.  Same as the case with BBB and Raspberry Pi, the result is much better:

$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b07)
Java HotSpot(TM) Client VM (build 25.51-b07, mixed mode)

$ time java -XX:+TieredCompilation -XX:+AggressiveOpts fastaredux 25000000 > /dev/null 2>&1

real    0m20.618s
user    0m20.355s
sys     0m0.290s




Wednesday, July 29, 2015

Java converting bytes to integers

Saw this Java method on github for converting Little Endian bytes to an integer:


private static int U8TO32_LE(byte[] x, int i) {
    return x[i] | (x[i + 1] << 8) | (x[i + 2] << 16) | (x[i + 3] << 24);
}

The implementation above is incorrect.  In Java, bytes are signed.  So the signed bit will got extended when a byte is casted as integer.  The correct way to do it should be:

private static int U8TO32_LE(byte[] x, int i) {
    return (x[i] & 0xff) | ((x[i + 1] & 0xff) << 8) | ((x[i + 2] & 0xff) << 16) | ((x[i + 3] & 0xff) << 24);
}





ChaCha20 Java implementation

My quick-and-dirty standalone Java implementation of the ChaCha20 stream cipher is available on GitHub

Thursday, July 16, 2015

Netwon's method with Java lambda expression

Here is a quick-and-dirty implementation of the Newton's method using Java Lambda Expression.



And here are some use/test cases:


Friday, June 12, 2015

Dual band wireless router as bridge with DD-WRT

Many people configure old wireless routers as repeaters to extend range of their networks:
(figure from dd-wrt web site)

However, with a dual band (2.4GHz and 5GHz) router, it can be used to boost wifi performance.  The idea is that, at location where wifi is weak, the secondary router can be configured as bridge to connect to primary router using 2.4GHz (as it has better penetration through walls than 5GHz).  Not to mention that with DD-WRT, the TX power can be configured to be much higher than your portable devices.

At the same time, the 5GHz channel of the secondary router can provide a strong and non-interfered signal to local devices.  Internet traffic will be bridged to primary router via the 2.4GHz channel.



I recently moved back to Canada and now living in a house sharing the existing Telus internet connection with others :(.  There are some tricks that can be done on the crappy V1000H router provided by Telus, but that will be another story.

Since the internet router is located in another floor, the wifi connection is not optimal for me.  So I reconfigured my DD-WRT-ed Asus RT-N66U as the diagram shown above.  Steps are clearly stated on DD-WRT web site on how to setup router as bridge.  The only different is that since my router is dual band, the 5GHz channel can be used to serve my devices while 2.4GHz is used as bridge.

Here are some speed tests ran on my Dell Inspiron 13 (Intel AC7265):

2.4GHz directly to the primary router:


5GHz to secondary router, bridged to primary router via 2.4GHz:

As expected:
- latency is higher when there is one more hop
- improvement on both download and upload speed is significant



Wednesday, April 29, 2015

Raspberry Pi to the rescue




With my freebsd machine down, this little white box will be my web machine for a while

Tuesday, March 10, 2015

Cross-compile kernel modules for Raspberry Pi

This is a quick note on how to cross-compile third party kernel modules for Raspberry Pi. The aim is to keep the stock kernel while compiling a module with matching version.

On Raspberry Pi
Update software on the Raspberry Pi

$ sudo apt-get update
$ sudo apt-get upgrade


Also, install rpi-update to upgrade the firmware and kernel. Then reboot.

$ sudo apt-get install rpi-update
$ sudo rpi-update
$ sudo sh -c “sync; sync; shutdown -r now”


Check the version of kernel

$ uname -a
Linux fax1 3.18.9+ #767 PREEMPT Sat Mar 7 21:41:13 GMT 2015 armv6l GNU/Linux


Also, we need to know the configuration used to compile the kernel. Execute the following command to save it to .config. Transfer the file to the cross-compile machine. We will use it later when compiling the kernel.

$ zcat /proc/config.gz > .config



Here the kernel version is 3.18.9. We can now work on a desktop computer for cross-compiling. First we need to compile the kernel source in order to generate the Module.symvers file. We need this file when compiling third-party modules. For the following example, I will be using a Ubuntu x64 machine.

On the cross-compile machine
Create a folder and download the Rapberry Pi kernel source.

$ git clone https://github.com/raspberrypi/linux.git


Check the Makefile and make sure the version is same as the kernel running on your Raspberry Pi.

Create another folder and download the compiler and tools

$ git clone https://github.com/raspberrypi/tools


Set an environment variable KERNEL_SRC to point to the location of the source, e.g.

$ KERNEL_SRC=/home/cho/work/rpi/linux


Set an environment variable CCPREFIX to the prefix of the path of tools, e.g.

$ CCPREFIX=/home/cho/work/rpi-tools/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-


Change directory to the kernel source and clean the tree

$ cd /home/cho/work/rpi/linux
$ make mrproper


Copy the .config file we created earlier on Raspberry Pi to the kernel source directory, e.g.

$ cp ~/download/.config /home/cho/work/rpi/linux


We can start to compile the kernel now. In theory we only needed to build the modules. However, here we build the whole kernel and modules just for fun

$ cd /home/cho/work/rpi/linux
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} make oldconfig


Once it is done, we can go ahead to build our third party modules, e.g.

$ cd /home/cho/work/tsc2007/raspi/tsc2007
$ ARCH=arm CROSS_COMPILE=${CCPREFIX} make


Note that you may need to modify the module's Makefile, e.g.
obj-m += tsc2007.o
obj-m += tsc_raspi.o

all:
        make -C /home/cho/work/rpi/linux M=$(PWD) modules

clean:
        make -C /home/cho/work/rpi/linux M=$(PWD) clean


Here, the "-C" path is pointing to the root of the kernel source we just built.

Thursday, June 12, 2014

FreeBSD and CPU scaling

Recently built a new FreeBSD machine with AMD Athlon 5350. Found that if powerd is enabled, the machine will randomly reboot itself. Forcing the lowest CPU frequency to max by setting debug.cpufreq.lowest=2050 in /boot/loader.conf can prevent that. So I suspect it is related to the CPU scaling or C-state.

This problem can be avoided if disabling the C6 state in BIOS.

For reference, the motherboard is ASRock AM1H-ITX.

Here are the CPU freq under normal powerd management.

$ sysctl dev.cpu.0.freq_levels
dev.cpu.0.freq_levels: 2050/5021 1850/4590 1650/3675 1443/3215 1400/2937 1225/2569 1200/2266 1050/1982 1000/1880 875/1645 800/1527 700/1336 600/1145 500/954 400/763 300/572 200/381 100/190


Update: 14 Jun

Found a matching bug report and reported my findings too. Basically, either C6 in BIOS or powerd in FreeBSD need to be disabled to get around the issue. Or, with C6 and powerd enable, the issue can be avoided if CPU throttling is disabled. Add this to /boot/loader.conf:

hint.acpi_throttle.0.disabled=1

But this can affect the reported lowest CPU freq.
Throttling disabled:
root@bart:~ # sysctl dev.cpu | grep freq
dev.cpu.0.freq: 800
dev.cpu.0.freq_levels: 2050/5021 1850/4590 1650/3675 1400/2937 1200/2266 1000/1880 800/1527

Saturday, May 10, 2014

When Java 8 meets Project Euler

Here is a Java implementation of a Project Euler problem. Used lambda expression and steam features of Java 8.
package net.clarenceho.euler;

import java.util.stream.IntStream;

public class Multiples3and5 {

    public static void main(String args[]) {
        System.out.println("The answer is " +
            Multiples3and5.dividableStream(1000).sum());
    }
 
    static boolean dividable(int i) {
        boolean result = false;
        if (i % 3 == 0 || i % 5 == 0) {
            result = true;
        }
        return result;
    }
 
    static IntStream dividableStream(int max) {
        return IntStream.range(1, max)
            .filter(i -> dividable(i));
    }
}