Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / RTCORBA / Activity / Task_Stats.cpp
blob9998b2bf3b707b4693d2b2d6c35f05b2e4513cee
1 #include "Task_Stats.h"
2 #include "ace/Log_Msg.h"
4 #if !defined (__ACE_INLINE__)
5 #include "Task_Stats.inl"
6 #endif /* __ACE_INLINE__ */
8 Base_Time::Base_Time (void)
10 base_time_ = ACE_OS::gethrtime ();
13 Task_Stats::Task_Stats (size_t max_samples)
14 : base_time_(0),
15 end_time_ (0),
16 max_samples_ (max_samples),
17 samples_count_ (0),
18 time_inv_ (0),
19 time_exec_ (0),
20 exec_time_min_ (0),
21 exec_time_min_at_ (0),
22 exec_time_max_ (0),
23 exec_time_max_at_(0),
24 sum_ (0),
25 sum2_ (0)
29 Task_Stats::~Task_Stats (void)
31 delete[] this->time_inv_;
32 delete[] this->time_exec_;
35 int
36 Task_Stats::init (void)
38 ACE_NEW_RETURN (this->time_inv_, ACE_UINT64[this->max_samples_], -1);
39 ACE_NEW_RETURN (this->time_exec_, ACE_UINT64[this->max_samples_], -1);
40 return 0;
43 void
44 Task_Stats::base_time (ACE_hrtime_t time)
46 base_time_ = time;
49 void
50 Task_Stats::end_time (ACE_hrtime_t time)
52 end_time_ = time;
55 void
56 Task_Stats::dump_samples (
57 const ACE_TCHAR *file_name,
58 const ACE_TCHAR *msg,
59 ACE_High_Res_Timer::global_scale_factor_type scale_factor)
61 FILE* output_file = ACE_OS::fopen (file_name, "w");
63 // first dump what the caller has to say.
64 ACE_OS::fprintf (output_file, "%s\n", ACE_TEXT_ALWAYS_CHAR (msg));
66 // next, compose and dump what we want to say.
68 // calc throughput.
70 ACE_TCHAR out_msg[BUFSIZ];
72 ACE_hrtime_t elapsed_microseconds = (end_time_ - base_time_) / scale_factor;
73 double elapsed_seconds =
74 ACE_CU64_TO_CU32(elapsed_microseconds) / 1000000.0;
75 double throughput =
76 double(samples_count_) / elapsed_seconds;
78 ACE_OS::sprintf (out_msg, ACE_TEXT("#Throughtput: %f\n"), throughput);
79 ACE_OS::fprintf (output_file, ACE_TEXT("%s\n"),out_msg);
81 // dump latency stats.
82 this->dump_latency_stats (out_msg, scale_factor);
83 ACE_OS::fprintf (output_file, "%s\n", ACE_TEXT_ALWAYS_CHAR (out_msg));
84 ACE_OS::fprintf (output_file, "#Invocation time \t Execution time\n");
86 // dump the samples recorded.
87 for (size_t i = 0; i != this->samples_count_; ++i)
89 ACE_UINT64 x = this->time_inv_[i] / scale_factor;
90 ACE_UINT32 val_1 = ACE_CU64_TO_CU32 (x);
92 ACE_UINT64 y = this->time_exec_[i] / scale_factor;
93 ACE_UINT32 val_2 = ACE_CU64_TO_CU32 (y);
95 ACE_OS::fprintf (output_file, "%u \t %u\n",val_1, val_2);
98 ACE_OS::fclose (output_file);
101 void
102 Task_Stats::dump_latency_stats (
103 ACE_TCHAR *out_msg,
104 ACE_High_Res_Timer::global_scale_factor_type sf)
106 if (this->samples_count_ == 0u)
108 ACE_OS::sprintf (out_msg,
109 ACE_TEXT ("# no data collected\n"));
110 return;
113 ACE_UINT64 avg = this->sum_ / this->samples_count_;
114 ACE_UINT64 dev =
115 this->sum2_ / this->samples_count_ - avg * avg;
117 ACE_UINT64 l_min_ = this->exec_time_min_ / sf;
118 ACE_UINT32 l_min = ACE_CU64_TO_CU32 (l_min_);
120 ACE_UINT64 l_max_ = this->exec_time_max_ / sf;
121 ACE_UINT32 l_max = ACE_CU64_TO_CU32 (l_max_);
123 ACE_UINT32 l_avg = ACE_CU64_TO_CU32(avg / sf);
124 ACE_UINT32 l_dev = ACE_CU64_TO_CU32(dev / (sf * sf));
126 ACE_UINT64 tmin_ = this->time_inv_[0] / sf;
127 ACE_UINT32 tmin = ACE_CU64_TO_CU32 (tmin_);
129 ACE_UINT64 tmax_ = this->time_inv_[samples_count_-1] / sf;
130 ACE_UINT32 tmax = ACE_CU64_TO_CU32 (tmax_);
132 ACE_OS::sprintf(out_msg,
133 ACE_TEXT ("#latency : %u[%d]/%u/%u[%d]/%u (min/avg/max/var^2)\n #first invocation time = %u, last invocation time = %u\n"),
134 l_min, this->exec_time_min_at_,
135 l_avg,
136 l_max, this->exec_time_max_at_,
137 l_dev,
138 tmin,tmax);
141 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, Base_Time, TAO_SYNCH_MUTEX);