Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.76 KB | None | 0 0
  1. /* -*-c++-*- */
  2. /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
  3.  * Copyright 2008-2012 Pelican Mapping
  4.  * http://osgearth.org
  5.  *
  6.  * osgEarth is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU Lesser General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>
  18.  */
  19.  
  20. #ifndef OSGEARTH_METRICS_H
  21. #define OSGEARTH_METRICS_H 1
  22.  
  23. #include <osgEarth/Common>
  24. #include <osgEarth/Config>
  25. #include <iostream>
  26. #include <osgDB/fstream>
  27.  
  28. namespace osgEarth
  29. {
  30.     /**
  31.      * Interface for a class that handles collecting metrics.
  32.      */
  33.     class OSGEARTH_EXPORT MetricsBackend : public osg::Referenced
  34.     {
  35.     public:
  36.         /**
  37.          * Begins an event.
  38.          * @param name
  39.          *        The name of the event
  40.          * @param args
  41.          *        The arguments to the event.
  42.          */
  43.         virtual void begin(const std::string& name, const Config& args =Config()) = 0;
  44.  
  45.         /**
  46.          * Ends an event
  47.          * @param name
  48.          *        The name of the event.
  49.          * @param args
  50.          *        The arguments to the event.
  51.          */
  52.         virtual void end(const std::string& name, const Config& args =Config()) = 0;
  53.  
  54.         /**
  55.          * A counter event
  56.          * @param graph
  57.          *        The graph to display the counters on.
  58.          * @param name0
  59.          *        The name of the first counter.
  60.          * @param value0
  61.          *        The value of the first counter.
  62.          * @param name1
  63.          *        The name of the second counter.
  64.          * @param value1
  65.          *        The value of the second counter.
  66.          * @param name2
  67.          *        The name of the third counter.
  68.          * @param value2
  69.          *        The value of the third counter.
  70.          */
  71.         virtual void counter(const std::string& graph,
  72.                              const std::string& name0, double value0,
  73.                              const std::string& name1, double value1,
  74.                              const std::string& name2, double value2) = 0;
  75.     };
  76.  
  77.     /**
  78.      * A MetricsProvider that uses the chrome://tracing format as described here: http://www.gamasutra.com/view/news/176420/Indepth_Using_Chrometracing_to_view_your_inline_profiling_data.php
  79.      * https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit?pli=1#
  80.      */
  81.     class OSGEARTH_EXPORT ChromeMetricsBackend : public MetricsBackend
  82.     {
  83.     public:
  84.         ChromeMetricsBackend(const std::string& filename);
  85.         ~ChromeMetricsBackend();
  86.  
  87.         virtual void begin(const std::string& name, const Config& args =Config());
  88.         virtual void end(const std::string& name, const Config& args =Config());
  89.        
  90.  
  91.         /**
  92.          * A counter event
  93.          * @param graph
  94.          *        The graph to display the counters on.
  95.          * @param name0
  96.          *        The name of the first counter.
  97.          * @param value0
  98.          *        The value of the first counter.
  99.          * @param name1
  100.          *        The name of the second counter.
  101.          * @param value1
  102.          *        The value of the second counter.
  103.          * @param name2
  104.          *        The name of the third counter.
  105.          * @param value2
  106.          *        The value of the third counter.
  107.          */
  108.         virtual void counter(const std::string& graph,
  109.                              const std::string& name0, double value0,
  110.                              const std::string& name1, double value1,
  111.                              const std::string& name2, double value2);
  112.  
  113.     protected:
  114.         std::ofstream _metricsFile;
  115.         OpenThreads::Mutex _mutex;    
  116.         bool _firstEvent;
  117.         osg::Timer_t _startTime;
  118.     };
  119.  
  120.     class OSGEARTH_EXPORT Metrics
  121.     {
  122.     public:
  123.         /**
  124.          * Begins an event.
  125.          * @param name
  126.          *        The name of the event
  127.          * @param args
  128.          *        The arguments to the event.
  129.          */
  130.         static void begin(const std::string& name, const Config& args =Config());
  131.  
  132.  
  133.         /**
  134.          * Begins an event with a variable number of arguments
  135.          * @param name
  136.          *        The name of the event
  137.          * @param argCount
  138.          *        The number of argument pairs (key, value) that will
  139.          */
  140.         static void begin(const std::string& name, unsigned int argCount, ...);
  141.  
  142.         /**
  143.          * Ends an event with a variable number of arguments
  144.          * @param name
  145.          *        The name of the event
  146.          * @param argCount
  147.          *        The number of argument pairs (key, value) that will
  148.  
  149.          */
  150.         static void end(const std::string& name, unsigned int argCount, ...);
  151.  
  152.         /**
  153.          * Ends an event
  154.          * @param name
  155.          *        The name of the event.
  156.          * @param args
  157.          *        The arguments to the event.
  158.          */
  159.         static void end(const std::string& name,  const Config& args = Config());
  160.  
  161.         /**
  162.          * A counter event
  163.          * @param graph
  164.          *        The graph to display the counters on.
  165.          * @param name0
  166.          *        The name of the first counter.
  167.          * @param value0
  168.          *        The value of the first counter.        
  169.          */
  170.         static void counter(const std::string& graph,const std::string& name0, double value0);
  171.            
  172.         /**
  173.          * A counter event
  174.          * @param graph
  175.          *        The graph to display the counters on.
  176.          * @param name0
  177.          *        The name of the first counter.
  178.          * @param value0
  179.          *        The value of the first counter.
  180.          * @param name1
  181.          *        The name of the second counter.
  182.          * @param value1
  183.          *        The value of the second counter.
  184.          */
  185.         static void counter(const std::string& graph,const std::string& name0, double value0,
  186.                                                      const std::string& name1, double value1);
  187.  
  188.         /**
  189.          * A counter event
  190.          * @param graph
  191.          *        The graph to display the counters on.
  192.          * @param name0
  193.          *        The name of the first counter.
  194.          * @param value0
  195.          *        The value of the first counter.
  196.          * @param name1
  197.          *        The name of the second counter.
  198.          * @param value1
  199.          *        The value of the second counter.
  200.          * @param name2
  201.          *        The name of the third counter.
  202.          * @param value2
  203.          *        The value of the third counter.
  204.          */
  205.         static void counter(const std::string& graph,const std::string& name0, double value0,
  206.                                                      const std::string& name1, double value1,
  207.                                                      const std::string& name2, double value2);
  208.  
  209.         /**
  210.          * Gets the metrics backend.
  211.          */
  212.         static MetricsBackend* getMetricsBackend();
  213.  
  214.         /*
  215.          * Whether metrics collection is enabled.  Equivalent to checking getMetricsBackend != NULL;
  216.          */
  217.         static bool enabled();
  218.  
  219.         /**
  220.          * Sets the MetricsBackend
  221.          */
  222.         static void setMetricsBackend( MetricsBackend* backend );
  223.  
  224.         static Config encodeArgs(unsigned count, ...);
  225.     };
  226.  
  227.     /**
  228.      * Utility that lets you start and stop metrics with a single line of code
  229.      * @example
  230.      * void myFunction() {
  231.      *     ScopedMetric("myFunction");
  232.      *     ...do some work
  233.      * }
  234.      */
  235.     class OSGEARTH_EXPORT ScopedMetric
  236.     {
  237.     public:
  238.         ScopedMetric(const std::string& name);
  239.         ScopedMetric(const std::string& name, const Config& args);
  240.         ScopedMetric(const std::string& name, int argCount, ...);
  241.         ~ScopedMetric();
  242.         std::string _name;
  243.     };
  244.  
  245. #define METRIC_BEGIN(...) if (Metrics::enabled()) Metrics::begin(__VA_ARGS__)
  246.  
  247. #define METRIC_END(...)   if (Metrics::enabled()) Metrics::end(__VA_ARGS__)
  248.    
  249. #define METRIC_SCOPED(NAME) \
  250.     ScopedMetric scoped_metric__(NAME)
  251.  
  252. #define METRIC_SCOPED_EX(NAME, COUNT, ...) \
  253.     ScopedMetric scoped_metric__(NAME, osgEarth::Metrics::enabled() ? osgEarth::Metrics::encodeArgs(COUNT, __VA_ARGS__) : Config())
  254. };
  255.  
  256. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement