stats.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // mgo - MongoDB driver for Go
  2. //
  3. // Copyright (c) 2010-2012 - Gustavo Niemeyer <gustavo@niemeyer.net>
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // 1. Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // 2. Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  20. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. package mgo
  27. import (
  28. "sync"
  29. )
  30. var stats *Stats
  31. var statsMutex sync.Mutex
  32. func SetStats(enabled bool) {
  33. statsMutex.Lock()
  34. if enabled {
  35. if stats == nil {
  36. stats = &Stats{}
  37. }
  38. } else {
  39. stats = nil
  40. }
  41. statsMutex.Unlock()
  42. }
  43. func GetStats() (snapshot Stats) {
  44. statsMutex.Lock()
  45. snapshot = *stats
  46. statsMutex.Unlock()
  47. return
  48. }
  49. func ResetStats() {
  50. statsMutex.Lock()
  51. debug("Resetting stats")
  52. old := stats
  53. stats = &Stats{}
  54. // These are absolute values:
  55. stats.Clusters = old.Clusters
  56. stats.SocketsInUse = old.SocketsInUse
  57. stats.SocketsAlive = old.SocketsAlive
  58. stats.SocketRefs = old.SocketRefs
  59. statsMutex.Unlock()
  60. return
  61. }
  62. type Stats struct {
  63. Clusters int
  64. MasterConns int
  65. SlaveConns int
  66. SentOps int
  67. ReceivedOps int
  68. ReceivedDocs int
  69. SocketsAlive int
  70. SocketsInUse int
  71. SocketRefs int
  72. }
  73. func (stats *Stats) cluster(delta int) {
  74. if stats != nil {
  75. statsMutex.Lock()
  76. stats.Clusters += delta
  77. statsMutex.Unlock()
  78. }
  79. }
  80. func (stats *Stats) conn(delta int, master bool) {
  81. if stats != nil {
  82. statsMutex.Lock()
  83. if master {
  84. stats.MasterConns += delta
  85. } else {
  86. stats.SlaveConns += delta
  87. }
  88. statsMutex.Unlock()
  89. }
  90. }
  91. func (stats *Stats) sentOps(delta int) {
  92. if stats != nil {
  93. statsMutex.Lock()
  94. stats.SentOps += delta
  95. statsMutex.Unlock()
  96. }
  97. }
  98. func (stats *Stats) receivedOps(delta int) {
  99. if stats != nil {
  100. statsMutex.Lock()
  101. stats.ReceivedOps += delta
  102. statsMutex.Unlock()
  103. }
  104. }
  105. func (stats *Stats) receivedDocs(delta int) {
  106. if stats != nil {
  107. statsMutex.Lock()
  108. stats.ReceivedDocs += delta
  109. statsMutex.Unlock()
  110. }
  111. }
  112. func (stats *Stats) socketsInUse(delta int) {
  113. if stats != nil {
  114. statsMutex.Lock()
  115. stats.SocketsInUse += delta
  116. statsMutex.Unlock()
  117. }
  118. }
  119. func (stats *Stats) socketsAlive(delta int) {
  120. if stats != nil {
  121. statsMutex.Lock()
  122. stats.SocketsAlive += delta
  123. statsMutex.Unlock()
  124. }
  125. }
  126. func (stats *Stats) socketRefs(delta int) {
  127. if stats != nil {
  128. statsMutex.Lock()
  129. stats.SocketRefs += delta
  130. statsMutex.Unlock()
  131. }
  132. }