Commit 2d6535b6 authored by zhujiashun's avatar zhujiashun

optimize some time-related codes

parent a264483a
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include <mach/clock.h> #include <mach/clock.h>
#include <mach/mach.h> #include <mach/mach.h>
inline void clock_gettime(clock_id_t id, timespec* time) { inline int clock_gettime(clock_id_t id, timespec* time) {
// clock_gettime is not available in MacOS, use clock_get_time instead // clock_gettime is not available in MacOS, use clock_get_time instead
clock_serv_t cclock; clock_serv_t cclock;
mach_timespec_t mts; mach_timespec_t mts;
...@@ -37,6 +37,7 @@ inline void clock_gettime(clock_id_t id, timespec* time) { ...@@ -37,6 +37,7 @@ inline void clock_gettime(clock_id_t id, timespec* time) {
mach_port_deallocate(mach_task_self(), cclock); mach_port_deallocate(mach_task_self(), cclock);
time->tv_sec = mts.tv_sec; time->tv_sec = mts.tv_sec;
time->tv_nsec = mts.tv_nsec; time->tv_nsec = mts.tv_nsec;
return 0;
} }
#endif #endif
......
...@@ -35,7 +35,7 @@ endif() ...@@ -35,7 +35,7 @@ endif()
set(CMAKE_CPP_FLAGS "-DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}") set(CMAKE_CPP_FLAGS "-DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__ -include ${CMAKE_SOURCE_DIR}/test/sstream_workaround.h") set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__ -include ${CMAKE_SOURCE_DIR}/test/sstream_workaround.h")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -g -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer")
use_cxx11() use_cxx11()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
......
...@@ -25,14 +25,8 @@ TEST(ButexTest, wait_on_already_timedout_butex) { ...@@ -25,14 +25,8 @@ TEST(ButexTest, wait_on_already_timedout_butex) {
uint32_t* butex = bthread::butex_create_checked<uint32_t>(); uint32_t* butex = bthread::butex_create_checked<uint32_t>();
ASSERT_TRUE(butex); ASSERT_TRUE(butex);
timespec now; timespec now;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time #if defined(OS_MACOSX)
clock_serv_t cclock; ASSERT_EQ(0, clock_gettime(CALENDAR_CLOCK, &now));
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
ASSERT_EQ(0, clock_get_time(cclock, &mts));
mach_port_deallocate(mach_task_self(), cclock);
now.tv_sec = mts.tv_sec;
now.tv_nsec = mts.tv_nsec;
#else #else
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now)); ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
#endif #endif
......
...@@ -34,14 +34,8 @@ public: ...@@ -34,14 +34,8 @@ public:
void run() void run()
{ {
timespec current_time; timespec current_time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time #if defined(OS_MACOSX)
clock_serv_t cclock; clock_gettime(CALENDAR_CLOCK, &current_time);
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
current_time.tv_sec = mts.tv_sec;
current_time.tv_nsec = mts.tv_nsec;
#else #else
clock_gettime(CLOCK_REALTIME, &current_time); clock_gettime(CLOCK_REALTIME, &current_time);
#endif #endif
...@@ -181,14 +175,8 @@ public: ...@@ -181,14 +175,8 @@ public:
void run() void run()
{ {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time #if defined(OS_MACOSX)
clock_serv_t cclock; clock_gettime(CALENDAR_CLOCK, &_running_time);
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
_running_time.tv_sec = mts.tv_sec;
_running_time.tv_nsec = mts.tv_nsec;
#else #else
clock_gettime(CLOCK_REALTIME, &_running_time); clock_gettime(CLOCK_REALTIME, &_running_time);
#endif #endif
...@@ -251,14 +239,8 @@ TEST(TimerThreadTest, schedule_and_unschedule_in_task) { ...@@ -251,14 +239,8 @@ TEST(TimerThreadTest, schedule_and_unschedule_in_task) {
timer_thread.stop_and_join(); timer_thread.stop_and_join();
timespec finish_time; timespec finish_time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time #if defined(OS_MACOSX)
clock_serv_t cclock; clock_gettime(CALENDAR_CLOCK, &finish_time);
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
finish_time.tv_sec = mts.tv_sec;
finish_time.tv_nsec = mts.tv_nsec;
#else #else
clock_gettime(CLOCK_REALTIME, &finish_time); clock_gettime(CLOCK_REALTIME, &finish_time);
#endif #endif
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "butil/strings/string_piece.h" #include "butil/strings/string_piece.h"
#include "butil/build_config.h" #include "butil/build_config.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <fstream>
namespace butil { namespace butil {
extern int read_command_output_through_clone(std::ostream&, const char*); extern int read_command_output_through_clone(std::ostream&, const char*);
......
...@@ -232,19 +232,21 @@ int main(int argc, char* argv[]) { ...@@ -232,19 +232,21 @@ int main(int argc, char* argv[]) {
} }
} }
std::vector<bthread_t> tids; std::vector<bthread_t> bids;
tids.resize(FLAGS_thread_num); std::vector<pthread_t> pids;
if (!FLAGS_use_bthread) { if (!FLAGS_use_bthread) {
pids.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) { for (int i = 0; i < FLAGS_thread_num; ++i) {
if (pthread_create((pthread_t*)&tids[i], NULL, replay_thread, &chan_group) != 0) { if (pthread_create(&pids[i], NULL, replay_thread, &chan_group) != 0) {
LOG(ERROR) << "Fail to create pthread"; LOG(ERROR) << "Fail to create pthread";
return -1; return -1;
} }
} }
} else { } else {
bids.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) { for (int i = 0; i < FLAGS_thread_num; ++i) {
if (bthread_start_background( if (bthread_start_background(
&tids[i], NULL, replay_thread, &chan_group) != 0) { &bids[i], NULL, replay_thread, &chan_group) != 0) {
LOG(ERROR) << "Fail to create bthread"; LOG(ERROR) << "Fail to create bthread";
return -1; return -1;
} }
...@@ -263,9 +265,9 @@ int main(int argc, char* argv[]) { ...@@ -263,9 +265,9 @@ int main(int argc, char* argv[]) {
for (int i = 0; i < FLAGS_thread_num; ++i) { for (int i = 0; i < FLAGS_thread_num; ++i) {
if (!FLAGS_use_bthread) { if (!FLAGS_use_bthread) {
pthread_join((pthread_t)tids[i], NULL); pthread_join(pids[i], NULL);
} else { } else {
bthread_join(tids[i], NULL); bthread_join(bids[i], NULL);
} }
} }
info_thr.stop(); info_thr.stop();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment