github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/stats.go (about)

     1  // Copyright 2021 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 objectio
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/util/metric/stats"
    23  )
    24  
    25  type hitStats struct {
    26  	hit   stats.Counter
    27  	total stats.Counter
    28  }
    29  
    30  func (s *hitStats) Record(hit, total int) {
    31  	s.total.Add(int64(total))
    32  	s.hit.Add(int64(hit))
    33  }
    34  
    35  func (s *hitStats) Export() (hit, total int64) {
    36  	hit = s.hit.Load()
    37  	total = s.total.Load()
    38  	return
    39  }
    40  
    41  func (s *hitStats) ExportW() (hit, total int64) {
    42  	hit = s.hit.SwapW(0)
    43  	total = s.total.SwapW(0)
    44  	return
    45  }
    46  
    47  func (s *hitStats) ExportAll() (whit, wtotal int64, hit, total int64) {
    48  	whit = s.hit.SwapW(0)
    49  	wtotal = s.total.SwapW(0)
    50  	hit = s.hit.Swap(0)
    51  	total = s.total.Swap(0)
    52  	return
    53  }
    54  
    55  type Stats struct {
    56  	blockSelectivity      hitStats
    57  	columnSelectivity     hitStats
    58  	readFilterSelectivity hitStats
    59  	readDelCnt            stats.Counter
    60  	readDelOpTotal        stats.Counter
    61  	readDelRead           stats.Counter
    62  	readDelBisect         stats.Counter
    63  }
    64  
    65  func NewStats() *Stats {
    66  	return &Stats{}
    67  }
    68  
    69  func (s *Stats) RecordReadFilterSelectivity(hit, total int) {
    70  	s.readFilterSelectivity.Record(hit, total)
    71  }
    72  
    73  func (s *Stats) ExportReadFilterSelectivity() (
    74  	whit, wtotal int64, hit, total int64,
    75  ) {
    76  	whit, wtotal = s.readFilterSelectivity.ExportW()
    77  	if wtotal == 0 {
    78  		whit = 0
    79  	}
    80  	hit, total = s.readFilterSelectivity.Export()
    81  	return
    82  }
    83  
    84  func (s *Stats) RecordBlockSelectivity(hit, total int) {
    85  	s.blockSelectivity.Record(hit, total)
    86  }
    87  
    88  func (s *Stats) ExportBlockSelectivity() (
    89  	whit, wtotal int64,
    90  ) {
    91  	whit, wtotal, _, _ = s.blockSelectivity.ExportAll()
    92  	if wtotal == 0 {
    93  		whit = 0
    94  	}
    95  	return
    96  }
    97  
    98  func (s *Stats) RecordColumnSelectivity(hit, total int) {
    99  	s.columnSelectivity.Record(hit, total)
   100  }
   101  
   102  func (s *Stats) ExportColumnSelctivity() (
   103  	hit, total int64,
   104  ) {
   105  	hit, total, _, _ = s.columnSelectivity.ExportAll()
   106  	if total == 0 {
   107  		hit = 0
   108  	}
   109  	return
   110  }
   111  
   112  func (s *Stats) RecordReadDel(total, read, bisect time.Duration) {
   113  	s.readDelOpTotal.Add(int64(total))
   114  	s.readDelRead.Add(int64(read))
   115  	s.readDelBisect.Add(int64(bisect))
   116  	s.readDelCnt.Add(1)
   117  }
   118  
   119  func (s *Stats) ExportReadDel() (total, read, bisect time.Duration, cnt int64) {
   120  	total = time.Duration(s.readDelOpTotal.SwapW(0))
   121  	read = time.Duration(s.readDelRead.SwapW(0))
   122  	bisect = time.Duration(s.readDelBisect.SwapW(0))
   123  	cnt = s.readDelCnt.SwapW(0)
   124  	return
   125  }
   126  
   127  func (s *Stats) ExportString() string {
   128  	var w bytes.Buffer
   129  	whit, wtotal := s.ExportBlockSelectivity()
   130  	wrate, rate := 0.0, 0.0
   131  	if wtotal != 0 {
   132  		wrate = float64(whit) / float64(wtotal)
   133  	}
   134  	fmt.Fprintf(&w, "SelectivityStats: BLK[%d/%d=%0.4f] ", whit, wtotal, wrate)
   135  	whit, wtotal = s.ExportColumnSelctivity()
   136  	wrate = 0.0
   137  	if wtotal != 0 {
   138  		wrate = float64(whit) / float64(wtotal)
   139  	}
   140  	fmt.Fprintf(&w, "COL[%d/%d=%0.4f] ", whit, wtotal, wrate)
   141  	whit, wtotal, hit, total := s.ExportReadFilterSelectivity()
   142  	wrate = 0.0
   143  	if wtotal != 0 {
   144  		wrate = float64(whit) / float64(wtotal)
   145  	}
   146  	if total != 0 {
   147  		rate = float64(hit) / float64(total)
   148  	}
   149  	fmt.Fprintf(&w, "RDF[%d/%d=%0.4f,%d/%d=%0.4f]", whit, wtotal, wrate, hit, total, rate)
   150  	rtotal, rread, rbisect, rcnt := s.ExportReadDel()
   151  	fmt.Fprintf(&w, "RDD[%v/%v/%v/%v]", rtotal, rread, rbisect, rcnt)
   152  	return w.String()
   153  }