Skip to content

Commit

Permalink
Opt1
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Dec 18, 2024
1 parent 2372541 commit 7c5d9cf
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: run tests
run: |
cd test
for i in `seq 1 10`; do sh ./run_tests.sh; done
for i in `seq 1 50`; do sh ./run_tests.sh; done
# gcc-compile-with-make:
# runs-on: ubuntu-20.04 # https://github.com/actions/runner-images
Expand Down
1 change: 1 addition & 0 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* [bthread or not](docs/cn/bthread_or_not.md)
* [thread-local](docs/cn/thread_local.md)
* [Execution Queue](docs/cn/execution_queue.md)
* [bthread tracer](docs/cn/bthread_tracer.md)
* Client
* [基础功能](docs/cn/client.md)
* [错误码](docs/cn/error_code.md)
Expand Down
12 changes: 11 additions & 1 deletion docs/cn/bthread_tracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ jump_stack是bthread挂起或者运行的必经之路,也是STB的拦截点。
3. 访问服务的内置服务:`http://ip:port/bthreads/<bthread_id>?st=1`或者代码里调用`bthread::stack_trace()`函数。
4. 如果希望追踪pthread的调用栈,在对应pthread上调用`bthread::init_for_pthread_stack_trace()`函数获取一个伪bthread_t,然后使用步骤3即可获取pthread调用栈。

下面是追踪bthread调用栈的输出示例:
```shell
#0 0x00007fdbbed500b5 __clock_gettime_2
#1 0x000000000041f2b6 butil::cpuwide_time_ns()
#2 0x000000000041f289 butil::cpuwide_time_us()
#3 0x000000000041f1b9 butil::EveryManyUS::operator bool()
#4 0x0000000000413289 (anonymous namespace)::spin_and_log()
#5 0x00007fdbbfa58dc0 bthread::TaskGroup::task_runner()
```

# 相关flag

- `enable_fast_unwind`:是否启用快速回溯功能,默认为true。大多数情况下,不需要关闭快速回溯功能。除非你关注的调用栈函数名转换失败,显示为`<unknown>`,则可以尝试关闭快速回溯功能,但这会导致性能下降。以包含30帧的调用栈举例,快速回溯只需要400~500us,而关闭快速回溯则需要4ms左右,性能下降了近10倍
- `enable_fast_unwind`:是否启用快速回溯功能,默认为true。大多数情况下,不需要关闭快速回溯功能。除非你关注的调用栈函数名转换失败,显示为`<unknown>`,则可以尝试关闭快速回溯功能,但这会导致性能下降。以包含30帧的调用栈举例,快速回溯基本上在200us以内就可以完成,而关闭快速回溯则需要4ms左右,性能下降了近20倍
- `signal_trace_timeout_ms`:信号追踪模式的超时时间,默认为50ms。虽然libunwind文档显示回溯功能是异步信号安全的,但是[gpertools社区发现libunwind在某些情况下会死锁](https://github.com/gperftools/gperftools/issues/775),所以TaskTracer会设置了超时时间,超时后会放弃回溯,打破死锁。
3 changes: 1 addition & 2 deletions src/brpc/input_messenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ int InputMessenger::ProcessNewMessage(
// This unique_ptr prevents msg to be lost before transfering
// ownership to last_msg
DestroyingPtr<InputMessageBase> msg(pr.message());
QueueMessage(last_msg.release(), &num_bthread_created,
m->_keytable_pool);
QueueMessage(last_msg.release(), &num_bthread_created, m->_keytable_pool);
if (_handlers[index].process == NULL) {
LOG(ERROR) << "process of index=" << index << " is NULL";
continue;
Expand Down
46 changes: 2 additions & 44 deletions src/brpc/shared_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,11 @@
#ifndef BRPC_SHARED_OBJECT_H
#define BRPC_SHARED_OBJECT_H

#include "butil/intrusive_ptr.hpp" // butil::intrusive_ptr
#include "butil/atomicops.h"

#include "butil/shared_object.h"

namespace brpc {

// Inherit this class to be intrusively shared. Comparing to shared_ptr,
// intrusive_ptr saves one malloc (for shared_count) and gets better cache
// locality when the ref/deref are frequent, in the cost of lack of weak_ptr
// and worse interface.
class SharedObject {
friend void intrusive_ptr_add_ref(SharedObject*);
friend void intrusive_ptr_release(SharedObject*);

public:
SharedObject() : _nref(0) { }
int ref_count() const { return _nref.load(butil::memory_order_relaxed); }

// Add ref and returns the ref_count seen before added.
// The effect is basically same as butil::intrusive_ptr<T>(obj).detach()
// except that the latter one does not return the seen ref_count which is
// useful in some scenarios.
int AddRefManually()
{ return _nref.fetch_add(1, butil::memory_order_relaxed); }

// Remove one ref, if the ref_count hit zero, delete this object.
// Same as butil::intrusive_ptr<T>(obj, false).reset(NULL)
void RemoveRefManually() {
if (_nref.fetch_sub(1, butil::memory_order_release) == 1) {
butil::atomic_thread_fence(butil::memory_order_acquire);
delete this;
}
}

protected:
virtual ~SharedObject() { }
private:
butil::atomic<int> _nref;
};

inline void intrusive_ptr_add_ref(SharedObject* obj) {
obj->AddRefManually();
}

inline void intrusive_ptr_release(SharedObject* obj) {
obj->RemoveRefManually();
}
using butil::SharedObject;

} // namespace brpc

Expand Down
6 changes: 4 additions & 2 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,13 +1013,15 @@ void print_task(std::ostream& os, bthread_t tid) {
<< "}\nhas_tls=" << has_tls
<< "\nuptime_ns=" << butil::cpuwide_time_ns() - cpuwide_start_ns
<< "\ncputime_ns=" << stat.cputime_ns
<< "\nnswitch=" << stat.nswitch
<< "\nnswitch=" << stat.nswitch
#ifdef BRPC_BTHREAD_TRACER
<< "\nstatus=" << status
<< "\ntraced=" << traced
<< "\nworker_tid=" << worker_tid;
#endif // BRPC_BTHREAD_TRACER
#else
;
(void)status;(void)traced;(void)worker_tid;
#endif // BRPC_BTHREAD_TRACER
}
}

Expand Down
Loading

0 comments on commit 7c5d9cf

Please sign in to comment.