2 * linux/fs/proc/proc_misc.c
4 * linux/fs/proc/array.c
5 * Copyright (C) 1992 by Linus Torvalds
6 * based on ideas by Darren Senn
8 * This used to be the part of array.c. See the rest of history and credits
9 * there. I took this into a separate file and switched the thing to generic
10 * proc_file_inode_operations, leaving in array.c only per-process stuff.
11 * Inumbers allocation made dynamic (via create_proc_entry()). AV, May 1999.
14 * Fulton Green : Encapsulated position metric calculations.
15 * <kernel@FultonGreen.com>
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
24 #include <linux/tty.h>
25 #include <linux/string.h>
26 #include <linux/mman.h>
27 #include <linux/proc_fs.h>
28 #include <linux/ioport.h>
29 #include <linux/config.h>
31 #include <linux/mmzone.h>
32 #include <linux/pagemap.h>
33 #include <linux/swap.h>
34 #include <linux/slab.h>
35 #include <linux/smp.h>
36 #include <linux/signal.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/smp_lock.h>
40 #include <linux/seq_file.h>
41 #include <linux/times.h>
42 #include <linux/profile.h>
43 #include <linux/blkdev.h>
44 #include <linux/hugetlb.h>
45 #include <linux/jiffies.h>
46 #include <linux/sysrq.h>
47 #include <linux/vmalloc.h>
48 #include <linux/crash_dump.h>
49 #include <asm/uaccess.h>
50 #include <asm/pgtable.h>
53 #include <asm/div64.h>
56 #include <linux/vs_cvirt.h>
58 #define LOAD_INT(x) ((x) >> FSHIFT)
59 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
61 * Warning: stuff below (imported functions) assumes that its output will fit
62 * into one page. For some of those functions it may be wrong. Moreover, we
63 * have a way to deal with that gracefully. Right now I used straightforward
64 * wrappers, but this needs further analysis wrt potential overflows.
66 extern int get_hardware_list(char *);
67 extern int get_stram_list(char *);
68 extern int get_filesystem_list(char *);
69 extern int get_exec_domain_list(char *);
70 extern int get_dma_list(char *);
71 extern int get_locks_status (char *, char **, off_t, int);
73 static int proc_calc_metrics(char *page, char **start, off_t off,
74 int count, int *eof, int len)
76 if (len <= off+count) *eof = 1;
79 if (len>count) len = count;
84 static int loadavg_read_proc(char *page, char **start, off_t off,
85 int count, int *eof, void *data)
87 unsigned int running, threads;
91 if (vx_flags(VXF_VIRT_LOAD, 0)) {
92 struct vx_info *vxi = current->vx_info;
94 a = vxi->cvirt.load[0] + (FIXED_1/200);
95 b = vxi->cvirt.load[1] + (FIXED_1/200);
96 c = vxi->cvirt.load[2] + (FIXED_1/200);
98 running = atomic_read(&vxi->cvirt.nr_running);
99 threads = atomic_read(&vxi->cvirt.nr_threads);
101 a = avenrun[0] + (FIXED_1/200);
102 b = avenrun[1] + (FIXED_1/200);
103 c = avenrun[2] + (FIXED_1/200);
105 running = nr_running();
106 threads = nr_threads;
108 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
109 LOAD_INT(a), LOAD_FRAC(a),
110 LOAD_INT(b), LOAD_FRAC(b),
111 LOAD_INT(c), LOAD_FRAC(c),
112 running, threads, last_pid);
113 return proc_calc_metrics(page, start, off, count, eof, len);
116 static int uptime_read_proc(char *page, char **start, off_t off,
117 int count, int *eof, void *data)
119 struct timespec uptime;
120 struct timespec idle;
122 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
124 do_posix_clock_monotonic_gettime(&uptime);
125 cputime_to_timespec(idletime, &idle);
126 if (vx_flags(VXF_VIRT_UPTIME, 0))
127 vx_vsi_uptime(&uptime, &idle);
129 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
130 (unsigned long) uptime.tv_sec,
131 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
132 (unsigned long) idle.tv_sec,
133 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
135 return proc_calc_metrics(page, start, off, count, eof, len);
138 static int meminfo_read_proc(char *page, char **start, off_t off,
139 int count, int *eof, void *data)
143 struct page_state ps;
144 unsigned long inactive;
145 unsigned long active;
147 unsigned long committed;
148 unsigned long allowed;
149 struct vmalloc_info vmi;
153 get_zone_counts(&active, &inactive, &free);
156 * display in kilobytes.
158 #define K(x) ((x) << (PAGE_SHIFT - 10))
161 committed = atomic_read(&vm_committed_space);
162 allowed = ((totalram_pages - hugetlb_total_pages())
163 * sysctl_overcommit_ratio / 100) + total_swap_pages;
165 cached = get_page_cache_size() - total_swapcache_pages - i.bufferram;
166 if (cached < 0 || vx_flags(VXF_VIRT_MEM, 0))
169 get_vmalloc_info(&vmi);
172 * Tagged format, for easy grepping and expansion.
175 "MemTotal: %8lu kB\n"
179 "SwapCached: %8lu kB\n"
181 "Inactive: %8lu kB\n"
182 "HighTotal: %8lu kB\n"
183 "HighFree: %8lu kB\n"
184 "LowTotal: %8lu kB\n"
186 "SwapTotal: %8lu kB\n"
187 "SwapFree: %8lu kB\n"
189 "Writeback: %8lu kB\n"
192 "CommitLimit: %8lu kB\n"
193 "Committed_AS: %8lu kB\n"
194 "PageTables: %8lu kB\n"
195 "VmallocTotal: %8lu kB\n"
196 "VmallocUsed: %8lu kB\n"
197 "VmallocChunk: %8lu kB\n",
202 K(total_swapcache_pages),
207 K(i.totalram-i.totalhigh),
208 K(i.freeram-i.freehigh),
217 K(ps.nr_page_table_pages),
218 (unsigned long)VMALLOC_TOTAL >> 10,
220 vmi.largest_chunk >> 10
223 len += hugetlb_report_meminfo(page + len);
225 return proc_calc_metrics(page, start, off, count, eof, len);
229 extern struct seq_operations fragmentation_op;
230 static int fragmentation_open(struct inode *inode, struct file *file)
233 return seq_open(file, &fragmentation_op);
236 static struct file_operations fragmentation_file_operations = {
237 .open = fragmentation_open,
240 .release = seq_release,
243 extern struct seq_operations zoneinfo_op;
244 static int zoneinfo_open(struct inode *inode, struct file *file)
246 return seq_open(file, &zoneinfo_op);
249 static struct file_operations proc_zoneinfo_file_operations = {
250 .open = zoneinfo_open,
253 .release = seq_release,
256 static int version_read_proc(char *page, char **start, off_t off,
257 int count, int *eof, void *data)
261 len = sprintf(page, vx_linux_banner,
263 vx_new_uts(version));
264 return proc_calc_metrics(page, start, off, count, eof, len);
267 extern struct seq_operations cpuinfo_op;
268 static int cpuinfo_open(struct inode *inode, struct file *file)
270 return seq_open(file, &cpuinfo_op);
273 static struct file_operations proc_cpuinfo_operations = {
274 .open = cpuinfo_open,
277 .release = seq_release,
280 static int devinfo_show(struct seq_file *f, void *v)
282 int i = *(loff_t *) v;
284 if (i < CHRDEV_MAJOR_HASH_SIZE) {
286 seq_printf(f, "Character devices:\n");
289 i -= CHRDEV_MAJOR_HASH_SIZE;
291 seq_printf(f, "\nBlock devices:\n");
297 static void *devinfo_start(struct seq_file *f, loff_t *pos)
299 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
304 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
307 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
312 static void devinfo_stop(struct seq_file *f, void *v)
317 static struct seq_operations devinfo_ops = {
318 .start = devinfo_start,
319 .next = devinfo_next,
320 .stop = devinfo_stop,
324 static int devinfo_open(struct inode *inode, struct file *filp)
326 return seq_open(filp, &devinfo_ops);
329 static struct file_operations proc_devinfo_operations = {
330 .open = devinfo_open,
333 .release = seq_release,
336 extern struct seq_operations vmstat_op;
337 static int vmstat_open(struct inode *inode, struct file *file)
339 return seq_open(file, &vmstat_op);
341 static struct file_operations proc_vmstat_file_operations = {
345 .release = seq_release,
348 #ifdef CONFIG_PROC_HARDWARE
349 static int hardware_read_proc(char *page, char **start, off_t off,
350 int count, int *eof, void *data)
352 int len = get_hardware_list(page);
353 return proc_calc_metrics(page, start, off, count, eof, len);
357 #ifdef CONFIG_STRAM_PROC
358 static int stram_read_proc(char *page, char **start, off_t off,
359 int count, int *eof, void *data)
361 int len = get_stram_list(page);
362 return proc_calc_metrics(page, start, off, count, eof, len);
366 extern struct seq_operations partitions_op;
367 static int partitions_open(struct inode *inode, struct file *file)
369 return seq_open(file, &partitions_op);
371 static struct file_operations proc_partitions_operations = {
372 .open = partitions_open,
375 .release = seq_release,
378 extern struct seq_operations diskstats_op;
379 static int diskstats_open(struct inode *inode, struct file *file)
381 return seq_open(file, &diskstats_op);
383 static struct file_operations proc_diskstats_operations = {
384 .open = diskstats_open,
387 .release = seq_release,
390 #ifdef CONFIG_MODULES
391 extern struct seq_operations modules_op;
392 static int modules_open(struct inode *inode, struct file *file)
394 return seq_open(file, &modules_op);
396 static struct file_operations proc_modules_operations = {
397 .open = modules_open,
400 .release = seq_release,
405 extern struct seq_operations slabinfo_op;
406 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
407 static int slabinfo_open(struct inode *inode, struct file *file)
409 return seq_open(file, &slabinfo_op);
411 static struct file_operations proc_slabinfo_operations = {
412 .open = slabinfo_open,
414 .write = slabinfo_write,
416 .release = seq_release,
419 #ifdef CONFIG_DEBUG_SLAB_LEAK
420 extern struct seq_operations slabstats_op;
421 static int slabstats_open(struct inode *inode, struct file *file)
423 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
426 ret = seq_open(file, &slabstats_op);
428 struct seq_file *m = file->private_data;
429 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
438 static int slabstats_release(struct inode *inode, struct file *file)
440 struct seq_file *m = file->private_data;
442 return seq_release(inode, file);
445 static struct file_operations proc_slabstats_operations = {
446 .open = slabstats_open,
449 .release = slabstats_release,
454 static int show_stat(struct seq_file *p, void *v)
458 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
461 user = nice = system = idle = iowait =
462 irq = softirq = steal = cputime64_zero;
463 jif = - wall_to_monotonic.tv_sec;
464 if (wall_to_monotonic.tv_nsec)
467 for_each_possible_cpu(i) {
470 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
471 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
472 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
473 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
474 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
475 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
476 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
477 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
478 for (j = 0 ; j < NR_IRQS ; j++)
479 sum += kstat_cpu(i).irqs[j];
482 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu\n",
483 (unsigned long long)cputime64_to_clock_t(user),
484 (unsigned long long)cputime64_to_clock_t(nice),
485 (unsigned long long)cputime64_to_clock_t(system),
486 (unsigned long long)cputime64_to_clock_t(idle),
487 (unsigned long long)cputime64_to_clock_t(iowait),
488 (unsigned long long)cputime64_to_clock_t(irq),
489 (unsigned long long)cputime64_to_clock_t(softirq),
490 (unsigned long long)cputime64_to_clock_t(steal));
491 for_each_online_cpu(i) {
493 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
494 user = kstat_cpu(i).cpustat.user;
495 nice = kstat_cpu(i).cpustat.nice;
496 system = kstat_cpu(i).cpustat.system;
497 idle = kstat_cpu(i).cpustat.idle;
498 iowait = kstat_cpu(i).cpustat.iowait;
499 irq = kstat_cpu(i).cpustat.irq;
500 softirq = kstat_cpu(i).cpustat.softirq;
501 steal = kstat_cpu(i).cpustat.steal;
502 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
504 (unsigned long long)cputime64_to_clock_t(user),
505 (unsigned long long)cputime64_to_clock_t(nice),
506 (unsigned long long)cputime64_to_clock_t(system),
507 (unsigned long long)cputime64_to_clock_t(idle),
508 (unsigned long long)cputime64_to_clock_t(iowait),
509 (unsigned long long)cputime64_to_clock_t(irq),
510 (unsigned long long)cputime64_to_clock_t(softirq),
511 (unsigned long long)cputime64_to_clock_t(steal));
513 seq_printf(p, "intr %llu", (unsigned long long)sum);
515 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA) && !defined(CONFIG_IA64)
516 for (i = 0; i < NR_IRQS; i++)
517 seq_printf(p, " %u", kstat_irqs(i));
524 "procs_running %lu\n"
525 "procs_blocked %lu\n",
526 nr_context_switches(),
535 static int stat_open(struct inode *inode, struct file *file)
537 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
542 /* don't ask for more than the kmalloc() max size, currently 128 KB */
543 if (size > 128 * 1024)
545 buf = kmalloc(size, GFP_KERNEL);
549 res = single_open(file, show_stat, NULL);
551 m = file->private_data;
558 static struct file_operations proc_stat_operations = {
562 .release = single_release,
568 static void *int_seq_start(struct seq_file *f, loff_t *pos)
570 return (*pos <= NR_IRQS) ? pos : NULL;
573 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
581 static void int_seq_stop(struct seq_file *f, void *v)
587 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
588 static struct seq_operations int_seq_ops = {
589 .start = int_seq_start,
590 .next = int_seq_next,
591 .stop = int_seq_stop,
592 .show = show_interrupts
595 static int interrupts_open(struct inode *inode, struct file *filp)
597 return seq_open(filp, &int_seq_ops);
600 static struct file_operations proc_interrupts_operations = {
601 .open = interrupts_open,
604 .release = seq_release,
607 static int filesystems_read_proc(char *page, char **start, off_t off,
608 int count, int *eof, void *data)
610 int len = get_filesystem_list(page);
611 return proc_calc_metrics(page, start, off, count, eof, len);
614 static int cmdline_read_proc(char *page, char **start, off_t off,
615 int count, int *eof, void *data)
619 len = sprintf(page, "%s\n", saved_command_line);
620 return proc_calc_metrics(page, start, off, count, eof, len);
623 static int locks_read_proc(char *page, char **start, off_t off,
624 int count, int *eof, void *data)
626 int len = get_locks_status(page, start, off, count);
633 static int execdomains_read_proc(char *page, char **start, off_t off,
634 int count, int *eof, void *data)
636 int len = get_exec_domain_list(page);
637 return proc_calc_metrics(page, start, off, count, eof, len);
640 #ifdef CONFIG_MAGIC_SYSRQ
642 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
644 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
645 size_t count, loff_t *ppos)
650 if (get_user(c, buf))
652 __handle_sysrq(c, NULL, NULL, 0);
657 static struct file_operations proc_sysrq_trigger_operations = {
658 .write = write_sysrq_trigger,
662 struct proc_dir_entry *proc_root_kcore;
664 void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
666 struct proc_dir_entry *entry;
667 entry = create_proc_entry(name, mode, NULL);
669 entry->proc_fops = f;
672 void __init proc_misc_init(void)
674 struct proc_dir_entry *entry;
677 int (*read_proc)(char*,char**,off_t,int,int*,void*);
678 } *p, simple_ones[] = {
679 {"loadavg", loadavg_read_proc},
680 {"uptime", uptime_read_proc},
681 {"meminfo", meminfo_read_proc},
682 {"version", version_read_proc},
683 #ifdef CONFIG_PROC_HARDWARE
684 {"hardware", hardware_read_proc},
686 #ifdef CONFIG_STRAM_PROC
687 {"stram", stram_read_proc},
689 {"filesystems", filesystems_read_proc},
690 {"cmdline", cmdline_read_proc},
691 {"locks", locks_read_proc},
692 {"execdomains", execdomains_read_proc},
695 for (p = simple_ones; p->name; p++)
696 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
698 proc_symlink("mounts", NULL, "self/mounts");
700 /* And now for trickier ones */
701 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
703 entry->proc_fops = &proc_kmsg_operations;
704 create_seq_entry("devices", 0, &proc_devinfo_operations);
705 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
706 create_seq_entry("partitions", 0, &proc_partitions_operations);
707 create_seq_entry("stat", 0, &proc_stat_operations);
708 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
710 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
711 #ifdef CONFIG_DEBUG_SLAB_LEAK
712 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);
715 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
716 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
717 create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
718 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
719 #ifdef CONFIG_MODULES
720 create_seq_entry("modules", 0, &proc_modules_operations);
722 #ifdef CONFIG_SCHEDSTATS
723 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
725 #ifdef CONFIG_PROC_KCORE
726 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
727 if (proc_root_kcore) {
728 proc_root_kcore->proc_fops = &proc_kcore_operations;
729 proc_root_kcore->size =
730 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
733 #ifdef CONFIG_PROC_VMCORE
734 proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
736 proc_vmcore->proc_fops = &proc_vmcore_operations;
738 #ifdef CONFIG_MAGIC_SYSRQ
739 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
741 entry->proc_fops = &proc_sysrq_trigger_operations;