-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit_busy.c
120 lines (96 loc) · 2.55 KB
/
jit_busy.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* jit_busy.c -- delay execution, using the "busy waiting" approach
*
* /proc/jit_busy delays a whole second each time one reads a line of text
* and the lines are guaranteed to be 20 bytes each.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h> /* we're using the seq_file interface */
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/sched.h> /* schedule() */
#include <asm/hardirq.h>
int jbusy_nr_lines = 5; /* Temporary fix; determines no. of lines return by `cat` */
int delay = HZ; /* the default delay, expressed in jiffies */
module_param(delay, int, 0);
MODULE_AUTHOR("Salym Senyonga");
MODULE_LICENSE("GPL");
/*
* /proc fs stuff : using seq_file
*/
/* The sequence iteration methods */
static void *jit_seq_start(struct seq_file *s, loff_t *pos)
{
if (*pos >= jbusy_nr_lines)
return NULL;
return jbusy_nr_lines + *pos;
}
static void *jit_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
if (++(*pos) >= jbusy_nr_lines)
return NULL;
return jbusy_nr_lines + *pos;
}
static void jit_seq_stop(struct seq_file *s, void *v)
{
}
static int jit_seq_show(struct seq_file *s, void *v)
{
unsigned long j0, j1; /* jiffies */
j0 = jiffies;
j1 = j0 + delay;
while (time_before(jiffies, j1))
cpu_relax();
j1 = jiffies; /* value after we delayed */
seq_printf(s, "%9li %9li\n", j0, j1);
return 0;
}
/* build up the seq_ops structure */
static struct seq_operations jit_seq_ops = {
.start = jit_seq_start,
.next = jit_seq_next,
.stop = jit_seq_stop,
.show = jit_seq_show
};
/* the open() method that connects the /proc file the the seq_ops */
static int jit_proc_open(struct inode *inode, struct file *file)
{
return seq_open(file, &jit_seq_ops);
}
/* Initialize a fops structure for the /proc file. Required by the seq_file
* interface. We only are concerned about the open() method
*/
static struct file_operations jit_proc_ops = {
.owner = THIS_MODULE,
.open = jit_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};
static void jit_create_proc(void)
{
struct proc_dir_entry *entry;
entry = proc_create_data("jit_busy", 0, NULL, &jit_proc_ops, NULL);
}
static void jit_remove_proc(void)
{
remove_proc_entry("jit_busy", NULL);
}
int __init jit_init(void)
{
jit_create_proc();
return 0; /* success */
}
void __exit jit_cleanup(void)
{
jit_remove_proc();
}
module_init(jit_init);
module_exit(jit_cleanup);