俺はビッグエンディアンで
テストがしたいんだ!!!
How to test on Big Endian arch on Travis CI
@kawasin73
Go Conference 2019 Spring
困った時はドキュメント
DOCUMENT KNOWS EVELYTHING
自己紹介 - Self-introduction
かわしん @kawasin73
東京大学工学部 4 年
- University of Tokyo 4th grade
Go 歴 2 年 (先月まで DMM でインターン)
- I have used Go for 2 years.
趣味は特にない(強いて挙げれば蟻の飼育)
- I like ant.
あらすじ - What I talk today
ビットセットライブラリを作って ビッグエンディアンでテスト した
- I create BitSet library and test it on Big Endian.
https://github.com/kawasin73/bitset
※ ビットセットは []bool の省メモリ版データ構造
- NOTE : BitSet is a less memory data structure like []bool
エンディアンとは
WHAT IS “ENDIANNESS”?
エンディアンとは - What is Endianness?
エンディアンは バイトの並び順
- Endianness is byte order.
ビッグエンディアン (Big Endian)
リトルエンディアン (Little Endian)
0x01
0x23
0x45
0x67
0x67
0x45
0x23
0x01
Example : uint32(0x01234567)
0 1 2 3 byte address
エンディアンとは - What is Endianness?
エンディアンは CPU アーキテクチャによって異なる
- Endianness depends on CPU architecture.
e.g. amd64(LittleEndian), ppc(BigEndian)
バイナリデータを外部とやりとり するとき(ファイル読み書き、ネットワーク通信)にエンディアンを意識する。異なるエンディアンでは間違った値になるため
- You must be aware of endianness when you handle binary data
from/to outside (e.g. File data, Network access)
Go におけるエンディアン - Endianness in Go
"encoding/binary" パッケージが []uint64 から []byte への安全な変換を提供
- "encoding/binary" package converts []uint64 to []byte safely.
大量のメモリコピー を変換時に行うため、頻繁なファイル書き出しには 🙅♂️
- But heavy memory copy occurs on converting.
🙅♂️ write back to file many times
unsafe.Pointer を使う - Use unsafe.Pointer
unsafe.Pointer を使って []uint64 を []byte に強制型変換
ファイルに 効率的 に読み書き
- Convert []uint64 to []byte using unsafe.Pointer.
Write []uint64 to a file efficiently.
両方のエンディアンマシンで正しく動くか テスト する必要がある
- You need to test it on both endianness.
俺はビッグエンディアンで
テストがしたいんだ!!!
How to test on Big Endian arch on Travis CI
@kawasin73
本編
Travis CI でテスト - Test on Travis CI
Travis CI は amd64 (リトルエンディアン) のみ
- Travis CI only supports amd64 (Little Endian).
QEMU でビッグエンディアンをエミュレートする
- Emulate Big Endian using QEMU.
QEMU で go test - "go test" with QEMU
Go の処理系が動くのはビッグエンディアンの中だと s390x のみ
- Go tools only supports s390x in Big Endian.
Supported Arch : amd64, 386, arm, arm64, s390x, ppc64le
https://golang.org/doc/install#requirements
※ arm はリトルエンディアン。armbe がビッグエンディアン
- NOTE : arm is Little Endian. armbe is Big Endian.
しかし、動かない - "go test" not works
しかし、動かない - "go test" not works
八方塞がり
STUCK
困った時はドキュメント
DOCUMENT KNOWS EVELYTHING
困った時はドキュメント - Document knows everything
テスト、コンパイルできるってよ - "go test" can be compiled.
コンパイルによって実行バイナリが動く
ビッグエンディアン
- Big Endian Archs supported by Go
ppc64, mips, mips64, s390x
ppc64 ではうまくいった
- ppc64 works
.travis.yml
やったね - It works
まとめ
QEMU によってビッグエンディアンをエミュレート
- Emulate Big Endian using QEMU.
go test はコンパイルできる
- "go test" can be compiled by "go test -c"
https://kawasin73.hatenablog.com/entry/2018/12/01/172708
https://github.com/kawasin73/bitset/issues/5