Skip to content

Commit ebded49

Browse files
author
Steve Powell
committed
Refactor pull request 1
Merge switches functions; eliminate initial switches step; remove GetSizes() method. [#99686520]
1 parent 714ab24 commit ebded49

File tree

6 files changed

+70
-97
lines changed

6 files changed

+70
-97
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
[Install Go][] and then `get` the memory calculator (in the Go source tree).
77

88
We run our tests with [Ginkgo/Gomega][] and manage dependencies with [Godep][].
9-
Ginkgo is one of the dependencies we manage, so get Godep before starting work.
9+
Ginkgo is one of the dependencies we manage, so get Godep before starting work:
1010

1111
```shell
1212
go get -v github.com/cloudfoundry/java-buildpack-memory-calculator
1313
cd src/github.com/cloudfoundry/java-buildpack-memory-calculator
1414

1515
go get -v github.com/tools/godep
1616
```
17+
1718
(The `-v` options on `go get` are there so you can see what packages are compiled under the covers.)
1819

1920
The (bash) script `scripts/runTests` uses (the correct version of) Ginkgo to
2021
run the tests (using the correct versions of the dependencies). `runTests`
2122
will recompile Ginkgo if necessary.
2223

23-
The parameters to runTests are passed directly to Ginkgo. For example:
24+
The parameters to `runTests` are passed directly to Ginkgo. For example:
2425

2526
```shell
2627
scripts/runTests -r=false memory
@@ -38,7 +39,7 @@ To develop against the code, you should issue:
3839
```shell
3940
godep restore
4041
```
41-
in the project directory before running tests or building from the command line.
42+
in the project directory before building or running tests directly from the command line.
4243

4344
If you wish to develop against a particular tagged *version* then, in the
4445
project directory, you need to checkout this version (using
@@ -49,6 +50,15 @@ obtained, or else it cannot be (re)set to the version this project depends on.
4950
Normally `go get -u <project>` for the dependency in error will then allow
5051
`godep restore` to complete normally.
5152

53+
### Release binaries
54+
55+
The executables are built for more than one platform, so the Go compiler must exist
56+
for the target platforms we need (currently linux and darwin). The shell script (`buildReleases`)
57+
will use the Go compiler with the `GOOS` environment variable to generate the executables.
58+
59+
This will not work if the Go installation doesn't support all these platforms, so you may have to
60+
ensure Go is installed with cross-compiler support.
61+
5262
[Install Go]: http://golang.org/doc/install
5363
[Godep]: http://github.com/tools/godep
5464
[Ginkgo/Gomega]: http://github.com/onsi/ginkgo

integration/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ var _ = Describe("java-buildpack-memory-calculator executable", func() {
322322

323323
It("issues a warning", func() {
324324
Ω(cmdErr).ShouldNot(HaveOccurred(), "exit status")
325-
Ω(string(sErr)).Should(Equal("The configured initial memory size 0 for heap is less than the jvm minimum 2M. Setting initial value to 2M.\n"), "stderr")
325+
Ω(string(sErr)).Should(Equal("The configured initial memory size 0 for heap is less than the minimum 2M. Setting initial value to 2M.\n"), "stderr")
326326
Ω(strings.Split(string(sOut), " ")).Should(ConsistOf(
327327
"-Xss1M",
328328
"-XX:MaxPermSize=1258291K",

main.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,15 @@ func main() {
4545
fmt.Fprintf(os.Stderr, "Cannot balance memory: %s", err)
4646
os.Exit(1)
4747
}
48-
if warnings := allocator.GetWarnings(); len(warnings) != 0 {
49-
fmt.Fprintln(os.Stderr, strings.Join(warnings, "\n"))
50-
}
51-
52-
allocatorSwitches := allocator.Switches(switches.AllocatorJreSwitchFuns)
5348

54-
initialSwitches, warnings := memory.InitialsSwitches(initials, allocator.GetSizes(), switches.InitialJreSwitchFuns)
49+
allocator.GenerateInitialAllocations(initials)
5550

56-
if len(warnings) != 0 {
51+
allocatorSwitches := allocator.Switches(switches.AllocatorJreSwitchFuns)
52+
53+
if warnings := allocator.GetWarnings(); len(warnings) != 0 {
5754
fmt.Fprintln(os.Stderr, strings.Join(warnings, "\n"))
5855
}
5956

60-
switches := append(allocatorSwitches, initialSwitches...)
61-
62-
fmt.Fprint(os.Stdout, strings.Join(switches, " "))
57+
fmt.Fprint(os.Stdout, strings.Join(allocatorSwitches, " "))
6358

6459
}

memory/allocator.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ import (
2424
)
2525

2626
type 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

3333
type 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

3940
func 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.
5866
func (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+
8098
func (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

memory/initials.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

memory/switches/switches.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ func apply(ss ...string) func(string) []string {
3636

3737
var (
3838
AllocatorJreSwitchFuns = Funs{
39-
"heap": apply("-Xmx%s"),
40-
"metaspace": apply("-XX:MaxMetaspaceSize=%s"),
41-
"permgen": apply("-XX:MaxPermSize=%s"),
42-
"stack": apply("-Xss%s"),
43-
}
44-
45-
InitialJreSwitchFuns = Funs{
46-
"heap": apply("-Xms%s"),
47-
"metaspace": apply("-XX:MetaspaceSize=%s"),
48-
"permgen": apply("-XX:PermSize=%s"),
39+
"heap": apply("-Xmx%s"),
40+
"metaspace": apply("-XX:MaxMetaspaceSize=%s"),
41+
"permgen": apply("-XX:MaxPermSize=%s"),
42+
"stack": apply("-Xss%s"),
43+
"initial_heap": apply("-Xms%s"),
44+
"initial_metaspace": apply("-XX:MetaspaceSize=%s"),
45+
"initial_permgen": apply("-XX:PermSize=%s"),
4946
}
5047
)
5148

0 commit comments

Comments
 (0)