📄 proc_misc.c
字号:
static int diskstats_open(struct inode *inode, struct file *file){ return seq_open(file, &diskstats_op);}static const struct file_operations proc_diskstats_operations = { .open = diskstats_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release,};#endif#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 const struct file_operations proc_modules_operations = { .open = modules_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release,};#endif#ifdef CONFIG_SLABINFOstatic int slabinfo_open(struct inode *inode, struct file *file){ return seq_open(file, &slabinfo_op);}static const struct file_operations proc_slabinfo_operations = { .open = slabinfo_open, .read = seq_read, .write = slabinfo_write, .llseek = seq_lseek, .release = seq_release,};#ifdef CONFIG_DEBUG_SLAB_LEAKextern struct seq_operations slabstats_op;static int slabstats_open(struct inode *inode, struct file *file){ unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL); int ret = -ENOMEM; if (n) { ret = seq_open(file, &slabstats_op); if (!ret) { struct seq_file *m = file->private_data; *n = PAGE_SIZE / (2 * sizeof(unsigned long)); m->private = n; n = NULL; } kfree(n); } return ret;}static const struct file_operations proc_slabstats_operations = { .open = slabstats_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release_private,};#endif#endifstatic int show_stat(struct seq_file *p, void *v){ int i; unsigned long jif; cputime64_t user, nice, system, idle, iowait, irq, softirq, steal; cputime64_t guest; u64 sum = 0; struct timespec boottime; unsigned int *per_irq_sum; per_irq_sum = kzalloc(sizeof(unsigned int)*NR_IRQS, GFP_KERNEL); if (!per_irq_sum) return -ENOMEM; user = nice = system = idle = iowait = irq = softirq = steal = cputime64_zero; guest = cputime64_zero; getboottime(&boottime); jif = boottime.tv_sec; for_each_possible_cpu(i) { int j; user = cputime64_add(user, kstat_cpu(i).cpustat.user); nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice); system = cputime64_add(system, kstat_cpu(i).cpustat.system); idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle); iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait); irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq); softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq); steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal); guest = cputime64_add(guest, kstat_cpu(i).cpustat.guest); for (j = 0; j < NR_IRQS; j++) { unsigned int temp = kstat_cpu(i).irqs[j]; sum += temp; per_irq_sum[j] += temp; } } seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", (unsigned long long)cputime64_to_clock_t(user), (unsigned long long)cputime64_to_clock_t(nice), (unsigned long long)cputime64_to_clock_t(system), (unsigned long long)cputime64_to_clock_t(idle), (unsigned long long)cputime64_to_clock_t(iowait), (unsigned long long)cputime64_to_clock_t(irq), (unsigned long long)cputime64_to_clock_t(softirq), (unsigned long long)cputime64_to_clock_t(steal), (unsigned long long)cputime64_to_clock_t(guest)); 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; steal = kstat_cpu(i).cpustat.steal; guest = kstat_cpu(i).cpustat.guest; seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", i, (unsigned long long)cputime64_to_clock_t(user), (unsigned long long)cputime64_to_clock_t(nice), (unsigned long long)cputime64_to_clock_t(system), (unsigned long long)cputime64_to_clock_t(idle), (unsigned long long)cputime64_to_clock_t(iowait), (unsigned long long)cputime64_to_clock_t(irq), (unsigned long long)cputime64_to_clock_t(softirq), (unsigned long long)cputime64_to_clock_t(steal), (unsigned long long)cputime64_to_clock_t(guest)); } seq_printf(p, "intr %llu", (unsigned long long)sum); for (i = 0; i < NR_IRQS; i++) seq_printf(p, " %u", per_irq_sum[i]); 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()); kfree(per_irq_sum); 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 const struct file_operations proc_stat_operations = { .open = stat_open, .read = seq_read, .llseek = seq_lseek, .release = single_release,};/* * /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};static int interrupts_open(struct inode *inode, struct file *filp){ return seq_open(filp, &int_seq_ops);}static const 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_open(struct inode *inode, struct file *filp){ return seq_open(filp, &locks_seq_operations);}static const struct file_operations proc_locks_operations = { .open = locks_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release,};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, 0); } return count;}static const struct file_operations proc_sysrq_trigger_operations = { .write = write_sysrq_trigger,};#endifstruct proc_dir_entry *proc_root_kcore;void create_seq_entry(char *name, mode_t mode, const 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){ 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 {"filesystems", filesystems_read_proc}, {"cmdline", cmdline_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 */#ifdef CONFIG_PRINTK { struct proc_dir_entry *entry; entry = create_proc_entry("kmsg", S_IRUSR, &proc_root); if (entry) entry->proc_fops = &proc_kmsg_operations; }#endif create_seq_entry("locks", 0, &proc_locks_operations); create_seq_entry("devices", 0, &proc_devinfo_operations); create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);#ifdef CONFIG_BLOCK create_seq_entry("partitions", 0, &proc_partitions_operations);#endif create_seq_entry("stat", 0, &proc_stat_operations); create_seq_entry("interrupts", 0, &proc_interrupts_operations);#ifdef CONFIG_SLABINFO create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);#ifdef CONFIG_DEBUG_SLAB_LEAK create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);#endif#endif create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations); create_seq_entry("pagetypeinfo", S_IRUGO, &pagetypeinfo_file_ops); create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations); create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);#ifdef CONFIG_BLOCK create_seq_entry("diskstats", 0, &proc_diskstats_operations);#endif#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_PROC_VMCORE proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL); if (proc_vmcore) proc_vmcore->proc_fops = &proc_vmcore_operations;#endif#ifdef CONFIG_MAGIC_SYSRQ { struct proc_dir_entry *entry; entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL); if (entry) entry->proc_fops = &proc_sysrq_trigger_operations; }#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -