00001 /* Available only for Linux at this moment */ 00002 #if ( (defined(unix) || defined(__unix)) ) 00003 00004 #include <sys/time.h> 00005 #include <sys/resource.h> 00006 #include <unistd.h> 00007 00008 #ifdef RUSAGE_SELF 00009 00010 #include <stdlib.h> 00011 #include "timer.h" 00012 00013 00014 it_timer_t * timer_new() 00015 { 00016 it_timer_t * timer = (it_timer_t *) malloc( sizeof(it_timer_t) ); 00017 timer_rtz( timer ); 00018 return timer; 00019 } 00020 00021 00022 void timer_rtz( it_timer_t * timer ) 00023 { 00024 timer->status = 0; 00025 timer->amount_wall = 0.0; 00026 timer->amount_user = 0.0; 00027 timer->amount_system = 0.0; 00028 } 00029 00030 00031 void timer_free( it_timer_t * timer ) 00032 { 00033 free( timer ); 00034 } 00035 00036 00037 void timer_on( it_timer_t * timer ) 00038 { 00039 assert( timer->status == 0 ); 00040 timer->status = 1; 00041 00042 /* store the current time for later use */ 00043 gettimeofday( &timer->time_last_wall, NULL ); 00044 getrusage( RUSAGE_SELF, &timer->time_last_cpu ); 00045 } 00046 00047 00048 void timer_off( it_timer_t * timer ) 00049 { 00050 assert( "Timer was already off" ); 00051 timer->status = 0; 00052 00053 /* store the current time for later use */ 00054 gettimeofday (&timer->time_now_wall, NULL) ; 00055 getrusage(RUSAGE_SELF, &timer->time_now_cpu); 00056 00057 /* update the current amounts */ 00058 timer->amount_wall += ((double) (timer->time_now_wall.tv_sec 00059 - timer->time_last_wall.tv_sec) 00060 + (double) (timer->time_now_wall.tv_usec 00061 - timer->time_last_wall.tv_usec) / 1000000); 00062 timer->amount_user += ((double) (timer->time_now_cpu.ru_utime.tv_sec 00063 - timer->time_last_cpu.ru_utime.tv_sec) 00064 + (double) (timer->time_now_cpu.ru_utime.tv_usec 00065 - timer->time_last_cpu.ru_utime.tv_usec) / 1000000); 00066 timer->amount_system += ((double) (timer->time_now_cpu.ru_stime.tv_sec 00067 - timer->time_last_cpu.ru_stime.tv_sec) 00068 + (double) (timer->time_now_cpu.ru_stime.tv_usec 00069 - timer->time_last_cpu.ru_stime.tv_usec) / 1000000); 00070 } 00071 00072 00073 /* Return the different kind of usages */ 00074 double timer_wall( it_timer_t * timer ) 00075 { 00076 return timer->amount_wall; 00077 } 00078 00079 00080 double timer_user( it_timer_t * timer ) 00081 { 00082 return timer->amount_user; 00083 } 00084 00085 00086 double timer_system( it_timer_t * timer ) 00087 { 00088 return timer->amount_system; 00089 } 00090 00091 00092 double timer_cpu( it_timer_t * timer ) 00093 { 00094 return timer->amount_user+timer->amount_system; 00095 } 00096 00097 #endif 00098 #endif
|
|