From 80516ed3351477793eec882508969b6b29b69b0a Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Fri, 9 Jun 2017 21:52:26 +0200 Subject: cryptocore: prefetch nonces in 512-byte blocks On my machine, reading 512-byte blocks from /dev/urandom (same via getentropy syscall) is a lot faster in terms of throughput: Blocksize Throughput 16 28.18 MB/s 512 83.75 MB/s For a single-threaded streaming write, this drops the CPU usage of nonceGenerator.Get to almost 1/3: flat flat% sum% cum cum% Before 0 0% 95.08% 0.35s 2.92% github.com/rfjakob/gocryptfs/internal/cryptocore.(*nonceGenerator).Get After 0.01s 0.092% 92.34% 0.13s 1.20% github.com/rfjakob/gocryptfs/internal/cryptocore.(*nonceGenerator).Get This change makes the nonce reading single-threaded, which may hurt massively-parallel writes. --- internal/cryptocore/randprefetch_test.go | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 internal/cryptocore/randprefetch_test.go (limited to 'internal/cryptocore/randprefetch_test.go') diff --git a/internal/cryptocore/randprefetch_test.go b/internal/cryptocore/randprefetch_test.go new file mode 100644 index 0000000..2a568f3 --- /dev/null +++ b/internal/cryptocore/randprefetch_test.go @@ -0,0 +1,40 @@ +package cryptocore + +import ( + "bytes" + "compress/flate" + "runtime" + "sync" + "testing" +) + +// TestRandPrefetch hammers the randPrefetcher with 100 goroutines and verifies +// that the result is incompressible +func TestRandPrefetch(t *testing.T) { + runtime.GOMAXPROCS(10) + p := 100 + l := 200 + vec := make([][]byte, p) + var wg sync.WaitGroup + for i := 0; i < p; i++ { + wg.Add(1) + go func(i int) { + var tmp []byte + for x := 0; x < l; x++ { + tmp = append(tmp, randPrefetcher.read(l)...) + } + vec[i] = tmp + wg.Done() + }(i) + } + wg.Wait() + var b bytes.Buffer + fw, _ := flate.NewWriter(&b, flate.BestCompression) + for _, v := range vec { + fw.Write(v) + } + fw.Close() + if b.Len() < p*l*l { + t.Errorf("random data should be incompressible, but: in=%d compressed=%d\n", p*l*l, b.Len()) + } +} -- cgit v1.2.3