x86_timer.hh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //=====================================================
  2. // File : x86_timer.hh
  3. // Author : L. Plagne <laurent.plagne@edf.fr)>
  4. // Copyright (C) EDF R&D, mar d�c 3 18:59:35 CET 2002
  5. //=====================================================
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. #ifndef _X86_TIMER_HH
  21. #define _X86_TIMER_HH
  22. #include <sys/time.h>
  23. #include <sys/resource.h>
  24. #include <unistd.h>
  25. #include <sys/times.h>
  26. //#include "system_time.h"
  27. #define u32 unsigned int
  28. #include <asm/msr.h>
  29. #include "utilities.h"
  30. #include <map>
  31. #include <fstream>
  32. #include <string>
  33. #include <iostream>
  34. // frequence de la becanne en Hz
  35. //#define FREQUENCY 648000000
  36. //#define FREQUENCY 1400000000
  37. #define FREQUENCY 1695000000
  38. using namespace std;
  39. class X86_Timer {
  40. public :
  41. X86_Timer( void ):_frequency(FREQUENCY),_nb_sample(0)
  42. {
  43. MESSAGE("X86_Timer Default Ctor");
  44. }
  45. inline void start( void ){
  46. rdtsc(_click_start.n32[0],_click_start.n32[1]);
  47. }
  48. inline void stop( void ){
  49. rdtsc(_click_stop.n32[0],_click_stop.n32[1]);
  50. }
  51. inline double frequency( void ){
  52. return _frequency;
  53. }
  54. double get_elapsed_time_in_second( void ){
  55. return (_click_stop.n64-_click_start.n64)/double(FREQUENCY);
  56. }
  57. unsigned long long get_click( void ){
  58. return (_click_stop.n64-_click_start.n64);
  59. }
  60. inline void find_frequency( void ){
  61. time_t initial, final;
  62. int dummy=2;
  63. initial = time(0);
  64. start();
  65. do {
  66. dummy+=2;
  67. }
  68. while(time(0)==initial);
  69. // On est au debut d'un cycle d'une seconde !!!
  70. initial = time(0);
  71. start();
  72. do {
  73. dummy+=2;
  74. }
  75. while(time(0)==initial);
  76. final=time(0);
  77. stop();
  78. // INFOS("fine grained time : "<< get_elapsed_time_in_second());
  79. // INFOS("coarse grained time : "<< final-initial);
  80. _frequency=_frequency*get_elapsed_time_in_second()/double(final-initial);
  81. /// INFOS("CPU frequency : "<< _frequency);
  82. }
  83. void add_get_click( void ){
  84. _nb_sample++;
  85. _counted_clicks[get_click()]++;
  86. fill_history_clicks();
  87. }
  88. void dump_statistics(string filemane){
  89. ofstream outfile (filemane.c_str(),ios::out) ;
  90. std::map<unsigned long long , unsigned long long>::iterator itr;
  91. for(itr=_counted_clicks.begin() ; itr!=_counted_clicks.end() ; itr++)
  92. {
  93. outfile << (*itr).first << " " << (*itr).second << endl ;
  94. }
  95. outfile.close();
  96. }
  97. void dump_history(string filemane){
  98. ofstream outfile (filemane.c_str(),ios::out) ;
  99. for(int i=0 ; i<_history_mean_clicks.size() ; i++)
  100. {
  101. outfile << i << " "
  102. << _history_mean_clicks[i] << " "
  103. << _history_shortest_clicks[i] << " "
  104. << _history_most_occured_clicks[i] << endl ;
  105. }
  106. outfile.close();
  107. }
  108. double get_mean_clicks( void ){
  109. std::map<unsigned long long,unsigned long long>::iterator itr;
  110. unsigned long long mean_clicks=0;
  111. for(itr=_counted_clicks.begin() ; itr!=_counted_clicks.end() ; itr++)
  112. {
  113. mean_clicks+=(*itr).second*(*itr).first;
  114. }
  115. return mean_clicks/double(_nb_sample);
  116. }
  117. double get_shortest_clicks( void ){
  118. return double((*_counted_clicks.begin()).first);
  119. }
  120. void fill_history_clicks( void ){
  121. _history_mean_clicks.push_back(get_mean_clicks());
  122. _history_shortest_clicks.push_back(get_shortest_clicks());
  123. _history_most_occured_clicks.push_back(get_most_occured_clicks());
  124. }
  125. double get_most_occured_clicks( void ){
  126. unsigned long long moc=0;
  127. unsigned long long max_occurence=0;
  128. std::map<unsigned long long,unsigned long long>::iterator itr;
  129. for(itr=_counted_clicks.begin() ; itr!=_counted_clicks.end() ; itr++)
  130. {
  131. if (max_occurence<=(*itr).second){
  132. max_occurence=(*itr).second;
  133. moc=(*itr).first;
  134. }
  135. }
  136. return double(moc);
  137. }
  138. void clear( void )
  139. {
  140. _counted_clicks.clear();
  141. _history_mean_clicks.clear();
  142. _history_shortest_clicks.clear();
  143. _history_most_occured_clicks.clear();
  144. _nb_sample=0;
  145. }
  146. private :
  147. union
  148. {
  149. unsigned long int n32[2] ;
  150. unsigned long long n64 ;
  151. } _click_start;
  152. union
  153. {
  154. unsigned long int n32[2] ;
  155. unsigned long long n64 ;
  156. } _click_stop;
  157. double _frequency ;
  158. map<unsigned long long,unsigned long long> _counted_clicks;
  159. vector<double> _history_mean_clicks;
  160. vector<double> _history_shortest_clicks;
  161. vector<double> _history_most_occured_clicks;
  162. unsigned long long _nb_sample;
  163. };
  164. #endif