CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
SimpleProfiler Class Reference

#include <SimpleProfiler.h>

Public Types

typedef VoidVec::size_type size_type
 
typedef std::vector< void * > VoidVec
 

Public Member Functions

void commitFrame (void **first, void **last)
 
void * stackTop ()
 
void start ()
 
void stop ()
 
void ** tempStack ()
 
size_type tempStackSize ()
 

Static Public Member Functions

static SimpleProfilerinstance ()
 

Private Member Functions

void complete ()
 
void doWrite ()
 
 SimpleProfiler ()
 
 ~SimpleProfiler ()
 

Private Attributes

void ** curr_
 
int fd_
 
std::string filename_
 
VoidVec frame_data_
 
void ** high_water_
 
bool installed_
 
pthread_t owner_
 
bool running_
 
void * stacktop_
 
VoidVec tmp_stack_
 

Static Private Attributes

static SimpleProfilerinst_ = 0
 
static boost::mutex lock_
 

Detailed Description

Definition at line 12 of file SimpleProfiler.h.

Member Typedef Documentation

typedef VoidVec::size_type SimpleProfiler::size_type

Definition at line 16 of file SimpleProfiler.h.

typedef std::vector<void*> SimpleProfiler::VoidVec

Definition at line 15 of file SimpleProfiler.h.

Constructor & Destructor Documentation

SimpleProfiler::SimpleProfiler ( )
private

Definition at line 597 of file SimpleProfiler.cc.

References fd_, and filename_.

597  :
598  frame_data_(10*1000*1000),
599  tmp_stack_(1000),
600  high_water_(&frame_data_[10*1000*1000-10*1000]),
601  curr_(&frame_data_[0]),
603  fd_(open(filename_.c_str(),
604  O_RDWR|O_CREAT,
605  S_IRWXU|S_IRGRP|S_IROTH|S_IWGRP|S_IWOTH)),
606  installed_(false),
607  running_(false),
608  owner_(),
609  stacktop_(setStacktop())
610 {
611  if (fd_ < 0) {
612  std::ostringstream ost;
613  ost << "failed to open profiler output file " << filename_;
614  throw std::runtime_error(ost.str().c_str());
615  }
616 
617  openCondFile();
618 }
std::string makeFileName(const std::string &base, int num)
Definition: TestConsumer.cc:18
pthread_t owner_
VoidVec frame_data_
VoidVec tmp_stack_
std::string filename_
void ** high_water_
SimpleProfiler::~SimpleProfiler ( )
private

Definition at line 620 of file SimpleProfiler.cc.

References benchmark_cfg::cerr, filename_, and running_.

621 {
622  if (running_)
623  std::cerr << "Warning: the profile timer was not stopped,\n"
624  << "profiling data in " << filename_
625  << " is probably incomplete and will not be processed\n";
626 
627  closeCondFile();
628 }
std::string filename_

Member Function Documentation

void SimpleProfiler::commitFrame ( void **  first,
void **  last 
)

Definition at line 630 of file SimpleProfiler.cc.

References filterCSVwithJSON::copy, curr_, doWrite(), and high_water_.

Referenced by sigFunc().

631 {
632  void** cnt_ptr = curr_;
633  *cnt_ptr = reinterpret_cast<void*>(std::distance(first,last));
634  ++curr_;
636  if(curr_ > high_water_) doWrite();
637 }
void ** high_water_
bool first
Definition: L1TdeRCT.cc:79
void SimpleProfiler::complete ( )
private

Definition at line 719 of file SimpleProfiler.cc.

References benchmark_cfg::cerr, doWrite(), fd_, filename_, edmtest::makeFileName(), and writeProfileData().

Referenced by stop().

720 {
721  doWrite();
722 
723  if(lseek(fd_,0,SEEK_SET)<0)
724  {
725  std::cerr << "SimpleProfiler: could not seek to the start of the profile\n"
726  << " data file during completion. Data will be lost.\n";
727  return;
728  }
729 
731  write_maps();
732 
733  std::string totsname = makeFileName();
734  totsname += "_sample_info";
735  std::ofstream ost(totsname.c_str());
736 
737  ost << "samples_total " << samples_total << "\n"
738  << "samples_missing_framepointer " << samples_missing_framepointer << "\n" ;
739 }
std::string makeFileName(const std::string &base, int num)
Definition: TestConsumer.cc:18
std::string filename_
void writeProfileData(int fd, const std::string &prefix)
Definition: ProfParse.cc:159
void SimpleProfiler::doWrite ( )
private

Definition at line 639 of file SimpleProfiler.cc.

References curr_, fd_, frame_data_, and start().

Referenced by commitFrame(), and complete().

640 {
641  void** start = &frame_data_[0];
642  if(curr_ == start) return;
643  unsigned int cnt = std::distance(start,curr_) * sizeof(void*);
644  unsigned int totwr=0;
645 
646  while (cnt>0 && (totwr=write(fd_,start,cnt)) != cnt) {
647  if(totwr==0)
648  throw std::runtime_error("SimpleProfiler::doWrite wrote zero bytes");
649  start+=(totwr/sizeof(void*));
650  cnt-=totwr;
651  }
652 
653  curr_ = &frame_data_[0];
654 }
VoidVec frame_data_
SimpleProfiler * SimpleProfiler::instance ( )
static

Definition at line 583 of file SimpleProfiler.cc.

References inst_, lock_, and L1TEmulatorMonitor_cff::p.

Referenced by edm::service::SimpleProfiling::postBeginJob(), edm::service::SimpleProfiling::postEndJob(), and sigFunc().

584 {
585  if(SimpleProfiler::inst_ == 0)
586  {
587  boost::mutex::scoped_lock sl(lock_);
588  if(SimpleProfiler::inst_ == 0)
589  {
590  static SimpleProfiler p;
592  }
593  }
594  return SimpleProfiler::inst_;
595 }
static SimpleProfiler * inst_
static boost::mutex lock_
void* SimpleProfiler::stackTop ( )
inline

Definition at line 23 of file SimpleProfiler.h.

References stacktop_.

23 { return stacktop_; }
void SimpleProfiler::start ( void  )

Definition at line 656 of file SimpleProfiler.cc.

References benchmark_cfg::cerr, installed_, lock_, owner_, and running_.

Referenced by doWrite(), and edm::service::SimpleProfiling::postBeginJob().

657 {
658 #if defined(__x86_64__) || defined(__LP64__) || defined(_LP64)
659  throw std::logic_error("SimpleProfiler not available on 64 bit platform");
660 #endif
661  {
662  boost::mutex::scoped_lock sl(lock_);
663 
664  if (installed_) {
665  std::cerr << "Warning: second thread " << pthread_self()
666  << " requested the profiler timer and another thread\n"
667  << owner_ << "has already started it.\n"
668  << "Only one thread can do profiling at a time.\n"
669  << "This second thread will not be profiled.\n";
670  return;
671  }
672 
673  installed_ = true;
674  }
675 
676  owner_ = pthread_self();
677  setupTimer();
678  running_ = true;
679 }
pthread_t owner_
static boost::mutex lock_
void SimpleProfiler::stop ( )

Definition at line 681 of file SimpleProfiler.cc.

References benchmark_cfg::cerr, complete(), installed_, owner_, and running_.

Referenced by edm::service::SimpleProfiling::postEndJob().

682 {
683  if(!installed_)
684  {
685  std::cerr << "SimpleProfiler::stop - no timer installed to stop\n";
686  return;
687  }
688 
689  if(owner_ != pthread_self())
690  {
691  std::cerr << "SimpleProfiler::stop - only owning thread can stop the timer\n";
692  return;
693  }
694 
695  if(!running_)
696  {
697  std::cerr << "SimpleProfiler::stop - no timer is running\n";
698  return;
699  }
700 
701  struct itimerval newval;
702 
703  newval.it_interval.tv_sec = 0;
704  newval.it_interval.tv_usec = 0;
705  newval.it_value.tv_sec = 0;
706  newval.it_value.tv_usec = 0;
707 
708  if(setitimer(ITIMER_PROF,&newval,0)!=0)
709  {
710  perror("setitimer call failed - could not stop the timer");
711  }
712 
713  running_=false;
714  complete();
715 }
pthread_t owner_
void** SimpleProfiler::tempStack ( )
inline

Definition at line 24 of file SimpleProfiler.h.

References tmp_stack_.

Referenced by sigFunc().

24 { return &tmp_stack_[0]; }
VoidVec tmp_stack_
size_type SimpleProfiler::tempStackSize ( )
inline

Definition at line 25 of file SimpleProfiler.h.

References tmp_stack_.

Referenced by sigFunc().

25 { return tmp_stack_.size(); }
VoidVec tmp_stack_

Member Data Documentation

void** SimpleProfiler::curr_
private

Definition at line 41 of file SimpleProfiler.h.

Referenced by commitFrame(), and doWrite().

int SimpleProfiler::fd_
private

Definition at line 43 of file SimpleProfiler.h.

Referenced by complete(), doWrite(), and SimpleProfiler().

std::string SimpleProfiler::filename_
private

Definition at line 42 of file SimpleProfiler.h.

Referenced by complete(), SimpleProfiler(), and ~SimpleProfiler().

VoidVec SimpleProfiler::frame_data_
private

Definition at line 38 of file SimpleProfiler.h.

Referenced by doWrite().

void** SimpleProfiler::high_water_
private

Definition at line 40 of file SimpleProfiler.h.

Referenced by commitFrame().

SimpleProfiler * SimpleProfiler::inst_ = 0
staticprivate

Definition at line 36 of file SimpleProfiler.h.

Referenced by instance().

bool SimpleProfiler::installed_
private

Definition at line 44 of file SimpleProfiler.h.

Referenced by start(), and stop().

boost::mutex SimpleProfiler::lock_
staticprivate

Definition at line 37 of file SimpleProfiler.h.

Referenced by instance(), and start().

pthread_t SimpleProfiler::owner_
private

Definition at line 46 of file SimpleProfiler.h.

Referenced by start(), and stop().

bool SimpleProfiler::running_
private

Definition at line 45 of file SimpleProfiler.h.

Referenced by start(), stop(), and ~SimpleProfiler().

void* SimpleProfiler::stacktop_
private

Definition at line 47 of file SimpleProfiler.h.

Referenced by stackTop().

VoidVec SimpleProfiler::tmp_stack_
private

Definition at line 39 of file SimpleProfiler.h.

Referenced by tempStack(), and tempStackSize().