MOS Source Code
Loading...
Searching...
No Matches
profiling.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#include <mos/mos_global.h>
3
4#if MOS_CONFIG(MOS_PROFILING)
7#include "mos/misc/setup.h"
8#include "mos/syslog/printk.h"
9
12#include <mos_stdio.h>
13#include <mos_stdlib.h>
14#include <mos_string.h>
15#endif
16
17#if MOS_CONFIG(MOS_PROFILING)
18#define PROFILER_HEADER "\nname,start_time,end_time,total_time\n"
19#define PROFILER_LINE "%s,%llu,%llu,%llu"
20
21static console_t *profile_console = NULL;
22static spinlock_t profile_lock = SPINLOCK_INIT;
23static char profile_buffer[MOS_PRINTK_BUFFER_SIZE] = { 0 };
24static char name_buffer[MOS_PRINTK_BUFFER_SIZE] = { 0 };
25
26static bool profile_output_console(const char *console)
27{
28 profile_console = console_get(console);
29 MOS_ASSERT(profile_console);
30 console_write(profile_console, PROFILER_HEADER, strlen(PROFILER_HEADER));
31 return true;
32}
33
34MOS_SETUP("profile_console", profile_output_console);
35
36void profile_leave(const pf_point_t start, const char *fmt, ...)
37{
38 const u64 end = platform_get_timestamp();
39
40 if (unlikely(!profile_console))
41 return;
42
43 const u64 total = end - start;
44
45 spinlock_acquire(&profile_lock);
46 va_list args;
47 va_start(args, fmt);
48 vsnprintf(name_buffer, sizeof(name_buffer), fmt, args);
49 va_end(args);
50
51 const size_t len = snprintf(profile_buffer, sizeof(profile_buffer), PROFILER_LINE "\n", name_buffer, start, end, total);
52 console_write(profile_console, profile_buffer, len);
53 spinlock_release(&profile_lock);
54}
55
56#endif
#define MOS_ASSERT(cond)
Definition assert.h:14
u32 const char * fmt
Definition assert.h:36
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
char args[3][16]
Definition avr_io.c:16
size_t console_write(console_t *con, const char *data, size_t size)
Definition console.c:124
console_t * console_get(const char *name)
Definition console.c:101
MOSAPI int vsnprintf(char *__restrict buf, size_t size, const char *__restrict format, va_list args)
#define unlikely(x)
Definition mos_global.h:40
int snprintf(char *__restrict str, size_t size, const char *__restrict format,...)
Definition mos_stdio.c:16
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
u64 pf_point_t
Definition profiling.h:8
#define profile_leave(p,...)
Definition profiling.h:35
#define MOS_SETUP(_param, _fn)
Definition setup.h:35
#define spinlock_acquire(lock)
Definition spinlock.h:61
#define SPINLOCK_INIT
Definition spinlock.h:28
#define spinlock_release(lock)
Definition spinlock.h:62
unsigned long long u64
Definition types.h:23
u64 platform_get_timestamp()