summaryrefslogtreecommitdiff
path: root/ace/Timeprobe.cpp
blob: 6f6d451b9bcf71d5805d0c31637b2b09344f318c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// $Id$

#if !defined (ACE_TIMEPROBE_C)
#define ACE_TIMEPROBE_C

#include "ace/Timeprobe.h"

#if defined (ACE_ENABLE_TIMEPROBES)

#if !defined (__ACE_INLINE__)
#include "ace/Timeprobe.i"
#endif /* __ACE_INLINE__ */

#include "ace/High_Res_Timer.h"

template <class ACE_LOCK>
ACE_Timeprobe<ACE_LOCK>::ACE_Timeprobe (u_long size,
                                        ACE_Allocator *alloc)
  : allocator_ (alloc != 0 ? alloc : ACE_Allocator::instance ()),
    timeprobes_ (0),
    lock_ (),
    max_size_ (size),
    current_size_ (0)
{
  void *space = this->allocator_->malloc ((sizeof timeprobe_t) * this->max_size_);
  this->timeprobes_ = new ((timeprobe_t *) space) timeprobe_t[this->max_size_];
}

template <class ACE_LOCK>
ACE_Timeprobe<ACE_LOCK>::~ACE_Timeprobe (void)
{
  for (int i = 0; i < this->max_size_; i++)
    this->timeprobes_[i].timeprobe_t::~timeprobe_t ();

  this->allocator_->free (this->timeprobes_);
}

template <class ACE_LOCK> void
ACE_Timeprobe<ACE_LOCK>::timeprobe (u_long event)
{
  ACE_GUARD (ACE_LOCK, ace_mon, this->lock_);

  ACE_ASSERT (this->current_size_ < this->max_size_);

  this->timeprobes_[this->current_size_].event_.event_number_ = event;
  this->timeprobes_[this->current_size_].event_type_ = timeprobe_t::NUMBER;
  this->timeprobes_[this->current_size_].time_ = ACE_OS::gethrtime ();
  this->timeprobes_[this->current_size_].thread_ = ACE_OS::thr_self ();

  this->current_size_++;
}

template <class ACE_LOCK> void
ACE_Timeprobe<ACE_LOCK>::timeprobe (const char *event)
{
  ACE_GUARD (ACE_LOCK, ace_mon, this->lock_);

  ACE_ASSERT (this->current_size_ < this->max_size_);

  this->timeprobes_[this->current_size_].event_.event_description_ = event;
  this->timeprobes_[this->current_size_].event_type_ = timeprobe_t::STRING;
  this->timeprobes_[this->current_size_].time_ = ACE_OS::gethrtime ();
  this->timeprobes_[this->current_size_].thread_ = ACE_OS::thr_self ();

  this->current_size_++;
}

template <class ACE_LOCK> void
ACE_Timeprobe<ACE_LOCK>::reset (void)
{
  ACE_GUARD (ACE_LOCK, ace_mon, this->lock_);

  this->current_size_ = 0;
}

template <class ACE_LOCK> void
ACE_Timeprobe<ACE_LOCK>::print_times (const char *event_descriptions[]) const
{
  ACE_DEBUG ((LM_DEBUG,
              "\nACE_Timeprobe; %d timestamps were recorded:\n",
              this->current_size_));

  if (this->current_size_ == 0)
    return;

  ACE_DEBUG ((LM_DEBUG,
              "\n%-50.50s %8.8s %13.13s\n\n",
              "Event",
              "thread",
              "usec"));

  const char *description = 0;
  if (this->timeprobes_[0].event_type_ == timeprobe_t::STRING)
    description = this->timeprobes_[0].event_.event_description_;
  else
    description = event_descriptions[this->timeprobes_[0].event_.event_number_];
                                    
  ACE_DEBUG ((LM_DEBUG,
              "%-50.50s %8.8x %13.13s\n",
              description,
              this->timeprobes_[0].thread_,
              "START"));

  for (u_int i = 1; i < this->current_size_; i++)
    {
      ACE_hrtime_t time_difference = 
        this->timeprobes_[i].time_ - this->timeprobes_[i-1].time_;

      float elapsed_time_in_micro_seconds = 
        (float) time_difference / ACE_High_Res_Timer::global_scale_factor ();

      if (this->timeprobes_[i].event_type_ == timeprobe_t::STRING)
        description = this->timeprobes_[i].event_.event_description_;
      else
        description = event_descriptions[this->timeprobes_[i].event_.event_number_];
                                    
      ACE_DEBUG ((LM_DEBUG,
                  "%-50.50s %8.8x %13.3f\n",
                  description,
                  this->timeprobes_[i].thread_,
                  elapsed_time_in_micro_seconds));
    }
}

template <class Timeprobe>
ACE_Function_Timeprobe<Timeprobe>::ACE_Function_Timeprobe (Timeprobe &timeprobe, 
                                                           u_long event)
  : timeprobe_ (timeprobe),
    event_ (event)
{
  this->timeprobe_.timeprobe (this->event_);
}

template <class Timeprobe>
ACE_Function_Timeprobe<Timeprobe>::~ACE_Function_Timeprobe (void)
{
  this->timeprobe_.timeprobe (this->event_ + 1);
}

#endif /* ACE_ENABLE_TIMEPROBES */

#endif /* ACE_TIMEPROBE_C */