github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/process/analyze.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 process
    16  
    17  import (
    18  	"sync"
    19  	"sync/atomic"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/reuse"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    25  )
    26  
    27  func init() {
    28  	reuse.CreatePool[AnalyzeInfo](
    29  		newAnalyzeInfo,
    30  		resetAnalyzeInfo,
    31  		reuse.DefaultOptions[AnalyzeInfo]().WithEnableChecker(),
    32  	)
    33  }
    34  
    35  func (a AnalyzeInfo) TypeName() string {
    36  	return "compile.anaylzeinfo"
    37  }
    38  
    39  func newAnalyzeInfo() *AnalyzeInfo {
    40  	a := &AnalyzeInfo{}
    41  	a.mu = &sync.Mutex{}
    42  	return a
    43  }
    44  
    45  func resetAnalyzeInfo(a *AnalyzeInfo) {
    46  	a.NodeId = 0
    47  	a.InputRows = 0
    48  	a.OutputRows = 0
    49  	a.TimeConsumed = 0
    50  	a.WaitTimeConsumed = 0
    51  	a.InputSize = 0
    52  	a.OutputSize = 0
    53  	a.MemorySize = 0
    54  	a.DiskIO = 0
    55  	a.S3IOByte = 0
    56  	a.S3IOInputCount = 0
    57  	a.S3IOOutputCount = 0
    58  	a.NetworkIO = 0
    59  	a.ScanTime = 0
    60  	a.InsertTime = 0
    61  	a.mu.Lock()
    62  	defer a.mu.Unlock()
    63  	a.TimeConsumedArrayMajor = a.TimeConsumedArrayMajor[:0]
    64  	a.TimeConsumedArrayMinor = a.TimeConsumedArrayMinor[:0]
    65  }
    66  
    67  func (a *analyze) Start() {
    68  	a.start = time.Now()
    69  }
    70  
    71  func (a *analyze) Stop() {
    72  	if a.analInfo != nil {
    73  		atomic.AddInt64(&a.analInfo.WaitTimeConsumed, int64(a.wait/time.Nanosecond))
    74  		consumeTime := int64((time.Since(a.start) - a.wait - a.childrenCallDuration) / time.Nanosecond)
    75  		atomic.AddInt64(&a.analInfo.TimeConsumed, consumeTime)
    76  		a.analInfo.AddSingleParallelTimeConsumed(a.parallelMajor, a.parallelIdx, consumeTime)
    77  	}
    78  }
    79  
    80  func (a *analyze) Alloc(size int64) {
    81  	if a.analInfo != nil {
    82  		atomic.AddInt64(&a.analInfo.MemorySize, size)
    83  	}
    84  }
    85  
    86  func (a *analyze) Input(bat *batch.Batch, isFirst bool) {
    87  	if a.analInfo != nil && bat != nil && isFirst {
    88  		atomic.AddInt64(&a.analInfo.InputSize, int64(bat.Size()))
    89  		atomic.AddInt64(&a.analInfo.InputRows, int64(bat.RowCount()))
    90  	}
    91  }
    92  
    93  func (a *analyze) Output(bat *batch.Batch, isLast bool) {
    94  	if a.analInfo != nil && bat != nil && isLast {
    95  		atomic.AddInt64(&a.analInfo.OutputSize, int64(bat.Size()))
    96  		atomic.AddInt64(&a.analInfo.OutputRows, int64(bat.RowCount()))
    97  	}
    98  }
    99  
   100  func (a *analyze) WaitStop(start time.Time) {
   101  	a.wait += time.Since(start)
   102  }
   103  
   104  func (a *analyze) ChildrenCallStop(start time.Time) {
   105  	a.childrenCallDuration += time.Since(start)
   106  }
   107  
   108  func (a *analyze) DiskIO(bat *batch.Batch) {
   109  	if a.analInfo != nil && bat != nil {
   110  		atomic.AddInt64(&a.analInfo.DiskIO, int64(bat.Size()))
   111  	}
   112  }
   113  
   114  func (a *analyze) S3IOByte(bat *batch.Batch) {
   115  	if a.analInfo != nil && bat != nil {
   116  		atomic.AddInt64(&a.analInfo.S3IOByte, int64(bat.Size()))
   117  	}
   118  }
   119  
   120  func (a *analyze) S3IOInputCount(count int) {
   121  	if a.analInfo != nil {
   122  		atomic.AddInt64(&a.analInfo.S3IOInputCount, int64(count))
   123  	}
   124  }
   125  
   126  func (a *analyze) S3IOOutputCount(count int) {
   127  	if a.analInfo != nil {
   128  		atomic.AddInt64(&a.analInfo.S3IOOutputCount, int64(count))
   129  	}
   130  }
   131  
   132  func (a *analyze) Network(bat *batch.Batch) {
   133  	if a.analInfo != nil && bat != nil {
   134  		atomic.AddInt64(&a.analInfo.NetworkIO, int64(bat.Size()))
   135  	}
   136  }
   137  
   138  func (a *analyze) AddScanTime(t time.Time) {
   139  	if a.analInfo != nil {
   140  		atomic.AddInt64(&a.analInfo.ScanTime, int64(time.Since(t)))
   141  	}
   142  }
   143  
   144  func (a *analyze) AddInsertTime(t time.Time) {
   145  	if a.analInfo != nil {
   146  		atomic.AddInt64(&a.analInfo.InsertTime, int64(time.Since(t)))
   147  	}
   148  }