Skip to content

Commit

Permalink
feat: add info section: server stats cpu commandstats (#338)
Browse files Browse the repository at this point in the history
* add infoCmd:server stats cpu commandstats

---------

Co-authored-by: Xin.Zh <[email protected]>
  • Loading branch information
hahahashen and AlexStocks committed Aug 26, 2024
1 parent 446fb66 commit 753b7a1
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 41 deletions.
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
46 changes: 46 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,41 @@

namespace pikiwidb {

struct CommandStatistics {
CommandStatistics() = default;
CommandStatistics(const CommandStatistics& other)
: cmd_count_(other.cmd_count_.load()), cmd_time_consuming_(other.cmd_time_consuming_.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_ = TimePoint::min();
TimePoint dequeue_ts_ = TimePoint::min();
TimePoint process_done_ts_ = TimePoint::min();
};

class CmdRes {
public:
enum CmdRet {
Expand Down Expand Up @@ -236,6 +272,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 +331,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

0 comments on commit 753b7a1

Please sign in to comment.