Repository: JuliaLang/julia
File: src/timing.c (C)

Code before:

    // No matching event found - create a new one
    cached_ittapi_event_t *new_event = (cached_ittapi_event_t *)malloc(sizeof(cached_ittapi_event_t));
    arraylist_push(&jl_timing_ittapi_events, (void *)new_event);
    new_event->name = event;
    new_event->event = __itt_event_create(event, strlen(event));
    JL_UNLOCK_NOGC(&jl_timing_ittapi_events_lock);

    return new_event->event;
}

#endif // USE_ITTAPI

#ifdef USE_APPLE_OSLOG

void _jl_os_log_signpost_print(jl_timing_block_t* block, const char *message) {
    // Buffer the metadata to be emitted with the interval end
    size_t len = strlen(message);
    size_t remaining = sizeof(block->oslog_metadata) - block->oslog_metadata_len - 1;
    if (remaining > 0) {
        size_t to_copy = len < remaining ? len : remaining;
        memcpy(block->oslog_metadata + block->oslog_metadata_len, message, to_copy);
        block->oslog_metadata_len += to_copy;
        block->oslog_metadata[block->oslog_metadata_len] = '\0';
    }
}

void _jl_os_signpost_start(jl_timing_block_t* block) {
    // Initialize metadata buffer
    block->oslog_metadata[0] = '\0';
    block->oslog_metadata_len = 0;

    if (!os_signpost_enabled(block->event->os_log_event.log)) {
        return;
    }
    uint8_t _Alignas(16) buffer[64];
    buffer[0] = 0;
    block->signpost_id = os_signpost_id_make_with_pointer((block)->event->os_log_event.log, (void *)(block));
    _os_signpost_emit_with_name_impl(&__dso_handle, block->event->os_log_event.log, OS_SIGNPOST_INTERVAL_BEGIN,
        block->signpost_id, (block)->event->os_log_event.name, "", buffer, sizeof(buffer));
}

void _jl_os_signpost_stop(jl_timing_block_t * block) {
    if (!os_signpost_enabled(block->event->os_log_event.log)) {
        return;
    }

    uint8_t _Alignas(16) buffer[128];
    if (block->oslog_metadata_len > 0) {
        // Emit with buffered metadata
        const char *msg = block->oslog_metadata;

Review comment (bug):
This `block` is stack-allocated, so this seems wrong according to the documentation:
> Creates a signpost identifier that’s unique among signposts logging to the specified log, using a pointer value to generate the unique value.

Should this be part of the `event` instead?

Code after:

    // No matching event found - create a new one
    cached_ittapi_event_t *new_event = (cached_ittapi_event_t *)malloc(sizeof(cached_ittapi_event_t));
    arraylist_push(&jl_timing_ittapi_events, (void *)new_event);
    new_event->name = event;
    new_event->event = __itt_event_create(event, strlen(event));
    JL_UNLOCK_NOGC(&jl_timing_ittapi_events_lock);

    return new_event->event;
}

#endif // USE_ITTAPI

#ifdef USE_APPLE_OSLOG

void _jl_timing_os_signpost_start(jl_timing_block_t* block) {
    // Initialize metadata buffer
    block->oslog_metadata[0] = '\0';
    block->oslog_metadata_len = 0;

    if (!os_signpost_enabled(block->event->os_log_event.log)) {
        return;
    }
    uint8_t _Alignas(16) buffer[64];
    buffer[0] = 0;
    block->signpost_id = os_signpost_id_make_with_pointer((block)->event->os_log_event.log, (void *)(block));
    _os_signpost_emit_with_name_impl(&__dso_handle, block->event->os_log_event.log, OS_SIGNPOST_INTERVAL_BEGIN,
        block->signpost_id, (block)->event->os_log_event.name, "", buffer, sizeof(buffer));
}

void _jl_timing_os_signpost_stop(jl_timing_block_t * block) {
    if (!os_signpost_enabled(block->event->os_log_event.log)) {
        return;
    }

    uint8_t _Alignas(16) buffer[128];
    if (block->oslog_metadata_len > 0) {
        // Emit with buffered metadata
        const char *msg = block->oslog_metadata;
        buffer[0] = 2;  // HasNonScalarItems
        buffer[1] = 1;  // ItemCount
        buffer[2] = 0x22; // StringKind + Public
        buffer[3] = 8;  // pointer size
        memcpy(&buffer[4], &msg, 8);
        _os_signpost_emit_with_name_impl(&__dso_handle, block->event->os_log_event.log, OS_SIGNPOST_INTERVAL_END,
            block->signpost_id, block->event->os_log_event.name, "%s", buffer, 12);
    } else {
        // No metadata, emit empty end
        buffer[0] = 0;
        _os_signpost_emit_with_name_impl(&__dso_handle, block->event->os_log_event.log, OS_SIGNPOST_INTERVAL_END,
            block->signpost_id, block->event->os_log_event.name, "", buffer, sizeof(buffer));