You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB

  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. package perfschema
  14. import (
  15. "github.com/pingcap/tidb/kv"
  16. "github.com/pingcap/tidb/model"
  17. "github.com/pingcap/tidb/table"
  18. )
  19. // StatementInstrument defines the methods for statement instrumentation points
  20. type StatementInstrument interface {
  21. RegisterStatement(category, name string, elem interface{})
  22. StartStatement(sql string, connID uint64, callerName EnumCallerName, elem interface{}) *StatementState
  23. EndStatement(state *StatementState)
  24. }
  25. // PerfSchema defines the methods to be invoked by the executor
  26. type PerfSchema interface {
  27. // For statement instrumentation only.
  28. StatementInstrument
  29. // GetDBMeta returns db info for PerformanceSchema.
  30. GetDBMeta() *model.DBInfo
  31. // GetTable returns table instance for name.
  32. GetTable(name string) (table.Table, bool)
  33. }
  34. type perfSchema struct {
  35. store kv.Storage
  36. dbInfo *model.DBInfo
  37. tables map[string]*model.TableInfo
  38. mTables map[string]table.Table // MemoryTables for perfSchema
  39. // Used for TableStmtsHistory
  40. historyHandles []int64
  41. historyCursor int
  42. }
  43. var _ PerfSchema = (*perfSchema)(nil)
  44. // PerfHandle is the only access point for the in-memory performance schema information
  45. var (
  46. PerfHandle PerfSchema
  47. )
  48. // NewPerfHandle creates a new perfSchema on store.
  49. func NewPerfHandle(store kv.Storage) PerfSchema {
  50. schema := PerfHandle.(*perfSchema)
  51. schema.store = store
  52. schema.historyHandles = make([]int64, 0, stmtsHistoryElemMax)
  53. _ = schema.initialize()
  54. registerStatements()
  55. return PerfHandle
  56. }
  57. func init() {
  58. schema := &perfSchema{}
  59. PerfHandle = schema
  60. }