327 lines
10 KiB
Objective-C
327 lines
10 KiB
Objective-C
/*
|
|
* Shader logging. A log state's buffer holds what `air_log.c` appends: records
|
|
* that carry their own strings and arguments. No record is complete while a
|
|
* command buffer using the state runs, so they are read once none does, and a
|
|
* state that never rests fills and drops messages, which it reports once.
|
|
* os_log's formats are printf's, with OpenCL's `%vN` before the length.
|
|
* MTL_LOG_LEVEL and MTL_LOG_BUFFER_SIZE give queues without a state of their
|
|
* own one, and MTL_LOG_TO_STDERR=1 sends what no handler takes to stderr.
|
|
*/
|
|
#include "Bundle.h"
|
|
|
|
/* Far enough under the offset's range that running invocations cannot wrap it
|
|
* past a full buffer. */
|
|
#define STELA_LOG_MAX_CAPACITY (1l << 30)
|
|
|
|
/* The words of `air_log.c`. */
|
|
typedef struct {
|
|
uint32_t next, capacity, reserved[2];
|
|
} StelaLogBuffer;
|
|
|
|
typedef struct {
|
|
uint32_t bytes, level, subsystem, category, format, arguments;
|
|
} StelaLogRecord;
|
|
|
|
/* What MTL_LOG_BUFFER_SIZE defaults to. */
|
|
#define STELA_LOG_DEFAULT_CAPACITY 1024
|
|
|
|
/* How `os_log_type_t` values rank as MTLLogLevel. */
|
|
static NSInteger log_level(uint32_t type)
|
|
{
|
|
switch (type) {
|
|
case 2:
|
|
return MTLLogLevelDebug;
|
|
case 1:
|
|
return MTLLogLevelInfo;
|
|
case 16:
|
|
return MTLLogLevelError;
|
|
case 17:
|
|
return MTLLogLevelFault;
|
|
default:
|
|
return MTLLogLevelNotice;
|
|
}
|
|
}
|
|
|
|
typedef struct {
|
|
const uint8_t *at, *end;
|
|
} StelaLogArguments;
|
|
|
|
static uint64_t next_argument(StelaLogArguments *arguments, size_t bytes)
|
|
{
|
|
uint64_t value = 0;
|
|
if ((size_t)(arguments->end - arguments->at) >= bytes) {
|
|
memcpy(&value, arguments->at, bytes);
|
|
arguments->at += bytes;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/* Appends one lane under the conversion `spec`, whose last character is the
|
|
* conversion: floats are 32-bit, and `l` integers and pointers 64-bit. */
|
|
static void append_lane(NSMutableData *out, const char *spec, size_t bytes, StelaLogArguments *arguments)
|
|
{
|
|
char text[128];
|
|
char conversion = spec[bytes - 1];
|
|
int length;
|
|
if (strchr("fFeEgGaA", conversion)) {
|
|
uint32_t bits = (uint32_t)next_argument(arguments, 4);
|
|
float value;
|
|
memcpy(&value, &bits, sizeof(value));
|
|
length = snprintf(text, sizeof(text), spec, (double)value);
|
|
} else if (conversion == 'p' || memchr(spec, 'l', bytes)) {
|
|
length = snprintf(text, sizeof(text), spec, (unsigned long long)next_argument(arguments, 8));
|
|
} else {
|
|
length = snprintf(text, sizeof(text), spec, (unsigned)next_argument(arguments, 4));
|
|
}
|
|
[out appendBytes:text length:length < (int)sizeof(text) ? length : sizeof(text) - 1];
|
|
}
|
|
|
|
/* Appends the conversion after the `%` at `format`, and answers its length. */
|
|
static size_t append_conversion(NSMutableData *out, const char *format, size_t bytes, StelaLogArguments *arguments)
|
|
{
|
|
char spec[32] = "%";
|
|
size_t length = 1, at = 1;
|
|
unsigned lanes = 1;
|
|
while (at < bytes && strchr("-+ #0", format[at]) && length < sizeof(spec) - 1) {
|
|
spec[length++] = format[at++];
|
|
}
|
|
while (at < bytes && format[at] >= '0' && format[at] <= '9' && length < sizeof(spec) - 1) {
|
|
spec[length++] = format[at++];
|
|
}
|
|
if (at < bytes && format[at] == '.') {
|
|
spec[length++] = format[at++];
|
|
while (at < bytes && format[at] >= '0' && format[at] <= '9' && length < sizeof(spec) - 1) {
|
|
spec[length++] = format[at++];
|
|
}
|
|
}
|
|
if (at < bytes && format[at] == 'v') {
|
|
lanes = 0;
|
|
for (at++; at < bytes && format[at] >= '0' && format[at] <= '9'; at++) {
|
|
lanes = lanes * 10 + (unsigned)(format[at] - '0');
|
|
}
|
|
}
|
|
size_t size = length;
|
|
while (at < bytes && strchr("hl", format[at]) && length < sizeof(spec) - 1) {
|
|
spec[length++] = format[at++];
|
|
}
|
|
if (at >= bytes || length >= sizeof(spec) - 1) {
|
|
[out appendBytes:format length:bytes];
|
|
return bytes;
|
|
}
|
|
/* Metal's compiler refuses the others, but a library need not be its. */
|
|
if (!strchr("diouxXcpfFeEgGaA", format[at])) {
|
|
[out appendBytes:format length:at + 1];
|
|
return at + 1;
|
|
}
|
|
/* Records widen a vector's lanes to 32 bits: OpenCL's `hl` is those, and
|
|
* a float's size says nothing more. `h` and `hh` still size an integer. */
|
|
if (lanes != 1 && (strchr("fFeEgGaA", format[at]) || (length == size + 2 && spec[size + 1] == 'l'))) {
|
|
length = size;
|
|
}
|
|
spec[length++] = format[at++];
|
|
spec[length] = 0;
|
|
for (unsigned lane = 0; lane < lanes; lane++) {
|
|
if (lane) {
|
|
[out appendBytes:"," length:1];
|
|
}
|
|
append_lane(out, spec, length, arguments);
|
|
}
|
|
return at;
|
|
}
|
|
|
|
static NSString *format_message(const char *format, size_t bytes, StelaLogArguments *arguments)
|
|
{
|
|
NSMutableData *out = [NSMutableData dataWithLength:0];
|
|
for (size_t at = 0; at < bytes;) {
|
|
if (format[at] != '%') {
|
|
[out appendBytes:&format[at++] length:1];
|
|
} else if (at + 1 < bytes && format[at + 1] == '%') {
|
|
[out appendBytes:"%" length:1];
|
|
at += 2;
|
|
} else {
|
|
at += append_conversion(out, &format[at], bytes - at, arguments);
|
|
}
|
|
}
|
|
[out appendBytes:"" length:1];
|
|
return [NSString stringWithUTF8String:out.bytes];
|
|
}
|
|
|
|
static NSString *record_string(const char *bytes, size_t length)
|
|
{
|
|
char text[length + 1];
|
|
memcpy(text, bytes, length);
|
|
text[length] = 0;
|
|
return [NSString stringWithUTF8String:text];
|
|
}
|
|
|
|
@implementation StelaGPULogState {
|
|
StelaGPUBuffer *_buffer;
|
|
NSMutableArray *_handlers;
|
|
NSInteger _level;
|
|
os_unfair_lock _lock;
|
|
NSUInteger _running;
|
|
bool _full, _standardError;
|
|
}
|
|
|
|
- (instancetype)initWithBuffer:(StelaGPUBuffer *)buffer level:(NSInteger)level capacity:(NSUInteger)capacity
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_buffer = buffer;
|
|
_level = level;
|
|
_lock = OS_UNFAIR_LOCK_INIT;
|
|
_handlers = [NSMutableArray arrayWithCapacity:2];
|
|
StelaLogBuffer *header = buffer->_contents;
|
|
header->capacity = (uint32_t)capacity;
|
|
const char *destination = getenv("MTL_LOG_TO_STDERR");
|
|
_standardError = destination && !strcmp(destination, "1");
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (uint64_t)bufferHandle
|
|
{
|
|
return _buffer->_handle;
|
|
}
|
|
|
|
- (void)addLogHandler:(void (^)(NSString *, NSString *, NSInteger, NSString *))handler
|
|
{
|
|
os_unfair_lock_lock(&_lock);
|
|
[_handlers addObject:[handler copy]];
|
|
os_unfair_lock_unlock(&_lock);
|
|
}
|
|
|
|
- (void)commandBufferStarted
|
|
{
|
|
os_unfair_lock_lock(&_lock);
|
|
_running++;
|
|
os_unfair_lock_unlock(&_lock);
|
|
}
|
|
|
|
- (void)deliver:(const StelaLogRecord *)record handlers:(NSArray *)handlers
|
|
{
|
|
const char *text = (const char *)(record + 1);
|
|
size_t strings = record->subsystem + record->category + record->format;
|
|
const uint8_t *arguments = (const uint8_t *)text + ((strings + 3) & ~(size_t)3);
|
|
NSInteger level = log_level(record->level);
|
|
if (level < _level) {
|
|
return;
|
|
}
|
|
StelaLogArguments reader = {arguments, arguments + record->arguments};
|
|
// os_log_default has neither, which Metal hands its handlers as nil.
|
|
NSString *subsystem = record->subsystem ? record_string(text, record->subsystem) : nil;
|
|
NSString *category = record->category ? record_string(text + record->subsystem, record->category) : nil;
|
|
NSString *message = format_message(text + record->subsystem + record->category, record->format, &reader);
|
|
for (NSUInteger index = 0; index < handlers.count; index++) {
|
|
void (^handler)(NSString *, NSString *, NSInteger, NSString *) = handlers[index];
|
|
handler(subsystem, category, level, message);
|
|
}
|
|
if (!handlers.count && _standardError) {
|
|
dprintf(2, "%s %s: %s\n", subsystem ? subsystem.UTF8String : "", category ? category.UTF8String : "",
|
|
message.UTF8String);
|
|
} else if (!handlers.count) {
|
|
NSLog(@"%@ %@: %@", subsystem, category, message);
|
|
}
|
|
}
|
|
|
|
/* Copies the records out once nothing writes them, so a handler may commit. */
|
|
- (void)commandBufferEnded
|
|
{
|
|
os_unfair_lock_lock(&_lock);
|
|
if (--_running) {
|
|
os_unfair_lock_unlock(&_lock);
|
|
return;
|
|
}
|
|
StelaLogBuffer *header = _buffer->_contents;
|
|
uint32_t next = header->next < header->capacity ? header->next : header->capacity;
|
|
bool dropped = header->next > header->capacity;
|
|
NSData *records = [NSData dataWithBytes:header + 1 length:next];
|
|
NSArray *handlers = [_handlers copy];
|
|
/* A record that did not fit leaves the rest unwritten, which reads as an
|
|
* end only when cleared. */
|
|
memset(header + 1, 0, next);
|
|
header->next = 0;
|
|
os_unfair_lock_unlock(&_lock);
|
|
|
|
const uint8_t *at = records.bytes;
|
|
const uint8_t *end = at + next;
|
|
while ((size_t)(end - at) >= sizeof(StelaLogRecord)) {
|
|
const StelaLogRecord *record = (const StelaLogRecord *)at;
|
|
if (record->bytes < sizeof(*record) || record->bytes > (size_t)(end - at)) {
|
|
break;
|
|
}
|
|
[self deliver:record handlers:handlers];
|
|
at += record->bytes;
|
|
}
|
|
if (dropped) {
|
|
StelaFailure failure = {.reason = "a log state's buffer filled and dropped messages"};
|
|
stela_report("shader logging", &failure);
|
|
}
|
|
}
|
|
|
|
@end
|
|
|
|
/* MTL_LOG_LEVEL's value, with or without Metal's prefix. */
|
|
static bool named_level(const char *text, NSInteger *level)
|
|
{
|
|
static const char *const names[] = {"Debug", "Info", "Notice", "Error", "Fault"};
|
|
if (!strncmp(text, "MTLLogLevel", strlen("MTLLogLevel"))) {
|
|
text += strlen("MTLLogLevel");
|
|
}
|
|
for (NSInteger index = 0; index < 5; index++) {
|
|
if (!strcmp(text, names[index])) {
|
|
*level = MTLLogLevelDebug + index;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@implementation StelaGPUDevice (Logging)
|
|
|
|
- (StelaGPULogState *)environmentLogState
|
|
{
|
|
const char *name = getenv("MTL_LOG_LEVEL"), *size = getenv("MTL_LOG_BUFFER_SIZE");
|
|
if (!name && !size) {
|
|
return nil;
|
|
}
|
|
NSInteger level = MTLLogLevelDebug;
|
|
long capacity = size ? strtol(size, NULL, 0) : STELA_LOG_DEFAULT_CAPACITY;
|
|
StelaGPULogState *state = !name || named_level(name, &level) ?
|
|
[self logStateWithCapacity:capacity level:level error:NULL] :
|
|
nil;
|
|
if (!state) {
|
|
StelaFailure failure = {.reason = "MTL_LOG_LEVEL or MTL_LOG_BUFFER_SIZE is invalid"};
|
|
stela_report("shader logging", &failure);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
- (id)newLogStateWithDescriptor:(id)descriptor error:(NSError **)error
|
|
{
|
|
return [self logStateWithCapacity:[descriptor bufferSize] level:(NSInteger)[descriptor level] error:error];
|
|
}
|
|
|
|
- (StelaGPULogState *)logStateWithCapacity:(NSInteger)capacity level:(NSInteger)level error:(NSError **)error
|
|
{
|
|
if (capacity < 1024 || capacity > STELA_LOG_MAX_CAPACITY) {
|
|
if (error) {
|
|
StelaFailure failure = {.reason = "a log state's buffer is from 1 KiB to 1 GiB"};
|
|
*error = stela_error(MTLLogStateErrorDomain, MTLLogStateErrorInvalidSize, &failure);
|
|
}
|
|
return nil;
|
|
}
|
|
StelaGPUBuffer *buffer = [self newBufferWithLength:sizeof(StelaLogBuffer) + (NSUInteger)capacity
|
|
options:MTLStorageModeShared << 4];
|
|
if (!buffer) {
|
|
if (error) {
|
|
StelaFailure failure = {.reason = "a log state's buffer"};
|
|
*error = stela_error(MTLLogStateErrorDomain, MTLLogStateErrorInvalid, &failure);
|
|
}
|
|
return nil;
|
|
}
|
|
return [[StelaGPULogState alloc] initWithBuffer:buffer level:level capacity:(NSUInteger)capacity];
|
|
}
|
|
|
|
@end
|