• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libsphinxbase/util/profile.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2001 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * profile.c -- For timing and event counting.
00039  *
00040  * **********************************************
00041  * CMU ARPA Speech Project
00042  *
00043  * Copyright (c) 1999 Carnegie Mellon University.
00044  * ALL RIGHTS RESERVED.
00045  * **********************************************
00046  * 
00047  * HISTORY
00048  * $Log: profile.c,v $
00049  * Revision 1.7  2005/06/22 03:10:59  arthchan2003
00050  * 1, Fixed doxygen documentation, 2, Added  keyword.
00051  *
00052  * Revision 1.3  2005/03/30 01:22:48  archan
00053  * Fixed mistakes in last updates. Add
00054  *
00055  * 
00056  * 11-Mar-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00057  *              Added ptmr_init().
00058  * 
00059  * 19-Jun-97    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00060  *              Created.
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) /* I know this, this is Unix... */
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     /*   E_INFO("Name %s, Count %d, inc %d\n",ctr->name, ctr->count, inc); */
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 /* NOT WINDOWS */
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;     /* Elapsed time */
00181 
00182 #if (! _HPUX_SOURCE)
00183     struct rusage start;        /* CPU time */
00184 
00185     /* Unix but not HPUX */
00186     getrusage(RUSAGE_SELF, &start);
00187     tm->start_cpu = make_sec(&start.ru_utime) + make_sec(&start.ru_stime);
00188 #endif
00189     /* Unix + HP */
00190     gettimeofday(&e_start, 0);
00191     tm->start_elapsed = make_sec(&e_start);
00192 #elif defined(_WIN32_WCE)
00193     /* No GetProcessTimes() on WinCE.  (Note CPU time will be bogus) */
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     /* PC */
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;      /* Elapsed time */
00217 
00218 #if (! _HPUX_SOURCE)
00219     struct rusage stop;         /* CPU time */
00220 
00221     /* Unix but not HPUX */
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     /* Unix + HP */
00230     gettimeofday(&e_stop, 0);
00231     dt_elapsed = (make_sec(&e_stop) - tm->start_elapsed);
00232 #elif defined(_WIN32_WCE)
00233         /* No GetProcessTimes() on WinCE.  (Note CPU time will be bogus) */
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     /* PC */
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     /* If buf[0] == lsB of BYTE_ORDER_MAGIC, we are little-endian */
00334     endian = (buf[0] == (BYTE_ORDER_MAGIC & 0x000000ff)) ? 1 : 0;
00335 
00336     return (endian);
00337 }

Generated on Fri Jan 14 2011 for SphinxBase by  doxygen 1.7.1