x86_timer.hh
Go to the documentation of this file.
00001 //=====================================================
00002 // File   :  x86_timer.hh
00003 // Author :  L. Plagne <laurent.plagne@edf.fr)>        
00004 // Copyright (C) EDF R&D,  mar d�c 3 18:59:35 CET 2002
00005 //=====================================================
00006 // 
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 // 
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 // 
00020 #ifndef _X86_TIMER_HH
00021 #define _X86_TIMER_HH
00022 
00023 #include <sys/time.h>
00024 #include <sys/resource.h>
00025 #include <unistd.h>
00026 #include <sys/times.h>
00027 //#include "system_time.h"
00028 #define u32 unsigned int
00029 #include <asm/msr.h>
00030 #include "utilities.h"
00031 #include <map>
00032 #include <fstream>
00033 #include <string>
00034 #include <iostream>
00035 
00036 // frequence de la becanne en Hz
00037 //#define FREQUENCY 648000000
00038 //#define FREQUENCY 1400000000
00039 #define FREQUENCY 1695000000
00040 
00041 using namespace std;
00042 
00043 
00044 class X86_Timer {
00045 
00046 public :
00047 
00048   X86_Timer( void ):_frequency(FREQUENCY),_nb_sample(0)
00049   {
00050     MESSAGE("X86_Timer Default Ctor");    
00051   }
00052 
00053   inline void start( void ){
00054 
00055     rdtsc(_click_start.n32[0],_click_start.n32[1]);
00056 
00057   }
00058 
00059 
00060   inline void stop( void ){
00061 
00062     rdtsc(_click_stop.n32[0],_click_stop.n32[1]);
00063 
00064   }
00065   
00066 
00067   inline double frequency( void ){
00068     return _frequency;
00069   }
00070 
00071   double get_elapsed_time_in_second( void ){
00072 
00073     return (_click_stop.n64-_click_start.n64)/double(FREQUENCY);
00074 
00075 
00076   }    
00077 
00078   unsigned long long  get_click( void ){
00079     
00080     return (_click_stop.n64-_click_start.n64);
00081 
00082   }    
00083 
00084   inline void find_frequency( void ){
00085 
00086     time_t initial, final;
00087     int dummy=2;
00088 
00089     initial = time(0);
00090     start();
00091     do {
00092       dummy+=2;
00093     }
00094     while(time(0)==initial);
00095     // On est au debut d'un cycle d'une seconde !!!
00096     initial = time(0);
00097     start();
00098     do {
00099       dummy+=2;
00100     }
00101     while(time(0)==initial);
00102     final=time(0);
00103     stop();
00104     //    INFOS("fine grained time : "<<  get_elapsed_time_in_second());
00105     //  INFOS("coarse grained time : "<<  final-initial);
00106     _frequency=_frequency*get_elapsed_time_in_second()/double(final-initial);
00108 
00109   }
00110 
00111   void  add_get_click( void ){
00112        
00113     _nb_sample++;
00114     _counted_clicks[get_click()]++;
00115     fill_history_clicks();
00116 
00117   }    
00118 
00119   void dump_statistics(string filemane){
00120     
00121     ofstream outfile (filemane.c_str(),ios::out) ;
00122 
00123     std::map<unsigned long long , unsigned long long>::iterator itr;
00124     for(itr=_counted_clicks.begin() ; itr!=_counted_clicks.end()  ; itr++)
00125       {      
00126       outfile  << (*itr).first << "  " << (*itr).second << endl ;       
00127       }      
00128     
00129     outfile.close();
00130 
00131   }
00132 
00133   void dump_history(string filemane){
00134     
00135     ofstream outfile (filemane.c_str(),ios::out) ;
00136 
00137 
00138 
00139     for(int i=0 ; i<_history_mean_clicks.size() ; i++)
00140       {      
00141         outfile  << i << " " 
00142                  << _history_mean_clicks[i] << " " 
00143                  << _history_shortest_clicks[i] << " " 
00144                  << _history_most_occured_clicks[i] << endl ;
00145       }      
00146     
00147     outfile.close();
00148 
00149   }
00150      
00151 
00152 
00153   double get_mean_clicks( void ){
00154     
00155     std::map<unsigned long long,unsigned long long>::iterator itr;
00156     
00157     unsigned long long mean_clicks=0;
00158 
00159     for(itr=_counted_clicks.begin() ; itr!=_counted_clicks.end()  ; itr++)
00160       {      
00161         
00162         mean_clicks+=(*itr).second*(*itr).first;
00163       }      
00164 
00165     return mean_clicks/double(_nb_sample);
00166 
00167   }
00168 
00169   double get_shortest_clicks( void ){
00170     
00171     return double((*_counted_clicks.begin()).first);
00172 
00173   }
00174 
00175   void fill_history_clicks( void ){
00176 
00177     _history_mean_clicks.push_back(get_mean_clicks());
00178     _history_shortest_clicks.push_back(get_shortest_clicks());
00179     _history_most_occured_clicks.push_back(get_most_occured_clicks());
00180 
00181   }
00182 
00183 
00184   double get_most_occured_clicks( void ){
00185 
00186     unsigned long long moc=0;
00187     unsigned long long max_occurence=0;
00188 
00189     std::map<unsigned long long,unsigned long long>::iterator itr;
00190 
00191     for(itr=_counted_clicks.begin() ; itr!=_counted_clicks.end()  ; itr++)
00192       {      
00193         
00194         if (max_occurence<=(*itr).second){
00195           max_occurence=(*itr).second;
00196           moc=(*itr).first;
00197         }
00198       }      
00199     
00200     return double(moc);    
00201 
00202   }
00203   
00204   void clear( void )
00205   {
00206     _counted_clicks.clear();
00207 
00208     _history_mean_clicks.clear();
00209     _history_shortest_clicks.clear();
00210     _history_most_occured_clicks.clear();
00211 
00212     _nb_sample=0;
00213   }
00214 
00215 
00216     
00217 private :
00218   
00219   union
00220   {
00221     unsigned long int n32[2] ;
00222     unsigned long long n64 ;
00223   } _click_start;
00224 
00225   union
00226   {
00227     unsigned long int n32[2] ;
00228     unsigned long long n64 ;
00229   } _click_stop;
00230 
00231   double _frequency ;
00232 
00233   map<unsigned long long,unsigned long long> _counted_clicks;
00234 
00235   vector<double> _history_mean_clicks;
00236   vector<double> _history_shortest_clicks;
00237   vector<double> _history_most_occured_clicks;
00238 
00239   unsigned long long _nb_sample;
00240 
00241   
00242 
00243 };
00244 
00245 
00246 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:43