1 of 20

俺はビッグエンディアンで

テストがしたいんだ!!!

How to test on Big Endian arch on Travis CI

@kawasin73

Go Conference 2019 Spring

2 of 20

困った時はドキュメント

DOCUMENT KNOWS EVELYTHING

3 of 20

自己紹介 - Self-introduction

かわしん @kawasin73

東京大学工学部 4 年

- University of Tokyo 4th grade

Go 歴 2 年 (先月まで DMM でインターン)

- I have used Go for 2 years.

趣味は特にない(強いて挙げれば蟻の飼育)

- I like ant.

4 of 20

あらすじ - 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

5 of 20

エンディアンとは

WHAT IS “ENDIANNESS”?

6 of 20

エンディアンとは - 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

7 of 20

エンディアンとは - 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)

8 of 20

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

9 of 20

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.

10 of 20

俺はビッグエンディアンで

テストがしたいんだ!!!

How to test on Big Endian arch on Travis CI

@kawasin73

本編

11 of 20

Travis CI でテスト - Test on Travis CI

Travis CI は amd64 (リトルエンディアン) のみ

- Travis CI only supports amd64 (Little Endian).

QEMU でビッグエンディアンをエミュレートする

- Emulate Big Endian using QEMU.

12 of 20

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.

13 of 20

しかし、動かない - "go test" not works

14 of 20

しかし、動かない - "go test" not works

八方塞がり

STUCK

15 of 20

困った時はドキュメント

DOCUMENT KNOWS EVELYTHING

16 of 20

困った時はドキュメント - Document knows everything

17 of 20

テスト、コンパイルできるってよ - "go test" can be compiled.

コンパイルによって実行バイナリが動く

ビッグエンディアン

- Big Endian Archs supported by Go

ppc64, mips, mips64, s390x

ppc64 ではうまくいった

- ppc64 works

18 of 20

.travis.yml

19 of 20

やったね - It works

20 of 20

まとめ

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