github.com/matrixorigin/matrixone@v1.2.0/pkg/perfcounter/named.go (about)

     1  // Copyright 2023 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 perfcounter
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/pb/query"
    19  	"strings"
    20  	"sync"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/logutil"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  var Named sync.Map
    27  
    28  func NameForNode(nodeType, uuid string) string {
    29  	return strings.Join([]string{
    30  		"node",
    31  		nodeType,
    32  		uuid,
    33  	}, ":")
    34  }
    35  
    36  func NameForFileService(nodeType string, uuid string, fsName string) string {
    37  	return strings.Join([]string{
    38  		"fs",
    39  		nodeType,
    40  		uuid,
    41  		fsName,
    42  	}, ":")
    43  }
    44  
    45  func decodeName(name string) (string, string) {
    46  	s := strings.Split(name, ":")
    47  	if len(s) != 3 {
    48  		return "", ""
    49  	}
    50  	return strings.TrimSpace(s[1]), strings.TrimSpace(s[2])
    51  }
    52  
    53  func LogNodeCacheStats(uuid string) {
    54  	v, ok := Named.Load(NameForNode("", uuid))
    55  	if !ok {
    56  		return
    57  	}
    58  	counter := v.(*CounterSet)
    59  	logutil.Debug("cache stats",
    60  		zap.Any("node", uuid),
    61  		zap.Any("type", "memory"),
    62  		zap.Any("used", counter.FileService.Cache.Memory.Used.Load()),
    63  		zap.Any("free", counter.FileService.Cache.Memory.Available.Load()),
    64  		zap.Any("hit ratio", float64(counter.FileService.Cache.Memory.Hit.Load())/
    65  			float64(counter.FileService.Cache.Memory.Read.Load())),
    66  	)
    67  	logutil.Debug("cache stats",
    68  		zap.Any("node", uuid),
    69  		zap.Any("type", "disk"),
    70  		zap.Any("hit ratio", float64(counter.FileService.Cache.Disk.Hit.Load())/
    71  			float64(counter.FileService.Cache.Disk.Read.Load())),
    72  	)
    73  }
    74  
    75  // GetCacheStats returns the cache stats for nodes.
    76  func GetCacheStats(callback func(info []*query.CacheInfo)) {
    77  	var ok = false
    78  	var ptr *CounterSet
    79  	var name string
    80  	Named.Range(func(k, v interface{}) bool {
    81  		if callback != nil {
    82  			name = ""
    83  			ptr = nil
    84  
    85  			if name, ok = k.(string); !ok {
    86  				return true
    87  			}
    88  			if strings.Contains(strings.ToLower(name), "global") {
    89  				return true
    90  			}
    91  			if ptr, ok = v.(*CounterSet); !ok {
    92  				return true
    93  			}
    94  			if ptr == nil {
    95  				return true
    96  			}
    97  
    98  			nodeType, nodeId := decodeName(name)
    99  			if len(nodeType) == 0 || len(nodeId) == 0 {
   100  				return true
   101  			}
   102  
   103  			//memory
   104  			read1 := ptr.FileService.Cache.Memory.Read.Load()
   105  			if read1 <= 0 {
   106  				read1 = 1
   107  			}
   108  			ci1 := &query.CacheInfo{
   109  				NodeType:  nodeType,
   110  				NodeId:    nodeId,
   111  				CacheType: "memory",
   112  				Used:      uint64(ptr.FileService.Cache.Memory.Used.Load()),
   113  				Free:      uint64(ptr.FileService.Cache.Memory.Available.Load()),
   114  				HitRatio:  float32(ptr.FileService.Cache.Memory.Hit.Load()) / float32(read1),
   115  			}
   116  
   117  			read2 := ptr.FileService.Cache.Disk.Read.Load()
   118  			if read2 <= 0 {
   119  				read2 = 1
   120  			}
   121  			ci2 := &query.CacheInfo{
   122  				NodeType:  nodeType,
   123  				NodeId:    nodeId,
   124  				CacheType: "disk",
   125  				Used:      0,
   126  				Free:      0,
   127  				HitRatio:  float32(ptr.FileService.Cache.Disk.Hit.Load()) / float32(read2),
   128  			}
   129  
   130  			callback([]*query.CacheInfo{ci1, ci2})
   131  		}
   132  
   133  		return true
   134  	})
   135  }