2014-12-05 20:45:49 +00:00
package restic
2014-11-23 15:48:00 +00:00
2015-02-08 12:40:11 +00:00
import (
"sync"
2014-11-23 15:48:00 +00:00
2015-02-08 12:40:11 +00:00
"github.com/restic/restic/debug"
2014-11-23 15:48:00 +00:00
)
2015-02-08 12:40:11 +00:00
type poolStats struct {
m sync . Mutex
mget map [ string ] int
mput map [ string ] int
mmax map [ string ] int
get int
put int
max int
2014-11-23 15:48:00 +00:00
}
2015-02-08 12:40:11 +00:00
func ( s * poolStats ) Get ( k string ) {
s . m . Lock ( )
defer s . m . Unlock ( )
s . get += 1
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
}
2014-11-23 15:48:00 +00:00
2015-02-08 12:40:11 +00:00
s . mget [ k ] ++
cur = s . mget [ k ] - s . mput [ k ]
if cur > s . mmax [ k ] {
s . mmax [ k ] = cur
}
}
2014-11-23 15:48:00 +00:00
}
2015-02-08 12:40:11 +00:00
func ( s * poolStats ) Put ( k string ) {
s . m . Lock ( )
defer s . m . Unlock ( )
2014-11-23 15:48:00 +00:00
2015-02-08 12:40:11 +00:00
s . put += 1
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 }
nodePool = sync . Pool { New : newNode }
chunkStats = newPoolStats ( )
nodeStats = newPoolStats ( )
)
func newChunkBuf ( ) interface { } {
2014-11-23 15:48:00 +00:00
// create buffer for iv, data and hmac
2014-11-23 21:58:28 +00:00
return make ( [ ] byte , maxCiphertextSize )
2014-11-23 15:48:00 +00:00
}
func newNode ( ) interface { } {
// create buffer for iv, data and hmac
return new ( Node )
}
func GetChunkBuf ( s string ) [ ] byte {
2015-02-08 12:40:11 +00:00
chunkStats . Get ( s )
2014-11-23 15:48:00 +00:00
return chunkPool . Get ( ) . ( [ ] byte )
}
func FreeChunkBuf ( s string , buf [ ] byte ) {
2015-02-08 12:40:11 +00:00
chunkStats . Put ( s )
2014-11-23 15:48:00 +00:00
chunkPool . Put ( buf )
}
func GetNode ( ) * Node {
2015-02-08 12:40:11 +00:00
nodeStats . Get ( "" )
2014-11-23 15:48:00 +00:00
return nodePool . Get ( ) . ( * Node )
}
func FreeNode ( n * Node ) {
2015-02-08 12:40:11 +00:00
nodeStats . Put ( "" )
2014-11-23 15:48:00 +00:00
nodePool . Put ( n )
}
func PoolAlloc ( ) {
2015-02-08 12:40:11 +00:00
debug . Log ( "pools.PoolAlloc" , "pool stats for chunk: get %d, put %d, diff %d, max %d\n" , 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 ] )
}
2014-11-23 15:48:00 +00:00
2015-02-08 12:40:11 +00:00
debug . Log ( "pools.PoolAlloc" , "pool stats for node: get %d, put %d, diff %d, max %d\n" , 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 ] )
}
2014-11-23 15:48:00 +00:00
}