📄 profile.cc
字号:
////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao// You may distribute under the terms of the Artistic License.//// Profile.cc - Profiling tools// $Id: Profile.cc,v 1.7 2003/05/16 09:32:42 yusuke Exp $////////////////////////////////////////////////////////////////////////#include <amis/configure.h>#include <amis/Profile.h>#include <amis/StringStream.h>#ifdef HAVE_UNISTD_H#include <unistd.h>#endif // HAVE_UNISTD_H#include <algorithm>#include <iostream>AMIS_NAMESPACE_BEGINusing namespace std;//////////////////////////////////////////////////////////////////////const long ProfUnit::PROFILE_INTERVAL = 10000;void Profile::signalHandler( int ) { if ( current_profile != NULL ) { current_profile->incTick(); }}//////////////////////////////////////////////////////////////////////int Profile::profile_enabled = 0;hash_map< const char*, ProfUnit*, profname_hash > Profile::unit_table;Profile* Profile::current_profile = NULL;struct sigaction Profile::old_action;struct itimerval Profile::old_timer;void Profile::initializeProfile( void ) { struct sigaction action; action.sa_handler = signalHandler; sigemptyset( &action.sa_mask ); action.sa_flags = SA_RESTART; ::sigaction( SIGPROF, &action, &old_action ); struct itimerval timer; timer.it_value.tv_sec = 0; timer.it_value.tv_usec = ProfUnit::PROFILE_INTERVAL; timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = ProfUnit::PROFILE_INTERVAL; setitimer( ITIMER_PROF, &timer, &old_timer );}void Profile::finalizeProfile( void ) { setitimer( ITIMER_PROF, &old_timer, NULL ); ::sigaction( SIGPROF, &old_action, NULL ); for ( hash_map< const char*, ProfUnit*, profname_hash >::iterator it = unit_table.begin(); it != unit_table.end(); ++it ) { delete it->second; }}string Profile::profileResult( void ) { OStringStream os; if ( ! unit_table.empty() ) { os << "Profiling result:\n"; os << "Pass Count Total sec. Child sec. Block\n"; vector< const ProfUnit* > unit_vector( unit_table.size() ); hash_map< const char*, ProfUnit*, profname_hash >::const_iterator hash_it = unit_table.begin(); for ( vector< const ProfUnit* >::iterator it = unit_vector.begin(); it != unit_vector.end(); ++it, ++hash_it ) { *it = hash_it->second; } std::stable_sort( unit_vector.begin(), unit_vector.end(), CompTick() ); for ( vector< const ProfUnit* >::const_iterator unit = unit_vector.begin(); unit != unit_vector.end(); ++unit ) { os << setw( 10 ) << (*unit)->passCount() << " " << (*unit)->totalTickString() << " " << (*unit)->childTickString() << " " << (*unit)->getName() << '\n'; } os << "Call graph:\n"; for ( vector< const ProfUnit* >::const_iterator it = unit_vector.begin(); it != unit_vector.end(); ++it ) { if ( ! (*it)->callGraph().empty() ) { os << (*it)->getName() << '\n'; os << " Pass Count Total sec. Block\n"; os << "* " << setw( 10 ) << (*it)->passCount() << " " << (*it)->totalTickString() << " " << (*it)->getName() << '\n'; for ( hash_map< const ProfUnit*, ProfTick, profunit_hash >::const_iterator child = (*it)->callGraph().begin(); child != (*it)->callGraph().end(); ++child ) { os << " " << setw( 10 ) << child->first->passCount() << " " << child->first->tickString( child->second ) << " " << child->first->getName() << '\n'; } } } } return os.str();}//////////////////////////////////////////////////////////////////////void ProfTimer::initProf() { (void)gettimeofday( &start_time, NULL ); (void)getrusage( RUSAGE_SELF, &cpu_usage );}void ProfTimer::startProf( void ) { (void)gettimeofday( &start_time, NULL ); (void)getrusage( RUSAGE_SELF, &cpu_usage );}void ProfTimer::stopProf( void ) { timeval current_time; (void)gettimeofday( ¤t_time, NULL ); real_sec = current_time.tv_sec - start_time.tv_sec; real_usec = current_time.tv_usec - start_time.tv_usec; rusage current_usage; (void)getrusage( RUSAGE_SELF, ¤t_usage ); user_sec = current_usage.ru_utime.tv_sec - cpu_usage.ru_utime.tv_sec; user_usec = current_usage.ru_utime.tv_usec - cpu_usage.ru_utime.tv_usec; system_sec = current_usage.ru_stime.tv_sec - cpu_usage.ru_stime.tv_sec; system_usec = current_usage.ru_stime.tv_usec - cpu_usage.ru_stime.tv_usec;}string ProfTimer::timeToStr( long sec, long usec ) const { OStringStream os; if ( usec < 0 ) { --sec; usec += 1000000; } if ( sec > 60 ) { long min = sec / 60; sec -= min * 60; if ( min > 60 ) { long hour = min / 60; min -= hour * 60; os << hour << " hour "; } os << min << " min "; } os << sec << " sec " << usec; return os.str();}AMIS_NAMESPACE_END// end of Profile.cc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -