Debian Codegraph @

stats.go

raw
1 // mgo - MongoDB driver for Go
2 //
3 // Copyright (c) 2010-2012 - Gustavo Niemeyer <[email protected]>
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
27 package mgo
28
29 import (
30 "sync"
31 )
32
33 var stats *Stats
34 var statsMutex sync.Mutex
35
36 func SetStats(enabled bool) {
37 statsMutex.Lock()
38 if enabled {
39 if stats == nil {
40 stats = &Stats{}
41 }
42 } else {
43 stats = nil
44 }
45 statsMutex.Unlock()
46 }
47
48 func GetStats() (snapshot Stats) {
49 statsMutex.Lock()
50 snapshot = *stats
51 statsMutex.Unlock()
52 return
53 }
54
55 func ResetStats() {
56 statsMutex.Lock()
57 debug("Resetting stats")
58 old := stats
59 stats = &Stats{}
60 // These are absolute values:
61 stats.Clusters = old.Clusters
62 stats.SocketsInUse = old.SocketsInUse
63 stats.SocketsAlive = old.SocketsAlive
64 stats.SocketRefs = old.SocketRefs
65 statsMutex.Unlock()
66 return
67 }
68
69 type Stats struct {
70 Clusters int
71 MasterConns int
72 SlaveConns int
73 SentOps int
74 ReceivedOps int
75 ReceivedDocs int
76 SocketsAlive int
77 SocketsInUse int
78 SocketRefs int
79 }
80
81 func (stats *Stats) cluster(delta int) {
82 if stats != nil {
83 statsMutex.Lock()
84 stats.Clusters += delta
85 statsMutex.Unlock()
86 }
87 }
88
89 func (stats *Stats) conn(delta int, master bool) {
90 if stats != nil {
91 statsMutex.Lock()
92 if master {
93 stats.MasterConns += delta
94 } else {
95 stats.SlaveConns += delta
96 }
97 statsMutex.Unlock()
98 }
99 }
100
101 func (stats *Stats) sentOps(delta int) {
102 if stats != nil {
103 statsMutex.Lock()
104 stats.SentOps += delta
105 statsMutex.Unlock()
106 }
107 }
108
109 func (stats *Stats) receivedOps(delta int) {
110 if stats != nil {
111 statsMutex.Lock()
112 stats.ReceivedOps += delta
113 statsMutex.Unlock()
114 }
115 }
116
117 func (stats *Stats) receivedDocs(delta int) {
118 if stats != nil {
119 statsMutex.Lock()
120 stats.ReceivedDocs += delta
121 statsMutex.Unlock()
122 }
123 }
124
125 func (stats *Stats) socketsInUse(delta int) {
126 if stats != nil {
127 statsMutex.Lock()
128 stats.SocketsInUse += delta
129 statsMutex.Unlock()
130 }
131 }
132
133 func (stats *Stats) socketsAlive(delta int) {
134 if stats != nil {
135 statsMutex.Lock()
136 stats.SocketsAlive += delta
137 statsMutex.Unlock()
138 }
139 }
140
141 func (stats *Stats) socketRefs(delta int) {
142 if stats != nil {
143 statsMutex.Lock()
144 stats.SocketRefs += delta
145 statsMutex.Unlock()
146 }
147 }