<commit_before>/**
 * \file thread.c
 * \author written by <NAME> <EMAIL> (c) 2012
 * \date 2012
 */

/* __LICENSE_HEADER_BEGIN__ */

/*
 * Copyright (C) 2012	<NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 */

/* __LICENSE_HEADER_END__ */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <assert.h>
#include <sched.h>
#include <sys/resource.h>

#include <libdabba/thread.h>
#include <libdabba/macros.h>
#include <dabbad/dabbad.h>
#include <dabba/dabba.h>
#include <dabba/ipc.h>

static struct sched_policy_name_mapping {
	const char key[8];
	int value;
} sched_policy_mapping[] = {
	{
	.key = "rr",.value = SCHED_RR}, {
	.key = "fifo",.value = SCHED_FIFO}, {
	.key = "other",.value = SCHED_OTHER}
};

static struct thread_name_mapping {
	const char key[8];
	int value;
} thread_name_mapping[] = {
	{
	.key = "capture",.value = CAPTURE_THREAD}
};

int sched_policy_value_get(const char *const policy_name)
{
	size_t a;

	assert(policy_name);

	for (a = 0; a < ARRAY_SIZE(sched_policy_mapping) - 1; a++)
		if (!strcmp(policy_name, sched_policy_mapping[a].key))
			break;

	return sched_policy_mapping[a].value;
}

const char *sched_policy_key_get(const int policy_value)
{
	size_t a;

	for (a = 0; a < ARRAY_SIZE(sched_policy_mapping) - 1; a++)
		if (policy_value == sched_policy_mapping[a].value)
			break;

	return sched_policy_mapping[a].key;
}

const char *thread_key_get(const int type)
{
	size_t a, max = ARRAY_SIZE(thread_name_mapping);

	for (a = 0; a < max; a++)
		if (type == thread_name_mapping[a].value)
			break;

	return a < max ? thread_name_mapping[a].key : "unknown";
}

int sched_policy_default_get(void)
{
	return SCHED_OTHER;
}

int sched_prio_default_get(void)
{
	return 0;
}

static void display_thread_list_header(void)
{
	printf("---\n");
	printf("  threads:\n");
}

static void display_thread_list(struct dabba_ipc_msg *const msg)
{
	struct dabba_thread *thread_msg;
	size_t a;

	assert(msg);
	assert(msg->msg_body.elem_nr <= ARRAY_SIZE(msg->msg_body.msg.thread));

	for (a = 0; a < msg->msg_body.elem_nr; a++) {
		thread_msg = &msg->msg_body.msg.thread[a];
		printf("    - id: %" PRIu64 "\n", (uint64_t) thread_msg->id);
		printf("      type: %s\n", thread_key_get(thread_msg->type));
		printf("      scheduling policy: %s\n",
		       sched_policy_key_get(thread_msg->sched_policy));
		printf("      scheduling priority: %i\n",
		       thread_msg->sched_prio);
	}
}

static void prepare_thread_list_query(struct dabba_ipc_msg *msg)
{
	assert(msg);
	msg->mtype = 1;
	msg->msg_body.type = DABBA_THREAD_LIST;
}

static void display_thread_list_msg(struct dabba_ipc_msg *const msg)
{
	assert(msg);

	switch (msg->msg_body.type) {
	case DABBA_THREAD_LIST:
		display_thread_list(msg);
		break;
	default:
		break;
	}
}

/**
 * \brief Request the current supported interface list
 * \param[in]           argc	        Argument counter
 * \param[in]           argv		Argument vector
 * \return 0 on success, else on failure.
 *
 * This function prepares an IPC message to query the supported network
 * interfaces present on the system. Once the message is sent, it waits for the<commit_msg>thread: print thread cpu affinity on 'dabba thread'<commit_after>/**
 * \file thread.c
 * \author written by <NAME> <EMAIL> (c) 2012
 * \date 2012
 */

/* __LICENSE_HEADER_BEGIN__ */

/*
 * Copyright (C) 2012	<NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 */

/* __LICENSE_HEADER_END__ */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <assert.h>
#include <sched.h>
#include <sys/resource.h>

#include <libdabba/thread.h>
#include <libdabba/macros.h>
#include <dabbad/dabbad.h>
#include <dabba/dabba.h>
#include <dabba/ipc.h>

static struct sched_policy_name_mapping {
	const char key[8];
	int value;
} sched_policy_mapping[] = {
	{
	.key = "rr",.value = SCHED_RR}, {
	.key = "fifo",.value = SCHED_FIFO}, {
	.key = "other",.value = SCHED_OTHER}
};

static struct thread_name_mapping {
	const char key[8];
	int value;
} thread_name_mapping[] = {
	{
	.key = "capture",.value = CAPTURE_THREAD}
};

int sched_policy_value_get(const char *const policy_name)
{
	size_t a;

	assert(policy_name);

	for (a = 0; a < ARRAY_SIZE(sched_policy_mapping) - 1; a++)
		if (!strcmp(policy_name, sched_policy_mapping[a].key))
			break;

	return sched_policy_mapping[a].value;
}

const char *sched_policy_key_get(const int policy_value)
{
	size_t a;

	for (a = 0; a < ARRAY_SIZE(sched_policy_mapping) - 1; a++)
		if (policy_value == sched_policy_mapping[a].value)
			break;

	return sched_policy_mapping[a].key;
}

const char *thread_key_get(const int type)
{
	size_t a, max = ARRAY_SIZE(thread_name_mapping);

	for (a = 0; a < max; a++)
		if (type == thread_name_mapping[a].value)
			break;

	return a < max ? thread_name_mapping[a].key : "unknown";
}

int sched_policy_default_get(void)
{
	return SCHED_OTHER;
}

int sched_prio_default_get(void)
{
	return 0;
}

static void display_thread_cpu_affinity(const cpu_set_t * const cpu)
{
      
    	size_t i, j, run = 0;
        
        assert(cpu);
        
	for (i = 0; i < CPU_SETSIZE; i++)
		if (CPU_ISSET(i, cpu)) {
			for (j = i + 1; j < CPU_SETSIZE; j++) {
				if (CPU_ISSET(j, cpu))
					run++;
				else
					break;
			}
                        
			if (!run)
				printf("%zu,", i);
			else if (run == 1) {
				printf("%zu,%zu,", i, i + 1);
				i++;
			} else {
				printf("%zu-%zu,", i, i + run);
				i += run;
			}
		}
}

static void display_thread_list_header(void)
{
	printf("---\n");
	printf("  threads:\n");
}

static void display_thread_list(struct dabba_ipc_msg *const msg)
{
	struct dabba_thread *thread_msg;
	size_t a;

	assert(msg);
	assert(msg->msg_body.elem_nr <= ARRAY_SIZE(msg->msg_body.msg.thread));

	for (a = 0; a < msg->msg_body.elem_nr; a++) {
		thread_msg = &msg->msg_body.msg.thread[a];
		printf("    - id: %" PRIu64 "\n", (uint64_t) thread_msg->id);
		printf("      type: %s\n", thread_key_get(thread_msg->type));
		printf("      scheduling policy: %s\n",
		       sched_policy_key_get(thread_msg->sched_policy));
		printf("      scheduling priority: %i\n",
		       thread_msg->sched_prio);
                printf("      cpu affinity: ");
                display_thread_cpu_affinity(&thread_msg->cpu);
                printf("\n");
	}
}

static void prepare_thread_list_query(struct dabba_ipc_msg *msg)
{
	assert(msg);
	msg->mtype = 1;
	msg->msg_body.type = DABBA_THREAD_LIST;
}

static void display_thread_list_msg(struct dabba_ipc_msg *const msg)
{
	assert(msg);

	switch (msg->msg_body.type) {
	case DABBA_THREAD_LIST:
		display_thread_list(msg);
		break;
	default:
		break;
	}
}

/**
 * \brief Request the current supported interface list
 * \param[in]           argc	        Argument counter
 * \param[in]           argv		Argument vector
 * \return 0 on success, else on failure.
 *
 * This function prepares an IPC message to query the supported network
 * interfaces present on the system. Once the message is sent, it waits for the
{'fraction_non_alphanumeric': 0.12935263850664114, 'fraction_numerical': 0.00897451238482709, 'mean_word_length': 3.695505617977528, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1722405', 'n_tokens_mistral': 3163, 'n_tokens_neox': 2961, 'n_words': 823}