counter.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Copyright (c) 2015 Arista Networks, Inc.
  19. // Use of this source code is governed by the Apache License 2.0
  20. // that can be found in the COPYING file.
  21. package stats
  22. import (
  23. "sync"
  24. "time"
  25. )
  26. var (
  27. // TimeNow is used for testing.
  28. TimeNow = time.Now
  29. )
  30. const (
  31. hour = 0
  32. tenminutes = 1
  33. minute = 2
  34. )
  35. // Counter is a counter that keeps track of its recent values over a given
  36. // period of time, and with a given resolution. Use newCounter() to instantiate.
  37. type Counter struct {
  38. mu sync.RWMutex
  39. ts [3]*timeseries
  40. lastUpdate time.Time
  41. }
  42. // newCounter returns a new Counter.
  43. func newCounter() *Counter {
  44. now := TimeNow()
  45. c := &Counter{}
  46. c.ts[hour] = newTimeSeries(now, time.Hour, time.Minute)
  47. c.ts[tenminutes] = newTimeSeries(now, 10*time.Minute, 10*time.Second)
  48. c.ts[minute] = newTimeSeries(now, time.Minute, time.Second)
  49. return c
  50. }
  51. func (c *Counter) advance() time.Time {
  52. now := TimeNow()
  53. for _, ts := range c.ts {
  54. ts.advanceTime(now)
  55. }
  56. return now
  57. }
  58. // Value returns the current value of the counter.
  59. func (c *Counter) Value() int64 {
  60. c.mu.RLock()
  61. defer c.mu.RUnlock()
  62. return c.ts[minute].headValue()
  63. }
  64. // LastUpdate returns the last update time of the counter.
  65. func (c *Counter) LastUpdate() time.Time {
  66. c.mu.RLock()
  67. defer c.mu.RUnlock()
  68. return c.lastUpdate
  69. }
  70. // Set updates the current value of the counter.
  71. func (c *Counter) Set(value int64) {
  72. c.mu.Lock()
  73. defer c.mu.Unlock()
  74. c.lastUpdate = c.advance()
  75. for _, ts := range c.ts {
  76. ts.set(value)
  77. }
  78. }
  79. // Incr increments the current value of the counter by 'delta'.
  80. func (c *Counter) Incr(delta int64) {
  81. c.mu.Lock()
  82. defer c.mu.Unlock()
  83. c.lastUpdate = c.advance()
  84. for _, ts := range c.ts {
  85. ts.incr(delta)
  86. }
  87. }
  88. // Delta1h returns the delta for the last hour.
  89. func (c *Counter) Delta1h() int64 {
  90. c.mu.RLock()
  91. defer c.mu.RUnlock()
  92. c.advance()
  93. return c.ts[hour].delta()
  94. }
  95. // Delta10m returns the delta for the last 10 minutes.
  96. func (c *Counter) Delta10m() int64 {
  97. c.mu.RLock()
  98. defer c.mu.RUnlock()
  99. c.advance()
  100. return c.ts[tenminutes].delta()
  101. }
  102. // Delta1m returns the delta for the last minute.
  103. func (c *Counter) Delta1m() int64 {
  104. c.mu.RLock()
  105. defer c.mu.RUnlock()
  106. c.advance()
  107. return c.ts[minute].delta()
  108. }
  109. // Rate1h returns the rate of change of the counter in the last hour.
  110. func (c *Counter) Rate1h() float64 {
  111. c.mu.RLock()
  112. defer c.mu.RUnlock()
  113. c.advance()
  114. return c.ts[hour].rate()
  115. }
  116. // Rate10m returns the rate of change of the counter in the last 10 minutes.
  117. func (c *Counter) Rate10m() float64 {
  118. c.mu.RLock()
  119. defer c.mu.RUnlock()
  120. c.advance()
  121. return c.ts[tenminutes].rate()
  122. }
  123. // Rate1m returns the rate of change of the counter in the last minute.
  124. func (c *Counter) Rate1m() float64 {
  125. c.mu.RLock()
  126. defer c.mu.RUnlock()
  127. c.advance()
  128. return c.ts[minute].rate()
  129. }
  130. // Reset resets the counter to an empty state.
  131. func (c *Counter) Reset() {
  132. c.mu.Lock()
  133. defer c.mu.Unlock()
  134. now := TimeNow()
  135. for _, ts := range c.ts {
  136. ts.reset(now)
  137. }
  138. }