OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
CounterStat.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * 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 CONTRIBUTORS BE LIABLE FOR ANY
20  * 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 
28 using System;
29 using System.Collections.Generic;
30 using System.Linq;
31 using System.Text;
32 
33 using OpenMetaverse.StructuredData;
34 
35 namespace OpenSim.Framework.Monitoring
36 {
37 // A statistic that wraps a counter.
38 // Built this way mostly so histograms and history can be created.
39 public class CounterStat : Stat
40 {
41  private SortedDictionary<string, EventHistogram> m_histograms;
42  private object counterLock = new object();
43 
44  public CounterStat(
45  string shortName,
46  string name,
47  string description,
48  string unitName,
49  string category,
50  string container,
51  StatVerbosity verbosity)
52  : base(shortName, name, description, unitName, category, container, StatType.Push, null, verbosity)
53  {
54  m_histograms = new SortedDictionary<string, EventHistogram>();
55  }
56 
57  // Histograms are presumably added at intialization time and the list does not change thereafter.
58  // Thus no locking of the histogram list.
59  public void AddHistogram(string histoName, EventHistogram histo)
60  {
61  m_histograms.Add(histoName, histo);
62  }
63 
64  public delegate void ProcessHistogram(string name, EventHistogram histo);
65  public void ForEachHistogram(ProcessHistogram process)
66  {
67  foreach (KeyValuePair<string, EventHistogram> kvp in m_histograms)
68  {
69  process(kvp.Key, kvp.Value);
70  }
71  }
72 
73  public void Event()
74  {
75  this.Event(1);
76  }
77 
78  // Count the underlying counter.
79  public void Event(int cnt)
80  {
81  lock (counterLock)
82  {
83  base.Value += cnt;
84 
85  foreach (EventHistogram histo in m_histograms.Values)
86  {
87  histo.Event(cnt);
88  }
89  }
90  }
91 
92  // CounterStat is a basic stat plus histograms
93  public override OSDMap ToOSDMap()
94  {
95  // Get the foundational instance
96  OSDMap map = base.ToOSDMap();
97 
98  map["StatType"] = "CounterStat";
99 
100  // If there are any histograms, add a new field that is an array of histograms as OSDMaps
101  if (m_histograms.Count > 0)
102  {
103  lock (counterLock)
104  {
105  if (m_histograms.Count > 0)
106  {
107  OSDArray histos = new OSDArray();
108  foreach (EventHistogram histo in m_histograms.Values)
109  {
110  histos.Add(histo.GetHistogramAsOSDMap());
111  }
112  map.Add("Histograms", histos);
113  }
114  }
115  }
116  return map;
117  }
118 }
119 }
CounterStat(string shortName, string name, string description, string unitName, string category, string container, StatVerbosity verbosity)
Definition: CounterStat.cs:44
OpenMetaverse.StructuredData.OSDArray OSDArray
void ForEachHistogram(ProcessHistogram process)
Definition: CounterStat.cs:65
OpenMetaverse.StructuredData.OSDMap OSDMap
Holds individual statistic details
Definition: Stat.cs:41
void AddHistogram(string histoName, EventHistogram histo)
Definition: CounterStat.cs:59
StatVerbosity
Verbosity of stat.