github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/perfschema/instrument.go (about)

     1  // Copyright 2016 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package perfschema
    15  
    16  import (
    17  	"fmt"
    18  	"sync"
    19  
    20  	"github.com/insionng/yougam/libraries/juju/errors"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    23  )
    24  
    25  // EnumCallerName is used as a parameter to avoid calling runtime.Caller(1) since
    26  // it is too expensive (500ns+ per call), we don't want to invoke it repeatedly for
    27  // each instrument.
    28  type EnumCallerName int
    29  
    30  const (
    31  	// CallerNameSessionExecute is for session.go:Execute() method.
    32  	CallerNameSessionExecute EnumCallerName = iota + 1
    33  )
    34  
    35  const (
    36  	stageInstrumentPrefix       = "stage/"
    37  	statementInstrumentPrefix   = "statement/"
    38  	transactionInstrumentPrefix = "transaction"
    39  )
    40  
    41  // Flag indicators for table setup_timers.
    42  const (
    43  	flagStage = iota + 1
    44  	flagStatement
    45  	flagTransaction
    46  )
    47  
    48  type enumTimerName int
    49  
    50  // Enum values for the TIMER_NAME columns.
    51  // This enum is found in the following tables:
    52  // - performance_schema.setup_timer (TIMER_NAME)
    53  const (
    54  	timerNameNone enumTimerName = iota
    55  	timerNameNanosec
    56  	timerNameMicrosec
    57  	timerNameMillisec
    58  )
    59  
    60  var (
    61  	callerNames = make(map[EnumCallerName]string)
    62  	callerLock  = &sync.RWMutex{}
    63  )
    64  
    65  // addInstrument is used to add an item to setup_instruments table.
    66  func (ps *perfSchema) addInstrument(name string) (uint64, error) {
    67  	record := types.MakeDatums(name, mysql.Enum{Name: "YES", Value: 1}, mysql.Enum{Name: "YES", Value: 1})
    68  	tbl := ps.mTables[TableSetupInstruments]
    69  	handle, err := tbl.AddRecord(nil, record)
    70  	return uint64(handle), errors.Trace(err)
    71  }
    72  
    73  func (ps *perfSchema) getTimerName(flag int) (enumTimerName, error) {
    74  	if flag < 0 || flag >= len(setupTimersRecords) {
    75  		return timerNameNone, errInvalidTimerFlag.Gen("Unknown timerName flag %d", flag)
    76  	}
    77  	timerName := fmt.Sprintf("%s", setupTimersRecords[flag][1].GetString())
    78  	switch timerName {
    79  	case "NANOSECOND":
    80  		return timerNameNanosec, nil
    81  	case "MICROSECOND":
    82  		return timerNameMicrosec, nil
    83  	case "MILLISECOND":
    84  		return timerNameMillisec, nil
    85  	}
    86  	return timerNameNone, nil
    87  }