collector.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package transferstats
  20. import (
  21. "sync"
  22. )
  23. // TODO: Stats for a server are only removed when they are sent in a status
  24. // update to that server. So if there's an unexpected disconnect from serverA
  25. // and then a reconnect to serverB, the stats for serverA will never get sent
  26. // (unless there's later a reconnect to serverA). That means the stats for
  27. // serverA will never get deleted and the memory won't get freed. This is only
  28. // a small amount of memory (< 1KB, probably), but we should still probably add
  29. // some kind of stale-stats cleanup.
  30. // Per-host/domain stats.
  31. // Note that the bytes we're counting are the ones going into the tunnel, so do
  32. // not include transport overhead.
  33. type hostStats struct {
  34. numBytesSent int64
  35. numBytesReceived int64
  36. }
  37. // AccumulatedStats holds the Psiphon Server API status request data for a
  38. // given server. To accommodate status requests that may fail, and be retried,
  39. // the TakeOutStatsForServer/PutBackStatsForServer procedure allows the requester
  40. // to check out stats for reporting and merge back stats for a later retry.
  41. type AccumulatedStats struct {
  42. hostnameToStats map[string]*hostStats
  43. }
  44. // GetStatsForStatusRequest summarizes AccumulatedStats data as
  45. // required for the Psiphon Server API status request.
  46. func (stats AccumulatedStats) GetStatsForStatusRequest() map[string]int64 {
  47. hostBytes := make(map[string]int64)
  48. for hostname, hostStats := range stats.hostnameToStats {
  49. totalBytes := hostStats.numBytesReceived + hostStats.numBytesSent
  50. hostBytes[hostname] = totalBytes
  51. }
  52. return hostBytes
  53. }
  54. // serverStats holds per-server stats.
  55. // accumulatedStats data is payload for the Psiphon status request
  56. // which is accessed via TakeOut/PutBack.
  57. // recentBytes data is for tunnel monitoring which is accessed via
  58. // ReportRecentBytesTransferredForServer.
  59. type serverStats struct {
  60. accumulatedStats *AccumulatedStats
  61. recentBytesSent int64
  62. recentBytesReceived int64
  63. }
  64. // allStats is the root object that holds stats for all servers and all hosts,
  65. // as well as the mutex to access them.
  66. var allStats = struct {
  67. statsMutex sync.RWMutex
  68. serverIDtoStats map[string]*serverStats
  69. }{serverIDtoStats: make(map[string]*serverStats)}
  70. // statsUpdate contains new stats counts to be aggregated.
  71. type statsUpdate struct {
  72. serverID string
  73. hostname string
  74. numBytesSent int64
  75. numBytesReceived int64
  76. }
  77. // recordStats makes sure the given stats update is added to the global
  78. // collection. recentBytes are not adjusted when isPutBack is true,
  79. // as recentBytes aren't subject to TakeOut/PutBack.
  80. func recordStat(stat *statsUpdate, isRecordingHostBytes, isPutBack bool) {
  81. allStats.statsMutex.Lock()
  82. defer allStats.statsMutex.Unlock()
  83. storedServerStats := allStats.serverIDtoStats[stat.serverID]
  84. if storedServerStats == nil {
  85. storedServerStats = &serverStats{
  86. accumulatedStats: &AccumulatedStats{
  87. hostnameToStats: make(map[string]*hostStats)}}
  88. allStats.serverIDtoStats[stat.serverID] = storedServerStats
  89. }
  90. if isRecordingHostBytes {
  91. if stat.hostname == "" {
  92. stat.hostname = "(OTHER)"
  93. }
  94. storedHostStats := storedServerStats.accumulatedStats.hostnameToStats[stat.hostname]
  95. if storedHostStats == nil {
  96. storedHostStats = &hostStats{}
  97. storedServerStats.accumulatedStats.hostnameToStats[stat.hostname] = storedHostStats
  98. }
  99. storedHostStats.numBytesSent += stat.numBytesSent
  100. storedHostStats.numBytesReceived += stat.numBytesReceived
  101. }
  102. if !isPutBack {
  103. storedServerStats.recentBytesSent += stat.numBytesSent
  104. storedServerStats.recentBytesReceived += stat.numBytesReceived
  105. }
  106. }
  107. // ReportRecentBytesTransferredForServer returns bytes sent and received since
  108. // the last call to ReportRecentBytesTransferredForServer. The accumulated sent
  109. // and received are reset to 0 by this call.
  110. func ReportRecentBytesTransferredForServer(serverID string) (sent, received int64) {
  111. allStats.statsMutex.Lock()
  112. defer allStats.statsMutex.Unlock()
  113. stats := allStats.serverIDtoStats[serverID]
  114. if stats == nil {
  115. return
  116. }
  117. sent = stats.recentBytesSent
  118. received = stats.recentBytesReceived
  119. stats.recentBytesSent = 0
  120. stats.recentBytesReceived = 0
  121. return
  122. }
  123. // TakeOutStatsForServer borrows the AccumulatedStats for the specified
  124. // server. When we fail to report these stats, resubmit them with
  125. // PutBackStatsForServer. Stats will continue to be accumulated between
  126. // TakeOut and PutBack calls. The recentBytes values are unaffected by
  127. // TakeOut/PutBack. Returns empty stats if the serverID is not found.
  128. func TakeOutStatsForServer(serverID string) (accumulatedStats *AccumulatedStats) {
  129. allStats.statsMutex.Lock()
  130. defer allStats.statsMutex.Unlock()
  131. newAccumulatedStats := &AccumulatedStats{
  132. hostnameToStats: make(map[string]*hostStats)}
  133. // Note: for an existing serverStats, only the accumulatedStats is
  134. // affected; the recentBytes fields are not changed.
  135. serverStats := allStats.serverIDtoStats[serverID]
  136. if serverStats != nil {
  137. accumulatedStats = serverStats.accumulatedStats
  138. serverStats.accumulatedStats = newAccumulatedStats
  139. } else {
  140. accumulatedStats = newAccumulatedStats
  141. }
  142. return
  143. }
  144. // PutBackStatsForServer re-adds a set of server stats to the collection.
  145. func PutBackStatsForServer(serverID string, accumulatedStats *AccumulatedStats) {
  146. for hostname, hoststats := range accumulatedStats.hostnameToStats {
  147. recordStat(
  148. &statsUpdate{
  149. serverID: serverID,
  150. hostname: hostname,
  151. numBytesSent: hoststats.numBytesSent,
  152. numBytesReceived: hoststats.numBytesReceived,
  153. },
  154. // We can set isRecordingHostBytes to true, regardless of whether there
  155. // are any regexes, since there will be no host bytes to put back if they
  156. // are not being recorded.
  157. true,
  158. true)
  159. }
  160. }