2 * linux/fs/proc/array.c
4 * Copyright (C) 1992 by Linus Torvalds
5 * based on ideas by Darren Senn
8 * Michael. K. Johnson: stat,statm extensions.
9 * <johnsonm@stolaf.edu>
11 * Pauline Middelink : Made cmdline,envline only break at '\0's, to
12 * make sure SET_PROCTITLE works. Also removed
13 * bad '!' which forced address recalculation for
14 * EVERY character on the current page.
15 * <middelin@polyware.iaf.nl>
17 * Danny ter Haar : added cpuinfo
20 * Alessandro Rubini : profile extension.
21 * <rubini@ipvvis.unipv.it>
23 * Jeff Tranter : added BogoMips field to cpuinfo
24 * <Jeff_Tranter@Mitel.COM>
26 * Bruno Haible : remove 4K limit for the maps file
27 * <haible@ma2s2.mathematik.uni-karlsruhe.de>
29 * Yves Arrouye : remove removal of trailing spaces in get_array.
30 * <Yves.Arrouye@marin.fdn.fr>
32 * Jerome Forissier : added per-CPU time information to /proc/stat
33 * and /proc/<pid>/cpu extension
34 * <forissier@isia.cma.fr>
35 * - Incorporation and non-SMP safe operation
36 * of forissier patch in 2.1.78 by
37 * Hans Marcus <crowbar@concepts.nl>
39 * aeb@cwi.nl : /proc/partitions
42 * Alan Cox : security fixes.
43 * <Alan.Cox@linux.org>
45 * Al Viro : safe handling of mm_struct
47 * Gerhard Wichert : added BIGMEM support
48 * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
50 * Al Viro & Jeff Garzik : moved most of the thing into base.c and
51 * : proc_misc.c. The rest may eventually go into
55 #include <linux/config.h>
56 #include <linux/types.h>
57 #include <linux/errno.h>
58 #include <linux/sched.h>
59 #include <linux/kernel.h>
60 #include <linux/kernel_stat.h>
61 #include <linux/tty.h>
62 #include <linux/string.h>
63 #include <linux/mman.h>
64 #include <linux/proc_fs.h>
65 #include <linux/ioport.h>
67 #include <linux/pagemap.h>
68 #include <linux/swap.h>
69 #include <linux/slab.h>
70 #include <linux/smp.h>
71 #include <linux/signal.h>
72 #include <linux/highmem.h>
73 #include <linux/seq_file.h>
75 #include <asm/uaccess.h>
76 #include <asm/pgtable.h>
78 #include <asm/processor.h>
80 /* Gcc optimizes away "strlen(x)" for constant x */
81 #define ADDBUF(buffer, string) \
82 do { memcpy(buffer, string, strlen(string)); \
83 buffer += strlen(string); } while (0)
85 static inline char * task_name(struct task_struct *p, char * buf)
89 char tcomm[sizeof(p->comm)];
91 get_task_comm(tcomm, p);
93 ADDBUF(buf, "Name:\t");
97 unsigned char c = *name;
121 * The task state array is a strange "bitmap" of
122 * reasons to sleep. Thus "running" is zero, and
123 * you can test for combinations of others with
126 static const char *task_state_array[] = {
127 "R (running)", /* 0 */
128 "S (sleeping)", /* 1 */
129 "D (disk sleep)", /* 2 */
130 "Z (zombie)", /* 4 */
131 "T (stopped)", /* 8 */
132 "W (paging)" /* 16 */
135 static inline const char * get_task_state(struct task_struct *tsk)
137 unsigned int state = tsk->state & (TASK_RUNNING |
139 TASK_UNINTERRUPTIBLE |
142 const char **p = &task_state_array[0];
151 static inline char * task_state(struct task_struct *p, char *buffer)
155 read_lock(&tasklist_lock);
156 buffer += sprintf(buffer,
162 "Uid:\t%d\t%d\t%d\t%d\n"
163 "Gid:\t%d\t%d\t%d\t%d\n",
164 get_task_state(p), p->tgid,
165 p->pid, p->pid ? p->p_opptr->pid : 0, 0,
166 p->uid, p->euid, p->suid, p->fsuid,
167 p->gid, p->egid, p->sgid, p->fsgid);
168 read_unlock(&tasklist_lock);
170 buffer += sprintf(buffer,
173 p->files ? p->files->max_fds : 0);
176 for (g = 0; g < p->ngroups; g++)
177 buffer += sprintf(buffer, "%d ", p->groups[g]);
179 buffer += sprintf(buffer, "\n");
183 static inline char * task_mem(struct mm_struct *mm, char *buffer)
185 struct vm_area_struct * vma;
186 unsigned long data = 0, stack = 0;
187 unsigned long exec = 0, lib = 0;
189 down_read(&mm->mmap_sem);
190 for (vma = mm->mmap; vma; vma = vma->vm_next) {
191 unsigned long len = (vma->vm_end - vma->vm_start) >> 10;
194 if (vma->vm_flags & VM_GROWSDOWN)
198 if (vma->vm_flags & VM_WRITE)
200 if (vma->vm_flags & VM_EXEC) {
202 if (vma->vm_flags & VM_EXECUTABLE)
207 buffer += sprintf(buffer,
215 mm->total_vm << (PAGE_SHIFT-10),
216 mm->locked_vm << (PAGE_SHIFT-10),
217 mm->rss << (PAGE_SHIFT-10),
220 up_read(&mm->mmap_sem);
224 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
227 struct k_sigaction *k;
233 spin_lock_irq(&p->sigmask_lock);
237 for (i = 1; i <= _NSIG; ++i, ++k) {
238 if (k->sa.sa_handler == SIG_IGN)
240 else if (k->sa.sa_handler != SIG_DFL)
244 spin_unlock_irq(&p->sigmask_lock);
247 static inline char * task_sig(struct task_struct *p, char *buffer)
251 buffer += sprintf(buffer, "SigPnd:\t");
252 buffer = render_sigset_t(&p->pending.signal, buffer);
254 buffer += sprintf(buffer, "SigBlk:\t");
255 buffer = render_sigset_t(&p->blocked, buffer);
258 collect_sigign_sigcatch(p, &ign, &catch);
259 buffer += sprintf(buffer, "SigIgn:\t");
260 buffer = render_sigset_t(&ign, buffer);
262 buffer += sprintf(buffer, "SigCgt:\t"); /* Linux 2.0 uses "SigCgt" */
263 buffer = render_sigset_t(&catch, buffer);
269 static inline char *task_cap(struct task_struct *p, char *buffer)
271 return buffer + sprintf(buffer, "CapInh:\t%016x\n"
274 cap_t(p->cap_inheritable),
275 cap_t(p->cap_permitted),
276 cap_t(p->cap_effective));
280 int proc_pid_status(struct task_struct *task, char * buffer)
282 char * orig = buffer;
283 struct mm_struct *mm;
285 buffer = task_name(task, buffer);
286 buffer = task_state(task, buffer);
290 atomic_inc(&mm->mm_users);
293 buffer = task_mem(mm, buffer);
296 buffer = task_sig(task, buffer);
297 buffer = task_cap(task, buffer);
298 #if defined(CONFIG_ARCH_S390)
299 buffer = task_show_regs(task, buffer);
301 return buffer - orig;
304 int proc_pid_stat(struct task_struct *task, char * buffer)
306 unsigned long vsize, eip, esp, wchan;
308 int tty_pgrp = -1, tty_nr = 0;
309 sigset_t sigign, sigcatch;
313 struct mm_struct *mm;
314 char tcomm[sizeof(task->comm)];
316 state = *get_task_state(task);
317 vsize = eip = esp = 0;
321 atomic_inc(&mm->mm_users);
323 tty_pgrp = task->tty->pgrp;
324 tty_nr = kdev_t_to_nr(task->tty->device);
328 struct vm_area_struct *vma;
329 down_read(&mm->mmap_sem);
332 vsize += vma->vm_end - vma->vm_start;
335 eip = KSTK_EIP(task);
336 esp = KSTK_ESP(task);
337 up_read(&mm->mmap_sem);
340 get_task_comm(tcomm, task);
342 wchan = get_wchan(task);
344 collect_sigign_sigcatch(task, &sigign, &sigcatch);
346 /* scale priority and nice values from timeslices to -20..20 */
347 /* to make it look like a "normal" Unix priority/nice value */
348 priority = task->counter;
349 priority = 20 - (priority * 10 + DEF_COUNTER / 2) / DEF_COUNTER;
352 read_lock(&tasklist_lock);
353 ppid = task->pid ? task->p_opptr->pid : 0;
354 read_unlock(&tasklist_lock);
355 res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
356 %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu \
357 %lu %lu %lu %lu %lu %lu %lu %lu %d %d\n",
371 task->times.tms_utime,
372 task->times.tms_stime,
373 task->times.tms_cutime,
374 task->times.tms_cstime,
381 mm ? mm->rss : 0, /* you might want to shift this left 3 */
382 task->rlim[RLIMIT_RSS].rlim_cur,
383 mm ? mm->start_code : 0,
384 mm ? mm->end_code : 0,
385 mm ? mm->start_stack : 0,
388 /* The signal information here is obsolete.
389 * It must be decimal for Linux 2.0 compatibility.
390 * Use /proc/#/status for real-time signals.
392 task->pending.signal.sig[0] & 0x7fffffffUL,
393 task->blocked.sig[0] & 0x7fffffffUL,
394 sigign .sig[0] & 0x7fffffffUL,
395 sigcatch .sig[0] & 0x7fffffffUL,
406 static inline void statm_pte_range(pmd_t * pmd, unsigned long address, unsigned long size,
407 int * pages, int * shared, int * dirty, int * total)
419 pte = pte_offset(pmd, address);
420 address &= ~PMD_MASK;
421 end = address + size;
428 address += PAGE_SIZE;
433 if (!pte_present(page))
435 ptpage = pte_page(page);
436 if ((!VALID_PAGE(ptpage)) || PageReserved(ptpage))
441 if (page_count(pte_page(page)) > 1)
443 } while (address < end);
446 static inline void statm_pmd_range(pgd_t * pgd, unsigned long address, unsigned long size,
447 int * pages, int * shared, int * dirty, int * total)
459 pmd = pmd_offset(pgd, address);
460 address &= ~PGDIR_MASK;
461 end = address + size;
462 if (end > PGDIR_SIZE)
465 statm_pte_range(pmd, address, end - address, pages, shared, dirty, total);
466 address = (address + PMD_SIZE) & PMD_MASK;
468 } while (address < end);
471 static void statm_pgd_range(pgd_t * pgd, unsigned long address, unsigned long end,
472 int * pages, int * shared, int * dirty, int * total)
474 while (address < end) {
475 statm_pmd_range(pgd, address, end - address, pages, shared, dirty, total);
476 address = (address + PGDIR_SIZE) & PGDIR_MASK;
481 int proc_pid_statm(struct task_struct *task, char * buffer)
483 struct mm_struct *mm;
484 int size=0, resident=0, share=0, trs=0, lrs=0, drs=0, dt=0;
489 atomic_inc(&mm->mm_users);
492 struct vm_area_struct * vma;
493 down_read(&mm->mmap_sem);
496 pgd_t *pgd = pgd_offset(mm, vma->vm_start);
497 int pages = 0, shared = 0, dirty = 0, total = 0;
499 statm_pgd_range(pgd, vma->vm_start, vma->vm_end, &pages, &shared, &dirty, &total);
504 if (vma->vm_flags & VM_EXECUTABLE)
505 trs += pages; /* text */
506 else if (vma->vm_flags & VM_GROWSDOWN)
507 drs += pages; /* stack */
508 else if (vma->vm_end > 0x60000000)
509 lrs += pages; /* library */
514 up_read(&mm->mmap_sem);
517 return sprintf(buffer,"%d %d %d %d %d %d %d\n",
518 size, resident, share, trs, lrs, drs, dt);
521 static int show_map(struct seq_file *m, void *v)
523 struct vm_area_struct *map = v;
524 struct file *file = map->vm_file;
525 int flags = map->vm_flags;
526 unsigned long ino = 0;
531 struct inode *inode = map->vm_file->f_dentry->d_inode;
532 dev = kdev_t_to_nr(inode->i_sb->s_dev);
536 seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
539 flags & VM_READ ? 'r' : '-',
540 flags & VM_WRITE ? 'w' : '-',
541 flags & VM_EXEC ? 'x' : '-',
542 flags & VM_MAYSHARE ? 's' : 'p',
543 map->vm_pgoff << PAGE_SHIFT,
544 MAJOR(dev), MINOR(dev), ino, &len);
547 len = 25 + sizeof(void*) * 6 - len;
550 seq_printf(m, "%*c", len, ' ');
551 seq_path(m, file->f_vfsmnt, file->f_dentry, "");
557 static void *m_start(struct seq_file *m, loff_t *pos)
559 struct task_struct *task = m->private;
560 struct mm_struct *mm;
561 struct vm_area_struct * map;
567 atomic_inc(&mm->mm_users);
573 down_read(&mm->mmap_sem);
578 up_read(&mm->mmap_sem);
584 static void m_stop(struct seq_file *m, void *v)
586 struct vm_area_struct *map = v;
588 struct mm_struct *mm = map->vm_mm;
589 up_read(&mm->mmap_sem);
594 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
596 struct vm_area_struct *map = v;
604 struct seq_operations proc_pid_maps_op = {
612 int proc_pid_cpu(struct task_struct *task, char * buffer)
616 len = sprintf(buffer,
618 task->times.tms_utime,
619 task->times.tms_stime);
621 for (i = 0 ; i < smp_num_cpus; i++)
622 len += sprintf(buffer + len, "cpu%d %lu %lu\n",
624 task->per_cpu_utime[cpu_logical_map(i)],
625 task->per_cpu_stime[cpu_logical_map(i)]);