Fix: Report the real runtime instead of a timezone skew in host info

The host info popup showed a constant "7h" runtime for every Linux host.
szStartTime carries no timezone marker and the two sides disagree: the
Windows client sends its own wall clock (client/LoginServer.cpp
getProcessTime), while the Linux and Android clients send Beijing time
unconditionally (linux/main.cpp and common/commands.h ToPekingTimeAsString
hardcode UTC+8). GetElapsedTime then built the base time with CTime as
local time and subtracted it from the local clock, so on any server that
is not at UTC+8 the start time landed in the future; the absolute value
turned that skew into a plausible-looking duration (UTC+1 gives exactly
7h).

GetElapsedTime now picks the reference "now" by client type: LNX and APK
are compared against the current Beijing time, everything else keeps
using the local clock. A start time in the future now clamps to 0 instead
of being mirrored, so a clock disagreement shows as unknown rather than
as a fake runtime.

The Linux client also reported the wrong thing entirely: it sent the
current time at login, not the process start time, so the value was
always about zero whenever the timezones happened to agree. It now
records its own wall clock once at load and reports Beijing time built
from it, matching the semantics of the Windows client. An upgrade forks
a new process, so the counter restarts after an upgrade as expected.

The first implementation read /proc/self/stat starttime plus /proc/stat
btime, which looked like the exact equivalent of the Windows call, but
btime is computed from the wall clock at boot and never corrected when
NTP later steps the clock, so on WSL2 it drifted by 1.8h. Recording the
value in the process itself has no such failure mode.

Verified against a Debug server with a WSL Ubuntu 24.04 client and a
Windows client: the popup now reports "2.3m" for a Linux process started
2.3 minutes earlier (previously 7h), the startup timestamp is the process
load time in Beijing time, and the Windows host still reports 14s right
after it connects.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-27 21:39:31 +01:00
1 parent 4d2abf34d0
commit c11df75225
2 files changed
+40 -6

No files matched your search

+19 -2
View File
@@ -18,6 +18,7 @@
#include <iostream>
#include <stdexcept>
#include <cstdio>
#include <ctime>
#include <memory>
#include <array>
#include <regex>
@@ -599,6 +600,21 @@ std::string getLinuxDistro()
return "Linux";
}
// 进程启动时刻(墙钟)。在程序加载时记录一次:升级走 fork+execv 会重新加载本进程
// 镜像,于是升级后重新计时,与 Windows 的 GetProcessTimes(创建时间) 语义一致。
// 刻意不用 /proc/self/stat 的 starttime + /proc/stat 的 btime:btime 是开机时按当时
// 墙钟算的,之后 NTP 校时/休眠都不会修正它,算出来的启动时刻可能偏出几小时
// (WSL2 实测偏 1.8h)。这里是本进程自己读到的墙钟,不受此影响。
static const std::time_t g_processStartTime = std::time(nullptr);
// 进程启动时间("YYYY-MM-DD HH:MM:SS",北京时间):与 Windows 客户端的
// getProcessTime() 一致,是进程创建时刻而非本次登录时刻。
std::string getProcessStartTime()
{
// 服务端按北京时间解释这个字符串(见服务端 GetElapsedTime),沿用既有约定
return ToPekingTimeAsString(&g_processStartTime);
}
// 获取 CPU 核心数
int getCPUCores()
{
@@ -1086,8 +1102,9 @@ int main(int argc, char* argv[])
strncpy(logInfo.OsVerInfoEx, distro.c_str(), sizeof(logInfo.OsVerInfoEx) - 1);
logInfo.OsVerInfoEx[sizeof(logInfo.OsVerInfoEx) - 1] = '\0';
// 启动时间
strncpy(logInfo.szStartTime, ToPekingTimeAsString(nullptr).c_str(), sizeof(logInfo.szStartTime) - 1);
// 启动时间:进程创建时刻(北京时间),非本次登录时刻
std::string procStart = getProcessStartTime();
strncpy(logInfo.szStartTime, procStart.c_str(), sizeof(logInfo.szStartTime) - 1);
logInfo.szStartTime[sizeof(logInfo.szStartTime) - 1] = '\0';
// CPU 主频