Build fixes to support legacy builds.

This commit is contained in:
Russell Jones 2019-05-03 14:14:12 -07:00 committed by Russell Jones
parent a795aec624
commit e6e4699163
3 changed files with 60 additions and 16 deletions

View file

@ -107,7 +107,7 @@ endif
clean:
@echo "---> Cleaning up OSS build artifacts."
rm -rf $(BUILDDIR)
go clean -cache
-go clean -cache
rm -rf $(GOPKGDIR)
rm -rf teleport
rm -rf *.gz

View file

@ -30,7 +30,6 @@ import (
"sort"
"strings"
"sync"
"syscall"
"time"
"github.com/gravitational/teleport"
@ -998,7 +997,7 @@ func (l *AuditLog) periodicSpaceMonitor() {
case <-ticker.C:
// Find out what percentage of disk space is used. If the syscall fails,
// emit that to prometheus as well.
usedPercent, err := percentUsed(l.DataDir)
usedPercent, err := utils.PercentUsed(l.DataDir)
if err != nil {
auditFailedDisk.Inc()
log.Warnf("Disk space monitoring failed: %v.", err)
@ -1017,16 +1016,3 @@ func (l *AuditLog) periodicSpaceMonitor() {
}
}
}
// percentUsed returns percentage of disk space used. The percentage of disk
// space used is calculated from (total blocks - free blocks)/total blocks.
// The value is rounded to the nearest whole integer.
func percentUsed(path string) (float64, error) {
var stat syscall.Statfs_t
err := syscall.Statfs(path, &stat)
if err != nil {
return 0, trace.Wrap(err)
}
ratio := float64(stat.Blocks-stat.Bfree) / float64(stat.Blocks)
return math.Round(ratio * 100), nil
}

58
lib/utils/round.go Normal file
View file

@ -0,0 +1,58 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utils
import (
"math"
)
const (
uvone = 0x3FF0000000000000
mask = 0x7FF
shift = 64 - 11 - 1
bias = 1023
signMask = 1 << 63
fracMask = 1<<shift - 1
)
// Round returns the nearest integer, rounding half away from zero.
//
// Special cases are:
// Round(±0) = ±0
// Round(±Inf) = ±Inf
// Round(NaN) = NaN
//
// Note: Copied from Go standard library to support Go 1.9.7 releases. This
// function was added in the standard library in Go 1.10.
func Round(x float64) float64 {
// Round is a faster implementation of:
//
// func Round(x float64) float64 {
// t := Trunc(x)
// if Abs(x-t) >= 0.5 {
// return t + Copysign(1, x)
// }
// return t
// }
bits := math.Float64bits(x)
e := uint(bits>>shift) & mask
if e < bias {
// Round abs(x) < 1 including denormals.
bits &= signMask // +-0
if e == bias-1 {
bits |= uvone // +-1
}
} else if e < bias+shift {
// Round any abs(x) >= 1 containing a fractional component [0,1).
//
// Numbers with larger exponents are returned unchanged since they
// must be either an integer, infinity, or NaN.
const half = 1 << (shift - 1)
e -= bias
bits += half >> e
bits &^= fracMask >> e
}
return math.Float64frombits(bits)
}