mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
Remove pools.go
This commit is contained in:
parent
1f33e29ce2
commit
bdbb3ab329
@ -56,8 +56,8 @@ func BenchmarkChunkEncrypt(b *testing.B) {
|
|||||||
s := SetupBackend(b)
|
s := SetupBackend(b)
|
||||||
defer TeardownBackend(b, s)
|
defer TeardownBackend(b, s)
|
||||||
|
|
||||||
buf := restic.GetChunkBuf("BenchmarkChunkEncrypt")
|
buf := make([]byte, chunker.MaxSize)
|
||||||
buf2 := restic.GetChunkBuf("BenchmarkChunkEncrypt")
|
buf2 := make([]byte, chunker.MaxSize)
|
||||||
chunkBuf := make([]byte, chunkerBufSize)
|
chunkBuf := make([]byte, chunkerBufSize)
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
@ -66,9 +66,6 @@ func BenchmarkChunkEncrypt(b *testing.B) {
|
|||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
benchmarkChunkEncrypt(b, buf, buf2, chunkBuf, rd, s.Key())
|
benchmarkChunkEncrypt(b, buf, buf2, chunkBuf, rd, s.Key())
|
||||||
}
|
}
|
||||||
|
|
||||||
restic.FreeChunkBuf("BenchmarkChunkEncrypt", buf)
|
|
||||||
restic.FreeChunkBuf("BenchmarkChunkEncrypt", buf2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchmarkChunkEncryptP(b *testing.PB, buf, chunkBuf []byte, rd Rdr, key *crypto.Key) {
|
func benchmarkChunkEncryptP(b *testing.PB, buf, chunkBuf []byte, rd Rdr, key *crypto.Key) {
|
||||||
@ -93,7 +90,7 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
|
|||||||
|
|
||||||
data := Random(23, 10<<20) // 10MiB
|
data := Random(23, 10<<20) // 10MiB
|
||||||
|
|
||||||
buf := restic.GetChunkBuf("BenchmarkChunkEncryptParallel")
|
buf := make([]byte, chunker.MaxSize)
|
||||||
chunkBuf := make([]byte, chunkerBufSize)
|
chunkBuf := make([]byte, chunkerBufSize)
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
@ -105,8 +102,6 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
|
|||||||
benchmarkChunkEncryptP(pb, buf, chunkBuf, rd, s.Key())
|
benchmarkChunkEncryptP(pb, buf, chunkBuf, rd, s.Key())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
restic.FreeChunkBuf("BenchmarkChunkEncryptParallel", buf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func archiveDirectory(b testing.TB) {
|
func archiveDirectory(b testing.TB) {
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
"github.com/restic/restic"
|
|
||||||
"github.com/restic/restic/backend"
|
"github.com/restic/restic/backend"
|
||||||
"github.com/restic/restic/backend/local"
|
"github.com/restic/restic/backend/local"
|
||||||
"github.com/restic/restic/backend/sftp"
|
"github.com/restic/restic/backend/sftp"
|
||||||
@ -182,7 +181,4 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// this prints some statistics for memory management using the debug package
|
|
||||||
restic.PoolAlloc()
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/restic"
|
|
||||||
"github.com/restic/restic/chunker"
|
"github.com/restic/restic/chunker"
|
||||||
"github.com/restic/restic/crypto"
|
"github.com/restic/restic/crypto"
|
||||||
. "github.com/restic/restic/test"
|
. "github.com/restic/restic/test"
|
||||||
@ -29,7 +28,9 @@ func TestEncryptDecrypt(t *testing.T) {
|
|||||||
_, err := io.ReadFull(RandomReader(42, size), data)
|
_, err := io.ReadFull(RandomReader(42, size), data)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
ciphertext, err := crypto.Encrypt(k, restic.GetChunkBuf("TestEncryptDecrypt"), data)
|
buf := make([]byte, size+crypto.Extension)
|
||||||
|
|
||||||
|
ciphertext, err := crypto.Encrypt(k, buf, data)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
Assert(t, len(ciphertext) == len(data)+crypto.Extension,
|
Assert(t, len(ciphertext) == len(data)+crypto.Extension,
|
||||||
"ciphertext length does not match: want %d, got %d",
|
"ciphertext length does not match: want %d, got %d",
|
||||||
@ -41,8 +42,6 @@ func TestEncryptDecrypt(t *testing.T) {
|
|||||||
"plaintext length does not match: want %d, got %d",
|
"plaintext length does not match: want %d, got %d",
|
||||||
len(data), len(plaintext))
|
len(data), len(plaintext))
|
||||||
|
|
||||||
restic.FreeChunkBuf("TestEncryptDecrypt", ciphertext)
|
|
||||||
|
|
||||||
Equals(t, plaintext, data)
|
Equals(t, plaintext, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,8 +225,6 @@ func BenchmarkEncryptDecryptReader(b *testing.B) {
|
|||||||
_, err = io.Copy(ioutil.Discard, r)
|
_, err = io.Copy(ioutil.Discard, r)
|
||||||
OK(b, err)
|
OK(b, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
restic.PoolAlloc()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkDecrypt(b *testing.B) {
|
func BenchmarkDecrypt(b *testing.B) {
|
||||||
@ -236,10 +233,8 @@ func BenchmarkDecrypt(b *testing.B) {
|
|||||||
|
|
||||||
k := crypto.NewRandomKey()
|
k := crypto.NewRandomKey()
|
||||||
|
|
||||||
ciphertext := restic.GetChunkBuf("BenchmarkDecrypt")
|
plaintext := make([]byte, size)
|
||||||
defer restic.FreeChunkBuf("BenchmarkDecrypt", ciphertext)
|
ciphertext := make([]byte, size+crypto.Extension)
|
||||||
plaintext := restic.GetChunkBuf("BenchmarkDecrypt")
|
|
||||||
defer restic.FreeChunkBuf("BenchmarkDecrypt", plaintext)
|
|
||||||
|
|
||||||
ciphertext, err := crypto.Encrypt(k, ciphertext, data)
|
ciphertext, err := crypto.Encrypt(k, ciphertext, data)
|
||||||
OK(b, err)
|
OK(b, err)
|
||||||
|
111
pools.go
111
pools.go
@ -1,111 +0,0 @@
|
|||||||
package restic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/restic/restic/chunker"
|
|
||||||
"github.com/restic/restic/crypto"
|
|
||||||
"github.com/restic/restic/debug"
|
|
||||||
)
|
|
||||||
|
|
||||||
type poolStats struct {
|
|
||||||
m sync.Mutex
|
|
||||||
mget map[string]int
|
|
||||||
mput map[string]int
|
|
||||||
mmax map[string]int
|
|
||||||
|
|
||||||
new int
|
|
||||||
get int
|
|
||||||
put int
|
|
||||||
max int
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
maxCiphertextSize = crypto.Extension + chunker.MaxSize
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *poolStats) Get(k string) {
|
|
||||||
s.m.Lock()
|
|
||||||
defer s.m.Unlock()
|
|
||||||
|
|
||||||
s.get++
|
|
||||||
cur := s.get - s.put
|
|
||||||
if cur > s.max {
|
|
||||||
s.max = cur
|
|
||||||
}
|
|
||||||
|
|
||||||
if k != "" {
|
|
||||||
if _, ok := s.mget[k]; !ok {
|
|
||||||
s.mget[k] = 0
|
|
||||||
s.mput[k] = 0
|
|
||||||
s.mmax[k] = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
s.mget[k]++
|
|
||||||
|
|
||||||
cur = s.mget[k] - s.mput[k]
|
|
||||||
if cur > s.mmax[k] {
|
|
||||||
s.mmax[k] = cur
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *poolStats) Put(k string) {
|
|
||||||
s.m.Lock()
|
|
||||||
defer s.m.Unlock()
|
|
||||||
|
|
||||||
s.put++
|
|
||||||
|
|
||||||
if k != "" {
|
|
||||||
s.mput[k]++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPoolStats() *poolStats {
|
|
||||||
return &poolStats{
|
|
||||||
mget: make(map[string]int),
|
|
||||||
mput: make(map[string]int),
|
|
||||||
mmax: make(map[string]int),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
chunkPool = sync.Pool{New: newChunkBuf}
|
|
||||||
|
|
||||||
chunkStats = newPoolStats()
|
|
||||||
nodeStats = newPoolStats()
|
|
||||||
)
|
|
||||||
|
|
||||||
func newChunkBuf() interface{} {
|
|
||||||
chunkStats.m.Lock()
|
|
||||||
defer chunkStats.m.Unlock()
|
|
||||||
chunkStats.new++
|
|
||||||
|
|
||||||
// create buffer for iv, data and mac
|
|
||||||
return make([]byte, maxCiphertextSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetChunkBuf(s string) []byte {
|
|
||||||
chunkStats.Get(s)
|
|
||||||
return chunkPool.Get().([]byte)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FreeChunkBuf(s string, buf []byte) {
|
|
||||||
chunkStats.Put(s)
|
|
||||||
chunkPool.Put(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PoolAlloc() {
|
|
||||||
debug.Log("pools.PoolAlloc", "pool stats for chunk: new %d, get %d, put %d, diff %d, max %d\n",
|
|
||||||
chunkStats.new, chunkStats.get, chunkStats.put, chunkStats.get-chunkStats.put, chunkStats.max)
|
|
||||||
for k, v := range chunkStats.mget {
|
|
||||||
debug.Log("pools.PoolAlloc", "pool stats for chunk[%s]: get %d, put %d, diff %d, max %d\n",
|
|
||||||
k, v, chunkStats.mput[k], v-chunkStats.mput[k], chunkStats.mmax[k])
|
|
||||||
}
|
|
||||||
|
|
||||||
debug.Log("pools.PoolAlloc", "pool stats for node: new %d, get %d, put %d, diff %d, max %d\n",
|
|
||||||
nodeStats.new, nodeStats.get, nodeStats.put, nodeStats.get-nodeStats.put, nodeStats.max)
|
|
||||||
for k, v := range nodeStats.mget {
|
|
||||||
debug.Log("pools.PoolAlloc", "pool stats for node[%s]: get %d, put %d, diff %d, max %d\n", k, v, nodeStats.mput[k], v-nodeStats.mput[k], nodeStats.mmax[k])
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user