add CPU usage
MODIFY
include/pika_admin.h
include/pika_admin.h
#define PIKA_ADMIN_H_
#include <sstream>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <iomanip>
#include "include/pika_command.h"
#include "include/pika_client_conn.h"
@@ -169,6 +172,7 @@ class InfoCmd : public Cmd {
kInfoClients,
kInfoHub,
kInfoStats,
+ kInfoCPU,
kInfoReplication,
kInfoKeyspace,
kInfoBgstats,
@@ -191,6 +195,7 @@ class InfoCmd : public Cmd {
const static std::string kClientsSection;
const static std::string kHubSection;
const static std::string kStatsSection;
+ const static std::string kCPUSection;
const static std::string kReplicationSection;
const static std::string kKeyspaceSection;
const static std::string kLogSection;
@@ -207,6 +212,7 @@ class InfoCmd : public Cmd {
void InfoClients(std::string &info);
void InfoHub(std::string &info);
void InfoStats(std::string &info);
+ void InfoCPU(std::string &info);
void InfoReplication(std::string &info);
void InfoKeyspace(std::string &info);
void InfoLog(std::string &info);

MODIFY
src/pika_admin.cc
src/pika_admin.cc
@@ -473,6 +473,7 @@ const std::string InfoCmd::kServerSection = "server";
const std::string InfoCmd::kClientsSection = "clients";
const std::string InfoCmd::kHubSection = "hub";
const std::string InfoCmd::kStatsSection = "stats";
+const std::string InfoCmd::kCPUSection = "cpu";
const std::string InfoCmd::kReplicationSection = "replication";
const std::string InfoCmd::kKeyspaceSection = "keyspace";
const std::string InfoCmd::kLogSection = "log";
@@ -501,6 +502,8 @@ void InfoCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
info_section_ = kInfoHub;
} else if (argv[1] == kStatsSection) {
info_section_ = kInfoStats;
+ } else if (argv[1] == kCPUSection) {
+ info_section_ = kInfoCPU;
} else if (argv[1] == kReplicationSection) {
info_section_ = kInfoReplication;
} else if (argv[1] == kKeyspaceSection) {
@@ -546,6 +549,8 @@ void InfoCmd::Do() {
info.append("\r\n");
InfoStats(info);
info.append("\r\n");
+ InfoCPU(info);
+ info.append("\r\n");
InfoReplication(info);
info.append("\r\n");
InfoKeyspace(info);
@@ -564,6 +569,9 @@ void InfoCmd::Do() {
case kInfoStats:
InfoStats(info);
break;
+ case kInfoCPU:
+ InfoCPU(info);
+ break;
case kInfoReplication:
InfoReplication(info);
break;
@@ -672,6 +680,31 @@ void InfoCmd::InfoStats(std::string &info) {
info.append(tmp_stream.str());
}
+void InfoCmd::InfoCPU(std::string &info) {
+ struct rusage self_ru, c_ru;
+ getrusage(RUSAGE_SELF, &self_ru);
+ getrusage(RUSAGE_CHILDREN, &c_ru);
+ std::stringstream tmp_stream;
+ tmp_stream << "# CPU\r\n";
+ tmp_stream << "used_cpu_sys:" <<
+ setiosflags(std::ios::fixed) << std::setprecision(2) <<
+ (float)self_ru.ru_stime.tv_sec+(float)self_ru.ru_stime.tv_usec/1000000 <<
+ "\r\n";
+ tmp_stream << "used_cpu_user:" <<
+ setiosflags(std::ios::fixed) << std::setprecision(2) <<
+ (float)self_ru.ru_utime.tv_sec+(float)self_ru.ru_utime.tv_usec/1000000 <<
+ "\r\n";
+ tmp_stream << "used_cpu_sys_children:" <<
+ setiosflags(std::ios::fixed) << std::setprecision(2) <<
+ (float)c_ru.ru_stime.tv_sec+(float)c_ru.ru_stime.tv_usec/1000000 <<
+ "\r\n";
+ tmp_stream << "used_cpu_user_children:" <<
+ setiosflags(std::ios::fixed) << std::setprecision(2) <<
+ (float)c_ru.ru_utime.tv_sec+(float)c_ru.ru_utime.tv_usec/1000000 <<
+ "\r\n";
+ info.append(tmp_stream.str());
+}
+
void InfoCmd::InfoReplication(std::string &info) {
int host_role = g_pika_server->role();
std::stringstream tmp_stream;

add CPU usage (#218)