Skip to content

Commit

Permalink
add CPU usage (OpenAtomFoundation#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leviathan1995 authored and KernelMaker committed Dec 21, 2017
1 parent 56d4426 commit ed7d6a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/pika_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#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"
Expand Down Expand Up @@ -169,6 +172,7 @@ class InfoCmd : public Cmd {
kInfoClients,
kInfoHub,
kInfoStats,
kInfoCPU,
kInfoReplication,
kInfoKeyspace,
kInfoBgstats,
Expand All @@ -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;
Expand All @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -564,6 +569,9 @@ void InfoCmd::Do() {
case kInfoStats:
InfoStats(info);
break;
case kInfoCPU:
InfoCPU(info);
break;
case kInfoReplication:
InfoReplication(info);
break;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ed7d6a4

Please sign in to comment.