/
logging_resource_adaptor.hpp
338 lines (310 loc) · 11.8 KB
/
logging_resource_adaptor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <rmm/cuda_stream_view.hpp>
#include <rmm/detail/error.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>
#include <rmm/resource_ref.hpp>
#include <fmt/core.h>
#include <spdlog/common.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/ostream_sink.h>
#include <spdlog/spdlog.h>
#include <cstddef>
#include <memory>
#include <sstream>
#include <string_view>
namespace rmm::mr {
/**
* @addtogroup device_resource_adaptors
* @{
* @file
*/
/**
* @brief Resource that uses `Upstream` to allocate memory and logs information
* about the requested allocation/deallocations.
*
* An instance of this resource can be constructed with an existing, upstream
* resource in order to satisfy allocation requests and log
* allocation/deallocation activity.
*
* @tparam Upstream Type of the upstream resource used for
* allocation/deallocation.
*/
template <typename Upstream>
class logging_resource_adaptor final : public device_memory_resource {
public:
/**
* @brief Construct a new logging resource adaptor using `upstream` to satisfy
* allocation requests and logging information about each allocation/free to
* the file specified by `filename`.
*
* The logfile will be written using CSV formatting.
*
* Clears the contents of `filename` if it already exists.
*
* Creating multiple `logging_resource_adaptor`s with the same `filename` will
* result in undefined behavior.
*
* @throws rmm::logic_error if `upstream == nullptr`
* @throws spdlog::spdlog_ex if opening `filename` failed
*
* @param upstream The resource used for allocating/deallocating device memory
* @param filename Name of file to write log info. If not specified, retrieves
* the file name from the environment variable "RMM_LOG_FILE".
* @param auto_flush If true, flushes the log for every (de)allocation. Warning, this will degrade
* performance.
*/
logging_resource_adaptor(Upstream* upstream,
std::string const& filename = get_default_filename(),
bool auto_flush = false)
: logger_{make_logger(filename)}, upstream_{upstream}
{
RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
init_logger(auto_flush);
}
/**
* @brief Construct a new logging resource adaptor using `upstream` to satisfy
* allocation requests and logging information about each allocation/free to
* the ostream specified by `stream`.
*
* The logfile will be written using CSV formatting.
*
* @throws rmm::logic_error if `upstream == nullptr`
*
* @param upstream The resource used for allocating/deallocating device memory
* @param stream The ostream to write log info.
* @param auto_flush If true, flushes the log for every (de)allocation. Warning, this will degrade
* performance.
*/
logging_resource_adaptor(Upstream* upstream, std::ostream& stream, bool auto_flush = false)
: logger_{make_logger(stream)}, upstream_{upstream}
{
RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
init_logger(auto_flush);
}
/**
* @brief Construct a new logging resource adaptor using `upstream` to satisfy
* allocation requests and logging information about each allocation/free to
* the ostream specified by `stream`.
*
* The logfile will be written using CSV formatting.
*
* @throws rmm::logic_error if `upstream == nullptr`
*
* @param upstream The resource used for allocating/deallocating device memory
* @param sinks A list of logging sinks to which log output will be written.
* @param auto_flush If true, flushes the log for every (de)allocation. Warning, this will degrade
* performance.
*/
logging_resource_adaptor(Upstream* upstream,
spdlog::sinks_init_list sinks,
bool auto_flush = false)
: logger_{make_logger(sinks)}, upstream_{upstream}
{
RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
init_logger(auto_flush);
}
logging_resource_adaptor() = delete;
~logging_resource_adaptor() override = default;
logging_resource_adaptor(logging_resource_adaptor const&) = delete;
logging_resource_adaptor& operator=(logging_resource_adaptor const&) = delete;
logging_resource_adaptor(logging_resource_adaptor&&) noexcept =
default; ///< @default_move_constructor
logging_resource_adaptor& operator=(logging_resource_adaptor&&) noexcept =
default; ///< @default_move_assignment{logging_resource_adaptor}
/**
* @briefreturn{rmm::device_async_resource_ref to the upstream resource}
*/
[[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
{
return upstream_;
}
/**
* @briefreturn{Upstream* to the upstream memory resource}
*/
[[nodiscard]] Upstream* get_upstream() const noexcept { return upstream_; }
/**
* @brief Flush logger contents.
*/
void flush() { logger_->flush(); }
/**
* @brief Return the CSV header string
*
* @return CSV formatted header string of column names
*/
[[nodiscard]] std::string header() const
{
return std::string{"Thread,Time,Action,Pointer,Size,Stream"};
}
/**
* @brief Return the value of the environment variable RMM_LOG_FILE.
*
* @throws rmm::logic_error if `RMM_LOG_FILE` is not set.
*
* @return The value of RMM_LOG_FILE as `std::string`.
*/
static std::string get_default_filename()
{
auto* filename = std::getenv("RMM_LOG_FILE");
RMM_EXPECTS(filename != nullptr,
"RMM logging requested without an explicit file name, but RMM_LOG_FILE is unset");
return std::string{filename};
}
private:
static auto make_logger(std::ostream& stream)
{
return std::make_shared<spdlog::logger>(
"RMM", std::make_shared<spdlog::sinks::ostream_sink_mt>(stream));
}
static auto make_logger(std::string const& filename)
{
return std::make_shared<spdlog::logger>(
"RMM", std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename, true /*truncate file*/));
}
static auto make_logger(spdlog::sinks_init_list sinks)
{
return std::make_shared<spdlog::logger>("RMM", sinks);
}
/**
* @brief Initialize the logger.
*/
void init_logger(bool auto_flush)
{
if (auto_flush) { logger_->flush_on(spdlog::level::info); }
logger_->set_pattern("%v");
logger_->info(header());
logger_->set_pattern("%t,%H:%M:%S.%f,%v");
}
/**
* @brief Allocates memory of size at least `bytes` using the upstream
* resource and logs the allocation.
*
* If the upstream allocation is successful, logs the following CSV formatted
* line to the file specified at construction:
* ```
* thread_id,*TIMESTAMP*,"allocate",*pointer*,*bytes*,*stream*
* ```
*
* If the upstream allocation failed, logs the following CSV formatted line
* to the file specified at construction:
* ```
* thread_id,*TIMESTAMP*,"allocate failure",0x0,*bytes*,*stream*
* ```
*
* The returned pointer has at least 256B alignment.
*
* @throws rmm::bad_alloc if the requested allocation could not be fulfilled
* by the upstream resource.
*
* @param bytes The size, in bytes, of the allocation
* @param stream Stream on which to perform the allocation
* @return void* Pointer to the newly allocated memory
*/
void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
{
try {
auto const ptr = upstream_->allocate(bytes, stream);
logger_->info("allocate,{},{},{}", ptr, bytes, fmt::ptr(stream.value()));
return ptr;
} catch (...) {
logger_->info("allocate failure,{},{},{}", nullptr, bytes, fmt::ptr(stream.value()));
throw;
}
}
/**
* @brief Free allocation of size `bytes` pointed to by `ptr` and log the
* deallocation.
*
* Every invocation of `logging_resource_adaptor::do_deallocate` will write
* the following CSV formatted line to the file specified at construction:
* ```
* thread_id,*TIMESTAMP*,"free",*bytes*,*stream*
* ```
*
* @param ptr Pointer to be deallocated
* @param bytes Size of the allocation
* @param stream Stream on which to perform the deallocation
*/
void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
{
logger_->info("free,{},{},{}", ptr, bytes, fmt::ptr(stream.value()));
upstream_->deallocate(ptr, bytes, stream);
}
/**
* @brief Compare the upstream resource to another.
*
* @param other The other resource to compare to
* @return true If the two resources are equivalent
* @return false If the two resources are not equal
*/
[[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
{
if (this == &other) { return true; }
auto const* cast = dynamic_cast<logging_resource_adaptor<Upstream> const*>(&other);
if (cast == nullptr) { return upstream_->is_equal(other); }
return get_upstream_resource() == cast->get_upstream_resource();
}
// make_logging_adaptor needs access to private get_default_filename
template <typename T>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend logging_resource_adaptor<T> make_logging_adaptor(T* upstream,
std::string const& filename,
bool auto_flush);
std::shared_ptr<spdlog::logger> logger_; ///< spdlog logger object
Upstream* upstream_; ///< The upstream resource used for satisfying
///< allocation requests
};
/**
* @brief Convenience factory to return a `logging_resource_adaptor` around the
* upstream resource `upstream`.
*
* @tparam Upstream Type of the upstream `device_memory_resource`.
* @param upstream Pointer to the upstream resource
* @param filename Name of the file to write log info. If not specified,
* retrieves the log file name from the environment variable "RMM_LOG_FILE".
* @param auto_flush If true, flushes the log for every (de)allocation. Warning, this will degrade
* performance.
* @return The new logging resource adaptor
*/
template <typename Upstream>
logging_resource_adaptor<Upstream> make_logging_adaptor(
Upstream* upstream,
std::string const& filename = logging_resource_adaptor<Upstream>::get_default_filename(),
bool auto_flush = false)
{
return logging_resource_adaptor<Upstream>{upstream, filename, auto_flush};
}
/**
* @brief Convenience factory to return a `logging_resource_adaptor` around the
* upstream resource `upstream`.
*
* @tparam Upstream Type of the upstream `device_memory_resource`.
* @param upstream Pointer to the upstream resource
* @param stream The ostream to write log info.
* @param auto_flush If true, flushes the log for every (de)allocation. Warning, this will degrade
* performance.
* @return The new logging resource adaptor
*/
template <typename Upstream>
logging_resource_adaptor<Upstream> make_logging_adaptor(Upstream* upstream,
std::ostream& stream,
bool auto_flush = false)
{
return logging_resource_adaptor<Upstream>{upstream, stream, auto_flush};
}
/** @} */ // end of group
} // namespace rmm::mr