顯示具有 programming 標籤的文章。 顯示所有文章
顯示具有 programming 標籤的文章。 顯示所有文章

2012年12月6日 星期四

Performance of Unaligned Memory Access in Raspberry Pi

SoC of Raspberry pi is a Broadcom BCM2835. This contains an ARM1176JZFS, ARMv6 architecture.

ARMv6 adds unaligned word(4 bytes) and halfword load and store data access support.
For detail, please check.
I would like to test performance when a user space process invokes a unaligned memory access (only load).
Here is my test environment and cases.

[Environment]

Kernel: Linux xbian 3.6.1 #4 PREEMPT Thu Nov 8 18:54:20 CET 2012 armv6l GNU/Linux
GCC: gcc version 4.6.3 (Debian 4.6.3-12+rpi1)

# cat /proc/cpuinfo
Processor       : ARMv6-compatible processor rev 7
                  (v6l)
BogoMIPS        : 697.95
Features        : swp half thumb fastmult vfp edsp

                  java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xb76
CPU revision    : 7

Hardware        : BCM2708
Revision        : 0002
Serial          : 0000000009b752ff


[Program]

unaligned.c
#include <stdio.h>
#include <stdint.h>

#ifdef USE_GCC_FIXUP
struct __una_u32 {
    uint32_t x;  
} __attribute__((packed));

static inline uint32_t get_unaligned_32(const void *p)
{
    const struct __una_u32 *ptr = 
        (const struct __una_u32 *)p;
    return ptr->x;
}
#endif

int main()
{
    uint8_t buf[16];
    uint32_t i, j;

    for (i = 0; i < sizeof(buf); i++)
        buf[i] = i;

    for (j = 0; j < 100000000; j++) {
        /* unaligned access */
#ifndef USE_GCC_FIXUP
        i = *(unsigned int*)(&buf[1]);
#else
        i = get_unaligned_32(&buf[1]);
#endif
    }

    printf("0x%X\n", i);

    return 0;
}

[Case]

  1. unaligned word access
    • fix up by hardware
      # gcc -c unaligned.c
      # time ./unaligned
      0x4030201

      real    0m2.053s
      user    0m2.040s
      sys     0m0.010s

    • fix up by software (gcc)
      # gcc -DUSE_GCC_FIXUP -o unaligned_gcc unaligned.c
      # time ./unaligned_gcc
      0x4030201

      real    0m6.384s
      user    0m6.370s
      sys     0m0.010s

    The result is very clear.

    Here I add a case is aligned access.
    Only modify:
    #ifndef USE_GCC_FIXUP
            i = *(unsigned int*)(&buf[0]);
    #else
    
    # time ./aligned
    0x3020100

    real    0m1.934s
    user    0m1.920s
    sys     0m0.000s

  2. unaligned double words access
    Modify unaligned.c to support double words access
    • fix up by kernel
      # time ./unaligned64
      0x807060504030201

      real    1m8.754s
      user    0m7.800s
      sys     1m0.830s


    • fix up by software (gcc)
      # time ./unaligned64_gcc
      0x807060504030201

      real    0m9.753s
      user    0m9.700s
      sys     0m0.030s

    Also add a case for aligned access.
    # time ./aligned64
    0x706050403020100

    real    0m2.413s
    user    0m2.400s
    sys     0m0.000s
So, to avoid unaligned memory access if possible!

2011年11月2日 星期三

const in C++

[18] Const correctness
Const Correctness in C++

I love C better than C++~~~!

2011年9月27日 星期二

How to Get High-resolution Time in Windows

Implement a Continuously Updating, High-Resolution Time Provider for Windows

This is a good article to explain how to get high-resolution time in Windows.
The idea can also apply to other operating systems.

C++ dynamic class creation

There are two simple implementations for c++ dynamic class creation. It is useful and make your code cleaner. :)

2011年3月4日 星期五

2010年9月30日 星期四

Wide character

The Microsoft Windows application programming interfaces Win32 and Win64 require that wide character variables be defined as 16-bit values, and that characters be encoded using UTF-16, while modern Unix-like systems generally require 32-bit values encoded using UTF-32.

[REF]
Wide character

2010年8月3日 星期二

Debug Memory Leak in VC

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

_CrtMemState old, new, diff;

_CrtMemCheckpoint(&old);
....
_CrtMemCheckpoint(&new);

_CrtMemDifference(&diff, &old, &new);
_CrtMemDumpStatistics(&diff);

_CrtDumpMemoryLeaks();

2010年7月9日 星期五

好用的 git-svn

最近在弄一個 project, 用的 SCM 是 svn
常常在改一個東西的時候..會有另一個 request 進來..
像是幫我 build 一下..有 bug 要 fix 之類的事..
這個時候需要一個 clean 的 code..
所以我就需要把改過的 code commit 或是 backup 一下...很麻煩...

想到 git 的好用...就試試 git-svn
配合 branch, stash 就可以解決問題..

可參考網頁
使用 git-svn 整合 git 與 svn
Develop with Git on a Google Code Project
Howto use Git and svn together

2010年6月29日 星期二

ENet, a relatively thin, simple and robust network communication layer on top of UDP

ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP.

2010年6月28日 星期一

Lock-free and Wait-free

multi-thread, multi-core programming 中..synchronization 總是個問題
能不用 lock 就不 lock, 最好..

Some notes on lock-free and wait-free algorithms

2010年6月25日 星期五

Memcached and Membase

Memcached
Membase
有時間再看看..有沒有什麼地方可以用~~

2010年5月26日 星期三

約耳談軟體 (Joel on Software)

《約耳談軟體(Joel on Software)》翻譯計畫
線上就有的看....可以不用買書..
我買了.XD

2010年4月14日 星期三

Convert Binary File as a Normal Object File

objcopy -I binary -O elf32-i386 -B i386 your_bin_file your_bin_file.o

Then you can use nm to check your_bin_file.o

01000000 D _binary_your_bin_file_end
01000000 A _binary_your_bin_file_size
00000000 D _binary_your_bin_file_start

Use this in your code
extern char _binary_your_bin_file_end[];
extern char _binary_your_bin_file_size[];
extern char _binary_your_bin_file_start[];

2010年1月6日 星期三

GNU cflow

GNU cflow
一個可以用來 trace code 的工具..
有機會再來試看看~~

2010年1月5日 星期二

GNOME, GTK, GDK, XLib, GLib

這幾個名詞總是記不太起來..可能是太少接觸了吧..^^"
把一個link記下來..以後要回憶比較快~~

2009年9月15日 星期二

Work with a git remote branch

git checkout --track -b my/master-dev2 origin/master-dev

Just keep a record~~

2009年8月8日 星期六

The GNU MP Bignum library

最近因為工作上客戶要求要做 RSA verify 的動作來確保 image 是沒被改動過的..
一開始是參考 openssl ..
在看 RSA 的部分..發現其實 RSA 主要是有一個 big number 的 libaray ..
然後做 big number 的運算..說實在並不是很複雜..
另外看了其它有關 RSA 的 libaray..也都是這樣子..
所以就打算放棄 porting openssl 這個功能超強的大怪物~~只拿 openssl 來當驗證用..
就找了 axTLS 這個 SSL library 來改..把 RSA 的 module 整個抽出來...
還算幸運..花了大概一天的時候把 RSA 的 code 整個抽出來..
然後驗證抽出來的 code 是沒問題的..另外再花了半天的時候把 code port 到我們的環境上...
不過對於 code size 上不是很滿意..目前 RSA 的部分花了大概是 6KB ..
很想把 code size 縮到 5KB 左右..看起來佔 code size 最多的就是 big number 的部分..
所以就上網找了一下..是不是其它的 libaray..
找到了 The GNU MP Bignum library
號稱是 the fastest bignum library on the planet!
不過是 GPL 的 code 就沒特別看了~~只是單純記錄一下下~~:)