stats.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package fileSystem
  2. import (
  3. "encoding/json"
  4. "sync"
  5. )
  6. const (
  7. // Operations that will be running serializely
  8. StatsSetsHit = 100
  9. StatsSetsMiss = 101
  10. StatsDeletesHit = 102
  11. StatsDeletesMiss = 103
  12. StatsUpdatesHit = 104
  13. StatsUpdatesMiss = 105
  14. StatsTestAndSetsHit = 106
  15. StatsTestAndSetsMiss = 107
  16. StatsRecoveryHit = 108
  17. StatsRecoveryMiss = 109
  18. // concurrent operations
  19. StatsGetsHit = 200
  20. StatsGetsMiss = 201
  21. StatsWatchHit = 300
  22. StatsWatchMiss = 301
  23. StatsInWatchingNum = 302
  24. StatsSaveHit = 400
  25. StatsSaveMiss = 401
  26. )
  27. type EtcdStats struct {
  28. // Lock for synchronization
  29. rwlock sync.RWMutex
  30. // Number of get requests
  31. GetsHit uint64 `json:"gets_hits"`
  32. GetsMiss uint64 `json:"gets_misses"`
  33. // Number of sets requests
  34. SetsHit uint64 `json:"sets_hits"`
  35. SetsMiss uint64 `json:"sets_misses"`
  36. // Number of delete requests
  37. DeletesHit uint64 `json:"deletes_hits"`
  38. DeletesMiss uint64 `json:"deletes_misses"`
  39. // Number of update requests
  40. UpdatesHit uint64 `json:"updates_hits"`
  41. UpdatesMiss uint64 `json:"updates_misses"`
  42. // Number of testAndSet requests
  43. TestAndSetsHit uint64 `json:"testAndSets_hits"`
  44. TestAndSetsMiss uint64 `json:"testAndSets_misses"`
  45. // Number of Watch requests
  46. WatchHit uint64 `json:"watch_hit"`
  47. WatchMiss uint64 `json:"watch_miss"`
  48. InWatchingNum uint64 `json:"in_watching_number"`
  49. // Number of save requests
  50. SaveHit uint64 `json:"save_hit"`
  51. SaveMiss uint64 `json:"save_miss"`
  52. // Number of recovery requests
  53. RecoveryHit uint64 `json:"recovery_hit"`
  54. RecoveryMiss uint64 `json:"recovery_miss"`
  55. }
  56. func newStats() *EtcdStats {
  57. e := new(EtcdStats)
  58. return e
  59. }
  60. // Status() return the statistics info of etcd storage its recent start
  61. func (e *EtcdStats) GetStats() []byte {
  62. b, _ := json.Marshal(e)
  63. return b
  64. }
  65. func (e *EtcdStats) TotalReads() uint64 {
  66. return e.GetsHit + e.GetsMiss
  67. }
  68. func (e *EtcdStats) TotalWrites() uint64 {
  69. return e.SetsHit + e.SetsMiss +
  70. e.DeletesHit + e.DeletesMiss +
  71. e.UpdatesHit + e.UpdatesMiss +
  72. e.TestAndSetsHit + e.TestAndSetsMiss
  73. }
  74. func (e *EtcdStats) IncStats(field int) {
  75. if field >= 200 {
  76. e.rwlock.Lock()
  77. switch field {
  78. case StatsGetsHit:
  79. e.GetsHit++
  80. case StatsGetsMiss:
  81. e.GetsMiss++
  82. case StatsWatchHit:
  83. e.WatchHit++
  84. case StatsWatchMiss:
  85. e.WatchMiss++
  86. case StatsInWatchingNum:
  87. e.InWatchingNum++
  88. case StatsSaveHit:
  89. e.SaveHit++
  90. case StatsSaveMiss:
  91. e.SaveMiss++
  92. }
  93. e.rwlock.Unlock()
  94. } else {
  95. e.rwlock.RLock()
  96. switch field {
  97. case StatsSetsHit:
  98. e.SetsHit++
  99. case StatsSetsMiss:
  100. e.SetsMiss++
  101. case StatsDeletesHit:
  102. e.DeletesHit++
  103. case StatsDeletesMiss:
  104. e.DeletesMiss++
  105. case StatsUpdatesHit:
  106. e.UpdatesHit++
  107. case StatsUpdatesMiss:
  108. e.UpdatesMiss++
  109. case StatsTestAndSetsHit:
  110. e.TestAndSetsHit++
  111. case StatsTestAndSetsMiss:
  112. e.TestAndSetsMiss++
  113. case StatsRecoveryHit:
  114. e.RecoveryHit++
  115. case StatsRecoveryMiss:
  116. e.RecoveryMiss++
  117. }
  118. e.rwlock.RUnlock()
  119. }
  120. }