113372Sgabeblack@google.com/*
213372Sgabeblack@google.com * Copyright (c) 2014 ARM Limited
313372Sgabeblack@google.com * All rights reserved
413372Sgabeblack@google.com *
513372Sgabeblack@google.com * The license below extends only to copyright in the software and shall
613372Sgabeblack@google.com * not be construed as granting a license to any other intellectual
713372Sgabeblack@google.com * property including but not limited to intellectual property relating
813372Sgabeblack@google.com * to a hardware implementation of the functionality of the software
913372Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1013372Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1113372Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1213372Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1313372Sgabeblack@google.com *
1413372Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1513372Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1613372Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
1713372Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1813372Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1913372Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2013372Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2113372Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2213372Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2313372Sgabeblack@google.com * this software without specific prior written permission.
2413372Sgabeblack@google.com *
2513372Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2613372Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2713372Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2813372Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2913372Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3013372Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3113372Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3213372Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3313372Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3413372Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3513372Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3613372Sgabeblack@google.com *
3713372Sgabeblack@google.com * Authors: Andrew Bardsley
3813372Sgabeblack@google.com *          Matthias Jung
3913372Sgabeblack@google.com *          Abdul Mutaal Ahmad
4013372Sgabeblack@google.com */
4113372Sgabeblack@google.com
4213372Sgabeblack@google.com/**
4313372Sgabeblack@google.com * @file
4413372Sgabeblack@google.com *
4513372Sgabeblack@google.com *  C++-only configuration stats handling example
4613372Sgabeblack@google.com *
4713372Sgabeblack@google.com *  Register with: Stats::registerHandlers(statsReset, statsDump)
4813372Sgabeblack@google.com */
4913372Sgabeblack@google.com
5013372Sgabeblack@google.com#include "base/output.hh"
5113372Sgabeblack@google.com#include "base/statistics.hh"
5213372Sgabeblack@google.com#include "base/stats/text.hh"
5313372Sgabeblack@google.com#include "stats.hh"
5413372Sgabeblack@google.com
5513372Sgabeblack@google.comnamespace CxxConfig
5613372Sgabeblack@google.com{
5713372Sgabeblack@google.com
5813372Sgabeblack@google.comvoid statsPrepare()
5913372Sgabeblack@google.com{
6013372Sgabeblack@google.com    std::list<Stats::Info *> stats = Stats::statsList();
6113372Sgabeblack@google.com
6213372Sgabeblack@google.com    /* gather_stats -> prepare */
6313372Sgabeblack@google.com    for (auto i = stats.begin(); i != stats.end(); ++i){
6413372Sgabeblack@google.com        Stats::Info *stat = *i;
6513372Sgabeblack@google.com        Stats::VectorInfo *vector = dynamic_cast<Stats::VectorInfo *>(stat);
6613372Sgabeblack@google.com        if (vector){
6713372Sgabeblack@google.com            (dynamic_cast<Stats::VectorInfo *>(*i))->prepare();
6813372Sgabeblack@google.com        }
6913372Sgabeblack@google.com        else {
7013372Sgabeblack@google.com            (*i)->prepare();
7113372Sgabeblack@google.com        }
7213372Sgabeblack@google.com
7313372Sgabeblack@google.com    }
7413372Sgabeblack@google.com}
7513372Sgabeblack@google.com
7613372Sgabeblack@google.comvoid statsDump()
7713372Sgabeblack@google.com{
7813372Sgabeblack@google.com    bool desc = true;
7913372Sgabeblack@google.com    Stats::Output *output = Stats::initText(filename, desc);
8013372Sgabeblack@google.com
8113372Sgabeblack@google.com    Stats::processDumpQueue();
8213372Sgabeblack@google.com
8313372Sgabeblack@google.com    std::list<Stats::Info *> stats = Stats::statsList();
8413372Sgabeblack@google.com
8513372Sgabeblack@google.com    statsEnable();
8613372Sgabeblack@google.com    statsPrepare();
8713372Sgabeblack@google.com
8813372Sgabeblack@google.com    output->begin();
8913372Sgabeblack@google.com    /* gather_stats -> convert_value */
9013372Sgabeblack@google.com    for (auto i = stats.begin(); i != stats.end(); ++i) {
9113372Sgabeblack@google.com        Stats::Info *stat = *i;
9213372Sgabeblack@google.com
9313372Sgabeblack@google.com        const Stats::ScalarInfo *scalar = dynamic_cast<Stats::ScalarInfo
9413372Sgabeblack@google.com            *>(stat);
9513372Sgabeblack@google.com        Stats::VectorInfo *vector = dynamic_cast<Stats::VectorInfo *>(stat);
9613372Sgabeblack@google.com        const Stats::Vector2dInfo *vector2d = dynamic_cast<Stats::Vector2dInfo
9713372Sgabeblack@google.com            *>(vector);
9813372Sgabeblack@google.com        const Stats::DistInfo *dist = dynamic_cast<Stats::DistInfo *>(stat);
9913372Sgabeblack@google.com        const Stats::VectorDistInfo *vectordist =
10013372Sgabeblack@google.com            dynamic_cast<Stats::VectorDistInfo *>(stat);
10113372Sgabeblack@google.com        const Stats::SparseHistInfo *sparse =
10213372Sgabeblack@google.com            dynamic_cast<Stats::SparseHistInfo *>(stat);
10313372Sgabeblack@google.com        const Stats::InfoProxy <Stats::Vector2d,Stats::Vector2dInfo> *info =
10413372Sgabeblack@google.com            dynamic_cast<Stats::InfoProxy
10513372Sgabeblack@google.com            <Stats::Vector2d,Stats::Vector2dInfo>*>(stat);
10613372Sgabeblack@google.com
10713372Sgabeblack@google.com        if (vector) {
10813372Sgabeblack@google.com            const Stats::FormulaInfo *formula = dynamic_cast<Stats::FormulaInfo
10913372Sgabeblack@google.com                *>(vector);
11013372Sgabeblack@google.com            if (formula){
11113372Sgabeblack@google.com                output->visit(*formula);
11213372Sgabeblack@google.com            } else {
11313372Sgabeblack@google.com                const Stats::VectorInfo *vector1 = vector;
11413372Sgabeblack@google.com                output->visit(*vector1);
11513372Sgabeblack@google.com            }
11613372Sgabeblack@google.com        } else if (vector2d) {
11713372Sgabeblack@google.com            output->visit(*vector2d);
11813372Sgabeblack@google.com        } else if (info){
11913372Sgabeblack@google.com            output->visit(*info);
12013372Sgabeblack@google.com        } else if (vectordist){
12113372Sgabeblack@google.com            output->visit(*vectordist);
12213372Sgabeblack@google.com        } else if (dist) {
12313372Sgabeblack@google.com            output->visit(*dist);
12413372Sgabeblack@google.com        } else if (sparse) {
12513372Sgabeblack@google.com            output->visit(*sparse);
12613372Sgabeblack@google.com        } else if (scalar) {
12713372Sgabeblack@google.com            output->visit(*scalar);
12813372Sgabeblack@google.com        } else {
12913372Sgabeblack@google.com            warn("Stat not dumped: %s\n", stat->name);
13013372Sgabeblack@google.com        }
13113372Sgabeblack@google.com    }
13213372Sgabeblack@google.com    output->end();
13313372Sgabeblack@google.com}
13413372Sgabeblack@google.com
13513372Sgabeblack@google.comvoid statsReset()
13613372Sgabeblack@google.com{
13713372Sgabeblack@google.com    std::cerr << "Stats reset\n";
13813372Sgabeblack@google.com
13913372Sgabeblack@google.com    Stats::processResetQueue();
14013372Sgabeblack@google.com}
14113372Sgabeblack@google.com
14213372Sgabeblack@google.comvoid statsEnable()
14313372Sgabeblack@google.com{
14413372Sgabeblack@google.com    std::list<Stats::Info *> stats = Stats::statsList();
14513372Sgabeblack@google.com
14613372Sgabeblack@google.com    for (auto i = stats.begin(); i != stats.end(); ++i){
14713372Sgabeblack@google.com        Stats::Info *stat = *i;
14813372Sgabeblack@google.com        Stats::VectorInfo *vector = dynamic_cast<Stats::VectorInfo *>(stat);
14913372Sgabeblack@google.com        if (vector){
15013372Sgabeblack@google.com            (dynamic_cast<Stats::VectorInfo *>(*i))->enable();
15113372Sgabeblack@google.com        }
15213372Sgabeblack@google.com        else {
15313372Sgabeblack@google.com            (*i)->enable();
15413372Sgabeblack@google.com        }
15513372Sgabeblack@google.com
15613372Sgabeblack@google.com    }
15713372Sgabeblack@google.com}
15813372Sgabeblack@google.com
15913372Sgabeblack@google.com}
160