forked from geodynamics/ellipsis3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Profiling.c
129 lines (93 loc) · 2.73 KB
/
Profiling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright (C) 1995 The GeoFramework Consortium
This file is part of Ellipsis3D.
Ellipsis3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
Ellipsis3D is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Author:
Louis Moresi <[email protected]>
*/
/* Profiling functions .... return elapsed CPU time etc.
These functions seem the most likely to get broken by
different architectures/operating systems &c */
#include "config.h"
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#if HAVE_SYS_TIMES_H
#include <sys/times.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_TIME_H
#include <time.h>
#endif
#include "global_defs.h"
#define GETRUSAGE 0
#define TIMES 1
#define STUPID 2
#if (!defined(TIME_FUNC) && HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H)
#define TIME_FUNC GETRUSAGE
#endif
#if (!defined(TIME_FUNC) && HAVE_TIMES && HAVE_SYS_TIMES_H)
#define TIME_FUNC TIMES
#endif
#ifndef TIME_FUNC
#define TIME_FUNC STUPID
#endif
/* ===============================================
Function to return currently elapsed CPU usage
=============================================== */
standard_precision CPU_time()
{
standard_precision time;
switch (TIME_FUNC) {
case GETRUSAGE:
#if (HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H)
{
struct rusage rusage;
getrusage(RUSAGE_SELF,&rusage);
time = (standard_precision)(rusage.ru_utime.tv_sec + 1.0e-6 * rusage.ru_utime.tv_usec);
}
#endif
break;
case TIMES:
#if (HAVE_TIMES && HAVE_SYS_TIMES_H && HAVE_UNISTD_H)
{
struct tms time_now;
time_t utime;
long sometime;
static standard_precision initial_time;
static int visit = 0;
if (visit==0) {
sometime=times(&time_now);
initial_time = (standard_precision) time_now.tms_utime / (standard_precision) sysconf(_SC_CLK_TCK);
visit++;
}
sometime=times(&time_now);
time = (standard_precision) time_now.tms_utime / (standard_precision) sysconf(_SC_CLK_TCK) - initial_time;
}
#endif
break;
case STUPID: /* stupid, break nothing "timer" */
default:
{
static standard_precision counter;
counter += 0.0001;
time = counter;
}
break;
} /* switch (TIME_FUNC) */
return time;
}