-
Notifications
You must be signed in to change notification settings - Fork 152
/
allocator.go
98 lines (86 loc) · 2.91 KB
/
allocator.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package memory
import (
"fmt"
"sync/atomic"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
)
// Allocator tracks the amount of memory being consumed by a query.
type Allocator struct {
// Limit is the limit on the amount of memory that this allocator
// can assign. If this is null, there is no limit.
Limit *int64
bytesAllocated int64
maxAllocated int64
}
// Allocate will ensure that the requested memory is available and
// record that it is in use.
func (a *Allocator) Allocate(size int) error {
if size < 0 {
panic(errors.New(codes.Internal, "cannot allocate negative memory"))
} else if size == 0 {
return nil
}
return a.count(size)
}
// Allocated returns the amount of currently allocated memory.
func (a *Allocator) Allocated() int64 {
return atomic.LoadInt64(&a.bytesAllocated)
}
// MaxAllocated reports the maximum amount of allocated memory at any point in the query.
func (a *Allocator) MaxAllocated() int64 {
return atomic.LoadInt64(&a.maxAllocated)
}
// Free will reduce the amount of memory used by this Allocator.
// In general, memory should be freed using the Reference returned
// by Allocate. Not all code is capable of using this though so this
// method provides a low-level way of releasing the memory without
// using a Reference.
func (a *Allocator) Free(size int) {
if size < 0 {
panic(errors.New(codes.Internal, "cannot free negative memory"))
}
atomic.AddInt64(&a.bytesAllocated, int64(-size))
}
func (a *Allocator) count(size int) error {
var c int64
if a.Limit != nil {
// We need to load the current bytes allocated, add to it, and
// compare if it is greater than the limit. If it is not, we need
// to modify the bytes allocated.
for {
allocated := atomic.LoadInt64(&a.bytesAllocated)
if want := allocated + int64(size); want > *a.Limit {
return errors.Wrap(LimitExceededError{
Limit: *a.Limit,
Allocated: allocated,
Wanted: want - allocated,
}, codes.ResourceExhausted)
} else if atomic.CompareAndSwapInt64(&a.bytesAllocated, allocated, want) {
c = want
break
}
// We did not succeed at swapping the bytes allocated so try again.
}
} else {
// Otherwise, add the size directly to the bytes allocated and
// compare and swap to modify the max allocated.
c = atomic.AddInt64(&a.bytesAllocated, int64(size))
}
// Modify the max allocated if the amount we just allocated is greater.
for max := atomic.LoadInt64(&a.maxAllocated); c > max; max = atomic.LoadInt64(&a.maxAllocated) {
if atomic.CompareAndSwapInt64(&a.maxAllocated, max, c) {
break
}
}
return nil
}
// LimitExceededError is an error when the allocation limit is exceeded.
type LimitExceededError struct {
Limit int64
Allocated int64
Wanted int64
}
func (a LimitExceededError) Error() string {
return fmt.Sprintf("memory allocation limit reached: limit %d bytes, allocated: %d, wanted: %d", a.Limit, a.Allocated, a.Wanted)
}