Changeset 371 for trunk


Ignore:
Timestamp:
05/22/15 13:25:59 (10 years ago)
Author:
epyon
Message:
  • profiler - removed sstream dependency - formating via snprintf
  • profiler - string_refs where applicable (still need string for unordered_map)
  • logger - removed sstream support
  • logger - log_sequence instead of detail logger functions
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/logging.hh

    r368 r371  
    1717#include <nv/stl/string.hh>
    1818#include <nv/stl/singleton.hh>
    19 #include <sstream>
    20 
    21 #include <nv/core/profiler.hh>
    22 
    23 
    2419
    2520namespace nv
     
    7267                        m_pos += f32_to_buffer( f, m_pos );
    7368                }
    74                 void log( log_level level )
    75                 {
    76                         *m_pos = '\0';
    77                         log( level, string_ref( m_message, (size_t) ( m_pos - m_message ) ) );
    78                         m_pos = m_message;
    79                 }
    8069                static bool can_log( log_level level )
    8170                {
    8271                        return logger_base::is_valid() && (unsigned int)reference().get_level() >= (unsigned int)level;
     72                }
     73
     74                void log_sequence( log_level level )
     75                {
     76                        *m_pos = '\0';
     77                        log( level, string_ref( m_message, (size_t)( m_pos - m_message ) ) );
     78                        m_pos = m_message;
     79                }
     80                template < typename T, typename... Args >
     81                inline void log_sequence( log_level level, T&& t, Args&&... args )
     82                {
     83                        log_append( t );
     84                        log_sequence( level, std::forward<Args>( args )... );
    8385                }
    8486                virtual ~logger_base() {}
     
    8991        };
    9092
    91         namespace detail
    92         {
    93                 inline void streamer( std::stringstream& )
    94                 {
    95                         // noop;
    96                 }
    97 
    98                 template < typename T, typename... Args >
    99                 inline void streamer( std::stringstream& ss, T&& t, Args&&... args )
    100                 {
    101                         ss << t;
    102                         streamer( ss, std::forward<Args>(args)... );
    103                 }
    104                 inline void logger( log_level level, logger_base& base )
    105                 {
    106                         base.log( level );
    107                 }
    108 
    109                 template < typename T, typename... Args >
    110                 inline void logger( log_level level, logger_base& base, T&& t, Args&&... args )
    111                 {
    112                         base.log_append( t );
    113                         logger( level, base, std::forward<Args>( args )... );
    114                 }
    115         }
    116 
    11793} // namespace nv
    118 
    119 #define NV_LOG_STREAM(level, message_stream) \
    120         if ( nv::logger_base::can_log(level) ) \
    121         {       \
    122                 std::stringstream ss; \
    123                 ss << message_stream;\
    124                 nv::logger_base::reference().log( level, string_ref( ss.str() ) ); \
    125         }
    12694
    12795#define NV_LOG(level, ...) \
    12896        if ( nv::logger_base::can_log(level) ) \
    129                                                                                 {       \
    130                 NV_PROFILE( "logging" );\
    131                 nv::detail::logger( level, nv::logger_base::reference(), __VA_ARGS__ ); \
    132                                 }
     97        {\
     98                nv::logger_base::reference().log_sequence( level, __VA_ARGS__ ); \
     99        }
    133100
    134101#if NV_DEBUG == 1
    135102#define NV_DEBUG_LOG(level, ...) \
    136103        if ( nv::logger_base::can_log(level) ) \
    137         {       \
    138                 nv::detail::logger( level, nv::logger_base::reference(), __VA_ARGS__ ); \
     104        {\
     105                nv::logger_base::reference().log_sequence( level, __VA_ARGS__ ); \
    139106        }
    140107#else
  • trunk/nv/core/profiler.hh

    r368 r371  
    1616#include <nv/core/common.hh>
    1717#include <nv/stl/singleton.hh>
     18#include <nv/stl/string.hh>
    1819#include <unordered_map>
    1920
     
    4041                        }
    4142                protected:
    42                         node( const char* tag, node* parent );
    43                         node* request_child( const char* tag );
     43                        node( const string_ref& tag, node* parent );
     44                        node* request_child( const string_ref& tag );
    4445                        void start();
    4546                        bool stop();
     
    6263                ~profiler();
    6364
    64                 void start_profile( const char* tag );
     65                void start_profile( const string_ref& tag );
    6566                void stop_profile();
    6667        public:
     
    6970                void log_report();
    7071        private:
    71                 void log_node_children( const std::string& ind, const node* n );
     72                void log_node_children( uint32 indent, const node* n );
    7273                node* m_root;
    7374                node* m_current;
     
    7778        {
    7879        public:
    79                 profiler_guard( const char* tag )
     80                profiler_guard( string_ref tag )
    8081                {
    8182                        profiler::pointer()->start_profile( tag );
  • trunk/src/core/profiler.cc

    r365 r371  
    55#include "nv/core/profiler.hh"
    66
    7 #include <iomanip>
    8 #include <ios>
    97#include "nv/core/time.hh"
    108#include "nv/core/logging.hh"
     9#include <cstdio>
     10#include <cstdlib>
    1111
    1212using namespace nv;
    1313
     14#ifdef NV_MSVC
     15#define snprintf sprintf_s
     16#endif
    1417
    1518profiler::profiler()
     
    2528}
    2629
    27 void profiler::start_profile( const char* tag )
     30void profiler::start_profile( const string_ref& tag )
    2831{
    2932        if ( tag != m_current->m_tag )
     
    4245}
    4346
    44 profiler::node::node( const char* tag, node* parent )
    45         : m_tag( tag )
     47profiler::node::node( const string_ref& tag, node* parent )
     48        : m_tag( tag.to_string() )
    4649        , m_parent( parent )
    4750        , m_recusion( 0 )
     
    5356}
    5457
    55 profiler::node* profiler::node::request_child( const char* tag )
     58profiler::node* profiler::node::request_child( const string_ref& tag )
    5659{
    57         auto it = m_children.find( tag );
     60        std::string stag( tag.to_string() );
     61        auto it = m_children.find( stag );
    5862        if ( it != m_children.end() )
    5963                return it->second;
     
    6165        {
    6266                node* result = new node( tag, this );
    63                 m_children[ tag ] = result;
     67                m_children[ stag ] = result;
    6468                return result;
    6569        }
     
    101105{
    102106        m_root->stop();
    103         NV_LOG_INFO( "-- PROFILER REPORT -----------------------------------------" );
    104         NV_LOG_STREAM( LOG_INFO, std::left << std::setw(24) << "TAG"
    105                 << std::setw(7) << "%PARNT"
    106                 << std::setw(7) << "CALLS"
    107                 << std::setw(10) << "TOTAL(ms)"
    108                 << std::setw(10) << "AVG(ms)" );
    109         log_node_children( "", m_root );
    110         NV_LOG_INFO( "-- PROFILER REPORT END -------------------------------------" );
     107        NV_LOG_INFO( "-- PROFILER REPORT -------------------------------------" );
     108        char buffer[128];
     109        snprintf( buffer, 128, "%-23s %6s %6s %9s %6s", "TAG", "%PARNT", "CALLS", "TOTAL(ms)", "AVG(ms)" );
     110        NV_LOG_INFO( string_ref( buffer, strlen(buffer) ) );
     111        log_node_children( 0, m_root );
     112        NV_LOG_INFO( "-- PROFILER REPORT END ---------------------------------" );
    111113        m_root->start();
    112114}
    113115
    114 void profiler::log_node_children( const std::string& ind, const node* n )
     116void profiler::log_node_children( uint32 indent, const node* n )
    115117{
     118        char buffer[128];
    116119        for ( const auto& pair : n->m_children )
    117120        {
     
    119122                if ( c->m_calls > 0 )
    120123                {
    121                         NV_LOG_STREAM( LOG_INFO, std::left << std::setw(24) << ind + c->m_tag
    122                                 << std::setw(7) << std::setprecision(2) << std::fixed << ( (double)c->m_total_time_us / (double)c->m_parent->m_total_time_us ) * 100.0
    123                                 << std::setw(7) << c->m_calls
    124                                 << std::setw(10) << std::setprecision(2) << std::fixed << c->m_total_time_us / 1000.f
    125                                 << std::setw(10) << std::setprecision(2) << std::fixed << ( (double)c->m_total_time_us / (double)c->m_calls ) / 1000.f
    126                                 );
     124                        double pparent  = ( (double)c->m_total_time_us / (double)c->m_parent->m_total_time_us ) * 100.f;
     125                        int calls       = c->m_calls;
     126                        double total_ms = c->m_total_time_us / 1000.f;
     127                        double avg_ms   = ( (double)c->m_total_time_us / (double)c->m_calls ) / 1000.f;
     128                        if ( indent > 0 ) memset( buffer, '-', indent );
     129                        snprintf( buffer + indent, 128 - indent, "%*.*s %6.2f %6d %9.2f %6.2f", indent - 23, 23 - indent,
     130                                c->m_tag.c_str(), pparent, calls, total_ms, avg_ms );
     131                        NV_LOG_INFO( string_ref( buffer, strlen( buffer ) ) );
    127132                        if ( c->m_children.size() > 0 )
    128133                        {
    129                                 log_node_children( ind + "-", c );
     134                                log_node_children( indent + 1, c );
    130135                        }
    131136                }
Note: See TracChangeset for help on using the changeset viewer.