github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/momem.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package unary
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func MoMemUsage(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	if len(vectors) != 1 {
    27  		return nil, moerr.NewInvalidInput(proc.Ctx, "no mpool name")
    28  	}
    29  	inputVector := vectors[0]
    30  	resultType := types.T_varchar.ToType()
    31  	inputValues := vector.MustStrCols(inputVector)
    32  	if inputVector.IsScalar() {
    33  		if inputVector.ConstVectorIsNull() {
    34  			return proc.AllocScalarNullVector(resultType), nil
    35  		}
    36  
    37  		memUsage := mpool.ReportMemUsage(inputValues[0])
    38  		return vector.NewConstString(resultType, inputVector.Length(), memUsage, proc.Mp()), nil
    39  	} else {
    40  		panic(moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input"))
    41  	}
    42  }
    43  
    44  func moMemUsageCmd(cmd string, vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    45  	if len(vectors) != 1 {
    46  		return nil, moerr.NewInvalidInput(proc.Ctx, "no mpool name")
    47  	}
    48  	inputVector := vectors[0]
    49  	resultType := types.T_varchar.ToType()
    50  	inputValues := vector.MustStrCols(inputVector)
    51  	if inputVector.IsScalar() {
    52  		if inputVector.ConstVectorIsNull() {
    53  			return proc.AllocScalarNullVector(resultType), nil
    54  		}
    55  
    56  		ok := mpool.MPoolControl(inputValues[0], cmd)
    57  		return vector.NewConstString(resultType, inputVector.Length(), ok, proc.Mp()), nil
    58  	} else {
    59  		panic(moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input"))
    60  	}
    61  }
    62  
    63  func MoEnableMemUsageDetail(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    64  	return moMemUsageCmd("enable_detail", vectors, proc)
    65  }
    66  
    67  func MoDisableMemUsageDetail(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    68  	return moMemUsageCmd("disable_detail", vectors, proc)
    69  }