proc_misc.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 645 行 · 第 1/2 页

C
645
字号
	.release	= seq_release,};#ifdef CONFIG_MODULESextern struct seq_operations modules_op;static int modules_open(struct inode *inode, struct file *file){	return seq_open(file, &modules_op);}static struct file_operations proc_modules_operations = {	.open		= modules_open,	.read		= seq_read,	.llseek		= seq_lseek,	.release	= seq_release,};#endifextern struct seq_operations slabinfo_op;extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);static int slabinfo_open(struct inode *inode, struct file *file){	return seq_open(file, &slabinfo_op);}static struct file_operations proc_slabinfo_operations = {	.open		= slabinfo_open,	.read		= seq_read,	.write		= slabinfo_write,	.llseek		= seq_lseek,	.release	= seq_release,};int show_stat(struct seq_file *p, void *v){	int i;	extern unsigned long total_forks;	unsigned long jif;	u64	sum = 0, user = 0, nice = 0, system = 0,		idle = 0, iowait = 0, irq = 0, softirq = 0;	jif = - wall_to_monotonic.tv_sec;	if (wall_to_monotonic.tv_nsec)		--jif;	for_each_cpu(i) {		int j;		user += kstat_cpu(i).cpustat.user;		nice += kstat_cpu(i).cpustat.nice;		system += kstat_cpu(i).cpustat.system;		idle += kstat_cpu(i).cpustat.idle;		iowait += kstat_cpu(i).cpustat.iowait;		irq += kstat_cpu(i).cpustat.irq;		softirq += kstat_cpu(i).cpustat.softirq;		for (j = 0 ; j < NR_IRQS ; j++)			sum += kstat_cpu(i).irqs[j];	}	seq_printf(p, "cpu  %llu %llu %llu %llu %llu %llu %llu\n",		(unsigned long long)jiffies_64_to_clock_t(user),		(unsigned long long)jiffies_64_to_clock_t(nice),		(unsigned long long)jiffies_64_to_clock_t(system),		(unsigned long long)jiffies_64_to_clock_t(idle),		(unsigned long long)jiffies_64_to_clock_t(iowait),		(unsigned long long)jiffies_64_to_clock_t(irq),		(unsigned long long)jiffies_64_to_clock_t(softirq));	for_each_online_cpu(i) {		/* Copy values here to work around gcc-2.95.3, gcc-2.96 */		user = kstat_cpu(i).cpustat.user;		nice = kstat_cpu(i).cpustat.nice;		system = kstat_cpu(i).cpustat.system;		idle = kstat_cpu(i).cpustat.idle;		iowait = kstat_cpu(i).cpustat.iowait;		irq = kstat_cpu(i).cpustat.irq;		softirq = kstat_cpu(i).cpustat.softirq;		seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu\n",			i,			(unsigned long long)jiffies_64_to_clock_t(user),			(unsigned long long)jiffies_64_to_clock_t(nice),			(unsigned long long)jiffies_64_to_clock_t(system),			(unsigned long long)jiffies_64_to_clock_t(idle),			(unsigned long long)jiffies_64_to_clock_t(iowait),			(unsigned long long)jiffies_64_to_clock_t(irq),			(unsigned long long)jiffies_64_to_clock_t(softirq));	}	seq_printf(p, "intr %llu", (unsigned long long)sum);#if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA)	for (i = 0; i < NR_IRQS; i++)		seq_printf(p, " %u", kstat_irqs(i));#endif	seq_printf(p,		"\nctxt %llu\n"		"btime %lu\n"		"processes %lu\n"		"procs_running %lu\n"		"procs_blocked %lu\n",		nr_context_switches(),		(unsigned long)jif,		total_forks,		nr_running(),		nr_iowait());	return 0;}static int stat_open(struct inode *inode, struct file *file){	unsigned size = 4096 * (1 + num_possible_cpus() / 32);	char *buf;	struct seq_file *m;	int res;	/* don't ask for more than the kmalloc() max size, currently 128 KB */	if (size > 128 * 1024)		size = 128 * 1024;	buf = kmalloc(size, GFP_KERNEL);	if (!buf)		return -ENOMEM;	res = single_open(file, show_stat, NULL);	if (!res) {		m = file->private_data;		m->buf = buf;		m->size = size;	} else		kfree(buf);	return res;}static struct file_operations proc_stat_operations = {	.open		= stat_open,	.read		= seq_read,	.llseek		= seq_lseek,	.release	= single_release,};static int devices_read_proc(char *page, char **start, off_t off,				 int count, int *eof, void *data){	int len = get_chrdev_list(page);	len += get_blkdev_list(page+len);	return proc_calc_metrics(page, start, off, count, eof, len);}/* * /proc/interrupts */static void *int_seq_start(struct seq_file *f, loff_t *pos){	return (*pos <= NR_IRQS) ? pos : NULL;}static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos){	(*pos)++;	if (*pos > NR_IRQS)		return NULL;	return pos;}static void int_seq_stop(struct seq_file *f, void *v){	/* Nothing to do */}extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */static struct seq_operations int_seq_ops = {	.start = int_seq_start,	.next  = int_seq_next,	.stop  = int_seq_stop,	.show  = show_interrupts};int interrupts_open(struct inode *inode, struct file *filp){	return seq_open(filp, &int_seq_ops);}static struct file_operations proc_interrupts_operations = {	.open		= interrupts_open,	.read		= seq_read,	.llseek		= seq_lseek,	.release	= seq_release,};static int filesystems_read_proc(char *page, char **start, off_t off,				 int count, int *eof, void *data){	int len = get_filesystem_list(page);	return proc_calc_metrics(page, start, off, count, eof, len);}static int cmdline_read_proc(char *page, char **start, off_t off,				 int count, int *eof, void *data){	int len;	len = sprintf(page, "%s\n", saved_command_line);	return proc_calc_metrics(page, start, off, count, eof, len);}static int locks_read_proc(char *page, char **start, off_t off,				 int count, int *eof, void *data){	int len = get_locks_status(page, start, off, count);	if (len < count)		*eof = 1;	return len;}static int execdomains_read_proc(char *page, char **start, off_t off,				 int count, int *eof, void *data){	int len = get_exec_domain_list(page);	return proc_calc_metrics(page, start, off, count, eof, len);}#ifdef CONFIG_MAGIC_SYSRQ/* * writing 'C' to /proc/sysrq-trigger is like sysrq-C */static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,				   size_t count, loff_t *ppos){	if (count) {		char c;		if (get_user(c, buf))			return -EFAULT;		__handle_sysrq(c, NULL, NULL);	}	return count;}static struct file_operations proc_sysrq_trigger_operations = {	.write		= write_sysrq_trigger,};#endifstruct proc_dir_entry *proc_root_kcore;static void create_seq_entry(char *name, mode_t mode, struct file_operations *f){	struct proc_dir_entry *entry;	entry = create_proc_entry(name, mode, NULL);	if (entry)		entry->proc_fops = f;}void __init proc_misc_init(void){	struct proc_dir_entry *entry;	static struct {		char *name;		int (*read_proc)(char*,char**,off_t,int,int*,void*);	} *p, simple_ones[] = {		{"loadavg",     loadavg_read_proc},		{"uptime",	uptime_read_proc},		{"meminfo",	meminfo_read_proc},		{"version",	version_read_proc},#ifdef CONFIG_PROC_HARDWARE		{"hardware",	hardware_read_proc},#endif#ifdef CONFIG_STRAM_PROC		{"stram",	stram_read_proc},#endif		{"devices",	devices_read_proc},		{"filesystems",	filesystems_read_proc},		{"cmdline",	cmdline_read_proc},		{"locks",	locks_read_proc},		{"execdomains",	execdomains_read_proc},		{NULL,}	};	for (p = simple_ones; p->name; p++)		create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);	proc_symlink("mounts", NULL, "self/mounts");	/* And now for trickier ones */	entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);	if (entry)		entry->proc_fops = &proc_kmsg_operations;	create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);	create_seq_entry("partitions", 0, &proc_partitions_operations);	create_seq_entry("stat", 0, &proc_stat_operations);	create_seq_entry("interrupts", 0, &proc_interrupts_operations);	create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);	create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);	create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);	create_seq_entry("diskstats", 0, &proc_diskstats_operations);#ifdef CONFIG_MODULES	create_seq_entry("modules", 0, &proc_modules_operations);#endif#ifdef CONFIG_SCHEDSTATS	create_seq_entry("schedstat", 0, &proc_schedstat_operations);#endif#ifdef CONFIG_PROC_KCORE	proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);	if (proc_root_kcore) {		proc_root_kcore->proc_fops = &proc_kcore_operations;		proc_root_kcore->size =				(size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;	}#endif#ifdef CONFIG_MAGIC_SYSRQ	entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);	if (entry)		entry->proc_fops = &proc_sysrq_trigger_operations;#endif#ifdef CONFIG_PPC32	{		extern struct file_operations ppc_htab_operations;		entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);		if (entry)			entry->proc_fops = &ppc_htab_operations;	}#endif}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?