zephyr/subsys/tracing/tracing_tracking.c
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00

168 lines
3.9 KiB
C

/*
* Copyright (c) 2021 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/init.h>
#include <zephyr/spinlock.h>
#include <zephyr/sys/slist.h>
#include <zephyr/tracing/tracking.h>
struct k_timer *_track_list_k_timer;
struct k_spinlock _track_list_k_timer_lock;
struct k_mem_slab *_track_list_k_mem_slab;
struct k_spinlock _track_list_k_mem_slab_lock;
struct k_sem *_track_list_k_sem;
struct k_spinlock _track_list_k_sem_lock;
struct k_mutex *_track_list_k_mutex;
struct k_spinlock _track_list_k_mutex_lock;
struct k_stack *_track_list_k_stack;
struct k_spinlock _track_list_k_stack_lock;
struct k_msgq *_track_list_k_msgq;
struct k_spinlock _track_list_k_msgq_lock;
struct k_mbox *_track_list_k_mbox;
struct k_spinlock _track_list_k_mbox_lock;
#ifdef CONFIG_PIPES
struct k_pipe *_track_list_k_pipe;
struct k_spinlock _track_list_k_pipe_lock;
#endif
struct k_queue *_track_list_k_queue;
struct k_spinlock _track_list_k_queue_lock;
#ifdef CONFIG_EVENTS
struct k_event *_track_list_k_event;
struct k_spinlock _track_list_k_event_lock;
#endif
#define SYS_TRACK_LIST_PREPEND(list, obj) \
do { \
k_spinlock_key_t key = k_spin_lock(&list ## _lock); \
obj->_obj_track_next = list; \
list = obj; \
k_spin_unlock(&list ## _lock, key); \
} while (false)
#define SYS_TRACK_STATIC_INIT(type, ...) \
do { \
STRUCT_SECTION_FOREACH(type, obj) \
_SYS_PORT_TRACKING_OBJ_INIT(type)(obj, ##__VA_ARGS__); \
} while (false)
void sys_track_k_timer_init(struct k_timer *timer)
{
SYS_PORT_TRACING_TYPE_MASK(k_timer,
SYS_TRACK_LIST_PREPEND(_track_list_k_timer, timer));
}
void sys_track_k_mem_slab_init(struct k_mem_slab *slab)
{
SYS_PORT_TRACING_TYPE_MASK(k_mem_slab,
SYS_TRACK_LIST_PREPEND(_track_list_k_mem_slab, slab));
}
void sys_track_k_sem_init(struct k_sem *sem)
{
if (sem) {
SYS_PORT_TRACING_TYPE_MASK(k_sem,
SYS_TRACK_LIST_PREPEND(_track_list_k_sem, sem));
}
}
void sys_track_k_mutex_init(struct k_mutex *mutex)
{
SYS_PORT_TRACING_TYPE_MASK(k_mutex,
SYS_TRACK_LIST_PREPEND(_track_list_k_mutex, mutex));
}
void sys_track_k_stack_init(struct k_stack *stack)
{
SYS_PORT_TRACING_TYPE_MASK(k_stack,
SYS_TRACK_LIST_PREPEND(_track_list_k_stack, stack));
}
void sys_track_k_msgq_init(struct k_msgq *msgq)
{
SYS_PORT_TRACING_TYPE_MASK(k_msgq,
SYS_TRACK_LIST_PREPEND(_track_list_k_msgq, msgq));
}
void sys_track_k_mbox_init(struct k_mbox *mbox)
{
SYS_PORT_TRACING_TYPE_MASK(k_mbox,
SYS_TRACK_LIST_PREPEND(_track_list_k_mbox, mbox));
}
#ifdef CONFIG_PIPES
void sys_track_k_pipe_init(struct k_pipe *pipe)
{
SYS_PORT_TRACING_TYPE_MASK(k_pipe,
SYS_TRACK_LIST_PREPEND(_track_list_k_pipe, pipe));
}
#endif
void sys_track_k_queue_init(struct k_queue *queue)
{
SYS_PORT_TRACING_TYPE_MASK(k_queue,
SYS_TRACK_LIST_PREPEND(_track_list_k_queue, queue));
}
#ifdef CONFIG_EVENTS
void sys_track_k_event_init(struct k_event *event)
{
SYS_PORT_TRACING_TYPE_MASK(k_event,
SYS_TRACK_LIST_PREPEND(_track_list_k_event, event));
}
#endif
static int sys_track_static_init(void)
{
SYS_PORT_TRACING_TYPE_MASK(k_timer,
SYS_TRACK_STATIC_INIT(k_timer));
SYS_PORT_TRACING_TYPE_MASK(k_mem_slab,
SYS_TRACK_STATIC_INIT(k_mem_slab, 0));
SYS_PORT_TRACING_TYPE_MASK(k_sem,
SYS_TRACK_STATIC_INIT(k_sem, 0));
SYS_PORT_TRACING_TYPE_MASK(k_mutex,
SYS_TRACK_STATIC_INIT(k_mutex, 0));
SYS_PORT_TRACING_TYPE_MASK(k_stack,
SYS_TRACK_STATIC_INIT(k_stack));
SYS_PORT_TRACING_TYPE_MASK(k_msgq,
SYS_TRACK_STATIC_INIT(k_msgq));
SYS_PORT_TRACING_TYPE_MASK(k_mbox,
SYS_TRACK_STATIC_INIT(k_mbox));
#ifdef CONFIG_PIPES
SYS_PORT_TRACING_TYPE_MASK(k_pipe,
SYS_TRACK_STATIC_INIT(k_pipe));
#endif
SYS_PORT_TRACING_TYPE_MASK(k_queue,
SYS_TRACK_STATIC_INIT(k_queue));
#ifdef CONFIG_EVENTS
SYS_PORT_TRACING_TYPE_MASK(k_event,
SYS_TRACK_STATIC_INIT(k_event));
#endif
return 0;
}
SYS_INIT(sys_track_static_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);