Skip to content

Commit 86eac8a

Browse files
committed
feat(mlx-swift): implement 1-second interval aggregated SSD read metrics for cleaner console output
1 parent c6b3b98 commit 86eac8a

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

‎LocalPackages/mlx-swift/Source/Cmlx/mlx/mlx/core/moe_stream_op.cpp‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
#include "mlx/core/moe_stream_op.h"
66
#include <iostream>
77
#include <chrono>
8+
#include <atomic>
89
#include "mlx/primitives.h"
910
#include "mlx/backend/metal/device.h"
1011
#include "mlx/backend/metal/utils.h"
11-
#include <iostream>
12+
13+
// Static SSD metric trackers for aggregate logging
14+
static std::atomic<size_t> g_total_bytes_read{0};
15+
static std::atomic<uint64_t> g_total_read_ns{0};
16+
static std::atomic<size_t> g_read_count{0};
17+
static std::atomic<uint64_t> g_last_log_ns{0};
1218

1319
namespace mlx::core {
1420

@@ -68,8 +74,33 @@ class StreamedGatherMM : public Primitive {
6874
auto start_read = std::chrono::high_resolution_clock::now();
6975
streamer_->load_sync(block_offset, matrix_bytes, w.data<void>());
7076
auto end_read = std::chrono::high_resolution_clock::now();
71-
std::chrono::duration<double, std::milli> read_ms = end_read - start_read;
72-
std::cout << "[SSD] Read " << (matrix_bytes / 1024.0 / 1024.0) << " MB in " << read_ms.count() << " ms\n";
77+
78+
// ─────────────────────────────────────────────────────────────────────
79+
// AGGREGATE LOGGING — 1-second metric intervals
80+
// ─────────────────────────────────────────────────────────────────────
81+
g_total_bytes_read += matrix_bytes;
82+
g_total_read_ns += std::chrono::duration_cast<std::chrono::nanoseconds>(end_read - start_read).count();
83+
g_read_count++;
84+
85+
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
86+
uint64_t last = g_last_log_ns.load();
87+
88+
// Output aggregated metrics once per second (1,000,000,000 ns)
89+
if (now_ns - last >= 1000000000ULL) {
90+
if (g_last_log_ns.compare_exchange_strong(last, now_ns)) {
91+
size_t count = g_read_count.exchange(0);
92+
size_t bytes = g_total_bytes_read.exchange(0);
93+
uint64_t ns_time = g_total_read_ns.exchange(0);
94+
95+
if (count > 0) {
96+
double avg_ms = (ns_time / 1000000.0) / count;
97+
double mb = bytes / (1024.0 * 1024.0);
98+
std::cout << "[⚡️ SSD Stream] " << mb << " MB/s over "
99+
<< count << " chunks | Avg latency per chunk: "
100+
<< avg_ms << " ms\n";
101+
}
102+
}
103+
}
73104

74105
auto& d = metal::device(mlx::core::Device::gpu);
75106

0 commit comments

Comments
 (0)