UV-Shell

libuv で遊ぶ(Play around libuv)



IIJ 大津繁樹

2011年10月29日

このプレゼン資料は http://ohtsu.no.de/ にあります。

FireFox4以上/Chrome 限定です。話者と共にスライドが動きます。

プレゼンで「へぇ~」と思った時は、左上の「へぇ~ボタン」を押して下さい。

東京Node学園祭2011

はじめに、(Intro)

Node0.6 のコアイベントエンジン libuv を使って、コマンドラインシェルもどきを作りました。 単なるおもちゃで、全然実用的ではありませんのでありからず。

       

With using libuv, a command line shell clone was made. It is nothing more than toy not for practical use.

自己紹介(About me)

名前: 大津 繁樹, Shigeki Ohtsu (twitter: @jovi0608)

勤務先:IIJ(Intenet Initiative Japan)

仕事: 主に HTML5/Kinect Hack/Node.js といった流行りものの検証・評価をしています。

Node は今年に入ってから触り始め、この夏ぐらいからちょっと力入れ始めました。わりと遅め。


Node Architecture (v>=0.5)

       

何ができる? What can it do?


  • ノンブロッキング TCPソケット(Windows IOCP利用)
  • ノンブロッキングnamed pipes
  • UDP
  • タイマー
  • 子プロセスの spawning (fork&exec)
  • c-ares や uv_getaddrinfoを使ったDNS
  • 非同期ファイルシステムAPI uv_fs_*
  • 高精度(ナノ秒)時間 uv_hrtime
  • カレント実行パス取得 uv_exepath
  • スレッドプールスケジューリング uv_queue_work
  • ファイルシステムイベント(RENAME/CHANGE) uv_fs_event_t
  • VT100 TTY uv_tty_t (stdin/stdout/stderr)
  • IPC and socket プロセス間共有 uv_write2
cf libuv README https://github.com/joyent/libuv

Let's Hello UV!


https://gist.github.com/1321702

#include <stdio.h>
#include "uv.h"
static void cb(uv_write_t* req, int status) {
  printf("Hello from Callback.\n");
  uv_unref(uv_default_loop());
}

int main() {
  uv_tty_t tty;   // tty stream handle
  uv_write_t req; // write request
  int ret = uv_tty_init(uv_default_loop(), &tty, 1, 0);
  uv_buf_t bufs[] = { uv_buf_init("Hello UV!\n",10) };
  ret = uv_write(&req, (uv_stream_t*)&tty, bufs, 1, cb);
  uv_run(uv_default_loop());
  return 0;
}

Hello UV! Demo

すみません、中継を見てください。

       

Sorry, please watch the live video

UV Shell Implementation

実装したコマンド

  • uvecho
  • uvmkdir,uvrmdir
  • uvcat,uvcp,uvtouch,uvrm,uvls
  • uvfwatch
  • uvspawn, uvqueue
  • uvloadavg

{ } 内にコールバックコマンドを記載

uv% uvtouch a.txt { uvcp a.txt b.txt { uvls }}
b.txt a.txt

はまった点 (Trouble Shooted)

コンパイルオプションを Node と合わせよう。

  • Linux: -D_FILE_OFFSET_BITS=64がないと uv_write/uv_read からコールバックが返ってこない。
  • Windows:ランタイムライブラリを「マルチスレッド デバッグ (/MTd)」にしないとビルドエラー

どこで Event Loop の参照カウントが変更さたのか、わかりずらい。

  • Loopを抜けたかったのにずっとまわりっぱなし。

UV-Shell Demo

すみません、中継を見てください。

      

Sorry, please watch the live video

uv_queue_work()

eio_custom()を利用した thread pool の Queue 実行API



Nodeでの利用例

  • 暗号化処理
  • zlib圧縮処理

うーん、UV-Shellではどう利用しよう?/

What to do with uv_queue_work on UV-Shell??

Fibonacci on UV-Shell

やっぱ、フィボナッチ計算でしょ!


Yes! Let's calc Fibonacci.



で早速デモ Demo


uvfib

One More

フィボナッチ数の計算時間はアルゴリズム依存です!

Fibonacci calculation time depends on its algorithm!


最後に1つ



One More before Close

Node誕生日 Node's Birthday

unix:~/tmp/github/node> git log --reverse
commit 9d7895c567e8f38abfff35da1b6d6d6a0a06f9aa
Author: Ryan <ry@tinyclouds.org>
Date:   Mon Feb 16 01:02:00 2009 +0100

    add dependencies
             

Node was born on Feb. 16, 2009.

Node は 2009年2月16日に誕生しました。

Nodeの星座 Node's Sign



Node is NOT a Cancer
but an Aquarius.


Node は「かに座」じゃありません、
「みずがめ座」です。

おわり

御清聴ありがとうございました。

このプレゼンは、

0へぇ~」

でした。

(0/20)
0へぇ~

閲覧中