@@ -24,16 +24,17 @@ import (
2424)
2525
2626type Allocator interface {
27- Balance (memLimit MemSize ) error // Balance allocations to buckets within memory limit
28- Switches (switches.Funs ) []string // Get selected memory switches from current allocations
29- GetWarnings () []string // Get warnings (if balancing succeeded)
30- GetSizes () map [string ]MemSize // Get a map of all the sizes after balancing
27+ Balance (memLimit MemSize ) error // Balance allocations to buckets within memory limit
28+ Switches (switches.Funs ) []string // Get selected memory switches from current allocations
29+ GetWarnings () []string // Get warnings (if balancing succeeded)
30+ GenerateInitialAllocations ( map [string ]float64 ) // Generate initial buckets
3131}
3232
3333type allocator struct {
34- originalSizes map [string ]Range // unmodified after creation
35- buckets map [string ]Bucket // named buckets for allocation
36- warnings []string // warnings if allocation found issues
34+ originalSizes map [string ]Range // unmodified after creation
35+ buckets map [string ]Bucket // named buckets for allocation
36+ warnings []string // warnings if allocation found issues
37+ allocations map [string ]MemSize // calculated sizes
3738}
3839
3940func NewAllocator (sizes map [string ]Range , heuristics map [string ]float64 ) (* allocator , error ) {
@@ -53,6 +54,13 @@ const (
5354 CLOSE_TO_DEFAULT_FACTOR float64 = 0.1
5455)
5556
57+ //Set of initial minimums
58+ var initialMinimums = map [string ]MemSize {
59+ "heap" : MemSize (2097152 ), //2MB minimum works for Java 7 and Java 8
60+ "metaspace" : NewMemSize (262144 ), //256KB
61+ "permgen" : NewMemSize (1048576 ), // 1MB
62+ }
63+
5664// Balance memory between buckets, adjusting stack units, observing
5765// constraints, and detecting memory wastage and default proximity.
5866func (a * allocator ) Balance (memLimit MemSize ) error {
@@ -74,13 +82,23 @@ func (a *allocator) Balance(memLimit MemSize) error {
7482 // reset stack bucket, if it exists
7583 a .unnormaliseStack (stackBucket , estNumThreads )
7684
85+ // generate allocations
86+ a .generateAllocations ()
87+
7788 return nil
7889}
7990
91+ func (a * allocator ) generateAllocations () {
92+ a .allocations = map [string ]MemSize {}
93+ for name , bucket := range a .buckets {
94+ a .allocations [name ] = * bucket .GetSize ()
95+ }
96+ }
97+
8098func (a * allocator ) Switches (sfs switches.Funs ) []string {
8199 var strs = make ([]string , 0 , 10 )
82- for s , b := range a .buckets {
83- strs = append (strs , sfs .Apply (s , b .GetSize (). String ())... )
100+ for s , b := range a .allocations {
101+ strs = append (strs , sfs .Apply (s , b .String ())... )
84102 }
85103 return strs
86104}
@@ -89,12 +107,24 @@ func (a *allocator) GetWarnings() []string {
89107 return a .warnings
90108}
91109
92- func (a * allocator ) GetSizes () map [string ]MemSize {
93- sizes := map [string ]MemSize {}
94- for name , bucket := range a .buckets {
95- sizes [name ] = * bucket .GetSize ()
110+ func (a * allocator ) GenerateInitialAllocations (initials map [string ]float64 ) {
111+ for name , ratio := range initials {
112+ initial_name := "initial_" + name
113+
114+ maxSize := a .buckets [name ].GetSize ()
115+ initSize , initMin := maxSize .Scale (ratio ), initialMinimums [name ]
116+
117+ if ! initSize .LessThan (initMin ) {
118+ a .allocations [initial_name ] = initSize
119+ } else if maxSize .LessThan (initMin ) {
120+ a .allocations [initial_name ] = * maxSize
121+ } else {
122+ a .allocations [initial_name ] = initMin
123+ a .warnings = append (a .warnings , fmt .Sprintf (
124+ "The configured initial memory size %[1]s for %[2]s is less than the minimum %[3]s. Setting initial value to %[3]s." ,
125+ initSize , name , initMin ))
126+ }
96127 }
97- return sizes
98128}
99129
100130// getSizes returns a slice of memory type range strings
0 commit comments