1/*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andrew Bardsley
38 *          Matthias Jung
39 *          Abdul Mutaal Ahmad
40 */
41
42/**
43 * @file
44 *
45 *  C++-only configuration stats handling example
46 *
47 *  Register with: Stats::registerHandlers(statsReset, statsDump)
48 */
49
50#include "base/output.hh"
51#include "base/statistics.hh"
52#include "base/stats/text.hh"
53#include "stats.hh"
54
55namespace CxxConfig
56{
57
58void statsPrepare()
59{
60    std::list<Stats::Info *> stats = Stats::statsList();
61
62    /* gather_stats -> prepare */
63    for (auto i = stats.begin(); i != stats.end(); ++i){
64        Stats::Info *stat = *i;
65        Stats::VectorInfo *vector = dynamic_cast<Stats::VectorInfo *>(stat);
66        if (vector){
67            (dynamic_cast<Stats::VectorInfo *>(*i))->prepare();
68        }
69        else {
70            (*i)->prepare();
71        }
72
73    }
74}
75
76void statsDump()
77{
78    bool desc = true;
79    Stats::Output *output = Stats::initText(filename, desc);
80
81    Stats::processDumpQueue();
82
83    std::list<Stats::Info *> stats = Stats::statsList();
84
85    statsEnable();
86    statsPrepare();
87
88    output->begin();
89    /* gather_stats -> convert_value */
90    for (auto i = stats.begin(); i != stats.end(); ++i) {
91        Stats::Info *stat = *i;
92
93        const Stats::ScalarInfo *scalar = dynamic_cast<Stats::ScalarInfo
94            *>(stat);
95        Stats::VectorInfo *vector = dynamic_cast<Stats::VectorInfo *>(stat);
96        const Stats::Vector2dInfo *vector2d = dynamic_cast<Stats::Vector2dInfo
97            *>(vector);
98        const Stats::DistInfo *dist = dynamic_cast<Stats::DistInfo *>(stat);
99        const Stats::VectorDistInfo *vectordist =
100            dynamic_cast<Stats::VectorDistInfo *>(stat);
101        const Stats::SparseHistInfo *sparse =
102            dynamic_cast<Stats::SparseHistInfo *>(stat);
103        const Stats::InfoProxy <Stats::Vector2d,Stats::Vector2dInfo> *info =
104            dynamic_cast<Stats::InfoProxy
105            <Stats::Vector2d,Stats::Vector2dInfo>*>(stat);
106
107        if (vector) {
108            const Stats::FormulaInfo *formula = dynamic_cast<Stats::FormulaInfo
109                *>(vector);
110            if (formula){
111                output->visit(*formula);
112            } else {
113                const Stats::VectorInfo *vector1 = vector;
114                output->visit(*vector1);
115            }
116        } else if (vector2d) {
117            output->visit(*vector2d);
118        } else if (info){
119            output->visit(*info);
120        } else if (vectordist){
121            output->visit(*vectordist);
122        } else if (dist) {
123            output->visit(*dist);
124        } else if (sparse) {
125            output->visit(*sparse);
126        } else if (scalar) {
127            output->visit(*scalar);
128        } else {
129            warn("Stat not dumped: %s\n", stat->name);
130        }
131    }
132    output->end();
133}
134
135void statsReset()
136{
137    std::cerr << "Stats reset\n";
138
139    Stats::processResetQueue();
140}
141
142void statsEnable()
143{
144    std::list<Stats::Info *> stats = Stats::statsList();
145
146    for (auto i = stats.begin(); i != stats.end(); ++i){
147        Stats::Info *stat = *i;
148        Stats::VectorInfo *vector = dynamic_cast<Stats::VectorInfo *>(stat);
149        if (vector){
150            (dynamic_cast<Stats::VectorInfo *>(*i))->enable();
151        }
152        else {
153            (*i)->enable();
154        }
155
156    }
157}
158
159}
160