DELAEMON BLOG

Live as if you were to die tomorrow. Learn as if you were to live forever.

curl HTTP/2@Mac

HTTP/2用サーバーを立てるなら、debug用途でcurlでもHTTP/2を使いたい。

環境: OS X EI Capitan 10.11.3

普通にやると

$ curl -I google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.jp/?gfe_rd=cr&ei=mshtV9S9BvLC8geyqZ7QAg
Content-Length: 261
Date: Fri, 24 Jun 2016 23:56:10 GMT

http2オプションつければ・・・

$curl -I --http2 google.com
curl: (1) Unsupported protocol

バージョン確認すると対応してるプロトコルや機能が見える

$ curl -V
curl 7.43.0 (x86_64-apple-darwin15.0) libcurl/7.43.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets

ビルドオプションにnghttp2を指定して、curlを再インストール

$ brew reinstall curl --  --with-nghttp2
$ brew link curl --force
$ hash -r

FeaturesにHTTP2が追加されてる

$ curl -V 
curl 7.48.0 (x86_64-apple-darwin15.3.0) libcurl/7.48.0 OpenSSL/1.0.2h zlib/1.2.5 nghttp2/1.10.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

HTTP/2が使われる

$ curl -I --http2 https://google.com
HTTP/2.0 302
cache-control:private
content-type:text/html; charset=UTF-8
location:https://www.google.co.jp/?gfe_rd=cr&ei=ncxtV9W1GbDK8geS5JegAg
content-length:262
date:Sat, 25 Jun 2016 00:13:17 GMT
alternate-protocol:443:quic
alt-svc:quic=":443"; ma=2592000; v="34,33,32,31,30,29,28,27,26,25"

https://じゃないとHTTP/2は使われない

$ curl -I --http2 google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.jp/?gfe_rd=cr&ei=ksxtV6X0MKfK8geE5LWgBA
Content-Length: 261
Date: Sat, 25 Jun 2016 00:13:06 GMT

Mac OSX EI Capitan Wifi Commands

Mac Bookのネットワーク接続(Wifi/Proxy)をターミナルからコマンドから操作したい。

OS version

OS X EI Capitan

Wifiの操作
# On
$ networksetup -setairportpower en0 on

# Off
$ networksetup -setairportpower en0 off

# Info
$ networksetup -getairportnetwork en0

# SSIDの一覧表示
$ sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s

# 任意のネットワークに接続
$ sudo networksetup -setairportnetwork en0 <ssid> <password>
Proxyの操作
# On
$ sudo networksetup -setwebproxystate Wi-Fi on; sudo networksetup -setsecurewebproxystate Wi-Fi on

# Off
$ sudo networksetup -setwebproxystate Wi-Fi off; sudo networksetup -setsecurewebproxystate Wi-Fi off

# Info
$ networksetup -getwebproxy Wi-Fi; networksetup -getsecurewebproxy Wi-Fi
エイリアスに設定
alias wifi-on='networksetup -setairportpower en0 on'
alias wifi-off='networksetup -setairportpower en0 off'
alias wifi-info='networksetup -getairportnetwork en0'
alias proxy-on='sudo networksetup -setwebproxystate Wi-Fi on; sudo networksetup -setsecurewebproxystate Wi-Fi on'
alias proxy-off='sudo networksetup -setwebproxystate Wi-Fi off; sudo networksetup -setsecurewebproxystate Wi-Fi off'
alias proxy-info='networksetup -getwebproxy Wi-Fi; networksetup -getsecurewebproxy Wi-Fi'
alias airport='sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'
実際に打ってるコマンド
$ proxy-on && wifi-on
$ proxy-off && wifi-off
$ proxy-info && wifi-info

LLVM build on Fedora23(VirtualBox)

VirtualBox上のFedora23でLLVMをビルドした。

インストール手順

1. 必要パッケージをインストール

$ sudo dnf install make gcc-c++ texinfo m4 autoconf automake libtool graphviz python-xdot

2. ソースコードをダウンロード・解凍・配置

$ mkdir llvm
$ cd llvm/
$ wget http://llvm.org/releases/3.8.0/llvm-3.8.0.src.tar.xz
$ wget http://llvm.org/releases/3.8.0/cfe-3.8.0.src.tar.xz
$ wget http://llvm.org/releases/3.8.0/compiler-rt-3.8.0.src.tar.xz

xz -dv llvm-3.8.0.src.tar.xz
tar -xvf llvm-3.8.0.src.tar
xz -dv cfe-3.8.0.src.tar.xz
tar xvf cfe-3.8.0.src.tar
mv cfe-3.8.0.src llvm-3.8.0.src/tools/clang
xz -dv compiler-rt-3.8.0.src.tar.xz
tar xvf compiler-rt-3.8.0.src.tar
mv compiler-rt-3.8.0.src llvm-3.8.0.src/projects/compiler-rt

3. ディレクトリを作成して、ビルド

$ mkdir llvm-build
$ cd llvm-build/
$ source ~/.bashrc
../llvm-3.8.0.src/configure --prefix=/usr/local/llvm --enable-optimized
$ make -j2
$ make check
$ sudo make install

かなり時間かかった。度々落ちるけど、メモリ不足だったので、make を繰り返せば終わる

基本操作
$ llvm
ll                llvm-as           llvm-diff         llvm-extract      llvm-nm           llvm-readobj      llvm-symbolizer
llc               llvm-bcanalyzer   llvm-dis          llvm-link         llvm-objdump      llvm-rtdyld       llvm-tblgen
lli               llvm-config       llvm-dsymutil     llvm-lto          llvm-pdbdump      llvm-size
lli-child-target  llvm-cov          llvm-dwarfdump    llvm-mc           llvm-profdata     llvm-split
llvm-ar           llvm-cxxdump      llvm-dwp          llvm-mcmarkup     llvm-ranlib       llvm-stress
$ clang -o hoge.o hoge.c
$ ./hoge.o
hoge
$ clang -emit-llvm -c -o hoge.bc hoge.c #LLVM ビットコードを出力
$ lli -force-interpreter hoge.bc #インタプリタで実行する
$ clang -emit-llvm -S -o hoge.ll hoge.c #LLVM アセンブリを出力
$ llvm-as -o hoge.bc hoge.ll #LLVM アセンブリをLLVM ビットコードへ変換
$ lli hoge.bc #実行
$ llc -o hoge.s hoge.bc #LLVM アセンブリを経由してアセンブリ出力
$ llc -filetype=obj -o hoge.o hoge.ll #オブジェクト出力
$ opt -O3 -o hoge.bc hoge.ll #最適化
$ lli hoge.bc #実行
$ llvm-objdump -d hoge.o #ディスアセンブル

hoge.o:	file format ELF64-x86-64

Disassembly of section .text:
main:
       0:	55 	pushq	%rbp
       1:	48 89 e5 	movq	%rsp, %rbp
       4:	bf 00 00 00 00 	movl	$0, %edi
       9:	31 c0 	xorl	%eax, %eax
       b:	e8 00 00 00 00 	callq	0 <main+10>
      10:	31 c0 	xorl	%eax, %eax
      12:	5d 	popq	%rbp

Clojure Leiningen

Clojureことはじめ

"Leiningen is the easiest way to use Clojure. With a focus on project automation and declarative configuration, it gets out of your way and lets you focus on your code."
Leiningen

leiningen をインストール

$ brew install leiningen

プロジェクトを作成

$ lein new hello

ディレクトリ構成は以下

$ tree
.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── dev-resources
├── doc
│   └── intro.md
├── project.clj
├── resources
├── src
│   └── hello
│       └── core.clj
├── target
│   ├── classes
│   │   └── META-INF
│   │       └── maven
│   │           └── hello
│   │               └── hello
│   │                   └── pom.properties
│   └── stale
│       └── extract-native.dependencies
└── test
    └── hello
        └── core_test.clj

14 directories, 9 files

project.cljを書き換え

cat project.clj
(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]]
  :main hello.core)


lein repl で 実行。lein --help すると "Start a repl session either with the current project or standalone."

$ lein repl
nREPL server started on port 52522 on host 127.0.0.1 - nrepl://127.0.0.1:52522
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_65-b17
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

hello.core=> fo #Tabで候補が表示される
foo      for      force    format
hello.core=> (foo "delaemon")
delaemon Hello, World!
nil

valgrind on Fedora22

Valgrind is an instrumentation framework for building dynamic analysis tools.
Valgrind Home

インストール

$ sudo dnf install valgrind

メモリリーク検出

$ valgrind --leak-check=full ./fib
==4784== Memcheck, a memory error detector
==4784== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4784== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==4784== Command: ./fib
==4784==
832040
==4784==
==4784== HEAP SUMMARY:
==4784==     in use at exit: 0 bytes in 0 blocks
==4784==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4784==
==4784== All heap blocks were freed -- no leaks are possible
==4784==
==4784== For counts of detected and suppressed errors, rerun with: -v
==4784== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

ヒーププロファイラ

$ valgrind --tool=massif ./fib
==4805== Massif, a heap profiler
==4805== Copyright (C) 2003-2013, and GNU GPL'd, by Nicholas Nethercote
==4805== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==4805== Command: ./fib
==4805==
832040
==4805==
$ ls -la massif.out.4805
-rw------- 1 dela dela 144 Feb  8 08:48 massif.out.4805
$ cat massif.out.4805
desc: (none)
cmd: ./fib
time_unit: i
#-----------
snapshot=0
#-----------
time=0
mem_heap_B=0
mem_heap_extra_B=0
mem_stacks_B=0
heap_tree=empty

コールグラフ

$ valgrind --tool=callgrind ./fib
==4837== Callgrind, a call-graph generating cache profiler
==4837== Copyright (C) 2002-2013, and GNU GPL'd, by Josef Weidendorfer et al.
==4837== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==4837== Command: ./fib
==4837==
==4837== For interactive control, run 'callgrind_control -h'.
832040
==4837==
==4837== Events    : Ir
==4837== Collected : 45872890
==4837==
==4837== I   refs:      45,872,890
$ ls -la callgrind.out.4837
-rw------- 1 dela dela 12618 Feb  8 08:49 callgrind.out.4837

perf on Fedora22

perf: Linux profiling with performance counters
Perf Wiki

インストール

$ sudo dnf isntall perf

てきとうなコードを用意

$ gcc fib.c -o fib

イベント数を取得

$ perf stat ./fib
832040
failed to read counter stalled-cycles-frontend
failed to read counter stalled-cycles-backend

 Performance counter stats for './fib':

         11.102711      task-clock (msec)         #    0.967 CPUs utilized
                 2      context-switches          #    0.180 K/sec
                 0      cpu-migrations            #    0.000 K/sec
                48      page-faults               #    0.004 M/sec
        19,328,246      cycles                    #    1.741 GHz
   <not supported>      stalled-cycles-frontend
   <not supported>      stalled-cycles-backend
        46,303,730      instructions              #    2.40  insns per cycle
         9,527,011      branches                  #  858.080 M/sec
             5,258      branch-misses             #    0.06% of all branches

       0.011485366 seconds time elapsed

解析実行

$ perf record ./fib
832040
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.012 MB perf.data (47 samples) ]

解析ファイルが作られる

$ ls -la
-rw------- 1 dela dela 14944 Feb  6 11:27 perf.data
-rw------- 1 dela dela 47080 Feb  6 11:16 perf.data.old

解析結果を見る

$ perf report

Samples: 68  of event 'cycles', Event count (approx.): 20695033
Overhead  Command  Shared Object      Symbol
  94.16%  fib      fib                [.] fib
   2.22%  fib      [kernel.kallsyms]  [k] _raw_spin_lock
   1.54%  fib      [kernel.kallsyms]  [k] filemap_map_pages
   1.49%  fib      [kernel.kallsyms]  [k] async_page_fault
   0.27%  fib      [kernel.kallsyms]  [k] bad_range
   0.17%  fib      [kernel.kallsyms]  [k] run_timer_softirq
   0.08%  fib      [kernel.kallsyms]  [k] __remove_hrtimer
   0.06%  fib      [kernel.kallsyms]  [k] native_write_msr_safe

Build coreutils on Fedora22

Fedora22上にcoreutilsをビルドする。

コードを用意して、./bootstrapを実行すると、必要なパッケージがわかる

git clone git@github.com:coreutils/coreutils.git
cd coreutils
./bootstrap
./bootstrap: line 424: autoconf: command not found
./bootstrap: Error: 'autoconf' not found
./bootstrap: line 424: automake: command not found
./bootstrap: Error: 'automake' not found
./bootstrap: line 424: autopoint: command not found
./bootstrap: Error: 'autopoint' not found
./bootstrap: line 221: bison: command not found
./bootstrap: Error: 'bison' not found
./bootstrap: line 221: gperf: command not found
./bootstrap: Error: 'gperf' not found
./bootstrap: line 424: makeinfo: command not found
./bootstrap: Error: 'makeinfo' not found
./bootstrap: line 221: patch: command not found
./bootstrap: Error: 'patch' not found

必要なものをインストール

sudo dnf install automake bison gperf patch gettext-devel texinfo

configure, make

./configure --prefix=${HOME}/bin/coreutils
make CFLAGS='-g'
make install

prefixで指定したディレクトリにビルドされたものが配置されてる

/home/user/bin/coreutils/bin/ls

ls以外のコマンドも出来てる。
次回はからは必要なものだけ make src/xxxで良い。