Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SWIFTsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SWIFT
SWIFTsim
Commits
c5808890
Commit
c5808890
authored
9 years ago
by
Peter W. Draper
Browse files
Options
Downloads
Patches
Plain Diff
Be clearer about ms versus ticks
parent
3faabb00
No related branches found
No related tags found
2 merge requests
!136
Master
,
!105
Show time in real milli seconds
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/clocks.c
+42
-19
42 additions, 19 deletions
src/clocks.c
src/clocks.h
+1
-0
1 addition, 0 deletions
src/clocks.h
with
43 additions
and
19 deletions
src/clocks.c
+
42
−
19
View file @
c5808890
...
@@ -19,8 +19,10 @@
...
@@ -19,8 +19,10 @@
/**
/**
* @file clocks.c
* @file clocks.c
* @brief support for measuring intervals in seconds. Use cycle.h
* @brief support for measuring intervals in milli seconds, when that
* or timers.h for relative times.
* is possible, otherwise ticks.
*
* Use cycle.h or timers.h for relative times.
*/
*/
/* Config parameters. */
/* Config parameters. */
...
@@ -39,6 +41,11 @@
...
@@ -39,6 +41,11 @@
/* The CPU frequency used to convert ticks to seconds. */
/* The CPU frequency used to convert ticks to seconds. */
static
unsigned
long
long
clocks_cpufreq
=
0
;
static
unsigned
long
long
clocks_cpufreq
=
0
;
/* The units of any returned times. */
static
char
*
clocks_units
[]
=
{
"ms"
,
"ticks"
};
static
int
clocks_units_index
=
0
;
static
double
clocks_units_scale
=
1000
.
0
;
/* Local prototypes. */
/* Local prototypes. */
static
void
clocks_estimate_cpufreq
();
static
void
clocks_estimate_cpufreq
();
...
@@ -57,12 +64,12 @@ void clocks_gettime(struct clocks_time *time) {
...
@@ -57,12 +64,12 @@ void clocks_gettime(struct clocks_time *time) {
}
}
/**
/**
* @brief Get difference in
milli-seconds
between two times.
* @brief Get difference in between two times.
*
*
* @param start the start time.
* @param start the start time.
* @param end the end time.
* @param end the end time.
*
*
* @return the difference
in milli-secinds
.
* @return the difference.
*/
*/
double
clocks_diff
(
struct
clocks_time
*
start
,
struct
clocks_time
*
end
)
{
double
clocks_diff
(
struct
clocks_time
*
start
,
struct
clocks_time
*
end
)
{
#ifdef HAVE_CLOCK_GETTIME
#ifdef HAVE_CLOCK_GETTIME
...
@@ -76,7 +83,7 @@ double clocks_diff(struct clocks_time *start, struct clocks_time *end) {
...
@@ -76,7 +83,7 @@ double clocks_diff(struct clocks_time *start, struct clocks_time *end) {
}
}
return
(
double
)
temp
.
tv_sec
*
1000
.
0
+
(
double
)
temp
.
tv_nsec
*
1.0E-6
;
return
(
double
)
temp
.
tv_sec
*
1000
.
0
+
(
double
)
temp
.
tv_nsec
*
1.0E-6
;
#else
#else
return
elapsed
(
end
->
time
,
start
->
time
)
/
clocks_get_cpufreq
()
*
1000
;
return
elapsed
(
end
->
time
,
start
->
time
)
/
clocks_get_cpufreq
()
*
clocks_units_scale
;
#endif
#endif
}
}
...
@@ -117,8 +124,8 @@ unsigned long long clocks_get_cpufreq() {
...
@@ -117,8 +124,8 @@ unsigned long long clocks_get_cpufreq() {
*
*
* The technique is either use a clock timed nanosleep (this was the best
* The technique is either use a clock timed nanosleep (this was the best
* method on i7), to read the value from the cpuinfo_max_freq
* method on i7), to read the value from the cpuinfo_max_freq
* file (probably a overestimate)
, to use the macro value CPU_TPS or
* file (probably a overestimate)
or finally just use a value of 1 with
*
finally just use a value of 1
.
*
time units of ticks
.
*/
*/
static
void
clocks_estimate_cpufreq
()
{
static
void
clocks_estimate_cpufreq
()
{
...
@@ -143,6 +150,8 @@ static void clocks_estimate_cpufreq() {
...
@@ -143,6 +150,8 @@ static void clocks_estimate_cpufreq() {
clocks_cpufreq
=
clocks_cpufreq
=
(
signed
long
long
)(
double
)(
toc
-
tic
)
*
1
.
0
/
realsleep
*
1000
.
0
;
(
signed
long
long
)(
double
)(
toc
-
tic
)
*
1
.
0
/
realsleep
*
1000
.
0
;
clocks_units_index
=
0
;
clocks_units_scale
=
1000
.
0
;
#endif
#endif
/* Look for the system value, if available. Tends to be too large. */
/* Look for the system value, if available. Tends to be too large. */
...
@@ -154,23 +163,24 @@ static void clocks_estimate_cpufreq() {
...
@@ -154,23 +163,24 @@ static void clocks_estimate_cpufreq() {
unsigned
long
long
maxfreq
;
unsigned
long
long
maxfreq
;
if
(
fscanf
(
file
,
"%llu"
,
&
maxfreq
)
==
1
)
{
if
(
fscanf
(
file
,
"%llu"
,
&
maxfreq
)
==
1
)
{
clocks_cpufreq
=
maxfreq
*
1000
;
clocks_cpufreq
=
maxfreq
*
1000
;
clocks_units_index
=
0
;
clocks_units_scale
=
1000
.
0
;
}
}
fclose
(
file
);
fclose
(
file
);
}
}
}
}
#endif
#endif
/* Nearly final attempt */
#ifdef CPU_TPS
if
(
clocks_cpufreq
==
0
)
clocks_cpufreq
=
CPU_TPS
;
#endif
/* If all fails just report ticks in any times. */
/* If all fails just report ticks in any times. */
if
(
clocks_cpufreq
==
0
)
clocks_cpufreq
=
1
;
if
(
clocks_cpufreq
==
0
)
{
clocks_cpufreq
=
1
;
clocks_units_index
=
1
;
clocks_units_scale
=
1
.
0
;
}
}
}
/**
/**
* @brief Return the difference between two ticks
in seconds
.
* @brief Return the difference between two ticks.
*
*
* Only an approximation as based on how well we have estimated the
* Only an approximation as based on how well we have estimated the
* rtc frequency. Should be good for machines that support constant_rtc
* rtc frequency. Should be good for machines that support constant_rtc
...
@@ -179,23 +189,36 @@ static void clocks_estimate_cpufreq() {
...
@@ -179,23 +189,36 @@ static void clocks_estimate_cpufreq() {
* @param tic a number of ticks returned by the cycle.h getticks() function.
* @param tic a number of ticks returned by the cycle.h getticks() function.
* @param toc a number of ticks returned by the cycle.h getticks() function.
* @param toc a number of ticks returned by the cycle.h getticks() function.
*
*
* @result the
absolute
difference
in approximated seconds
.
* @result the difference.
*/
*/
double
clocks_diff_ticks
(
ticks
tic
,
ticks
toc
)
{
double
clocks_diff_ticks
(
ticks
tic
,
ticks
toc
)
{
return
clocks_from_ticks
(
tic
-
toc
);
return
clocks_from_ticks
(
tic
-
toc
);
}
}
/**
/**
* @brief Convert a number of ticks into seconds.
* @brief Convert a number of ticks into
milli
seconds
, if possible
.
*
*
* Only an approximation as based on how well we have estimated the
* Only an approximation as based on how well we have estimated the
* rtc frequency. Should be good for machines that support constant_rtc
* rtc frequency. Should be good for machines that support constant_rtc
* and clock_gettime().
* and clock_gettime(), and reasonable for most Linux machines, otherwise
* ticks will just be returned. See clocks_getunit() for the actual units.
*
*
* @param tics a number of ticks returned by the cycle.h getticks() function.
* @param tics a number of ticks returned by the cycle.h getticks() function.
*
*
* @result the
approximated seconds
.
* @result the
milli seconds, if possible
.
*/
*/
double
clocks_from_ticks
(
ticks
tics
)
{
double
clocks_from_ticks
(
ticks
tics
)
{
return
((
double
)
tics
/
(
double
)
clocks_get_cpufreq
()
*
1000
.
0
);
return
((
double
)
tics
/
(
double
)
clocks_get_cpufreq
()
*
clocks_units_scale
);
}
/**
* @brief return the time units.
*
* Normally "ms" for milliseconds, but can be "ticks" when no conversion
* factor is available.
*
* @result the current time units.
*/
const
char
*
clocks_getunit
()
{
return
clocks_units
[
clocks_units_index
];
}
}
This diff is collapsed.
Click to expand it.
src/clocks.h
+
1
−
0
View file @
c5808890
...
@@ -33,6 +33,7 @@ struct clocks_time {
...
@@ -33,6 +33,7 @@ struct clocks_time {
void
clocks_gettime
(
struct
clocks_time
*
time
);
void
clocks_gettime
(
struct
clocks_time
*
time
);
double
clocks_diff
(
struct
clocks_time
*
start
,
struct
clocks_time
*
end
);
double
clocks_diff
(
struct
clocks_time
*
start
,
struct
clocks_time
*
end
);
const
char
*
clocks_getunit
();
void
clocks_set_cpufreq
(
unsigned
long
long
freq
);
void
clocks_set_cpufreq
(
unsigned
long
long
freq
);
unsigned
long
long
clocks_get_cpufreq
();
unsigned
long
long
clocks_get_cpufreq
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment