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