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

Code before:
    ios_t buf;
    ios_mem(&buf, IOS_INLSIZE);
    buf.growable = 0; // Restrict to inline buffer to avoid allocation
    jl_vprintf((JL_STREAM*)&buf, format, args);
    if (buf.size == buf.maxsize)
        memset(&buf.buf[IOS_INLSIZE - 3], '.', 3);
    buf.buf[buf.size] = '\0'; // Ensure null-termination
    jl_timing_puts(cur_block, buf.buf);
#endif
    va_end(args);
}

JL_DLLEXPORT void jl_timing_puts(jl_timing_block_t *cur_block, const char *str)
{
#ifdef USE_TRACY
    TracyCZoneText(cur_block->tracy_ctx, str, strlen(str));
#endif
#ifdef USE_APPLE_OSLOG
        // Buffer the metadata to be emitted with the interval end
    size_t len = strlen(str);
    size_t remaining = sizeof(cur_block->oslog_metadata) - cur_block->oslog_metadata_len - 1;
    if (remaining > 0) {
        size_t to_copy = len < remaining ? len : remaining;
        memcpy(cur_block->oslog_metadata + cur_block->oslog_metadata_len, str, to_copy);
        cur_block->oslog_metadata_len += to_copy;
        cur_block->oslog_metadata[cur_block->oslog_metadata_len] = '\0';
    }
#endif
}

void jl_timing_task_init(jl_task_t *t)
{
#ifdef USE_TRACY
    jl_value_t *start_type = jl_typeof(t->start);
    const char *start_name = "";
    if (jl_is_datatype(start_type))
        start_name = jl_symbol_name(((jl_datatype_t *) start_type)->name->name);

    static uint16_t task_id = 1;

    // XXX: Tracy uses this as a handle internally and requires that this
    // string live forever, so this allocation is intentionally leaked.
    char *fiber_name;
    if (start_name[0] == '#') {
        jl_method_instance_t *mi = jl_method_lookup(&t->start, 1, jl_get_world_counter());
        const char *filename = gnu_basename(jl_symbol_name(mi->def.method->file));
        const char *module_name = jl_symbol_name(mi->def.method->module->name);

        // 26 characters in "Task 65535 (:0000000 in )\0"
        size_t fiber_name_len = strlen(filename) + strlen(module_name) + 26;
        fiber_name = (char *)malloc(fiber_name_len);

Review comment (suggestion):
```suggestion
    assert(cur_block->oslog_metadata_len < sizeof(cur_block->oslog_metadata));
    size_t remaining = sizeof(cur_block->oslog_metadata) - cur_block->oslog_metadata_len - 1;
```
this is correct as-is, but easy to violate so worth the assert

Code after:

JL_DLLEXPORT void jl_timing_printf(jl_timing_block_t *cur_block, const char *format, ...)
{
    va_list args;
    va_start(args, format);

#if defined(USE_APPLE_OSLOG) || defined(USE_TRACY)
    ios_t buf;
    ios_mem(&buf, IOS_INLSIZE);
    buf.growable = 0; // Restrict to inline buffer to avoid allocation
    jl_vprintf((JL_STREAM*)&buf, format, args);
    _jl_timing_show_buf(cur_block, &buf);
#endif
    va_end(args);
}

JL_DLLEXPORT void jl_timing_puts(jl_timing_block_t *cur_block, const char *str)
{
#ifdef USE_TRACY
    TracyCZoneText(cur_block->tracy_ctx, str, strlen(str));
#endif
#ifdef USE_APPLE_OSLOG
        // Buffer the metadata to be emitted with the interval end
    assert(cur_block->oslog_metadata_len < sizeof(cur_block->oslog_metadata));
    size_t len = strlen(str);
    size_t remaining = sizeof(cur_block->oslog_metadata) - cur_block->oslog_metadata_len - 1;
    if (remaining > 0) {
        size_t to_copy = len < remaining ? len : remaining;
        memcpy(cur_block->oslog_metadata + cur_block->oslog_metadata_len, str, to_copy);
        cur_block->oslog_metadata_len += to_copy;
        cur_block->oslog_metadata[cur_block->oslog_metadata_len] = '\0';
    }
#endif
}

void jl_timing_task_init(jl_task_t *t)
{
#ifdef USE_TRACY
    jl_value_t *start_type = jl_typeof(t->start);
    const char *start_name = "";
    if (jl_is_datatype(start_type))
        start_name = jl_symbol_name(((jl_datatype_t *) start_type)->name->name);

    static uint16_t task_id = 1;

    // XXX: Tracy uses this as a handle internally and requires that this
    // string live forever, so this allocation is intentionally leaked.
    char *fiber_name;
    if (start_name[0] == '#') {
        jl_method_instance_t *mi = jl_method_lookup(&t->start, 1, jl_get_world_counter());
        const char *filename = gnu_basename(jl_symbol_name(mi->def.method->file));