CMS 3D CMS Logo

Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

SimpleProfiler Class Reference

#include <SimpleProfiler.h>

List of all members.

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 551 of file SimpleProfiler.cc.

References fd_, and filename_.

                              :
  frame_data_(10*1000*1000),
  tmp_stack_(1000),
  high_water_(&frame_data_[10*1000*1000-10*1000]),
  curr_(&frame_data_[0]),
  filename_(makeFileName()),
  fd_(open(filename_.c_str(),
           O_RDWR|O_CREAT,
           S_IRWXU|S_IRGRP|S_IROTH|S_IWGRP|S_IWOTH)),
  installed_(false),
  running_(false),
  owner_(),
  stacktop_(setStacktop()) {
  if(fd_ < 0) {
    std::ostringstream ost;
    ost << "failed to open profiler output file " << filename_;
    throw std::runtime_error(ost.str().c_str());
  }
  openCondFile();
}
SimpleProfiler::~SimpleProfiler ( ) [private]

Definition at line 572 of file SimpleProfiler.cc.

References dtNoiseDBValidation_cfg::cerr, filename_, and running_.

                                {
  if(running_) {
    std::cerr << "Warning: the profile timer was not stopped,\n"
              << "profiling data in " << filename_
              << " is probably incomplete and will not be processed\n";
  }
  closeCondFile();
}

Member Function Documentation

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

Definition at line 581 of file SimpleProfiler.cc.

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

Referenced by sigFunc().

                                                          {
  void** cnt_ptr = curr_;
  *cnt_ptr = reinterpret_cast<void*>(std::distance(first, last));
  ++curr_;
  curr_ = std::copy(first, last, curr_);
  if(curr_ > high_water_) doWrite();
}
void SimpleProfiler::complete ( ) [private]

Definition at line 663 of file SimpleProfiler.cc.

References dtNoiseDBValidation_cfg::cerr, doWrite(), fd_, filename_, edmtest::makeFileName(), AlCaHLTBitMon_QueryRunRegistry::string, and writeProfileData().

Referenced by stop().

                              {
  doWrite();

  if(lseek(fd_, 0, SEEK_SET) < 0) {
      std::cerr << "SimpleProfiler: could not seek to the start of the profile\n"
                << " data file during completion.  Data will be lost.\n";
      return;
  }

  writeProfileData(fd_, filename_);
  write_maps();

  std::string totsname = makeFileName();
  totsname += "_sample_info";
  std::ofstream ost(totsname.c_str());

  ost << "samples_total " << samples_total << "\n"
      << "samples_missing_framepointer " << samples_missing_framepointer << "\n" ;
}
void SimpleProfiler::doWrite ( ) [private]

Definition at line 589 of file SimpleProfiler.cc.

References curr_, fd_, frame_data_, start(), and TablePrint::write.

Referenced by commitFrame(), and complete().

                             {
  void** start = &frame_data_[0];
  if(curr_ == start) return;
  unsigned int cnt = std::distance(start, curr_) * sizeof(void*);
  unsigned int totwr = 0;

  while(cnt>0 && (totwr = write(fd_, start, cnt)) != cnt) {
    if(totwr == 0)
      throw std::runtime_error("SimpleProfiler::doWrite wrote zero bytes");
    start += (totwr/sizeof(void*));
    cnt -= totwr;
  }

  curr_ = &frame_data_[0];
}
SimpleProfiler * SimpleProfiler::instance ( ) [static]

Definition at line 540 of file SimpleProfiler.cc.

References inst_, lock_, and AlCaHLTBitMon_ParallelJobs::p.

Referenced by sigFunc().

                                         {
  if(SimpleProfiler::inst_ == 0) {
      boost::mutex::scoped_lock sl(lock_);
      if(SimpleProfiler::inst_ == 0) {
          static SimpleProfiler p;
          SimpleProfiler::inst_ = &p;
      }
  }
  return SimpleProfiler::inst_;
}
void* SimpleProfiler::stackTop ( ) [inline]

Definition at line 23 of file SimpleProfiler.h.

References stacktop_.

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

Definition at line 610 of file SimpleProfiler.cc.

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

Referenced by doWrite().

                           {
  {
    boost::mutex::scoped_lock sl(lock_);

    if(installed_) {
      std::cerr << "Warning: second thread " << pthread_self()
                << " requested the profiler timer and another thread\n"
                << owner_ << "has already started it.\n"
                << "Only one thread can do profiling at a time.\n"
                << "This second thread will not be profiled.\n";
      return;
    }

    installed_ = true;
  }

  owner_ = pthread_self();
  setupTimer();
  running_ = true;
}
void SimpleProfiler::stop ( )

Definition at line 632 of file SimpleProfiler.cc.

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

                          {
  if(!installed_) {
      std::cerr << "SimpleProfiler::stop - no timer installed to stop\n";
      return;
  }

  if(owner_ != pthread_self()) {
      std::cerr << "SimpleProfiler::stop - only owning thread can stop the timer\n";
      return;
  }

  if(!running_) {
      std::cerr << "SimpleProfiler::stop - no timer is running\n";
      return;
  }

  struct itimerval newval;

  newval.it_interval.tv_sec = 0;
  newval.it_interval.tv_usec = 0;
  newval.it_value.tv_sec = 0;
  newval.it_value.tv_usec = 0;

  if(setitimer(ITIMER_PROF, &newval, 0) != 0) {
      perror("setitimer call failed - could not stop the timer");
  }

  running_ = false;
  complete();
}
void** SimpleProfiler::tempStack ( ) [inline]

Definition at line 24 of file SimpleProfiler.h.

References tmp_stack_.

Referenced by sigFunc().

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

Definition at line 25 of file SimpleProfiler.h.

References tmp_stack_.

Referenced by sigFunc().

{ return tmp_stack_.size(); }

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().

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 [static, private]

Definition at line 36 of file SimpleProfiler.h.

Referenced by instance().

Definition at line 44 of file SimpleProfiler.h.

Referenced by start(), and stop().

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().

Definition at line 39 of file SimpleProfiler.h.

Referenced by tempStack(), and tempStackSize().