-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
90 lines (75 loc) · 2.13 KB
/
main.c
File metadata and controls
90 lines (75 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <linux/printk.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/init.h>
#include <linux/string.h>
#include "proccon.h"
/*
### How this module is going to work
This module would make two entries in /sys/kernel interface.
- /sys/kernel/freqcon/max
- /sys/kernel/freqcon/min
These two entries are going to be used to control
the CPU's max/minimal frequency at all cores.
It's important to note that the making of this module is solely for educational purposes.
*/
static int max = 0;
static int min = 0;
static struct kobject *freq_kobj;
void check_val()
{
if(max > 0 && min > 0)
limit_freq(max, min);
}
static ssize_t min_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", min);
}
static ssize_t min_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%d", &min);
check_val();
return count;
}
static ssize_t max_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", max);
}
static ssize_t max_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%d", &max);
check_val();
return count;
}
static struct kobj_attribute max_attr = __ATTR(max, 0660, max_show, max_store);
static struct kobj_attribute min_attr = __ATTR(min, 0660, min_show, min_store);
static struct attribute *attrs[] = {
&max_attr.attr,
&min_attr.attr,
NULL
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
static int __init freqcon_init(void)
{
int ret;
freq_kobj = kobject_create_and_add("freqcon", kernel_kobj);
if(!freq_kobj)
return -ENOMEM;
ret = sysfs_create_group(freq_kobj, &attr_group);
if(ret)
kobject_put(freq_kobj);
printk(KERN_INFO "Added kobject freqcon\n");
return 0;
}
static void __exit freqcon_exit(void)
{
kobject_put(freq_kobj);
}
module_init(freqcon_init);
module_exit(freqcon_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Eric Kim");
MODULE_DESCRIPTION("A CPU max/min frequency controller module");