Skip to content

Commit

Permalink
feat:给常用模块都加了RECORD_SCOPE()
Browse files Browse the repository at this point in the history
  • Loading branch information
hevake committed Jun 10, 2024
1 parent b9eeec5 commit d5c9895
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/alarm/alarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/event/loop.h>
#include <tbox/event/timer_event.h>

Expand Down Expand Up @@ -187,6 +188,7 @@ void Alarm::onTimeExpired() {
state_ = State::kInited;
activeTimer();

RECORD_SCOPE();
++cb_level_;
if (cb_)
cb_();
Expand Down
2 changes: 2 additions & 0 deletions modules/eventx/loop_wdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/defines.h>
#include <tbox/base/wrapped_recorder.h>

namespace tbox {
namespace eventx {
Expand Down Expand Up @@ -92,6 +93,7 @@ void CheckLoopTag() {
if (loop_info_sptr->state == State::kTobeCheck) {
loop_info_sptr->state = State::kTimeout;
_loop_die_cb(loop_info_sptr->name);
RECORD_EVENT();
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions modules/eventx/timer_fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/defines.h>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/event/loop.h>
#include <tbox/event/fd_event.h>

Expand Down Expand Up @@ -183,6 +184,7 @@ void TimerFd::onEvent(short events)
disable();

if (d_->cb) {
RECORD_SCOPE();
++d_->cb_level;
d_->cb();
--d_->cb_level;
Expand Down
7 changes: 7 additions & 0 deletions modules/http/server/server_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/util/buffer.h>

#include "middleware.h"
Expand Down Expand Up @@ -104,13 +105,15 @@ void Server::Impl::use(Middleware *wp_middleware)

void Server::Impl::onTcpConnected(const TcpServer::ConnToken &ct)
{
RECORD_SCOPE();
auto conn = new Connection;
tcp_server_.setContext(ct, conn);
conns_.insert(conn);
}

void Server::Impl::onTcpDisconnected(const TcpServer::ConnToken &ct)
{
RECORD_SCOPE();
Connection *conn = static_cast<Connection*>(tcp_server_.getContext(ct));
TBOX_ASSERT(conn != nullptr);

Expand Down Expand Up @@ -142,6 +145,7 @@ bool IsLastRequest(const Request *req)

void Server::Impl::onTcpReceived(const TcpServer::ConnToken &ct, Buffer &buff)
{
RECORD_SCOPE();
Connection *conn = static_cast<Connection*>(tcp_server_.getContext(ct));
TBOX_ASSERT(conn != nullptr);

Expand Down Expand Up @@ -188,6 +192,7 @@ void Server::Impl::onTcpReceived(const TcpServer::ConnToken &ct, Buffer &buff)

void Server::Impl::onTcpSendCompleted(const TcpServer::ConnToken &ct)
{
RECORD_SCOPE();
if (!tcp_server_.isClientValid(ct))
return;

Expand All @@ -209,6 +214,7 @@ void Server::Impl::onTcpSendCompleted(const TcpServer::ConnToken &ct)
*/
void Server::Impl::commitRespond(const TcpServer::ConnToken &ct, int index, Respond *res)
{
RECORD_SCOPE();
if (!tcp_server_.isClientValid(ct)) {
delete res;
return;
Expand Down Expand Up @@ -264,6 +270,7 @@ void Server::Impl::commitRespond(const TcpServer::ConnToken &ct, int index, Resp

void Server::Impl::handle(ContextSptr sp_ctx, size_t cb_index)
{
RECORD_SCOPE();
if (cb_index >= req_cb_.size())
return;

Expand Down
7 changes: 7 additions & 0 deletions modules/jsonrpc/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <tbox/base/log.h>
#include <tbox/base/json.hpp>
#include <tbox/base/wrapped_recorder.h>
#include "proto.h"
#include "inner_types.h"

Expand Down Expand Up @@ -69,6 +70,7 @@ void Rpc::cleanup()

void Rpc::request(const std::string &method, const Json &js_params, RequestCallback &&cb)
{
RECORD_SCOPE();
int id = 0;
if (cb) {
id = ++id_alloc_;
Expand Down Expand Up @@ -100,6 +102,7 @@ void Rpc::addService(const std::string &method, ServiceCallback &&cb)

void Rpc::respond(int id, int errcode, const Json &js_result)
{
RECORD_SCOPE();
if (id == 0) {
LogWarn("send id == 0 respond");
return;
Expand All @@ -116,6 +119,7 @@ void Rpc::respond(int id, int errcode, const Json &js_result)

void Rpc::respond(int id, const Json &js_result)
{
RECORD_SCOPE();
if (id == 0) {
LogWarn("send id == 0 respond");
return;
Expand All @@ -127,6 +131,7 @@ void Rpc::respond(int id, const Json &js_result)

void Rpc::respond(int id, int errcode)
{
RECORD_SCOPE();
if (id == 0) {
LogWarn("send id == 0 respond");
return;
Expand All @@ -138,6 +143,7 @@ void Rpc::respond(int id, int errcode)

void Rpc::onRecvRequest(int id, const std::string &method, const Json &js_params)
{
RECORD_SCOPE();
auto iter = method_services_.find(method);
if (iter != method_services_.end() && iter->second) {
int errcode = 0;
Expand All @@ -159,6 +165,7 @@ void Rpc::onRecvRequest(int id, const std::string &method, const Json &js_params

void Rpc::onRecvRespond(int id, int errcode, const Json &js_result)
{
RECORD_SCOPE();
auto iter = request_callback_.find(id);
if (iter != request_callback_.end()) {
if (iter->second)
Expand Down
15 changes: 15 additions & 0 deletions modules/mqtt/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/lifetime_tag.hpp>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/event/timer_event.h>
#include <tbox/event/fd_event.h>

Expand Down Expand Up @@ -209,6 +210,7 @@ bool Client::start()
return false;
}

RECORD_SCOPE();
//! 基础设置
const char *client_id = nullptr;
if (!d_->config.base.client_id.empty())
Expand Down Expand Up @@ -289,6 +291,7 @@ bool Client::start()
//! 由于 mosquitto_connect() 是阻塞函数,为了避免阻塞其它事件,特交给子线程去做
d_->sp_thread = new thread(
[this, is_alive] {
RECORD_SCOPE();
int ret = mosquitto_connect_async(d_->sp_mosq,
d_->config.base.broker.domain.c_str(),
d_->config.base.broker.port,
Expand All @@ -312,6 +315,7 @@ void Client::stop()
if (d_->state <= State::kInited)
return;

RECORD_SCOPE();
if (d_->state == State::kTcpConnected ||
d_->state == State::kMqttConnected) {
//! 如果已成功连接,则立即断开
Expand All @@ -337,6 +341,7 @@ int Client::subscribe(const std::string &topic, int *p_mid, int qos)
return false;
}

RECORD_SCOPE();
int mid = 0;
int ret = mosquitto_subscribe(d_->sp_mosq, &mid, topic.c_str(), qos);
if (p_mid != nullptr)
Expand All @@ -353,6 +358,7 @@ int Client::unsubscribe(const std::string &topic, int *p_mid)
return false;
}

RECORD_SCOPE();
int mid = 0;
int ret = mosquitto_unsubscribe(d_->sp_mosq, &mid, topic.c_str());
if (p_mid != nullptr)
Expand All @@ -370,6 +376,7 @@ int Client::publish(const std::string &topic, const void *payload_ptr, size_t pa
return false;
}

RECORD_SCOPE();
int mid = 0;
int ret = mosquitto_publish(d_->sp_mosq, &mid, topic.c_str(),
payload_size, payload_ptr,
Expand Down Expand Up @@ -405,6 +412,7 @@ void Client::onTimerTick()
auto is_alive = d_->alive_tag.get(); //! 原理见Q1
d_->sp_thread = new thread(
[this, is_alive] {
RECORD_SCOPE();
int ret = mosquitto_reconnect_async(d_->sp_mosq);
d_->wp_loop->runInLoop(
[this, is_alive, ret] {
Expand Down Expand Up @@ -483,6 +491,7 @@ void Client::onConnected(int rc)

LogInfo("connected");

RECORD_SCOPE();
if (d_->state != State::kMqttConnected) {
d_->state = State::kMqttConnected;
++d_->cb_level;
Expand All @@ -494,6 +503,7 @@ void Client::onConnected(int rc)

void Client::onDisconnected(int rc)
{
RECORD_SCOPE();
disableSocketRead();
disableSocketWrite();

Expand All @@ -515,6 +525,7 @@ void Client::onPublish(int mid)
{
LogInfo("mid:%d", mid);

RECORD_SCOPE();
++d_->cb_level;
if (d_->callbacks.message_pub)
d_->callbacks.message_pub(mid);
Expand All @@ -525,6 +536,7 @@ void Client::onSubscribe(int mid, int qos, const int *granted_qos)
{
LogInfo("mid:%d, qos:%d", mid, qos);

RECORD_SCOPE();
++d_->cb_level;
if (d_->callbacks.subscribed)
d_->callbacks.subscribed(mid, qos, granted_qos);
Expand All @@ -535,6 +547,7 @@ void Client::onUnsubscribe(int mid)
{
LogInfo("mid:%d", mid);

RECORD_SCOPE();
++d_->cb_level;
if (d_->callbacks.unsubscribed)
d_->callbacks.unsubscribed(mid);
Expand All @@ -548,6 +561,7 @@ void Client::onMessage(const struct mosquitto_message *msg)

LogInfo("mid:%d, topic:%s", msg->mid, msg->topic);

RECORD_SCOPE();
++d_->cb_level;
if (d_->callbacks.message_recv)
d_->callbacks.message_recv(msg->mid,
Expand Down Expand Up @@ -582,6 +596,7 @@ void Client::onTcpConnectDone(int ret, bool first_connect)
if (d_->sp_thread == nullptr)
return;

RECORD_SCOPE();
d_->sp_thread->join();
CHECK_DELETE_RESET_OBJ(d_->sp_thread);

Expand Down
3 changes: 3 additions & 0 deletions modules/network/buffered_fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cstring>
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/event/loop.h>
#include <tbox/event/fd_event.h>

Expand Down Expand Up @@ -180,6 +181,7 @@ void BufferedFd::shrinkSendBuffer()

void BufferedFd::onReadCallback(short)
{
RECORD_SCOPE();
struct iovec rbuf[2];
char extbuf[1024]; //! 扩展存储空间

Expand Down Expand Up @@ -246,6 +248,7 @@ void BufferedFd::onReadCallback(short)

void BufferedFd::onWriteCallback(short)
{
RECORD_SCOPE();
//! 如果发送缓冲中已无数据要发送了,那就关闭可写事件
if (send_buff_.readableSize() == 0) {
sp_write_event_->disable();
Expand Down
2 changes: 2 additions & 0 deletions modules/network/dns_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <memory>

#include <tbox/base/assert.h>
#include <tbox/base/wrapped_recorder.h>
#include <tbox/util/serializer.h>
#include <tbox/util/string.h>
#include <tbox/util/fs.h>
Expand Down Expand Up @@ -210,6 +211,7 @@ void DnsRequest::onUdpRecv(const void *data_ptr, size_t data_size, const SockAdd
if (requests_.empty())
return;

RECORD_SCOPE();
util::Deserializer parser(data_ptr, data_size);

uint16_t req_id, flags;
Expand Down
2 changes: 2 additions & 0 deletions modules/network/tcp_acceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/wrapped_recorder.h>

#include "tcp_connection.h"

Expand Down Expand Up @@ -147,6 +148,7 @@ void TcpAcceptor::onSocketRead(short events)

void TcpAcceptor::onClientConnected()
{
RECORD_SCOPE();
struct sockaddr addr;
socklen_t addr_len = sizeof(addr);
SocketFd peer_sock = sock_fd_.accept(&addr, &addr_len);
Expand Down
5 changes: 5 additions & 0 deletions modules/network/tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/defines.h>
#include <tbox/base/wrapped_recorder.h>

#include "tcp_connector.h"
#include "tcp_connection.h"
Expand Down Expand Up @@ -204,6 +205,7 @@ void TcpClient::unbind()

void TcpClient::onTcpConnected(TcpConnection *new_conn)
{
RECORD_SCOPE();
new_conn->setDisconnectedCallback(std::bind(&TcpClient::onTcpDisconnected, this));
new_conn->setReceiveCallback(d_->received_cb, d_->received_threshold);
new_conn->setSendCompleteCallback(d_->send_complete_cb);
Expand All @@ -217,6 +219,7 @@ void TcpClient::onTcpConnected(TcpConnection *new_conn)
d_->state = State::kConnected;

if (d_->connected_cb) {
RECORD_SCOPE();
++d_->cb_level;
d_->connected_cb();
--d_->cb_level;
Expand All @@ -225,6 +228,7 @@ void TcpClient::onTcpConnected(TcpConnection *new_conn)

void TcpClient::onTcpDisconnected()
{
RECORD_SCOPE();
TcpConnection *tobe_delete = nullptr;
std::swap(tobe_delete, d_->sp_connection);

Expand All @@ -240,6 +244,7 @@ void TcpClient::onTcpDisconnected()
start();

if (d_->disconnected_cb) {
RECORD_SCOPE();
++d_->cb_level;
d_->disconnected_cb();
--d_->cb_level;
Expand Down
2 changes: 2 additions & 0 deletions modules/network/tcp_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <tbox/base/log.h>
#include <tbox/base/assert.h>
#include <tbox/base/wrapped_recorder.h>

#include "tcp_connection.h"

Expand Down Expand Up @@ -269,6 +270,7 @@ void TcpConnector::onConnectFail()

void TcpConnector::onSocketWritable()
{
RECORD_SCOPE();
//! 读取Socket错误码
int sock_errno = 0;
socklen_t optlen = sizeof(sock_errno);
Expand Down
Loading

0 comments on commit d5c9895

Please sign in to comment.