|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cuda_runtime.h> |
| 12 | +#include <algorithm> |
| 13 | +#include <limits> |
| 14 | + |
| 15 | +#include <executorch/runtime/platform/log.h> |
| 16 | + |
| 17 | +namespace executorch::backends::cuda { |
| 18 | + |
| 19 | +/** |
| 20 | + * @class CudaMemoryTracker |
| 21 | + * @brief Tracks CUDA memory usage and logs memory state at key points |
| 22 | + * |
| 23 | + * This class provides utilities to query and track CUDA memory usage, |
| 24 | + * including peak memory usage and detailed memory state logging. |
| 25 | + */ |
| 26 | +class CudaMemoryTracker { |
| 27 | + public: |
| 28 | + /** |
| 29 | + * @brief Constructor - initializes tracker and logs startup memory state |
| 30 | + */ |
| 31 | + CudaMemoryTracker() { |
| 32 | + if (!query(&last_free_bytes_, &total_bytes_)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + available_ = true; |
| 36 | + // Record the initial free bytes observed at startup. We'll use this as a |
| 37 | + // baseline so reported "peak usage" reflects additional memory used |
| 38 | + // since the tracker was created (instead of the absolute device usage, |
| 39 | + // which may include other processes). |
| 40 | + initial_free_bytes_ = last_free_bytes_; |
| 41 | + min_free_bytes_ = last_free_bytes_; |
| 42 | + log_state("startup", last_free_bytes_, total_bytes_); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Logs current memory state at a tagged checkpoint |
| 47 | + * @param tag Descriptive tag for this memory sample (e.g., "after_load") |
| 48 | + */ |
| 49 | + void log_sample(const char* tag) { |
| 50 | + if (!available_) { |
| 51 | + return; |
| 52 | + } |
| 53 | + size_t free_bytes = 0; |
| 54 | + size_t total_bytes = 0; |
| 55 | + if (!query(&free_bytes, &total_bytes)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + min_free_bytes_ = std::min(min_free_bytes_, free_bytes); |
| 59 | + total_bytes_ = total_bytes; |
| 60 | + last_free_bytes_ = free_bytes; |
| 61 | + log_state(tag, free_bytes, total_bytes); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @brief Destructor - logs final memory state and peak usage summary |
| 66 | + */ |
| 67 | + ~CudaMemoryTracker() { |
| 68 | + if (!available_) { |
| 69 | + return; |
| 70 | + } |
| 71 | + size_t free_bytes = 0; |
| 72 | + size_t total_bytes = 0; |
| 73 | + if (!query(&free_bytes, &total_bytes)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + min_free_bytes_ = std::min(min_free_bytes_, free_bytes); |
| 77 | + total_bytes_ = total_bytes; |
| 78 | + last_free_bytes_ = free_bytes; |
| 79 | + // Compute peak usage relative to the initial free baseline so that |
| 80 | + // allocations by other processes present at startup are not attributed |
| 81 | + // to this process. If for some reason initial_free_bytes_ was not set, |
| 82 | + // fall back to absolute device usage. |
| 83 | + double peak_mb = 0.0; |
| 84 | + if (initial_free_bytes_ != std::numeric_limits<size_t>::max()) { |
| 85 | + size_t used_delta = 0; |
| 86 | + if (initial_free_bytes_ > min_free_bytes_) { |
| 87 | + used_delta = initial_free_bytes_ - min_free_bytes_; |
| 88 | + } |
| 89 | + peak_mb = static_cast<double>(used_delta) / (1024.0 * 1024.0); |
| 90 | + } else { |
| 91 | + peak_mb = static_cast<double>(total_bytes_ - min_free_bytes_) / |
| 92 | + (1024.0 * 1024.0); |
| 93 | + } |
| 94 | + const double total_mb = |
| 95 | + static_cast<double>(total_bytes_) / (1024.0 * 1024.0); |
| 96 | + ET_LOG( |
| 97 | + Info, |
| 98 | + "CUDA memory peak usage (since startup): %.2f MB, device total: %.2f MB", |
| 99 | + peak_mb, |
| 100 | + total_mb); |
| 101 | + } |
| 102 | + |
| 103 | + private: |
| 104 | + /** |
| 105 | + * @brief Queries current CUDA memory info |
| 106 | + * @param free_bytes Output parameter for free memory in bytes |
| 107 | + * @param total_bytes Output parameter for total memory in bytes |
| 108 | + * @return true if query succeeded, false otherwise |
| 109 | + */ |
| 110 | + bool query(size_t* free_bytes, size_t* total_bytes) { |
| 111 | + cudaError_t err = cudaMemGetInfo(free_bytes, total_bytes); |
| 112 | + if (err != cudaSuccess) { |
| 113 | + if (!error_logged_) { |
| 114 | + error_logged_ = true; |
| 115 | + ET_LOG( |
| 116 | + Error, |
| 117 | + "cudaMemGetInfo failed with error: %s", |
| 118 | + cudaGetErrorString(err)); |
| 119 | + } |
| 120 | + available_ = false; |
| 121 | + return false; |
| 122 | + } |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @brief Logs the current memory state |
| 128 | + * @param tag Tag describing this log point |
| 129 | + * @param free_bytes Current free memory in bytes |
| 130 | + * @param total_bytes Current total memory in bytes |
| 131 | + */ |
| 132 | + void log_state(const char* tag, size_t free_bytes, size_t total_bytes) const { |
| 133 | + const double used_mb = |
| 134 | + static_cast<double>(total_bytes - free_bytes) / (1024.0 * 1024.0); |
| 135 | + const double free_mb = static_cast<double>(free_bytes) / (1024.0 * 1024.0); |
| 136 | + const double total_mb = |
| 137 | + static_cast<double>(total_bytes) / (1024.0 * 1024.0); |
| 138 | + ET_LOG( |
| 139 | + Info, |
| 140 | + "CUDA memory (%s): used %.2f MB, free %.2f MB, total %.2f MB", |
| 141 | + tag, |
| 142 | + used_mb, |
| 143 | + free_mb, |
| 144 | + total_mb); |
| 145 | + } |
| 146 | + |
| 147 | + bool available_{false}; |
| 148 | + bool error_logged_{false}; |
| 149 | + size_t last_free_bytes_{0}; |
| 150 | + size_t total_bytes_{0}; |
| 151 | + size_t min_free_bytes_{std::numeric_limits<size_t>::max()}; |
| 152 | + // Baseline free bytes observed at tracker construction. Used to compute |
| 153 | + // peak usage attributable to this process since the tracker started. |
| 154 | + size_t initial_free_bytes_{std::numeric_limits<size_t>::max()}; |
| 155 | + |
| 156 | + public: |
| 157 | + // Simple accessors to allow other components to read last-sampled values. |
| 158 | + // These are safe to call after a successful log_sample() invocation. |
| 159 | + uint64_t last_free_bytes() const { |
| 160 | + return static_cast<uint64_t>(last_free_bytes_); |
| 161 | + } |
| 162 | + uint64_t total_bytes() const { |
| 163 | + return static_cast<uint64_t>(total_bytes_); |
| 164 | + } |
| 165 | + uint64_t min_free_bytes() const { |
| 166 | + return static_cast<uint64_t>(min_free_bytes_); |
| 167 | + } |
| 168 | + uint64_t initial_free_bytes() const { |
| 169 | + return static_cast<uint64_t>(initial_free_bytes_); |
| 170 | + } |
| 171 | + double peak_usage_mb() const { |
| 172 | + // Prefer peak relative to the initial free baseline; fall back to |
| 173 | + // absolute device peak if baseline isn't available. |
| 174 | + if (min_free_bytes_ == std::numeric_limits<size_t>::max()) { |
| 175 | + return 0.0; |
| 176 | + } |
| 177 | + if (initial_free_bytes_ != std::numeric_limits<size_t>::max()) { |
| 178 | + size_t used_delta = 0; |
| 179 | + if (initial_free_bytes_ > min_free_bytes_) { |
| 180 | + used_delta = initial_free_bytes_ - min_free_bytes_; |
| 181 | + } |
| 182 | + return static_cast<double>(used_delta) / (1024.0 * 1024.0); |
| 183 | + } |
| 184 | + if (total_bytes_ == 0) { |
| 185 | + return 0.0; |
| 186 | + } |
| 187 | + return static_cast<double>(total_bytes_ - min_free_bytes_) / |
| 188 | + (1024.0 * 1024.0); |
| 189 | + } |
| 190 | +}; |
| 191 | + |
| 192 | +} // namespace executorch::backends::cuda |
0 commit comments