-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.go
65 lines (57 loc) · 1.52 KB
/
resource.go
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
63
64
65
package contract
import "github.com/ampchain/go-amp/pb"
const (
maxResourceLimit = 0xFFFFFFFF
)
// Limits describes the usage or limit of resources
type Limits struct {
Cpu int64
Memory int64
Disk int64
XFee int64
}
// TotalGas converts resource to gas
func (l *Limits) TotalGas() int64 {
// FIXME:
cpuGas := roundup(l.Cpu, 1000)
memGas := roundup(l.Memory, 1000000)
diskGas := roundup(l.Disk, 1)
feeGas := roundup(l.XFee, 1)
return cpuGas + memGas + diskGas + feeGas
}
// MaxLimits describes the maximum limit of resources
var MaxLimits = Limits{
Cpu: maxResourceLimit,
Memory: maxResourceLimit,
Disk: maxResourceLimit,
XFee: maxResourceLimit,
}
// FromPbLimits converts []*pb.ResourceLimit to Limits
func FromPbLimits(rlimits []*pb.ResourceLimit) Limits {
limits := Limits{}
for _, l := range rlimits {
switch l.GetType() {
case pb.ResourceType_CPU:
limits.Cpu = l.GetLimit()
case pb.ResourceType_MEMORY:
limits.Memory = l.GetLimit()
case pb.ResourceType_DISK:
limits.Disk = l.GetLimit()
case pb.ResourceType_XFEE:
limits.XFee = l.GetLimit()
}
}
return limits
}
// FromPbLimits converts Limits to []*pb.ResourceLimit
func ToPbLimits(limits Limits) []*pb.ResourceLimit {
return []*pb.ResourceLimit{
{Type: pb.ResourceType_CPU, Limit: limits.Cpu},
{Type: pb.ResourceType_MEMORY, Limit: limits.Memory},
{Type: pb.ResourceType_DISK, Limit: limits.Disk},
{Type: pb.ResourceType_XFEE, Limit: limits.XFee},
}
}
func roundup(n, scale int64) int64 {
return (n + scale - 1) / scale
}