Skip to content

Commit

Permalink
Version 4
Browse files Browse the repository at this point in the history
This change makes massive, incompatible updates to the project resulting in a
bump to version 4.  The code has been reworked to be more modular and simpler.
It takes lessons from the current building of Cloud Native Buildpacks to
become more idiomatic go.  Finally, the input flags are now POSIX-style rather
than Go style.
  • Loading branch information
nebhale committed Dec 18, 2018
1 parent d631044 commit f48b126
Show file tree
Hide file tree
Showing 52 changed files with 1,749 additions and 2,871 deletions.
42 changes: 16 additions & 26 deletions .gitignore
@@ -1,27 +1,17 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Copyright 2015-2018 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Folders
_obj
_test
.idea

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

java-buildpack-memory-calculator*
.idea/
bin/
*.iml
165 changes: 0 additions & 165 deletions CONTRIBUTING.md

This file was deleted.

113 changes: 113 additions & 0 deletions calculator/calculator.go
@@ -0,0 +1,113 @@
/*
* Copyright 2015-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package calculator

import (
"fmt"

"github.com/cloudfoundry/java-buildpack-memory-calculator/flags"
"github.com/cloudfoundry/java-buildpack-memory-calculator/memory"
)

type Calculator struct {
HeadRoom *flags.HeadRoom
JvmOptions *flags.JVMOptions
LoadedClassCount *flags.LoadedClassCount
ThreadCount *flags.ThreadCount
TotalMemory *flags.TotalMemory
}

func (c Calculator) Calculate() ([]fmt.Stringer, error) {
var options []fmt.Stringer

j := c.JvmOptions
if j == nil {
j = &flags.JVMOptions{}
}

headRoom := c.headRoom()

directMemory := j.MaxDirectMemory
if directMemory == nil {
d := memory.DefaultMaxDirectMemory
directMemory = &d
options = append(options, *directMemory)
}

metaspace := j.MaxMetaspace
if metaspace == nil {
m := c.metaspace()
metaspace = &m
options = append(options, *metaspace)
}

reservedCodeCache := j.ReservedCodeCache
if reservedCodeCache == nil {
r := memory.DefaultReservedCodeCache
reservedCodeCache = &r
options = append(options, *reservedCodeCache)
}

stack := j.Stack
if stack == nil {
s := memory.DefaultStack
stack = &s
options = append(options, *stack)
}

overhead := c.overhead(headRoom, directMemory, metaspace, reservedCodeCache, stack)
available := memory.Size(*c.TotalMemory)

if overhead > available {
return nil, fmt.Errorf("allocated memory is greater than %s available for allocation: %s, %s, %s, %s x %d threads",
available, directMemory, metaspace, reservedCodeCache, stack, *c.ThreadCount)
}

heap := j.MaxHeap
if heap == nil {
h := c.heap(overhead)
heap = &h
options = append(options, *heap)
}

if overhead+memory.Size(*heap) > available {
return nil, fmt.Errorf("allocated memory is greater than %s available for allocation: %s, %s, %s, %s, %s x %d threads",
available, directMemory, heap, metaspace, reservedCodeCache, stack, *c.ThreadCount)
}

return options, nil
}

func (c Calculator) headRoom() memory.Size {
return memory.Size(float64(*c.TotalMemory) * (float64(*c.HeadRoom) / 100))
}

func (c Calculator) heap(overhead memory.Size) memory.MaxHeap {
return memory.MaxHeap(memory.Size(*c.TotalMemory) - overhead)
}

func (c Calculator) metaspace() memory.MaxMetaspace {
return memory.MaxMetaspace((*c.LoadedClassCount * 5800) + 14000000)
}

func (c Calculator) overhead(headRoom memory.Size, directMemory *memory.MaxDirectMemory, metaspace *memory.MaxMetaspace, reservedCodeCache *memory.ReservedCodeCache, stack *memory.Stack) memory.Size {
return headRoom +
memory.Size(*directMemory) +
memory.Size(*metaspace) +
memory.Size(*reservedCodeCache) +
memory.Size(int64(*stack)*int64(*c.ThreadCount))
}

0 comments on commit f48b126

Please sign in to comment.