Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add info section: server stats cpu commandstats #338

Merged
merged 15 commits into from
Aug 26, 2024
12 changes: 11 additions & 1 deletion src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

#include "base_cmd.h"
#include "config.h"
#include "env.h"
#include "pikiwidb.h"
#include "pstd_string.h"
#include "slow_log.h"
#include "store.h"

namespace pikiwidb {

Expand Down Expand Up @@ -353,7 +357,8 @@ int PClient::handlePacket(const char* start, int bytes) {
// executeCommand();
// return static_cast<int>(ptr - start);
// }

auto now = std::chrono::steady_clock::now();
time_stat_->SetEnqueueTs(now);
g_pikiwidb->SubmitFast(std::make_shared<CmdThreadPoolTask>(shared_from_this()));

// check transaction
Expand Down Expand Up @@ -424,6 +429,7 @@ PClient* PClient::Current() { return s_current; }
PClient::PClient() : parser_(params_) {
auth_ = false;
reset();
time_stat_.reset(new TimeStat());
}

void PClient::OnConnect() {
Expand Down Expand Up @@ -665,4 +671,8 @@ void PClient::SetKey(std::vector<std::string>& names) {
keys_ = std::move(names); // use std::move clear copy expense
}

std::unordered_map<std::string, CommandStatistics>* PClient::GetCommandStatMap() { return &cmdstat_map_; }

std::shared_ptr<TimeStat> PClient::GetTimeStat() { return time_stat_; }

} // namespace pikiwidb
48 changes: 48 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <chrono>
#include <set>
#include <span>
#include <unordered_map>
Expand All @@ -21,6 +22,43 @@

namespace pikiwidb {

struct CommandStatistics {
CommandStatistics() = default;
CommandStatistics(const CommandStatistics& other)
:cmd_count_(other.cmd_count_.load()), cmd_time_consuming_(other.cmd_time_consuming_.load()) {
cmd_time_consuming_.store(other.cmd_time_consuming_.load());
cmd_count_.store(other.cmd_count_.load());
}
std::atomic<uint64_t> cmd_count_ = 0;
std::atomic<uint64_t> cmd_time_consuming_ = 0;
};

struct TimeStat {
using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;

TimeStat() = default;

void Reset() {
enqueue_ts_ = TimePoint::min();
dequeue_ts_ = TimePoint::min();
process_done_ts_ = TimePoint::min();
}

uint64_t GetTotalTime() const {
return (process_done_ts_ > enqueue_ts_)
? std::chrono::duration_cast<std::chrono::milliseconds>(process_done_ts_ - enqueue_ts_).count()
: 0;
}

void SetEnqueueTs(TimePoint now_time) { enqueue_ts_ = now_time; }
void SetDequeueTs(TimePoint now_time) { dequeue_ts_ = now_time; }
void SetProcessDoneTs(TimePoint now_time) { process_done_ts_ = now_time; }

TimePoint enqueue_ts_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是可以初始化为 TimePoint::min()?

TimePoint dequeue_ts_;
TimePoint process_done_ts_;
};

class CmdRes {
public:
enum CmdRet {
Expand Down Expand Up @@ -236,6 +274,10 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
// e.g:["set","key","value"]
std::span<std::string> argv_;

// Info Commandstats used
std::unordered_map<std::string, CommandStatistics>* GetCommandStatMap();
std::shared_ptr<TimeStat> GetTimeStat();

// std::shared_ptr<TcpConnection> getTcpConnection() const { return tcp_connection_.lock(); }
int handlePacket(const char*, int);

Expand Down Expand Up @@ -291,5 +333,11 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
net::SocketAddr addr_;

static thread_local PClient* s_current;

/*
* Info Commandstats used
*/
std::unordered_map<std::string, CommandStatistics> cmdstat_map_;
std::shared_ptr<TimeStat> time_stat_;
};
} // namespace pikiwidb
Loading
Loading