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/blkdev.h>
43 #include <linux/hugetlb.h>
44 #include <linux/jiffies.h>
45 #include <linux/sysrq.h>
46 #include <linux/vmalloc.h>
47 #include <linux/crash_dump.h>
48 #include <asm/uaccess.h>
49 #include <asm/pgtable.h>
52 #include <asm/div64.h>
54 #include <linux/vs_cvirt.h>
56 #define LOAD_INT(x) ((x) >> FSHIFT)
57 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
59 * Warning: stuff below (imported functions) assumes that its output will fit
60 * into one page. For some of those functions it may be wrong. Moreover, we
61 * have a way to deal with that gracefully. Right now I used straightforward
62 * wrappers, but this needs further analysis wrt potential overflows.
64 extern int get_hardware_list(char *);
65 extern int get_stram_list(char *);
66 extern int get_filesystem_list(char *);
67 extern int get_exec_domain_list(char *);
68 extern int get_dma_list(char *);
69 extern int get_locks_status (char *, char **, off_t, int);
71 static int proc_calc_metrics(char *page, char **start, off_t off,
72 int count, int *eof, int len)
74 if (len <= off+count) *eof = 1;
77 if (len>count) len = count;
82 static int loadavg_read_proc(char *page, char **start, off_t off,
83 int count, int *eof, void *data)
85 unsigned int running, threads;
89 if (vx_flags(VXF_VIRT_LOAD, 0)) {
90 struct vx_info *vxi = current->vx_info;
92 a = vxi->cvirt.load[0] + (FIXED_1/200);
93 b = vxi->cvirt.load[1] + (FIXED_1/200);
94 c = vxi->cvirt.load[2] + (FIXED_1/200);
96 running = atomic_read(&vxi->cvirt.nr_running);
97 threads = atomic_read(&vxi->cvirt.nr_threads);
99 a = avenrun[0] + (FIXED_1/200);
100 b = avenrun[1] + (FIXED_1/200);
101 c = avenrun[2] + (FIXED_1/200);
103 running = nr_running();
104 threads = nr_threads;
106 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %d/%d %d\n",
107 LOAD_INT(a), LOAD_FRAC(a),
108 LOAD_INT(b), LOAD_FRAC(b),
109 LOAD_INT(c), LOAD_FRAC(c),
110 running, threads, last_pid);
111 return proc_calc_metrics(page, start, off, count, eof, len);
114 static int uptime_read_proc(char *page, char **start, off_t off,
115 int count, int *eof, void *data)
117 struct timespec uptime;
118 struct timespec idle;
120 cputime_t idletime = cputime_add(init_task.utime, init_task.stime);
122 do_posix_clock_monotonic_gettime(&uptime);
123 cputime_to_timespec(idletime, &idle);
124 if (vx_flags(VXF_VIRT_UPTIME, 0))
125 vx_vsi_uptime(&uptime, &idle);
127 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
128 (unsigned long) uptime.tv_sec,
129 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
130 (unsigned long) idle.tv_sec,
131 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
133 return proc_calc_metrics(page, start, off, count, eof, len);
136 static int meminfo_read_proc(char *page, char **start, off_t off,
137 int count, int *eof, void *data)
141 unsigned long inactive;
142 unsigned long active;
144 unsigned long committed;
145 unsigned long allowed;
146 struct vmalloc_info vmi;
149 get_zone_counts(&active, &inactive, &free);
152 * display in kilobytes.
154 #define K(x) ((x) << (PAGE_SHIFT - 10))
157 committed = atomic_read(&vm_committed_space);
158 allowed = ((totalram_pages - hugetlb_total_pages())
159 * sysctl_overcommit_ratio / 100) + total_swap_pages;
161 cached = global_page_state(NR_FILE_PAGES) -
162 total_swapcache_pages - i.bufferram;
163 if (cached < 0 || vx_flags(VXF_VIRT_MEM, 0))
166 get_vmalloc_info(&vmi);
169 * Tagged format, for easy grepping and expansion.
172 "MemTotal: %8lu kB\n"
176 "SwapCached: %8lu kB\n"
178 "Inactive: %8lu kB\n"
179 "HighTotal: %8lu kB\n"
180 "HighFree: %8lu kB\n"
181 "LowTotal: %8lu kB\n"
183 "SwapTotal: %8lu kB\n"
184 "SwapFree: %8lu kB\n"
186 "Writeback: %8lu kB\n"
187 "AnonPages: %8lu kB\n"
190 "PageTables: %8lu kB\n"
191 "NFS_Unstable: %8lu kB\n"
193 "CommitLimit: %8lu kB\n"
194 "Committed_AS: %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),
211 K(global_page_state(NR_FILE_DIRTY)),
212 K(global_page_state(NR_WRITEBACK)),
213 K(global_page_state(NR_ANON_PAGES)),
214 K(global_page_state(NR_FILE_MAPPED)),
215 K(global_page_state(NR_SLAB)),
216 K(global_page_state(NR_PAGETABLE)),
217 K(global_page_state(NR_UNSTABLE_NFS)),
218 K(global_page_state(NR_BOUNCE)),
221 (unsigned long)VMALLOC_TOTAL >> 10,
223 vmi.largest_chunk >> 10
226 len += hugetlb_report_meminfo(page + len);
228 return proc_calc_metrics(page, start, off, count, eof, len);
232 extern struct seq_operations fragmentation_op;
233 static int fragmentation_open(struct inode *inode, struct file *file)
236 return seq_open(file, &fragmentation_op);
239 static struct file_operations fragmentation_file_operations = {
240 .open = fragmentation_open,
243 .release = seq_release,
246 extern struct seq_operations zoneinfo_op;
247 static int zoneinfo_open(struct inode *inode, struct file *file)
249 return seq_open(file, &zoneinfo_op);
252 static struct file_operations proc_zoneinfo_file_operations = {
253 .open = zoneinfo_open,
256 .release = seq_release,
259 static int version_read_proc(char *page, char **start, off_t off,
260 int count, int *eof, void *data)
264 len = sprintf(page, vx_linux_banner,
266 vx_new_uts(version));
267 return proc_calc_metrics(page, start, off, count, eof, len);
270 extern struct seq_operations cpuinfo_op;
271 static int cpuinfo_open(struct inode *inode, struct file *file)
273 return seq_open(file, &cpuinfo_op);
276 static struct file_operations proc_cpuinfo_operations = {
277 .open = cpuinfo_open,
280 .release = seq_release,
283 static int devinfo_show(struct seq_file *f, void *v)
285 int i = *(loff_t *) v;
287 if (i < CHRDEV_MAJOR_HASH_SIZE) {
289 seq_printf(f, "Character devices:\n");
292 i -= CHRDEV_MAJOR_HASH_SIZE;
294 seq_printf(f, "\nBlock devices:\n");
300 static void *devinfo_start(struct seq_file *f, loff_t *pos)
302 if (*pos < (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
307 static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
310 if (*pos >= (BLKDEV_MAJOR_HASH_SIZE + CHRDEV_MAJOR_HASH_SIZE))
315 static void devinfo_stop(struct seq_file *f, void *v)
320 static struct seq_operations devinfo_ops = {
321 .start = devinfo_start,
322 .next = devinfo_next,
323 .stop = devinfo_stop,
327 static int devinfo_open(struct inode *inode, struct file *filp)
329 return seq_open(filp, &devinfo_ops);
332 static struct file_operations proc_devinfo_operations = {
333 .open = devinfo_open,
336 .release = seq_release,
339 extern struct seq_operations vmstat_op;
340 static int vmstat_open(struct inode *inode, struct file *file)
342 return seq_open(file, &vmstat_op);
344 static struct file_operations proc_vmstat_file_operations = {
348 .release = seq_release,
351 #ifdef CONFIG_PROC_HARDWARE
352 static int hardware_read_proc(char *page, char **start, off_t off,
353 int count, int *eof, void *data)
355 int len = get_hardware_list(page);
356 return proc_calc_metrics(page, start, off, count, eof, len);
360 #ifdef CONFIG_STRAM_PROC
361 static int stram_read_proc(char *page, char **start, off_t off,
362 int count, int *eof, void *data)
364 int len = get_stram_list(page);
365 return proc_calc_metrics(page, start, off, count, eof, len);
369 extern struct seq_operations partitions_op;
370 static int partitions_open(struct inode *inode, struct file *file)
372 return seq_open(file, &partitions_op);
374 static struct file_operations proc_partitions_operations = {
375 .open = partitions_open,
378 .release = seq_release,
381 extern struct seq_operations diskstats_op;
382 static int diskstats_open(struct inode *inode, struct file *file)
384 return seq_open(file, &diskstats_op);
386 static struct file_operations proc_diskstats_operations = {
387 .open = diskstats_open,
390 .release = seq_release,
393 #ifdef CONFIG_MODULES
394 extern struct seq_operations modules_op;
395 static int modules_open(struct inode *inode, struct file *file)
397 return seq_open(file, &modules_op);
399 static struct file_operations proc_modules_operations = {
400 .open = modules_open,
403 .release = seq_release,
408 extern struct seq_operations slabinfo_op;
409 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
410 static int slabinfo_open(struct inode *inode, struct file *file)
412 return seq_open(file, &slabinfo_op);
414 static struct file_operations proc_slabinfo_operations = {
415 .open = slabinfo_open,
417 .write = slabinfo_write,
419 .release = seq_release,
422 #ifdef CONFIG_DEBUG_SLAB_LEAK
423 extern struct seq_operations slabstats_op;
424 static int slabstats_open(struct inode *inode, struct file *file)
426 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
429 ret = seq_open(file, &slabstats_op);
431 struct seq_file *m = file->private_data;
432 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
441 static int slabstats_release(struct inode *inode, struct file *file)
443 struct seq_file *m = file->private_data;
445 return seq_release(inode, file);
448 static struct file_operations proc_slabstats_operations = {
449 .open = slabstats_open,
452 .release = slabstats_release,
457 static int show_stat(struct seq_file *p, void *v)
461 cputime64_t user, nice, system, idle, iowait, irq, softirq, steal;
464 user = nice = system = idle = iowait =
465 irq = softirq = steal = cputime64_zero;
466 jif = - wall_to_monotonic.tv_sec;
467 if (wall_to_monotonic.tv_nsec)
470 for_each_possible_cpu(i) {
473 user = cputime64_add(user, kstat_cpu(i).cpustat.user);
474 nice = cputime64_add(nice, kstat_cpu(i).cpustat.nice);
475 system = cputime64_add(system, kstat_cpu(i).cpustat.system);
476 idle = cputime64_add(idle, kstat_cpu(i).cpustat.idle);
477 iowait = cputime64_add(iowait, kstat_cpu(i).cpustat.iowait);
478 irq = cputime64_add(irq, kstat_cpu(i).cpustat.irq);
479 softirq = cputime64_add(softirq, kstat_cpu(i).cpustat.softirq);
480 steal = cputime64_add(steal, kstat_cpu(i).cpustat.steal);
481 for (j = 0 ; j < NR_IRQS ; j++)
482 sum += kstat_cpu(i).irqs[j];
485 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu %llu\n",
486 (unsigned long long)cputime64_to_clock_t(user),
487 (unsigned long long)cputime64_to_clock_t(nice),
488 (unsigned long long)cputime64_to_clock_t(system),
489 (unsigned long long)cputime64_to_clock_t(idle),
490 (unsigned long long)cputime64_to_clock_t(iowait),
491 (unsigned long long)cputime64_to_clock_t(irq),
492 (unsigned long long)cputime64_to_clock_t(softirq),
493 (unsigned long long)cputime64_to_clock_t(steal));
494 for_each_online_cpu(i) {
496 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
497 user = kstat_cpu(i).cpustat.user;
498 nice = kstat_cpu(i).cpustat.nice;
499 system = kstat_cpu(i).cpustat.system;
500 idle = kstat_cpu(i).cpustat.idle;
501 iowait = kstat_cpu(i).cpustat.iowait;
502 irq = kstat_cpu(i).cpustat.irq;
503 softirq = kstat_cpu(i).cpustat.softirq;
504 steal = kstat_cpu(i).cpustat.steal;
505 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu\n",
507 (unsigned long long)cputime64_to_clock_t(user),
508 (unsigned long long)cputime64_to_clock_t(nice),
509 (unsigned long long)cputime64_to_clock_t(system),
510 (unsigned long long)cputime64_to_clock_t(idle),
511 (unsigned long long)cputime64_to_clock_t(iowait),
512 (unsigned long long)cputime64_to_clock_t(irq),
513 (unsigned long long)cputime64_to_clock_t(softirq),
514 (unsigned long long)cputime64_to_clock_t(steal));
516 seq_printf(p, "intr %llu", (unsigned long long)sum);
518 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA) && !defined(CONFIG_IA64)
519 for (i = 0; i < NR_IRQS; i++)
520 seq_printf(p, " %u", kstat_irqs(i));
527 "procs_running %lu\n"
528 "procs_blocked %lu\n",
529 nr_context_switches(),
538 static int stat_open(struct inode *inode, struct file *file)
540 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
545 /* don't ask for more than the kmalloc() max size, currently 128 KB */
546 if (size > 128 * 1024)
548 buf = kmalloc(size, GFP_KERNEL);
552 res = single_open(file, show_stat, NULL);
554 m = file->private_data;
561 static struct file_operations proc_stat_operations = {
565 .release = single_release,
571 static void *int_seq_start(struct seq_file *f, loff_t *pos)
573 return (*pos <= NR_IRQS) ? pos : NULL;
576 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
584 static void int_seq_stop(struct seq_file *f, void *v)
590 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
591 static struct seq_operations int_seq_ops = {
592 .start = int_seq_start,
593 .next = int_seq_next,
594 .stop = int_seq_stop,
595 .show = show_interrupts
598 static int interrupts_open(struct inode *inode, struct file *filp)
600 return seq_open(filp, &int_seq_ops);
603 static struct file_operations proc_interrupts_operations = {
604 .open = interrupts_open,
607 .release = seq_release,
610 static int filesystems_read_proc(char *page, char **start, off_t off,
611 int count, int *eof, void *data)
613 int len = get_filesystem_list(page);
614 return proc_calc_metrics(page, start, off, count, eof, len);
617 static int cmdline_read_proc(char *page, char **start, off_t off,
618 int count, int *eof, void *data)
622 len = sprintf(page, "%s\n", saved_command_line);
623 return proc_calc_metrics(page, start, off, count, eof, len);
626 static int locks_read_proc(char *page, char **start, off_t off,
627 int count, int *eof, void *data)
629 int len = get_locks_status(page, start, off, count);
636 static int execdomains_read_proc(char *page, char **start, off_t off,
637 int count, int *eof, void *data)
639 int len = get_exec_domain_list(page);
640 return proc_calc_metrics(page, start, off, count, eof, len);
643 #ifdef CONFIG_MAGIC_SYSRQ
645 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
647 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
648 size_t count, loff_t *ppos)
653 if (get_user(c, buf))
655 __handle_sysrq(c, NULL, NULL, 0);
660 static struct file_operations proc_sysrq_trigger_operations = {
661 .write = write_sysrq_trigger,
665 struct proc_dir_entry *proc_root_kcore;
667 void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
669 struct proc_dir_entry *entry;
670 entry = create_proc_entry(name, mode, NULL);
672 entry->proc_fops = f;
675 void __init proc_misc_init(void)
677 struct proc_dir_entry *entry;
680 int (*read_proc)(char*,char**,off_t,int,int*,void*);
681 } *p, simple_ones[] = {
682 {"loadavg", loadavg_read_proc},
683 {"uptime", uptime_read_proc},
684 {"meminfo", meminfo_read_proc},
685 {"version", version_read_proc},
686 #ifdef CONFIG_PROC_HARDWARE
687 {"hardware", hardware_read_proc},
689 #ifdef CONFIG_STRAM_PROC
690 {"stram", stram_read_proc},
692 {"filesystems", filesystems_read_proc},
693 {"cmdline", cmdline_read_proc},
694 {"locks", locks_read_proc},
695 {"execdomains", execdomains_read_proc},
698 for (p = simple_ones; p->name; p++)
699 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
701 proc_symlink("mounts", NULL, "self/mounts");
703 /* And now for trickier ones */
704 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
706 entry->proc_fops = &proc_kmsg_operations;
707 create_seq_entry("devices", 0, &proc_devinfo_operations);
708 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
709 create_seq_entry("partitions", 0, &proc_partitions_operations);
710 create_seq_entry("stat", 0, &proc_stat_operations);
711 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
713 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
714 #ifdef CONFIG_DEBUG_SLAB_LEAK
715 create_seq_entry("slab_allocators", 0 ,&proc_slabstats_operations);
718 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
719 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
720 create_seq_entry("zoneinfo",S_IRUGO, &proc_zoneinfo_file_operations);
721 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
722 #ifdef CONFIG_MODULES
723 create_seq_entry("modules", 0, &proc_modules_operations);
725 #ifdef CONFIG_SCHEDSTATS
726 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
728 #ifdef CONFIG_PROC_KCORE
729 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
730 if (proc_root_kcore) {
731 proc_root_kcore->proc_fops = &proc_kcore_operations;
732 proc_root_kcore->size =
733 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
736 #ifdef CONFIG_PROC_VMCORE
737 proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
739 proc_vmcore->proc_fops = &proc_vmcore_operations;
741 #ifdef CONFIG_MAGIC_SYSRQ
742 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
744 entry->proc_fops = &proc_sysrq_trigger_operations;