<commit_before>/*
 * sysfs.c - sysfs support
 *
 * (C) 2006-2007 <NAME> <<EMAIL>>
 *
 * This code is licenced under the GPL.
 */

#include <linux/kernel.h>
#include <linux/cpuidle.h>
#include <linux/sysfs.h>
#include <linux/cpu.h>

#include "cpuidle.h"

static unsigned int sysfs_switch;
static int __init cpuidle_sysfs_setup(char *unused)
{
	sysfs_switch = 1;
	return 1;
}
__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);

static ssize_t show_available_governors(struct sys_device *dev,
		struct sysdev_attribute *attr, char *buf)
{
	ssize_t i = 0;
	struct cpuidle_governor *tmp;

	mutex_lock(&cpuidle_lock);
	list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
		if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
			goto out;
		i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
	}

out:
	i+= sprintf(&buf[i], "\n");
	mutex_unlock(&cpuidle_lock);
	return i;
}

static ssize_t show_current_driver(struct sys_device *dev,
		struct sysdev_attribute *attr, char *buf)
{
	ssize_t ret;

	spin_lock(&cpuidle_driver_lock);
	if (cpuidle_curr_driver)
		ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
	else
		ret = sprintf(buf, "none\n");
	spin_unlock(&cpuidle_driver_lock);

	return ret;
}

static ssize_t show_current_governor(struct sys_device *dev,
			struct sysdev_attribute *attr, char *buf)
{
	ssize_t ret;

	mutex_lock(&cpuidle_lock);
	if (cpuidle_curr_governor)
		ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
	else
		ret = sprintf(buf, "none\n");
	mutex_unlock(&cpuidle_lock);

	return ret;
}

static ssize_t store_current_governor(struct sys_device *dev,
	struct sysdev_attribute *attr,
	const char *buf, size_t count)
{
	char gov_name[CPUIDLE_NAME_LEN];
	int ret = -EINVAL;
	size_t len = count;
	struct cpuidle_governor *gov;

	if (!len || len >= sizeof(gov_name))
		return -EINVAL;

	memcpy(gov_name, buf, len);
	gov_name[len] = '\0';
	if (gov_name[len - 1] == '\n')
		gov_name[--len] = '\0';

	mutex_lock(&cpuidle_lock);

	list_for_each_entry(gov, &cpuidle_governors, governor_list) {
		if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
			ret = cpuidle_switch_governor(gov);
			break;
		}
	}

	mutex_unlock(&cpuidle_lock);

	if (ret)
		return ret;
	else
		return count;
}

static SYSDEV_ATTR(current_driver, 0444, show_current_driver, NULL);
static SYSDEV_ATTR(current_governor_ro, 0444, show_current_governor, NULL);

static struct attribute *cpuclass_default_attrs[] = {
	&attr_current_driver.attr,
	&attr_current_governor_ro.attr,
	NULL
};

static SYSDEV_ATTR(available_governors, 0444, show_available_governors, NULL);
static SYSDEV_ATTR(current_governor, 0644, show_current_governor,
	store_current_governor);

static struct attribute *cpuclass_switch_attrs[] = {
	&attr_available_governors.attr,
	&attr_current_driver.attr,
	&attr_current_governor.attr,
	NULL
};

static struct attribute_group cpuclass_attr_group = {
	.attrs = cpuclass_default_attrs,
	.name = "cpuidle",
};

/**
 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
 */
int cpuidle_add_class_sysfs(struct sysdev_class *cls)
{
	if (sysfs_switch)
		cpuclass_attr_group.attrs = cpuclass_switch_attrs;
<commit_msg>cpuidle: make sysfs attributes sysdev class attributes<commit_after>/*
 * sysfs.c - sysfs support
 *
 * (C) 2006-2007 <NAME> <<EMAIL>>
 *
 * This code is licenced under the GPL.
 */

#include <linux/kernel.h>
#include <linux/cpuidle.h>
#include <linux/sysfs.h>
#include <linux/cpu.h>

#include "cpuidle.h"

static unsigned int sysfs_switch;
static int __init cpuidle_sysfs_setup(char *unused)
{
	sysfs_switch = 1;
	return 1;
}
__setup("cpuidle_sysfs_switch", cpuidle_sysfs_setup);

static ssize_t show_available_governors(struct sysdev_class *class,
					char *buf)
{
	ssize_t i = 0;
	struct cpuidle_governor *tmp;

	mutex_lock(&cpuidle_lock);
	list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
		if (i >= (ssize_t) ((PAGE_SIZE/sizeof(char)) - CPUIDLE_NAME_LEN - 2))
			goto out;
		i += scnprintf(&buf[i], CPUIDLE_NAME_LEN, "%s ", tmp->name);
	}

out:
	i+= sprintf(&buf[i], "\n");
	mutex_unlock(&cpuidle_lock);
	return i;
}

static ssize_t show_current_driver(struct sysdev_class *class,
				   char *buf)
{
	ssize_t ret;

	spin_lock(&cpuidle_driver_lock);
	if (cpuidle_curr_driver)
		ret = sprintf(buf, "%s\n", cpuidle_curr_driver->name);
	else
		ret = sprintf(buf, "none\n");
	spin_unlock(&cpuidle_driver_lock);

	return ret;
}

static ssize_t show_current_governor(struct sysdev_class *class,
				     char *buf)
{
	ssize_t ret;

	mutex_lock(&cpuidle_lock);
	if (cpuidle_curr_governor)
		ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
	else
		ret = sprintf(buf, "none\n");
	mutex_unlock(&cpuidle_lock);

	return ret;
}

static ssize_t store_current_governor(struct sysdev_class *class,
				      const char *buf, size_t count)
{
	char gov_name[CPUIDLE_NAME_LEN];
	int ret = -EINVAL;
	size_t len = count;
	struct cpuidle_governor *gov;

	if (!len || len >= sizeof(gov_name))
		return -EINVAL;

	memcpy(gov_name, buf, len);
	gov_name[len] = '\0';
	if (gov_name[len - 1] == '\n')
		gov_name[--len] = '\0';

	mutex_lock(&cpuidle_lock);

	list_for_each_entry(gov, &cpuidle_governors, governor_list) {
		if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
			ret = cpuidle_switch_governor(gov);
			break;
		}
	}

	mutex_unlock(&cpuidle_lock);

	if (ret)
		return ret;
	else
		return count;
}

static SYSDEV_CLASS_ATTR(current_driver, 0444, show_current_driver, NULL);
static SYSDEV_CLASS_ATTR(current_governor_ro, 0444, show_current_governor,
			 NULL);

static struct attribute *cpuclass_default_attrs[] = {
	&attr_current_driver.attr,
	&attr_current_governor_ro.attr,
	NULL
};

static SYSDEV_CLASS_ATTR(available_governors, 0444, show_available_governors,
			 NULL);
static SYSDEV_CLASS_ATTR(current_governor, 0644, show_current_governor,
			 store_current_governor);

static struct attribute *cpuclass_switch_attrs[] = {
	&attr_available_governors.attr,
	&attr_current_driver.attr,
	&attr_current_governor.attr,
	NULL
};

static struct attribute_group cpuclass_attr_group = {
	.attrs = cpuclass_default_attrs,
	.name = "cpuidle",
};

/**
 * cpuidle_add_class_sysfs - add CPU global sysfs attributes
 */
int cpuidle_add_class_sysfs(struct sysdev_class *cls)
{
	if (sysfs_switch)
		cpuclass_attr_group.attrs = cpuclass_switch_attrs;

{'fraction_non_alphanumeric': 0.12634069400630915, 'fraction_numerical': 0.009779179810725552, 'mean_word_length': 5.379275653923541, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, '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': '4002397', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2484, 'n_words': 494}