1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2001 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
38 * profile.c -- For timing and event counting.
40 * **********************************************
41 * CMU ARPA Speech Project
43 * Copyright (c) 1999 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
49 * Revision 1.7 2005/06/22 03:10:59 arthchan2003
50 * 1, Fixed doxygen documentation, 2, Added keyword.
52 * Revision 1.3 2005/03/30 01:22:48 archan
53 * Fixed mistakes in last updates. Add
56 * 11-Mar-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
59 * 19-Jun-97 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
69 #if defined(_WIN32) && !defined(__SYMBIAN32__)
74 #elif defined(HAVE_UNISTD_H) /* I know this, this is Unix... */
76 # include <sys/time.h>
77 # include <sys/resource.h>
81 #pragma warning (disable: 4996)
84 #include "sphinxbase/profile.h"
85 #include "sphinxbase/err.h"
86 #include "sphinxbase/ckd_alloc.h"
88 /* Silvio Moioli: updated to use Unicode */
90 DWORD unlink(const char *filename)
96 len = mbstowcs(NULL, filename, 0);
97 wfilename = ckd_calloc(len+1, sizeof(*wfilename));
98 mbstowcs(wfilename, filename, len);
99 rv = DeleteFileW(wfilename);
111 pc = ckd_calloc(1, sizeof(pctr_t));
112 pc->name = ckd_salloc(nm);
119 pctr_reset(pctr_t * ctr)
126 pctr_increment(pctr_t * ctr, int32 inc)
129 /* E_INFO("Name %s, Count %d, inc %d\n",ctr->name, ctr->count, inc); */
133 pctr_print(FILE * fp, pctr_t * ctr)
136 fprintf(fp, "[%d %s]", ctr->count, ctr->name);
140 pctr_free(pctr_t * pc)
150 #if defined(_WIN32) && !defined(GNUWINCE) && !defined(__SYMBIAN32__)
152 #define TM_LOWSCALE 1e-7
153 #define TM_HIGHSCALE (4294967296.0 * TM_LOWSCALE);
156 make_sec(FILETIME * tm)
160 dt = tm->dwLowDateTime * TM_LOWSCALE;
161 dt += tm->dwHighDateTime * TM_HIGHSCALE;
166 #else /* NOT WINDOWS */
169 make_sec(struct timeval *s)
171 return (s->tv_sec + s->tv_usec * 0.000001);
178 ptmr_start(ptmr_t * tm)
180 #if (! defined(_WIN32)) || defined(GNUWINCE) || defined(__SYMBIAN32__)
181 struct timeval e_start; /* Elapsed time */
183 #if (! defined(_HPUX_SOURCE)) && (! defined(__SYMBIAN32__))
184 struct rusage start; /* CPU time */
186 /* Unix but not HPUX */
187 getrusage(RUSAGE_SELF, &start);
188 tm->start_cpu = make_sec(&start.ru_utime) + make_sec(&start.ru_stime);
191 gettimeofday(&e_start, 0);
192 tm->start_elapsed = make_sec(&e_start);
193 #elif defined(_WIN32_WCE)
194 /* No GetProcessTimes() on WinCE. (Note CPU time will be bogus) */
195 tm->start_cpu = GetTickCount() / 1000;
196 tm->start_elapsed = GetTickCount() / 1000;
199 FILETIME t_create, t_exit, kst, ust;
202 pid = GetCurrentProcess();
203 GetProcessTimes(pid, &t_create, &t_exit, &kst, &ust);
204 tm->start_cpu = make_sec(&ust) + make_sec(&kst);
206 tm->start_elapsed = (float64) clock() / CLOCKS_PER_SEC;
212 ptmr_stop(ptmr_t * tm)
214 float64 dt_cpu, dt_elapsed;
216 #if (! defined(_WIN32)) || defined(GNUWINCE) || defined(__SYMBIAN32__)
217 struct timeval e_stop; /* Elapsed time */
219 #if (! defined(_HPUX_SOURCE)) && (! defined(__SYMBIAN32__))
220 struct rusage stop; /* CPU time */
222 /* Unix but not HPUX */
223 getrusage(RUSAGE_SELF, &stop);
225 make_sec(&stop.ru_utime) + make_sec(&stop.ru_stime) -
231 gettimeofday(&e_stop, 0);
232 dt_elapsed = (make_sec(&e_stop) - tm->start_elapsed);
233 #elif defined(_WIN32_WCE)
234 /* No GetProcessTimes() on WinCE. (Note CPU time will be bogus) */
235 dt_cpu = GetTickCount() / 1000 - tm->start_cpu;
236 dt_elapsed = GetTickCount() / 1000 - tm->start_elapsed;
239 FILETIME t_create, t_exit, kst, ust;
242 pid = GetCurrentProcess();
243 GetProcessTimes(pid, &t_create, &t_exit, &kst, &ust);
244 dt_cpu = make_sec(&ust) + make_sec(&kst) - tm->start_cpu;
245 dt_elapsed = ((float64) clock() / CLOCKS_PER_SEC) - tm->start_elapsed;
249 tm->t_elapsed += dt_elapsed;
251 tm->t_tot_cpu += dt_cpu;
252 tm->t_tot_elapsed += dt_elapsed;
257 ptmr_reset(ptmr_t * tm)
265 ptmr_init(ptmr_t * tm)
270 tm->t_tot_elapsed = 0.0;
275 ptmr_reset_all(ptmr_t * tm)
277 for (; tm->name; tm++)
283 ptmr_print_all(FILE * fp, ptmr_t * tm, float64 norm)
287 for (; tm->name; tm++)
288 fprintf(fp, " %6.2fx %s", tm->t_cpu * norm, tm->name);
297 int32 BYTE_ORDER_MAGIC;
302 file = "/tmp/__EnDiAn_TeSt__";
304 if ((fp = fopen(file, "wb")) == NULL) {
305 E_ERROR("Failed to open file '%s' for writing: %s\n", file, strerror(errno));
309 BYTE_ORDER_MAGIC = (int32) 0x11223344;
311 k = (int32) BYTE_ORDER_MAGIC;
312 if (fwrite(&k, sizeof(int32), 1, fp) != 1) {
313 E_ERROR("Failed to write to file '%s'\n", file);
320 if ((fp = fopen(file, "rb")) == NULL) {
321 E_ERROR("Failed to open file '%s' for reading: %s\n", file, strerror(errno));
325 if (fread(buf, 1, sizeof(int32), fp) != sizeof(int32)) {
326 E_ERROR("Failed to read from file '%s'\n", file);
334 /* If buf[0] == lsB of BYTE_ORDER_MAGIC, we are little-endian */
335 endian = (buf[0] == (BYTE_ORDER_MAGIC & 0x000000ff)) ? 1 : 0;