MOS Source Code
Loading...
Searching...
No Matches
profiling.cpp
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.hpp"
9
12#include <mos_stdio.hpp>
13#include <mos_stdlib.hpp>
14#include <mos_string.hpp>
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 *profile_console = NULL;
22static spinlock_t profile_lock static char profile_buffer[MOS_PRINTK_BUFFER_SIZE] = { 0 };
23static char name_buffer[MOS_PRINTK_BUFFER_SIZE] = { 0 };
24
25static bool profile_output_console(const char *console)
26{
27 profile_console = console_get(console);
28 MOS_ASSERT(profile_console);
29 console_write(profile_console, PROFILER_HEADER, strlen(PROFILER_HEADER));
30 return true;
31}
32
33MOS_SETUP("profile_console", profile_output_console);
34
35void profile_leave(const pf_point_t start, const char *fmt, ...)
36{
37 const u64 end = platform_get_timestamp();
38
39 if (unlikely(!profile_console))
40 return;
41
42 const u64 total = end - start;
43
44 spinlock_acquire(&profile_lock);
45 va_list args;
46 va_start(args, fmt);
47 vsnprintf(name_buffer, sizeof(name_buffer), fmt, args);
48 va_end(args);
49
50 const size_t len = snprintf(profile_buffer, sizeof(profile_buffer), PROFILER_LINE "\n", name_buffer, start, end, total);
51 console_write(profile_console, profile_buffer, len);
52 spinlock_release(&profile_lock);
53}
54
55#endif
#define MOS_ASSERT(cond)
Definition assert.hpp:14
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
char args[3][16]
Definition avr_io.c:16
Console * console_get(const char *name)
Definition console.cpp:117
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.cpp: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.hpp:8
#define profile_leave(p,...)
Definition profiling.hpp:35
#define MOS_SETUP(_param, _fn)
Definition setup.hpp:33
#define spinlock_acquire(lock)
Definition spinlock.hpp:64
#define spinlock_release(lock)
Definition spinlock.hpp:65
unsigned long long u64
Definition types.h:19
u64 platform_get_timestamp()