mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-07 01:53:24 +09:00
The spawn work had the two roles append to one MOBILEGL_LOG_FILE_PATH under O_APPEND. It was correct - two processes at one offset would corrupt the file, O_APPEND made each write atomic - but it put two sessions' lines in one place and made every per-side assertion a search. This splits the sink: the client role writes <base>.client.log and the server role writes <base>.server.log. THE BASE NAME NAMES NO FILE ANY MORE, AND THAT IS THE POINT. Leaving the client at the old unsuffixed path would have been the compatible choice and the wrong one: a reader that was not updated would go on finding a file, go on passing, and go on seeing only half the session - and a refusal census that reports a confident zero for the half it cannot see is worse than one that fails. With both names moved, an un-updated reader gets "file not found" and says so. There were 34 such readers; every one is moved here. THE ROLE IS PER-THREAD, not per-process, because under inproc both roles are threads of one process. Log.cpp keys the sink on a thread-local role: the apply thread declares itself server (ServerLoop.cpp), a spawned image is server for its whole life (MOBILEGL_IPC_ROLE=server, read once), and everything else is the client. The naming rule (RoleLogPath) and the two whole-log operations (ReadRoleLogs / TruncateRoleLogs) are EXPORTED from the library, so no reader reimplements the derivation - a second copy is the copy that falls behind. Readers, by how they read: - unit mains set MOBILEGL_LOG_FILE_PATH and read a role file: they keep the base in the env and derive the client path from the library helper. - the death-test child runners (ReadLog) now concatenate BOTH roles: a death test asserts the CHILD said something, and the applier's refusals - the very diagnostics these check - are written under the server role. - the offset-based Emit readers derive the client path once after setenv. - PipeStatsWindow reads both, because draws/vbs increment in the backend's PrepareForDraw - the apply thread, the server role - so the summary line is in the server file even under inproc. - run_trace_case.cmake's split census reads both files; the client keeps the transport markers. - split_log_paths.py (and its self-test, and the control stub) derive both role files from the declared base. - the Android script pulls both role files off the device and concatenates them, so every downstream check is unchanged - and it judges a role file by CONTENT, because `run-as test -f` through `exec-out` returned 0 for a missing file on the device and `exec-out` folds cat's own not-found line into stdout. The monolith arm legitimately has no server file, and that is a silent skip rather than a one-line server log. Guarded so a pull build is untouched: RoleLogPath and the role sink are wholly inside MOBILEGL_BUILD_DISAGGREGATED; a non-disaggregated build writes the base path exactly as before. G1 re-measured - 0 symbol diff, .text unchanged at 0xa52203. Verified on device 2f7cbe2e: monolith writes only the client log (no phantom server log), inproc and spawn each write both and pass their transport proofs (spawn: server role in pid 16125). Unit 2353 (one pre-existing environmental failure), integration-split 186/186, integration-spawn 102/102, retrace 6/6, control smoke 12/12, split-log-paths self-test 9/9.
268 lines
11 KiB
C++
268 lines
11 KiB
C++
// MobileGL - MobileGL/MG_Util/Debug/Log.cpp
|
|
// Copyright (c) 2025-2026 MobileGL-Dev
|
|
// Licensed under the GNU Lesser General Public License v3.0:
|
|
// https://www.gnu.org/licenses/gpl-3.0.txt
|
|
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// End of Source File Header
|
|
|
|
#include "../../Includes.h"
|
|
|
|
#include <cstring> // std::strcmp, for the spawned-server role check in InitFile
|
|
|
|
namespace MobileGL {
|
|
namespace MG_Util::Debug {
|
|
static FILE* s_logFile = nullptr;
|
|
|
|
#if MOBILEGL_BUILD_DISAGGREGATED
|
|
// The server role's own sink. Opened lazily, on the first line a server-role thread
|
|
// writes, so a process that never plays the role never creates the file.
|
|
static FILE* s_serverLogFile = nullptr;
|
|
|
|
// THE PROCESS DEFAULT, read once. A spawned server image is the server on every thread -
|
|
// there is no other role in it - and MOBILEGL_IPC_ROLE is set by the launcher before
|
|
// exec, so this is answerable before any MobileGL code runs. Under inproc it is false
|
|
// and the apply thread opts in per-thread.
|
|
static bool ProcessIsSpawnedServer() {
|
|
static const bool value = [] {
|
|
const char* role = std::getenv("MOBILEGL_IPC_ROLE");
|
|
return role != nullptr && std::strcmp(role, "server") == 0;
|
|
}();
|
|
return value;
|
|
}
|
|
|
|
// Per THREAD, not per process: under inproc the server role is the mgl-srv-apply thread
|
|
// and the GL thread is the client, in one address space. -1 means "not asked yet", which
|
|
// is resolved from the process default on first use.
|
|
static thread_local int t_logRole = -1;
|
|
|
|
static bool ThreadIsServerRole() {
|
|
if (t_logRole < 0) {
|
|
t_logRole = ProcessIsSpawnedServer() ? 1 : 0;
|
|
}
|
|
return t_logRole == 1;
|
|
}
|
|
|
|
void SetThreadLogRole(LogRole role) { t_logRole = role == LogRole::Server ? 1 : 0; }
|
|
|
|
std::string ReadRoleLogs(const char* basePath) {
|
|
std::string all;
|
|
for (const LogRole role : {LogRole::Client, LogRole::Server}) {
|
|
const std::string path = RoleLogPath(basePath, role);
|
|
if (FILE* file = std::fopen(path.c_str(), "rb")) {
|
|
char chunk[4096];
|
|
std::size_t got = 0;
|
|
while ((got = std::fread(chunk, 1, sizeof(chunk), file)) > 0) {
|
|
all.append(chunk, got);
|
|
}
|
|
std::fclose(file);
|
|
}
|
|
}
|
|
return all;
|
|
}
|
|
|
|
void TruncateRoleLogs(const char* basePath) {
|
|
// BOTH, because a reader that concatenates them would otherwise fold a previous
|
|
// run's server lines into this one's - which is the stale-tail failure R-16 was
|
|
// written after, one file over.
|
|
for (const LogRole role : {LogRole::Client, LogRole::Server}) {
|
|
if (FILE* file = std::fopen(RoleLogPath(basePath, role).c_str(), "w")) {
|
|
std::fclose(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
// MOBILEGL_LOG_FILE_PATH IS A BASE NAME NOW, AND NEITHER ROLE KEEPS IT.
|
|
//
|
|
// `foo.log` becomes `foo.client.log` and `foo.server.log`; a path with no extension gets
|
|
// the suffix appended. The suffix goes BEFORE the extension so the pair sorts together
|
|
// and a `*.log` glob still finds both.
|
|
//
|
|
// THE CLIENT IS RENAMED TOO, ON PURPOSE. Leaving it at the unsuffixed path would have
|
|
// been the compatible choice and the wrong one: every reader that was not updated would
|
|
// go on finding a file, go on passing, and go on seeing only half the session - and a
|
|
// refusal census that reports a confident zero for the half it cannot see is worse than
|
|
// one that fails. With both names moved, an un-updated reader gets ENOENT and says so.
|
|
// There were fourteen such readers in this tree when the split landed.
|
|
std::string RoleLogPath(const char* basePath, LogRole role) {
|
|
const char* roleSuffix = role == LogRole::Server ? "server" : "client";
|
|
std::string path(basePath);
|
|
const std::string::size_type slash = path.find_last_of("/\\");
|
|
const std::string::size_type dot = path.find_last_of('.');
|
|
if (dot == std::string::npos || (slash != std::string::npos && dot < slash)) {
|
|
return path + "." + roleSuffix;
|
|
}
|
|
return path.substr(0, dot) + "." + roleSuffix + path.substr(dot);
|
|
}
|
|
#endif
|
|
|
|
std::mutex& LogMutex() {
|
|
static auto* mutex = new std::mutex();
|
|
return *mutex;
|
|
}
|
|
|
|
constexpr char* GetOSName() {
|
|
#if defined(_WIN32)
|
|
return (char*)"Windows";
|
|
#elif defined(__ANDROID__)
|
|
return (char*)"Android";
|
|
#elif defined(__APPLE__)
|
|
return (char*)"macOS";
|
|
#elif defined(__linux__)
|
|
return (char*)"Linux";
|
|
#else
|
|
return (char*)"UnknownOS";
|
|
#endif
|
|
}
|
|
|
|
std::string GetThreadName() {
|
|
char buffer[64] = {0};
|
|
#if defined(_WIN32)
|
|
PWSTR desc = nullptr;
|
|
if (SUCCEEDED(GetThreadDescription(GetCurrentThread(), &desc))) {
|
|
WideCharToMultiByte(CP_UTF8, 0, desc, -1, buffer, sizeof(buffer), nullptr, nullptr);
|
|
LocalFree(desc);
|
|
}
|
|
#elif defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
|
|
pthread_getname_np(pthread_self(), buffer, sizeof(buffer));
|
|
#endif
|
|
return buffer[0] ? buffer : "UnknownThread";
|
|
}
|
|
|
|
std::string GetCurrentTime() {
|
|
using namespace std::chrono;
|
|
auto now = system_clock::now();
|
|
std::time_t tt = system_clock::to_time_t(now);
|
|
struct tm tm{};
|
|
#if defined(_WIN32)
|
|
localtime_s(&tm, &tt);
|
|
#else
|
|
localtime_r(&tt, &tm);
|
|
#endif
|
|
char buf[16];
|
|
std::strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
|
|
return buf;
|
|
}
|
|
|
|
void InitFile() {
|
|
#if MOBILEGL_LOG_ENABLE_FILE
|
|
// MOBILEGL_LOG_FILE_PATH must stay a raw getenv (not MG_Config::Features):
|
|
// InitFile() runs before MG_ConfigLoader::Init() in MobileGL::Initialize().
|
|
const char* logPath = std::getenv("MOBILEGL_LOG_FILE_PATH");
|
|
if (!logPath || !*logPath) {
|
|
logPath = MOBILEGL_LOG_FILE_PATH;
|
|
}
|
|
if (!logPath || !*logPath) {
|
|
return;
|
|
}
|
|
#if MOBILEGL_BUILD_DISAGGREGATED
|
|
// ONE FILE PER ROLE, OPENED LAZILY BY WHOEVER FIRST WRITES ONE.
|
|
//
|
|
// Each role truncates ITS OWN file exactly once, which is what the previous
|
|
// single-file design could not do: there the client truncated and the server was
|
|
// forbidden to, because a truncate from the far side would have deleted the very
|
|
// startup lines a failed session needs. With separate files that asymmetry is gone -
|
|
// nobody else writes the file being truncated.
|
|
//
|
|
// "w" IS SAFE AGAIN HERE, and the O_APPEND this replaces was never about ordering:
|
|
// it was about two PROCESSES holding two handles to one path at two offsets, each
|
|
// overwriting the other's bytes. Two paths, two writers, no sharing.
|
|
if (ThreadIsServerRole()) {
|
|
if (!s_serverLogFile) {
|
|
s_serverLogFile = std::fopen(RoleLogPath(logPath, LogRole::Server).c_str(), "w");
|
|
}
|
|
return;
|
|
}
|
|
if (!s_logFile) {
|
|
s_logFile = std::fopen(RoleLogPath(logPath, LogRole::Client).c_str(), "w");
|
|
}
|
|
#else
|
|
if (!s_logFile) {
|
|
s_logFile = std::fopen(logPath, "w");
|
|
}
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
void WriteToFile(const char* msg) {
|
|
#if MOBILEGL_LOG_ENABLE_FILE
|
|
#if MOBILEGL_BUILD_DISAGGREGATED
|
|
// The sink is chosen by the CALLING THREAD's role, which is the only choice that is
|
|
// correct under inproc: there both roles are threads of one process, so anything
|
|
// process-scoped would file the apply thread's lines under the client.
|
|
FILE** sink = ThreadIsServerRole() ? &s_serverLogFile : &s_logFile;
|
|
if (!*sink) InitFile();
|
|
if (*sink) {
|
|
std::fputs(msg, *sink);
|
|
std::fflush(*sink);
|
|
}
|
|
#else
|
|
if (!s_logFile) InitFile();
|
|
if (s_logFile) {
|
|
std::fputs(msg, s_logFile);
|
|
std::fflush(s_logFile);
|
|
}
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
void Log(const char* levelTag, android_LogPriority androidLogLevel, const char* fmt, ...) {
|
|
std::lock_guard<std::mutex> lock(LogMutex());
|
|
|
|
#if MOBILEGL_LOG_ENABLE_STACKTRACE
|
|
auto trace = std::stacktrace::current();
|
|
std::string padding(4 * trace.size(), ' ');
|
|
#endif
|
|
|
|
std::string header =
|
|
"[" + GetCurrentTime() + "] [" + GetOSName() + " " + GetThreadName() + "/" + levelTag + "]: ";
|
|
|
|
static char buffer[1024000];
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
int n = std::vsnprintf(buffer, sizeof(buffer), fmt, args);
|
|
const SizeT messageLength =
|
|
(n < 0) ? 0 : std::min(static_cast<SizeT>(n), sizeof(buffer) - static_cast<SizeT>(1));
|
|
std::string out = header +
|
|
#if MOBILEGL_LOG_ENABLE_STACKTRACE
|
|
padding +
|
|
#endif
|
|
std::string(buffer, messageLength) + "\n";
|
|
|
|
#if MOBILEGL_LOG_ENABLE_CONSOLE
|
|
std::fwrite(out.c_str(), 1, out.size(), stdout);
|
|
fflush(stdout);
|
|
#endif
|
|
|
|
#if MOBILEGL_LOG_ENABLE_ANDROID && defined(__ANDROID__)
|
|
// Without the trailing newline that the file sink needs: logcat terminates
|
|
// records itself, so handing it an already-newline-terminated string made
|
|
// every MobileGL log occupy TWO logcat records, the second one empty. That
|
|
// halved the useful depth of every `adb logcat -t N` window the CI
|
|
// diagnostics read (android-plugin/trace-replay-ci.sh).
|
|
__android_log_print(androidLogLevel, "MobileGL", "%.*s", static_cast<int>(out.size() - 1),
|
|
out.c_str());
|
|
#endif
|
|
|
|
WriteToFile(out.c_str());
|
|
|
|
va_end(args);
|
|
}
|
|
|
|
void Close() {
|
|
#if MOBILEGL_LOG_ENABLE_FILE
|
|
#if MOBILEGL_BUILD_DISAGGREGATED
|
|
if (s_serverLogFile) {
|
|
std::fclose(s_serverLogFile);
|
|
s_serverLogFile = nullptr;
|
|
}
|
|
#endif
|
|
if (s_logFile) {
|
|
std::fclose(s_logFile);
|
|
s_logFile = nullptr;
|
|
}
|
|
#endif
|
|
}
|
|
} // namespace MG_Util::Debug
|
|
} // namespace MobileGL
|