00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <stdio.h>
00041 #include <sys/time.h>
00042 #ifdef __MINGW32__
00043 #include <time.h>
00044 #else
00045 #include <sys/resource.h>
00046 #endif
00047 #include "timelib.h"
00048
00049 #ifdef __cplusplus
00050 extern "C" {
00051 #endif
00052
00053
00054
00055 static double get_cpu()
00056 {
00057 #ifdef __MINGW32__
00058 return clock();
00059 #else
00060 struct rusage ru;
00061 double cpu;
00062
00063 getrusage (RUSAGE_SELF, &ru);
00064 cpu = (double)ru.ru_utime.tv_sec +
00065 (double)ru.ru_utime.tv_usec * 1e-6 +
00066 (double)ru.ru_stime.tv_sec +
00067 (double)ru.ru_stime.tv_usec * 1e-6;
00068 return(cpu);
00069 #endif
00070 }
00071
00072 #define MAX_TIMERS 100
00073
00074 static double cumtime[MAX_TIMERS];
00075 static double start_time[MAX_TIMERS];
00076 static int timer_running[MAX_TIMERS];
00077 static int initialized=0;
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 static void init_package()
00100 {
00101 int i;
00102 if (!initialized) {
00103 for (i=0; i < MAX_TIMERS; i++) {
00104 cumtime[i] = 0.;
00105 start_time[i] = 0.;
00106 timer_running[i] = 0;
00107 }
00108 initialized = 1;
00109 }
00110 }
00111
00112 void start_timer (int timer)
00113 {
00114 int timer_index;
00115
00116 init_package();
00117 timer_index = timer-1;
00118 if (timer_index < 0 || timer_index >=MAX_TIMERS) {
00119 fprintf(stderr,"### Timer error: %d is not a valid timer value\n",timer);
00120 return;
00121 }
00122
00123 if (timer_running[timer_index]) return;
00124 timer_running[timer_index] = 1;
00125 start_time[timer_index] = get_cpu();
00126 return;
00127 }
00128
00129 void stop_timer (int timer)
00130 {
00131 int timer_index;
00132
00133 init_package();
00134 timer_index = timer-1;
00135 if (timer_index < 0 || timer_index >=MAX_TIMERS) {
00136 fprintf(stderr,"### Timer error: %d is not a valid timer value\n",timer);
00137 return;
00138 }
00139
00140 if (!timer_running[timer_index]) return;
00141 timer_running[timer_index] = 0;
00142 cumtime[timer_index] += (get_cpu() - start_time[timer_index]);
00143 return;
00144 }
00145
00146 void clear_timer(int timer)
00147 {
00148 int timer_index;
00149
00150 init_package();
00151 timer_index = timer-1;
00152 if (timer_index < 0 || timer_index >=MAX_TIMERS) {
00153 fprintf(stderr,"### Timer error: %d is not a valid timer value\n",timer);
00154 return;
00155 }
00156 timer_running[timer_index] = 0;
00157 cumtime[timer_index] = 0.0;
00158 }
00159
00160
00161 double get_timer_time (int timer)
00162 {
00163 int timer_index,running;
00164 double cputime;
00165
00166 init_package();
00167 timer_index = timer-1;
00168 if (timer_index < 0 || timer_index >=MAX_TIMERS) {
00169 fprintf(stderr,"### Timer error: %d is not a valid timer value\n",timer);
00170 return (0.0);
00171 }
00172 running = timer_running[timer_index];
00173 stop_timer(timer);
00174 cputime = cumtime[timer_index];
00175 if (running) start_timer(timer);
00176 return (cputime);
00177 }
00178
00179
00180 #ifdef __cplusplus
00181 }
00182 #endif