39 #include <sys/ptrace.h> 40 #include <sys/resource.h> 42 #include <sys/types.h> 58 #define MAX_EVT_NAME_LEN 256 59 #define NUM_PMCS PFMLIB_MAX_PMCS 60 #define NUM_PMDS PFMLIB_MAX_PMDS 61 #define FMT_NAME PFM_DFL_SMPL_NAME 62 #define BPL ( sizeof( uint64_t ) << 3 ) 65 #define SYM_NAME_MAX_LENGTH 10000 66 #define MAX_OUTPUT_FILENAME_LENGTH 1024 67 #define MAX_EVENT_NAME_LENGTH 500 68 #define MAX_PREFIX_NAME_LENGTH 1024 69 #define FILENAME_MAX_LENGTH 1024 71 #define MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS 4 73 #define cpuid( func, ax, bx, cx, dx ) \ 74 __asm__ __volatile__( "cpuid" : "=a"( ax ), "=b"( bx ), "=c"( cx ), "=d"( dx ) : "a"( func ) ); 77 static uint64_t collected_samples, collected_partial;
81 static size_t entry_size;
82 static unsigned int num_smpl_pmds;
98 inline T function_cast(
void* p ) {
104 return caster.function;
109 typedef void ( *pfm_stop_t )( int );
111 typedef void ( *pfm_self_stop_t )( int );
112 pfm_self_stop_t pfm_self_stop;
113 typedef os_err_t ( *pfm_restart_t )( int );
115 typedef int ( *pfm_read_pmds_t )( int,
pfarg_pmd_t*, int );
117 typedef pfm_err_t ( *pfm_initialize_t )();
127 typedef os_err_t ( *pfm_write_pmds_t )( int, pfarg_pmd_t*, int );
133 typedef char* ( *pfm_strerror_t )( int );
137 typedef pfm_err_t ( *pfm_get_num_counters_t )(
unsigned int* );
139 static PFMon& instance() {
return s_instance; }
147 handle = dlopen(
"libpfm.so", RTLD_NOW );
154 pfm_start = function_cast<pfm_start_t>( dlsym( handle,
"pfm_start" ) );
155 pfm_stop = function_cast<pfm_stop_t>( dlsym( handle,
"pfm_stop" ) );
156 pfm_self_stop = function_cast<pfm_self_stop_t>( dlsym( handle,
"pfm_stop" ) );
157 pfm_restart = function_cast<pfm_restart_t>( dlsym( handle,
"pfm_restart" ) );
158 pfm_read_pmds = function_cast<pfm_read_pmds_t>( dlsym( handle,
"pfm_read_pmds" ) );
159 pfm_initialize = function_cast<pfm_initialize_t>( dlsym( handle,
"pfm_initialize" ) );
160 pfm_find_full_event = function_cast<pfm_find_full_event_t>( dlsym( handle,
"pfm_find_full_event" ) );
161 pfm_dispatch_events = function_cast<pfm_dispatch_events_t>( dlsym( handle,
"pfm_dispatch_events" ) );
162 pfm_create_context = function_cast<pfm_create_context_t>( dlsym( handle,
"pfm_create_context" ) );
163 pfm_write_pmcs = function_cast<pfm_write_pmcs_t>( dlsym( handle,
"pfm_write_pmcs" ) );
164 pfm_write_pmds = function_cast<pfm_write_pmds_t>( dlsym( handle,
"pfm_write_pmds" ) );
165 pfm_load_context = function_cast<pfm_load_context_t>( dlsym( handle,
"pfm_load_context" ) );
166 pfm_strerror = function_cast<pfm_strerror_t>( dlsym( handle,
"pfm_strerror" ) );
167 pfm_set_options = function_cast<pfm_set_options_t>( dlsym( handle,
"pfm_set_options" ) );
168 pfm_get_num_counters = function_cast<pfm_get_num_counters_t>( dlsym( handle,
"pfm_get_num_counters" ) );
176 if ( handle ) dlclose( handle );
179 static PFMon s_instance;
182 PFMon PFMon::s_instance;
217 # pragma warning( push ) 218 # pragma warning( disable : 593 ) 221 cpuid( 1, a, b, c, d );
222 int sse4_2_mask = 1 << 20;
223 if ( c & sse4_2_mask )
228 # pragma warning( pop ) 274 , m_pfm( PFMon::instance() )
277 , m_inEvent( false ) {
392 return bv[rnum >>
LBPL] & ( 1UL << ( rnum & (
BPL - 1 ) ) ) ? 1 : 0;
402 static void sigio_handler(
int, siginfo_t*,
void* );
406 void finalize_smpl();
422 memset( &ctx, 0,
sizeof( ctx ) );
423 memset( &inp, 0,
sizeof( inp ) );
424 memset( &outp, 0,
sizeof( outp ) );
425 memset( pd, 0,
sizeof( pd ) );
426 memset(
pc, 0,
sizeof(
pc ) );
427 memset( &load_arg, 0,
sizeof( load_arg ) );
428 memset( ¶ms, 0,
sizeof( params ) );
429 memset( &nhm_params, 0,
sizeof( nhm_params ) );
431 for (
int i = 0; i < used_counters_number; i++ ) {
432 ret = m_pfm.pfm_find_full_event( event_cstr[i], &inp.pfp_events[i] );
434 error() <<
"ERROR: cannot find event: " << event_cstr[i] <<
". Aborting..." <<
endmsg;
438 inp.pfp_event_count = 4;
439 for (
int i = 0; i < used_counters_number; i++ ) {
444 if ( cmask[i] > 0 ) {
445 ( params.pfp_core_counters[i] ).cnt_mask = cmask[i];
446 ( nhm_params.pfp_nhm_counters[i] ).cnt_mask = cmask[i];
449 if ( nehalem || westmere ) {
450 ret = m_pfm.pfm_dispatch_events( &inp, &nhm_params, &outp, NULL );
452 ret = m_pfm.pfm_dispatch_events( &inp, ¶ms, &outp, NULL );
455 error() <<
"ERROR: cannot dispatch events: " << m_pfm.pfm_strerror( ret ) <<
". Aborting..." <<
endmsg;
457 for (
unsigned int i = 0; i < outp.pfp_pmc_count; i++ ) {
458 pc[i].reg_num = outp.pfp_pmcs[i].reg_num;
459 pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
461 for (
unsigned int i = 0; i < outp.pfp_pmd_count; i++ ) {
462 pd[i].reg_num = outp.pfp_pmds[i].reg_num;
465 fd = m_pfm.pfm_create_context( &ctx, NULL, 0, 0 );
466 if ( fd == -1 ) { error() <<
"ERROR: Context not created. Aborting..." <<
endmsg; }
467 if ( m_pfm.pfm_write_pmcs( fd,
pc, outp.pfp_pmc_count ) == -1 ) {
468 error() <<
"ERROR: Could not write pmcs. Aborting..." <<
endmsg;
470 if ( m_pfm.pfm_write_pmds( fd, pd, outp.pfp_pmd_count ) == -1 ) {
471 error() <<
"ERROR: Could not write pmds. Aborting..." <<
endmsg;
473 load_arg.load_pid = getpid();
474 if ( m_pfm.pfm_load_context( fd, &load_arg ) == -1 ) {
475 error() <<
"ERROR: Could not load context. Aborting..." <<
endmsg;
479 m_pfm.pfm_start( fd, NULL );
487 m_pfm.pfm_stop( fd );
488 if ( m_pfm.pfm_read_pmds( fd, pd, inp.pfp_event_count ) == -1 ) { error() <<
"Could not read pmds" <<
endmsg; }
489 for (
int i = 0; i < used_counters_number; i++ ) {
490 results[i][( alg_stack.
top().first )->
name()].push_back( alg_stack.
top().second[i] + pd[i].reg_value );
497 m_pfm.pfm_stop( fd );
498 if ( m_pfm.pfm_read_pmds( fd, pd, inp.pfp_event_count ) == -1 ) { error() <<
"Could not read pmds" <<
endmsg; }
500 for (
int i = 0; i < used_counters_number; i++ ) { alg_stack.
top().second[i] += pd[i].reg_value; }
509 info() <<
"start of finalizepm ucn:" << used_counters_number <<
endmsg;
510 for (
int i = 0; i < used_counters_number; i++ ) {
513 filename += event_cstr[i];
515 for (
auto&
c : filename )
516 if (
c ==
':' )
c =
'-';
518 if ( inv[i] ) filename +=
"_INV_1";
519 if ( cmask[i] > 0 ) filename +=
"_CMASK_" +
std::to_string( cmask[i] );
522 info() <<
"Filename:" << filename <<
endmsg;
523 FILE* outfile = fopen( filename.c_str(),
"w" );
525 fprintf( outfile,
"NHM " );
526 }
else if ( westmere ) {
527 fprintf( outfile,
"WSM " );
529 fprintf( outfile,
"CORE " );
531 fprintf( outfile,
"%s %d %d %d\n", event_cstr[i], cmask[i], inv[i],
sp[i] );
533 it != ( results[i] ).
end(); it++ ) {
534 fprintf( outfile,
"%s\n", ( it->first ).c_str() );
536 fprintf( outfile,
"%lu\n", *j );
544 if ( !m_pfm.loaded ) {
545 error() <<
"pfm library could not be loaded" <<
endmsg;
549 info() <<
"Initializing..." <<
endmsg;
552 used_counters_number = 0;
554 if ( event_str[i].length() > 0 ) used_counters_number++;
557 strcpy( prefix_cstr,
prefix.c_str() );
559 if ( m_pfm.pfm_initialize() !=
PFMLIB_SUCCESS ) { error() <<
"Cannot initialize perfmon!!" <<
endmsg; }
562 event_count_reached =
false;
566 if ( family.compare(
"CORE" ) == 0 )
568 else if ( family.compare(
"NEHALEM" ) == 0 )
570 else if ( family.compare(
"WESTMERE" ) == 0 )
573 error() <<
"ERROR: Unsupported processor family " << family <<
". aborting..." <<
endmsg;
576 info() <<
"Initialized!" <<
endmsg;
590 printf(
"skipping identical set of samples...\n" );
595 pos = (
unsigned long)ent;
596 entry = collected_samples;
600 ( ( samples[ent->
ovfl_pmd] )[( alg_stack.
top().first )->
name()] )[(
unsigned long)( ent->
ip )]++;
606 collected_samples = entry;
608 if ( last_count != hdr->
hdr_count && ( last_count || last_overflow == 0 ) ) { collected_partial += hdr->
hdr_count; }
618 PFMon& pfm = PFMon::instance();
622 if ( fd != ctx_fd ) {
625 if ( pfm.pfm_read_pmds( fd, pd_smpl + 1, 1 ) == -1 ) {
629 r =
read( fd, &msg,
sizeof( msg ) );
630 if ( r !=
sizeof( msg ) ) {
631 if ( r == -1 && errno == EINTR ) {
632 printf(
"read interrupted, retrying\n" );
639 switch ( msg.
type ) {
641 process_smpl_buf( hdr, entry_size );
643 if ( pfm.pfm_restart( fd ) ) {
644 if ( errno != EBUSY ) {
647 printf(
"pfm_restart: task probably terminated \n" );
666 memset( &pfmlib_options, 0,
sizeof( pfmlib_options ) );
667 pfmlib_options.pfm_debug = 0;
668 pfmlib_options.pfm_verbose = 0;
669 m_pfm.pfm_set_options( &pfmlib_options );
670 ret = m_pfm.pfm_initialize();
672 error() <<
"ERROR: Cannot initialize library: " << m_pfm.pfm_strerror( ret ) <<
". Aborting..." <<
endmsg;
674 struct sigaction act;
675 memset( &act, 0,
sizeof( act ) );
676 act.sa_sigaction = sigio_handler;
678 sigaction( SIGIO, &act, 0 );
679 memset( &ctx, 0,
sizeof( ctx ) );
680 memset( &buf_arg, 0,
sizeof( buf_arg ) );
681 memset( &inp, 0,
sizeof( inp ) );
682 memset( &outp, 0,
sizeof( outp ) );
683 memset( pd_smpl, 0,
sizeof( pd_smpl ) );
684 memset(
pc, 0,
sizeof(
pc ) );
685 memset( &load_args, 0,
sizeof( load_args ) );
686 m_pfm.pfm_get_num_counters( &num_counters );
687 memset( ¶ms, 0,
sizeof( params ) );
688 memset( &nhm_params, 0,
sizeof( nhm_params ) );
690 for (
int i = 0; i < used_counters_number; i++ ) {
691 ret = m_pfm.pfm_find_full_event( event_cstr[i], &inp.pfp_events[i] );
693 error() <<
"ERROR: cannot find event: " << event_cstr[i] <<
". Aborting..." <<
endmsg;
697 inp.pfp_event_count = 4;
698 for (
int i = 0; i < used_counters_number; i++ ) {
703 if ( cmask[i] > 0 ) {
704 ( params.pfp_core_counters[i] ).cnt_mask = cmask[i];
705 ( nhm_params.pfp_nhm_counters[i] ).cnt_mask = cmask[i];
708 if ( nehalem || westmere ) {
709 ret = m_pfm.pfm_dispatch_events( &inp, &nhm_params, &outp, NULL );
711 ret = m_pfm.pfm_dispatch_events( &inp, ¶ms, &outp, NULL );
714 error() <<
"ERROR: cannot configure events: " << m_pfm.pfm_strerror( ret ) <<
". Aborting..." <<
endmsg;
716 for (
unsigned int i = 0; i < outp.pfp_pmc_count; i++ ) {
717 pc[i].reg_num = outp.pfp_pmcs[i].reg_num;
718 pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
720 for (
unsigned int i = 0; i < outp.pfp_pmd_count; i++ ) {
721 pd_smpl[i].
reg_num = outp.pfp_pmds[i].reg_num;
723 pfm_bv_set( pd_smpl[0].reg_smpl_pmds, pd_smpl[i].reg_num );
724 if ( pd_smpl[i].reg_num > max_pmd ) { max_pmd = pd_smpl[i].
reg_num; }
728 for (
int i = 0; i < used_counters_number; i++ ) {
730 pfm_bv_copy( pd_smpl[i].reg_reset_pmds, pd_smpl[i].reg_smpl_pmds, max_pmd );
739 buf_arg.buf_size = 3 * getpagesize() + 512;
740 ctx_fd = m_pfm.pfm_create_context( &ctx, (
char*)
FMT_NAME, &buf_arg,
sizeof( buf_arg ) );
741 if ( ctx_fd == -1 ) {
742 if ( errno == ENOSYS ) {
743 error() <<
"ERROR: Your kernel does not have performance monitoring support! Aborting..." <<
endmsg;
745 error() <<
"ERROR: Can't create PFM context " << strerror( errno ) <<
". Aborting..." <<
endmsg;
747 buf_addr = mmap( NULL, buf_arg.buf_size, PROT_READ, MAP_PRIVATE, ctx_fd, 0 );
748 if ( buf_addr == MAP_FAILED ) {
749 error() <<
"ERROR: cannot mmap sampling buffer: " << strerror( errno ) <<
". Aborting..." <<
endmsg;
753 error() <<
"ERROR: invalid buffer format version. Aborting..." <<
endmsg;
755 if ( m_pfm.pfm_write_pmcs( ctx_fd,
pc, outp.pfp_pmc_count ) ) {
756 error() <<
"ERROR: pfm_write_pmcs error errno " << strerror( errno ) <<
". Aborting..." <<
endmsg;
758 if ( m_pfm.pfm_write_pmds( ctx_fd, pd_smpl, outp.pfp_pmd_count ) ) {
759 error() <<
"ERROR: pfm_write_pmds error errno " << strerror( errno ) <<
". Aborting..." <<
endmsg;
761 load_args.load_pid = getpid();
762 if ( m_pfm.pfm_load_context( ctx_fd, &load_args ) ) {
763 error() <<
"ERROR: pfm_load_context error errno " << strerror( errno ) <<
". Aborting..." <<
endmsg;
765 ret = fcntl( ctx_fd, F_SETFL, fcntl( ctx_fd, F_GETFL, 0 ) | O_ASYNC );
766 if ( ret == -1 ) { error() <<
"ERROR: cannot set ASYNC: " << strerror( errno ) <<
". Aborting..." <<
endmsg; }
767 ret = fcntl( ctx_fd, F_SETOWN, getpid() );
768 if ( ret == -1 ) { error() <<
"ERROR: cannot setown: " << strerror( errno ) <<
". Aborting..." <<
endmsg; }
770 m_pfm.pfm_start( ctx_fd, NULL );
778 m_pfm.pfm_self_stop( ctx_fd );
779 process_smpl_buf( hdr, entry_size );
781 ret = munmap( hdr, buf_arg.buf_size );
782 if ( ret ) { error() <<
"Cannot unmap buffer: %s" << strerror( errno ) <<
endmsg; }
792 for (
int i = 0; i < used_counters_number; i++ ) {
795 filename += event_cstr[i];
797 for (
auto&
c : filename )
798 if (
c ==
':' )
c =
'-';
800 if ( inv[i] ) filename +=
"_INV_1";
801 if ( cmask[i] > 0 ) filename +=
"_CMASK_" +
std::to_string( cmask[i] );
802 filename +=
".txt.gz";
804 gzFile outfile = gzopen( filename.c_str(),
"wb" );
805 if ( outfile != NULL ) {
807 gzprintf( outfile,
"NHM " );
808 }
else if ( westmere ) {
809 gzprintf( outfile,
"WSM " );
811 gzprintf( outfile,
"CORE " );
813 if ( gzprintf( outfile,
"%s %d %d %d\n", event_cstr[i], cmask[i], inv[i],
sp[i] ) <
814 (
int)strlen( event_cstr[i] ) ) {
815 error() <<
"ERROR: gzputs err: " << gzerror( outfile, &err ) <<
". Aborting..." <<
endmsg;
818 it != samples[i].end(); it++ ) {
819 unsigned long long sum = 0;
824 if ( gzprintf( outfile,
"%s%%%llu\n", ( it->first ).c_str(),
sum ) < (
int)( ( it->first ).length() ) ) {
825 error() <<
"ERROR: gzputs err: " << gzerror( outfile, &err ) <<
". Aborting..." <<
endmsg;
832 const char* symbolName;
836 if ( sym_addr != NULL ) {
839 if ( symbolName != NULL && strlen( symbolName ) > 0 ) {
840 strcpy( sym_name, symbolName );
841 strcat( sym_name,
" " );
843 strcpy( sym_name,
"??? " );
845 if ( libName != NULL && strlen( libName ) > 0 ) {
846 strcat( sym_name, libName );
847 strcat( sym_name,
" " );
849 strcat( sym_name,
"??? " );
851 sprintf( sym_name + strlen( sym_name ),
"%d ", libOffset );
852 if ( strlen( sym_name ) <= 0 ) { error() <<
"ERROR: Symbol name length is zero. Aborting..." <<
endmsg; }
854 strcpy( sym_name,
"??? ??? 0 " );
857 strcpy( sym_name,
"??? ??? 0 " );
859 if ( gzprintf( outfile,
"%s %d\n", sym_name, jt->second ) < (
int)strlen( sym_name ) ) {
860 error() <<
"ERROR: gzputs err: " << gzerror( outfile, &err ) <<
endmsg;
865 error() <<
"ERROR: Could not open file: " << filename <<
". Aborting..." <<
endmsg;
882 i_beforeInitialize( alg );
885 i_beforeExecute( alg );
896 i_afterInitialize( alg );
899 i_afterExecute( alg );
922 first_alg_name = alg->
name();
925 if ( !event_count_reached ) {
926 if ( !first_alg_name.compare( alg->
name() ) ) {
929 if ( ph_ev_count == start_at_event ) {
930 event_count_reached =
true;
935 if ( event_count_reached ) {
938 if ( !alg_stack.
empty() ) {
955 if ( !alg ) {
return; }
957 if ( event_count_reached ) {
966 if ( !alg_stack.
empty() ) {
pfmlib_core_input_param_t params
os_err_t pfm_write_pmds(int fd, pfarg_pmd_t *pmds, int count)
#define cpuid(func, ax, bx, cx, dx)
virtual StatusCode finalize()
#define PFM_REGFL_OVFL_NOTIFY
#define MAX_PREFIX_NAME_LENGTH
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
os_err_t pfm_create_context(pfarg_ctx_t *ctx, char *smpl_name, void *smpl_arg, size_t smpl_size)
PerfMonAuditor(const std::string &name, ISvcLocator *pSvc)
char * pfm_strerror(int code)
static void * tosymbol(void *address)
double sum(double x, double y, double z)
constexpr static const auto SUCCESS
pfm_err_t pfm_get_num_counters(unsigned int *num)
#define SYM_NAME_MAX_LENGTH
class MergingTransformer< Out(const vector_of_const_< In > void
def read(f, regex='.*', skipevents=0)
unsigned long long uint64_t
void i_afterExecute(INamedInterface *alg)
unsigned short int uint16_t
os_err_t pfm_write_pmcs(int fd, pfarg_pmc_t *pmcs, int count)
Performance Monitoring Auditor that uses Perfmon2 library to monitor algorithms.
pfm_err_t pfm_dispatch_events(pfmlib_input_param_t *p, void *model_in, pfmlib_output_param_t *q, void *model_out)
pfmlib_options_t pfmlib_options
std::string first_alg_name
pfm_err_t pfm_find_full_event(const char *str, pfmlib_event_t *e)
StatusCode finalize() override
#define MAX_EVENT_NAME_LENGTH
pfmlib_nhm_input_param_t nhm_params
PropertyMgr & operator=(const PropertyMgr &)=delete
#define DECLARE_COMPONENT(type)
#define PFM_VERSION_MAJOR(x)
void pfm_bv_copy(uint64_t *d, uint64_t *j, uint16_t n)
GaudiUtils::VectorMap< const INamedInterface *, int > Map
int detect_unavail_pmcs(int fd, pfmlib_regmask_t *r_pmcs)
void pfm_bv_set(uint64_t *bv, uint16_t rnum)
os_err_t pfm_restart(int fd)
This class is used for returning status codes from appropriate routines.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
pfmlib_output_param_t outp
void after(StandardEventType, INamedInterface *, const StatusCode &) override
static bool symbol(void *address, const char *&sym, const char *&lib, int &offset, int &liboffset)
void after(StandardEventType evt, INamedInterface *alg, const StatusCode &sc) override
os_err_t pfm_start(int fd, pfarg_start_t *start)
pfm_err_t pfm_initialize(void)
IInterface compliant class extending IInterface with the name() method.
int pfm_bv_isset(uint64_t *bv, uint16_t rnum)
unsigned int start_at_event
static void process_smpl_buf(pfm_dfl_smpl_hdr_t *hdr, size_t entry_size)
os_err_t pfm_load_context(int fd, pfarg_load_t *load)
constexpr static const auto FAILURE
pfm_dfl_smpl_arg_t buf_arg
static void sigio_handler(int, siginfo_t *, void *)
os_err_t pfm_read_pmds(int fd, pfarg_pmd_t *pmds, int count)
StatusCode initialize() override
void i_beforeExecute(INamedInterface *alg)
virtual StatusCode initialize()
AttribStringParser::Iterator begin(const AttribStringParser &parser)
#define MAX_NUMBER_OF_PROGRAMMABLE_COUNTERS
void i_afterInitialize(INamedInterface *alg)
void i_beforeInitialize(INamedInterface *alg)
os_err_t pfm_stop(int fd)
virtual ~PerfMonAuditor()
void before(StandardEventType, INamedInterface *) override
The following methods are meant to be implemented by the child class...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
virtual const std::string & name() const =0
Retrieve the name of the instance.
void before(StandardEventType evt, INamedInterface *alg) override
pfm_err_t pfm_set_options(pfmlib_options_t *opt)
Base class from which all concrete auditor classes should be derived.