1// SPDX-License-Identifier: GPL-3.0-or-later
2#include <mos/mos_global.h>
3
4#if MOS_CONFIG(MOS_PROFILING)
5#include "mos/device/console.h"
6#include "mos/misc/profiling.h"
7#include "mos/misc/setup.h"
8#include "mos/syslog/printk.h"
9
10#include <mos/lib/structures/hashmap.h>
11#include <mos/lib/structures/hashmap_common.h>
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
57