ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
emu_prof.c File Reference

Firmware profiler implementation (see emu_prof.h). More...

#include "emu_prof.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "emu_elf.h"
#include "emu_host_io_internal.h"
Include dependency graph for emu_prof.c:

Go to the source code of this file.

Data Structures

struct  prof_sym_t
struct  prof_name_sink_t
 Sink configuration for one streamed profiler symbol name. More...

Enumerations

enum  : uint32_t {
  k_prof_max_syms = 8192U ,
  k_prof_top_n = 40U
}
enum  : uint32_t {
  k_prof_max_depth = 64U ,
  k_prof_max_samples = 16384U ,
  k_prof_samp_every = 256U
}
enum  prof_phase_t : uint32_t {
  k_no_fn = 0xFFFFFFFFU ,
  k_phase_depth = 2U ,
  k_phase_lines = 24U ,
  k_phase_pct_x1 = 100U
}
 Boot-timeline "phase" collapse constants for internal_prof_print_boot_timeline. More...

Functions

double board_now_s (void)
 Monotonic wall-clock seconds.
static int internal_prof_cmp (const void *a, const void *b)
 qsort comparator: order the FUNC symbols by entry address.
static bool internal_prof_symbol (const emu_elf_symbol_t *symbol, void *ctx)
 Collect one sized STT_FUNC symbol into the bounded profiler table.
void prof_load (const emu_elf_source_t *elf)
 Collect + sort FUNC symbols (RA8_EMU_PROFILE only) for PC bucketing.
static uint32_t internal_prof_find (uint32_t pc)
 Binary-search the FUNC symbol owning pc; returns s_prof_n if none.
void prof_add (uint32_t pc, double dt)
 Attribute dt wall seconds to pc's function (wall-sample mode).
static void internal_prof_decimate (void)
 Halve the sample store (merge adjacent pairs) when it fills up.
static void internal_prof_sample (uint32_t weight)
 Append the live call chain as one chronological sample of weight insns.
static void internal_prof_stack_update (uint32_t f)
 Fold f (PC's owning FUNC index) into the live call chain (push/pop).
static void internal_prof_insn_hook (uc_engine *uc, uint64_t address, uint32_t size, void *user)
 Perform prof insn hook for the emu prof model.
static bool internal_prof_io_ok (emu_io_result_t result)
 Convert a host-I/O result to the profiler writer's boolean accumulator.
static bool internal_prof_name_chunk (const char *bytes, size_t length, void *opaque)
 Emit one transient source-name chunk to a selected sink.
static bool internal_prof_name (uint32_t symbol, int fd, char escape, bool error_sink)
 Stream one retained-offset symbol name to a file or error sink.
static bool internal_prof_json_frames (int fd)
 Write the speedscope shared.frames array: one entry per symbol.
static bool internal_prof_json_samples (int fd)
 Write the speedscope samples array: one frame-index stack per sample.
static bool internal_prof_json_weights (int fd)
 Write the speedscope weights array, parallel to the samples array.
static bool internal_prof_js_frames (int fd)
 Write the flamechart page's FRAMES array as JavaScript string literals.
static void internal_prof_write_speedscope (const char *path)
 Perform prof write speedscope for the emu prof model.
static void internal_prof_write_html (const char *path, uint64_t total)
 Write a self-contained, locally-openable HTML flamechart of the samples.
static uint64_t internal_prof_accumulate_incl_self (void)
 Reset then fill s_incl/s_self from the samples; return total weight.
static void internal_prof_print_phase (uint32_t segfn, uint64_t cum, uint64_t segw, uint64_t total)
 Print one boot-timeline row: start percent, width percent, and phase.
static void internal_prof_print_boot_timeline (uint64_t total)
 Print the boot timeline: each contiguous shallow-depth phase run.
static void internal_prof_print_incl_self_table (const char *html, const char *out, uint64_t total)
 Print the inclusive/self table (sorted by inclusive weight).
static void internal_prof_report_flamechart (void)
 Speedscope export + inclusive/self breakdown + phase timeline (insn mode).
void prof_report (void)
 Print the top hot functions (by wall time or instruction count) at run end.
void emu_prof_install (uc_engine *uc)
 Implementation of emu_prof_install() – arms the insn hook in insn mode.
prof_mode_t emu_prof_mode (void)
 Implementation of emu_prof_mode() – plain state read.
uint64_t emu_prof_total_insns (void)
 Implementation of emu_prof_total_insns() – plain counter read.
void emu_prof_set_stop_pc (uint32_t pc)
 Implementation of emu_prof_set_stop_pc() – plain state write.
bool emu_prof_stop_hit (void)
 Implementation of emu_prof_stop_hit() – plain flag read.

Variables

static const double s_nsec_per_sec = 1.0e9
 Nanoseconds per second (timespec tv_nsec -> seconds).
static prof_sym_t s_prof [k_prof_max_syms]
static const emu_elf_source_ts_prof_elf
 Borrowed run-long source for names.
static uint32_t s_prof_n = 0U
static double s_prof_total_s = 0.0
static uint64_t s_prof_total_i = 0U
static prof_mode_t s_prof_mode = k_prof_off
static uint16_t s_pstk [k_prof_max_depth]
 Live chain.
static uint32_t s_pstk_n = 0U
 Chain depth.
static uint16_t s_samp [k_prof_max_samples][k_prof_max_depth]
 root..leaf.
static uint8_t s_samp_d [k_prof_max_samples]
 Per-sample chain depth.
static uint32_t s_samp_w [k_prof_max_samples]
 Per-sample weight (insns).
static uint32_t s_samp_n = 0U
 Stored sample count.
static uint64_t s_samp_every = (uint64_t)k_prof_samp_every
 Insns per sample (>>x2).
static uint64_t s_samp_acc = 0U
 Insns since last sample.
static uint32_t s_prof_stop_pc = 0U
 RA8_EMU_STOP_PC (0=off).
static bool s_prof_stop_hit = false
 Set when STOP_PC reached.
static uint64_t s_incl [k_prof_max_syms]
 Inclusive weight (report).
static uint64_t s_self [k_prof_max_syms]
 Self (leaf) weight.
static const char s_k_prof_html_head []
static const char s_k_prof_html_js []
static const double s_percent_scale = 100.0
 Fraction-to-per-cent scale (fraction * 100.0 == per-cent).

Detailed Description

Firmware profiler implementation (see emu_prof.h).

The RA8_EMU_PROFILE profiler: FUNC-symbol collection, the wall-time sampler, the exact per-instruction hook with call-chain reconstruction, and the run-end reports (speedscope JSON export, the self-contained HTML flamechart, the boot timeline, and the inclusive/self table) – moved verbatim out of the ra8_emulator main translation unit.

Since
0.1.0

Definition in file emu_prof.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum : uint32_t
Enumerator
k_prof_max_depth 

Deepest call chain captured per sample.

k_prof_max_samples 

Chronological stack samples (decimated).

k_prof_samp_every 

Default instructions per chain sample.

Definition at line 84 of file emu_prof.c.

◆ anonymous enum

anonymous enum : uint32_t
Enumerator
k_prof_max_syms 

Cap on profiled FUNC symbols.

k_prof_top_n 

Top entries printed in the report.

Definition at line 50 of file emu_prof.c.

◆ prof_phase_t

enum prof_phase_t : uint32_t

Boot-timeline "phase" collapse constants for internal_prof_print_boot_timeline.

Enumerator
k_no_fn 

Sentinel for "no phase frame".

k_phase_depth 

Chain depth used as the boot "phase".

k_phase_lines 

Cap on printed timeline segments.

k_phase_pct_x1 

Per-cent base: keep segments >= 1%.

Definition at line 701 of file emu_prof.c.

Function Documentation

◆ board_now_s()

double board_now_s ( void )

Monotonic wall-clock seconds.

CLOCK_MONOTONIC as a double, the time base of the wall-sample profiler mode (and any other host-side wall measurement).

Returns
Monotonic seconds since an arbitrary epoch.
Return values
0.0Only if the clock read fails (it does not on supported hosts).
Precondition
None (safe at any time).
The host provides CLOCK_MONOTONIC.
Postcondition
No state is modified.
Note
Thread-safe (pure system-clock read).
Since
0.1.0
Postcondition
Ownership of caller-supplied storage is unchanged.

Definition at line 31 of file emu_prof.c.

References s_nsec_per_sec.

Referenced by internal_run_loop_prologue().

◆ emu_prof_install()

void emu_prof_install ( uc_engine * uc)

Implementation of emu_prof_install() – arms the insn hook in insn mode.

Arm the per-instruction profiling hook (insn mode only).

Definition at line 940 of file emu_prof.c.

References internal_prof_insn_hook(), k_prof_insn, and s_prof_mode.

Referenced by internal_main_install_run_seams().

◆ emu_prof_mode()

prof_mode_t emu_prof_mode ( void )

Implementation of emu_prof_mode() – plain state read.

The latched profiler mode.

Definition at line 949 of file emu_prof.c.

References s_prof_mode.

Referenced by internal_run_loop_prologue(), and internal_run_stop_prof_idle().

◆ emu_prof_set_stop_pc()

void emu_prof_set_stop_pc ( uint32_t pc)

Implementation of emu_prof_set_stop_pc() – plain state write.

Set the RA8_EMU_STOP_PC early-stop address (0 disables).

Definition at line 961 of file emu_prof.c.

References s_prof_stop_pc.

Referenced by internal_guard_apply_stop_pc().

◆ emu_prof_stop_hit()

bool emu_prof_stop_hit ( void )

Implementation of emu_prof_stop_hit() – plain flag read.

Whether the RA8_EMU_STOP_PC address was reached.

Definition at line 967 of file emu_prof.c.

References s_prof_stop_hit.

Referenced by internal_run_inner_check_stops(), and internal_run_loop_run_chunk().

◆ emu_prof_total_insns()

uint64_t emu_prof_total_insns ( void )

Implementation of emu_prof_total_insns() – plain counter read.

Total instructions retired under the per-instruction hook.

Definition at line 955 of file emu_prof.c.

References s_prof_total_i.

Referenced by internal_run_stop_prof_idle().

◆ internal_prof_accumulate_incl_self()

uint64_t internal_prof_accumulate_incl_self ( void )
static

Reset then fill s_incl/s_self from the samples; return total weight.

Reset then fill s_incl/s_self from the samples; return total weight; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Returns
The prof accumulate incl self result produced by the emu prof model.
Return values
valueThe operation-specific prof accumulate incl self value.
Precondition
Arguments satisfy the ranges documented for prof accumulate incl self.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 718 of file emu_prof.c.

References RA8_INTERNAL, s_incl, s_prof_n, s_samp, s_samp_d, s_samp_n, s_samp_w, and s_self.

Referenced by internal_prof_report_flamechart().

◆ internal_prof_cmp()

int internal_prof_cmp ( const void * a,
const void * b )
static

qsort comparator: order the FUNC symbols by entry address.

Qsort comparator: order the func symbols by entry address; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]aA input used by the operation.
[in]bB input used by the operation.
Returns
The prof cmp result produced by the emu prof model.
Return values
valueThe operation-specific prof cmp value.
Precondition
Arguments satisfy the ranges documented for prof cmp.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 114 of file emu_prof.c.

References RA8_INTERNAL.

Referenced by prof_load().

◆ internal_prof_decimate()

void internal_prof_decimate ( void )
static

Halve the sample store (merge adjacent pairs) when it fills up.

Halve the sample store (merge adjacent pairs) when it fills up; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for prof decimate.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 230 of file emu_prof.c.

References memcpy(), RA8_INTERNAL, s_samp, s_samp_d, s_samp_every, s_samp_n, and s_samp_w.

Referenced by internal_prof_sample().

◆ internal_prof_find()

uint32_t internal_prof_find ( uint32_t pc)
static

Binary-search the FUNC symbol owning pc; returns s_prof_n if none.

Binary-search the func symbol owning pc; returns s_prof_n if none; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]pcGuest program-counter value associated with the operation.
Returns
The prof find result produced by the emu prof model.
Return values
valueThe operation-specific prof find value.
Precondition
Arguments satisfy the ranges documented for prof find.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 190 of file emu_prof.c.

References RA8_INTERNAL, s_prof, and s_prof_n.

Referenced by internal_prof_insn_hook(), and prof_add().

◆ internal_prof_insn_hook()

void internal_prof_insn_hook ( uc_engine * uc,
uint64_t address,
uint32_t size,
void * user )
static

Perform prof insn hook for the emu prof model.

Perform prof insn hook for the emu prof model; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
[in]addressGuest address involved in the operation.
[in]sizeSize of the requested region or access in bytes.
[in,out]userHook context supplied when the callback was registered.
Precondition
Arguments satisfy the ranges documented for prof insn hook.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 314 of file emu_prof.c.

References internal_prof_find(), internal_prof_sample(), internal_prof_stack_update(), s_prof, s_prof_n, s_prof_stop_hit, s_prof_stop_pc, s_prof_total_i, s_samp_acc, and s_samp_every.

Referenced by emu_prof_install().

◆ internal_prof_io_ok()

bool internal_prof_io_ok ( emu_io_result_t result)
static

Convert a host-I/O result to the profiler writer's boolean accumulator.

Keeps each writer expression explicit while preserving the first failure.

Parameters
[in]resultRaw descriptor operation result.
Returns
True only for complete success.
Return values
trueresult reports k_emu_io_ok.
falseresult reports any failure or truncation.
Precondition
result is fully initialised.
The caller has not discarded an earlier failure.
Postcondition
No profiler or descriptor state changes.
The returned value depends only on result.status.
Note
Pure value predicate; thread-safe.
Since
0.1.0

Definition at line 352 of file emu_prof.c.

References k_emu_io_ok, RA8_INTERNAL, and emu_io_result_t::status.

Referenced by internal_prof_js_frames(), internal_prof_json_frames(), internal_prof_json_samples(), internal_prof_json_weights(), internal_prof_name_chunk(), internal_prof_write_html(), and internal_prof_write_speedscope().

◆ internal_prof_js_frames()

bool internal_prof_js_frames ( int fd)
static

Write the flamechart page's FRAMES array as JavaScript string literals.

The same frame names as internal_prof_json_frames, but the viewer markup quotes with ', so ' and \\endiskip are the characters escaped here.

Parameters
[in]fdRaw output descriptor positioned after the FRAMES=[.
Returns
True only when every raw write completed.
Return values
trueAll quoted names were written.
falseAt least one descriptor operation failed.
Precondition
fd is a valid writable descriptor.
s_prof_n frames are populated.
Postcondition
Exactly s_prof_n comma-separated quoted names are written.
The enclosing bracket is left for the caller to close.
Note
Not thread-safe; the profiler is single-threaded host-side.
Since
0.1.0

Definition at line 539 of file emu_prof.c.

References internal_prof_io_ok(), internal_prof_name(), priv_emu_io_file_char(), priv_emu_io_file_text(), RA8_INTERNAL, and s_prof_n.

Referenced by internal_prof_write_html().

◆ internal_prof_json_frames()

bool internal_prof_json_frames ( int fd)
static

Write the speedscope shared.frames array: one entry per symbol.

Escapes JSON quote and backslash characters while copying ELF symbol names.

Parameters
[in]fdRaw output descriptor positioned after the "frames":[.
Returns
True only when every raw write completed.
Return values
trueAll frame objects were written.
falseAt least one descriptor operation failed.
Precondition
fd is a valid writable descriptor.
s_prof_n frames are populated.
Postcondition
Exactly s_prof_n objects are attempted in order.
The enclosing bracket is left for the caller to close.
Note
Not thread-safe; the profiler is single-threaded host-side.
Since
0.1.0

Definition at line 443 of file emu_prof.c.

References internal_prof_io_ok(), internal_prof_name(), priv_emu_io_file_text(), RA8_INTERNAL, and s_prof_n.

Referenced by internal_prof_write_speedscope().

◆ internal_prof_json_samples()

bool internal_prof_json_samples ( int fd)
static

Write the speedscope samples array: one frame-index stack per sample.

Emits each root-to-leaf frame-index vector with JSON delimiters.

Parameters
[in]fdRaw output descriptor positioned after the "samples":[.
Returns
True only when every raw write completed.
Return values
trueAll sample arrays were written.
falseAt least one descriptor operation failed.
Precondition
fd is a valid writable descriptor.
s_samp_n samples are populated with matching depths in s_samp_d.
Postcondition
Exactly s_samp_n comma-separated arrays are written.
The enclosing bracket is left for the caller to close.
Note
Not thread-safe; the profiler is single-threaded host-side.
Since
0.1.0

Definition at line 473 of file emu_prof.c.

References internal_prof_io_ok(), priv_emu_io_file_char(), priv_emu_io_filef(), RA8_INTERNAL, s_samp, s_samp_d, and s_samp_n.

Referenced by internal_prof_write_html(), and internal_prof_write_speedscope().

◆ internal_prof_json_weights()

bool internal_prof_json_weights ( int fd)
static

Write the speedscope weights array, parallel to the samples array.

Emits the instruction weight corresponding to every captured sample.

Parameters
[in]fdRaw output descriptor positioned after the "weights":[.
Returns
True only when every raw write completed.
Return values
trueAll weights were written.
falseAt least one descriptor operation failed.
Precondition
fd is a valid writable descriptor.
s_samp_w holds s_samp_n weights.
Postcondition
Exactly s_samp_n comma-separated integers are written.
The enclosing bracket is left for the caller to close.
Note
Not thread-safe; the profiler is single-threaded host-side.
Since
0.1.0

Definition at line 508 of file emu_prof.c.

References internal_prof_io_ok(), priv_emu_io_filef(), RA8_INTERNAL, s_samp_n, and s_samp_w.

Referenced by internal_prof_write_html(), and internal_prof_write_speedscope().

◆ internal_prof_name()

bool internal_prof_name ( uint32_t symbol,
int fd,
char escape,
bool error_sink )
static

Stream one retained-offset symbol name to a file or error sink.

Resolves the retained offset against the still-open source in bounded chunks.

Parameters
[in]symbolProfiler symbol index.
[in]fdExplicit file descriptor, ignored for error sink.
[in]escapeFile-sink quote character to escape, or NUL.
[in]error_sinkWhether to use the injected error sink.
Returns
True only after a terminating NUL and exact output.
Return values
trueThe complete name was emitted exactly.
falseThe index/source was invalid or a read/write failed.
Precondition
symbol is below s_prof_n and s_prof_elf remains open.
File mode supplies a valid fd.
Postcondition
Output bytes match the old resident-string walk exactly.
No source byte pointer survives the call.
Note
Reads only bounded transient name chunks.
Since
0.1.0

Definition at line 416 of file emu_prof.c.

References elf_string_foreach(), internal_prof_name_chunk(), prof_name_sink_t::ok, RA8_INTERNAL, s_prof, s_prof_elf, and s_prof_n.

Referenced by internal_prof_js_frames(), internal_prof_json_frames(), internal_prof_print_incl_self_table(), internal_prof_print_phase(), and prof_report().

◆ internal_prof_name_chunk()

bool internal_prof_name_chunk ( const char * bytes,
size_t length,
void * opaque )
static

Emit one transient source-name chunk to a selected sink.

Writes directly to the chosen descriptor or injected error sink.

Parameters
[in]bytesName bytes without a terminating NUL.
[in]lengthTransient chunk length.
[in,out]opaqueprof_name_sink_t output state.
Returns
True while every write succeeds.
Return values
trueThe complete chunk was emitted.
falseA sink operation failed.
Precondition
bytes spans length readable bytes.
opaque selects a valid sink.
Postcondition
Every input byte is emitted, with selected escaping.
Failure latches ok=false and stops string streaming.
Note
Consumes the transient chunk before returning.
Since
0.1.0

Definition at line 381 of file emu_prof.c.

References prof_name_sink_t::error_sink, prof_name_sink_t::escape, prof_name_sink_t::fd, internal_prof_io_ok(), prof_name_sink_t::ok, priv_emu_io_err_bytes(), priv_emu_io_file_char(), and RA8_INTERNAL.

Referenced by internal_prof_name().

◆ internal_prof_print_boot_timeline()

void internal_prof_print_boot_timeline ( uint64_t total)
static

Print the boot timeline: each contiguous shallow-depth phase run.

Print the boot timeline: each contiguous shallow-depth phase run; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]totalTotal count used to normalize or report the operation.
Precondition
Arguments satisfy the ranges documented for prof print boot timeline.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 780 of file emu_prof.c.

References internal_prof_print_phase(), k_no_fn, k_phase_depth, k_phase_lines, k_phase_pct_x1, priv_emu_io_errf(), RA8_INTERNAL, s_samp, s_samp_d, s_samp_n, and s_samp_w.

Referenced by internal_prof_report_flamechart().

◆ internal_prof_print_incl_self_table()

void internal_prof_print_incl_self_table ( const char * html,
const char * out,
uint64_t total )
static

Print the inclusive/self table (sorted by inclusive weight).

Print the inclusive/self table (sorted by inclusive weight); this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]htmlHtml input used by the operation.
[in]outDestination storage receiving the computed result.
[in]totalTotal count used to normalize or report the operation.
Precondition
Arguments satisfy the ranges documented for prof print incl self table.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 831 of file emu_prof.c.

References internal_prof_name(), k_prof_top_n, priv_emu_io_err_text(), priv_emu_io_errf(), s_incl, s_percent_scale, s_prof_n, s_samp_n, and s_self.

Referenced by internal_prof_report_flamechart().

◆ internal_prof_print_phase()

void internal_prof_print_phase ( uint32_t segfn,
uint64_t cum,
uint64_t segw,
uint64_t total )
static

Print one boot-timeline row: start percent, width percent, and phase.

Emits the two normalized percentages, then the phase name when the run belongs to a known function and a placeholder when it does not.

Parameters
[in]segfnPhase function index, or an out-of-range value when unknown.
[in]cumWeight accumulated before this run.
[in]segwWeight of this run.
[in]totalTotal sample weight used to normalize both percentages.
Precondition
total is non-zero.
The call executes on the emulator's single owning thread.
Postcondition
One complete line is written to the error stream.
The profile tables are unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 758 of file emu_prof.c.

References internal_prof_name(), priv_emu_io_err_text(), priv_emu_io_errf(), s_percent_scale, and s_prof_n.

Referenced by internal_prof_print_boot_timeline().

◆ internal_prof_report_flamechart()

void internal_prof_report_flamechart ( void )
static

Speedscope export + inclusive/self breakdown + phase timeline (insn mode).

Speedscope export + inclusive/self breakdown + phase timeline (insn mode); this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for prof report flamechart.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 870 of file emu_prof.c.

References internal_prof_accumulate_incl_self(), internal_prof_print_boot_timeline(), internal_prof_print_incl_self_table(), internal_prof_write_html(), internal_prof_write_speedscope(), and RA8_INTERNAL.

Referenced by prof_report().

◆ internal_prof_sample()

void internal_prof_sample ( uint32_t weight)
static

Append the live call chain as one chronological sample of weight insns.

Append the live call chain as one chronological sample of weight insns; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]weightWeight input used by the operation.
Precondition
Arguments satisfy the ranges documented for prof sample.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 255 of file emu_prof.c.

References internal_prof_decimate(), k_prof_max_depth, k_prof_max_samples, RA8_INTERNAL, s_pstk, s_pstk_n, s_samp, s_samp_d, s_samp_n, and s_samp_w.

Referenced by internal_prof_insn_hook().

◆ internal_prof_stack_update()

void internal_prof_stack_update ( uint32_t f)
static

Fold f (PC's owning FUNC index) into the live call chain (push/pop).

Fold f (pc's owning func index) into the live call chain (push/pop); this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]fF input used by the operation.
Precondition
Arguments satisfy the ranges documented for prof stack update.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 281 of file emu_prof.c.

References k_prof_max_depth, RA8_INTERNAL, s_prof_n, s_pstk, and s_pstk_n.

Referenced by internal_prof_insn_hook().

◆ internal_prof_symbol()

bool internal_prof_symbol ( const emu_elf_symbol_t * symbol,
void * ctx )
static

Collect one sized STT_FUNC symbol into the bounded profiler table.

Retains only address bounds and the immutable source-name offset.

Parameters
[in]symbolBounds-checked symbol entry.
[in]ctxUnused callback context.
Returns
Whether symbol iteration should continue.
Return values
trueThe symbol was skipped or collected below the fixed cap.
falseThe fixed profiler-symbol capacity was exhausted.
Precondition
symbol is non-null.
The profiler source remains open.
Postcondition
s_prof_n never exceeds k_prof_max_syms.
Collected entries retain no pointer into source bytes.
Note
Not thread-safe; profiler setup is single-threaded.
Since
0.1.0

Definition at line 142 of file emu_prof.c.

References emu_elf_symbol_t::info, k_elf_st_type_mask, k_prof_max_syms, emu_elf_symbol_t::name_offset, RA8_INTERNAL, s_prof, s_prof_n, emu_elf_symbol_t::size, and emu_elf_symbol_t::value.

Referenced by prof_load().

◆ internal_prof_write_html()

void internal_prof_write_html ( const char * path,
uint64_t total )
static

Write a self-contained, locally-openable HTML flamechart of the samples.

Write a self-contained, locally-openable html flamechart of the samples; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]pathNUL-terminated host path used by the operation.
[in]totalTotal count used to normalize or report the operation.
Precondition
Arguments satisfy the ranges documented for prof write html.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 665 of file emu_prof.c.

References emu_io_txn_t::fd, internal_prof_io_ok(), internal_prof_js_frames(), internal_prof_json_samples(), internal_prof_json_weights(), k_emu_io_ok, priv_emu_io_file_text(), priv_emu_io_filef(), priv_emu_io_txn_abort(), priv_emu_io_txn_begin(), priv_emu_io_txn_commit(), RA8_INTERNAL, s_k_prof_html_head, s_k_prof_html_js, s_prof_n, and s_samp_n.

Referenced by internal_prof_report_flamechart().

◆ internal_prof_write_speedscope()

void internal_prof_write_speedscope ( const char * path)
static

Perform prof write speedscope for the emu prof model.

Perform prof write speedscope for the emu prof model; this step is contained within the emu prof model and uses bounded caller or module-owned storage.

Parameters
[in]pathNUL-terminated host path used by the operation.
Precondition
Arguments satisfy the ranges documented for prof write speedscope.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the emu prof model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 559 of file emu_prof.c.

References emu_io_txn_t::fd, internal_prof_io_ok(), internal_prof_json_frames(), internal_prof_json_samples(), internal_prof_json_weights(), k_emu_io_ok, priv_emu_io_file_text(), priv_emu_io_filef(), priv_emu_io_txn_abort(), priv_emu_io_txn_begin(), priv_emu_io_txn_commit(), RA8_INTERNAL, s_prof_n, s_samp_n, and s_samp_w.

Referenced by internal_prof_report_flamechart().

◆ prof_add()

void prof_add ( uint32_t pc,
double dt )

Attribute dt wall seconds to pc's function (wall-sample mode).

Attribute wall seconds to the function owning pc (wall mode).

Definition at line 210 of file emu_prof.c.

References internal_prof_find(), k_prof_wall, s_prof, s_prof_mode, s_prof_n, and s_prof_total_s.

Referenced by internal_run_loop_prologue().

◆ prof_load()

void prof_load ( const emu_elf_source_t * elf)

Collect + sort FUNC symbols (RA8_EMU_PROFILE only) for PC bucketing.

Reads RA8_EMU_PROFILE to pick the mode; when enabled, walks the ELF .symtab for sized FUNC symbols, records each one's [entry, end) range and name, and sorts by entry address so per-PC lookup can binary-search. With the variable unset the profiler stays off at zero cost.

Parameters
[in]elfOpen ELF source; symbol names are retained as source offsets.
Returns
Nothing.
Precondition
elf is a validated ELF32 source.
The source remains open through the run-end profiler report.
Postcondition
The symbol table is loaded and the mode is latched.
One [profile] injected error sink line is printed when profiling is enabled.
Note
Not thread-safe; call once during setup.
See also
emu_prof_install() Arms the per-instruction hook afterwards.
Since
0.1.0

Definition at line 160 of file emu_prof.c.

References elf_foreach_symbol(), internal_prof_cmp(), internal_prof_symbol(), k_prof_insn, k_prof_off, k_prof_wall, priv_emu_io_errf(), s_prof, s_prof_elf, s_prof_mode, s_prof_n, and strcmp().

Referenced by internal_main_install_run_seams().

◆ prof_report()

void prof_report ( void )

Print the top hot functions (by wall time or instruction count) at run end.

Print the top hot functions (by wall time or instruction count).

Definition at line 892 of file emu_prof.c.

References internal_prof_name(), internal_prof_report_flamechart(), k_prof_insn, k_prof_off, k_prof_top_n, priv_emu_io_err_text(), priv_emu_io_errf(), s_percent_scale, s_prof, s_prof_mode, s_prof_n, s_prof_total_i, s_prof_total_s, and s_samp_n.

Referenced by internal_run_print_stop_summary().

Variable Documentation

◆ s_incl

uint64_t s_incl[k_prof_max_syms]
static

Inclusive weight (report).

Definition at line 99 of file emu_prof.c.

Referenced by internal_prof_accumulate_incl_self(), and internal_prof_print_incl_self_table().

◆ s_k_prof_html_head

const char s_k_prof_html_head[]
static
Initial value:
=
"<!doctype html><html><head><meta charset='utf-8'><title>ra8_emulator profile</title>\n"
"<style>\n"
"body{margin:0;font:12px Menlo,monospace;background:#1e1e1e;color:#ddd}\n"
"#bar{padding:7px 10px;background:#2a2a2a;border-bottom:1px solid #444}\n"
"#bar b{color:#fff}#bar button,#bar input{font:11px monospace;margin-left:10px;"
"background:#3a3a3a;color:#ddd;border:1px solid #555;padding:2px 6px}\n"
"#tip{position:fixed;pointer-events:none;background:#000;color:#fff;padding:5px 7px;"
"border:1px solid #888;display:none;white-space:nowrap;z-index:9;font:11px monospace}\n"
"canvas{display:block;cursor:crosshair}\n"
"</style></head><body>\n"
"<div id='bar'><b id='title'></b><span id='info'></span>"
"<button onclick='resetView()'>Reset zoom</button>"
"search:<input id='q' size='18' oninput='onSearch()'></div>\n"
"<canvas id='fc'></canvas><div id='tip'></div>\n<script>\n"

Definition at line 600 of file emu_prof.c.

Referenced by internal_prof_write_html().

◆ s_k_prof_html_js

const char s_k_prof_html_js[]
static

Definition at line 616 of file emu_prof.c.

Referenced by internal_prof_write_html().

◆ s_nsec_per_sec

const double s_nsec_per_sec = 1.0e9
static

Nanoseconds per second (timespec tv_nsec -> seconds).

Definition at line 28 of file emu_prof.c.

Referenced by board_now_s().

◆ s_percent_scale

const double s_percent_scale = 100.0
static

Fraction-to-per-cent scale (fraction * 100.0 == per-cent).

Definition at line 698 of file emu_prof.c.

Referenced by internal_prof_print_incl_self_table(), internal_prof_print_phase(), and prof_report().

◆ s_prof

◆ s_prof_elf

const emu_elf_source_t* s_prof_elf
static

Borrowed run-long source for names.

Definition at line 63 of file emu_prof.c.

Referenced by internal_prof_name(), and prof_load().

◆ s_prof_mode

prof_mode_t s_prof_mode = k_prof_off
static

Definition at line 67 of file emu_prof.c.

Referenced by emu_prof_install(), emu_prof_mode(), prof_add(), prof_load(), and prof_report().

◆ s_prof_n

◆ s_prof_stop_hit

bool s_prof_stop_hit = false
static

Set when STOP_PC reached.

Definition at line 98 of file emu_prof.c.

Referenced by emu_prof_stop_hit(), and internal_prof_insn_hook().

◆ s_prof_stop_pc

uint32_t s_prof_stop_pc = 0U
static

RA8_EMU_STOP_PC (0=off).

Definition at line 97 of file emu_prof.c.

Referenced by emu_prof_set_stop_pc(), and internal_prof_insn_hook().

◆ s_prof_total_i

uint64_t s_prof_total_i = 0U
static

Definition at line 66 of file emu_prof.c.

Referenced by emu_prof_total_insns(), internal_prof_insn_hook(), and prof_report().

◆ s_prof_total_s

double s_prof_total_s = 0.0
static

Definition at line 65 of file emu_prof.c.

Referenced by prof_add(), and prof_report().

◆ s_pstk

uint16_t s_pstk[k_prof_max_depth]
static

Live chain.

Definition at line 89 of file emu_prof.c.

Referenced by internal_prof_sample(), and internal_prof_stack_update().

◆ s_pstk_n

uint32_t s_pstk_n = 0U
static

Chain depth.

Definition at line 90 of file emu_prof.c.

Referenced by internal_prof_sample(), and internal_prof_stack_update().

◆ s_samp

◆ s_samp_acc

uint64_t s_samp_acc = 0U
static

Insns since last sample.

Definition at line 96 of file emu_prof.c.

Referenced by internal_prof_insn_hook().

◆ s_samp_d

◆ s_samp_every

uint64_t s_samp_every = (uint64_t)k_prof_samp_every
static

Insns per sample (>>x2).

Definition at line 95 of file emu_prof.c.

Referenced by internal_prof_decimate(), and internal_prof_insn_hook().

◆ s_samp_n

◆ s_samp_w

◆ s_self

uint64_t s_self[k_prof_max_syms]
static

Self (leaf) weight.

Definition at line 100 of file emu_prof.c.

Referenced by internal_prof_accumulate_incl_self(), and internal_prof_print_incl_self_table().