<commit_before>/* profiler.c - Profiler code for flushing data on UART */

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <zephyr.h>
#include <misc/kernel_event_logger.h>
#include <uart.h>

#define PROF_LOG_HEADER "*** [PROF] "

#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT(format, ...)           printf(PROF_LOG_HEADER format, ##__VA_ARGS__)
#else
#include <misc/printk.h>
#define PRINT(format, ...)           printk(PROF_LOG_HEADER format, ##__VA_ARGS__)
#endif

#define PROF_HEADER_SIZE_U8 4
#define PROF_MAX_EVENT_SIZE_U32 4
#define PROFILER_SLEEP_MS 5000
#define PROFILER_SLEEPTICKS (PROFILER_SLEEP_MS * sys_clock_ticks_per_sec / 1000)
/*
 * This flag can be defined to check UART consistency
 * In that case, "profterm" used on the host must be compiled
 * in TESTMODE
 */
#undef PROF_UART_TESTMODE

struct prof_platform_info {
	uint32_t hw_ticks_per_sec;
};

struct prof_platform_info prof_pltfm_info;
static struct device *uart_console_dev;

int prof_initialized;

#define out(c) uart_poll_out(uart_console_dev, (char)c)

#define DLE 16
#define PROFILER_INFO 255 /* Must not overlap with event ID */

void prof_send_platform_info(void)
{
	uint8_t data_length = SIZE32_OF(prof_pltfm_info);
	uint32_t *data = (uint32_t *)(&prof_pltfm_info);
	int key;
	int i;

	key = irq_lock();
	out(DLE);
	out(PROFILER_INFO); /* Event ID */
	out(data_length);
	for (i = 0; i < data_length; i++) {
		out((data[i] & 0x000000ff));
		out(((data[i] & 0x0000ff00) >> 8));
		out(((data[i] & 0x00ff0000) >> 16));
		out(((data[i] & 0xff000000) >> 24));
	}
	irq_unlock(key);
}

#if CONFIG_CONSOLE_HANDLER_SHELL && CONFIG_KERNEL_EVENT_LOGGER_DYNAMIC
/* Profiler shell is only enabled if kernel event logger dynamic
 * enable/disable and console handler shell are enabled
 * Warning: if kernel event logger dynamic is enabled but no shell is available,
 * kernel event logger won't log anything
 */
#define PROFILER_SHELL
#else
#undef PROFILER_SHELL
#endif

#ifdef PROFILER_SHELL

#include "profiler.h"
#include <stdlib.h>
#include <string.h>
#include <misc/shell.h>

/* kernel_event_logger flags. Bit N = enable event ID N, except for task monitor
 * by default all enabled events at build time are logged when profiler enabled
 */
int cfg1 = 0xFFFF;
/* task monitor flags. By default set to build time configuration */
#if defined(CONFIG_TASK_MONITOR_MASK)
int cfg2 = CONFIG_TASK_MONITOR_MASK;
#else
int cfg2;
#endif

/*TODO: could configure flush period as well ? */
int prof_started;

int shell_cmd_prof(int argc, char *argv[])
{
	/*
	 * prof [cmd]
	 *   start
	 *   stop
	 *   cfg id1 id2
	 */
	int len = strlen(argv[1]);

	if (!strncmp(argv[1], "start", len)) {
		PRINT("Start\n");
		sys_k_event_logger_set_mask(cfg1);
#if CONFIG_TASK_MONITOR
		sys_k_event_logger_set_monitor_mask(cfg2);
#endif
		if ((sys_k_event_logger_get_mask() != 0)
#if CONFIG_TASK_MONITOR
		    || (sys_k_event_logger_get_monitor_mask() != 0)
#endif
		   ) {
			prof_started = 1;
			prof_send_platform_info();
		}
	} else if (!strncmp(argv[1], "stop", len)) {
		PRINT("Stop\n");
		sys_k_event_logger_set_mask(0);
#if CONFIG_TASK_MONITOR
		sys_k_event_logger_set_monitor_mask(0);
#endif
		/* Could flush buffer here... but flush may be on-going... */
	} else if (!strncmp(argv[1], "cfg", len)) {
		if (argc >= 4) {
			cfg1 = atoi(argv[2]);
			cfg2 = atoi(argv[3]);
			PRINT("Configure %d %d\n", cfg1, cfg2);
		} else {
#if CONFIG_TASK_MONITOR
			PRINT("%d(%d) %d(%d)\n",
					sys_k_event_logger_get_mask(),
					cfg1,
					sys_k_event_logger_get_monitor_mask(),
					cfg2);
#else
			PRINT("%d(%d)\n",
					sys_k_event_logger_get_mask(),
					cfg1);
#endif
		}
	} else {
		return -EINVAL;
	}

	return 0;
}

struct shell_cmd commands[] = {
	PROF_CMD,
	{ NULL, NULL}
};
#endif /* PROFILER_SHELL */

#undef TIMESTAMP_MODE_RTC<commit_msg>samples/task_profiler: fix #if to #ifdef<commit_after>/* profiler.c - Profiler code for flushing data on UART */

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <zephyr.h>
#include <misc/kernel_event_logger.h>
#include <uart.h>

#define PROF_LOG_HEADER "*** [PROF] "

#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT(format, ...)           printf(PROF_LOG_HEADER format, ##__VA_ARGS__)
#else
#include <misc/printk.h>
#define PRINT(format, ...)           printk(PROF_LOG_HEADER format, ##__VA_ARGS__)
#endif

#define PROF_HEADER_SIZE_U8 4
#define PROF_MAX_EVENT_SIZE_U32 4
#define PROFILER_SLEEP_MS 5000
#define PROFILER_SLEEPTICKS (PROFILER_SLEEP_MS * sys_clock_ticks_per_sec / 1000)
/*
 * This flag can be defined to check UART consistency
 * In that case, "profterm" used on the host must be compiled
 * in TESTMODE
 */
#undef PROF_UART_TESTMODE

struct prof_platform_info {
	uint32_t hw_ticks_per_sec;
};

struct prof_platform_info prof_pltfm_info;
static struct device *uart_console_dev;

int prof_initialized;

#define out(c) uart_poll_out(uart_console_dev, (char)c)

#define DLE 16
#define PROFILER_INFO 255 /* Must not overlap with event ID */

void prof_send_platform_info(void)
{
	uint8_t data_length = SIZE32_OF(prof_pltfm_info);
	uint32_t *data = (uint32_t *)(&prof_pltfm_info);
	int key;
	int i;

	key = irq_lock();
	out(DLE);
	out(PROFILER_INFO); /* Event ID */
	out(data_length);
	for (i = 0; i < data_length; i++) {
		out((data[i] & 0x000000ff));
		out(((data[i] & 0x0000ff00) >> 8));
		out(((data[i] & 0x00ff0000) >> 16));
		out(((data[i] & 0xff000000) >> 24));
	}
	irq_unlock(key);
}

#if defined(CONFIG_CONSOLE_HANDLER_SHELL) && defined(CONFIG_KERNEL_EVENT_LOGGER_DYNAMIC)
/* Profiler shell is only enabled if kernel event logger dynamic
 * enable/disable and console handler shell are enabled
 * Warning: if kernel event logger dynamic is enabled but no shell is available,
 * kernel event logger won't log anything
 */
#define PROFILER_SHELL
#else
#undef PROFILER_SHELL
#endif

#ifdef PROFILER_SHELL

#include "profiler.h"
#include <stdlib.h>
#include <string.h>
#include <misc/shell.h>

/* kernel_event_logger flags. Bit N = enable event ID N, except for task monitor
 * by default all enabled events at build time are logged when profiler enabled
 */
int cfg1 = 0xFFFF;
/* task monitor flags. By default set to build time configuration */
#if defined(CONFIG_TASK_MONITOR_MASK)
int cfg2 = CONFIG_TASK_MONITOR_MASK;
#else
int cfg2;
#endif

/*TODO: could configure flush period as well ? */
int prof_started;

int shell_cmd_prof(int argc, char *argv[])
{
	/*
	 * prof [cmd]
	 *   start
	 *   stop
	 *   cfg id1 id2
	 */
	int len = strlen(argv[1]);

	if (!strncmp(argv[1], "start", len)) {
		PRINT("Start\n");
		sys_k_event_logger_set_mask(cfg1);
#ifdef CONFIG_TASK_MONITOR
		sys_k_event_logger_set_monitor_mask(cfg2);
#endif
		if ((sys_k_event_logger_get_mask() != 0)
#ifdef CONFIG_TASK_MONITOR
		    || (sys_k_event_logger_get_monitor_mask() != 0)
#endif
		   ) {
			prof_started = 1;
			prof_send_platform_info();
		}
	} else if (!strncmp(argv[1], "stop", len)) {
		PRINT("Stop\n");
		sys_k_event_logger_set_mask(0);
#ifdef CONFIG_TASK_MONITOR
		sys_k_event_logger_set_monitor_mask(0);
#endif
		/* Could flush buffer here... but flush may be on-going... */
	} else if (!strncmp(argv[1], "cfg", len)) {
		if (argc >= 4) {
			cfg1 = atoi(argv[2]);
			cfg2 = atoi(argv[3]);
			PRINT("Configure %d %d\n", cfg1, cfg2);
		} else {
#ifdef CONFIG_TASK_MONITOR
			PRINT("%d(%d) %d(%d)\n",
					sys_k_event_logger_get_mask(),
					cfg1,
					sys_k_event_logger_get_monitor_mask(),
					cfg2);
#else
			PRINT("%d(%d)\n",
					sys_k_event_logger_get_mask(),
					cfg1);
#endif
		}
	} else {
		return -EINVAL;
	}

	return 0;
}

struct shell_cmd commands[] = {
	PROF_CMD,
	{ NULL, NULL}
};
#endif /* PROFILER_SHELL */

#undef TIMESTAMP_MODE_RTC
{'fraction_non_alphanumeric': 0.12064859961446876, 'fraction_numerical': 0.021997958952262163, 'mean_word_length': 4.3714981729598055, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5517506', 'n_tokens_mistral': 3485, 'n_tokens_neox': 3211, 'n_words': 877}