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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
use super::CUresult;
use super::Settings;
use std::error::Error;
use std::fmt::Display;
use std::fs::File;
use std::io;
use std::io::Stderr;
use std::io::Write;
const LOG_PREFIX: &[u8] = b"[ZLUDA_DUMP] ";
// This type holds all the relevant settings for logging like output path and
// creates objects which match those settings
pub(crate) struct Factory {
// Fallible emitter is optional emitter to file system, we might lack
// file permissions or be out of disk space
fallible_emitter: Option<Box<dyn WriteTrailingZeroAware>>,
// This is emitter that "always works" (and if it does not, then we don't
// care). In addition of normal logs it emits errors from fallible emitter
infallible_emitter: Box<dyn WriteTrailingZeroAware>,
write_buffer: WriteBuffer,
// another shared buffer, so we dont't reallocate on every function call
log_queue: Vec<LogEntry>,
}
// When writing out to the emitter (file, WinAPI, whatever else) instead of
// writing piece-by-piece it's better to first concatenate everything in memory
// then write out from memory to the slow emitter only once.
// Additionally we might have an unprefixed and prefixed buffer, this struct
// handles this detail
struct WriteBuffer {
prefixed_buffer: Option<Vec<u8>>,
unprefixed_buffer: Option<Vec<u8>>,
}
impl WriteBuffer {
fn new() -> Self {
WriteBuffer {
prefixed_buffer: None,
unprefixed_buffer: None,
}
}
fn init(
&mut self,
fallible_emitter: &Option<Box<dyn WriteTrailingZeroAware>>,
infallible_emitter: &Box<dyn WriteTrailingZeroAware>,
) {
if infallible_emitter.should_prefix() {
self.prefixed_buffer = Some(Vec::new());
} else {
self.unprefixed_buffer = Some(Vec::new());
}
if let Some(emitter) = fallible_emitter {
if emitter.should_prefix() {
self.prefixed_buffer = Some(Vec::new());
} else {
self.unprefixed_buffer = Some(Vec::new());
}
}
}
fn all_buffers(&mut self) -> impl Iterator<Item = &mut Vec<u8>> {
self.prefixed_buffer
.as_mut()
.into_iter()
.chain(self.unprefixed_buffer.as_mut().into_iter())
}
fn start_line(&mut self) {
if let Some(buffer) = &mut self.prefixed_buffer {
buffer.extend_from_slice(LOG_PREFIX);
}
}
fn end_line(&mut self) {
for buffer in self.all_buffers() {
buffer.push(b'\n');
}
}
fn write(&mut self, s: &str) {
for buffer in self.all_buffers() {
buffer.extend_from_slice(s.as_bytes());
}
}
fn finish(&mut self) {
for buffer in self.all_buffers() {
buffer.push(b'\0');
}
}
fn undo_finish(&mut self) {
for buffer in self.all_buffers() {
buffer.truncate(buffer.len() - 1);
}
}
fn send_to(&self, log_emitter: &mut Box<dyn WriteTrailingZeroAware>) -> Result<(), io::Error> {
if log_emitter.should_prefix() {
log_emitter.write_zero_aware(
&*self
.prefixed_buffer
.as_ref()
.unwrap_or_else(|| unreachable!()),
)
} else {
log_emitter.write_zero_aware(
&*self
.unprefixed_buffer
.as_ref()
.unwrap_or_else(|| unreachable!()),
)
}
}
fn reset(&mut self) {
for buffer in self.all_buffers() {
unsafe { buffer.set_len(0) };
}
}
}
impl Write for WriteBuffer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if let Some(buffer) = &mut self.prefixed_buffer {
buffer.extend_from_slice(buf);
}
if let Some(buffer) = &mut self.unprefixed_buffer {
buffer.extend_from_slice(buf);
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Factory {
pub(crate) fn new() -> Self {
let debug_emitter = os::new_debug_logger();
Factory {
infallible_emitter: debug_emitter,
fallible_emitter: None,
write_buffer: WriteBuffer::new(),
log_queue: Vec::new(),
}
}
fn initalize_fallible_emitter(
settings: &Settings,
) -> std::io::Result<Option<Box<dyn WriteTrailingZeroAware>>> {
settings
.dump_dir
.as_ref()
.map(|path| {
Ok::<_, std::io::Error>(Box::new(File::create(path.to_path_buf().join("log.txt"))?)
as Box<dyn WriteTrailingZeroAware>)
})
.transpose()
}
// We load settings during first function call, since during that time we
// also create one of the loggers, what do we do about errors encountered
// at that time? We log them to the newly created logger, but to make it
// "nice" we do both of those in a single function
// An alternative would be to have something like this:
// let mut factory = Factory::new();
// let mut cuInitLog = factory.get_logger("cuInit");
// cuInitLog.load_settings(&settings);
// which is a bit nonsensical
pub(crate) fn get_first_logger_and_init_settings(
&mut self,
func: &'static str,
) -> (FunctionLogger, Settings) {
let mut first_logger = self.get_logger(func);
let settings = Settings::read_and_init(&mut first_logger);
match Self::initalize_fallible_emitter(&settings) {
Ok(fallible_emitter) => {
*first_logger.fallible_emitter = fallible_emitter;
}
Err(err) => first_logger.log(LogEntry::IoError(err)),
}
first_logger.write_buffer.init(
first_logger.fallible_emitter,
first_logger.infallible_emitter,
);
(first_logger, settings)
}
pub(crate) fn get_logger(&mut self, func: &'static str) -> FunctionLogger {
FunctionLogger {
result: None,
name: func,
fallible_emitter: &mut self.fallible_emitter,
infallible_emitter: &mut self.infallible_emitter,
write_buffer: &mut self.write_buffer,
log_queue: &mut self.log_queue,
}
}
}
// This encapsulates log output for a single function call.
// It's a separate struct and not just a plain function for two reasons:
// * While we want to always display return code before logging errors,
// logging errors might come before return code is returned
// * We want to handle panics gracefully with Drop
pub(crate) struct FunctionLogger<'a> {
pub(crate) result: Option<CUresult>,
name: &'static str,
infallible_emitter: &'a mut Box<dyn WriteTrailingZeroAware>,
fallible_emitter: &'a mut Option<Box<dyn WriteTrailingZeroAware>>,
write_buffer: &'a mut WriteBuffer,
log_queue: &'a mut Vec<LogEntry>,
}
impl<'a> FunctionLogger<'a> {
pub(crate) fn log(&mut self, l: LogEntry) {
self.log_queue.push(l);
}
fn flush_log_queue_to_write_buffer(&mut self) {
self.write_buffer.start_line();
self.write_buffer.write(self.name);
self.write_buffer.write("(...) -> ");
if let Some(result) = self.result {
write!(self.write_buffer, "{:#X}", result.0).unwrap_or_else(|_| unreachable!());
} else {
self.write_buffer.write("(INTERNAL ERROR)");
};
self.write_buffer.end_line();
for entry in self.log_queue.iter() {
write!(self.write_buffer, " {}", entry).unwrap_or_else(|_| unreachable!());
self.write_buffer.end_line();
}
self.write_buffer.finish();
}
// This is a dirty hack: we call it at the point where our write buffer is
// already finalized and squeeze the error produced by the previous emitter
fn hack_squeeze_in_additional_error(&mut self, entry: LogEntry) {
self.write_buffer.undo_finish();
write!(self.write_buffer, " {}", entry).unwrap_or_else(|_| unreachable!());
self.write_buffer.end_line();
self.write_buffer.finish();
}
}
impl<'a> Drop for FunctionLogger<'a> {
fn drop(&mut self) {
self.flush_log_queue_to_write_buffer();
let error_from_writing_to_fallible_emitter = match self.fallible_emitter {
Some(emitter) => self.write_buffer.send_to(emitter),
None => Ok(()),
};
if let Err(e) = error_from_writing_to_fallible_emitter {
self.hack_squeeze_in_additional_error(LogEntry::IoError(e))
}
self.write_buffer.send_to(self.infallible_emitter).ok();
self.write_buffer.reset();
self.log_queue.truncate(0);
}
}
// Structured log type. We don't want frontend to care about log formatting
pub(crate) enum LogEntry {
IoError(io::Error),
ErrorBox(Box<dyn Error>),
}
impl Display for LogEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogEntry::IoError(e) => e.fmt(f),
LogEntry::ErrorBox(e) => e.fmt(f),
}
}
}
// Some of our writers want to have trailing zero (WinAPI debug logger) and some
// don't (everything else), this trait encapsulates that logic
pub(crate) trait WriteTrailingZeroAware {
fn write_zero_aware(&mut self, buf: &[u8]) -> std::io::Result<()>;
fn flush(&mut self) -> std::io::Result<()>;
fn should_prefix(&self) -> bool;
}
impl WriteTrailingZeroAware for File {
fn write_zero_aware(&mut self, buf: &[u8]) -> std::io::Result<()> {
<Self as std::io::Write>::write_all(self, buf.split_last().unwrap().1)
}
fn flush(&mut self) -> std::io::Result<()> {
<Self as std::io::Write>::flush(self)
}
fn should_prefix(&self) -> bool {
false
}
}
impl WriteTrailingZeroAware for Stderr {
fn write_zero_aware(&mut self, buf: &[u8]) -> std::io::Result<()> {
<Self as std::io::Write>::write_all(self, buf.split_last().unwrap().1)
}
fn flush(&mut self) -> std::io::Result<()> {
<Self as std::io::Write>::flush(self)
}
fn should_prefix(&self) -> bool {
true
}
}
#[cfg(windows)]
mod os {
use super::WriteTrailingZeroAware;
use std::{os::windows::prelude::AsRawHandle, ptr};
use winapi::um::debugapi::OutputDebugStringA;
struct OutputDebugString {}
impl WriteTrailingZeroAware for OutputDebugString {
fn write_zero_aware(&mut self, buf: &[u8]) -> std::io::Result<()> {
unsafe { OutputDebugStringA(buf.as_ptr() as *const _) };
Ok(())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
fn should_prefix(&self) -> bool {
true
}
}
pub(crate) fn new_debug_logger() -> Box<dyn WriteTrailingZeroAware> {
let stderr = std::io::stderr();
let log_to_stderr = stderr.as_raw_handle() != ptr::null_mut();
if log_to_stderr {
Box::new(stderr)
} else {
Box::new(OutputDebugString {})
}
}
}
#[cfg(not(windows))]
mod os {
use super::WriteTrailingZeroAware;
pub(crate) fn new_debug_logger() -> Box<dyn WriteTrailingZeroAware> {
Box::new(std::io::stderr())
}
}
#[cfg(test)]
mod tests {
use std::{cell::RefCell, io, rc::Rc, str};
use super::{FunctionLogger, LogEntry, WriteTrailingZeroAware};
use crate::{log::WriteBuffer, CUresult};
struct FailOnNthWrite {
fail_on: usize,
counter: usize,
}
impl WriteTrailingZeroAware for FailOnNthWrite {
fn write_zero_aware(&mut self, _: &[u8]) -> std::io::Result<()> {
self.counter += 1;
if self.counter >= self.fail_on {
Err(io::Error::from_raw_os_error(4))
} else {
Ok(())
}
}
fn flush(&mut self) -> std::io::Result<()> {
panic!()
}
fn should_prefix(&self) -> bool {
false
}
}
// Custom type to not trigger trait coherence rules
#[derive(Clone)]
struct RcVec<T>(Rc<RefCell<Vec<T>>>);
impl WriteTrailingZeroAware for RcVec<u8> {
fn write_zero_aware(&mut self, buf: &[u8]) -> std::io::Result<()> {
let mut vec = self.0.borrow_mut();
vec.extend_from_slice(buf.split_last().unwrap().1);
Ok(())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
fn should_prefix(&self) -> bool {
false
}
}
#[test]
fn error_in_fallible_emitter_is_handled_gracefully() {
let result = RcVec(Rc::new(RefCell::new(Vec::<u8>::new())));
let mut infallible_emitter = Box::new(result.clone()) as Box<dyn WriteTrailingZeroAware>;
let mut fallible_emitter = Some(Box::new(FailOnNthWrite {
fail_on: 1,
counter: 0,
}) as Box<dyn WriteTrailingZeroAware>);
let mut write_buffer = WriteBuffer::new();
write_buffer.unprefixed_buffer = Some(Vec::new());
let mut log_queue = Vec::new();
let mut func_logger = FunctionLogger {
result: Some(CUresult::CUDA_SUCCESS),
name: "cuInit",
infallible_emitter: &mut infallible_emitter,
fallible_emitter: &mut fallible_emitter,
write_buffer: &mut write_buffer,
log_queue: &mut log_queue,
};
func_logger.log(LogEntry::IoError(io::Error::from_raw_os_error(1)));
func_logger.log(LogEntry::IoError(io::Error::from_raw_os_error(2)));
func_logger.log(LogEntry::IoError(io::Error::from_raw_os_error(3)));
drop(func_logger);
drop(infallible_emitter);
let result = result.0.borrow_mut();
let result_str = str::from_utf8(&*result).unwrap();
let result_lines = result_str.lines().collect::<Vec<_>>();
assert_eq!(result_lines.len(), 5);
assert_eq!(result_lines[0], "cuInit(...) -> 0x0");
assert!(result_lines[1].starts_with(" "));
assert!(result_lines[2].starts_with(" "));
assert!(result_lines[3].starts_with(" "));
assert!(result_lines[4].starts_with(" "));
}
}
|