Repository navigation
Expand file tree
/
Copy pathsettings.go
More file actions
62 lines (48 loc) · 1.38 KB
/
Copy pathsettings.go
File metadata and controls
62 lines (48 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package optimal
import (
"github.com/daanv2/go-optimal/pkg/cpu"
env "github.com/daanv2/go-optimal/pkg/environment"
)
func init() {
tempTarget, found := env.Int64.LookupIf("CPU_CACHE_OPTIMAL_SIZE")
if !found {
tempTarget = OptimalBytesForCache(cpu.GetDefaultCacheTarget())
}
targetSize = tempTarget
}
// OptimalBytesForCache returns the optimal amount of bytes that can be used in a cache
func OptimalBytesForCache(cache cpu.CacheKind) int64 {
switch cache {
default:
fallthrough
case cpu.CacheL3:
// 95% of the L3
size := cpu.GetCacheSize(cpu.CacheL3)
return percentage(size, 95)
case cpu.CacheL2:
// 90% of the L2
size := cpu.GetCacheSize(cpu.CacheL2)
return percentage(size, 90)
case cpu.CacheL1:
// 60% of the L1 cache
size := cpu.GetCacheSize(cpu.CacheL1)
return percentage(size, 60)
}
}
func percentage(value, percentage int64) int64 {
return (value * percentage) / 100
}
// The amount of bytes data can maximally be
var targetSize int64
// GetTargetSize The target size in bytes of what can be used in a cache
func GetTargetSize() int64 {
return targetSize
}
// GetTargetSizeFor returns the target size in bytes of what can be used in a cache
func GetTargetSizeFor(kind cpu.CacheKind) int64 {
return OptimalBytesForCache(kind)
}
// SetTargetSize sets the target size in bytes of what can be used in a cache
func SetTargetSize(size int64) {
targetSize = size
}