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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include <config.h>
00064
00065 #include <stdio.h>
00066 #include <stdlib.h>
00067 #include <string.h>
00068
00069 #if defined(_WIN32)
00070 # include <windows.h>
00071 # ifndef _WIN32_WCE
00072 # include <time.h>
00073 # endif
00074 #elif defined(HAVE_UNISTD_H)
00075 # include <unistd.h>
00076 # include <sys/time.h>
00077 # include <sys/resource.h>
00078 #endif
00079
00080 #ifdef _MSC_VER
00081 #pragma warning (disable: 4996)
00082 #endif
00083
00084 #include "profile.h"
00085 #include "err.h"
00086 #include "ckd_alloc.h"
00087
00088 #ifdef _WIN32_WCE
00089 DWORD unlink(const char *filename)
00090 {
00091 WCHAR *wfilename;
00092 DWORD rv;
00093 size_t len;
00094
00095 len = mbstowcs(NULL, filename, 0);
00096 wfilename = ckd_calloc(len+1, sizeof(*wfilename));
00097 mbstowcs(wfilename, filename, len);
00098 rv = DeleteFile(wfilename);
00099 ckd_free(wfilename);
00100
00101 return rv;
00102 }
00103 #endif
00104
00105 pctr_t *
00106 pctr_new(char *nm)
00107 {
00108 pctr_t *pc;
00109
00110 pc = ckd_calloc(1, sizeof(pctr_t));
00111 pc->name = ckd_salloc(nm);
00112 pc->count = 0;
00113
00114 return pc;
00115 }
00116
00117 void
00118 pctr_reset(pctr_t * ctr)
00119 {
00120 ctr->count = 0;
00121 }
00122
00123
00124 void
00125 pctr_increment(pctr_t * ctr, int32 inc)
00126 {
00127 ctr->count += inc;
00128
00129 }
00130
00131 void
00132 pctr_print(FILE * fp, pctr_t * ctr)
00133 {
00134 fprintf(fp, "CTR:");
00135 fprintf(fp, "[%d %s]", ctr->count, ctr->name);
00136 }
00137
00138 void
00139 pctr_free(pctr_t * pc)
00140 {
00141 if (pc) {
00142 if (pc->name)
00143 ckd_free(pc->name);
00144 }
00145 ckd_free(pc);
00146 }
00147
00148
00149 #if (WIN32) && !defined(GNUWINCE)
00150
00151 #define TM_LOWSCALE 1e-7
00152 #define TM_HIGHSCALE (4294967296.0 * TM_LOWSCALE);
00153
00154 static float64
00155 make_sec(FILETIME * tm)
00156 {
00157 float64 dt;
00158
00159 dt = tm->dwLowDateTime * TM_LOWSCALE;
00160 dt += tm->dwHighDateTime * TM_HIGHSCALE;
00161
00162 return (dt);
00163 }
00164
00165 #else
00166
00167 static float64
00168 make_sec(struct timeval *s)
00169 {
00170 return (s->tv_sec + s->tv_usec * 0.000001);
00171 }
00172
00173 #endif
00174
00175
00176 void
00177 ptmr_start(ptmr_t * tm)
00178 {
00179 #if (! WIN32) || defined(GNUWINCE)
00180 struct timeval e_start;
00181
00182 #if (! _HPUX_SOURCE)
00183 struct rusage start;
00184
00185
00186 getrusage(RUSAGE_SELF, &start);
00187 tm->start_cpu = make_sec(&start.ru_utime) + make_sec(&start.ru_stime);
00188 #endif
00189
00190 gettimeofday(&e_start, 0);
00191 tm->start_elapsed = make_sec(&e_start);
00192 #elif defined(_WIN32_WCE)
00193
00194 tm->start_cpu = GetTickCount() / 1000;
00195 tm->start_elapsed = GetTickCount() / 1000;
00196 #else
00197 HANDLE pid;
00198 FILETIME t_create, t_exit, kst, ust;
00199
00200
00201 pid = GetCurrentProcess();
00202 GetProcessTimes(pid, &t_create, &t_exit, &kst, &ust);
00203 tm->start_cpu = make_sec(&ust) + make_sec(&kst);
00204
00205 tm->start_elapsed = (float64) clock() / CLOCKS_PER_SEC;
00206 #endif
00207 }
00208
00209
00210 void
00211 ptmr_stop(ptmr_t * tm)
00212 {
00213 float64 dt_cpu, dt_elapsed;
00214
00215 #if (! WIN32) || defined(GNUWINCE)
00216 struct timeval e_stop;
00217
00218 #if (! _HPUX_SOURCE)
00219 struct rusage stop;
00220
00221
00222 getrusage(RUSAGE_SELF, &stop);
00223 dt_cpu =
00224 make_sec(&stop.ru_utime) + make_sec(&stop.ru_stime) -
00225 tm->start_cpu;
00226 #else
00227 dt_cpu = 0.0;
00228 #endif
00229
00230 gettimeofday(&e_stop, 0);
00231 dt_elapsed = (make_sec(&e_stop) - tm->start_elapsed);
00232 #elif defined(_WIN32_WCE)
00233
00234 dt_cpu = GetTickCount() / 1000 - tm->start_cpu;
00235 dt_elapsed = GetTickCount() / 1000 - tm->start_elapsed;
00236 #else
00237 HANDLE pid;
00238 FILETIME t_create, t_exit, kst, ust;
00239
00240
00241 pid = GetCurrentProcess();
00242 GetProcessTimes(pid, &t_create, &t_exit, &kst, &ust);
00243 dt_cpu = make_sec(&ust) + make_sec(&kst) - tm->start_cpu;
00244 dt_elapsed = ((float64) clock() / CLOCKS_PER_SEC) - tm->start_elapsed;
00245 #endif
00246
00247 tm->t_cpu += dt_cpu;
00248 tm->t_elapsed += dt_elapsed;
00249
00250 tm->t_tot_cpu += dt_cpu;
00251 tm->t_tot_elapsed += dt_elapsed;
00252 }
00253
00254
00255 void
00256 ptmr_reset(ptmr_t * tm)
00257 {
00258 tm->t_cpu = 0.0;
00259 tm->t_elapsed = 0.0;
00260 }
00261
00262
00263 void
00264 ptmr_init(ptmr_t * tm)
00265 {
00266 tm->t_cpu = 0.0;
00267 tm->t_elapsed = 0.0;
00268 tm->t_tot_cpu = 0.0;
00269 tm->t_tot_elapsed = 0.0;
00270 }
00271
00272
00273 void
00274 ptmr_reset_all(ptmr_t * tm)
00275 {
00276 for (; tm->name; tm++)
00277 ptmr_reset(tm);
00278 }
00279
00280
00281 void
00282 ptmr_print_all(FILE * fp, ptmr_t * tm, float64 norm)
00283 {
00284 if (norm != 0.0) {
00285 norm = 1.0 / norm;
00286 for (; tm->name; tm++)
00287 fprintf(fp, " %6.2fx %s", tm->t_cpu * norm, tm->name);
00288 }
00289 }
00290
00291
00292 int32
00293 host_endian(void)
00294 {
00295 FILE *fp;
00296 int32 BYTE_ORDER_MAGIC;
00297 char *file;
00298 char buf[8];
00299 int32 k, endian;
00300
00301 file = "/tmp/__EnDiAn_TeSt__";
00302
00303 if ((fp = fopen(file, "wb")) == NULL) {
00304 E_ERROR("fopen(%s,wb) failed\n", file);
00305 return -1;
00306 }
00307
00308 BYTE_ORDER_MAGIC = (int32) 0x11223344;
00309
00310 k = (int32) BYTE_ORDER_MAGIC;
00311 if (fwrite(&k, sizeof(int32), 1, fp) != 1) {
00312 E_ERROR("fwrite(%s) failed\n", file);
00313 fclose(fp);
00314 unlink(file);
00315 return -1;
00316 }
00317
00318 fclose(fp);
00319 if ((fp = fopen(file, "rb")) == NULL) {
00320 E_ERROR("fopen(%s,rb) failed\n", file);
00321 unlink(file);
00322 return -1;
00323 }
00324 if (fread(buf, 1, sizeof(int32), fp) != sizeof(int32)) {
00325 E_ERROR("fread(%s) failed\n", file);
00326 fclose(fp);
00327 unlink(file);
00328 return -1;
00329 }
00330 fclose(fp);
00331 unlink(file);
00332
00333
00334 endian = (buf[0] == (BYTE_ORDER_MAGIC & 0x000000ff)) ? 1 : 0;
00335
00336 return (endian);
00337 }