diff --git a/app/ll_userspace_overlay.conf b/app/ll_userspace_overlay.conf index a2f7ef54292f..1633f9ff1edb 100644 --- a/app/ll_userspace_overlay.conf +++ b/app/ll_userspace_overlay.conf @@ -33,7 +33,8 @@ CONFIG_MAX_THREAD_BYTES=4 # store execution is enabled. Disable it for now until rootcause is found. CONFIG_COLD_STORE_EXECUTE_DEBUG=n -# telemetry not yet user-space compatible +# telemetry performance measurements not yet user-space compatible, +# the systick, memory and thread telemetry windows work CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS=n CONFIG_SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS=n diff --git a/src/include/sof/debug/telemetry/telemetry.h b/src/include/sof/debug/telemetry/telemetry.h index 86f45d5b9bd7..301a5b7458b7 100644 --- a/src/include/sof/debug/telemetry/telemetry.h +++ b/src/include/sof/debug/telemetry/telemetry.h @@ -340,13 +340,25 @@ struct telemetry_perf_queue { size_t sum; /**< Running cumulative sum of valid measurements */ }; +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_TELEMETRY) /** - * \brief Update periodic telemetry metrics with elapsed core clock cycles. + * \brief Mark the start of an LL scheduler tick on the current core. * \ingroup telemetry_perf_api - * @param begin_ccount Cycle count snapshot at start of measurement interval. - * @param current_ccount Cycle count snapshot at end of measurement interval. + * + * A system call, so that the LL user thread of CONFIG_SOF_USERSPACE_LL can + * report its ticks: the timestamps and the telemetry window are kernel-only. */ -void telemetry_update(uint32_t begin_ccount, uint32_t current_ccount); +__syscall void telemetry_ll_tick_start(void); + +/** + * \brief Mark the end of an LL scheduler tick and update the systick telemetry + * of the current core. + * \ingroup telemetry_perf_api + */ +__syscall void telemetry_ll_tick_end(void); + +#include +#endif #ifdef CONFIG_INTEL_ADSP_DEBUG_SLOT_MANAGER /** @@ -375,6 +387,18 @@ static inline void telemetry_collect_fast_get_stats(struct sof_tel_fast_get_info static inline void telemetry_notify_mem_alloc_fail(uint16_t heap_type) { (void)heap_type; } #endif +#if defined(CONFIG_SOF_TELEMETRY_MEMORY) && defined(CONFIG_SOF_USERSPACE_LL) +/** + * \brief Copy the LL user heap counters for telemetry_collect_mem_stats(). + * + * Must be called in a system call of an LL or IPC user thread: kernel threads + * must not access the LL user memory. + */ +void telemetry_sample_ll_user_heap(void); +#else +static inline void telemetry_sample_ll_user_heap(void) {} +#endif + #ifdef CONFIG_SOF_TELEMETRY_VREGION void telemetry_collect_vpage_stats(struct sof_tel_vregion_section *vsec); void telemetry_collect_vregion_stats(struct sof_tel_vregion_section *vsec); diff --git a/src/kernel/debug/debug_stream/debug_stream_thread_info.c b/src/kernel/debug/debug_stream/debug_stream_thread_info.c index 5c0d1bd7ffb1..5936524c3bab 100644 --- a/src/kernel/debug/debug_stream/debug_stream_thread_info.c +++ b/src/kernel/debug/debug_stream/debug_stream_thread_info.c @@ -152,6 +152,17 @@ static uint8_t thread_info_stack_level(struct k_thread *thread, const char *name size_t stack_size, stack_unused; int ret; +#ifdef CONFIG_USERSPACE + /* + * Reading a user stack from this kernel thread leaves kernel-ring + * TLB entries that make the window handlers of that thread fault + * and hang the DSP, unless Zephyr flushes them on domain switch. + */ + if ((thread->base.user_options & K_USER) && + !IS_ENABLED(CONFIG_XTENSA_MMU_FLUSH_AUTOREFILL_DTLBS_ON_SWAP)) + return 0; +#endif + stack_size = thread->stack_info.size; ret = k_thread_stack_space_get(thread, &stack_unused); if (ret) { diff --git a/src/kernel/debug/telemetry/Kconfig b/src/kernel/debug/telemetry/Kconfig index ced6729d3860..f6d8e55bf047 100644 --- a/src/kernel/debug/telemetry/Kconfig +++ b/src/kernel/debug/telemetry/Kconfig @@ -3,7 +3,6 @@ config SOF_TELEMETRY bool "enable telemetry" default n - depends on !SOF_USERSPACE_LL help Enables telemetry. Enables performance measurements and debug utilities that use memory window 2 (debug window) as interface. Measurements include @@ -13,6 +12,8 @@ config SOF_TELEMETRY config SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS bool "enable performance measurements" default n + # component measurements are updated from the LL user thread + depends on !SOF_USERSPACE_LL help Enables performance measurements. Requires ADSP_MW interface. Each created component can have its performance checked by measuring execution time of copy function. @@ -21,6 +22,8 @@ config SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS config SOF_TELEMETRY_IO_PERFORMANCE_MEASUREMENTS bool "enable I/O performance measurements" + # I/O counters are updated from the LL and IPC user threads + depends on !SOF_USERSPACE_LL help Enables IO performance measurements. Each data interface will have its data throughput measured (IPC/IDC and GPIO will measure number of messages/state changes). @@ -68,6 +71,9 @@ config SOF_TELEMETRY_VREGION config SOF_TELEMETRY_PIPELINES bool "Pipeline and LL/DP scheduler telemetry" depends on SOF_TELEMETRY_EXTENDED_WINDOW + # updated from the LL and IPC user threads, and walks user-writable + # pipeline lists + depends on !SOF_USERSPACE_LL default y help Exports active pipeline states, periods, priorities, XRUN counts, @@ -76,6 +82,8 @@ config SOF_TELEMETRY_PIPELINES config SOF_TELEMETRY_DAIS bool "DAI and Host DMA gateway telemetry" depends on SOF_TELEMETRY_EXTENDED_WINDOW + # updated from the LL and IPC user threads + depends on !SOF_USERSPACE_LL default y help Exports active DAI and Host DMA gateway state, DMA channel, buffer diff --git a/src/kernel/debug/telemetry/telemetry.c b/src/kernel/debug/telemetry/telemetry.c index 08408c05b53b..6557cb7fab47 100644 --- a/src/kernel/debug/telemetry/telemetry.c +++ b/src/kernel/debug/telemetry/telemetry.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "../../../audio/copier/host_copier.h" @@ -62,15 +63,14 @@ BUILD_ASSERT(sizeof(struct telemetry_wnd_data) <= 4096, "telemetry_wnd_data exce /* Systick variables, one set per core */ static int telemetry_systick_counter[CONFIG_MAX_CORE_COUNT]; +static uint32_t telemetry_ll_tick_begin[CONFIG_MAX_CORE_COUNT]; #ifdef CONFIG_INTEL_ADSP_DEBUG_SLOT_MANAGER static struct telemetry_wnd_data *wnd_data; #endif -#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS static int telemetry_perf_period_sum[CONFIG_MAX_CORE_COUNT]; static int telemetry_perf_period_cnt[CONFIG_MAX_CORE_COUNT]; static struct telemetry_perf_queue telemetry_perf_queue[CONFIG_MAX_CORE_COUNT]; -#endif #ifdef CONFIG_SOF_TELEMETRY_EXTENDED_WINDOW #define TELEMETRY_BG_REFRESH_PERIOD K_MSEC(1000) @@ -146,6 +146,7 @@ static inline struct sof_tel_hot_line0 *telemetry_get_hot_line0(void) #endif } +#ifdef CONFIG_SOF_TELEMETRY_DAIS static inline struct sof_tel_gw_hot *telemetry_get_gw_hot(int slot) { #ifdef CONFIG_SOF_TELEMETRY_HOT_CACHE_STAGING @@ -162,6 +163,7 @@ static inline struct sof_tel_gw_hot *telemetry_get_gw_hot(int slot) return wnd ? &wnd->gw_hot[slot] : NULL; #endif } +#endif static inline void telemetry_flush_hot_staging(struct telemetry_wnd_data *wnd) { @@ -324,11 +326,9 @@ static void telemetry_update_pipelines_section(struct telemetry_wnd_data *wnd) if (psec->core_sched[c].ll_task_count == 0) { wnd->system_tick_info[c].last_time_elapsed = 0; wnd->system_tick_info[c].avg_utilization = 0; -#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS telemetry_perf_period_sum[c] = 0; telemetry_perf_period_cnt[c] = 0; memset(&telemetry_perf_queue[c], 0, sizeof(telemetry_perf_queue[c])); -#endif } } @@ -356,12 +356,10 @@ void telemetry_notify_ll_task_count(int core, unsigned int n_tasks) if (count == 0) { wnd->system_tick_info[core].last_time_elapsed = 0; wnd->system_tick_info[core].avg_utilization = 0; -#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS telemetry_perf_period_sum[core] = 0; telemetry_perf_period_cnt[core] = 0; memset(&telemetry_perf_queue[core], 0, sizeof(telemetry_perf_queue[core])); -#endif } telemetry_flush_hot_staging(wnd); telemetry_seq_end(wnd); @@ -1091,7 +1089,6 @@ static void telemetry_ext_init(struct telemetry_wnd_data *wnd) } #endif /* CONFIG_SOF_TELEMETRY_EXTENDED_WINDOW */ -#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS static void telemetry_perf_queue_append(struct telemetry_perf_queue *q, size_t element) { if (!q->full) { @@ -1123,7 +1120,6 @@ static size_t telemetry_perf_queue_avg(struct telemetry_perf_queue *q) return 0; return q->sum / q->size; } -#endif #ifdef CONFIG_INTEL_ADSP_DEBUG_SLOT_MANAGER struct system_tick_info *telemetry_get_systick_info_ptr(void) @@ -1189,7 +1185,7 @@ __cold int telemetry_init(void) return 0; } -void telemetry_update(uint32_t begin_stamp, uint32_t current_stamp) +static void telemetry_update(uint32_t begin_stamp, uint32_t current_stamp) { int prid = cpu_get_id(); uint32_t elapsed = current_stamp - begin_stamp; @@ -1213,7 +1209,6 @@ void telemetry_update(uint32_t begin_stamp, uint32_t current_stamp) MAX(elapsed, systick_info[prid].max_time_elapsed); systick_info[prid].last_ccount = current_stamp; -#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS const size_t measured_systick = elapsed; if (telemetry_systick_counter[prid] > 2) { @@ -1248,7 +1243,6 @@ void telemetry_update(uint32_t begin_stamp, uint32_t current_stamp) if ((telemetry_systick_counter[prid] & 0x1fff) == 0) systick_info[prid].peak_utilization_8k = 0; } -#endif #ifdef CONFIG_SOF_TELEMETRY_EXTENDED_WINDOW if (telemetry_ext_ready) { @@ -1272,24 +1266,58 @@ void telemetry_update(uint32_t begin_stamp, uint32_t current_stamp) tel_state()->ll_task_count[prid] == 0) { systick_info[prid].last_time_elapsed = 0; systick_info[prid].avg_utilization = 0; -#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS telemetry_perf_period_sum[prid] = 0; telemetry_perf_period_cnt[prid] = 0; memset(&telemetry_perf_queue[prid], 0, sizeof(telemetry_perf_queue[prid])); -#endif } #endif telemetry_flush_hot_staging(wnd_data); ticks = ++tel_state()->total_tick_counter; - if ((ticks & 0x3ff) == 0) + if ((ticks & 0x3ff) == 0) { + /* in a system call of the LL user thread with SOF_USERSPACE_LL */ + telemetry_sample_ll_user_heap(); k_work_reschedule(&telemetry_bg_dwork, K_NO_WAIT); + } } #endif } +void z_impl_telemetry_ll_tick_start(void) +{ + telemetry_ll_tick_begin[cpu_get_id()] = (uint32_t)telemetry_timestamp(); +} + +void z_impl_telemetry_ll_tick_end(void) +{ + const uint32_t current_stamp = (uint32_t)telemetry_timestamp(); + + telemetry_update(telemetry_ll_tick_begin[cpu_get_id()], current_stamp); +} + +#ifdef CONFIG_USERSPACE +#include +#include + +void z_vrfy_telemetry_ll_tick_start(void) +{ + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_LL); + + z_impl_telemetry_ll_tick_start(); +} +#include + +void z_vrfy_telemetry_ll_tick_end(void) +{ + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_LL); + + z_impl_telemetry_ll_tick_end(); +} +#include +#endif + /* * Register the telemetry debug window slot before notify_host_boot_complete * (POST_KERNEL, 0) sends IPC4_FW_READY, then refresh all cold sections once diff --git a/src/schedule/zephyr_ll.c b/src/schedule/zephyr_ll.c index 95f4cd17620d..4f8408c094a2 100644 --- a/src/schedule/zephyr_ll.c +++ b/src/schedule/zephyr_ll.c @@ -309,13 +309,11 @@ static void zephyr_ll_run(void *data) static void schedule_ll_callback(void *data) { #ifdef CONFIG_SOF_TELEMETRY - const uint32_t begin_stamp = (uint32_t)telemetry_timestamp(); + telemetry_ll_tick_start(); #endif zephyr_ll_run(data); #ifdef CONFIG_SOF_TELEMETRY - const uint32_t current_stamp = (uint32_t)telemetry_timestamp(); - - telemetry_update(begin_stamp, current_stamp); + telemetry_ll_tick_end(); #endif } diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 095856795841..f54e27d38f35 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -638,6 +638,7 @@ endif() zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/lib/fast-get.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/ipc/ipc_reply.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/schedule/dp_schedule.h) +zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/debug/telemetry/telemetry.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/schedule/ll_schedule_domain.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/ipc4/handler.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/ipc/msg.h) diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 28b84a53ec1d..b593e2462af5 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -1064,15 +1064,23 @@ static void fill_one_heap_telemetry(struct sof_tel_heap_info *dst, struct k_heap #ifdef CONFIG_SOF_USERSPACE_LL /* * The LL user heap metadata is user-writable, so do not parse it: report the - * counters that the user-mode allocator keeps. + * counters that the user-mode allocator keeps. Those are in LL user memory, + * which kernel threads such as the telemetry work must not access: that + * leaves kernel-ring DTLB entries, on which the user threads of the same core + * then fault. So read a copy that the LL user thread makes in a system call. */ +static struct zephyr_ll_user_heap_stats ll_user_heap_stats_copy; + +void telemetry_sample_ll_user_heap(void) +{ + zephyr_ll_user_heap_stats_get(&ll_user_heap_stats_copy); +} + static void fill_ll_user_heap_telemetry(struct sof_tel_heap_info *dst, uint16_t fail_cnt) { - struct zephyr_ll_user_heap_stats stats; + struct zephyr_ll_user_heap_stats stats = ll_user_heap_stats_copy; size_t total = zephyr_ll_user_heap_size(); - zephyr_ll_user_heap_stats_get(&stats); - dst->heap_type = SOF_TEL_HEAP_LL_USER; dst->alloc_fail_count = fail_cnt + stats.fail_count; dst->total_bytes = (uint32_t)total;