github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/metrics.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 bitalosdb
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/json"
    20  )
    21  
    22  type MetricsInfo struct {
    23  	FlushMemTime       int64 `json:"-"`
    24  	BithashFileTotal   int   `json:"bithash_file_total"`
    25  	BithashKeyTotal    int   `json:"bithash_key_total"`
    26  	BithashDelKeyTotal int   `json:"bithash_del_key_total"`
    27  }
    28  
    29  func (s MetricsInfo) String() string {
    30  	str, err := json.Marshal(s)
    31  	if err != nil {
    32  		return ""
    33  	} else {
    34  		return string(str)
    35  	}
    36  }
    37  
    38  func (d *DB) statsDiskSize() int64 {
    39  	var size int64
    40  	for _, bitower := range d.bitowers {
    41  		ps := bitower.btree.BitpageStats()
    42  		size += ps.Size
    43  	}
    44  	return size
    45  }
    46  
    47  func (d *DB) MetricsInfo() MetricsInfo {
    48  	var stat MetricsInfo
    49  	for i := range d.bitowers {
    50  		bs := d.bitowers[i].btree.BithashStats()
    51  		if bs != nil {
    52  			fileTotal := int(bs.FileTotal.Load())
    53  			stat.BithashFileTotal += fileTotal
    54  			stat.BithashKeyTotal += int(bs.KeyTotal.Load())
    55  			stat.BithashDelKeyTotal += int(bs.DelKeyTotal.Load())
    56  		}
    57  	}
    58  
    59  	stat.FlushMemTime = d.flushMemTime.Load()
    60  	return stat
    61  }
    62  
    63  func (d *DB) CacheInfo() string {
    64  	if d.cache == nil {
    65  		return ""
    66  	}
    67  	return d.cache.MetricsInfo()
    68  }
    69  
    70  func (d *DB) DebugInfo() string {
    71  	dataType := d.opts.DataType
    72  	if dataType == "" {
    73  		dataType = "base"
    74  	}
    75  
    76  	dinfo := new(bytes.Buffer)
    77  	for _, bitower := range d.bitowers {
    78  		bpInfo := bitower.btree.BitreeDebugInfo(dataType)
    79  		if bpInfo != "" {
    80  			dinfo.WriteString(bpInfo)
    81  		}
    82  
    83  		btInfo := bitower.btree.BitableDebugInfo(dataType)
    84  		if btInfo != "" {
    85  			dinfo.WriteString(btInfo)
    86  		}
    87  
    88  		bhInfo := bitower.btree.BithashDebugInfo(dataType)
    89  		if bhInfo != "" {
    90  			dinfo.WriteString(bhInfo)
    91  		}
    92  	}
    93  
    94  	return dinfo.String()
    95  }