BRL-CAD
timerunix.c
Go to the documentation of this file.
1 /* T I M E R U N I X . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 
22 #include "common.h"
23 
24 #include <stdio.h>
25 #ifdef HAVE_MEMORY_H
26 #include <memory.h>
27 #endif
28 #include <sys/types.h>
29 #include <sys/times.h>
30 #include <sys/param.h>
31 
32 #ifdef HAVE_SYS_MACHD_H
33 # include <sys/machd.h>
34 #endif
35 
36 #ifndef HZ
37 /* It's not always in sys/param.h; if not, guess */
38 # define HZ 60
39 # define DEFAULT_HZ yes
40 #endif
41 
42 
43 
44 /* Standard System V stuff */
45 static time_t time0;
46 static struct tms tms0;
47 
48 void
50 {
51  (void)time(&time0);
52  (void)times(&tms0);
53 }
54 
55 
56 double
57 rt_get_timer(struct bu_vls *vp, double *elapsed)
58 {
59  time_t now;
60  double user_cpu_secs;
61  double sys_cpu_secs;
62  double elapsed_secs;
63  double percent;
64  struct tms tmsnow;
65 
66  /* Real time. 1 second resolution. */
67  (void)time(&now);
68  elapsed_secs = now-time0;
69 
70  /* CPU time */
71  (void)times(&tmsnow);
72  user_cpu_secs = (tmsnow.tms_utime + tmsnow.tms_cutime) -
73  (tms0.tms_utime + tms0.tms_cutime);
74  user_cpu_secs /= HZ;
75  sys_cpu_secs = (tmsnow.tms_stime + tmsnow.tms_cstime) -
76  (tms0.tms_stime + tms0.tms_cstime);
77  sys_cpu_secs /= HZ;
78  if (user_cpu_secs < 0.00001) user_cpu_secs = 0.00001;
79  if (elapsed_secs < 0.00001) elapsed_secs = user_cpu_secs; /* It can't be any less! */
80 
81  if (elapsed) *elapsed = elapsed_secs;
82 
83  if (vp) {
84  percent = user_cpu_secs/elapsed_secs*100.0;
85  BU_CK_VLS(vp);
86 #ifdef DEFAULT_HZ
87  bu_vls_printf(vp,
88  "%g user + %g sys in %g elapsed secs (%g%%) WARNING: HZ=60 assumed, fix librt/timerunix.c",
89  user_cpu_secs, sys_cpu_secs, elapsed_secs, percent);
90 #else
91  bu_vls_printf(vp,
92  "%g user + %g sys in %g elapsed secs (%g%%)",
93  user_cpu_secs, sys_cpu_secs, elapsed_secs, percent);
94 #endif
95  }
96  return user_cpu_secs;
97 }
98 
99 
100 double
101 rt_read_timer(char *str, int len)
102 {
103  struct bu_vls vls = BU_VLS_INIT_ZERO;
104  double cpu;
105  int todo;
106 
107  if (!str)
108  return rt_get_timer((struct bu_vls *)0, (double *)0);
109 
110  cpu = rt_get_timer(&vls, (double *)0);
111  todo = bu_vls_strlen(&vls);
112 
113  if (todo > len)
114  todo = len;
115  bu_strlcpy(str, bu_vls_addr(&vls), todo);
116 
117  return cpu;
118 }
119 
120 
121 /*
122  * Local Variables:
123  * mode: C
124  * tab-width: 8
125  * indent-tabs-mode: t
126  * c-file-style: "stroustrup"
127  * End:
128  * ex: shiftwidth=4 tabstop=8
129  */
Header file for the BRL-CAD common definitions.
long time(time_t *)
double rt_read_timer(char *str, int len)
Definition: timerunix.c:101
double rt_get_timer(struct bu_vls *vp, double *elapsed)
Definition: timerunix.c:57
#define BU_CK_VLS(_vp)
Definition: vls.h:69
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
time_t time0
Definition: timer-nt.c:32
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
#define HZ
Definition: timerunix.c:38
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
void rt_prep_timer(void)
Definition: timerunix.c:49