Metric.cpp revision 9de4dec874148d30cc1d4c498d38cd048a8164ca
1//===-- Metric.cpp ----------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Metric.h"
11
12#include "CFCMutableArray.h"
13#include "CFCMutableDictionary.h"
14#include "CFCString.h"
15#include "MemoryGauge.h"
16
17using namespace lldb_perf;
18
19template <class T>
20Metric<T>::Metric () : Metric ("")
21{
22}
23
24template <class T>
25Metric<T>::Metric (const char* n, const char* d) :
26    m_name(n ? n : ""),
27    m_description(d ? d : ""),
28    m_dataset ()
29{
30}
31
32template <class T>
33void
34Metric<T>::Append (T v)
35{
36    m_dataset.push_back(v);
37}
38
39template <class T>
40size_t
41Metric<T>::GetCount () const
42{
43    return m_dataset.size();
44}
45
46template <class T>
47T
48Metric<T>::GetSum () const
49{
50    T sum = 0;
51    for (auto v : m_dataset)
52        sum += v;
53    return sum;
54}
55
56template <class T>
57T
58Metric<T>::GetAverage () const
59{
60    return GetSum()/GetCount();
61}
62
63template <>
64void Metric<double>::WriteImpl (CFCMutableDictionary& parent_dict, const char *name, const char *description, double value)
65{
66    assert(name && name[0]);
67    CFCMutableDictionary dict;
68    if (description && description[0])
69        dict.AddValueCString(CFCString("description").get(),description, true);
70    dict.AddValueDouble(CFCString("value").get(),value, true);
71    parent_dict.AddValue(CFCString(name).get(), dict.get(), true);
72}
73
74template <>
75void Metric<MemoryStats>::WriteImpl (CFCMutableDictionary& parent_dict, const char *name, const char *description, MemoryStats value)
76{
77    CFCMutableDictionary dict;
78    if (description && description[0])
79        dict.AddValueCString(CFCString("description").get(),description, true);
80    CFCMutableDictionary value_dict;
81    // don't write out the "virtual size", it doesn't mean anything useful as it includes
82    // all of the shared cache and many other things that make it way too big to be useful
83    //value_dict.AddValueUInt64(CFCString("virtual").get(), value.GetVirtualSize(), true);
84    value_dict.AddValueUInt64(CFCString("resident").get(), value.GetResidentSize(), true);
85    value_dict.AddValueUInt64(CFCString("max_resident").get(), value.GetMaxResidentSize(), true);
86
87    dict.AddValue(CFCString("value").get(),value_dict.get(), true);
88
89    parent_dict.AddValue(CFCString(name).get(), dict.get(), true);
90}
91
92template class lldb_perf::Metric<double>;
93template class lldb_perf::Metric<MemoryStats>;
94