SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
timer.c
Go to the documentation of this file.
1/**
2 * @file timer.c
3 * @brief Provides functions for collecting run-time statistics such as CPU time, memory usage, I/O counts, and elapsed time.
4 *
5 * @details
6 * This file contains implementations of functions like `init_stats()`, `cpu_time()`, `memory_count()`, and others,
7 * which collect various run-time statistics. The implementations vary depending on the operating system
8 * (VAX/VMS, SUN UNIX, Linux, Solaris, etc.). The functions utilize system-specific libraries and system calls
9 * to retrieve the necessary information.
10 *
11 * @copyright
12 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
13 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
14 *
15 * @license
16 * This file is distributed under the terms of the Software License Agreement
17 * found in the file LICENSE included with this distribution.
18 *
19 * @author M. Borland, C Saunders, R. Soliday, Y. Wang
20 */
21
22#include "mdb.h"
23
24double delapsed_time(void);
25
26/* routine elapsed_time() returns the clock time elapsed since init_stats()
27 * was last called, as a character string
28 */
29
30char *elapsed_time() {
31 static char buffer[20];
32 static double dtime;
33 int h, m;
34 float s;
35
36 dtime = delapsed_time();
37 h = dtime / 3600;
38 dtime -= h * 3600;
39 m = dtime / 60;
40 dtime -= m * 60;
41 s = dtime;
42
43 sprintf(buffer, "%02d:%02d:%02.3f", h, m, s);
44 return (buffer);
45}
46
47# if defined(_WIN32)
48# include <time.h>
49# else
50# if defined(vxWorks)
51# include <sysLib.h>
52# include <time.h>
53# else
54# if defined(linux)
55# include <time.h>
56# endif
57# if defined(__rtems__)
58# include <unistd.h>
59# endif
60# include <sys/time.h>
61# endif
62# endif
63
64# if defined(linux)
65static struct timespec delapsedStart;
66static short delapsedStartInitialized = 0;
67/**
68 * @brief Initializes the run-time statistics collection.
69 *
70 * This function initializes the timer by calling system-specific initialization routines.
71 * It should be called before collecting any run-time statistics.
72 */
73void init_stats() {
74 clock_gettime(CLOCK_REALTIME, &delapsedStart);
75 delapsedStartInitialized = 1;
76}
77# else
78static double delapsed_start = 0;
79void init_stats() {
80 delapsed_start = delapsed_time();
81 clock();
82}
83# endif
84
85/**
86 * @brief Calculates the elapsed clock time since the last initialization as a numerical value.
87 *
88 * This function calculates the elapsed time in seconds since `init_stats()` was last called.
89 *
90 * @return The elapsed time in seconds, or 0.0 if not available.
91 */
92double delapsed_time() {
93# if defined(linux)
94 struct timespec delapsedNow;
95 if (!delapsedStartInitialized)
96 init_stats();
97 clock_gettime(CLOCK_REALTIME, &delapsedNow);
98 return (double)(delapsedNow.tv_sec - delapsedStart.tv_sec) +
99 (delapsedNow.tv_nsec - delapsedStart.tv_nsec)/1e9;
100# else
101 static long delapsed_start = -1;
102 if (delapsed_start == -1) {
103 delapsed_start = time(0);
104 return 0.0;
105 } else
106 return time(0) - delapsed_start;
107# endif
108}
109
110/**
111 * @brief Retrieves the CPU time used since the last initialization.
112 *
113 * This function retrieves the CPU time consumed by the process since `init_stats()` was last called.
114 *
115 * @return The CPU time used in hundredths of seconds, or -1 on error.
116 */
117long cpu_time() {
118 return (long)((clock() * 100.0) / CLOCKS_PER_SEC);
119}
120
121/* routine bio_count() returns the buffered io count since init_stats() was
122 * last called
123 */
124
125long bio_count() {
126 return 0;
127}
128
129/* routine dio_count() returns the direct io count since init_stats() was
130 * last called
131 */
132
133long dio_count() {
134 return (0);
135}
136
137/**
138 * @brief Retrieves the number of page faults since the last initialization.
139 *
140 * This function retrieves the number of page faults that have occurred since `init_stats()` was last called.
141 *
142 * @return The number of page faults, or -1 on error.
143 */
145 return 0;
146}
147
148# if defined(linux) && !defined(__powerpc__)
149# include <stdio.h>
150# include <sys/types.h>
151# include <unistd.h>
152
153/**
154 * @brief Retrieves the memory usage count since the last initialization.
155 *
156 * This function retrieves the memory usage information in kilobytes since `init_stats()` was last called.
157 *
158 * @return The memory usage in kilobytes, or 0 on error.
159 */
160long memory_count()
161{
162 FILE *fp;
163 static short first = 1;
164 static char proc[100];
165 long size1;
166
167 if (first) {
168 sprintf(proc, "/proc/%ld/statm", (long)getpid());
169 first = 0;
170 }
171 if (!(fp = fopen(proc, "r"))) {
172 perror("fopen failed in memory_count()");
173 exit(1);
174 }
175 if (fscanf(fp, "%ld", &size1) != 1) {
176 perror("fscanf failed in memory_count()");
177 exit(1);
178 }
179 fclose(fp);
180 /* Assume a page is 1 kB */
181 return size1;
182}
183# else
184long memory_count() {
185 return 0;
186}
187# endif
188
long page_faults()
Retrieves the number of page faults since the last initialization.
Definition timer.c:144
long cpu_time()
Retrieves the CPU time used since the last initialization.
Definition timer.c:117
double delapsed_time(void)
Calculates the elapsed clock time since the last initialization as a numerical value.
Definition timer.c:92