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>
30 #include <linux/mmzone.h>
31 #include <linux/pagemap.h>
32 #include <linux/swap.h>
33 #include <linux/slab.h>
34 #include <linux/smp.h>
35 #include <linux/signal.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/smp_lock.h>
39 #include <linux/seq_file.h>
40 #include <linux/times.h>
41 #include <linux/profile.h>
42 #include <linux/utsname.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 <linux/pid_namespace.h>
50 #include <asm/uaccess.h>
51 #include <asm/pgtable.h>
54 #include <asm/div64.h>
57 #include <linux/vs_cvirt.h>
59 #define LOAD_INT(x) ((x) >> FSHIFT)
60 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
62 * Warning: stuff below (imported functions) assumes that its output will fit
63 * into one page. For some of those functions it may be wrong. Moreover, we
64 * have a way to deal with that gracefully. Right now I used straightforward
65 * wrappers, but this needs further analysis wrt potential overflows.
67 extern int get_hardware_list(char *);
68 extern int get_stram_list(char *);
69 extern int get_filesystem_list(char *);
70 extern int get_exec_domain_list(char *);
71 extern int get_dma_list(char *);
72 extern int get_locks_status (char *, char **, off_t, int);
74 static int proc_calc_metrics(char *page, char **start, off_t off,
75 int count, int *eof, int len)
77 if (len <= off+count) *eof = 1;
80 if (len>count) len = count;
85 static int loadavg_read_proc(char *page, char **start, off_t off,
86 int count, int *eof, void *data)
88 unsigned int running, threads;
92 if (vx_flags(VXF_VIRT_LOAD, 0)) {
93 struct vx_info *vxi = current->vx_info;
95 a = vxi->cvirt.load[0] + (FIXED_1/200);
96 b = vxi->cvirt.load[1] + (FIXED_1/200);
97 c = vxi->cvirt.load[2] + (FIXED_1/200);
99 running = atomic_read(&vxi->cvirt.nr_running);
100 threads = atomic_read(&vxi->cvirt.nr_threads);
102 a = avenrun[0] + (FIXED_1/200);
103 b = avenrun[1] + (FIXED_1/200);
104 c = avenrun[2] + (FIXED_1/200);
106 running = nr_running();
107 threads = nr_threads;
109 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
110 LOAD_INT(a), LOAD_FRAC(a),
111 LOAD_INT(b), LOAD_FRAC(b),
112 LOAD_INT(c), LOAD_FRAC(c),
113 running, threads, current->nsproxy->pid_ns->last_pid);
114 return proc_calc_metrics(page, start, off, count, eof, len);
117 static int uptime_read_proc(char *page, char **start, off_t off,
118 int count, int *eof, void *data)
120 struct timespec uptime;
121 struct timespec idle;
123 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
125 do_posix_clock_monotonic_gettime(&uptime);
126 cputime_to_timespec(idletime, &idle);
127 if (vx_flags(VXF_VIRT_UPTIME, 0))
128 vx_vsi_uptime(&uptime, &idle);
130 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
131 (unsigned long) uptime.tv_sec,
132 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
133 (unsigned long) idle.tv_sec,
134 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
136 return proc_calc_metrics(page, start, off, count, eof, len);
139 static int meminfo_read_proc(char *page, char **start, off_t off,
140 int count, int *eof, void *data)
144 unsigned long inactive;
145 unsigned long active;
147 unsigned long committed;
148 unsigned long allowed;
149 struct vmalloc_info vmi;
152 get_zone_counts(&active, &inactive, &free);
155 * display in kilobytes.
157 #define K(x) ((x) << (PAGE_SHIFT - 10))
160 committed = atomic_read(&vm_committed_space);
161 allowed = ((totalram_pages - hugetlb_total_pages())
162 * sysctl_overcommit_ratio / 100) + total_swap_pages;
164 cached = global_page_state(NR_FILE_PAGES) -
165 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 #ifdef CONFIG_HIGHMEM
183 "HighTotal: %8lu kB\n"
184 "HighFree: %8lu kB\n"
185 "LowTotal: %8lu kB\n"
188 "SwapTotal: %8lu kB\n"
189 "SwapFree: %8lu kB\n"
191 "Writeback: %8lu kB\n"
192 "AnonPages: %8lu kB\n"
195 "SReclaimable: %8lu kB\n"
196 "SUnreclaim: %8lu kB\n"
197 "PageTables: %8lu kB\n"
198 "NFS_Unstable: %8lu kB\n"
200 "CommitLimit: %8lu kB\n"
201 "Committed_AS: %8lu kB\n"
202 "VmallocTotal: %8lu kB\n"
203 "VmallocUsed: %8lu kB\n"
204 "VmallocChunk: %8lu kB\n",
209 K(total_swapcache_pages),
212 #ifdef CONFIG_HIGHMEM
215 K(i.totalram-i.totalhigh),
216 K(i.freeram-i.freehigh),
220 K(global_page_state(NR_FILE_DIRTY)),
221 K(global_page_state(NR_WRITEBACK)),
222 K(global_page_state(NR_ANON_PAGES)),
223 K(global_page_state(NR_FILE_MAPPED)),
224 K(global_page_state(NR_SLAB_RECLAIMABLE) +
225 global_page_state(NR_SLAB_UNRECLAIMABLE)),
226 K(global_page_state(NR_SLAB_RECLAIMABLE)),
227 K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
228 K(global_page_state(NR_PAGETABLE)),
229 K(global_page_state(NR_UNSTABLE_NFS)),
230 K(global_page_state(NR_BOUNCE)),
233 (unsigned long)VMALLOC_TOTAL >> 10,
235 vmi.largest_chunk >> 10
238 len += hugetlb_report_meminfo(page + len);
240 return proc_calc_metrics(page, start, off, count, eof, len);
244 extern struct seq_operations fragmentation_op;
245 static int fragmentation_open(struct inode *inode, struct file *file)
248 return seq_open(file, &fragmentation_op);
251 static struct file_operations fragmentation_file_operations = {
252 .open = fragmentation_open,
255 .release = seq_release,
258 extern struct seq_operations zoneinfo_op;
259 static int zoneinfo_open(struct inode *inode, struct file *file)
261 return seq_open(file, &zoneinfo_op);
264 static struct file_operations proc_zoneinfo_file_operations = {
265 .open = zoneinfo_open,
268 .release = seq_release,
271 static int version_read_proc(char *page, char **start, off_t off,
272 int count, int *eof, void *data)
276 len = snprintf(page, PAGE_SIZE, linux_proc_banner,
280 return proc_calc_metrics(page, start, off, count, eof, len);
283 extern struct seq_operations cpuinfo_op;
284 static int cpuinfo_open(struct inode *inode, struct file *file)
286 return seq_open(file, &cpuinfo_op);
289 static struct file_operations proc_cpuinfo_operations = {
290 .open = cpuinfo_open,
293 .release = seq_release,
296 static int devinfo_show(struct seq_file *f, void *v)
298 int i = *(loff_t *) v;
300 if (i < CHRDEV_MAJOR_HASH_SIZE) {
302 seq_printf(f, "Character devices:\n");
307 i -= CHRDEV_MAJOR_HASH_SIZE;
309 seq_printf(f, "\nBlock devices:\n");
316 static void *devinfo_start(struct seq_file *f, loff_t *pos)
318 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
323 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
326 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
331 static void devinfo_stop(struct seq_file *f, void *v)
336 static struct seq_operations devinfo_ops = {
337 .start = devinfo_start,
338 .next = devinfo_next,
339 .stop = devinfo_stop,
343 static int devinfo_open(struct inode *inode, struct file *filp)
345 return seq_open(filp, &devinfo_ops);
348 static struct file_operations proc_devinfo_operations = {
349 .open = devinfo_open,
352 .release = seq_release,
355 extern struct seq_operations vmstat_op;
356 static int vmstat_open(struct inode *inode, struct file *file)
358 return seq_open(file, &vmstat_op);
360 static struct file_operations proc_vmstat_file_operations = {
364 .release = seq_release,
367 #ifdef CONFIG_PROC_HARDWARE
368 static int hardware_read_proc(char *page, char **start, off_t off,
369 int count, int *eof, void *data)
371 int len = get_hardware_list(page);
372 return proc_calc_metrics(page, start, off, count, eof, len);
376 #ifdef CONFIG_STRAM_PROC
377 static int stram_read_proc(char *page, char **start, off_t off,
378 int count, int *eof, void *data)
380 int len = get_stram_list(page);
381 return proc_calc_metrics(page, start, off, count, eof, len);
386 extern struct seq_operations partitions_op;
387 static int partitions_open(struct inode *inode, struct file *file)
389 return seq_open(file, &partitions_op);
391 static struct file_operations proc_partitions_operations = {
392 .open = partitions_open,
395 .release = seq_release,
398 extern struct seq_operations diskstats_op;
399 static int diskstats_open(struct inode *inode, struct file *file)
401 return seq_open(file, &diskstats_op);
403 static struct file_operations proc_diskstats_operations = {
404 .open = diskstats_open,
407 .release = seq_release,
411 #ifdef CONFIG_MODULES
412 extern struct seq_operations modules_op;
413 static int modules_open(struct inode *inode, struct file *file)
415 return seq_open(file, &modules_op);
417 static struct file_operations proc_modules_operations = {
418 .open = modules_open,
421 .release = seq_release,
426 extern struct seq_operations slabinfo_op;
427 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
428 static int slabinfo_open(struct inode *inode, struct file *file)
430 return seq_open(file, &slabinfo_op);
432 static struct file_operations proc_slabinfo_operations = {
433 .open = slabinfo_open,
435 .write = slabinfo_write,
437 .release = seq_release,
440 #ifdef CONFIG_DEBUG_SLAB_LEAK
441 extern struct seq_operations slabstats_op;
442 static int slabstats_open(struct inode *inode, struct file *file)
444 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
447 ret = seq_open(file, &slabstats_op);
449 struct seq_file *m = file->private_data;
450 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
459 static int slabstats_release(struct inode *inode, struct file *file)
461 struct seq_file *m = file->private_data;
463 return seq_release(inode, file);
466 static struct file_operations proc_slabstats_operations = {
467 .open = slabstats_open,
470 .release = slabstats_release,
475 static int show_stat(struct seq_file *p, void *v)
479 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
482 user = nice = system = idle = iowait =
483 irq = softirq = steal = cputime64_zero;
484 jif = - wall_to_monotonic.tv_sec;
485 if (wall_to_monotonic.tv_nsec)
488 for_each_possible_cpu(i) {
491 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
492 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
493 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
494 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
495 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
496 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
497 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
498 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
499 for (j = 0 ; j < NR_IRQS ; j++)
500 sum += kstat_cpu(i).irqs[j];
503 seq_printf(p, "cpu %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));
512 for_each_online_cpu(i) {
514 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
515 user = kstat_cpu(i).cpustat.user;
516 nice = kstat_cpu(i).cpustat.nice;
517 system = kstat_cpu(i).cpustat.system;
518 idle = kstat_cpu(i).cpustat.idle;
519 iowait = kstat_cpu(i).cpustat.iowait;
520 irq = kstat_cpu(i).cpustat.irq;
521 softirq = kstat_cpu(i).cpustat.softirq;
522 steal = kstat_cpu(i).cpustat.steal;
523 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
525 (unsigned long long)cputime64_to_clock_t(user),
526 (unsigned long long)cputime64_to_clock_t(nice),
527 (unsigned long long)cputime64_to_clock_t(system),
528 (unsigned long long)cputime64_to_clock_t(idle),
529 (unsigned long long)cputime64_to_clock_t(iowait),
530 (unsigned long long)cputime64_to_clock_t(irq),
531 (unsigned long long)cputime64_to_clock_t(softirq),
532 (unsigned long long)cputime64_to_clock_t(steal));
534 seq_printf(p, "intr %llu", (unsigned long long)sum);
536 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA) && !defined(CONFIG_IA64)
537 for (i = 0; i < NR_IRQS; i++)
538 seq_printf(p, " %u", kstat_irqs(i));
545 "procs_running %lu\n"
546 "procs_blocked %lu\n",
547 nr_context_switches(),
556 static int stat_open(struct inode *inode, struct file *file)
558 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
563 /* don't ask for more than the kmalloc() max size, currently 128 KB */
564 if (size > 128 * 1024)
566 buf = kmalloc(size, GFP_KERNEL);
570 res = single_open(file, show_stat, NULL);
572 m = file->private_data;
579 static struct file_operations proc_stat_operations = {
583 .release = single_release,
589 static void *int_seq_start(struct seq_file *f, loff_t *pos)
591 return (*pos <= NR_IRQS) ? pos : NULL;
594 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
602 static void int_seq_stop(struct seq_file *f, void *v)
608 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
609 static struct seq_operations int_seq_ops = {
610 .start = int_seq_start,
611 .next = int_seq_next,
612 .stop = int_seq_stop,
613 .show = show_interrupts
616 static int interrupts_open(struct inode *inode, struct file *filp)
618 return seq_open(filp, &int_seq_ops);
621 static struct file_operations proc_interrupts_operations = {
622 .open = interrupts_open,
625 .release = seq_release,
628 static int filesystems_read_proc(char *page, char **start, off_t off,
629 int count, int *eof, void *data)
631 int len = get_filesystem_list(page);
632 return proc_calc_metrics(page, start, off, count, eof, len);
635 static int cmdline_read_proc(char *page, char **start, off_t off,
636 int count, int *eof, void *data)
640 len = sprintf(page, "%s\n", saved_command_line);
641 return proc_calc_metrics(page, start, off, count, eof, len);
644 static int locks_read_proc(char *page, char **start, off_t off,
645 int count, int *eof, void *data)
647 int len = get_locks_status(page, start, off, count);
654 static int execdomains_read_proc(char *page, char **start, off_t off,
655 int count, int *eof, void *data)
657 int len = get_exec_domain_list(page);
658 return proc_calc_metrics(page, start, off, count, eof, len);
661 #ifdef CONFIG_MAGIC_SYSRQ
663 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
665 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
666 size_t count, loff_t *ppos)
671 if (get_user(c, buf))
673 __handle_sysrq(c, NULL, 0);
678 static struct file_operations proc_sysrq_trigger_operations = {
679 .write = write_sysrq_trigger,
683 struct proc_dir_entry *proc_root_kcore;
685 void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
687 struct proc_dir_entry *entry;
688 entry = create_proc_entry(name, mode, NULL);
690 entry->proc_fops = f;
693 void __init proc_misc_init(void)
695 struct proc_dir_entry *entry;
698 int (*read_proc)(char*,char**,off_t,int,int*,void*);
699 } *p, simple_ones[] = {
700 {"loadavg", loadavg_read_proc},
701 {"uptime", uptime_read_proc},
702 {"meminfo", meminfo_read_proc},
703 {"version", version_read_proc},
704 #ifdef CONFIG_PROC_HARDWARE
705 {"hardware", hardware_read_proc},
707 #ifdef CONFIG_STRAM_PROC
708 {"stram", stram_read_proc},
710 {"filesystems", filesystems_read_proc},
711 {"cmdline", cmdline_read_proc},
712 {"locks", locks_read_proc},
713 {"execdomains", execdomains_read_proc},
716 for (p = simple_ones; p->name; p++)
717 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
719 proc_symlink("mounts", NULL, "self/mounts");
721 /* And now for trickier ones */
723 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
725 entry->proc_fops = &proc_kmsg_operations;
727 create_seq_entry("devices", 0, &proc_devinfo_operations);
728 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
730 create_seq_entry("partitions", 0, &proc_partitions_operations);
732 create_seq_entry("stat", 0, &proc_stat_operations);
733 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
735 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
736 #ifdef CONFIG_DEBUG_SLAB_LEAK
737 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);
740 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
741 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
742 create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
744 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
746 #ifdef CONFIG_MODULES
747 create_seq_entry("modules", 0, &proc_modules_operations);
749 #ifdef CONFIG_SCHEDSTATS
750 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
752 #ifdef CONFIG_PROC_KCORE
753 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
754 if (proc_root_kcore) {
755 proc_root_kcore->proc_fops = &proc_kcore_operations;
756 proc_root_kcore->size =
757 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
760 #ifdef CONFIG_PROC_VMCORE
761 proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
763 proc_vmcore->proc_fops = &proc_vmcore_operations;
765 #ifdef CONFIG_MAGIC_SYSRQ
766 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
768 entry->proc_fops = &proc_sysrq_trigger_operations;