Bogdan Caprita | 092a557 | 2015-08-28 22:14:07 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "sync" |
| 9 | |
| 10 | "v.io/v23/naming" |
| 11 | libstats "v.io/x/ref/lib/stats" |
| 12 | "v.io/x/ref/lib/stats/counter" |
| 13 | ) |
| 14 | |
| 15 | // stats contains various exported stats we maintain for the device manager. |
| 16 | type stats struct { |
| 17 | sync.Mutex |
| 18 | // How many times apps were run via the Run rpc. |
| 19 | runs *counter.Counter |
| 20 | // How many times apps were auto-restarted by the reaper. |
| 21 | restarts *counter.Counter |
| 22 | // Same as above, but broken down by instance. |
| 23 | // This is not of type lib/stats::Map since we want the detailed rate |
| 24 | // and delta stats per instance. |
| 25 | // TODO(caprita): Garbage-collect old instances? |
| 26 | runsPerInstance map[string]*counter.Counter |
| 27 | restartsPerInstance map[string]*counter.Counter |
| 28 | } |
| 29 | |
| 30 | func newStats() *stats { |
| 31 | return &stats{ |
| 32 | runs: libstats.NewCounter("runs"), |
| 33 | runsPerInstance: make(map[string]*counter.Counter), |
| 34 | restarts: libstats.NewCounter("restarts"), |
| 35 | restartsPerInstance: make(map[string]*counter.Counter), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func (s *stats) incrRestarts(instance string) { |
| 40 | s.Lock() |
| 41 | defer s.Unlock() |
| 42 | s.restarts.Incr(1) |
| 43 | perInstanceCtr, ok := s.restartsPerInstance[instance] |
| 44 | if !ok { |
| 45 | perInstanceCtr = libstats.NewCounter(naming.Join("restarts", instance)) |
| 46 | s.restartsPerInstance[instance] = perInstanceCtr |
| 47 | } |
| 48 | perInstanceCtr.Incr(1) |
| 49 | } |
| 50 | |
| 51 | func (s *stats) incrRuns(instance string) { |
| 52 | s.Lock() |
| 53 | defer s.Unlock() |
| 54 | s.runs.Incr(1) |
| 55 | perInstanceCtr, ok := s.runsPerInstance[instance] |
| 56 | if !ok { |
| 57 | perInstanceCtr = libstats.NewCounter(naming.Join("runs", instance)) |
| 58 | s.runsPerInstance[instance] = perInstanceCtr |
| 59 | } |
| 60 | perInstanceCtr.Incr(1) |
| 61 | } |