SDDSlib
Loading...
Searching...
No Matches
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
24#if defined(UNIX) || defined(_WIN32) || defined(vxWorks)
25double delapsed_time(void);
26
27/* routine elapsed_time() returns the clock time elapsed since init_stats()
28 * was last called, as a character string
29 */
30
31char *elapsed_time() {
32 static char buffer[20];
33 static double dtime;
34 int h, m;
35 float s;
36
37 dtime = delapsed_time();
38 h = dtime / 3600;
39 dtime -= h * 3600;
40 m = dtime / 60;
41 dtime -= m * 60;
42 s = dtime;
43
44 sprintf(buffer, "%02d:%02d:%02.3f", h, m, s);
45 return (buffer);
46}
47
48# if defined(_WIN32)
49# include <time.h>
50# else
51# if defined(vxWorks)
52# include <sysLib.h>
53# include <time.h>
54# else
55# if defined(linux)
56# include <time.h>
57# endif
58# if defined(__rtems__)
59# include <unistd.h>
60# endif
61# include <sys/time.h>
62# endif
63# endif
64
65# if defined(linux)
66static struct timespec delapsedStart;
67static short delapsedStartInitialized = 0;
68/**
69 * @brief Initializes the run-time statistics collection.
70 *
71 * This function initializes the timer by calling system-specific initialization routines.
72 * It should be called before collecting any run-time statistics.
73 */
74void init_stats() {
75 clock_gettime(CLOCK_REALTIME, &delapsedStart);
76 delapsedStartInitialized = 1;
77}
78# else
79static double delapsed_start = 0;
80void init_stats() {
81 delapsed_start = delapsed_time();
82 clock();
83}
84# endif
85
86/**
87 * @brief Calculates the elapsed clock time since the last initialization as a numerical value.
88 *
89 * This function calculates the elapsed time in seconds since `init_stats()` was last called.
90 *
91 * @return The elapsed time in seconds, or 0.0 if not available.
92 */
93double delapsed_time() {
94# if defined(linux)
95 struct timespec delapsedNow;
96 if (!delapsedStartInitialized)
97 init_stats();
98 clock_gettime(CLOCK_REALTIME, &delapsedNow);
99 return (double)(delapsedNow.tv_sec - delapsedStart.tv_sec) +
100 (delapsedNow.tv_nsec - delapsedStart.tv_nsec)/1e9;
101# else
102 static long delapsed_start = -1;
103 if (delapsed_start == -1) {
104 delapsed_start = time(0);
105 return 0.0;
106 } else
107 return time(0) - delapsed_start;
108# endif
109}
110
111/**
112 * @brief Retrieves the CPU time used since the last initialization.
113 *
114 * This function retrieves the CPU time consumed by the process since `init_stats()` was last called.
115 *
116 * @return The CPU time used in hundredths of seconds, or -1 on error.
117 */
118long cpu_time() {
119 return (long)((clock() * 100.0) / CLOCKS_PER_SEC);
120}
121
122/* routine bio_count() returns the buffered io count since init_stats() was
123 * last called
124 */
125
126long bio_count() {
127 return 0;
128}
129
130/* routine dio_count() returns the direct io count since init_stats() was
131 * last called
132 */
133
134long dio_count() {
135 return (0);
136}
137
138/**
139 * @brief Retrieves the number of page faults since the last initialization.
140 *
141 * This function retrieves the number of page faults that have occurred since `init_stats()` was last called.
142 *
143 * @return The number of page faults, or -1 on error.
144 */
145long page_faults() {
146 return 0;
147}
148
149# if defined(linux) && !defined(__powerpc__)
150# include <stdio.h>
151# include <sys/types.h>
152# include <unistd.h>
153
154/**
155 * @brief Retrieves the memory usage count since the last initialization.
156 *
157 * This function retrieves the memory usage information in kilobytes since `init_stats()` was last called.
158 *
159 * @return The memory usage in kilobytes, or 0 on error.
160 */
161long memory_count()
162{
163 FILE *fp;
164 static short first = 1;
165 static char proc[100];
166 long size1;
167
168 if (first) {
169 sprintf(proc, "/proc/%ld/statm", (long)getpid());
170 first = 0;
171 }
172 if (!(fp = fopen(proc, "r"))) {
173 perror("fopen failed in memory_count()");
174 exit(1);
175 }
176 if (fscanf(fp, "%ld", &size1) != 1) {
177 perror("fscanf failed in memory_count()");
178 exit(1);
179 }
180 fclose(fp);
181 /* Assume a page is 1 kB */
182 return size1;
183}
184# else
185long memory_count() {
186 return 0;
187}
188# endif
189
190
191#endif