Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

feat: per-phase scan timing metrics (refs #3)

Vishnu Srivatsavabcc03eea7c1d
diff --git a/engine/daemon.cpp b/engine/daemon.cpp
index fac1589..f8a4e52 100644
--- a/engine/daemon.cpp
+++ b/engine/daemon.cpp
@@ -36,6 +36,13 @@ static volatile sig_atomic_t g_running = 1;
static void on_signal(int) { g_running = 0; }
static std::string g_scan_root; // tracks the active scan root for delete validation
+// Per-phase scan timing (see SCAN in handle_client). Counters reset per scan.
+static int64_t g_sort_ns = 0; // cumulative time sorting directory children
+static int64_t g_send_ns = 0; // cumulative time blocked in ::send (socket writes)
+static int64_t g_send_bytes = 0; // cumulative bytes handed to ::send
+
+static inline int64_t ns_to_ms(int64_t ns) { return ns / 1000000; }
+
// ── Delete path validation ──────────────────────────────────────────────────
/// Returns true if `target` is strictly inside `root` (component-wise).
@@ -100,14 +107,19 @@ public:
}
static bool send_all(int fd, const char* p, size_t left) {
+ const size_t total = left;
+ const auto t0 = std::chrono::steady_clock::now();
while (left > 0) {
ssize_t n = ::send(fd, p, left, MSG_NOSIGNAL);
if (n < 0 && errno == EINTR) continue;
- if (n <= 0) return false;
+ if (n <= 0) break;
p += n;
left -= static_cast<size_t>(n);
}
- return true;
+ g_send_ns += std::chrono::duration_cast<std::chrono::nanoseconds>(
+ std::chrono::steady_clock::now() - t0).count();
+ g_send_bytes += total - left;
+ return left == 0;
}
};
@@ -244,10 +256,13 @@ static FileNode scan(const std::string& path, const std::string& name,
}
::closedir(dir);
+ const auto sort_t0 = std::chrono::steady_clock::now();
std::sort(node.children.begin(), node.children.end(),
[](const FileNode& a, const FileNode& b) {
return a.size > b.size;
});
+ g_sort_ns += std::chrono::duration_cast<std::chrono::nanoseconds>(
+ std::chrono::steady_clock::now() - sort_t0).count();
return node;
}
@@ -309,17 +324,25 @@ static void handle_client(int fd) {
std::fprintf(stderr, "[engine] SCAN \"%s\" ...\n", root.c_str());
g_scan_root = root; // store for delete validation
+ g_sort_ns = 0;
+ g_send_ns = 0;
+ g_send_bytes = 0;
+
auto t0 = std::chrono::steady_clock::now();
ScanStats stats{};
FileNode tree = scan(root, root_name, stats, 0);
auto t1 = std::chrono::steady_clock::now();
int64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();
+ int64_t traverse_ms = ms - ns_to_ms(g_sort_ns);
+ if (traverse_ms < 0) traverse_ms = 0;
std::fprintf(stderr, "[engine] Done: %lld files, %lld dirs, "
- "%lld bytes, %lld errors, %lld ms\n",
+ "%lld bytes, %lld errors, %lld ms "
+ "(traverse %lld ms, sort %lld ms)\n",
(long long)stats.files, (long long)stats.dirs,
(long long)stats.total_size, (long long)stats.errors,
- (long long)ms);
+ (long long)ms, (long long)traverse_ms,
+ (long long)ns_to_ms(g_sort_ns));
out.write("{\"status\":\"ok\",\"scan_time_ms\":");
out.write(std::to_string(ms));
@@ -333,6 +356,27 @@ static void handle_client(int fd) {
out.write(std::to_string(stats.errors));
out.write(",\"tree\":");
serialize_node(out, tree);
+
+ // Phase metrics are appended after the tree because serialization and
+ // socket sends stream interleaved with it — the numbers are only final
+ // once the tree has been written. Flush before snapshotting so the
+ // byte/send counters cover the whole tree; only the trailing metrics
+ // fields themselves (~100 bytes) go out uncounted via the destructor.
+ out.flush();
+ const auto t2 = std::chrono::steady_clock::now();
+ const int64_t serialize_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
+ const int64_t send_ms = ns_to_ms(g_send_ns);
+
+ out.write(",\"traverse_ms\":");
+ out.write(std::to_string(traverse_ms));
+ out.write(",\"sort_ms\":");
+ out.write(std::to_string(ns_to_ms(g_sort_ns)));
+ out.write(",\"serialize_ms\":");
+ out.write(std::to_string(serialize_ms));
+ out.write(",\"send_ms\":");
+ out.write(std::to_string(send_ms));
+ out.write(",\"bytes_sent\":");
+ out.write(std::to_string(g_send_bytes));
out.write("}\n");
}
else {
diff --git a/src/App.tsx b/src/App.tsx
index b024855..b9e2070 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -20,9 +20,22 @@ interface ScanResponse {
total_dirs: number;
total_size: number;
errors: number;
+ /** Per-phase timings from daemon v1.2+ (issue #3); absent in older daemons. */
+ traverse_ms?: number;
+ sort_ms?: number;
+ serialize_ms?: number;
+ send_ms?: number;
+ bytes_sent?: number;
tree: FileNode;
}
+/** One-line phase breakdown for the terminal log; null if the daemon predates #3. */
+const scanPhaseLog = (r: ScanResponse): string | null => {
+ if (r.traverse_ms === undefined) return null;
+ const kb = r.bytes_sent !== undefined ? `, ${(r.bytes_sent / 1024).toFixed(0)} KB sent` : "";
+ return `[SCAN] Phases: traverse ${r.traverse_ms}ms · sort ${r.sort_ms}ms · serialize ${r.serialize_ms}ms · send ${r.send_ms}ms${kb}`;
+};
+
interface Toast {
id: number;
message: string;
@@ -709,6 +722,8 @@ function App() {
setScanData(parsed);
setPhase("result");
addLog(`[SCAN] Complete! ${parsed.total_files} files, ${parsed.scan_time_ms}ms.`);
+ const phases = scanPhaseLog(parsed);
+ if (phases) addLog(phases);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
pushToast(message, "error");
@@ -728,6 +743,8 @@ function App() {
setScanData(parsed);
setPhase("result");
addLog(`[SCAN] Complete! ${parsed.scan_time_ms}ms.`);
+ const phases = scanPhaseLog(parsed);
+ if (phases) addLog(phases);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
pushToast(message, "error");