Re: Swap Files

Ion Badulescu (ionut@moisil.wal.rhno.columbia.edu)
Sun, 19 Jan 1997 17:33:50 -0500 (EST)


On Sun, 19 Jan 1997, Gerd Knorr wrote:

> Looks good:
>
> felix kraxel ~# cat /proc/swap
> Filename Type Size
> /dev/sdb3 partition 40956
> /dos/c/386spart.par file 16384
>
> Could be better justified (ok, just cosmetic), and would be nice to have
> a column with swap priority.

Done, and I also added a 'used' column, it's really interesting to see how
your swap partitions/files are being used. Patch follows..

BTW, I'd like to point out that if the priority is not specified, the
latest added swap partition/file has the lowest priority of all; also, the
implicit priorities are negative numbers whereas explicit priorities are
positive numbers, so if you _do_ specify a priority for a file/partition,
it will be higher than any implicit priority.

For example,

# swapon /dev/hda1
# swapon -p 1 /swapfile

will lead to /swapfile having a higher priority than /dev/hda1 and being
used first. Check /proc/swaps for further details. :-)

Ionut

--
  It is better to keep your mouth shut and be thought a fool,
            than to open it and remove all doubt.

--- linux-2.0.28.vanilla/fs/proc/root.c.old Tue Apr 30 06:09:45 1996 +++ linux/fs/proc/root.c Sat Jan 18 16:36:16 1997 @@ -366,6 +366,9 @@ proc_register( &proc_root, &(struct proc_dir_entry) { PROC_MTAB, 6, "mounts", S_IFREG | S_IRUGO, 1, 0, 0, } ); + proc_register( &proc_root, &(struct proc_dir_entry) + { PROC_SWAP, 5, "swaps", S_IFREG | S_IRUGO, 1, 0, 0, } ); + if (prof_shift) { proc_register(&proc_root, &(struct proc_dir_entry) { PROC_PROFILE, 7, "profile", --- linux-2.0.28.vanilla/fs/proc/array.c.old Tue Oct 29 20:42:41 1996 +++ linux/fs/proc/array.c Sat Jan 18 17:04:34 1997 @@ -976,6 +976,7 @@ extern int get_md_status (char *); extern int get_rtc_status (char *); extern int get_locks_status (char *); +extern int get_swaparea_info (char *); #ifdef __SMP_PROF__ extern int get_smp_prof_list(char *); #endif @@ -1046,6 +1047,9 @@ case PROC_MTAB: return get_filesystem_info( page ); + + case PROC_SWAP: + return get_swaparea_info(page); #ifdef CONFIG_RTC case PROC_RTC: return get_rtc_status(page); --- linux-2.0.28.vanilla/include/linux/swap.h.old Mon Jun 3 08:38:37 1996 +++ linux/include/linux/swap.h Sat Jan 18 15:04:28 1997 @@ -19,6 +19,7 @@ struct swap_info_struct { unsigned int flags; kdev_t swap_device; + char *swap_filename; struct inode * swap_file; unsigned char * swap_map; unsigned char * swap_lockmap; --- linux-2.0.28.vanilla/include/linux/proc_fs.h.old Sun Dec 1 12:59:28 1996 +++ linux/include/linux/proc_fs.h Sat Jan 18 18:24:17 1997 @@ -41,5 +41,6 @@ PROC_CMDLINE, PROC_SYS, PROC_MTAB, + PROC_SWAP, PROC_MD, PROC_RTC, --- linux-2.0.28.vanilla/mm/swapfile.c.old Sat Dec 14 07:24:31 1996 +++ linux/mm/swapfile.c Sun Jan 19 17:12:30 1997 @@ -16,6 +16,7 @@ #include <linux/swap.h> #include <linux/fs.h> #include <linux/swapctl.h> +#include <linux/malloc.h> #include <linux/blkdev.h> /* for blk_size */ #include <asm/dma.h> @@ -387,6 +388,8 @@ nr_swap_pages -= p->pages; iput(p->swap_file); + if (p->swap_filename) + kfree(p->swap_filename); p->swap_file = NULL; p->swap_device = 0; vfree(p->swap_map); @@ -397,6 +400,37 @@ return 0; } +int get_swaparea_info(char *buf) +{ + struct swap_info_struct *ptr = swap_info; + int i, j, len = 0, usedswap; + + len += sprintf(buf, "Filename\t\t\tType\t\tSize\tUsed\tPriority\n"); + for (i = 0 ; i < nr_swapfiles ; i++, ptr++) + if (ptr->flags & SWP_USED) { + if (ptr->swap_filename) + len += sprintf(buf + len, "%-31s ", ptr->swap_filename); + else + len += sprintf(buf + len, "(null)\t\t\t"); + if (ptr->swap_file) + len += sprintf(buf + len, "file\t\t"); + else + len += sprintf(buf + len, "partition\t"); + usedswap = 0; + for (j = 0; j < ptr->max; ++j) + switch (ptr->swap_map[j]) { + case 128: + case 0: + continue; + default: + usedswap++; + } + len += sprintf(buf + len, "%d\t%d\t%d\n", ptr->pages << (PAGE_SHIFT - 10), + usedswap << (PAGE_SHIFT - 10), ptr->prio); + } + return len; +} + /* * Written 01/25/92 by Simmule Turner, heavily changed by Linus. * @@ -409,6 +443,7 @@ unsigned int type; int i, j, prev; int error; + char *tmp; struct file filp; static int least_priority = 0; @@ -424,6 +459,7 @@ if (type >= nr_swapfiles) nr_swapfiles = type+1; p->flags = SWP_USED; + p->swap_filename = NULL; p->swap_file = NULL; p->swap_device = 0; p->swap_map = NULL; @@ -530,6 +566,12 @@ prev = i; } p->next = i; + if (!getname(specialfile, &tmp)) { + if ((p->swap_filename = + (char *) kmalloc(strlen(tmp)+1, GFP_KERNEL)) != (char *)NULL) + strcpy(p->swap_filename, tmp); + putname(tmp); + } if (prev < 0) { swap_list.head = swap_list.next = p - swap_info; } else {