memory

package
v1.1.0-beta.0...-c4d8025 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2025 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefPanicPriority = iota
	DefLogPriority
	DefSpillPriority
	// DefCursorFetchSpillPriority is higher than normal disk spill, because it can release much more memory in the future.
	// And the performance impaction of it is less than other disk-spill action, because it's write-only in execution stage.
	DefCursorFetchSpillPriority
	DefRateLimitPriority
)

Default OOM Action priority.

View Source
const (
	// ArbitratorSoftLimitModDisableName is the name of the soft limit mode default
	ArbitratorSoftLimitModDisableName = "0"
	// ArbitratorSoftLimitModeAutoName is the name of the soft limit mode auto
	ArbitratorSoftLimitModeAutoName = "auto"
	// ArbitratorModeStandardName is the name of the standard mode
	ArbitratorModeStandardName = "standard"
	// ArbitratorModePriorityName is the name of the priority mode
	ArbitratorModePriorityName = "priority"
	// ArbitratorModeDisableName is the name of the disable mode
	ArbitratorModeDisableName = "disable"
	// DefMaxLimit is the default maximum limit of mem quota
	DefMaxLimit int64 = 5e15
)
View Source
const (
	// LabelForSQLText represents the label of the SQL Text
	LabelForSQLText int = -1
	// LabelForIndexWorker represents the label of the index worker
	LabelForIndexWorker int = -2
	// LabelForInnerList represents the label of the inner list
	LabelForInnerList int = -3
	// LabelForInnerTable represents the label of the inner table
	LabelForInnerTable int = -4
	// LabelForOuterTable represents the label of the outer table
	LabelForOuterTable int = -5
	// LabelForCoprocessor represents the label of the coprocessor
	LabelForCoprocessor int = -6
	// LabelForChunkList represents the label of the chunk list
	LabelForChunkList int = -7
	// LabelForGlobalSimpleLRUCache represents the label of the Global SimpleLRUCache
	LabelForGlobalSimpleLRUCache int = -8
	// LabelForChunkDataInDiskByRows represents the label of the chunk list in disk
	LabelForChunkDataInDiskByRows int = -9
	// LabelForRowContainer represents the label of the row container
	LabelForRowContainer int = -10
	// LabelForGlobalStorage represents the label of the Global Storage
	LabelForGlobalStorage int = -11
	// LabelForGlobalMemory represents the label of the Global Memory
	LabelForGlobalMemory int = -12
	// LabelForBuildSideResult represents the label of the BuildSideResult
	LabelForBuildSideResult int = -13
	// LabelForRowChunks represents the label of the row chunks
	LabelForRowChunks int = -14
	// LabelForStatsCache represents the label of the stats cache
	LabelForStatsCache int = -15
	// LabelForOuterList represents the label of the outer list
	LabelForOuterList int = -16
	// LabelForApplyCache represents the label of the apply cache
	LabelForApplyCache int = -17
	// LabelForSimpleTask represents the label of the simple task
	LabelForSimpleTask int = -18
	// LabelForCTEStorage represents the label of CTE storage
	LabelForCTEStorage int = -19
	// LabelForIndexJoinInnerWorker represents the label of IndexJoin InnerWorker
	LabelForIndexJoinInnerWorker int = -20
	// LabelForIndexJoinOuterWorker represents the label of IndexJoin OuterWorker
	LabelForIndexJoinOuterWorker int = -21
	// LabelForBindCache represents the label of the bind cache
	LabelForBindCache int = -22
	// LabelForNonTransactionalDML represents the label of the non-transactional DML
	LabelForNonTransactionalDML = -23
	// LabelForAnalyzeMemory represents the label of the memory of each analyze job
	LabelForAnalyzeMemory int = -24
	// LabelForGlobalAnalyzeMemory represents the label of the global memory of all analyze jobs
	LabelForGlobalAnalyzeMemory int = -25
	// LabelForPreparedPlanCache represents the label of the prepared plan cache memory usage
	LabelForPreparedPlanCache int = -26
	// LabelForSession represents the label of a session.
	LabelForSession int = -27
	// LabelForMemDB represents the label of the MemDB
	LabelForMemDB int = -28
	// LabelForCursorFetch represents the label of the execution of cursor fetch
	LabelForCursorFetch int = -29
	// LabelForChunkDataInDiskByChunks represents the label of the chunk list in disk
	LabelForChunkDataInDiskByChunks int = -30
	// LabelForSortPartition represents the label of the sort partition
	LabelForSortPartition int = -31
	// LabelForHashTableInHashJoinV2 represents the label of the hash join v2's hash table
	LabelForHashTableInHashJoinV2 int = -32
)
View Source
const DefMaxUnusedBlocks int64 = 10

DefMaxUnusedBlocks indicates the default maximum unused blocks*alloc-align-size of the resource pool

View Source
const DefMemQuotaQuery = 1073741824 // 1GB

DefMemQuotaQuery is default memory quota for query.

View Source
const DefPoolAllocAlignSize int64 = 10 * 1024

DefPoolAllocAlignSize indicates the default allocation alignment size

View Source
const ReadMemInterval = 300 * time.Millisecond

ReadMemInterval controls the interval to read memory stats.

View Source
const TrackMemWhenExceeds = 104857600 // 100MB

TrackMemWhenExceeds is the threshold when memory usage needs to be tracked.

Variables

View Source
var (
	ServerMemoryLimitOriginText  = atomicutil.NewString("0")
	ServerMemoryLimit            = atomicutil.NewUint64(0)
	ServerMemoryLimitSessMinSize = atomicutil.NewUint64(128 << 20)

	QueryForceDisk       = atomicutil.NewInt64(0)
	TriggerMemoryLimitGC = atomicutil.NewBool(false)
	MemoryLimitGCLast    = atomicutil.NewTime(time.Time{})
	MemoryLimitGCTotal   = atomicutil.NewInt64(0)
)

Process global variables for memory limit.

View Source
var EnableGCAwareMemoryTrack = atomicutil.NewBool(false)

EnableGCAwareMemoryTrack is used to turn on/off the GC-aware memory track

View Source
var MemTotal func() (uint64, error)

MemTotal returns the total amount of RAM on this system

View Source
var MemUsageTop1Tracker atomic.Pointer[Tracker]

MemUsageTop1Tracker record the use memory top1 session's tracker for kill.

View Source
var MemUsed func() (uint64, error)

MemUsed returns the total used amount of RAM on this system

View Source
var MetricsTypes = map[int][]string{
	LabelForGlobalAnalyzeMemory: {"analyze", "inuse", "released"},
}

MetricsTypes is used to get label for metrics string[0] is LblModule, string[1] is heap-in-use type, string[2] is released type

Functions

func AjustGlobalMemArbitratorLimit

func AjustGlobalMemArbitratorLimit()

AjustGlobalMemArbitratorLimit adjusts the quota limit of the global memory arbitrator through the server memory limit.

func BytesToString

func BytesToString(numBytes int64) string

BytesToString converts the memory consumption to a readable string.

func CleanupGlobalMemArbitratorForTest

func CleanupGlobalMemArbitratorForTest()

CleanupGlobalMemArbitratorForTest stops the async runner of the global memory arbitrator (suggest to use in tests only).

func ForceReadMemStats

func ForceReadMemStats() *runtime.MemStats

ForceReadMemStats is to force read memory stats.

func FormatBytes

func FormatBytes(numBytes int64) string

FormatBytes uses to format bytes, this function will prune precision before format bytes.

func GetGlobalMemArbitratorSoftLimitText

func GetGlobalMemArbitratorSoftLimitText() string

GetGlobalMemArbitratorSoftLimitText returns the text of the global memory arbitrator soft limit.

func GetGlobalMemArbitratorWorkModeText

func GetGlobalMemArbitratorWorkModeText() string

GetGlobalMemArbitratorWorkModeText returns the text of the global memory arbitrator work mode.

func GetMemTotalIgnoreErr

func GetMemTotalIgnoreErr() uint64

GetMemTotalIgnoreErr returns the total amount of RAM on this system/container. If error occurs, return 0.

func HandleGlobalMemArbitratorRuntime

func HandleGlobalMemArbitratorRuntime(s *runtime.MemStats)

HandleGlobalMemArbitratorRuntime is used to handle runtime memory stats.

func HashEvenNum

func HashEvenNum(key uint64) uint64

HashEvenNum hashes a uint64 even number to a uint64 value

func HashStr

func HashStr(key string) uint64

HashStr hashes a string to a uint64 value

func InitMemoryHook

func InitMemoryHook() error

InitMemoryHook initializes the memory hook. It is to solve the problem that tidb cannot read cgroup in the systemd. so if we are not in the container, we compare the cgroup memory limit and the physical memory, the cgroup memory limit is smaller, we use the cgroup memory hook.

func InitTracker

func InitTracker(t *Tracker, label int, bytesLimit int64, action ActionOnExceed)

InitTracker initializes a memory tracker.

  1. "label" is the label used in the usage string.
  2. "bytesLimit <= 0" means no limit.

For the common tracker, isGlobal is default as false

func InstanceMemUsed

func InstanceMemUsed() (uint64, error)

InstanceMemUsed returns the memory usage of this TiDB server

func MemTotalCGroup

func MemTotalCGroup() (uint64, error)

MemTotalCGroup returns the total amount of RAM on this system in container environment.

func MemTotalNormal

func MemTotalNormal() (uint64, error)

MemTotalNormal returns the total amount of RAM on this system in non-container environment.

func MemUsedCGroup

func MemUsedCGroup() (uint64, error)

MemUsedCGroup returns the total used amount of RAM on this system in container environment.

func MemUsedNormal

func MemUsedNormal() (uint64, error)

MemUsedNormal returns the total used amount of RAM on this system in non-container environment.

func NewActionWithPriority

func NewActionWithPriority(action ActionOnExceed, priority int64) *actionWithPriority

NewActionWithPriority wraps the action with a new priority

func ReadMemStats

func ReadMemStats() (memStats *runtime.MemStats)

ReadMemStats read the mem stats from runtime.ReadMemStats

func RegisterCallbackForGlobalMemArbitrator

func RegisterCallbackForGlobalMemArbitrator(f func())

RegisterCallbackForGlobalMemArbitrator registers a callback to be called after the global memory arbitrator is enabled.

func RemovePoolFromGlobalMemArbitrator

func RemovePoolFromGlobalMemArbitrator(uid uint64) bool

RemovePoolFromGlobalMemArbitrator removes a pool from the global memory arbitrator by its UID.

func SetGlobalMemArbitratorSoftLimit

func SetGlobalMemArbitratorSoftLimit(str string)

SetGlobalMemArbitratorSoftLimit sets the soft limit of the global memory arbitrator.

func SetGlobalMemArbitratorWorkMode

func SetGlobalMemArbitratorWorkMode(str string) bool

SetGlobalMemArbitratorWorkMode sets the work mode of the global memory arbitrator.

func SetupGlobalMemArbitratorForTest

func SetupGlobalMemArbitratorForTest(baseDir string)

SetupGlobalMemArbitratorForTest sets up the global memory arbitrator for tests.

func UsingGlobalMemArbitration

func UsingGlobalMemArbitration() bool

UsingGlobalMemArbitration returns true if the global memory arbitration policy is used. It needs to return true when the work mode of the global memory arbitrator is changeing from disable to other modes.

Types

type ActionOnExceed

type ActionOnExceed interface {
	// Action will be called when memory usage exceeds memory quota by the
	// corresponding Tracker.
	Action(t *Tracker)
	// SetFallback sets a fallback action which will be triggered if itself has
	// already been triggered.
	SetFallback(a ActionOnExceed)
	// GetFallback get the fallback action of the Action.
	GetFallback() ActionOnExceed
	// GetPriority get the priority of the Action.
	GetPriority() int64
	// SetFinished sets the finished state of the Action.
	SetFinished()
	// IsFinished returns the finished state of the Action.
	IsFinished() bool
}

ActionOnExceed is the action taken when memory usage exceeds memory quota. NOTE: All the implementors should be thread-safe.

type ArbitrateHelper

type ArbitrateHelper interface {
	Stop(ArbitratorStopReason) bool // kill by arbitrator only when meeting oom risk; cancel by arbitrator;
	HeapInuse() int64               // track heap usage
	Finish()
}

ArbitrateHelper is an interface for the arbitrate helper

type ArbitrateResult

type ArbitrateResult int32

ArbitrateResult represents the results of the arbitration process

const (
	// ArbitrateOk indicates that the arbitration is successful.
	ArbitrateOk ArbitrateResult = iota
	// ArbitrateFail indicates that the arbitration is failed
	ArbitrateFail
)

type ArbitrationContext

type ArbitrationContext struct {
	PrevMaxMem int64
	// contains filtered or unexported fields
}

ArbitrationContext represents the context & properties of the root pool

func NewArbitrationContext

func NewArbitrationContext(
	cancelCh <-chan struct{},
	prevMaxMem, memQuotaLimit int64,
	arbitrateHelper ArbitrateHelper,
	memPriority ArbitrationPriority,
	waitAverse bool,
	preferPrivilege bool,
) *ArbitrationContext

NewArbitrationContext creates a new arbitration context

type ArbitrationPriority

type ArbitrationPriority int32

ArbitrationPriority represents the priority of the task: Low, Medium, High

const (
	// ArbitrationPriorityLow indicates the low priority
	ArbitrationPriorityLow ArbitrationPriority = iota
	// ArbitrationPriorityMedium indicates the medium priority
	ArbitrationPriorityMedium
	// ArbitrationPriorityHigh indicates the high priority
	ArbitrationPriorityHigh

	// ArbitrationWaitAverse indicates the wait-averse property
	ArbitrationWaitAverse = maxArbitrationPriority
)

func (ArbitrationPriority) String

func (p ArbitrationPriority) String() string

String returns the string representation of the ArbitrationPriority

type ArbitratorStopReason

type ArbitratorStopReason int

ArbitratorStopReason represents the reason why the arbitrate helper will be stopped

const (
	ArbitratorOOMRiskKill ArbitratorStopReason = iota
	ArbitratorWaitAverseCancel
	ArbitratorStandardCancel
	ArbitratorPriorityCancel
)

ArbitrateHelperReason values

func (ArbitratorStopReason) String

func (r ArbitratorStopReason) String() (desc string)

String returns the string representation of the ArbitratorStopReason

type ArbitratorWorkMode

type ArbitratorWorkMode int32

ArbitratorWorkMode represents the work mode of the arbitrator: Standard, Priority, Disable

const (
	// ArbitratorModeStandard indicates the standard mode
	ArbitratorModeStandard ArbitratorWorkMode = iota
	// ArbitratorModePriority indicates the priority mode
	ArbitratorModePriority
	// ArbitratorModeDisable indicates the mem-arbitrator is disabled
	ArbitratorModeDisable
)

func (ArbitratorWorkMode) String

func (m ArbitratorWorkMode) String() string

String returns the string representation of the ArbitratorWorkMode

type BaseOOMAction

type BaseOOMAction struct {
	// contains filtered or unexported fields
}

BaseOOMAction manages the fallback action for all Action.

func (*BaseOOMAction) GetFallback

func (b *BaseOOMAction) GetFallback() ActionOnExceed

GetFallback get the fallback action and remove finished fallback.

func (*BaseOOMAction) IsFinished

func (b *BaseOOMAction) IsFinished() bool

IsFinished returns the finished state of the Action.

func (*BaseOOMAction) SetFallback

func (b *BaseOOMAction) SetFallback(a ActionOnExceed)

SetFallback sets a fallback action which will be triggered if itself has already been triggered.

func (*BaseOOMAction) SetFinished

func (b *BaseOOMAction) SetFinished()

SetFinished sets the finished state of the Action.

func (*BaseOOMAction) TriggerFallBackAction

func (b *BaseOOMAction) TriggerFallBackAction(tracker *Tracker)

TriggerFallBackAction triggers the fallback action of the current action

type Budget

type Budget struct {
	// contains filtered or unexported fields
}

Budget represents the budget of a resource pool

func (*Budget) Capacity

func (b *Budget) Capacity() int64

Capacity returns the capacity of the budget

func (*Budget) Clear

func (b *Budget) Clear()

Clear releases the budget and resets

func (*Budget) Empty

func (b *Budget) Empty()

Empty releases the used budget

func (*Budget) Grow

func (b *Budget) Grow(request int64) error

Grow the budget by the given size

func (*Budget) Pool

func (b *Budget) Pool() *ResourcePool

Pool returns the resource pool of the budget

func (*Budget) Reserve

func (b *Budget) Reserve(request int64) error

Reserve reserves the budget through the allocate aligned given size; update the explicit reserved size;

func (*Budget) ResizeTo

func (b *Budget) ResizeTo(newSz int64) error

ResizeTo resizes the budget to the new size

func (*Budget) Shrink

func (b *Budget) Shrink(delta int64)

Shrink the budget and reduce the given size

func (*Budget) Used

func (b *Budget) Used() int64

Used returns the used bytes of the budget

type ConcurrentBudget

type ConcurrentBudget struct {
	Pool            *ResourcePool
	Capacity        int64
	LastUsedTimeSec int64
	sync.Mutex

	Used atomic.Int64
	// contains filtered or unexported fields
}

ConcurrentBudget represents a wrapped budget of the resource pool for concurrent usage

func (*ConcurrentBudget) Clear

func (b *ConcurrentBudget) Clear() int64

Clear clears the concurrent budget and returns the capacity

func (*ConcurrentBudget) ConsumeQuota

func (b *ConcurrentBudget) ConsumeQuota(utimeSec int64, req int64) error

ConsumeQuota consumes quota from the concurrent budget req > 0: alloc quota; try to pull from upstream; req <= 0: release quota

func (*ConcurrentBudget) PullFromUpstream

func (b *ConcurrentBudget) PullFromUpstream() (err error)

PullFromUpstream tries to pull from the upstream pool when facing `out of capacity` It requires the action of the pool to be non-blocking

func (*ConcurrentBudget) Reserve

func (b *ConcurrentBudget) Reserve(newCap int64) (err error)

Reserve reserves a given capacity for the concurrent budget

type DebugFields

type DebugFields struct {
	// contains filtered or unexported fields
}

DebugFields is used to store debug fields for logging

type LastRisk

type LastRisk struct {
	HeapAlloc  int64 `json:"heap"`
	QuotaAlloc int64 `json:"quota"`
}

LastRisk represents the last risk state of memory

type LogOnExceed

type LogOnExceed struct {
	BaseOOMAction
	ConnID uint64
	// contains filtered or unexported fields
}

LogOnExceed logs a warning only once when memory usage exceeds memory quota.

func (*LogOnExceed) Action

func (a *LogOnExceed) Action(t *Tracker)

Action logs a warning only once when memory usage exceeds memory quota.

func (*LogOnExceed) GetPriority

func (*LogOnExceed) GetPriority() int64

GetPriority get the priority of the Action

func (*LogOnExceed) SetLogHook

func (a *LogOnExceed) SetLogHook(hook func(uint64))

SetLogHook sets a hook for LogOnExceed.

type MemArbitrator

type MemArbitrator struct {
	UnixTimeSec int64 // approximate unix time in seconds
	// contains filtered or unexported fields
}

MemArbitrator represents the main structure aka `mem-arbitrator`

func GlobalMemArbitrator

func GlobalMemArbitrator() *MemArbitrator

GlobalMemArbitrator returns the global memory arbitrator if it is enabled.

func NewMemArbitrator

func NewMemArbitrator(limit int64, shardNum uint64, maxQuotaShardNum int, minQuotaForReclaim int64, recorder RecordMemState) *MemArbitrator

NewMemArbitrator creates a new mem-arbitrator heap instance

func (*MemArbitrator) Allocated

func (m *MemArbitrator) Allocated() int64

Allocated returns the allocated mem quota of the mem-arbitrator

func (*MemArbitrator) AtMemRisk

func (m *MemArbitrator) AtMemRisk() bool

AtMemRisk checks if the memory is under risk

func (*MemArbitrator) AtOOMRisk

func (m *MemArbitrator) AtOOMRisk() bool

AtOOMRisk checks if the memory is under risk

func (*MemArbitrator) AutoRun

func (m *MemArbitrator) AutoRun(
	actions MemArbitratorActions,
	awaitFreePoolAllocAlignSize, awaitFreePoolShardNum int64,
	taskTickDur time.Duration,
) bool

AutoRun starts the work groutine of the mem-arbitrator asynchronously

func (*MemArbitrator) ConsumeQuotaFromAwaitFreePool

func (m *MemArbitrator) ConsumeQuotaFromAwaitFreePool(uid uint64, req int64) bool

ConsumeQuotaFromAwaitFreePool consumes quota from the awaitfree-pool by the given uid

func (*MemArbitrator) EmplaceRootPool

func (m *MemArbitrator) EmplaceRootPool(uid uint64) (rootPool, error)

EmplaceRootPool emplaces a new root pool with the given uid (uid < 0 means the internal pool)

func (*MemArbitrator) ExecMetrics

func (m *MemArbitrator) ExecMetrics() execMetricsCounter

ExecMetrics returns the reference of the execution metrics

func (*MemArbitrator) FindRootPool

func (m *MemArbitrator) FindRootPool(uid uint64) rootPool

FindRootPool finds the root pool by ID

func (*MemArbitrator) GetAwaitFreeBudgets

func (m *MemArbitrator) GetAwaitFreeBudgets(uid uint64) *TrackedConcurrentBudget

GetAwaitFreeBudgets returns the concurrent budget shard by the given uid

func (*MemArbitrator) GetDigestProfileCache

func (m *MemArbitrator) GetDigestProfileCache(digestID uint64, utimeSec int64) (int64, bool)

GetDigestProfileCache returns the digest profile cache for a given digest-id and utime

func (*MemArbitrator) HandleRuntimeStats

func (m *MemArbitrator) HandleRuntimeStats(s RuntimeMemStats)

HandleRuntimeStats handles the runtime memory statistics

func (*MemArbitrator) Limit

func (m *MemArbitrator) Limit() uint64

Limit returns the mem quota limit of the mem-arbitrator

func (*MemArbitrator) OutOfControl

func (m *MemArbitrator) OutOfControl() int64

OutOfControl returns the size of the out-of-control mem

func (*MemArbitrator) PoolAllocProfile

func (m *MemArbitrator) PoolAllocProfile() (res PoolAllocProfile)

PoolAllocProfile returns the profile of root pool allocation in the mem-arbitrator

func (*MemArbitrator) RemoveRootPoolByID

func (m *MemArbitrator) RemoveRootPoolByID(uid uint64) bool

RemoveRootPoolByID removes & terminates the root pool by ID

func (*MemArbitrator) ReportHeapInuseToAwaitFreePool

func (m *MemArbitrator) ReportHeapInuseToAwaitFreePool(uid uint64, req int64)

ReportHeapInuseToAwaitFreePool reports the heap inuse to the awaitfree-pool by the given uid

func (*MemArbitrator) ResetRootPoolByID

func (m *MemArbitrator) ResetRootPoolByID(uid uint64, maxMemConsumed int64, tune bool)

ResetRootPoolByID resets the root pool by ID and analyze the memory consumption info

func (*MemArbitrator) RootPoolNum

func (m *MemArbitrator) RootPoolNum() int64

RootPoolNum returns the number of root pools in the mem-arbitrator

func (*MemArbitrator) SetDigestProfileCacheLimit

func (m *MemArbitrator) SetDigestProfileCacheLimit(limit int64)

SetDigestProfileCacheLimit sets the limit of the digest profile cache

func (*MemArbitrator) SetLimit

func (m *MemArbitrator) SetLimit(x uint64) (changed bool)

SetLimit sets the limit of the mem-arbitrator and returns whether the limit has changed

func (*MemArbitrator) SetRuntimeMemStats

func (m *MemArbitrator) SetRuntimeMemStats(s RuntimeMemStats)

SetRuntimeMemStats sets the runtime memory statistics. It may be invoked by `refreshRuntimeMemStats` -> `actions.UpdateRuntimeMemStats`

func (*MemArbitrator) SetSoftLimit

func (m *MemArbitrator) SetSoftLimit(softLimit int64, sortLimitRatio float64, mode SoftLimitMode)

SetSoftLimit sets the soft limit of the mem-arbitrator

func (*MemArbitrator) SetWorkMode

func (m *MemArbitrator) SetWorkMode(newMode ArbitratorWorkMode) (oriMode ArbitratorWorkMode)

SetWorkMode sets the work mode of the mem-arbitrator

func (*MemArbitrator) SoftLimit

func (m *MemArbitrator) SoftLimit() uint64

SoftLimit returns the soft limit of the mem-arbitrator

func (*MemArbitrator) SuggestPoolInitCap

func (m *MemArbitrator) SuggestPoolInitCap() int64

SuggestPoolInitCap returns the suggested initial capacity for the pool

func (*MemArbitrator) TaskNum

func (m *MemArbitrator) TaskNum() int64

TaskNum returns the number of pending tasks in the mem-arbitrator

func (*MemArbitrator) TaskNumByPattern

func (m *MemArbitrator) TaskNumByPattern() (res NumByPattern)

TaskNumByPattern returns the number of tasks by pattern and there may be overlap

func (*MemArbitrator) UpdateDigestProfileCache

func (m *MemArbitrator) UpdateDigestProfileCache(digestID uint64, memConsumed int64, utimeSec int64)

UpdateDigestProfileCache updates the digest profile cache for a given digest-id

func (*MemArbitrator) WaitingAllocSize

func (m *MemArbitrator) WaitingAllocSize() int64

WaitingAllocSize returns the pending alloc mem quota of the mem-arbitrator

func (*MemArbitrator) WorkMode

func (m *MemArbitrator) WorkMode() ArbitratorWorkMode

WorkMode returns the current work mode of the mem-arbitrator

type MemArbitratorActions

type MemArbitratorActions struct {
	Info, Warn, Error func(format string, args ...zap.Field) // log actions

	UpdateRuntimeMemStats func() // update runtime memory statistics
	GC                    func() // garbage collection
}

MemArbitratorActions represents the actions of the mem-arbitrator

type NoteAction

type NoteAction struct {
	CB        func(NoteActionState)
	Threshold int64
}

NoteAction represents the action to be taken when the allocated size exceeds the threshold

type NoteActionState

type NoteActionState struct {
	Pool      *ResourcePool
	Allocated int64
}

NoteActionState wraps the arguments of a note action

type Notifer

type Notifer struct {
	C chan struct{}
	// contains filtered or unexported fields
}

Notifer works as the multiple producer & single consumer mode.

func NewNotifer

func NewNotifer() Notifer

NewNotifer creates a new Notifer instance.

func (*Notifer) Wait

func (n *Notifer) Wait()

Wait for signal synchronously (consumer)

func (*Notifer) Wake

func (n *Notifer) Wake()

Wake the consumer

func (*Notifer) WeakWake

func (n *Notifer) WeakWake()

WeakWake wakes the consumer if it is not awake (may loose signal under concurrent scenarios).

type NumByPattern

type NumByPattern [maxArbitrateMode]int64

NumByPattern represents the number of tasks by 4 pattern: priority(low, medium, high), wait-averse

type NumByPriority

type NumByPriority [maxArbitrationPriority]int64

NumByPriority represents the number of tasks by priority

type OutOfCapacityActionArgs

type OutOfCapacityActionArgs struct {
	Pool    *ResourcePool
	Request int64
}

OutOfCapacityActionArgs wraps the arguments for out of capacity action

type PanicOnExceed

type PanicOnExceed struct {
	Killer *sqlkiller.SQLKiller

	BaseOOMAction
	ConnID uint64
	// contains filtered or unexported fields
}

PanicOnExceed panics when memory usage exceeds memory quota.

func (*PanicOnExceed) Action

func (a *PanicOnExceed) Action(t *Tracker)

Action panics when memory usage exceeds memory quota.

func (*PanicOnExceed) GetPriority

func (*PanicOnExceed) GetPriority() int64

GetPriority get the priority of the Action

func (*PanicOnExceed) SetLogHook

func (a *PanicOnExceed) SetLogHook(hook func(uint64))

SetLogHook sets a hook for PanicOnExceed.

type PoolActions

type PoolActions struct {
	OutOfCapacityActionCB func(OutOfCapacityActionArgs) error // Called when the resource pool is out of capacity
	OutOfLimitActionCB    func(*ResourcePool) error           // Called when the resource pool is out of limit
	NoteAction            NoteAction
}

PoolActions represents the actions to be taken when the resource pool meets certain conditions

type PoolAllocProfile

type PoolAllocProfile struct {
	SmallPoolLimit   int64 // limit / 1000
	PoolAllocUnit    int64 // limit / 500
	MaxPoolAllocUnit int64 // limit / 100
}

PoolAllocProfile represents the profile of root pool allocation in the mem-arbitrator

type RecordMemState

type RecordMemState interface {
	Load() (*RuntimeMemStateV1, error)
	Store(*RuntimeMemStateV1) error
}

RecordMemState is an interface for recording runtime memory state

type ResourcePool

type ResourcePool struct {
	// contains filtered or unexported fields
}

ResourcePool manages a set of resource quota

func NewResourcePool

func NewResourcePool(
	uid uint64,
	name string,
	limit int64,
	allocAlignSize int64,
	maxUnusedBlocks int64,
	actions PoolActions,
) *ResourcePool

NewResourcePool creates a new resource pool

func NewResourcePoolDefault

func NewResourcePoolDefault(
	name string,
	allocAlignSize int64,
) *ResourcePool

NewResourcePoolDefault creates a new resource pool

func (*ResourcePool) AdjustBudget

func (p *ResourcePool) AdjustBudget()

AdjustBudget adjusts the budget of the resource pool

func (*ResourcePool) Allocated

func (p *ResourcePool) Allocated() (res int64)

Allocated returns the allocated bytes

func (*ResourcePool) ApproxCap

func (p *ResourcePool) ApproxCap() int64

ApproxCap returns the approximate capacity of the resource pool

func (*ResourcePool) Capacity

func (p *ResourcePool) Capacity() (res int64)

Capacity returns the capacity of the resource pool

func (*ResourcePool) CreateBudget

func (p *ResourcePool) CreateBudget() Budget

CreateBudget creates a new budget from the resource pool

func (*ResourcePool) ExplicitReserve

func (p *ResourcePool) ExplicitReserve(request int64) (err error)

ExplicitReserve reserves the budget explicitly

func (*ResourcePool) IsStopped

func (p *ResourcePool) IsStopped() (res bool)

IsStopped checks if the resource pool is stopped

func (*ResourcePool) Limit

func (p *ResourcePool) Limit() int64

Limit returns the limit of the resource pool

func (*ResourcePool) MaxAllocated

func (p *ResourcePool) MaxAllocated() (res int64)

MaxAllocated returns the maximum allocated bytes

func (*ResourcePool) Name

func (p *ResourcePool) Name() string

Name returns the name of the resource pool

func (*ResourcePool) NewResourcePoolInheritWithLimit

func (p *ResourcePool) NewResourcePoolInheritWithLimit(
	name string, limit int64,
) *ResourcePool

NewResourcePoolInheritWithLimit creates a new resource pool inheriting from the parent pool

func (*ResourcePool) SetAllocAlignSize

func (p *ResourcePool) SetAllocAlignSize(size int64) (ori int64)

SetAllocAlignSize sets the allocation alignment size and returns the original value of allocAlignSize

func (*ResourcePool) SetLimit

func (p *ResourcePool) SetLimit(newLimit int64)

SetLimit sets the limit of the resource pool

func (*ResourcePool) SetOutOfCapacityAction

func (p *ResourcePool) SetOutOfCapacityAction(f func(OutOfCapacityActionArgs) error)

SetOutOfCapacityAction sets the out of capacity action It is called when the resource pool is out of capacity

func (*ResourcePool) SetOutOfLimitAction

func (p *ResourcePool) SetOutOfLimitAction(f func(*ResourcePool) error)

SetOutOfLimitAction sets the out of limit action It is called when the resource pool is out of limit

func (*ResourcePool) Start

func (p *ResourcePool) Start(parentPool *ResourcePool, reserved int64)

Start starts the resource pool with a parent pool and reserved quota

func (*ResourcePool) StartNoReserved

func (p *ResourcePool) StartNoReserved(pool *ResourcePool)

StartNoReserved creates a new resource pool with no reserved quota

func (*ResourcePool) Stop

func (p *ResourcePool) Stop() (released int64)

Stop stops the resource pool and releases the budget & returns the quota released

func (*ResourcePool) Traverse

func (p *ResourcePool) Traverse(stateCb func(ResourcePoolState) error) error

Traverse the resource pool and calls the callback function

func (*ResourcePool) UID

func (p *ResourcePool) UID() uint64

UID returns the unique ID of the resource pool

type ResourcePoolState

type ResourcePoolState struct {
	Name     string
	Level    int
	ID       uint64
	ParentID uint64
	Used     int64
	Reserved int64
	Budget   int64
}

ResourcePoolState represents the state of a resource pool

type RuntimeMemStateV1

type RuntimeMemStateV1 struct {
	Version  int64    `json:"version"`
	LastRisk LastRisk `json:"last-risk"`
	// magnification ratio of heap-alloc/quota
	Magnif int64 `json:"magnif"`
	// medium quota usage of root pools
	PoolMediumCap int64 `json:"pool-medium-cap"`
}

RuntimeMemStateV1 represents the runtime memory state

type RuntimeMemStats

type RuntimeMemStats struct {
	HeapAlloc, HeapInuse, TotalFree, MemOffHeap, LastGC int64
}

RuntimeMemStats represents the runtime memory statistics

type SoftLimitMode

type SoftLimitMode int32

SoftLimitMode represents the mode of soft limit for the mem-arbitrator

const (
	// SoftLimitModeDisable indicates that soft-limit is same as the threshold of oom risk
	SoftLimitModeDisable SoftLimitMode = iota
	// SoftLimitModeSpecified indicates that the soft-limit is a specified num of bytes or rate of the limit
	SoftLimitModeSpecified
	// SoftLimitModeAuto indicates that the soft-limit is auto calculated by the mem-arbitrator
	SoftLimitModeAuto
)

type TrackedConcurrentBudget

type TrackedConcurrentBudget struct {
	ConcurrentBudget
	HeapInuse atomic.Int64
	// contains filtered or unexported fields
}

TrackedConcurrentBudget consists of ConcurrentBudget and heap inuse

func (*TrackedConcurrentBudget) ReportHeapInuse

func (b *TrackedConcurrentBudget) ReportHeapInuse(req int64)

ReportHeapInuse reports the heap inuse to the concurrent budget req > 0: consume req < 0: release

type Tracker

type Tracker struct {
	MemArbitrator *memArbitrator
	Killer        *sqlkiller.SQLKiller

	SessionID atomicutil.Uint64 // SessionID indicates the sessionID the tracker is bound.

	IsRootTrackerOfSess bool // IsRootTrackerOfSess indicates whether this tracker is bound for session
	// contains filtered or unexported fields
}

Tracker is used to track the memory usage during query execution. It contains an optional limit and can be arranged into a tree structure such that the consumption tracked by a Tracker is also tracked by its ancestors. The main idea comes from Apache Impala:

https://github.com/cloudera/Impala/blob/cdh5-trunk/be/src/runtime/mem-tracker.h

By default, memory consumption is tracked via calls to "Consume()", either to the tracker itself or to one of its descendents. A typical sequence of calls for a single Tracker is: 1. tracker.SetLabel() / tracker.SetActionOnExceed() / tracker.AttachTo() 2. tracker.Consume() / tracker.ReplaceChild() / tracker.BytesConsumed()

NOTE: We only protect concurrent access to "bytesConsumed" and "children", that is to say: 1. Only "BytesConsumed()", "Consume()" and "AttachTo()" are thread-safe. 2. Other operations of a Tracker tree is not thread-safe.

We have two limits for the memory quota: soft limit and hard limit. If the soft limit is exceeded, we will trigger the action that alleviates the speed of memory growth. The soft limit is hard-coded as `0.8*hard limit`. The actions that could be triggered are: AggSpillDiskAction.

If the hard limit is exceeded, we will trigger the action that immediately reduces memory usage. The hard limit is set by the system variable `tidb_mem_query_quota`. The actions that could be triggered are: SpillDiskAction, SortAndSpillDiskAction, rateLimitAction, PanicOnExceed, globalPanicOnExceed, LogOnExceed.

func NewGlobalTracker

func NewGlobalTracker(label int, bytesLimit int64) *Tracker

NewGlobalTracker creates a global tracker, its isGlobal is default as true

func NewTracker

func NewTracker(label int, bytesLimit int64) *Tracker

NewTracker creates a memory tracker.

  1. "label" is the label used in the usage string.
  2. "bytesLimit <= 0" means no limit.

For the common tracker, isGlobal is default as false

func (*Tracker) AttachTo

func (t *Tracker) AttachTo(parent *Tracker)

AttachTo attaches this memory tracker as a child to another Tracker. If it already has a parent, this function will remove it from the old parent. Its consumed memory usage is used to update all its ancestors.

func (*Tracker) AttachToGlobalTracker

func (t *Tracker) AttachToGlobalTracker(globalTracker *Tracker)

AttachToGlobalTracker attach the tracker to the global tracker AttachToGlobalTracker should be called at the initialization for the session executor's tracker

func (*Tracker) BufferedConsume

func (t *Tracker) BufferedConsume(bufferedMemSize *int64, bytes int64)

BufferedConsume is used to buffer memory usage and do late consume not thread-safe, should be called in one goroutine

func (*Tracker) BufferedRelease

func (t *Tracker) BufferedRelease(bufferedMemSize *int64, bytes int64)

BufferedRelease is used to buffer memory release and do late release not thread-safe, should be called in one goroutine

func (*Tracker) BytesConsumed

func (t *Tracker) BytesConsumed() int64

BytesConsumed returns the consumed memory usage value in bytes.

func (*Tracker) BytesReleased

func (t *Tracker) BytesReleased() int64

BytesReleased returns the released memory value in bytes.

func (*Tracker) CheckBytesLimit

func (t *Tracker) CheckBytesLimit(val int64) bool

CheckBytesLimit check whether the bytes limit of the tracker is equal to a value. Only used in test.

func (*Tracker) CheckExceed

func (t *Tracker) CheckExceed() bool

CheckExceed checks whether the consumed bytes is exceed for this tracker.

func (*Tracker) Consume

func (t *Tracker) Consume(bs int64)

Consume is used to consume a memory usage. "bytes" can be a negative value, which means this is a memory release operation. When memory usage of a tracker exceeds its bytesSoftLimit/bytesHardLimit, the tracker calls its action, so does each of its ancestors.

func (*Tracker) CountAllChildrenMemUse

func (t *Tracker) CountAllChildrenMemUse() map[string]int64

CountAllChildrenMemUse return memory used tree for the tracker

func (*Tracker) Detach

func (t *Tracker) Detach()

Detach de-attach the tracker child from its parent, then set its parent property as nil

func (*Tracker) DetachFromGlobalTracker

func (t *Tracker) DetachFromGlobalTracker()

DetachFromGlobalTracker detach itself from its parent Note that only the parent of this tracker is Global Tracker could call this function Otherwise it should use Detach

func (*Tracker) FallbackOldAndSetNewAction

func (t *Tracker) FallbackOldAndSetNewAction(a ActionOnExceed)

FallbackOldAndSetNewAction sets the action when memory usage exceeds bytesHardLimit and set the original action as its fallback.

func (*Tracker) FallbackOldAndSetNewActionForSoftLimit

func (t *Tracker) FallbackOldAndSetNewActionForSoftLimit(a ActionOnExceed)

FallbackOldAndSetNewActionForSoftLimit sets the action when memory usage exceeds bytesSoftLimit and set the original action as its fallback.

func (*Tracker) FormatBytes

func (*Tracker) FormatBytes(numBytes int64) string

FormatBytes uses to format bytes, this function will prune precision before format bytes.

func (*Tracker) GetBytesLimit

func (t *Tracker) GetBytesLimit() int64

GetBytesLimit gets the bytes limit for this tracker. "bytesHardLimit <= 0" means no limit.

func (*Tracker) GetChildrenForTest

func (t *Tracker) GetChildrenForTest() []*Tracker

GetChildrenForTest returns children trackers

func (*Tracker) GetFallbackForTest

func (t *Tracker) GetFallbackForTest(ignoreFinishedAction bool) ActionOnExceed

GetFallbackForTest get the oom action used by test.

func (*Tracker) HandleKillSignal

func (t *Tracker) HandleKillSignal()

HandleKillSignal checks if a kill signal has been sent to the session root tracker. If a kill signal is detected, it panics with the error returned by the signal handler.

func (*Tracker) InitMemArbitrator

func (t *Tracker) InitMemArbitrator(
	g *MemArbitrator,
	memQuotaQuery int64,
	killer *sqlkiller.SQLKiller,
	digestKey string,
	memPriority ArbitrationPriority,
	waitAverse bool,
	explicitReserveSize int64,
) bool

InitMemArbitrator attaches (not thread-safe) to the mem arbitrator and initializes the context "m" is the mem-arbitrator. "memQuotaQuery" is the maximum memory quota for query. "killer" is the sql killer. "digestKey" is the digest key. "memPriority" is the memory priority for arbitration. "waitAverse" represents the wait averse property. "explicitReserveSize" is the explicit mem quota size to be reserved.

func (*Tracker) Label

func (t *Tracker) Label() int

Label gets the label of a Tracker.

func (*Tracker) LessThan

func (t *Tracker) LessThan(t2 *Tracker) bool

LessThan indicates whether t byteConsumed is less than t2 byteConsumed.

func (*Tracker) MaxConsumed

func (t *Tracker) MaxConsumed() int64

MaxConsumed returns max number of bytes consumed during execution. Note: Don't make this method return -1 for special meanings in the future. Because binary plan has used -1 to distinguish between "0 bytes" and "N/A". ref: binaryOpFromFlatOp()

func (*Tracker) MemArbitration

func (t *Tracker) MemArbitration() time.Duration

MemArbitration returns the time cost of memory arbitration in nanoseconds

func (*Tracker) Release

func (t *Tracker) Release(bytes int64)

Release is used to release memory tracked, track the released memory until GC triggered if needed If you want your track to be GC-aware, please use Release(bytes) instead of Consume(-bytes), and pass the memory size of the real object. Only Analyze is integrated with Release so far.

func (*Tracker) ReplaceBytesUsed

func (t *Tracker) ReplaceBytesUsed(bytes int64)

ReplaceBytesUsed replace bytesConsume for the tracker

func (*Tracker) ReplaceChild

func (t *Tracker) ReplaceChild(oldChild, newChild *Tracker)

ReplaceChild removes the old child specified in "oldChild" and add a new child specified in "newChild". old child's memory consumption will be removed and new child's memory consumption will be added.

func (*Tracker) Reset

func (t *Tracker) Reset()

Reset detach the tracker from the old parent and clear the old children. The label and byteLimit would not be reset.

func (*Tracker) ResetMaxConsumed

func (t *Tracker) ResetMaxConsumed()

ResetMaxConsumed should be invoked before executing a new statement in a session.

func (*Tracker) SearchTrackerConsumedMoreThanNBytes

func (t *Tracker) SearchTrackerConsumedMoreThanNBytes(limit int64) (res []*Tracker)

SearchTrackerConsumedMoreThanNBytes searches the specific tracker that consumes more than NBytes.

func (*Tracker) SearchTrackerWithoutLock

func (t *Tracker) SearchTrackerWithoutLock(label int) *Tracker

SearchTrackerWithoutLock searches the specific tracker under this tracker without lock.

func (*Tracker) SetActionOnExceed

func (t *Tracker) SetActionOnExceed(a ActionOnExceed)

SetActionOnExceed sets the action when memory usage exceeds bytesHardLimit.

func (*Tracker) SetBytesLimit

func (t *Tracker) SetBytesLimit(bytesLimit int64)

SetBytesLimit sets the bytes limit for this tracker. "bytesHardLimit <= 0" means no limit.

func (*Tracker) SetLabel

func (t *Tracker) SetLabel(label int)

SetLabel sets the label of a Tracker.

func (*Tracker) String

func (t *Tracker) String() string

String returns the string representation of this Tracker tree.

func (*Tracker) UnbindActionFromHardLimit

func (t *Tracker) UnbindActionFromHardLimit(actionToUnbind ActionOnExceed)

UnbindActionFromHardLimit unbinds action from hardLimit.

func (*Tracker) UnbindActions

func (t *Tracker) UnbindActions()

UnbindActions unbinds actionForHardLimit and actionForSoftLimit.

func (*Tracker) WaitArbitrate

func (t *Tracker) WaitArbitrate() (ts time.Time, size int64)

WaitArbitrate returns the start time and size of the last memory allocation attempt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL