- Timestamp:
- 05/22/15 13:25:59 (10 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/core/logging.hh
r368 r371 17 17 #include <nv/stl/string.hh> 18 18 #include <nv/stl/singleton.hh> 19 #include <sstream>20 21 #include <nv/core/profiler.hh>22 23 24 19 25 20 namespace nv … … 72 67 m_pos += f32_to_buffer( f, m_pos ); 73 68 } 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 }80 69 static bool can_log( log_level level ) 81 70 { 82 71 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 )... ); 83 85 } 84 86 virtual ~logger_base() {} … … 89 91 }; 90 92 91 namespace detail92 {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 117 93 } // 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 }126 94 127 95 #define NV_LOG(level, ...) \ 128 96 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 } 133 100 134 101 #if NV_DEBUG == 1 135 102 #define NV_DEBUG_LOG(level, ...) \ 136 103 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__ ); \ 139 106 } 140 107 #else -
trunk/nv/core/profiler.hh
r368 r371 16 16 #include <nv/core/common.hh> 17 17 #include <nv/stl/singleton.hh> 18 #include <nv/stl/string.hh> 18 19 #include <unordered_map> 19 20 … … 40 41 } 41 42 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 ); 44 45 void start(); 45 46 bool stop(); … … 62 63 ~profiler(); 63 64 64 void start_profile( const char*tag );65 void start_profile( const string_ref& tag ); 65 66 void stop_profile(); 66 67 public: … … 69 70 void log_report(); 70 71 private: 71 void log_node_children( const std::string& ind, const node* n );72 void log_node_children( uint32 indent, const node* n ); 72 73 node* m_root; 73 74 node* m_current; … … 77 78 { 78 79 public: 79 profiler_guard( const char*tag )80 profiler_guard( string_ref tag ) 80 81 { 81 82 profiler::pointer()->start_profile( tag ); -
trunk/src/core/profiler.cc
r365 r371 5 5 #include "nv/core/profiler.hh" 6 6 7 #include <iomanip>8 #include <ios>9 7 #include "nv/core/time.hh" 10 8 #include "nv/core/logging.hh" 9 #include <cstdio> 10 #include <cstdlib> 11 11 12 12 using namespace nv; 13 13 14 #ifdef NV_MSVC 15 #define snprintf sprintf_s 16 #endif 14 17 15 18 profiler::profiler() … … 25 28 } 26 29 27 void profiler::start_profile( const char*tag )30 void profiler::start_profile( const string_ref& tag ) 28 31 { 29 32 if ( tag != m_current->m_tag ) … … 42 45 } 43 46 44 profiler::node::node( const char* tag, node* parent )45 : m_tag( tag )47 profiler::node::node( const string_ref& tag, node* parent ) 48 : m_tag( tag.to_string() ) 46 49 , m_parent( parent ) 47 50 , m_recusion( 0 ) … … 53 56 } 54 57 55 profiler::node* profiler::node::request_child( const char*tag )58 profiler::node* profiler::node::request_child( const string_ref& tag ) 56 59 { 57 auto it = m_children.find( tag ); 60 std::string stag( tag.to_string() ); 61 auto it = m_children.find( stag ); 58 62 if ( it != m_children.end() ) 59 63 return it->second; … … 61 65 { 62 66 node* result = new node( tag, this ); 63 m_children[ tag ] = result;67 m_children[ stag ] = result; 64 68 return result; 65 69 } … … 101 105 { 102 106 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 ---------------------------------" ); 111 113 m_root->start(); 112 114 } 113 115 114 void profiler::log_node_children( const std::string& ind, const node* n )116 void profiler::log_node_children( uint32 indent, const node* n ) 115 117 { 118 char buffer[128]; 116 119 for ( const auto& pair : n->m_children ) 117 120 { … … 119 122 if ( c->m_calls > 0 ) 120 123 { 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 ) ) ); 127 132 if ( c->m_children.size() > 0 ) 128 133 { 129 log_node_children( ind + "-", c );134 log_node_children( indent + 1, c ); 130 135 } 131 136 }
Note: See TracChangeset
for help on using the changeset viewer.