http://www.cs.virginia.edu/stream/
ダウンロードして、gcc でコンパイルするだけで利用できます。計測結果は次のような感じ。
[root@hoge Downloads]# ./stream_c.exe ------------------------------------------------------------- STREAM version $Revision: 5.10 $ ------------------------------------------------------------- This system uses 8 bytes per array element. ------------------------------------------------------------- Array size = 10000000 (elements), Offset = 0 (elements) Memory per array = 76.3 MiB (= 0.1 GiB). Total memory required = 228.9 MiB (= 0.2 GiB). Each kernel will be executed 10 times. The *best* time for each kernel (excluding the first iteration) will be used to compute the reported bandwidth. ------------------------------------------------------------- Your clock granularity/precision appears to be 1 microseconds. Each test below will take on the order of 43741 microseconds. (= 43741 clock ticks) Increase the size of the arrays if this shows that you are not getting at least 20 clock ticks per test. ------------------------------------------------------------- WARNING -- The above is only a rough guideline. For best results, please be sure you know the precision of your system timer. ------------------------------------------------------------- Function Best Rate MB/s Avg time Min time Max time Copy: 2966.1 0.054175 0.053943 0.054633 Scale: 2920.8 0.054945 0.054779 0.055157 Add: 3518.2 0.068352 0.068217 0.068576 Triad: 3520.2 0.072915 0.068177 0.105948 ------------------------------------------------------------- Solution Validates: avg error less than 1.000000e-13 on all three arrays -------------------------------------------------------------実行環境は ThinkPad X301 + CentOS6 x86_64 です。DIMM の種類は DDR3-800 です。
いろいろなマシンで計ることを考えると、標準のツールで代用できるといいので、dd を使ってページキャッシュから読み出す方法を試してみました。
[root@hoge ~]# ls -l test.file -rw-r--r-- 1 root root 1024000000 Aug 18 19:05 test.file ※予め用意しておいたテスト用ファイル [root@hoge ~]# ls -lh test.file -rw-r--r-- 1 root root 977M Aug 18 19:05 test.file [root@hoge ~]# dd if=test.file of=/dev/null bs=4k 250000+0 records in 250000+0 records out 1024000000 bytes (1.0 GB) copied, 3.89807 s, 263 MB/s ※1回目はキャッシュに載ってないのでSSDの計測になる [root@hoge ~]# dd if=test.file of=/dev/null bs=4k 250000+0 records in 250000+0 records out 1024000000 bytes (1.0 GB) copied, 0.558606 s, 1.8 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=4k 250000+0 records in 250000+0 records out 1024000000 bytes (1.0 GB) copied, 0.492317 s, 2.1 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=4k 250000+0 records in 250000+0 records out 1024000000 bytes (1.0 GB) copied, 0.488601 s, 2.1 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=8k ※以降、ブロックサイズを2倍にして、3回づつ計測 125000+0 records in 125000+0 records out 1024000000 bytes (1.0 GB) copied, 0.39059 s, 2.6 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=8k 125000+0 records in 125000+0 records out 1024000000 bytes (1.0 GB) copied, 0.387988 s, 2.6 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=8k 125000+0 records in 125000+0 records out 1024000000 bytes (1.0 GB) copied, 0.387874 s, 2.6 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=16k 62500+0 records in 62500+0 records out 1024000000 bytes (1.0 GB) copied, 0.3597 s, 2.8 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=16k 62500+0 records in 62500+0 records out 1024000000 bytes (1.0 GB) copied, 0.359784 s, 2.8 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=16k 62500+0 records in 62500+0 records out 1024000000 bytes (1.0 GB) copied, 0.358306 s, 2.9 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=32k 31250+0 records in 31250+0 records out 1024000000 bytes (1.0 GB) copied, 0.343135 s, 3.0 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=32k 31250+0 records in 31250+0 records out 1024000000 bytes (1.0 GB) copied, 0.342479 s, 3.0 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=32k 31250+0 records in 31250+0 records out 1024000000 bytes (1.0 GB) copied, 0.342495 s, 3.0 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=64k 15625+0 records in 15625+0 records out 1024000000 bytes (1.0 GB) copied, 0.32722 s, 3.1 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=64k 15625+0 records in 15625+0 records out 1024000000 bytes (1.0 GB) copied, 0.327119 s, 3.1 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=64k 15625+0 records in 15625+0 records out 1024000000 bytes (1.0 GB) copied, 0.328853 s, 3.1 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=128k 7812+1 records in 7812+1 records out 1024000000 bytes (1.0 GB) copied, 0.319119 s, 3.2 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=128k 7812+1 records in 7812+1 records out 1024000000 bytes (1.0 GB) copied, 0.320496 s, 3.2 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=128k 7812+1 records in 7812+1 records out 1024000000 bytes (1.0 GB) copied, 0.325546 s, 3.1 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=256k 3906+1 records in 3906+1 records out 1024000000 bytes (1.0 GB) copied, 0.314302 s, 3.3 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=256k 3906+1 records in 3906+1 records out 1024000000 bytes (1.0 GB) copied, 0.315943 s, 3.2 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=256k 3906+1 records in 3906+1 records out 1024000000 bytes (1.0 GB) copied, 0.316061 s, 3.2 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=512k 1953+1 records in 1953+1 records out 1024000000 bytes (1.0 GB) copied, 0.313515 s, 3.3 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=512k 1953+1 records in 1953+1 records out 1024000000 bytes (1.0 GB) copied, 0.315126 s, 3.2 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=512k 1953+1 records in 1953+1 records out 1024000000 bytes (1.0 GB) copied, 0.316339 s, 3.2 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=1024k 976+1 records in 976+1 records out 1024000000 bytes (1.0 GB) copied, 0.343852 s, 3.0 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=1024k 976+1 records in 976+1 records out 1024000000 bytes (1.0 GB) copied, 0.338554 s, 3.0 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=1024k 976+1 records in 976+1 records out 1024000000 bytes (1.0 GB) copied, 0.342826 s, 3.0 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=2048k 488+1 records in 488+1 records out 1024000000 bytes (1.0 GB) copied, 0.674414 s, 1.5 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=2048k 488+1 records in 488+1 records out 1024000000 bytes (1.0 GB) copied, 0.68936 s, 1.5 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=2048k 488+1 records in 488+1 records out 1024000000 bytes (1.0 GB) copied, 0.677181 s, 1.5 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=4096k 244+1 records in 244+1 records out 1024000000 bytes (1.0 GB) copied, 0.773668 s, 1.3 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=4096k 244+1 records in 244+1 records out 1024000000 bytes (1.0 GB) copied, 0.76228 s, 1.3 GB/s [root@hoge ~]# dd if=test.file of=/dev/null bs=4096k 244+1 records in 244+1 records out 1024000000 bytes (1.0 GB) copied, 0.754367 s, 1.4 GB/s [root@hoge ~]#このように、ブロックサイズ (bs) が小さすぎてはダメですが、かと言って、大きすぎてもダメでした。実験は大事ですね。CPU のキャッシュの影響かな? おそらくは
この方法だと、read システムコールの延長のカーネル内コードで、ページキャッシュ上のデータが、ユーザ空間にコピーされる動きになるはずです。/dev/null への write システムコールが余計ですが、カーネル内では何もせず(コピー処理などはなし)にリターンするので、ほぼ無視できるかと思います。/dev/null に対応するコードは、drivers/char/mem.c の中です。
別のやり方(/dev/zero を読み出す方法)だと、次のように stream と乖離した値が見えます。
[root@hoge ~]# dd if=/dev/zero of=/dev/null bs=512k count=10000 10000+0 records in 10000+0 records out 5242880000 bytes (5.2 GB) copied, 0.738394 s, 7.1 GB/s [root@hoge ~]#/dev/zero を read する際のカーネル内コードは、次のとおりです。メモリ間のコピーではなく、レジスタ上の値(ゼロ)をメモリへ転送(ストア)する繰り返し処理になってます。
    677 static ssize_t read_zero(struct file * file, char __user * buf,
    678                          size_t count, loff_t *ppos)
    679 {
    680         size_t written;
    681 
    682         if (!count)
    683                 return 0;
    684 
    685         if (!access_ok(VERIFY_WRITE, buf, count))
    686                 return -EFAULT;
    687 
    688         written = 0;
    689         while (count) {
    690                 unsigned long unwritten;
    691                 size_t chunk = count;
    692 
    693                 if (chunk > PAGE_SIZE)
    694                         chunk = PAGE_SIZE;      /* Just for latency reasons */
    695                 unwritten = __clear_user(buf, chunk);
    696                 written += chunk - unwritten;
    697                 if (unwritten)
    698                         break;
    699                 if (signal_pending(current))
    700                         return written ? written : -ERESTARTSYS;
    701                 buf += chunk;
    702                 count -= chunk;
    703                 cond_resched();
    704         }
    705         return written ? written : -EFAULT;
    706 }
"drivers/char/mem.c"
     60 /*
     61  * Zero Userspace
     62  */
     63 
     64 unsigned long __clear_user(void __user *addr, unsigned long size)
     65 {
     66         long __d0;
     67         might_fault();
     68         /* no memory constraint because it doesn't change any memory gcc knows
     69            about */
     70         asm volatile(
     71                 "       testq  %[size8],%[size8]\n"
     72                 "       jz     4f\n"
     73                 "0:     movq %[zero],(%[dst])\n"
     74                 "       addq   %[eight],%[dst]\n"
     75                 "       decl %%ecx ; jnz   0b\n"
     76                 "4:     movq  %[size1],%%rcx\n"
     77                 "       testl %%ecx,%%ecx\n"
     78                 "       jz     2f\n"
     79                 "1:     movb   %b[zero],(%[dst])\n"
     80                 "       incq   %[dst]\n"
     81                 "       decl %%ecx ; jnz  1b\n"
     82                 "2:\n"
     83                 ".section .fixup,\"ax\"\n"
     84                 "3:     lea 0(%[size1],%[size8],8),%[size8]\n"
     85                 "       jmp 2b\n"
     86                 ".previous\n"
     87                 _ASM_EXTABLE(0b,3b)
     88                 _ASM_EXTABLE(1b,2b)
     89                 : [size8] "=&c"(size), [dst] "=&D" (__d0)
     90                 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
     91                   [zero] "r" (0UL), [eight] "r" (8UL));
     92         return size;
     93 }
     94 EXPORT_SYMBOL(__clear_user);
"arch/x86/lib/usercopy_64.c"
なお、MB/s の MB は、stream も dd も 1000*1000 単位で計算されています。GB/s も同様。1024*1024単位ではないか?と迷うことがあるので、備忘録。