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 <it/io.h> 00012 #include <it/timer.h> 00013 00014 00015 it_timer_t *timer_new () 00016 { 00017 it_timer_t *timer = (it_timer_t *) malloc (sizeof (it_timer_t)); 00018 timer_rtz (timer); 00019 return timer; 00020 } 00021 00022 00023 void timer_rtz (it_timer_t * timer) 00024 { 00025 timer->status = 0; 00026 timer->amount_wall = 0.0; 00027 timer->amount_user = 0.0; 00028 timer->amount_system = 0.0; 00029 } 00030 00031 00032 void timer_free (it_timer_t * timer) 00033 { 00034 free (timer); 00035 } 00036 00037 00038 void timer_on (it_timer_t * timer) 00039 { 00040 it_assert (timer->status == 0, "Timer was already set"); 00041 timer->status = 1; 00042 00043 /* store the current time for later use */ 00044 gettimeofday (&timer->time_last_wall, NULL); 00045 getrusage (RUSAGE_SELF, &timer->time_last_cpu); 00046 } 00047 00048 00049 void timer_off (it_timer_t * timer) 00050 { 00051 it_assert (timer->status == 1, "Timer was already off"); 00052 timer->status = 0; 00053 00054 /* store the current time for later use */ 00055 gettimeofday (&timer->time_now_wall, NULL); 00056 getrusage (RUSAGE_SELF, &timer->time_now_cpu); 00057 00058 /* update the current amounts */ 00059 timer->amount_wall += ((double) (timer->time_now_wall.tv_sec 00060 - timer->time_last_wall.tv_sec) 00061 + (double) (timer->time_now_wall.tv_usec 00062 - 00063 timer->time_last_wall.tv_usec) / 00064 1000000); 00065 timer->amount_user += ((double) 00066 (timer->time_now_cpu.ru_utime.tv_sec - 00067 timer->time_last_cpu.ru_utime.tv_sec) + 00068 (double) (timer->time_now_cpu.ru_utime.tv_usec - 00069 timer->time_last_cpu.ru_utime. 00070 tv_usec) / 1000000); 00071 timer->amount_system += ((double) 00072 (timer->time_now_cpu.ru_stime.tv_sec - 00073 timer->time_last_cpu.ru_stime.tv_sec) + 00074 (double) (timer->time_now_cpu.ru_stime. 00075 tv_usec - 00076 timer->time_last_cpu.ru_stime. 00077 tv_usec) / 1000000); 00078 } 00079 00080 00081 /* Return the different kind of usages */ 00082 double timer_wall (it_timer_t * timer) 00083 { 00084 return timer->amount_wall; 00085 } 00086 00087 00088 double timer_user (it_timer_t * timer) 00089 { 00090 return timer->amount_user; 00091 } 00092 00093 00094 double timer_system (it_timer_t * timer) 00095 { 00096 return timer->amount_system; 00097 } 00098 00099 00100 double timer_cpu (it_timer_t * timer) 00101 { 00102 return timer->amount_user + timer->amount_system; 00103 } 00104 00105 #endif 00106 #endif
|
|