| 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.hpp" |
| 6 | #include "mos/misc/profiling.hpp" |
| 7 | #include "mos/misc/setup.hpp" |
| 8 | #include "mos/syslog/printk.hpp" |
| 9 | |
| 10 | #include <mos/lib/structures/hashmap.hpp> |
| 11 | #include <mos/lib/structures/hashmap_common.hpp> |
| 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 | |
| 21 | static console *profile_console = NULL; |
| 22 | static spinlock_t profile_lock static char profile_buffer[MOS_PRINTK_BUFFER_SIZE] = { 0 }; |
| 23 | static char name_buffer[MOS_PRINTK_BUFFER_SIZE] = { 0 }; |
| 24 | |
| 25 | static 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 | |
| 33 | MOS_SETUP("profile_console" , profile_output_console); |
| 34 | |
| 35 | void 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 |
| 56 | |