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>
23 #include <linux/tty.h>
24 #include <linux/string.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/ioport.h>
28 #include <linux/config.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 <asm/uaccess.h>
48 #include <asm/pgtable.h>
51 #include <asm/div64.h>
53 #define LOAD_INT(x) ((x) >> FSHIFT)
54 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
56 * Warning: stuff below (imported functions) assumes that its output will fit
57 * into one page. For some of those functions it may be wrong. Moreover, we
58 * have a way to deal with that gracefully. Right now I used straightforward
59 * wrappers, but this needs further analysis wrt potential overflows.
61 extern int get_hardware_list(char *);
62 extern int get_stram_list(char *);
63 extern int get_chrdev_list(char *);
64 extern int get_blkdev_list(char *);
65 extern int get_filesystem_list(char *);
66 extern int get_exec_domain_list(char *);
67 extern int get_dma_list(char *);
68 extern int get_locks_status (char *, char **, off_t, int);
70 static int proc_calc_metrics(char *page, char **start, off_t off,
71 int count, int *eof, int len)
73 if (len <= off+count) *eof = 1;
76 if (len>count) len = count;
81 static int loadavg_read_proc(char *page, char **start, off_t off,
82 int count, int *eof, void *data)
87 a = avenrun[0] + (FIXED_1/200);
88 b = avenrun[1] + (FIXED_1/200);
89 c = avenrun[2] + (FIXED_1/200);
90 len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
91 LOAD_INT(a), LOAD_FRAC(a),
92 LOAD_INT(b), LOAD_FRAC(b),
93 LOAD_INT(c), LOAD_FRAC(c),
94 nr_running(), nr_threads, last_pid);
95 return proc_calc_metrics(page, start, off, count, eof, len);
100 unsigned long largest_chunk;
103 static struct vmalloc_info get_vmalloc_info(void)
105 unsigned long prev_end = VMALLOC_START;
106 struct vm_struct* vma;
107 struct vmalloc_info vmi;
110 read_lock(&vmlist_lock);
113 vmi.largest_chunk = (VMALLOC_END-VMALLOC_START);
115 vmi.largest_chunk = 0;
117 for (vma = vmlist; vma; vma = vma->next) {
118 unsigned long free_area_size =
119 (unsigned long)vma->addr - prev_end;
120 vmi.used += vma->size;
121 if (vmi.largest_chunk < free_area_size )
123 vmi.largest_chunk = free_area_size;
124 prev_end = vma->size + (unsigned long)vma->addr;
126 if(VMALLOC_END-prev_end > vmi.largest_chunk)
127 vmi.largest_chunk = VMALLOC_END-prev_end;
129 read_unlock(&vmlist_lock);
133 static int uptime_read_proc(char *page, char **start, off_t off,
134 int count, int *eof, void *data)
136 struct timespec uptime;
137 struct timespec idle;
139 u64 idle_jiffies = init_task.utime + init_task.stime;
141 do_posix_clock_monotonic_gettime(&uptime);
142 jiffies_to_timespec(idle_jiffies, &idle);
143 len = sprintf(page,"%lu.%02lu %lu.%02lu\n",
144 (unsigned long) uptime.tv_sec,
145 (uptime.tv_nsec / (NSEC_PER_SEC / 100)),
146 (unsigned long) idle.tv_sec,
147 (idle.tv_nsec / (NSEC_PER_SEC / 100)));
149 return proc_calc_metrics(page, start, off, count, eof, len);
152 static int meminfo_read_proc(char *page, char **start, off_t off,
153 int count, int *eof, void *data)
157 struct page_state ps;
158 unsigned long inactive;
159 unsigned long active;
162 unsigned long committed;
163 unsigned long allowed;
164 struct vmalloc_info vmi;
167 get_zone_counts(&active, &inactive, &free);
170 * display in kilobytes.
172 #define K(x) ((x) << (PAGE_SHIFT - 10))
175 committed = atomic_read(&vm_committed_space);
176 allowed = ((totalram_pages - hugetlb_total_pages())
177 * sysctl_overcommit_ratio / 100) + total_swap_pages;
179 vmtot = (VMALLOC_END-VMALLOC_START)>>10;
180 vmi = get_vmalloc_info();
182 vmi.largest_chunk >>= 10;
185 * Tagged format, for easy grepping and expansion.
188 "MemTotal: %8lu kB\n"
192 "SwapCached: %8lu kB\n"
194 "Inactive: %8lu kB\n"
195 "HighTotal: %8lu kB\n"
196 "HighFree: %8lu kB\n"
197 "LowTotal: %8lu kB\n"
199 "SwapTotal: %8lu kB\n"
200 "SwapFree: %8lu kB\n"
202 "Writeback: %8lu kB\n"
205 "CommitLimit: %8lu kB\n"
206 "Committed_AS: %8lu kB\n"
207 "PageTables: %8lu kB\n"
208 "VmallocTotal: %8lu kB\n"
209 "VmallocUsed: %8lu kB\n"
210 "VmallocChunk: %8lu kB\n",
214 K(get_page_cache_size()-total_swapcache_pages-i.bufferram),
215 K(total_swapcache_pages),
220 K(i.totalram-i.totalhigh),
221 K(i.freeram-i.freehigh),
230 K(ps.nr_page_table_pages),
236 len += hugetlb_report_meminfo(page + len);
238 return proc_calc_metrics(page, start, off, count, eof, len);
242 extern struct seq_operations fragmentation_op;
243 static int fragmentation_open(struct inode *inode, struct file *file)
246 return seq_open(file, &fragmentation_op);
249 static struct file_operations fragmentation_file_operations = {
250 .open = fragmentation_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)
259 extern char *linux_banner;
262 strcpy(page, linux_banner);
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);
272 static struct file_operations proc_cpuinfo_operations = {
273 .open = cpuinfo_open,
276 .release = seq_release,
279 extern struct seq_operations vmstat_op;
280 static int vmstat_open(struct inode *inode, struct file *file)
282 return seq_open(file, &vmstat_op);
284 static struct file_operations proc_vmstat_file_operations = {
288 .release = seq_release,
291 #ifdef CONFIG_PROC_HARDWARE
292 static int hardware_read_proc(char *page, char **start, off_t off,
293 int count, int *eof, void *data)
295 int len = get_hardware_list(page);
296 return proc_calc_metrics(page, start, off, count, eof, len);
300 #ifdef CONFIG_STRAM_PROC
301 static int stram_read_proc(char *page, char **start, off_t off,
302 int count, int *eof, void *data)
304 int len = get_stram_list(page);
305 return proc_calc_metrics(page, start, off, count, eof, len);
309 extern struct seq_operations partitions_op;
310 static int partitions_open(struct inode *inode, struct file *file)
312 return seq_open(file, &partitions_op);
314 static struct file_operations proc_partitions_operations = {
315 .open = partitions_open,
318 .release = seq_release,
321 extern struct seq_operations diskstats_op;
322 static int diskstats_open(struct inode *inode, struct file *file)
324 return seq_open(file, &diskstats_op);
326 static struct file_operations proc_diskstats_operations = {
327 .open = diskstats_open,
330 .release = seq_release,
333 #ifdef CONFIG_MODULES
334 extern struct seq_operations modules_op;
335 static int modules_open(struct inode *inode, struct file *file)
337 return seq_open(file, &modules_op);
339 static struct file_operations proc_modules_operations = {
340 .open = modules_open,
343 .release = seq_release,
347 extern struct seq_operations slabinfo_op;
348 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
349 static int slabinfo_open(struct inode *inode, struct file *file)
351 return seq_open(file, &slabinfo_op);
353 static struct file_operations proc_slabinfo_operations = {
354 .open = slabinfo_open,
356 .write = slabinfo_write,
358 .release = seq_release,
361 int show_stat(struct seq_file *p, void *v)
364 extern unsigned long total_forks;
366 u64 sum = 0, user = 0, nice = 0, system = 0,
367 idle = 0, iowait = 0, irq = 0, softirq = 0;
369 jif = - wall_to_monotonic.tv_sec;
370 if (wall_to_monotonic.tv_nsec)
376 user += kstat_cpu(i).cpustat.user;
377 nice += kstat_cpu(i).cpustat.nice;
378 system += kstat_cpu(i).cpustat.system;
379 idle += kstat_cpu(i).cpustat.idle;
380 iowait += kstat_cpu(i).cpustat.iowait;
381 irq += kstat_cpu(i).cpustat.irq;
382 softirq += kstat_cpu(i).cpustat.softirq;
383 for (j = 0 ; j < NR_IRQS ; j++)
384 sum += kstat_cpu(i).irqs[j];
387 seq_printf(p, "cpu %llu %llu %llu %llu %llu %llu %llu\n",
388 (unsigned long long)jiffies_64_to_clock_t(user),
389 (unsigned long long)jiffies_64_to_clock_t(nice),
390 (unsigned long long)jiffies_64_to_clock_t(system),
391 (unsigned long long)jiffies_64_to_clock_t(idle),
392 (unsigned long long)jiffies_64_to_clock_t(iowait),
393 (unsigned long long)jiffies_64_to_clock_t(irq),
394 (unsigned long long)jiffies_64_to_clock_t(softirq));
395 for_each_online_cpu(i) {
397 /* Copy values here to work around gcc-2.95.3, gcc-2.96 */
398 user = kstat_cpu(i).cpustat.user;
399 nice = kstat_cpu(i).cpustat.nice;
400 system = kstat_cpu(i).cpustat.system;
401 idle = kstat_cpu(i).cpustat.idle;
402 iowait = kstat_cpu(i).cpustat.iowait;
403 irq = kstat_cpu(i).cpustat.irq;
404 softirq = kstat_cpu(i).cpustat.softirq;
405 seq_printf(p, "cpu%d %llu %llu %llu %llu %llu %llu %llu\n",
407 (unsigned long long)jiffies_64_to_clock_t(user),
408 (unsigned long long)jiffies_64_to_clock_t(nice),
409 (unsigned long long)jiffies_64_to_clock_t(system),
410 (unsigned long long)jiffies_64_to_clock_t(idle),
411 (unsigned long long)jiffies_64_to_clock_t(iowait),
412 (unsigned long long)jiffies_64_to_clock_t(irq),
413 (unsigned long long)jiffies_64_to_clock_t(softirq));
415 seq_printf(p, "intr %llu", (unsigned long long)sum);
417 #if !defined(CONFIG_PPC64) && !defined(CONFIG_ALPHA)
418 for (i = 0; i < NR_IRQS; i++)
419 seq_printf(p, " %u", kstat_irqs(i));
426 "procs_running %lu\n"
427 "procs_blocked %lu\n",
428 nr_context_switches(),
437 static int stat_open(struct inode *inode, struct file *file)
439 unsigned size = 4096 * (1 + num_possible_cpus() / 32);
444 /* don't ask for more than the kmalloc() max size, currently 128 KB */
445 if (size > 128 * 1024)
447 buf = kmalloc(size, GFP_KERNEL);
451 res = single_open(file, show_stat, NULL);
453 m = file->private_data;
460 static struct file_operations proc_stat_operations = {
464 .release = single_release,
467 static int devices_read_proc(char *page, char **start, off_t off,
468 int count, int *eof, void *data)
470 int len = get_chrdev_list(page);
471 len += get_blkdev_list(page+len);
472 return proc_calc_metrics(page, start, off, count, eof, len);
478 static void *int_seq_start(struct seq_file *f, loff_t *pos)
480 return (*pos <= NR_IRQS) ? pos : NULL;
483 static void *int_seq_next(struct seq_file *f, void *v, loff_t *pos)
491 static void int_seq_stop(struct seq_file *f, void *v)
497 extern int show_interrupts(struct seq_file *f, void *v); /* In arch code */
498 static struct seq_operations int_seq_ops = {
499 .start = int_seq_start,
500 .next = int_seq_next,
501 .stop = int_seq_stop,
502 .show = show_interrupts
505 int interrupts_open(struct inode *inode, struct file *filp)
507 return seq_open(filp, &int_seq_ops);
510 static struct file_operations proc_interrupts_operations = {
511 .open = interrupts_open,
514 .release = seq_release,
517 static int filesystems_read_proc(char *page, char **start, off_t off,
518 int count, int *eof, void *data)
520 int len = get_filesystem_list(page);
521 return proc_calc_metrics(page, start, off, count, eof, len);
524 static int cmdline_read_proc(char *page, char **start, off_t off,
525 int count, int *eof, void *data)
529 len = sprintf(page, "%s\n", saved_command_line);
530 return proc_calc_metrics(page, start, off, count, eof, len);
533 static int locks_read_proc(char *page, char **start, off_t off,
534 int count, int *eof, void *data)
536 int len = get_locks_status(page, start, off, count);
543 static int execdomains_read_proc(char *page, char **start, off_t off,
544 int count, int *eof, void *data)
546 int len = get_exec_domain_list(page);
547 return proc_calc_metrics(page, start, off, count, eof, len);
550 #ifdef CONFIG_MAGIC_SYSRQ
552 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
554 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
555 size_t count, loff_t *ppos)
560 if (get_user(c, buf))
562 __handle_sysrq(c, NULL, NULL);
567 static struct file_operations proc_sysrq_trigger_operations = {
568 .write = write_sysrq_trigger,
572 struct proc_dir_entry *proc_root_kcore;
574 static void create_seq_entry(char *name, mode_t mode, struct file_operations *f)
576 struct proc_dir_entry *entry;
577 entry = create_proc_entry(name, mode, NULL);
579 entry->proc_fops = f;
582 void __init proc_misc_init(void)
584 struct proc_dir_entry *entry;
587 int (*read_proc)(char*,char**,off_t,int,int*,void*);
588 } *p, simple_ones[] = {
589 {"loadavg", loadavg_read_proc},
590 {"uptime", uptime_read_proc},
591 {"meminfo", meminfo_read_proc},
592 {"version", version_read_proc},
593 #ifdef CONFIG_PROC_HARDWARE
594 {"hardware", hardware_read_proc},
596 #ifdef CONFIG_STRAM_PROC
597 {"stram", stram_read_proc},
599 {"devices", devices_read_proc},
600 {"filesystems", filesystems_read_proc},
601 {"cmdline", cmdline_read_proc},
602 {"locks", locks_read_proc},
603 {"execdomains", execdomains_read_proc},
606 for (p = simple_ones; p->name; p++)
607 create_proc_read_entry(p->name, 0, NULL, p->read_proc, NULL);
609 proc_symlink("mounts", NULL, "self/mounts");
611 /* And now for trickier ones */
612 entry = create_proc_entry("kmsg", S_IRUSR, &proc_root);
614 entry->proc_fops = &proc_kmsg_operations;
615 create_seq_entry("cpuinfo", 0, &proc_cpuinfo_operations);
616 create_seq_entry("partitions", 0, &proc_partitions_operations);
617 create_seq_entry("stat", 0, &proc_stat_operations);
618 create_seq_entry("interrupts", 0, &proc_interrupts_operations);
619 create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
620 create_seq_entry("buddyinfo",S_IRUGO, &fragmentation_file_operations);
621 create_seq_entry("vmstat",S_IRUGO, &proc_vmstat_file_operations);
622 create_seq_entry("diskstats", 0, &proc_diskstats_operations);
623 #ifdef CONFIG_MODULES
624 create_seq_entry("modules", 0, &proc_modules_operations);
626 #ifdef CONFIG_SCHEDSTATS
627 create_seq_entry("schedstat", 0, &proc_schedstat_operations);
629 #ifdef CONFIG_PROC_KCORE
630 proc_root_kcore = create_proc_entry("kcore", S_IRUSR, NULL);
631 if (proc_root_kcore) {
632 proc_root_kcore->proc_fops = &proc_kcore_operations;
633 proc_root_kcore->size =
634 (size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
637 #ifdef CONFIG_MAGIC_SYSRQ
638 entry = create_proc_entry("sysrq-trigger", S_IWUSR, NULL);
640 entry->proc_fops = &proc_sysrq_trigger_operations;
644 extern struct file_operations ppc_htab_operations;
645 entry = create_proc_entry("ppc_htab", S_IRUGO|S_IWUSR, NULL);
647 entry->proc_fops = &ppc_htab_operations;