Skip to content

Commit eb5b992

Browse files
committed
add SystemInspector::overview()
1 parent 159ee29 commit eb5b992

5 files changed

Lines changed: 108 additions & 18 deletions

File tree

‎muduo/base/Timestamp.cc‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,27 @@ string Timestamp::toString() const
2626
return buf;
2727
}
2828

29-
string Timestamp::toFormattedString() const
29+
string Timestamp::toFormattedString(bool showMicroseconds) const
3030
{
3131
char buf[32] = {0};
3232
time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);
33-
int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
3433
struct tm tm_time;
3534
gmtime_r(&seconds, &tm_time);
3635

37-
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
38-
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
39-
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
40-
microseconds);
36+
if (showMicroseconds)
37+
{
38+
int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);
39+
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
40+
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
41+
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
42+
microseconds);
43+
}
44+
else
45+
{
46+
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d",
47+
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
48+
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
49+
}
4150
return buf;
4251
}
4352

‎muduo/base/Timestamp.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Timestamp : public muduo::copyable,
4141
// default copy/assignment/dtor are Okay
4242

4343
string toString() const;
44-
string toFormattedString() const;
44+
string toFormattedString(bool showMicroseconds = true) const;
4545

4646
bool valid() const { return microSecondsSinceEpoch_ > 0; }
4747

‎muduo/net/inspect/ProcessInspector.cc‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,30 @@
1818
using namespace muduo;
1919
using namespace muduo::net;
2020

21-
namespace
21+
namespace muduo
22+
{
23+
namespace inspect
2224
{
2325

24-
string uptime(Timestamp now)
26+
string uptime(Timestamp now, Timestamp start, bool showMicroseconds)
2527
{
2628
char buf[256];
27-
int64_t age = now.microSecondsSinceEpoch() - ProcessInfo::startTime().microSecondsSinceEpoch();
28-
int microseconds = static_cast<int>(age % Timestamp::kMicroSecondsPerSecond);
29+
int64_t age = now.microSecondsSinceEpoch() - start.microSecondsSinceEpoch();
2930
int seconds = static_cast<int>(age / Timestamp::kMicroSecondsPerSecond);
3031
int days = seconds/86400;
3132
int hours = (seconds % 86400) / 3600;
3233
int minutes = (seconds % 3600) / 60;
33-
snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d.%06d",
34-
days, hours, minutes, seconds % 60, microseconds);
34+
if (showMicroseconds)
35+
{
36+
int microseconds = static_cast<int>(age % Timestamp::kMicroSecondsPerSecond);
37+
snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d.%06d",
38+
days, hours, minutes, seconds % 60, microseconds);
39+
}
40+
else
41+
{
42+
snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d",
43+
days, hours, minutes, seconds % 60);
44+
}
3545
return buf;
3646
}
3747

@@ -105,6 +115,9 @@ int stringPrintf(string* out, const char* fmt, ...)
105115
}
106116

107117
}
118+
}
119+
120+
using namespace muduo::inspect;
108121

109122
string ProcessInspector::username_ = ProcessInfo::username();
110123

@@ -127,7 +140,7 @@ string ProcessInspector::overview(HttpRequest::Method, const Inspector::ArgList&
127140
result += " (UTC)\nStarted at ";
128141
result += ProcessInfo::startTime().toFormattedString();
129142
result += " (UTC), up for ";
130-
result += uptime(now);
143+
result += uptime(now, ProcessInfo::startTime(), true/* show microseconds */);
131144
result += "\n";
132145

133146
string procStatus = ProcessInfo::procStatus();

‎muduo/net/inspect/SystemInspector.cc‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,25 @@
1010
#include <muduo/net/inspect/SystemInspector.h>
1111
#include <muduo/base/FileUtil.h>
1212

13+
#include <sys/utsname.h>
14+
1315
using namespace muduo;
1416
using namespace muduo::net;
1517

18+
namespace muduo
19+
{
20+
namespace inspect
21+
{
22+
23+
string uptime(Timestamp now, Timestamp start, bool showMicroseconds);
24+
long getLong(const string& content, const char* key);
25+
int stringPrintf(string* out, const char* fmt, ...) __attribute__ ((format (printf, 2, 3)));
26+
27+
}
28+
}
29+
30+
using namespace muduo::inspect;
31+
1632
void SystemInspector::registerCommands(Inspector* ins)
1733
{
1834
ins->add("sys", "overview", SystemInspector::overview, "print system overview");
@@ -60,5 +76,59 @@ string SystemInspector::stat(HttpRequest::Method, const Inspector::ArgList&)
6076

6177
string SystemInspector::overview(HttpRequest::Method, const Inspector::ArgList&)
6278
{
63-
return "Not implemented.";
79+
string result;
80+
result.reserve(1024);
81+
Timestamp now = Timestamp::now();
82+
result += "Page generated at ";
83+
result += now.toFormattedString();
84+
result += " (UTC)\n";
85+
// Hardware and OS
86+
{
87+
struct utsname un;
88+
if (::uname(&un) == 0)
89+
{
90+
stringPrintf(&result, "Hostname: %s\n", un.nodename);
91+
stringPrintf(&result, "Machine: %s\n", un.machine);
92+
stringPrintf(&result, "OS: %s %s %s\n", un.sysname, un.release, un.version);
93+
}
94+
}
95+
string stat;
96+
FileUtil::readFile("/proc/stat", 65536, &stat);
97+
Timestamp bootTime(Timestamp::kMicroSecondsPerSecond * getLong(stat, "btime "));
98+
result += "Boot time: ";
99+
result += bootTime.toFormattedString(false /* show microseconds */);
100+
result += " (UTC)\n";
101+
result += "Up time: ";
102+
result += uptime(now, bootTime, false /* show microseconds */);
103+
result += "\n";
104+
105+
// CPU load
106+
{
107+
string loadavg;
108+
FileUtil::readFile("/proc/loadavg", 65536, &loadavg);
109+
stringPrintf(&result, "Processes created: %ld\n", getLong(stat, "processes "));
110+
stringPrintf(&result, "Loadavg: %s\n", loadavg.c_str());
111+
}
112+
113+
// Memory
114+
{
115+
string meminfo;
116+
FileUtil::readFile("/proc/meminfo", 65536, &meminfo);
117+
long total_kb = getLong(meminfo, "MemTotal:");
118+
long free_kb = getLong(meminfo, "MemFree:");
119+
long buffers_kb = getLong(meminfo, "Buffers:");
120+
long cached_kb = getLong(meminfo, "Cached:");
121+
122+
stringPrintf(&result, "Total Memory: %6ld MiB\n", total_kb / 1024);
123+
stringPrintf(&result, "Free Memory: %6ld MiB\n", free_kb / 1024);
124+
stringPrintf(&result, "Buffers: %6ld MiB\n", buffers_kb / 1024);
125+
stringPrintf(&result, "Cached: %6ld MiB\n", cached_kb / 1024);
126+
stringPrintf(&result, "Real Used: %6ld MiB\n", (total_kb - free_kb - buffers_kb - cached_kb) / 1024);
127+
stringPrintf(&result, "Real Free: %6ld MiB\n", (free_kb + buffers_kb + cached_kb) / 1024);
128+
129+
// Swap
130+
}
131+
// Disk
132+
// Network
133+
return result;
64134
}

‎muduo/net/protobuf/CMakeLists.txt‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ target_link_libraries(muduo_protobuf_codec_cpp11 muduo_net_cpp11 protobuf z)
1010
install(TARGETS muduo_protobuf_codec DESTINATION lib)
1111
install(TARGETS muduo_protobuf_codec_cpp11 DESTINATION lib)
1212

13-
set(HEADERS
14-
ProtobufCodecLite.h
15-
)
13+
file(GLOB HEADERS "*.h")
1614
install(FILES ${HEADERS} DESTINATION include/muduo/net/protobuf)
1715

0 commit comments

Comments
 (0)