Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
23d6ecc7
Commit
23d6ecc7
authored
Apr 26, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use gettimeofday implement realtime
parent
22e14ab6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
17 additions
and
26 deletions
+17
-26
time.h
src/butil/time.h
+17
-8
baidu_time_unittest.cpp
test/baidu_time_unittest.cpp
+0
-6
bthread_butex_unittest.cpp
test/bthread_butex_unittest.cpp
+0
-4
bthread_timer_thread_unittest.cpp
test/bthread_timer_thread_unittest.cpp
+0
-8
No files found.
src/butil/time.h
View file @
23d6ecc7
...
...
@@ -33,15 +33,24 @@
# define CLOCK_MONOTONIC SYSTEM_CLOCK
typedef
int
clockid_t
;
// clock_gettime is not available in MacOS < 10.12
inline
int
clock_gettime
(
clockid_t
id
,
timespec
*
time
)
{
// clock_gettime is not available in MacOS, use clock_get_time instead
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
id
,
&
cclock
);
clock_get_time
(
cclock
,
&
mts
);
mach_port_deallocate
(
mach_task_self
(),
cclock
);
time
->
tv_sec
=
mts
.
tv_sec
;
time
->
tv_nsec
=
mts
.
tv_nsec
;
if
(
id
==
CLOCK_MONOTONIC
)
{
clock_serv_t
cclock
;
mach_timespec_t
mts
;
host_get_clock_service
(
mach_host_self
(),
id
,
&
cclock
);
clock_get_time
(
cclock
,
&
mts
);
mach_port_deallocate
(
mach_task_self
(),
cclock
);
time
->
tv_sec
=
mts
.
tv_sec
;
time
->
tv_nsec
=
mts
.
tv_nsec
;
}
else
if
(
id
==
CLOCK_REALTIME
)
{
struct
timeval
now
;
if
(
gettimeofday
(
&
now
,
NULL
)
<
0
)
{
return
-
1
;
}
time
->
tv_sec
=
now
.
tv_sec
;
time
->
tv_nsec
=
now
.
tv_usec
*
1000
;
}
return
0
;
}
# endif
...
...
test/baidu_time_unittest.cpp
View file @
23d6ecc7
...
...
@@ -19,11 +19,7 @@ namespace {
TEST
(
BaiduTimeTest
,
diff_between_gettimeofday_and_REALTIME
)
{
long
t1
=
butil
::
gettimeofday_us
();
timespec
time
;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_gettime
(
CALENDAR_CLOCK
,
&
time
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
time
);
#endif
long
t2
=
butil
::
timespec_to_microseconds
(
time
);
LOG
(
INFO
)
<<
"t1="
<<
t1
<<
" t2="
<<
t2
;
}
...
...
@@ -104,7 +100,6 @@ TEST(BaiduTimeTest, cost_of_timer) {
clock_desc
[
i
],
t1
.
n_elapsed
()
/
N
);
}
#endif
#if !defined(OS_MACOSX)
if
(
0
==
clock_gettime
((
clockid_t
)
i
,
&
ts
))
{
t1
.
start
();
for
(
size_t
j
=
0
;
j
<
N
;
++
j
)
{
...
...
@@ -114,7 +109,6 @@ TEST(BaiduTimeTest, cost_of_timer) {
printf
(
"glibc clock_gettime(%s) takes %"
PRId64
"ns
\n
"
,
clock_desc
[
i
],
t1
.
n_elapsed
()
/
N
);
}
#endif
}
}
...
...
test/bthread_butex_unittest.cpp
View file @
23d6ecc7
...
...
@@ -25,11 +25,7 @@ TEST(ButexTest, wait_on_already_timedout_butex) {
uint32_t
*
butex
=
bthread
::
butex_create_checked
<
uint32_t
>
();
ASSERT_TRUE
(
butex
);
timespec
now
;
#if defined(OS_MACOSX)
ASSERT_EQ
(
0
,
clock_gettime
(
CALENDAR_CLOCK
,
&
now
));
#else
ASSERT_EQ
(
0
,
clock_gettime
(
CLOCK_REALTIME
,
&
now
));
#endif
*
butex
=
1
;
ASSERT_EQ
(
-
1
,
bthread
::
butex_wait
(
butex
,
1
,
&
now
));
ASSERT_EQ
(
ETIMEDOUT
,
errno
);
...
...
test/bthread_timer_thread_unittest.cpp
View file @
23d6ecc7
...
...
@@ -171,11 +171,7 @@ public:
void
run
()
{
#if defined(OS_MACOSX)
clock_gettime
(
CALENDAR_CLOCK
,
&
_running_time
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
_running_time
);
#endif
EXPECT_EQ
(
_expected_unschedule_result
,
_timer_thread
->
unschedule
(
_keeper1
->
_task_id
));
_keeper2
->
schedule
(
_timer_thread
);
...
...
@@ -235,11 +231,7 @@ TEST(TimerThreadTest, schedule_and_unschedule_in_task) {
timer_thread
.
stop_and_join
();
timespec
finish_time
;
#if defined(OS_MACOSX)
clock_gettime
(
CALENDAR_CLOCK
,
&
finish_time
);
#else
clock_gettime
(
CLOCK_REALTIME
,
&
finish_time
);
#endif
keeper1
.
expect_not_run
();
keeper2
.
expect_first_run
(
test_task1
.
_running_time
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment