Skip to content

Instantly share code, notes, and snippets.

@hiboma
Created April 13, 2015 10:14
Show Gist options
  • Select an option

  • Save hiboma/fd6b43c6964aecfc6dcf to your computer and use it in GitHub Desktop.

Select an option

Save hiboma/fd6b43c6964aecfc6dcf to your computer and use it in GitHub Desktop.
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <asm-generic/uaccess.h>
MODULE_AUTHOR("hiroya");
MODULE_DESCRIPTION("debugfs preempt_count");
MODULE_LICENSE("GPL");
static int file_value;
static struct dentry *dentry_file;
static ssize_t debugfs_test_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
char buffer[16];
pid_t id;
struct pid *pid;
struct task_struct *task;
if (count > sizeof(buffer) -1)
return -EINVAL;
if (copy_from_user(buffer, buf, count))
return -EFAULT;
buffer[count] = '\0';
id = simple_strtoul(buffer, NULL, 10);
if (!id)
return -EINVAL;
pid = find_get_pid(id);
if (!pid)
return -ESRCH;
task = get_pid_task(pid, PIDTYPE_PID);
if (!task) {
put_pid(pid);
return -ESRCH;
}
printk(KERN_INFO "[%s] preempt_count %d\n",
task->comm,
task_thread_info(task)->preempt_count);
put_task_struct(task);
put_pid(pid);
return count;
}
static struct file_operations fops = {
.write = debugfs_test_write,
};
static int __init debugfs_init(void)
{
dentry_file = debugfs_create_file("preempt_count", 0644, NULL, &file_value, &fops);
if (!dentry_file) {
printk(KERN_ERR "Failed to debugfs_create_file: preempt_count");
return -ENODEV;
}
return 0;
}
static void __exit debugfs_exit(void)
{
debugfs_remove(dentry_file);
}
module_init(debugfs_init);
module_exit(debugfs_exit);
@hiboma
Copy link
Author

hiboma commented Apr 13, 2015

USAGE

# echo $$ > /sys/kernel/debug/preempt_count
# tail -1 /var/log/messages
Apr 13 08:58:36 vagrant-centos65 kernel: [bash] preempt_count  0
# echo 1 > /sys/kernel/debug/preempt_count 
# tail -1 /var/log/messages
Apr 13 10:17:14 vagrant-centos65 kernel: [init] preempt_count 0

作ってみたけどちゃんとうごいてるのかどうか

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment