aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/system.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zencore/system.cpp')
-rw-r--r--src/zencore/system.cpp122
1 files changed, 110 insertions, 12 deletions
diff --git a/src/zencore/system.cpp b/src/zencore/system.cpp
index 8985a8a76..6909e1a9b 100644
--- a/src/zencore/system.cpp
+++ b/src/zencore/system.cpp
@@ -14,20 +14,27 @@
# include <zencore/windows.h>
ZEN_THIRD_PARTY_INCLUDES_START
-# include <iphlpapi.h>
# include <winsock2.h>
+# include <ws2tcpip.h>
+# include <iphlpapi.h>
# include <pdh.h>
# pragma comment(lib, "pdh.lib")
ZEN_THIRD_PARTY_INCLUDES_END
#elif ZEN_PLATFORM_LINUX
# include <sys/utsname.h>
# include <unistd.h>
+# include <ifaddrs.h>
+# include <arpa/inet.h>
+# include <net/if.h>
#elif ZEN_PLATFORM_MAC
# include <unistd.h>
# include <mach/mach.h>
# include <mach/mach_host.h>
# include <sys/types.h>
# include <sys/sysctl.h>
+# include <ifaddrs.h>
+# include <arpa/inet.h>
+# include <net/if.h>
#endif
namespace zen {
@@ -140,6 +147,51 @@ GetSystemMetrics()
return Metrics;
}
+
+std::vector<std::string>
+GetLocalIpAddresses()
+{
+ std::vector<std::string> Result;
+
+ ULONG BufSize = 15000;
+ auto* Addresses = static_cast<IP_ADAPTER_ADDRESSES*>(Memory::Alloc(BufSize));
+
+ ULONG Flags = GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_ANYCAST;
+ ULONG Ret = GetAdaptersAddresses(AF_INET, Flags, nullptr, Addresses, &BufSize);
+ if (Ret == ERROR_BUFFER_OVERFLOW)
+ {
+ Memory::Free(Addresses);
+ Addresses = static_cast<IP_ADAPTER_ADDRESSES*>(Memory::Alloc(BufSize));
+ Ret = GetAdaptersAddresses(AF_INET, Flags, nullptr, Addresses, &BufSize);
+ }
+
+ if (Ret == NO_ERROR)
+ {
+ for (IP_ADAPTER_ADDRESSES* Adapter = Addresses; Adapter != nullptr; Adapter = Adapter->Next)
+ {
+ if (Adapter->OperStatus != IfOperStatusUp)
+ {
+ continue;
+ }
+ if (Adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
+ {
+ continue;
+ }
+ for (IP_ADAPTER_UNICAST_ADDRESS* Unicast = Adapter->FirstUnicastAddress; Unicast != nullptr; Unicast = Unicast->Next)
+ {
+ sockaddr_in* SockAddr = reinterpret_cast<sockaddr_in*>(Unicast->Address.lpSockaddr);
+ char AddrBuf[INET_ADDRSTRLEN];
+ if (inet_ntop(AF_INET, &SockAddr->sin_addr, AddrBuf, sizeof(AddrBuf)))
+ {
+ Result.emplace_back(AddrBuf);
+ }
+ }
+ }
+ }
+
+ Memory::Free(Addresses);
+ return Result;
+}
#elif ZEN_PLATFORM_LINUX
std::string
GetMachineName()
@@ -350,6 +402,42 @@ GetSystemMetrics()
# error "Unknown platform"
#endif
+#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
+std::vector<std::string>
+GetLocalIpAddresses()
+{
+ std::vector<std::string> Result;
+
+ ifaddrs* Interfaces = nullptr;
+ if (getifaddrs(&Interfaces) != 0)
+ {
+ return Result;
+ }
+
+ for (ifaddrs* Iface = Interfaces; Iface != nullptr; Iface = Iface->ifa_next)
+ {
+ if (Iface->ifa_addr == nullptr || Iface->ifa_addr->sa_family != AF_INET)
+ {
+ continue;
+ }
+ if ((Iface->ifa_flags & IFF_LOOPBACK) != 0 || (Iface->ifa_flags & IFF_UP) == 0)
+ {
+ continue;
+ }
+
+ sockaddr_in* SockAddr = reinterpret_cast<sockaddr_in*>(Iface->ifa_addr);
+ char AddrBuf[INET_ADDRSTRLEN];
+ if (inet_ntop(AF_INET, &SockAddr->sin_addr, AddrBuf, sizeof(AddrBuf)))
+ {
+ Result.emplace_back(AddrBuf);
+ }
+ }
+
+ freeifaddrs(Interfaces);
+ return Result;
+}
+#endif
+
ExtendedSystemMetrics
ApplyReportingOverrides(ExtendedSystemMetrics Metrics)
{
@@ -445,23 +533,15 @@ struct CpuSampler
PDH_HQUERY QueryHandle = nullptr;
PDH_HCOUNTER CounterHandle = nullptr;
bool HasPreviousSample = false;
+ bool IsInitialized = false;
bool IsWine = false;
ProcStatCpuSampler ProcStat{"Z:\\proc\\stat"};
CpuSampler()
{
IsWine = zen::windows::IsRunningOnWine();
-
- if (!IsWine)
- {
- if (PdhOpenQueryW(nullptr, 0, &QueryHandle) == ERROR_SUCCESS)
- {
- if (PdhAddEnglishCounterW(QueryHandle, L"\\Processor(_Total)\\% Processor Time", 0, &CounterHandle) != ERROR_SUCCESS)
- {
- CounterHandle = nullptr;
- }
- }
- }
+ // PDH initialization is deferred to the first Sample() call because
+ // PdhAddEnglishCounterW can take hundreds of milliseconds on first invocation.
}
~CpuSampler()
@@ -472,6 +552,22 @@ struct CpuSampler
}
}
+ void InitializePdh()
+ {
+ if (IsInitialized)
+ {
+ return;
+ }
+ IsInitialized = true;
+ if (PdhOpenQueryW(nullptr, 0, &QueryHandle) == ERROR_SUCCESS)
+ {
+ if (PdhAddEnglishCounterW(QueryHandle, L"\\Processor(_Total)\\% Processor Time", 0, &CounterHandle) != ERROR_SUCCESS)
+ {
+ CounterHandle = nullptr;
+ }
+ }
+ }
+
float Sample()
{
if (IsWine)
@@ -479,6 +575,8 @@ struct CpuSampler
return ProcStat.Sample();
}
+ InitializePdh();
+
if (!QueryHandle || !CounterHandle)
{
return 0.0f;