Unverified Commit b1f7ea03 authored by jamesge's avatar jamesge Committed by GitHub

Merge pull request #1270 from gydong/master

coding style fix in brpc/trackme.cpp
parents a4e28d68 123e4440
...@@ -43,11 +43,11 @@ static const int32_t TRACKME_MIN_INTERVAL = 30; ...@@ -43,11 +43,11 @@ static const int32_t TRACKME_MIN_INTERVAL = 30;
static const int32_t TRACKME_MAX_INTERVAL = 600; static const int32_t TRACKME_MAX_INTERVAL = 600;
static int32_t s_trackme_interval = TRACKME_MIN_INTERVAL; static int32_t s_trackme_interval = TRACKME_MIN_INTERVAL;
// Protecting global vars on trackme // Protecting global vars on trackme
static pthread_mutex_t g_trackme_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t s_trackme_mutex = PTHREAD_MUTEX_INITIALIZER;
// For contacting with trackme_server. // For contacting with trackme_server.
static Channel* g_trackme_chan = NULL; static Channel* s_trackme_chan = NULL;
// Any server address in this process. // Any server address in this process.
static std::string* g_trackme_addr = NULL; static std::string* s_trackme_addr = NULL;
// Information of bugs. // Information of bugs.
// Notice that this structure may be a combination of all affected bugs. // Notice that this structure may be a combination of all affected bugs.
...@@ -65,10 +65,10 @@ struct BugInfo { ...@@ -65,10 +65,10 @@ struct BugInfo {
// can avoid showing the same bug repeatly. // can avoid showing the same bug repeatly.
static BugInfo* g_bug_info = NULL; static BugInfo* g_bug_info = NULL;
// The timestamp(microseconds) that we sent TrackMeRequest. // The timestamp(microseconds) that we sent TrackMeRequest.
static int64_t g_trackme_last_time = 0; static int64_t s_trackme_last_time = 0;
// version of RPC. // version of RPC.
// Since the code for getting BRPC_REVISION often fails, // Since the code for getting BRPC_REVISION often fails,
// BRPC_REVISION must be defined to string and be converted to number // BRPC_REVISION must be defined to string and be converted to number
// within our code. // within our code.
// The code running before main() may see g_rpc_version=0, should be OK. // The code running before main() may see g_rpc_version=0, should be OK.
...@@ -116,8 +116,8 @@ int ReadJPaasHostPort(int container_port) { ...@@ -116,8 +116,8 @@ int ReadJPaasHostPort(int container_port) {
// Called in server.cpp // Called in server.cpp
void SetTrackMeAddress(butil::EndPoint pt) { void SetTrackMeAddress(butil::EndPoint pt) {
BAIDU_SCOPED_LOCK(g_trackme_mutex); BAIDU_SCOPED_LOCK(s_trackme_mutex);
if (g_trackme_addr == NULL) { if (s_trackme_addr == NULL) {
// JPAAS has NAT capabilities, read its log to figure out the open port // JPAAS has NAT capabilities, read its log to figure out the open port
// accessible from outside. // accessible from outside.
const int jpaas_port = ReadJPaasHostPort(pt.port); const int jpaas_port = ReadJPaasHostPort(pt.port);
...@@ -126,7 +126,7 @@ void SetTrackMeAddress(butil::EndPoint pt) { ...@@ -126,7 +126,7 @@ void SetTrackMeAddress(butil::EndPoint pt) {
<< " instead of jpaas_container_port=" << pt.port; << " instead of jpaas_container_port=" << pt.port;
pt.port = jpaas_port; pt.port = jpaas_port;
} }
g_trackme_addr = new std::string(butil::endpoint2str(pt).c_str()); s_trackme_addr = new std::string(butil::endpoint2str(pt).c_str());
} }
} }
...@@ -139,7 +139,7 @@ static void HandleTrackMeResponse(Controller* cntl, TrackMeResponse* res) { ...@@ -139,7 +139,7 @@ static void HandleTrackMeResponse(Controller* cntl, TrackMeResponse* res) {
cur_info.error_text = res->error_text(); cur_info.error_text = res->error_text();
bool already_reported = false; bool already_reported = false;
{ {
BAIDU_SCOPED_LOCK(g_trackme_mutex); BAIDU_SCOPED_LOCK(s_trackme_mutex);
if (g_bug_info != NULL && *g_bug_info == cur_info) { if (g_bug_info != NULL && *g_bug_info == cur_info) {
// we've shown the bug. // we've shown the bug.
already_reported = true; already_reported = true;
...@@ -187,10 +187,10 @@ static void HandleTrackMeResponse(Controller* cntl, TrackMeResponse* res) { ...@@ -187,10 +187,10 @@ static void HandleTrackMeResponse(Controller* cntl, TrackMeResponse* res) {
} }
static void TrackMeNow(std::unique_lock<pthread_mutex_t>& mu) { static void TrackMeNow(std::unique_lock<pthread_mutex_t>& mu) {
if (g_trackme_addr == NULL) { if (s_trackme_addr == NULL) {
return; return;
} }
if (g_trackme_chan == NULL) { if (s_trackme_chan == NULL) {
Channel* chan = new (std::nothrow) Channel; Channel* chan = new (std::nothrow) Channel;
if (chan == NULL) { if (chan == NULL) {
LOG(FATAL) << "Fail to new trackme channel"; LOG(FATAL) << "Fail to new trackme channel";
...@@ -204,17 +204,17 @@ static void TrackMeNow(std::unique_lock<pthread_mutex_t>& mu) { ...@@ -204,17 +204,17 @@ static void TrackMeNow(std::unique_lock<pthread_mutex_t>& mu) {
delete chan; delete chan;
return; return;
} }
g_trackme_chan = chan; s_trackme_chan = chan;
} }
mu.unlock(); mu.unlock();
TrackMeService_Stub stub(g_trackme_chan); TrackMeService_Stub stub(s_trackme_chan);
TrackMeRequest req; TrackMeRequest req;
req.set_rpc_version(g_rpc_version); req.set_rpc_version(g_rpc_version);
req.set_server_addr(*g_trackme_addr); req.set_server_addr(*s_trackme_addr);
TrackMeResponse* res = new TrackMeResponse; TrackMeResponse* res = new TrackMeResponse;
Controller* cntl = new Controller; Controller* cntl = new Controller;
cntl->set_request_code(policy::MurmurHash32(g_trackme_addr->data(), g_trackme_addr->size())); cntl->set_request_code(policy::MurmurHash32(s_trackme_addr->data(), s_trackme_addr->size()));
google::protobuf::Closure* done = google::protobuf::Closure* done =
::brpc::NewCallback(&HandleTrackMeResponse, cntl, res); ::brpc::NewCallback(&HandleTrackMeResponse, cntl, res);
stub.TrackMe(cntl, &req, res, done); stub.TrackMe(cntl, &req, res, done);
} }
...@@ -226,15 +226,15 @@ void TrackMe() { ...@@ -226,15 +226,15 @@ void TrackMe() {
return; return;
} }
int64_t now = butil::gettimeofday_us(); int64_t now = butil::gettimeofday_us();
std::unique_lock<pthread_mutex_t> mu(g_trackme_mutex); std::unique_lock<pthread_mutex_t> mu(s_trackme_mutex);
if (g_trackme_last_time == 0) { if (s_trackme_last_time == 0) {
// Delay the first ping randomly within s_trackme_interval. This // Delay the first ping randomly within s_trackme_interval. This
// protects trackme_server from ping storms. // protects trackme_server from ping storms.
g_trackme_last_time = s_trackme_last_time =
now + butil::fast_rand_less_than(s_trackme_interval) * 1000000L; now + butil::fast_rand_less_than(s_trackme_interval) * 1000000L;
} }
if (now > g_trackme_last_time + 1000000L * s_trackme_interval) { if (now > s_trackme_last_time + 1000000L * s_trackme_interval) {
g_trackme_last_time = now; s_trackme_last_time = now;
return TrackMeNow(mu); return TrackMeNow(mu);
} }
} }
......
...@@ -146,7 +146,7 @@ void TaskGroup::run_main_task() { ...@@ -146,7 +146,7 @@ void TaskGroup::run_main_task() {
bvar::PassiveStatus<double> cumulated_cputime( bvar::PassiveStatus<double> cumulated_cputime(
get_cumulated_cputime_from_this, this); get_cumulated_cputime_from_this, this);
std::unique_ptr<bvar::PerSecond<bvar::PassiveStatus<double> > > usage_bvar; std::unique_ptr<bvar::PerSecond<bvar::PassiveStatus<double> > > usage_bvar;
TaskGroup* dummy = this; TaskGroup* dummy = this;
bthread_t tid; bthread_t tid;
while (wait_task(&tid)) { while (wait_task(&tid)) {
...@@ -169,7 +169,6 @@ void TaskGroup::run_main_task() { ...@@ -169,7 +169,6 @@ void TaskGroup::run_main_task() {
(name, &cumulated_cputime, 1)); (name, &cumulated_cputime, 1));
} }
} }
// stop_main_task() was called.
// Don't forget to add elapse of last wait_task. // Don't forget to add elapse of last wait_task.
current_task()->stat.cputime_ns += butil::cpuwide_time_ns() - _last_run_ns; current_task()->stat.cputime_ns += butil::cpuwide_time_ns() - _last_run_ns;
} }
...@@ -188,7 +187,7 @@ TaskGroup::TaskGroup(TaskControl* c) ...@@ -188,7 +187,7 @@ TaskGroup::TaskGroup(TaskControl* c)
, _nswitch(0) , _nswitch(0)
, _last_context_remained(NULL) , _last_context_remained(NULL)
, _last_context_remained_arg(NULL) , _last_context_remained_arg(NULL)
, _pl(NULL) , _pl(NULL)
, _main_stack(NULL) , _main_stack(NULL)
, _main_tid(0) , _main_tid(0)
, _remote_num_nosignal(0) , _remote_num_nosignal(0)
...@@ -275,7 +274,7 @@ void TaskGroup::task_runner(intptr_t skip_remained) { ...@@ -275,7 +274,7 @@ void TaskGroup::task_runner(intptr_t skip_remained) {
// user function is never called, the variables will be unchanged // user function is never called, the variables will be unchanged
// however they'd better reflect failures because the task is stopped // however they'd better reflect failures because the task is stopped
// abnormally. // abnormally.
// Meta and identifier of the task is persistent in this run. // Meta and identifier of the task is persistent in this run.
TaskMeta* const m = g->_cur_meta; TaskMeta* const m = g->_cur_meta;
...@@ -286,25 +285,25 @@ void TaskGroup::task_runner(intptr_t skip_remained) { ...@@ -286,25 +285,25 @@ void TaskGroup::task_runner(intptr_t skip_remained) {
g->_control->exposed_pending_time() << g->_control->exposed_pending_time() <<
(butil::cpuwide_time_ns() - m->cpuwide_start_ns) / 1000L; (butil::cpuwide_time_ns() - m->cpuwide_start_ns) / 1000L;
} }
// Not catch exceptions except ExitException which is for implementing // Not catch exceptions except ExitException which is for implementing
// bthread_exit(). User code is intended to crash when an exception is // bthread_exit(). User code is intended to crash when an exception is
// not caught explicitly. This is consistent with other threading // not caught explicitly. This is consistent with other threading
// libraries. // libraries.
void* thread_return; void* thread_return;
try { try {
thread_return = m->fn(m->arg); thread_return = m->fn(m->arg);
} catch (ExitException& e) { } catch (ExitException& e) {
thread_return = e.value(); thread_return = e.value();
} }
// Group is probably changed // Group is probably changed
g = tls_task_group; g = tls_task_group;
// TODO: Save thread_return // TODO: Save thread_return
(void)thread_return; (void)thread_return;
// Logging must be done before returning the keytable, since the logging lib // Logging must be done before returning the keytable, since the logging lib
// use bthread local storage internally, or will cause memory leak. // use bthread local storage internally, or will cause memory leak.
// FIXME: the time from quiting fn to here is not counted into cputime // FIXME: the time from quiting fn to here is not counted into cputime
if (m->attr.flags & BTHREAD_LOG_START_AND_FINISH) { if (m->attr.flags & BTHREAD_LOG_START_AND_FINISH) {
...@@ -322,7 +321,7 @@ void TaskGroup::task_runner(intptr_t skip_remained) { ...@@ -322,7 +321,7 @@ void TaskGroup::task_runner(intptr_t skip_remained) {
tls_bls.keytable = NULL; tls_bls.keytable = NULL;
m->local_storage.keytable = NULL; // optional m->local_storage.keytable = NULL; // optional
} }
// Increase the version and wake up all joiners, if resulting version // Increase the version and wake up all joiners, if resulting version
// is 0, change it to 1 to make bthread_t never be 0. Any access // is 0, change it to 1 to make bthread_t never be 0. Any access
// or join to the bthread after changing version will be rejected. // or join to the bthread after changing version will be rejected.
...@@ -338,9 +337,9 @@ void TaskGroup::task_runner(intptr_t skip_remained) { ...@@ -338,9 +337,9 @@ void TaskGroup::task_runner(intptr_t skip_remained) {
g->_control->_nbthreads << -1; g->_control->_nbthreads << -1;
g->set_remained(TaskGroup::_release_last_context, m); g->set_remained(TaskGroup::_release_last_context, m);
ending_sched(&g); ending_sched(&g);
} while (g->_cur_meta->tid != g->_main_tid); } while (g->_cur_meta->tid != g->_main_tid);
// Was called from a pthread and we don't have BTHREAD_STACKTYPE_PTHREAD // Was called from a pthread and we don't have BTHREAD_STACKTYPE_PTHREAD
// tasks to run, quit for more tasks. // tasks to run, quit for more tasks.
} }
...@@ -591,7 +590,7 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta) { ...@@ -591,7 +590,7 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta) {
cur_meta->local_storage = tls_bls; cur_meta->local_storage = tls_bls;
tls_bls = next_meta->local_storage; tls_bls = next_meta->local_storage;
// Logging must be done after switching the local storage, since the logging lib // Logging must be done after switching the local storage, since the logging lib
// use bthread local storage internally, or will cause memory leak. // use bthread local storage internally, or will cause memory leak.
if ((cur_meta->attr.flags & BTHREAD_LOG_CONTEXT_SWITCH) || if ((cur_meta->attr.flags & BTHREAD_LOG_CONTEXT_SWITCH) ||
(next_meta->attr.flags & BTHREAD_LOG_CONTEXT_SWITCH)) { (next_meta->attr.flags & BTHREAD_LOG_CONTEXT_SWITCH)) {
...@@ -741,7 +740,7 @@ void TaskGroup::_add_sleep_event(void* void_args) { ...@@ -741,7 +740,7 @@ void TaskGroup::_add_sleep_event(void* void_args) {
// will be gone. // will be gone.
SleepArgs e = *static_cast<SleepArgs*>(void_args); SleepArgs e = *static_cast<SleepArgs*>(void_args);
TaskGroup* g = e.group; TaskGroup* g = e.group;
TimerThread::TaskId sleep_id; TimerThread::TaskId sleep_id;
sleep_id = get_global_timer_thread()->schedule( sleep_id = get_global_timer_thread()->schedule(
ready_to_run_from_timer_thread, void_args, ready_to_run_from_timer_thread, void_args,
...@@ -752,7 +751,7 @@ void TaskGroup::_add_sleep_event(void* void_args) { ...@@ -752,7 +751,7 @@ void TaskGroup::_add_sleep_event(void* void_args) {
g->ready_to_run(e.tid); g->ready_to_run(e.tid);
return; return;
} }
// Set TaskMeta::current_sleep which is for interruption. // Set TaskMeta::current_sleep which is for interruption.
const uint32_t given_ver = get_version(e.tid); const uint32_t given_ver = get_version(e.tid);
{ {
...@@ -931,7 +930,7 @@ void print_task(std::ostream& os, bthread_t tid) { ...@@ -931,7 +930,7 @@ void print_task(std::ostream& os, bthread_t tid) {
<< "\narg=" << (void*)arg << "\narg=" << (void*)arg
<< "\nattr={stack_type=" << attr.stack_type << "\nattr={stack_type=" << attr.stack_type
<< " flags=" << attr.flags << " flags=" << attr.flags
<< " keytable_pool=" << attr.keytable_pool << " keytable_pool=" << attr.keytable_pool
<< "}\nhas_tls=" << has_tls << "}\nhas_tls=" << has_tls
<< "\nuptime_ns=" << butil::cpuwide_time_ns() - cpuwide_start_ns << "\nuptime_ns=" << butil::cpuwide_time_ns() - cpuwide_start_ns
<< "\ncputime_ns=" << stat.cputime_ns << "\ncputime_ns=" << stat.cputime_ns
......
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