1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "top_cmd.h"
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/system.h>
#include <zencore/uid.h>
#include <zenutil/zenserverprocess.h>
#include <memory>
//////////////////////////////////////////////////////////////////////////
namespace zen {
TopCommand::TopCommand()
{
}
TopCommand::~TopCommand() = default;
void
TopCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions, argc, argv);
SystemMetrics Metrics = GetSystemMetrics();
struct SystemMetric
{
const char* Name;
uint64_t Value;
} MetricValues[] = {
{"Cpus", Metrics.CpuCount},
{"Cores", Metrics.CoreCount},
{"LPs", Metrics.LogicalProcessorCount},
{"SysMemMiB", Metrics.SystemMemoryMiB},
{"AvailSysMemMiB", Metrics.AvailSystemMemoryMiB},
{"VirtMemMiB", Metrics.VirtualMemoryMiB},
{"AvailVirtMemMiB", Metrics.AvailVirtualMemoryMiB},
{"PageFileMiB", Metrics.PageFileMiB},
{"AvailPageFileMiB", Metrics.AvailPageFileMiB},
};
std::vector<size_t> ColumnWidths;
for (const SystemMetric& Metric : MetricValues)
{
size_t NameLen = strlen(Metric.Name);
size_t ValueLen = fmt::formatted_size("{}", Metric.Value);
ColumnWidths.push_back(std::max(NameLen, ValueLen));
}
ExtendableStringBuilder<128> Header;
for (size_t i = 0; i < sizeof(MetricValues) / sizeof(MetricValues[0]); ++i)
{
Header << fmt::format("{:<{}} ", MetricValues[i].Name, ColumnWidths[i]);
}
ZEN_CONSOLE("{}", Header);
// Print values with same adaptive widths
ExtendableStringBuilder<128> Values;
for (size_t i = 0; i < sizeof(MetricValues) / sizeof(MetricValues[0]); ++i)
{
Values << fmt::format("{:>{}} ", MetricValues[i].Value, ColumnWidths[i]);
}
ZEN_CONSOLE("{}\n", Values);
ZenServerState State;
if (!State.InitializeReadOnly())
{
ZEN_CONSOLE("No Zen state found");
return;
}
int n = 0;
const int HeaderPeriod = 20;
for (;;)
{
if ((n++ % HeaderPeriod) == 0)
{
ZEN_CONSOLE("{:>5} {:>6} {:>24} {}", "port", "pid", "session", "socket");
}
State.Snapshot([&](const ZenServerState::ZenServerEntry& Entry) {
StringBuilder<25> SessionStringBuilder;
Entry.GetSessionId().ToString(SessionStringBuilder);
std::string SocketPath;
if (Entry.HasInstanceInfo())
{
ZenServerInstanceInfo Info;
if (Info.OpenReadOnly(Entry.GetSessionId()))
{
InstanceInfoData Data = Info.Read();
if (!Data.UnixSocketPath.empty())
{
SocketPath = PathToUtf8(Data.UnixSocketPath);
}
}
}
std::string PortStr = Entry.IsNoNetwork() ? std::string("-") : fmt::to_string(Entry.EffectiveListenPort.load());
ZEN_CONSOLE("{:>5} {:>6} {:>24} {}", PortStr, Entry.Pid.load(), SessionStringBuilder, SocketPath);
});
zen::Sleep(1000);
if (!State.IsReadOnly())
{
State.Sweep();
}
}
}
//////////////////////////////////////////////////////////////////////////
PsCommand::PsCommand()
{
}
PsCommand::~PsCommand() = default;
void
PsCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions, argc, argv);
ZenServerState State;
if (!State.InitializeReadOnly())
{
ZEN_CONSOLE("No Zen state found");
return;
}
State.Snapshot([&](const ZenServerState::ZenServerEntry& Entry) {
std::string Extra;
if (Entry.HasInstanceInfo())
{
ZenServerInstanceInfo Info;
if (Info.OpenReadOnly(Entry.GetSessionId()))
{
InstanceInfoData Data = Info.Read();
if (!Data.UnixSocketPath.empty())
{
Extra = fmt::format(" (unix: {})", Data.UnixSocketPath);
}
}
}
std::string PortStr = Entry.IsNoNetwork() ? std::string("-") : fmt::to_string(Entry.EffectiveListenPort.load());
ZEN_CONSOLE("Port {} : pid {}{}", PortStr, Entry.Pid.load(), Extra);
});
}
} // namespace zen
|