1 #include "Task_Stats.h"
3 #include "ace/Log_Msg.h"
5 #if !defined (__ACE_INLINE__)
6 #include "Task_Stats.inl"
7 #endif /* __ACE_INLINE__ */
9 ACE_High_Res_Timer::global_scale_factor_type
Task_Stats::gsf_
=
10 ACE_High_Res_Timer::global_scale_factor ();
12 Base_Time::Base_Time ()
14 base_time_
= ACE_OS::gethrtime ();
17 Task_Stats::Task_Stats ()
29 Task_Stats::~Task_Stats ()
31 delete[] this->time_inv_
;
32 delete[] this->time_exec_
;
36 Task_Stats::init (size_t max_samples
)
38 this->max_samples_
= max_samples
;
40 ACE_NEW_RETURN (this->time_inv_
, ACE_UINT64
[this->max_samples_
], -1);
41 ACE_NEW_RETURN (this->time_exec_
, ACE_UINT64
[this->max_samples_
], -1);
46 Task_Stats::base_time (ACE_UINT64 time
)
52 Task_Stats::end_time (ACE_UINT64 time
)
58 Task_Stats::dump_samples (const ACE_TCHAR
*file_name
, const ACE_TCHAR
*msg
, int dump_samples
)
60 FILE* output_file
= ACE_OS::fopen (file_name
, "w");
62 // first dump what the caller has to say.
63 ACE_OS::fprintf (output_file
, "%s\n", ACE_TEXT_ALWAYS_CHAR (msg
));
65 // next, compose and dump what we want to say.
68 double seconds
= this->diff_sec (base_time_
, end_time_
);
72 if (ACE::is_equal (seconds
, 0.0) || samples_count_
== 0)
74 ACE_OS::sprintf (out_msg
,
75 "# No samples recorded\n");
76 ACE_OS::fprintf (output_file
, "%s",out_msg
);
77 ACE_OS::fclose (output_file
);
82 double t_avg
= samples_count_
/ seconds
;
84 ACE_OS::sprintf (out_msg
,
85 "# Throughput: %.2f (events/second) ["
86 ACE_SIZE_T_FORMAT_SPECIFIER_ASCII
87 " samples in %.2f seconds]\n",
88 t_avg
, samples_count_
, seconds
);
89 ACE_OS::fprintf (output_file
, "%s",out_msg
);
94 for (i
= 0; i
!= this->samples_count_
; ++i
)
96 ACE_UINT32 val_2
= Task_Stats::diff_usec (time_inv_
[i
], time_exec_
[i
]);
98 // Write the normalized value.
99 // we will need this to calculate the var^2
100 this->time_exec_
[i
] = val_2
;
102 this->mean_
+= val_2
;
105 // calculate the mean.
106 this->mean_
/= this->samples_count_
;
108 // Calculate the var^2
110 for (i
= 0; i
!= this->samples_count_
; ++i
)
112 ACE_UINT64 diff
= this->time_exec_
[i
] - this->mean_
;
113 ACE_UINT64 diff_sq
= diff
* diff
;
115 this->var_2_
+= diff_sq
;
118 this->var_2_
/= this->samples_count_
;
120 ACE_OS::fprintf (output_file
, "## Latency: Avg = %u, Var^2 = %u\n"
121 , ACE_CU64_TO_CU32 (this->mean_
)
122 , ACE_CU64_TO_CU32 (this->var_2_
));
124 if (TAO_debug_level
> 0)
125 ACE_DEBUG ((LM_DEBUG
, " Latency: Avg = %u, Var^2 = %u\n"
126 , ACE_CU64_TO_CU32 (this->mean_
)
127 , ACE_CU64_TO_CU32 (this->var_2_
)));
129 // if we are asked to, dump the samples recorded.
132 ACE_OS::fprintf (output_file
, "#Invocation time \t Execution time\n");
134 for (i
= 0; i
!= this->samples_count_
; ++i
)
136 ACE_UINT32 val_1
= Task_Stats::diff_usec (base_time_
, time_inv_
[i
]);
138 ACE_OS::fprintf (output_file
, "%u \t %u\n",val_1
,
139 ACE_CU64_TO_CU32 (time_exec_
[i
]));
143 ACE_OS::fclose (output_file
);