Repository navigation
Expand file tree
/
Copy pathtelemetry.c
More file actions
1420 lines (1193 loc) · 40.5 KB
/
Copy pathtelemetry.c
File metadata and controls
1420 lines (1193 loc) · 40.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2024 Intel Corporation. All rights reserved.
//
// Author: Tobiasz Dryjanski <tobiaszx.dryjanski@intel.com>
#include <zephyr/debug/sparse.h>
#include <zephyr/kernel.h>
#include <sof/audio/buffer.h>
#include <sof/audio/component.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/pipeline.h>
#include <sof/debug/telemetry/telemetry.h>
#include <sof/ipc/common.h>
#include <sof/ipc/topology.h>
#include <sof/lib/cold.h>
#include <sof/lib/cpu-clk-manager.h>
#include <sof/lib/cpu.h>
#include <sof/lib/dai.h>
#include <sof/lib_manager.h>
#include <sof/list.h>
#include <sof/schedule/ll_schedule_domain.h>
#include <rtos/cache.h>
#include <rtos/timer.h>
#include "../../../audio/copier/host_copier.h"
#include <ipc/trace.h>
#include <ipc4/base_fw.h>
#include <ipc4/base_fw_vendor.h>
#include <adsp_debug_window.h>
#include <errno.h>
#include <limits.h>
#include <mem_window.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
LOG_MODULE_DECLARE(ipc, CONFIG_SOF_LOG_LEVEL);
#ifdef CONFIG_SOF_TELEMETRY_EXTENDED_WINDOW
BUILD_ASSERT(sizeof(struct sof_tel_ext_hdr) == 64, "sof_tel_ext_hdr must be 64B");
BUILD_ASSERT(sizeof(struct sof_tel_hot_line0) == 64, "sof_tel_hot_line0 must be 64B");
BUILD_ASSERT(sizeof(struct sof_tel_gw_hot) == 8, "sof_tel_gw_hot must be 8B");
BUILD_ASSERT(sizeof(struct sof_tel_mem_section) == 256, "sof_tel_mem_section must be 256B");
BUILD_ASSERT(sizeof(struct sof_tel_vregion_section) == 512, "sof_tel_vregion_section must be 512B");
BUILD_ASSERT(sizeof(struct sof_tel_ppl_section) == 384, "sof_tel_ppl_section must be 384B");
BUILD_ASSERT(sizeof(struct sof_tel_dai_section) == 256, "sof_tel_dai_section must be 256B");
BUILD_ASSERT(sizeof(struct sof_tel_thread_section) == 832, "sof_tel_thread_section must be 832B");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, ext_hdr) == 0x0100, "ext_hdr offset != 0x0100");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, hot_line0) == 0x0140, "hot_line0 offset != 0x0140");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, gw_hot) == 0x0180, "gw_hot offset != 0x0180");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, mem_sec) == 0x0240, "mem_sec offset != 0x0240");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, vreg_sec) == 0x0340, "vreg_sec offset != 0x0340");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, ppl_sec) == 0x0540, "ppl_sec offset != 0x0540");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, dai_sec) == 0x06C0, "dai_sec offset != 0x06C0");
BUILD_ASSERT(offsetof(struct telemetry_wnd_data, thd_sec) == 0x07C0, "thd_sec offset != 0x07C0");
BUILD_ASSERT(sizeof(struct telemetry_wnd_data) <= 4096, "telemetry_wnd_data exceeds 4KB slot");
#endif
/* 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
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];
#ifdef CONFIG_SOF_TELEMETRY_EXTENDED_WINDOW
#define TELEMETRY_BG_REFRESH_PERIOD K_MSEC(1000)
static struct k_work_delayable telemetry_bg_dwork;
static struct k_spinlock tel_bg_lock;
static bool telemetry_wnd_initialized;
static bool telemetry_ext_ready;
struct telemetry_shared_state {
#ifdef CONFIG_SOF_TELEMETRY_HOT_CACHE_STAGING
struct sof_tel_hot_line0 hot_line0_cache __aligned(64);
struct sof_tel_gw_hot gw_hot_cache[SOF_TEL_MAX_GATEWAYS] __aligned(64);
bool hot_line0_dirty;
bool gw_line1_dirty;
bool gw_line2_dirty;
#endif
uint32_t total_tick_counter;
uint16_t ll_task_count[SOF_TEL_MAX_CORES];
bool bg_paused;
} __aligned(64);
static struct telemetry_shared_state tel_state_storage __aligned(64);
static inline struct telemetry_shared_state *tel_state(void)
{
return (struct telemetry_shared_state *)cache_to_uncache(
(__sparse_force void __sparse_cache *)&tel_state_storage);
}
#ifdef CONFIG_SOF_TELEMETRY_PIPELINES
struct tel_ppl_slot {
bool in_use;
uint16_t pipeline_id;
uint16_t xrun_count;
int32_t last_xrun_bytes;
};
static struct tel_ppl_slot tel_ppl_slots[SOF_TEL_MAX_PIPELINES];
#endif
#ifdef CONFIG_SOF_TELEMETRY_DAIS
struct tel_gw_slot {
bool in_use;
bool is_host;
bool prev_xrun;
const void *key;
struct comp_dev *dev;
struct sof_tel_gw_cold cold;
};
static struct tel_gw_slot tel_gw_slots[SOF_TEL_MAX_GATEWAYS];
#endif
struct telemetry_wnd_data *telemetry_get_wnd_ptr(void)
{
#ifdef CONFIG_INTEL_ADSP_DEBUG_SLOT_MANAGER
return wnd_data;
#else
return (struct telemetry_wnd_data *)ADSP_DW->slots[SOF_DW_TELEMETRY_SLOT];
#endif
}
static inline struct sof_tel_hot_line0 *telemetry_get_hot_line0(void)
{
#ifdef CONFIG_SOF_TELEMETRY_HOT_CACHE_STAGING
struct telemetry_shared_state *st = tel_state();
st->hot_line0_dirty = true;
return &st->hot_line0_cache;
#else
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
return wnd ? &wnd->hot_line0 : NULL;
#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
struct telemetry_shared_state *st = tel_state();
if (slot < 8)
st->gw_line1_dirty = true;
else
st->gw_line2_dirty = true;
return &st->gw_hot_cache[slot];
#else
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
return wnd ? &wnd->gw_hot[slot] : NULL;
#endif
}
#endif
static inline void telemetry_flush_hot_staging(struct telemetry_wnd_data *wnd)
{
#ifdef CONFIG_SOF_TELEMETRY_HOT_CACHE_STAGING
struct telemetry_shared_state *st = tel_state();
if (!wnd)
return;
if (st->hot_line0_dirty) {
wnd->hot_line0 = st->hot_line0_cache;
st->hot_line0_dirty = false;
}
if (st->gw_line1_dirty) {
memcpy(&wnd->gw_hot[0], &st->gw_hot_cache[0], 64);
st->gw_line1_dirty = false;
}
if (st->gw_line2_dirty) {
memcpy(&wnd->gw_hot[8], &st->gw_hot_cache[8], 64);
st->gw_line2_dirty = false;
}
#else
(void)wnd;
#endif
}
static inline void telemetry_seq_begin(struct telemetry_wnd_data *wnd)
{
wnd->ext_hdr.seq |= 1U;
compiler_barrier();
}
static inline void telemetry_seq_end(struct telemetry_wnd_data *wnd)
{
compiler_barrier();
wnd->ext_hdr.timestamp_us = (uint32_t)k_cyc_to_us_floor64(k_cycle_get_64());
compiler_barrier();
wnd->ext_hdr.seq = (wnd->ext_hdr.seq + 1U) & ~1U;
}
#ifdef CONFIG_SOF_TELEMETRY_PIPELINES
static void telemetry_update_pipelines_section(struct telemetry_wnd_data *wnd)
{
struct ipc *ipc = ipc_get();
struct sof_tel_ppl_section *psec = &wnd->ppl_sec;
struct sof_tel_hot_line0 *hot0 = telemetry_get_hot_line0();
struct tel_ppl_slot new_slots[SOF_TEL_MAX_PIPELINES];
struct list_item *clist;
uint16_t ppl_cnt = 0;
uint16_t comp_cnt = 0;
memset(new_slots, 0, sizeof(new_slots));
memset(psec->core_sched, 0, sizeof(psec->core_sched));
for (int c = 0; c < MIN(CONFIG_MAX_CORE_COUNT, SOF_TEL_MAX_CORES); c++) {
uint16_t ll_tasks = tel_state()->ll_task_count[c];
psec->core_sched[c].core_id = (uint8_t)c;
psec->core_sched[c].ll_task_count = ll_tasks;
if (hot0)
hot0->active_ll_tasks[c] = ll_tasks;
#if CONFIG_KCPS_DYNAMIC_CLOCK_CONTROL
if (cpu_is_core_enabled(c))
psec->core_sched[c].active_kcps_budget = (uint32_t)core_kcps_get(c);
#endif
}
if (!ipc) {
memset(tel_ppl_slots, 0, sizeof(tel_ppl_slots));
if (hot0)
memset(hot0->ppl_xrun_count, 0, sizeof(hot0->ppl_xrun_count));
psec->active_ppl_count = 0;
psec->total_comp_count = 0;
wnd->ext_hdr.secs[3].active_mask = 0;
return;
}
/* First pass: collect pipelines */
list_for_item(clist, &ipc->comp_list) {
struct ipc_comp_dev *icd = container_of(clist, struct ipc_comp_dev, list);
if (icd->type == COMP_TYPE_COMPONENT) {
comp_cnt++;
if (icd->cd && icd->core < SOF_TEL_MAX_CORES) {
if (icd->cd->ipc_config.proc_domain == COMP_PROCESSING_DOMAIN_DP)
psec->core_sched[icd->core].dp_task_count++;
}
continue;
}
if (icd->type != COMP_TYPE_PIPELINE || !icd->pipeline)
continue;
if (ppl_cnt >= SOF_TEL_MAX_PIPELINES)
continue;
struct pipeline *p = icd->pipeline;
struct sof_tel_ppl_info *pinfo = &psec->pipelines[ppl_cnt];
uint16_t pid = (uint16_t)p->pipeline_id;
uint16_t prev_xruns = 0;
int32_t prev_xrun_bytes = 0;
for (int s = 0; s < SOF_TEL_MAX_PIPELINES; s++) {
if (tel_ppl_slots[s].in_use && tel_ppl_slots[s].pipeline_id == pid) {
prev_xruns = tel_ppl_slots[s].xrun_count;
prev_xrun_bytes = tel_ppl_slots[s].last_xrun_bytes;
break;
}
}
new_slots[ppl_cnt].in_use = true;
new_slots[ppl_cnt].pipeline_id = pid;
new_slots[ppl_cnt].xrun_count = prev_xruns;
new_slots[ppl_cnt].last_xrun_bytes = p->xrun_bytes ? p->xrun_bytes :
prev_xrun_bytes;
pinfo->pipeline_id = pid;
pinfo->core = (uint8_t)p->core;
pinfo->priority = (uint8_t)p->priority;
pinfo->status = (uint8_t)p->status;
pinfo->comp_count = 0;
pinfo->attributes = (uint16_t)p->attributes;
pinfo->period_us = p->period;
pinfo->sched_comp_id = p->sched_id;
pinfo->last_xrun_bytes = new_slots[ppl_cnt].last_xrun_bytes;
if (p->core < SOF_TEL_MAX_CORES)
psec->core_sched[p->core].active_ppl_count++;
ppl_cnt++;
}
/* Clear unused pipeline slots */
for (uint16_t i = ppl_cnt; i < SOF_TEL_MAX_PIPELINES; i++)
memset(&psec->pipelines[i], 0, sizeof(psec->pipelines[i]));
/* Second pass: count components per pipeline */
list_for_item(clist, &ipc->comp_list) {
struct ipc_comp_dev *icd = container_of(clist, struct ipc_comp_dev, list);
if (icd->type != COMP_TYPE_COMPONENT || !icd->cd)
continue;
uint32_t pid = dev_comp_pipe_id(icd->cd);
for (uint16_t i = 0; i < ppl_cnt; i++) {
if (psec->pipelines[i].pipeline_id == (uint16_t)pid) {
psec->pipelines[i].comp_count++;
break;
}
}
}
memcpy(tel_ppl_slots, new_slots, sizeof(tel_ppl_slots));
if (hot0) {
for (uint16_t i = 0; i < SOF_TEL_MAX_PIPELINES; i++)
hot0->ppl_xrun_count[i] = tel_ppl_slots[i].in_use ?
tel_ppl_slots[i].xrun_count : 0;
}
for (int c = 0; c < MIN(CONFIG_MAX_CORE_COUNT, SOF_TEL_MAX_CORES); c++) {
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;
telemetry_perf_period_sum[c] = 0;
telemetry_perf_period_cnt[c] = 0;
memset(&telemetry_perf_queue[c], 0, sizeof(telemetry_perf_queue[c]));
}
}
psec->active_ppl_count = ppl_cnt;
psec->total_comp_count = comp_cnt;
wnd->ext_hdr.secs[3].active_mask = ppl_cnt;
}
void telemetry_notify_pipeline_change(void)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
if (!telemetry_ext_ready || !wnd)
return;
telemetry_seq_begin(wnd);
telemetry_update_pipelines_section(wnd);
telemetry_flush_hot_staging(wnd);
telemetry_seq_end(wnd);
}
void telemetry_notify_pipeline_xrun(struct pipeline *p, int32_t bytes)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
uint16_t pid;
int slot = -1;
int free_slot = -1;
if (!telemetry_ext_ready || !wnd || !p)
return;
pid = (uint16_t)p->pipeline_id;
struct sof_tel_hot_line0 *hot0 = telemetry_get_hot_line0();
if (hot0)
hot0->total_xrun_events++;
for (int i = 0; i < SOF_TEL_MAX_PIPELINES; i++) {
if (tel_ppl_slots[i].in_use && tel_ppl_slots[i].pipeline_id == pid) {
slot = i;
break;
}
if (!tel_ppl_slots[i].in_use && free_slot < 0)
free_slot = i;
}
if (slot < 0 && free_slot >= 0) {
slot = free_slot;
tel_ppl_slots[slot].in_use = true;
tel_ppl_slots[slot].pipeline_id = pid;
tel_ppl_slots[slot].xrun_count = 0;
}
if (slot >= 0) {
tel_ppl_slots[slot].xrun_count++;
tel_ppl_slots[slot].last_xrun_bytes = bytes;
if (hot0)
hot0->ppl_xrun_count[slot] = tel_ppl_slots[slot].xrun_count;
telemetry_seq_begin(wnd);
wnd->ppl_sec.pipelines[slot].pipeline_id = pid;
wnd->ppl_sec.pipelines[slot].last_xrun_bytes = bytes;
telemetry_seq_end(wnd);
}
}
void telemetry_notify_dp_deadline_miss(int core)
{
if (!telemetry_ext_ready || core < 0 || core >= SOF_TEL_MAX_CORES)
return;
struct sof_tel_hot_line0 *hot0 = telemetry_get_hot_line0();
if (hot0)
hot0->dp_deadline_misses[core]++;
}
#endif /* CONFIG_SOF_TELEMETRY_PIPELINES */
#ifdef CONFIG_SOF_TELEMETRY_DAIS
static uint8_t map_sof_dai_type(uint32_t type)
{
switch (type) {
case SOF_DAI_INTEL_SSP:
return SOF_TEL_GW_SSP;
case SOF_DAI_INTEL_DMIC:
return SOF_TEL_GW_DMIC;
case SOF_DAI_INTEL_ALH:
return SOF_TEL_GW_ALH;
case SOF_DAI_INTEL_HDA:
return SOF_TEL_GW_HDA;
default:
return SOF_TEL_GW_OTHER;
}
}
static void telemetry_fill_dai_cold(struct sof_tel_gw_cold *cold,
struct dai_data *dd, struct comp_dev *dev)
{
memset(cold, 0, sizeof(*cold));
cold->gw_type = map_sof_dai_type(dd->ipc_config.type);
cold->index = (uint8_t)dd->ipc_config.dai_index;
cold->direction = dev ? (uint8_t)dev->direction : (uint8_t)dd->ipc_config.direction;
cold->dma_chan = (int8_t)dd->chan_index;
cold->comp_id = dev ? (uint32_t)dev_comp_id(dev) : 0;
cold->state = dev ? (uint8_t)dev->state : 0;
cold->group_id = dd->group ? (uint8_t)dd->group->group_id : 0;
cold->period_bytes = (uint16_t)MIN(dd->period_bytes, UINT16_MAX);
cold->dma_buf_size = dd->dma_buffer ?
(uint32_t)audio_stream_get_size(&dd->dma_buffer->stream) :
dd->ipc_config.dma_buffer_size;
}
static void telemetry_fill_host_cold(struct sof_tel_gw_cold *cold,
struct host_data *hd, struct comp_dev *dev)
{
memset(cold, 0, sizeof(*cold));
cold->gw_type = SOF_TEL_GW_HOST_DMA;
cold->index = (uint8_t)MAX(hd->chan_index, 0);
cold->direction = dev ? (uint8_t)dev->direction : 0;
cold->dma_chan = (int8_t)hd->chan_index;
cold->comp_id = dev ? (uint32_t)dev_comp_id(dev) : 0;
cold->state = dev ? (uint8_t)dev->state : 0;
cold->group_id = 0;
cold->period_bytes = (uint16_t)MIN(hd->period_bytes, UINT16_MAX);
cold->dma_buf_size = hd->dma_buffer ?
(uint32_t)audio_stream_get_size(&hd->dma_buffer->stream) :
hd->host_size;
}
static void telemetry_sync_gw_cold_slot(struct telemetry_wnd_data *wnd, int slot)
{
uint16_t mask = 0;
if (!telemetry_ext_ready || !wnd || slot < 0 || slot >= SOF_TEL_MAX_GATEWAYS)
return;
struct sof_tel_gw_cold *cold = &wnd->dai_sec.gateways[slot];
struct tel_gw_slot *s = &tel_gw_slots[slot];
if (s->in_use) {
if (s->dev)
s->cold.state = (uint8_t)s->dev->state;
*cold = s->cold;
} else {
memset(cold, 0, sizeof(*cold));
}
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use)
mask |= BIT(i);
}
wnd->ext_hdr.secs[4].active_mask = mask;
}
int telemetry_register_dai(struct dai_data *dd, struct comp_dev *dev)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
struct sof_tel_gw_cold cold;
int free_slot = -1;
int slot = -1;
if (!dd)
return -EINVAL;
telemetry_fill_dai_cold(&cold, dd, dev);
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && !tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == dd) {
slot = i;
break;
}
if (!tel_gw_slots[i].in_use && free_slot < 0)
free_slot = i;
}
if (slot < 0) {
if (free_slot < 0)
return -ENOMEM;
slot = free_slot;
tel_gw_slots[slot].in_use = true;
tel_gw_slots[slot].is_host = false;
tel_gw_slots[slot].prev_xrun = false;
tel_gw_slots[slot].key = dd;
}
tel_gw_slots[slot].dev = dev;
tel_gw_slots[slot].cold = cold;
if (telemetry_ext_ready && wnd) {
telemetry_seq_begin(wnd);
telemetry_sync_gw_cold_slot(wnd, slot);
telemetry_seq_end(wnd);
}
return slot;
}
void telemetry_unregister_dai(struct dai_data *dd)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && !tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == dd) {
struct sof_tel_gw_hot *hot = telemetry_get_gw_hot(i);
tel_gw_slots[i].in_use = false;
tel_gw_slots[i].prev_xrun = false;
tel_gw_slots[i].key = NULL;
tel_gw_slots[i].dev = NULL;
memset(&tel_gw_slots[i].cold, 0, sizeof(tel_gw_slots[i].cold));
if (hot)
memset(hot, 0, sizeof(*hot));
if (telemetry_ext_ready && wnd) {
telemetry_seq_begin(wnd);
telemetry_sync_gw_cold_slot(wnd, i);
telemetry_flush_hot_staging(wnd);
telemetry_seq_end(wnd);
}
break;
}
}
}
void telemetry_update_dai_hot(struct dai_data *dd, struct comp_dev *dev)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
struct sof_tel_gw_cold cold;
uint32_t avail = 0;
bool has_avail = false;
if (!dd)
return;
if (dd->dma_buffer) {
avail = audio_stream_get_avail_bytes(&dd->dma_buffer->stream);
has_avail = true;
}
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && !tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == dd) {
struct comp_dev *cdev = dev ? dev : tel_gw_slots[i].dev;
struct sof_tel_gw_hot *hot = telemetry_get_gw_hot(i);
if (dev)
tel_gw_slots[i].dev = dev;
telemetry_fill_dai_cold(&cold, dd, cdev);
if (!dev && !dd->dma_buffer)
cold.state = COMP_STATE_READY;
else if (dd->dma_buffer && cold.state == COMP_STATE_READY)
cold.state = COMP_STATE_PREPARE;
if (memcmp(&tel_gw_slots[i].cold, &cold, sizeof(cold)) != 0) {
tel_gw_slots[i].cold = cold;
if (telemetry_ext_ready && wnd) {
telemetry_seq_begin(wnd);
wnd->dai_sec.gateways[i] = cold;
telemetry_seq_end(wnd);
}
}
if (!hot)
break;
hot->total_kb_or_frames = (uint32_t)(dd->total_data_processed >> 10);
if (has_avail)
hot->dma_buf_avail_bytes = (uint16_t)MIN(avail, UINT16_MAX);
else if (!dd->dma_buffer)
hot->dma_buf_avail_bytes = 0;
if (dd->xrun && !tel_gw_slots[i].prev_xrun)
hot->xrun_or_skip_count++;
tel_gw_slots[i].prev_xrun = dd->xrun;
break;
}
}
}
int telemetry_register_host(struct host_data *hd, struct comp_dev *dev)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
struct sof_tel_gw_cold cold;
int free_slot = -1;
int slot = -1;
if (!hd)
return -EINVAL;
telemetry_fill_host_cold(&cold, hd, dev);
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == hd) {
slot = i;
break;
}
if (!tel_gw_slots[i].in_use && free_slot < 0)
free_slot = i;
}
if (slot < 0) {
if (free_slot < 0)
return -ENOMEM;
slot = free_slot;
tel_gw_slots[slot].in_use = true;
tel_gw_slots[slot].is_host = true;
tel_gw_slots[slot].prev_xrun = false;
tel_gw_slots[slot].key = hd;
}
tel_gw_slots[slot].dev = dev;
tel_gw_slots[slot].cold = cold;
if (telemetry_ext_ready && wnd) {
telemetry_seq_begin(wnd);
telemetry_sync_gw_cold_slot(wnd, slot);
telemetry_seq_end(wnd);
}
return slot;
}
void telemetry_unregister_host(struct host_data *hd)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == hd) {
struct sof_tel_gw_hot *hot = telemetry_get_gw_hot(i);
tel_gw_slots[i].in_use = false;
tel_gw_slots[i].prev_xrun = false;
tel_gw_slots[i].key = NULL;
tel_gw_slots[i].dev = NULL;
memset(&tel_gw_slots[i].cold, 0, sizeof(tel_gw_slots[i].cold));
if (hot)
memset(hot, 0, sizeof(*hot));
if (telemetry_ext_ready && wnd) {
telemetry_seq_begin(wnd);
telemetry_sync_gw_cold_slot(wnd, i);
telemetry_flush_hot_staging(wnd);
telemetry_seq_end(wnd);
}
break;
}
}
}
void telemetry_update_host_hot(struct host_data *hd, struct comp_dev *dev)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
struct sof_tel_gw_cold cold;
uint32_t avail = 0;
bool has_avail = false;
if (!hd)
return;
if (hd->dma_buffer) {
avail = audio_stream_get_avail_bytes(&hd->dma_buffer->stream);
has_avail = true;
}
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == hd) {
struct comp_dev *cdev = dev ? dev : tel_gw_slots[i].dev;
struct sof_tel_gw_hot *hot = telemetry_get_gw_hot(i);
if (dev)
tel_gw_slots[i].dev = dev;
telemetry_fill_host_cold(&cold, hd, cdev);
if (!dev && !hd->dma_buffer)
cold.state = COMP_STATE_READY;
else if (hd->dma_buffer && cold.state == COMP_STATE_READY)
cold.state = COMP_STATE_PREPARE;
if (memcmp(&tel_gw_slots[i].cold, &cold, sizeof(cold)) != 0) {
tel_gw_slots[i].cold = cold;
if (telemetry_ext_ready && wnd) {
telemetry_seq_begin(wnd);
wnd->dai_sec.gateways[i] = cold;
telemetry_seq_end(wnd);
}
}
if (!hot)
break;
hot->total_kb_or_frames = (uint32_t)(hd->total_data_processed >> 10);
if (has_avail)
hot->dma_buf_avail_bytes = (uint16_t)MIN(avail, UINT16_MAX);
else if (!hd->dma_buffer)
hot->dma_buf_avail_bytes = 0;
break;
}
}
}
void telemetry_notify_host_skip(struct host_data *hd)
{
if (!hd)
return;
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++) {
if (tel_gw_slots[i].in_use && tel_gw_slots[i].is_host &&
tel_gw_slots[i].key == hd) {
struct sof_tel_gw_hot *hot = telemetry_get_gw_hot(i);
if (hot)
hot->xrun_or_skip_count++;
break;
}
}
}
#endif /* CONFIG_SOF_TELEMETRY_DAIS */
#ifdef CONFIG_SOF_TELEMETRY_MEMORY
static struct sof_tel_mem_section tel_mem_staging;
#endif
#ifdef CONFIG_SOF_TELEMETRY_VREGION
static struct sof_tel_vregion_section tel_vreg_staging;
void telemetry_notify_vregion_change(void)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
if (!telemetry_ext_ready || !wnd || k_is_in_isr())
return;
memset(&tel_vreg_staging, 0, sizeof(tel_vreg_staging));
telemetry_collect_vpage_stats(&tel_vreg_staging);
telemetry_collect_vregion_stats(&tel_vreg_staging);
telemetry_seq_begin(wnd);
wnd->vreg_sec = tel_vreg_staging;
wnd->ext_hdr.secs[2].active_mask = tel_vreg_staging.active_vregion_count;
telemetry_seq_end(wnd);
}
#endif
#ifdef CONFIG_SOF_TELEMETRY_THREADS
extern int z_impl_k_thread_stack_space_get(const struct k_thread *thread, size_t *unused_ptr);
static struct sof_tel_thread_section tel_thd_staging;
static uint32_t tel_thd_sample_seq;
#ifdef CONFIG_THREAD_RUNTIME_STATS
static uint64_t tel_prev_core_active[SOF_TEL_MAX_CORES];
static uint64_t tel_prev_core_all[SOF_TEL_MAX_CORES];
static uint32_t tel_last_core_delta_all[SOF_TEL_MAX_CORES];
static struct {
const struct k_thread *tid;
uint64_t cycles;
bool valid;
} tel_prev_thread_cycles[SOF_TEL_MAX_THREADS];
#endif
struct tel_thread_collect_ctx {
struct sof_tel_thread_section *tsec;
#ifdef CONFIG_THREAD_RUNTIME_STATS
const struct k_thread *threads[SOF_TEL_MAX_THREADS];
uint64_t exec_cycles[SOF_TEL_MAX_THREADS];
bool has_stats[SOF_TEL_MAX_THREADS];
#endif
};
static struct tel_thread_collect_ctx tel_thd_collect_ctx;
static void telemetry_thread_cb(const struct k_thread *cthread, void *user_data)
{
struct tel_thread_collect_ctx *ctx = user_data;
struct sof_tel_thread_section *tsec = ctx->tsec;
struct k_thread *thread = (struct k_thread *)cthread;
uint16_t idx = tsec->thread_count;
if (idx >= SOF_TEL_MAX_THREADS) {
tsec->dropped_threads++;
return;
}
struct sof_tel_thread_info *ti = &tsec->threads[idx];
const char *name = k_thread_name_get((k_tid_t)thread);
memset(ti, 0, sizeof(*ti));
ti->thread_id = (uint32_t)POINTER_TO_UINT(thread);
#ifdef CONFIG_SMP
ti->core = thread->base.cpu;
if (thread->base.is_idle)
ti->flags |= SOF_TEL_THD_FLG_IDLE;
#endif
ti->prio = thread->base.prio;
ti->state = thread->base.thread_state;
#ifdef CONFIG_USERSPACE
if (thread->base.user_options & K_USER)
ti->flags |= SOF_TEL_THD_FLG_USER;
#endif
if (name) {
if (strncmp(name, "ll_thread", 9) == 0)
ti->flags |= SOF_TEL_THD_FLG_LL;
else if (strncmp(name, "DP_", 3) == 0 || strncmp(name, "edf_work", 8) == 0)
ti->flags |= SOF_TEL_THD_FLG_DP;
else if (strncmp(name, "idle", 4) == 0)
ti->flags |= SOF_TEL_THD_FLG_IDLE;
}
#ifdef CONFIG_THREAD_STACK_INFO
if (thread->stack_info.size > 0) {
size_t unused = 0;
/*
* 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.
*/
bool scan = !(ti->flags & SOF_TEL_THD_FLG_USER) ||
IS_ENABLED(CONFIG_XTENSA_MMU_FLUSH_AUTOREFILL_DTLBS_ON_SWAP);
ti->stack_size_chunks = (uint16_t)(thread->stack_info.size >> 6);
if (scan && z_impl_k_thread_stack_space_get(thread, &unused) == 0 &&
unused <= thread->stack_info.size) {
ti->stack_usage = (uint8_t)((255ULL * (thread->stack_info.size - unused)) /
thread->stack_info.size);
}
}
#endif
#ifdef CONFIG_THREAD_RUNTIME_STATS
k_thread_runtime_stats_t rt_stats;
ctx->threads[idx] = cthread;
if (k_thread_runtime_stats_get(thread, &rt_stats) == 0) {
ctx->exec_cycles[idx] = rt_stats.execution_cycles;
ctx->has_stats[idx] = true;
ti->total_exec_kcycles = (uint32_t)(rt_stats.execution_cycles >> 10);
}
#endif
if (name && name[0] != '\0')
strncpy(ti->name, name, sizeof(ti->name));
tsec->thread_count++;
}
static void telemetry_collect_threads_section(struct sof_tel_thread_section *tsec)
{
struct tel_thread_collect_ctx *ctx = &tel_thd_collect_ctx;
memset(ctx, 0, sizeof(*ctx));
ctx->tsec = tsec;
memset(tsec, 0, sizeof(*tsec));
#ifdef CONFIG_THREAD_RUNTIME_STATS
for (int c = 0; c < MIN(CONFIG_MAX_CORE_COUNT, SOF_TEL_MAX_CORES); c++) {
k_thread_runtime_stats_t core_stats;
if (!cpu_is_core_enabled(c)) {
tsec->core_load[c] = 0;
tel_last_core_delta_all[c] = 0;
continue;
}
if (k_thread_runtime_stats_cpu_get(c, &core_stats) == 0) {
uint32_t act = (uint32_t)(core_stats.total_cycles - tel_prev_core_active[c]);
uint32_t all = (uint32_t)(core_stats.execution_cycles - tel_prev_core_all[c]);
tel_prev_core_active[c] = core_stats.total_cycles;
tel_prev_core_all[c] = core_stats.execution_cycles;
tel_last_core_delta_all[c] = all;
tsec->core_load[c] = all ? (uint8_t)MIN(255ULL, (255ULL * act) / all) : 0;
}
}
#endif
k_thread_foreach_unlocked(telemetry_thread_cb, ctx);
tsec->sample_seq = ++tel_thd_sample_seq;
#ifdef CONFIG_THREAD_RUNTIME_STATS
for (uint16_t idx = 0; idx < tsec->thread_count; idx++) {
struct sof_tel_thread_info *ti = &tsec->threads[idx];
uint32_t delta_cyc = 0;
if (!ctx->has_stats[idx])
continue;
for (int i = 0; i < SOF_TEL_MAX_THREADS; i++) {
if (tel_prev_thread_cycles[i].valid &&
tel_prev_thread_cycles[i].tid == ctx->threads[idx]) {
delta_cyc = (uint32_t)(ctx->exec_cycles[idx] -
tel_prev_thread_cycles[i].cycles);
break;
}
}
uint8_t c = ti->core < SOF_TEL_MAX_CORES ? ti->core : 0;
if (tel_last_core_delta_all[c] > 0 && delta_cyc <= tel_last_core_delta_all[c]) {
ti->cpu_load = (uint8_t)((255ULL * delta_cyc) /
tel_last_core_delta_all[c]);
}
}
for (uint16_t idx = 0; idx < tsec->thread_count; idx++) {
tel_prev_thread_cycles[idx].tid = ctx->threads[idx];
tel_prev_thread_cycles[idx].cycles = ctx->exec_cycles[idx];
tel_prev_thread_cycles[idx].valid = ctx->has_stats[idx];
}
for (uint16_t idx = tsec->thread_count; idx < SOF_TEL_MAX_THREADS; idx++) {
tel_prev_thread_cycles[idx].tid = NULL;
tel_prev_thread_cycles[idx].cycles = 0;
tel_prev_thread_cycles[idx].valid = false;
}
#endif
}
#endif /* CONFIG_SOF_TELEMETRY_THREADS */
void telemetry_refresh_cold_sections(void)
{
struct telemetry_wnd_data *wnd = telemetry_get_wnd_ptr();
if (!telemetry_ext_ready || !wnd || k_is_in_isr())
return;
#ifdef CONFIG_SOF_TELEMETRY_MEMORY
memset(&tel_mem_staging, 0, sizeof(tel_mem_staging));
telemetry_collect_mem_stats(&tel_mem_staging);
#endif
#ifdef CONFIG_SOF_TELEMETRY_VREGION
memset(&tel_vreg_staging, 0, sizeof(tel_vreg_staging));
telemetry_collect_vpage_stats(&tel_vreg_staging);
telemetry_collect_vregion_stats(&tel_vreg_staging);
#endif
#ifdef CONFIG_SOF_TELEMETRY_THREADS
telemetry_collect_threads_section(&tel_thd_staging);
#endif
telemetry_seq_begin(wnd);
#ifdef CONFIG_SOF_TELEMETRY_MEMORY
wnd->mem_sec = tel_mem_staging;
wnd->ext_hdr.secs[1].active_mask = SOF_TEL_MAX_HEAPS;
#endif
#ifdef CONFIG_SOF_TELEMETRY_VREGION
wnd->vreg_sec = tel_vreg_staging;
wnd->ext_hdr.secs[2].active_mask = tel_vreg_staging.active_vregion_count;
#endif
#ifdef CONFIG_SOF_TELEMETRY_PIPELINES
telemetry_update_pipelines_section(wnd);
#endif
#ifdef CONFIG_SOF_TELEMETRY_DAIS
for (int i = 0; i < SOF_TEL_MAX_GATEWAYS; i++)
telemetry_sync_gw_cold_slot(wnd, i);
#endif
#ifdef CONFIG_SOF_TELEMETRY_THREADS
wnd->thd_sec = tel_thd_staging;
wnd->ext_hdr.secs[5].active_mask = tel_thd_staging.thread_count;
#endif
telemetry_flush_hot_staging(wnd);
telemetry_seq_end(wnd);
}
static void telemetry_bg_reschedule(k_timeout_t delay)
{
k_spinlock_key_t key = k_spin_lock(&tel_bg_lock);