1 #include <linux/signal.h>
2 #include <linux/sched.h>
3 #include <linux/interrupt.h>
5 #include <linux/version.h>
6 #include <linux/vmalloc.h>
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/smp.h>
14 #include <asm/system.h>
15 #include <asm/uaccess.h>
17 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
18 #include <linux/wrapper.h>
20 #define NB_CPUS weight (phys_cpu_present_map)
25 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 4, 20)
26 #define iminor(inode) MINOR((inode)->i_rdev)
28 #define iminor(inode) minor((inode)->i_rdev)
31 #define NB_CPUS num_present_cpus ()
35 MODULE_DESCRIPTION ("Idle time collector");
37 /* there are many ways to prevent gcc from complaining about module_param
38 and function pointer vs long, but let's not */
39 static void (*idle_func
) (void);
41 #if LINUX_VERSION_CODE < KERNEL_VERSION (2, 6, 0)
42 MODULE_PARM (idle_func
, "l");
44 module_param (idle_func
, long, 0777);
46 MODULE_PARM_DESC (idle_func
, "address of default idle function");
49 static spinlock_t lock
= SPIN_LOCK_UNLOCKED
;
51 static void (*orig_pm_idle
) (void);
52 static unsigned int itc_major
;
56 struct timeval cumm_sleep_time
;
57 struct timeval sleep_started
;
61 static struct itc global_itc
[NR_CPUS
];
63 /**********************************************************************
67 **********************************************************************/
69 cpeamb (struct timeval
*c
, struct timeval
*a
, struct timeval
*b
)
71 __typeof (c
->tv_sec
) sec
= a
->tv_sec
- b
->tv_sec
;
72 __typeof (c
->tv_usec
) usec
= a
->tv_usec
- b
->tv_usec
;
77 usec
= 1000000 + usec
;
80 if (c
->tv_usec
> 1000000)
83 c
->tv_usec
-= 1000000;
91 /**********************************************************************
95 **********************************************************************/
97 itc_open (struct inode
* inode
, struct file
* file
);
100 itc_release (struct inode
* inode
, struct file
* file
);
103 itc_ioctl (struct inode
* inode
, struct file
* file
,
104 unsigned int cmd
, unsigned long arg
);
107 itc_read (struct file
* file
, char * buf
, size_t count
, loff_t
* ppos
);
109 static struct file_operations itc_fops
=
111 .owner
= THIS_MODULE
,
113 .release
= itc_release
,
121 itc_release (struct inode
* inode
, struct file
* filp
)
127 itc_open (struct inode
* inode
, struct file
* filp
)
130 const struct file_operations
*old_fops
= filp
->f_op
;
131 unsigned int minor
= iminor (inode
);
136 /* old_fops = filp->f_op; */
137 filp
->f_op
= fops_get (&itc_fops
);
143 itc_read (struct file
*file
, char * buf
, size_t count
, loff_t
* ppos
)
146 size_t itemsize
= sizeof (global_itc
[0].cumm_sleep_time
);
149 struct itc
*itc
= &global_itc
[0];
151 /* printk ("itemsize=%d cpus=%d count=%d\n", itemsize, NR_CPUS, count); */
152 if (count
< itemsize
* NB_CPUS
)
155 "attempt to read something funny %d expected %d(%d,%d)\n",
156 count
, itemsize
* NB_CPUS
, itemsize
, NB_CPUS
);
160 spin_lock_irqsave (&lock
, flags
);
161 for (i
= 0; i
< NB_CPUS
; ++i
, ++itc
)
167 do_gettimeofday (&tv
);
168 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
169 itc
->sleep_started
.tv_sec
= tv
.tv_sec
;
170 itc
->sleep_started
.tv_usec
= tv
.tv_usec
;
173 if (copy_to_user (buf
, &itc
->cumm_sleep_time
, itemsize
))
175 printk (KERN_ERR
"failed to write %zu bytes to %p\n", itemsize
, buf
);
183 spin_unlock_irqrestore (&lock
, flags
);
187 /**********************************************************************
191 **********************************************************************/
193 itc_ioctl (struct inode
* inode
, struct file
* filp
,
194 unsigned int cmd
, unsigned long arg
)
199 /**********************************************************************
203 **********************************************************************/
204 #if LINUX_VERSION_CODE > KERNEL_VERSION (2, 6, 0)
209 void default_idle (void);
219 spin_lock_irqsave (&lock
, flags
);
220 itc
= &global_itc
[smp_processor_id ()];
221 do_gettimeofday (&itc
->sleep_started
);
222 spin_unlock_irqrestore (&lock
, flags
);
251 spin_lock_irqsave (&lock
, flags
);
252 do_gettimeofday (&tv
);
253 cpeamb (&itc
->cumm_sleep_time
, &tv
, &itc
->sleep_started
);
255 spin_unlock_irqrestore (&lock
, flags
);
258 /**********************************************************************
262 **********************************************************************/
269 if (!pm_idle
&& !idle_func
)
273 "itc: no idle function\n"
274 "itc: boot kernel with idle=halt option\n"
275 "itc: or specify idle_func (modprobe itc idle_func=<address>)\n");
280 err
= register_chrdev (itc_major
, DEVNAME
, &itc_fops
);
281 if (err
< 0 || ((itc_major
&& err
) || (!itc_major
&& !err
)))
283 printk (KERN_ERR
"itc: register_chrdev failed itc_major=%d err=%d\n",
295 "itc: driver loaded pm_idle=%p default_idle=%p, idle_func=%p\n",
305 orig_pm_idle
= pm_idle
;
310 /**********************************************************************
314 **********************************************************************/
318 printk (KERN_DEBUG
"itc: unloading\n");
320 unregister_chrdev (itc_major
, DEVNAME
);
321 printk (KERN_DEBUG
"itc: unloaded\n");
323 pm_idle
= orig_pm_idle
;