-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_mem_usage.go
153 lines (131 loc) · 4.98 KB
/
session_mem_usage.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2016 The Cockroach 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 sql
import (
"math"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/mon"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
)
// OpenAccount interfaces between Session and mon.MemoryMonitor.
func (s *Session) OpenAccount() WrappableMemoryAccount {
res := WrappableMemoryAccount{}
s.mon.OpenAccount(&res.acc)
return res
}
// OpenAccount interfaces between TxnState and mon.MemoryMonitor.
func (ts *txnState) OpenAccount() WrappableMemoryAccount {
res := WrappableMemoryAccount{}
ts.mon.OpenAccount(&res.acc)
return res
}
// WrappableMemoryAccount encapsulates a MemoryAccount to
// give it the Wsession()/Wtxn() method below.
type WrappableMemoryAccount struct {
acc mon.BytesAccount
}
// Wsession captures the current session monitor pointer so it can be provided
// transparently to the other Account APIs below.
func (w *WrappableMemoryAccount) Wsession(s *Session) WrappedMemoryAccount {
return WrappedMemoryAccount{
acc: &w.acc,
mon: &s.sessionMon,
}
}
// Wtxn captures the current txn-specific monitor pointer so it can be provided
// transparently to the other Account APIs below.
func (w *WrappableMemoryAccount) Wtxn(s *Session) WrappedMemoryAccount {
return WrappedMemoryAccount{
acc: &w.acc,
mon: &s.TxnState.mon,
}
}
// WrappedMemoryAccount is the transient structure that carries
// the extra argument to the MemoryAccount APIs.
type WrappedMemoryAccount struct {
acc *mon.BytesAccount
mon *mon.BytesMonitor
}
// OpenAndInit interfaces between Session and mon.MemoryMonitor.
func (w WrappedMemoryAccount) OpenAndInit(ctx context.Context, initialAllocation int64) error {
return w.mon.OpenAndInitAccount(ctx, w.acc, initialAllocation)
}
// Grow interfaces between Session and mon.MemoryMonitor.
func (w WrappedMemoryAccount) Grow(ctx context.Context, extraSize int64) error {
return w.mon.GrowAccount(ctx, w.acc, extraSize)
}
// Close interfaces between Session and mon.MemoryMonitor.
func (w WrappedMemoryAccount) Close(ctx context.Context) {
w.mon.CloseAccount(ctx, w.acc)
}
// Clear interfaces between Session and mon.MemoryMonitor.
func (w WrappedMemoryAccount) Clear(ctx context.Context) {
w.mon.ClearAccount(ctx, w.acc)
}
// ResizeItem interfaces between Session and mon.MemoryMonitor.
func (w WrappedMemoryAccount) ResizeItem(ctx context.Context, oldSize, newSize int64) error {
return w.mon.ResizeItem(ctx, w.acc, oldSize, newSize)
}
// noteworthyMemoryUsageBytes is the minimum size tracked by a
// transaction or session monitor before the monitor starts explicitly
// logging overall usage growth in the log.
var noteworthyMemoryUsageBytes = envutil.EnvOrDefaultInt64("COCKROACH_NOTEWORTHY_SESSION_MEMORY_USAGE", 1024*1024)
// StartMonitor interfaces between Session and mon.MemoryMonitor
func (s *Session) StartMonitor(pool *mon.BytesMonitor, reserved mon.BoundAccount) {
// Note: we pass `reserved` to s.mon where it causes `s.mon` to act
// as a buffer. This is not done for sessionMon nor TxnState.mon:
// these monitors don't start with any buffer, so they'll need to
// ask their "parent" for memory as soon as the first
// allocation. This is acceptable because the session is single
// threaded, and the point of buffering is just to avoid contention.
s.mon = mon.MakeMonitor("root",
mon.MemoryResource,
s.memMetrics.CurBytesCount,
s.memMetrics.MaxBytesHist,
-1, math.MaxInt64)
s.mon.Start(s.context, pool, reserved)
s.deriveAndStartMonitors()
}
// StartUnlimitedMonitor interfaces between Session and mon.MemoryMonitor
func (s *Session) StartUnlimitedMonitor() {
s.mon = mon.MakeUnlimitedMonitor(s.context,
"root",
mon.MemoryResource,
s.memMetrics.CurBytesCount,
s.memMetrics.MaxBytesHist,
math.MaxInt64,
)
s.deriveAndStartMonitors()
}
func (s *Session) deriveAndStartMonitors() {
s.sessionMon = mon.MakeMonitor("session",
mon.MemoryResource,
s.memMetrics.SessionCurBytesCount,
s.memMetrics.SessionMaxBytesHist,
-1, noteworthyMemoryUsageBytes)
s.sessionMon.Start(s.context, &s.mon, mon.BoundAccount{})
// We merely prepare the txn monitor here. It is fully started in
// resetForNewSQLTxn().
s.TxnState.mon = mon.MakeMonitor("txn",
mon.MemoryResource,
s.memMetrics.TxnCurBytesCount,
s.memMetrics.TxnMaxBytesHist,
-1, noteworthyMemoryUsageBytes)
}
func (s *Session) makeBoundAccount() mon.BoundAccount {
return s.sessionMon.MakeBoundAccount()
}
func (ts *txnState) makeBoundAccount() mon.BoundAccount {
return ts.mon.MakeBoundAccount()
}