collector.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, int64) {
  47. hostBytes := make(map[string]int64)
  48. bytesTransferred := int64(0)
  49. for hostname, hostStats := range stats.hostnameToStats {
  50. totalBytes := hostStats.numBytesReceived + hostStats.numBytesSent
  51. bytesTransferred += totalBytes
  52. hostBytes[hostname] = totalBytes
  53. }
  54. return hostBytes, bytesTransferred
  55. }
  56. // serverStats holds per-server stats.
  57. // accumulatedStats data is payload for the Psiphon status request
  58. // which is accessed via TakeOut/PutBack.
  59. // recentBytes data is for tunnel monitoring which is accessed via
  60. // ReportRecentBytesTransferredForServer.
  61. type serverStats struct {
  62. accumulatedStats *AccumulatedStats
  63. recentBytesSent int64
  64. recentBytesReceived int64
  65. }
  66. // allStats is the root object that holds stats for all servers and all hosts,
  67. // as well as the mutex to access them.
  68. var allStats = struct {
  69. statsMutex sync.RWMutex
  70. serverIDtoStats map[string]*serverStats
  71. }{serverIDtoStats: make(map[string]*serverStats)}
  72. // statsUpdate contains new stats counts to be aggregated.
  73. type statsUpdate struct {
  74. serverID string
  75. hostname string
  76. numBytesSent int64
  77. numBytesReceived int64
  78. }
  79. // recordStats makes sure the given stats update is added to the global
  80. // collection. recentBytes are not adjusted when isPutBack is true,
  81. // as recentBytes aren't subject to TakeOut/PutBack.
  82. func recordStat(stat *statsUpdate, isPutBack bool) {
  83. allStats.statsMutex.Lock()
  84. defer allStats.statsMutex.Unlock()
  85. if stat.hostname == "" {
  86. stat.hostname = "(OTHER)"
  87. }
  88. storedServerStats := allStats.serverIDtoStats[stat.serverID]
  89. if storedServerStats == nil {
  90. storedServerStats = &serverStats{
  91. accumulatedStats: &AccumulatedStats{
  92. hostnameToStats: make(map[string]*hostStats)}}
  93. allStats.serverIDtoStats[stat.serverID] = storedServerStats
  94. }
  95. storedHostStats := storedServerStats.accumulatedStats.hostnameToStats[stat.hostname]
  96. if storedHostStats == nil {
  97. storedHostStats = &hostStats{}
  98. storedServerStats.accumulatedStats.hostnameToStats[stat.hostname] = storedHostStats
  99. }
  100. storedHostStats.numBytesSent += stat.numBytesSent
  101. storedHostStats.numBytesReceived += stat.numBytesReceived
  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. true)
  155. }
  156. }