Skip to content

Commit

Permalink
add ProcessInfo::cpuTime().
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed Mar 15, 2014
1 parent 6171a47 commit 77b8ec2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/ace/logging/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Session : boost::noncopyable
private:

// FIXME: duplicate code LogFile
// or use LogFile instead
string getFileName(const TcpConnectionPtr& conn)
{
string filename;
Expand Down
15 changes: 15 additions & 0 deletions muduo/base/ProcessInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/times.h>

namespace muduo
{
Expand Down Expand Up @@ -174,6 +175,20 @@ int ProcessInfo::maxOpenFiles()
}
}

ProcessInfo::CpuTime ProcessInfo::cpuTime()
{
ProcessInfo::CpuTime t;
bzero(&t, sizeof t);
struct tms tms;
if (::times(&tms) >= 0)
{
const double hz = static_cast<double>(clockTicksPerSecond());
t.userSeconds = static_cast<double>(tms.tms_utime) / hz;
t.systemSeconds = static_cast<double>(tms.tms_stime) / hz;
}
return t;
}

int ProcessInfo::numThreads()
{
int result = 0;
Expand Down
7 changes: 7 additions & 0 deletions muduo/base/ProcessInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ namespace ProcessInfo
int openedFiles();
int maxOpenFiles();

struct CpuTime
{
double userSeconds;
double systemSeconds;
};
CpuTime cpuTime();

int numThreads();
std::vector<pid_t> threads();
}
Expand Down
20 changes: 8 additions & 12 deletions muduo/net/inspect/ProcessInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <muduo/base/ProcessInfo.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/times.h>
#include <unistd.h>

using namespace muduo;
Expand All @@ -20,11 +19,14 @@ using namespace muduo::net;
string uptime()
{
char buf[256];
int seconds = static_cast<int>(timeDifference(Timestamp::now(), ProcessInfo::startTime()));
int64_t age = Timestamp::now().microSecondsSinceEpoch() - ProcessInfo::startTime().microSecondsSinceEpoch();
int microseconds = static_cast<int>(age % Timestamp::kMicroSecondsPerSecond);
int seconds = static_cast<int>(age / Timestamp::kMicroSecondsPerSecond);
int days = seconds/86400;
int hours = (seconds % 86400) / 3600;
int minutes = (seconds % 3600) / 60;
snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d", days, hours, minutes, seconds % 60);
snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d.%06d",
days, hours, minutes, seconds % 60, microseconds);
return buf;
}

Expand Down Expand Up @@ -157,15 +159,9 @@ string ProcessInspector::overview(HttpRequest::Method, const Inspector::ArgList&
stringPrintf(&result, "pgid %ld\n", getStatField(procStat, 1));
*/

struct tms tms;
clock_t clk = ::times(&tms);
double hz = static_cast<double>(ProcessInfo::clockTicksPerSecond());
if (clk >= 0)
{
stringPrintf(&result, "User time: %.3fs, Sys time: %.3fs\n",
static_cast<double>(tms.tms_utime) / hz,
static_cast<double>(tms.tms_stime) / hz);
}
ProcessInfo::CpuTime t = ProcessInfo::cpuTime();
stringPrintf(&result, "User time: %12.3fs\nSys time: %12.3fs\n",
t.userSeconds, t.systemSeconds);

// FIXME: add context switches

Expand Down
2 changes: 2 additions & 0 deletions muduo/net/protorpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ set_target_properties(muduo_protorpc_wire PROPERTIES COMPILE_FLAGS "-Wno-error=s
add_library(muduo_protorpc_wire_cpp11 rpc.pb.cc RpcCodec.cc)
set_target_properties(muduo_protorpc_wire_cpp11 PROPERTIES COMPILE_FLAGS "-std=c++0x -Wno-error=shadow")

if(NOT CMAKE_BUILD_NO_EXAMPLES)
add_executable(protobuf_rpc_wire_test RpcCodec_test.cc)
target_link_libraries(protobuf_rpc_wire_test muduo_protorpc_wire muduo_protobuf_codec)
set_target_properties(protobuf_rpc_wire_test PROPERTIES COMPILE_FLAGS "-Wno-error=shadow")
endif()

add_library(muduo_protorpc RpcChannel.cc RpcServer.cc)
set_target_properties(muduo_protorpc PROPERTIES COMPILE_FLAGS "-Wno-error=shadow")
Expand Down

0 comments on commit 77b8ec2

Please sign in to comment.