ia64/xen-unstable

changeset 6358:5f4724c13040

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Aug 23 08:40:58 2005 +0000 (2005-08-23)
parents 2d3a7be68ba3
children d13f5c7daddf
files .hgignore Config.mk Makefile buildconfigs/Rules.mk docs/src/user.tex extras/mini-os/include/time.h extras/mini-os/kernel.c extras/mini-os/time.c linux-2.6-xen-sparse/arch/xen/Kconfig linux-2.6-xen-sparse/arch/xen/configs/xen0_defconfig_x86_32 linux-2.6-xen-sparse/arch/xen/configs/xen0_defconfig_x86_64 linux-2.6-xen-sparse/arch/xen/configs/xenU_defconfig_x86_32 linux-2.6-xen-sparse/arch/xen/configs/xenU_defconfig_x86_64 linux-2.6-xen-sparse/arch/xen/configs/xen_defconfig_x86_32 linux-2.6-xen-sparse/arch/xen/configs/xen_defconfig_x86_64 linux-2.6-xen-sparse/arch/xen/i386/Kconfig linux-2.6-xen-sparse/arch/xen/i386/kernel/Makefile linux-2.6-xen-sparse/arch/xen/i386/kernel/cpu/common.c linux-2.6-xen-sparse/arch/xen/i386/kernel/head.S linux-2.6-xen-sparse/arch/xen/i386/kernel/i386_ksyms.c linux-2.6-xen-sparse/arch/xen/i386/kernel/ioport.c linux-2.6-xen-sparse/arch/xen/i386/kernel/ldt.c linux-2.6-xen-sparse/arch/xen/i386/kernel/pci-dma.c linux-2.6-xen-sparse/arch/xen/i386/kernel/process.c linux-2.6-xen-sparse/arch/xen/i386/kernel/setup.c linux-2.6-xen-sparse/arch/xen/i386/kernel/smp.c linux-2.6-xen-sparse/arch/xen/i386/kernel/smpboot.c linux-2.6-xen-sparse/arch/xen/i386/kernel/time.c linux-2.6-xen-sparse/arch/xen/i386/mm/fault.c linux-2.6-xen-sparse/arch/xen/i386/mm/highmem.c linux-2.6-xen-sparse/arch/xen/i386/mm/hypervisor.c linux-2.6-xen-sparse/arch/xen/i386/mm/init.c linux-2.6-xen-sparse/arch/xen/i386/mm/ioremap.c linux-2.6-xen-sparse/arch/xen/i386/mm/pgtable.c linux-2.6-xen-sparse/arch/xen/i386/pci/Makefile linux-2.6-xen-sparse/arch/xen/kernel/evtchn.c linux-2.6-xen-sparse/arch/xen/kernel/reboot.c linux-2.6-xen-sparse/arch/xen/kernel/skbuff.c linux-2.6-xen-sparse/arch/xen/x86_64/Kconfig linux-2.6-xen-sparse/arch/xen/x86_64/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/ia32/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/kernel/Makefile linux-2.6-xen-sparse/arch/xen/x86_64/kernel/apic.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/e820.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/entry.S linux-2.6-xen-sparse/arch/xen/x86_64/kernel/pci-nommu.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/process.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/setup.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/setup64.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/smpboot.c linux-2.6-xen-sparse/arch/xen/x86_64/kernel/vsyscall.c linux-2.6-xen-sparse/arch/xen/x86_64/mm/fault.c linux-2.6-xen-sparse/arch/xen/x86_64/mm/init.c linux-2.6-xen-sparse/arch/xen/x86_64/mm/ioremap.c linux-2.6-xen-sparse/arch/xen/x86_64/pci/Makefile linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c linux-2.6-xen-sparse/drivers/xen/blkback/Makefile linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c linux-2.6-xen-sparse/drivers/xen/blkback/common.h linux-2.6-xen-sparse/drivers/xen/blkback/interface.c linux-2.6-xen-sparse/drivers/xen/blkback/vbd.c linux-2.6-xen-sparse/drivers/xen/blkfront/block.h linux-2.6-xen-sparse/drivers/xen/blkfront/vbd.c linux-2.6-xen-sparse/drivers/xen/blktap/blktap.c linux-2.6-xen-sparse/drivers/xen/blktap/blktap.h linux-2.6-xen-sparse/drivers/xen/blktap/blktap_controlmsg.c linux-2.6-xen-sparse/drivers/xen/blktap/blktap_userdev.c linux-2.6-xen-sparse/drivers/xen/console/console.c linux-2.6-xen-sparse/drivers/xen/netback/common.h linux-2.6-xen-sparse/drivers/xen/netback/interface.c linux-2.6-xen-sparse/drivers/xen/netback/netback.c linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c linux-2.6-xen-sparse/drivers/xen/privcmd/privcmd.c linux-2.6-xen-sparse/drivers/xen/usbback/usbback.c linux-2.6-xen-sparse/drivers/xen/usbfront/usbfront.c linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_comms.c linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_comms.h linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_xs.c linux-2.6-xen-sparse/include/asm-xen/asm-i386/desc.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/dma-mapping.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/fixmap.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/hypercall.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/mmu_context.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/page.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pci.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgalloc.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgtable-2level.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgtable-3level.h linux-2.6-xen-sparse/include/asm-xen/asm-i386/pgtable.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/desc.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/dma-mapping.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/hypercall.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/page.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pci.h linux-2.6-xen-sparse/include/asm-xen/asm-x86_64/pgtable.h linux-2.6-xen-sparse/include/asm-xen/balloon.h linux-2.6-xen-sparse/include/asm-xen/hypervisor.h linux-2.6-xen-sparse/include/asm-xen/xenbus.h linux-2.6-xen-sparse/include/linux/highmem.h linux-2.6-xen-sparse/include/linux/mm.h linux-2.6-xen-sparse/mm/highmem.c linux-2.6-xen-sparse/mm/memory.c tools/Makefile tools/Rules.mk tools/console/daemon/io.c tools/debugger/libxendebug/xendebug.c tools/debugger/libxendebug/xendebug.h tools/debugger/pdb/Domain.ml tools/debugger/pdb/Domain.mli tools/debugger/pdb/Makefile tools/debugger/pdb/PDB.ml tools/debugger/pdb/Process.ml tools/debugger/pdb/Process.mli tools/debugger/pdb/debugger.ml tools/debugger/pdb/linux-2.6-module/debug.c tools/debugger/pdb/linux-2.6-module/module.c tools/debugger/pdb/linux-2.6-module/pdb_debug.h tools/debugger/pdb/linux-2.6-module/pdb_module.h tools/debugger/pdb/linux-2.6-patches/i386_ksyms.patch tools/debugger/pdb/pdb_caml_domain.c tools/debugger/pdb/pdb_caml_process.c tools/debugger/pdb/pdb_caml_xcs.c tools/debugger/pdb/readme tools/examples/Makefile tools/examples/xend-config.sxp tools/examples/xmexample.vmx tools/ioemu/hw/ide.c tools/libxc/Makefile tools/libxc/xc.h tools/libxc/xc_core.c tools/libxc/xc_gnttab.c tools/libxc/xc_linux_build.c tools/libxc/xc_linux_restore.c tools/libxc/xc_linux_save.c tools/libxc/xc_load_elf.c tools/libxc/xc_ptrace.c tools/libxc/xc_vmx_build.c tools/misc/xend tools/python/setup.py tools/python/xen/lowlevel/xu/xu.c tools/python/xen/xend/XendDomain.py tools/python/xen/xend/XendDomainInfo.py tools/python/xen/xend/XendRoot.py tools/python/xen/xend/image.py tools/python/xen/xend/server/blkif.py tools/python/xen/xend/server/controller.py tools/python/xen/xend/server/event.py tools/python/xen/xend/server/netif.py tools/python/xen/xend/server/relocate.py tools/python/xen/xm/create.py tools/python/xen/xm/main.py tools/security/Makefile tools/security/secpol_tool.c tools/xenstat/Makefile tools/xenstat/libxenstat/COPYING tools/xenstat/libxenstat/Makefile tools/xenstat/libxenstat/bindings/swig/perl/.empty tools/xenstat/libxenstat/bindings/swig/python/.empty tools/xenstat/libxenstat/bindings/swig/xenstat.i tools/xenstat/libxenstat/src/xen-interface.c tools/xenstat/libxenstat/src/xen-interface.h tools/xenstat/libxenstat/src/xenstat.c tools/xenstat/libxenstat/src/xenstat.h tools/xenstat/xentop/Makefile tools/xenstat/xentop/TODO tools/xenstat/xentop/xentop.1 tools/xenstat/xentop/xentop.c tools/xenstore/xenstored.h tools/xenstore/xenstored_core.c tools/xentrace/xentrace.c xen/Rules.mk xen/arch/ia64/grant_table.c xen/arch/ia64/xenmisc.c xen/arch/ia64/xentime.c xen/arch/x86/domain.c xen/arch/x86/domain_build.c xen/arch/x86/mm.c xen/arch/x86/physdev.c xen/arch/x86/setup.c xen/arch/x86/shadow.c xen/arch/x86/shadow32.c xen/arch/x86/time.c xen/arch/x86/traps.c xen/arch/x86/vmx.c xen/arch/x86/x86_32/traps.c xen/arch/x86/x86_64/traps.c xen/common/domain.c xen/common/event_channel.c xen/common/grant_table.c xen/common/lib.c xen/common/perfc.c xen/common/schedule.c xen/common/trace.c xen/drivers/char/console.c xen/include/asm-x86/e820.h xen/include/asm-x86/mm.h xen/include/asm-x86/page.h xen/include/asm-x86/shadow.h xen/include/asm-x86/uaccess.h xen/include/asm-x86/vmx_vmcs.h xen/include/asm-x86/x86_32/page-3level.h xen/include/asm-x86/x86_32/uaccess.h xen/include/public/arch-ia64.h xen/include/public/arch-x86_32.h xen/include/public/arch-x86_64.h xen/include/public/dom0_ops.h xen/include/public/grant_table.h xen/include/public/io/blkif.h xen/include/public/io/domain_controller.h xen/include/public/io/netif.h xen/include/public/physdev.h xen/include/public/xen.h xen/include/xen/grant_table.h xen/include/xen/sched.h xen/include/xen/time.h
line diff
     1.1 --- a/Config.mk	Tue Aug 23 08:40:50 2005 +0000
     1.2 +++ b/Config.mk	Tue Aug 23 08:40:58 2005 +0000
     1.3 @@ -14,6 +14,7 @@ LD         = $(CROSS_COMPILE)ld
     1.4  CC         = $(CROSS_COMPILE)gcc
     1.5  CPP        = $(CROSS_COMPILE)gcc -E
     1.6  AR         = $(CROSS_COMPILE)ar
     1.7 +RANLIB     = $(CROSS_COMPILE)ranlib
     1.8  NM         = $(CROSS_COMPILE)nm
     1.9  STRIP      = $(CROSS_COMPILE)strip
    1.10  OBJCOPY    = $(CROSS_COMPILE)objcopy
    1.11 @@ -43,3 +44,7 @@ KERNEL_REPO = http://www.kernel.org
    1.12  #	ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY
    1.13  #	ACM_CHINESE_WALL_AND_SIMPLE_TYPE_ENFORCEMENT_POLICY
    1.14  ACM_USE_SECURITY_POLICY ?= ACM_NULL_POLICY
    1.15 +
    1.16 +# Optional components
    1.17 +XENSTAT_XENTOP ?= y
    1.18 +
     2.1 --- a/linux-2.6-xen-sparse/arch/xen/i386/mm/hypervisor.c	Tue Aug 23 08:40:50 2005 +0000
     2.2 +++ b/linux-2.6-xen-sparse/arch/xen/i386/mm/hypervisor.c	Tue Aug 23 08:40:58 2005 +0000
     2.3 @@ -405,54 +405,6 @@ void xen_destroy_contiguous_region(unsig
     2.4  	balloon_unlock(flags);
     2.5  }
     2.6  
     2.7 -
     2.8 -unsigned long allocate_empty_lowmem_region(unsigned long pages)
     2.9 -{
    2.10 -	pgd_t         *pgd;
    2.11 -	pud_t         *pud; 
    2.12 -	pmd_t         *pmd;
    2.13 -	pte_t         *pte;
    2.14 -	unsigned long *pfn_array;
    2.15 -	unsigned long  vstart;
    2.16 -	unsigned long  i;
    2.17 -	unsigned int   order = get_order(pages*PAGE_SIZE);
    2.18 -
    2.19 -	vstart = __get_free_pages(GFP_KERNEL, order);
    2.20 -	if (vstart == 0)
    2.21 -		return 0UL;
    2.22 -
    2.23 -	scrub_pages(vstart, 1 << order);
    2.24 -
    2.25 -	pfn_array = vmalloc((1<<order) * sizeof(*pfn_array));
    2.26 -	BUG_ON(pfn_array == NULL);
    2.27 -
    2.28 -	for (i = 0; i < (1<<order); i++) {
    2.29 -		pgd = pgd_offset_k(   (vstart + (i*PAGE_SIZE)));
    2.30 -		pud = pud_offset(pgd, (vstart + (i*PAGE_SIZE)));
    2.31 -		pmd = pmd_offset(pud, (vstart + (i*PAGE_SIZE)));
    2.32 -		pte = pte_offset_kernel(pmd, (vstart + (i*PAGE_SIZE))); 
    2.33 -		pfn_array[i] = pte_mfn(*pte);
    2.34 -#ifdef CONFIG_X86_64
    2.35 -		xen_l1_entry_update(pte, __pte(0));
    2.36 -#else
    2.37 -		BUG_ON(HYPERVISOR_update_va_mapping(vstart + (i*PAGE_SIZE), 
    2.38 -						    __pte_ma(0), 0));
    2.39 -#endif
    2.40 -		phys_to_machine_mapping[(__pa(vstart)>>PAGE_SHIFT)+i] =
    2.41 -			INVALID_P2M_ENTRY;
    2.42 -	}
    2.43 -
    2.44 -	flush_tlb_all();
    2.45 -
    2.46 -	balloon_put_pages(pfn_array, 1 << order);
    2.47 -
    2.48 -	vfree(pfn_array);
    2.49 -
    2.50 -	return vstart;
    2.51 -}
    2.52 -
    2.53 -EXPORT_SYMBOL(allocate_empty_lowmem_region);
    2.54 -
    2.55  /*
    2.56   * Local variables:
    2.57   *  c-file-style: "linux"
     3.1 --- a/linux-2.6-xen-sparse/arch/xen/i386/mm/ioremap.c	Tue Aug 23 08:40:50 2005 +0000
     3.2 +++ b/linux-2.6-xen-sparse/arch/xen/i386/mm/ioremap.c	Tue Aug 23 08:40:58 2005 +0000
     3.3 @@ -368,35 +368,37 @@ int direct_remap_area_pages(struct mm_st
     3.4  
     3.5  EXPORT_SYMBOL(direct_remap_area_pages);
     3.6  
     3.7 +static int lookup_pte_fn(
     3.8 +	pte_t *pte, struct page *pte_page, unsigned long addr, void *data)
     3.9 +{
    3.10 +	unsigned long *ptep = (unsigned long *)data;
    3.11 +	if (ptep)
    3.12 +		*ptep = (pfn_to_mfn(page_to_pfn(pte_page)) <<
    3.13 +			 PAGE_SHIFT) |
    3.14 +			((unsigned long)pte & ~PAGE_MASK);
    3.15 +	return 0;
    3.16 +}
    3.17 +
    3.18  int create_lookup_pte_addr(struct mm_struct *mm, 
    3.19  			   unsigned long address,
    3.20  			   unsigned long *ptep)
    3.21  {
    3.22 -	int f(pte_t *pte, struct page *pte_page, unsigned long addr,
    3.23 -	      void *data) {
    3.24 -		unsigned long *ptep = (unsigned long *)data;
    3.25 -		if (ptep)
    3.26 -			*ptep = (pfn_to_mfn(page_to_pfn(pte_page)) <<
    3.27 -				 PAGE_SHIFT) |
    3.28 -				((unsigned long)pte & ~PAGE_MASK);
    3.29 -		return 0;
    3.30 -	}
    3.31 -
    3.32 -	return generic_page_range(mm, address, PAGE_SIZE, f, ptep);
    3.33 +	return generic_page_range(mm, address, PAGE_SIZE, lookup_pte_fn, ptep);
    3.34  }
    3.35  
    3.36  EXPORT_SYMBOL(create_lookup_pte_addr);
    3.37  
    3.38 +static int noop_fn(
    3.39 +	pte_t *pte, struct page *pte_page, unsigned long addr, void *data)
    3.40 +{
    3.41 +	return 0;
    3.42 +}
    3.43 +
    3.44  int touch_pte_range(struct mm_struct *mm,
    3.45  		    unsigned long address,
    3.46  		    unsigned long size)
    3.47  {
    3.48 -	int f(pte_t *pte, struct page *pte_page, unsigned long addr,
    3.49 -	      void *data) {
    3.50 -		return 0;
    3.51 -	}
    3.52 -
    3.53 -	return generic_page_range(mm, address, size, f, NULL);
    3.54 +	return generic_page_range(mm, address, size, noop_fn, NULL);
    3.55  } 
    3.56  
    3.57  EXPORT_SYMBOL(touch_pte_range);
     4.1 --- a/linux-2.6-xen-sparse/arch/xen/x86_64/mm/ioremap.c	Tue Aug 23 08:40:50 2005 +0000
     4.2 +++ b/linux-2.6-xen-sparse/arch/xen/x86_64/mm/ioremap.c	Tue Aug 23 08:40:58 2005 +0000
     4.3 @@ -465,33 +465,35 @@ int direct_remap_area_pages(struct mm_st
     4.4  
     4.5  EXPORT_SYMBOL(direct_remap_area_pages);
     4.6  
     4.7 +static int lookup_pte_fn(
     4.8 +    pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
     4.9 +{
    4.10 +    unsigned long *ptep = (unsigned long *)data;
    4.11 +    if (ptep) *ptep = (pfn_to_mfn(page_to_pfn(pte_page)) << PAGE_SHIFT)
    4.12 +                  | ((unsigned long)pte & ~PAGE_MASK);
    4.13 +    return 0;
    4.14 +}
    4.15 +
    4.16  int create_lookup_pte_addr(struct mm_struct *mm, 
    4.17                             unsigned long address,
    4.18                             unsigned long *ptep)
    4.19  {
    4.20 -    int f(pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
    4.21 -    {
    4.22 -        unsigned long *ptep = (unsigned long *)data;
    4.23 -        if (ptep) *ptep = (pfn_to_mfn(page_to_pfn(pte_page)) << PAGE_SHIFT)
    4.24 -                       | ((unsigned long)pte & ~PAGE_MASK);
    4.25 -        return 0;
    4.26 -    }
    4.27 -
    4.28 -    return generic_page_range(mm, address, PAGE_SIZE, f, ptep);
    4.29 +    return generic_page_range(mm, address, PAGE_SIZE, lookup_pte_fn, ptep);
    4.30  }
    4.31  
    4.32  EXPORT_SYMBOL(create_lookup_pte_addr);
    4.33  
    4.34 +static int noop_fn(
    4.35 +    pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
    4.36 +{
    4.37 +    return 0;
    4.38 +}
    4.39 +
    4.40  int touch_pte_range(struct mm_struct *mm,
    4.41                      unsigned long address,
    4.42                      unsigned long size)
    4.43  {
    4.44 -    int f(pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
    4.45 -    {
    4.46 -        return 0;
    4.47 -    }
    4.48 -
    4.49 -    return generic_page_range(mm, address, size, f, NULL);
    4.50 -}                 
    4.51 +    return generic_page_range(mm, address, size, noop_fn, NULL);
    4.52 +}
    4.53  
    4.54  EXPORT_SYMBOL(touch_pte_range);
     5.1 --- a/linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c	Tue Aug 23 08:40:50 2005 +0000
     5.2 +++ b/linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c	Tue Aug 23 08:40:58 2005 +0000
     5.3 @@ -83,12 +83,15 @@ static struct timer_list balloon_timer;
     5.4  
     5.5  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
     5.6  /* Use the private and mapping fields of struct page as a list. */
     5.7 -#define PAGE_TO_LIST(p) ( (struct list_head *)&p->private )
     5.8 -#define LIST_TO_PAGE(l) ( list_entry( ((unsigned long *)l),   \
     5.9 -                                      struct page, private ) )
    5.10 -#define UNLIST_PAGE(p)  do { list_del(PAGE_TO_LIST(p));       \
    5.11 -                             p->mapping = NULL;               \
    5.12 -                             p->private = 0; } while(0)
    5.13 +#define PAGE_TO_LIST(p) ((struct list_head *)&p->private)
    5.14 +#define LIST_TO_PAGE(l)				\
    5.15 +	(list_entry(((unsigned long *)l), struct page, private))
    5.16 +#define UNLIST_PAGE(p)				\
    5.17 +	do {					\
    5.18 +		list_del(PAGE_TO_LIST(p));	\
    5.19 +		p->mapping = NULL;		\
    5.20 +		p->private = 0;			\
    5.21 +	} while(0)
    5.22  #else
    5.23  /* There's a dedicated list field in struct page we can use.    */
    5.24  #define PAGE_TO_LIST(p) ( &p->list )
    5.25 @@ -104,56 +107,53 @@ static struct timer_list balloon_timer;
    5.26  #endif
    5.27  
    5.28  #define IPRINTK(fmt, args...) \
    5.29 -    printk(KERN_INFO "xen_mem: " fmt, ##args)
    5.30 +	printk(KERN_INFO "xen_mem: " fmt, ##args)
    5.31  #define WPRINTK(fmt, args...) \
    5.32 -    printk(KERN_WARNING "xen_mem: " fmt, ##args)
    5.33 +	printk(KERN_WARNING "xen_mem: " fmt, ##args)
    5.34  
    5.35  /* balloon_append: add the given page to the balloon. */
    5.36  static void balloon_append(struct page *page)
    5.37  {
    5.38 -    /* Low memory is re-populated first, so highmem pages go at list tail. */
    5.39 -    if ( PageHighMem(page) )
    5.40 -    {
    5.41 -        list_add_tail(PAGE_TO_LIST(page), &ballooned_pages);
    5.42 -        balloon_high++;
    5.43 -    }
    5.44 -    else
    5.45 -    {
    5.46 -        list_add(PAGE_TO_LIST(page), &ballooned_pages);
    5.47 -        balloon_low++;
    5.48 -    }
    5.49 +	/* Lowmem is re-populated first, so highmem pages go at list tail. */
    5.50 +	if (PageHighMem(page)) {
    5.51 +		list_add_tail(PAGE_TO_LIST(page), &ballooned_pages);
    5.52 +		balloon_high++;
    5.53 +	} else {
    5.54 +		list_add(PAGE_TO_LIST(page), &ballooned_pages);
    5.55 +		balloon_low++;
    5.56 +	}
    5.57  }
    5.58  
    5.59  /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
    5.60  static struct page *balloon_retrieve(void)
    5.61  {
    5.62 -    struct page *page;
    5.63 -
    5.64 -    if ( list_empty(&ballooned_pages) )
    5.65 -        return NULL;
    5.66 +	struct page *page;
    5.67  
    5.68 -    page = LIST_TO_PAGE(ballooned_pages.next);
    5.69 -    UNLIST_PAGE(page);
    5.70 +	if (list_empty(&ballooned_pages))
    5.71 +		return NULL;
    5.72  
    5.73 -    if ( PageHighMem(page) )
    5.74 -        balloon_high--;
    5.75 -    else
    5.76 -        balloon_low--;
    5.77 +	page = LIST_TO_PAGE(ballooned_pages.next);
    5.78 +	UNLIST_PAGE(page);
    5.79  
    5.80 -    return page;
    5.81 +	if (PageHighMem(page))
    5.82 +		balloon_high--;
    5.83 +	else
    5.84 +		balloon_low--;
    5.85 +
    5.86 +	return page;
    5.87  }
    5.88  
    5.89  static void balloon_alarm(unsigned long unused)
    5.90  {
    5.91 -    schedule_work(&balloon_worker);
    5.92 +	schedule_work(&balloon_worker);
    5.93  }
    5.94  
    5.95  static unsigned long current_target(void)
    5.96  {
    5.97 -    unsigned long target = min(target_pages, hard_limit);
    5.98 -    if ( target > (current_pages + balloon_low + balloon_high) )
    5.99 -        target = current_pages + balloon_low + balloon_high;
   5.100 -    return target;
   5.101 +	unsigned long target = min(target_pages, hard_limit);
   5.102 +	if (target > (current_pages + balloon_low + balloon_high))
   5.103 +		target = current_pages + balloon_low + balloon_high;
   5.104 +	return target;
   5.105  }
   5.106  
   5.107  /*
   5.108 @@ -164,161 +164,147 @@ static unsigned long current_target(void
   5.109   */
   5.110  static void balloon_process(void *unused)
   5.111  {
   5.112 -    unsigned long *mfn_list, pfn, i, flags;
   5.113 -    struct page   *page;
   5.114 -    long           credit, debt, rc;
   5.115 -    void          *v;
   5.116 +	unsigned long *mfn_list, pfn, i, flags;
   5.117 +	struct page   *page;
   5.118 +	long           credit, debt, rc;
   5.119 +	void          *v;
   5.120  
   5.121 -    down(&balloon_mutex);
   5.122 +	down(&balloon_mutex);
   5.123  
   5.124   retry:
   5.125 -    mfn_list = NULL;
   5.126 -
   5.127 -    if ( (credit = current_target() - current_pages) > 0 )
   5.128 -    {
   5.129 -        mfn_list = (unsigned long *)vmalloc(credit * sizeof(*mfn_list));
   5.130 -        if ( mfn_list == NULL )
   5.131 -            goto out;
   5.132 -
   5.133 -        balloon_lock(flags);
   5.134 -        rc = HYPERVISOR_dom_mem_op(
   5.135 -            MEMOP_increase_reservation, mfn_list, credit, 0);
   5.136 -        balloon_unlock(flags);
   5.137 -        if ( rc < credit )
   5.138 -        {
   5.139 -            /* We hit the Xen hard limit: reprobe. */
   5.140 -            if ( HYPERVISOR_dom_mem_op(
   5.141 -                MEMOP_decrease_reservation, mfn_list, rc, 0) != rc )
   5.142 -                BUG();
   5.143 -            hard_limit = current_pages + rc - driver_pages;
   5.144 -            vfree(mfn_list);
   5.145 -            goto retry;
   5.146 -        }
   5.147 -
   5.148 -        for ( i = 0; i < credit; i++ )
   5.149 -        {
   5.150 -            if ( (page = balloon_retrieve()) == NULL )
   5.151 -                BUG();
   5.152 -
   5.153 -            pfn = page - mem_map;
   5.154 -            if ( phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY )
   5.155 -                BUG();
   5.156 -
   5.157 -            /* Update P->M and M->P tables. */
   5.158 -            phys_to_machine_mapping[pfn] = mfn_list[i];
   5.159 -            xen_machphys_update(mfn_list[i], pfn);
   5.160 -            
   5.161 -            /* Link back into the page tables if it's not a highmem page. */
   5.162 -            if ( pfn < max_low_pfn )
   5.163 -            {
   5.164 -                BUG_ON(HYPERVISOR_update_va_mapping(
   5.165 -                    (unsigned long)__va(pfn << PAGE_SHIFT),
   5.166 -                    pfn_pte_ma(mfn_list[i], PAGE_KERNEL), 0));
   5.167 -            }
   5.168 -
   5.169 -            /* Finally, relinquish the memory back to the system allocator. */
   5.170 -            ClearPageReserved(page);
   5.171 -            set_page_count(page, 1);
   5.172 -            __free_page(page);
   5.173 -        }
   5.174 +	mfn_list = NULL;
   5.175  
   5.176 -        current_pages += credit;
   5.177 -    }
   5.178 -    else if ( credit < 0 )
   5.179 -    {
   5.180 -        debt = -credit;
   5.181 -
   5.182 -        mfn_list = (unsigned long *)vmalloc(debt * sizeof(*mfn_list));
   5.183 -        if ( mfn_list == NULL )
   5.184 -            goto out;
   5.185 -
   5.186 -        for ( i = 0; i < debt; i++ )
   5.187 -        {
   5.188 -            if ( (page = alloc_page(GFP_HIGHUSER)) == NULL )
   5.189 -            {
   5.190 -                debt = i;
   5.191 -                break;
   5.192 -            }
   5.193 -
   5.194 -            pfn = page - mem_map;
   5.195 -            mfn_list[i] = phys_to_machine_mapping[pfn];
   5.196 +	if ((credit = current_target() - current_pages) > 0) {
   5.197 +		mfn_list = vmalloc(credit * sizeof(*mfn_list));
   5.198 +		if (mfn_list == NULL)
   5.199 +			goto out;
   5.200  
   5.201 -            if ( !PageHighMem(page) )
   5.202 -            {
   5.203 -                v = phys_to_virt(pfn << PAGE_SHIFT);
   5.204 -                scrub_pages(v, 1);
   5.205 -                BUG_ON(HYPERVISOR_update_va_mapping(
   5.206 -                    (unsigned long)v, __pte_ma(0), 0));
   5.207 -            }
   5.208 -#ifdef CONFIG_XEN_SCRUB_PAGES
   5.209 -            else
   5.210 -            {
   5.211 -                v = kmap(page);
   5.212 -                scrub_pages(v, 1);
   5.213 -                kunmap(page);
   5.214 -            }
   5.215 -#endif
   5.216 -        }
   5.217 +		balloon_lock(flags);
   5.218 +		rc = HYPERVISOR_dom_mem_op(
   5.219 +			MEMOP_increase_reservation, mfn_list, credit, 0);
   5.220 +		balloon_unlock(flags);
   5.221 +		if (rc < credit) {
   5.222 +			/* We hit the Xen hard limit: reprobe. */
   5.223 +			BUG_ON(HYPERVISOR_dom_mem_op(
   5.224 +				MEMOP_decrease_reservation,
   5.225 +				mfn_list, rc, 0) != rc);
   5.226 +			hard_limit = current_pages + rc - driver_pages;
   5.227 +			vfree(mfn_list);
   5.228 +			goto retry;
   5.229 +		}
   5.230  
   5.231 -        /* Ensure that ballooned highmem pages don't have cached mappings. */
   5.232 -        kmap_flush_unused();
   5.233 -        flush_tlb_all();
   5.234 +		for (i = 0; i < credit; i++) {
   5.235 +			page = balloon_retrieve();
   5.236 +			BUG_ON(page == NULL);
   5.237  
   5.238 -        /* No more mappings: invalidate pages in P2M and add to balloon. */
   5.239 -        for ( i = 0; i < debt; i++ )
   5.240 -        {
   5.241 -            pfn = mfn_to_pfn(mfn_list[i]);
   5.242 -            phys_to_machine_mapping[pfn] = INVALID_P2M_ENTRY;
   5.243 -            balloon_append(pfn_to_page(pfn));
   5.244 -        }
   5.245 +			pfn = page - mem_map;
   5.246 +			if (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY)
   5.247 +				BUG();
   5.248  
   5.249 -        if ( HYPERVISOR_dom_mem_op(
   5.250 -            MEMOP_decrease_reservation, mfn_list, debt, 0) != debt )
   5.251 -            BUG();
   5.252 +			/* Update P->M and M->P tables. */
   5.253 +			phys_to_machine_mapping[pfn] = mfn_list[i];
   5.254 +			xen_machphys_update(mfn_list[i], pfn);
   5.255 +            
   5.256 +			/* Link back into the page tables if not highmem. */
   5.257 +			if (pfn < max_low_pfn)
   5.258 +				BUG_ON(HYPERVISOR_update_va_mapping(
   5.259 +					(unsigned long)__va(pfn << PAGE_SHIFT),
   5.260 +					pfn_pte_ma(mfn_list[i], PAGE_KERNEL),
   5.261 +					0));
   5.262  
   5.263 -        current_pages -= debt;
   5.264 -    }
   5.265 +			/* Relinquish the page back to the allocator. */
   5.266 +			ClearPageReserved(page);
   5.267 +			set_page_count(page, 1);
   5.268 +			__free_page(page);
   5.269 +		}
   5.270 +
   5.271 +		current_pages += credit;
   5.272 +	} else if (credit < 0) {
   5.273 +		debt = -credit;
   5.274 +
   5.275 +		mfn_list = vmalloc(debt * sizeof(*mfn_list));
   5.276 +		if (mfn_list == NULL)
   5.277 +			goto out;
   5.278 +
   5.279 +		for (i = 0; i < debt; i++) {
   5.280 +			if ((page = alloc_page(GFP_HIGHUSER)) == NULL) {
   5.281 +				debt = i;
   5.282 +				break;
   5.283 +			}
   5.284 +
   5.285 +			pfn = page - mem_map;
   5.286 +			mfn_list[i] = phys_to_machine_mapping[pfn];
   5.287 +
   5.288 +			if (!PageHighMem(page)) {
   5.289 +				v = phys_to_virt(pfn << PAGE_SHIFT);
   5.290 +				scrub_pages(v, 1);
   5.291 +				BUG_ON(HYPERVISOR_update_va_mapping(
   5.292 +					(unsigned long)v, __pte_ma(0), 0));
   5.293 +			}
   5.294 +#ifdef CONFIG_XEN_SCRUB_PAGES
   5.295 +			else {
   5.296 +				v = kmap(page);
   5.297 +				scrub_pages(v, 1);
   5.298 +				kunmap(page);
   5.299 +			}
   5.300 +#endif
   5.301 +		}
   5.302 +
   5.303 +		/* Ensure that ballooned highmem pages don't have kmaps. */
   5.304 +		kmap_flush_unused();
   5.305 +		flush_tlb_all();
   5.306 +
   5.307 +		/* No more mappings: invalidate P2M and add to balloon. */
   5.308 +		for (i = 0; i < debt; i++) {
   5.309 +			pfn = mfn_to_pfn(mfn_list[i]);
   5.310 +			phys_to_machine_mapping[pfn] = INVALID_P2M_ENTRY;
   5.311 +			balloon_append(pfn_to_page(pfn));
   5.312 +		}
   5.313 +
   5.314 +		BUG_ON(HYPERVISOR_dom_mem_op(
   5.315 +			MEMOP_decrease_reservation,mfn_list, debt, 0) != debt);
   5.316 +
   5.317 +		current_pages -= debt;
   5.318 +	}
   5.319  
   5.320   out:
   5.321 -    if ( mfn_list != NULL )
   5.322 -        vfree(mfn_list);
   5.323 +	if (mfn_list != NULL)
   5.324 +		vfree(mfn_list);
   5.325  
   5.326 -    /* Schedule more work if there is some still to be done. */
   5.327 -    if ( current_target() != current_pages )
   5.328 -        mod_timer(&balloon_timer, jiffies + HZ);
   5.329 +	/* Schedule more work if there is some still to be done. */
   5.330 +	if (current_target() != current_pages)
   5.331 +		mod_timer(&balloon_timer, jiffies + HZ);
   5.332  
   5.333 -    up(&balloon_mutex);
   5.334 +	up(&balloon_mutex);
   5.335  }
   5.336  
   5.337  /* Resets the Xen limit, sets new target, and kicks off processing. */
   5.338  static void set_new_target(unsigned long target)
   5.339  {
   5.340 -    /* No need for lock. Not read-modify-write updates. */
   5.341 -    hard_limit   = ~0UL;
   5.342 -    target_pages = target;
   5.343 -    schedule_work(&balloon_worker);
   5.344 +	/* No need for lock. Not read-modify-write updates. */
   5.345 +	hard_limit   = ~0UL;
   5.346 +	target_pages = target;
   5.347 +	schedule_work(&balloon_worker);
   5.348  }
   5.349  
   5.350  static struct xenbus_watch target_watch =
   5.351  {
   5.352 -    .node = "memory/target"
   5.353 +	.node = "memory/target"
   5.354  };
   5.355  
   5.356  /* React to a change in the target key */
   5.357  static void watch_target(struct xenbus_watch *watch, const char *node)
   5.358  {
   5.359 -    unsigned long new_target;
   5.360 -    int err;
   5.361 +	unsigned long new_target;
   5.362 +	int err;
   5.363  
   5.364 -    err = xenbus_scanf("memory", "target", "%lu", &new_target);
   5.365 +	err = xenbus_scanf("memory", "target", "%lu", &new_target);
   5.366 +	if (err != 1) {
   5.367 +		printk(KERN_ERR "Unable to read memory/target\n");
   5.368 +		return;
   5.369 +	} 
   5.370          
   5.371 -    if(err != 1) 
   5.372 -    {
   5.373 -        printk(KERN_ERR "Unable to read memory/target\n");
   5.374 -        return;
   5.375 -    } 
   5.376 -        
   5.377 -    set_new_target(new_target >> PAGE_SHIFT);
   5.378 +	set_new_target(new_target >> PAGE_SHIFT);
   5.379      
   5.380  }
   5.381  
   5.382 @@ -329,141 +315,185 @@ int balloon_init_watcher(struct notifier
   5.383                           unsigned long event,
   5.384                           void *data)
   5.385  {
   5.386 -    int err;
   5.387 -
   5.388 -    BUG_ON(down_trylock(&xenbus_lock) == 0);
   5.389 +	int err;
   5.390  
   5.391 -    err = register_xenbus_watch(&target_watch);
   5.392 +	BUG_ON(down_trylock(&xenbus_lock) == 0);
   5.393  
   5.394 -    if (err) {
   5.395 -        printk(KERN_ERR "Failed to set balloon watcher\n");
   5.396 -    }
   5.397 +	err = register_xenbus_watch(&target_watch);
   5.398 +	if (err)
   5.399 +		printk(KERN_ERR "Failed to set balloon watcher\n");
   5.400  
   5.401 -    return NOTIFY_DONE;
   5.402 +	return NOTIFY_DONE;
   5.403      
   5.404  }
   5.405  
   5.406  static int balloon_write(struct file *file, const char __user *buffer,
   5.407                           unsigned long count, void *data)
   5.408  {
   5.409 -    char memstring[64], *endchar;
   5.410 -    unsigned long long target_bytes;
   5.411 -
   5.412 -    if ( !capable(CAP_SYS_ADMIN) )
   5.413 -        return -EPERM;
   5.414 +	char memstring[64], *endchar;
   5.415 +	unsigned long long target_bytes;
   5.416  
   5.417 -    if ( count <= 1 )
   5.418 -        return -EBADMSG; /* runt */
   5.419 -    if ( count > sizeof(memstring) )
   5.420 -        return -EFBIG;   /* too long */
   5.421 +	if (!capable(CAP_SYS_ADMIN))
   5.422 +		return -EPERM;
   5.423  
   5.424 -    if ( copy_from_user(memstring, buffer, count) )
   5.425 -        return -EFAULT;
   5.426 -    memstring[sizeof(memstring)-1] = '\0';
   5.427 +	if (count <= 1)
   5.428 +		return -EBADMSG; /* runt */
   5.429 +	if (count > sizeof(memstring))
   5.430 +		return -EFBIG;   /* too long */
   5.431  
   5.432 -    target_bytes = memparse(memstring, &endchar);
   5.433 -    set_new_target(target_bytes >> PAGE_SHIFT);
   5.434 +	if (copy_from_user(memstring, buffer, count))
   5.435 +		return -EFAULT;
   5.436 +	memstring[sizeof(memstring)-1] = '\0';
   5.437  
   5.438 -    return count;
   5.439 +	target_bytes = memparse(memstring, &endchar);
   5.440 +	set_new_target(target_bytes >> PAGE_SHIFT);
   5.441 +
   5.442 +	return count;
   5.443  }
   5.444  
   5.445  static int balloon_read(char *page, char **start, off_t off,
   5.446                          int count, int *eof, void *data)
   5.447  {
   5.448 -    int len;
   5.449 -
   5.450 -    len = sprintf(
   5.451 -        page,
   5.452 -        "Current allocation: %8lu kB\n"
   5.453 -        "Requested target:   %8lu kB\n"
   5.454 -        "Low-mem balloon:    %8lu kB\n"
   5.455 -        "High-mem balloon:   %8lu kB\n"
   5.456 -        "Xen hard limit:     ",
   5.457 -        PAGES2KB(current_pages), PAGES2KB(target_pages), 
   5.458 -        PAGES2KB(balloon_low), PAGES2KB(balloon_high));
   5.459 +	int len;
   5.460  
   5.461 -    if ( hard_limit != ~0UL )
   5.462 -        len += sprintf(
   5.463 -            page + len, 
   5.464 -            "%8lu kB (inc. %8lu kB driver headroom)\n",
   5.465 -            PAGES2KB(hard_limit), PAGES2KB(driver_pages));
   5.466 -    else
   5.467 -        len += sprintf(
   5.468 -            page + len,
   5.469 -            "     ??? kB\n");
   5.470 +	len = sprintf(
   5.471 +		page,
   5.472 +		"Current allocation: %8lu kB\n"
   5.473 +		"Requested target:   %8lu kB\n"
   5.474 +		"Low-mem balloon:    %8lu kB\n"
   5.475 +		"High-mem balloon:   %8lu kB\n"
   5.476 +		"Xen hard limit:     ",
   5.477 +		PAGES2KB(current_pages), PAGES2KB(target_pages), 
   5.478 +		PAGES2KB(balloon_low), PAGES2KB(balloon_high));
   5.479  
   5.480 -    *eof = 1;
   5.481 -    return len;
   5.482 +	if (hard_limit != ~0UL) {
   5.483 +		len += sprintf(
   5.484 +			page + len, 
   5.485 +			"%8lu kB (inc. %8lu kB driver headroom)\n",
   5.486 +			PAGES2KB(hard_limit), PAGES2KB(driver_pages));
   5.487 +	} else {
   5.488 +		len += sprintf(
   5.489 +			page + len,
   5.490 +			"     ??? kB\n");
   5.491 +	}
   5.492 +
   5.493 +	*eof = 1;
   5.494 +	return len;
   5.495  }
   5.496  
   5.497  static struct notifier_block xenstore_notifier;
   5.498  
   5.499  static int __init balloon_init(void)
   5.500  {
   5.501 -    unsigned long pfn;
   5.502 -    struct page *page;
   5.503 -
   5.504 -    IPRINTK("Initialising balloon driver.\n");
   5.505 +	unsigned long pfn;
   5.506 +	struct page *page;
   5.507  
   5.508 -    current_pages = min(xen_start_info.nr_pages, max_pfn);
   5.509 -    target_pages  = current_pages;
   5.510 -    balloon_low   = 0;
   5.511 -    balloon_high  = 0;
   5.512 -    driver_pages  = 0UL;
   5.513 -    hard_limit    = ~0UL;
   5.514 +	IPRINTK("Initialising balloon driver.\n");
   5.515  
   5.516 -    init_timer(&balloon_timer);
   5.517 -    balloon_timer.data = 0;
   5.518 -    balloon_timer.function = balloon_alarm;
   5.519 +	current_pages = min(xen_start_info.nr_pages, max_pfn);
   5.520 +	target_pages  = current_pages;
   5.521 +	balloon_low   = 0;
   5.522 +	balloon_high  = 0;
   5.523 +	driver_pages  = 0UL;
   5.524 +	hard_limit    = ~0UL;
   5.525 +
   5.526 +	init_timer(&balloon_timer);
   5.527 +	balloon_timer.data = 0;
   5.528 +	balloon_timer.function = balloon_alarm;
   5.529      
   5.530 -    if ( (balloon_pde = create_xen_proc_entry("balloon", 0644)) == NULL )
   5.531 -    {
   5.532 -        WPRINTK("Unable to create /proc/xen/balloon.\n");
   5.533 -        return -1;
   5.534 -    }
   5.535 +	if ((balloon_pde = create_xen_proc_entry("balloon", 0644)) == NULL) {
   5.536 +		WPRINTK("Unable to create /proc/xen/balloon.\n");
   5.537 +		return -1;
   5.538 +	}
   5.539  
   5.540 -    balloon_pde->read_proc  = balloon_read;
   5.541 -    balloon_pde->write_proc = balloon_write;
   5.542 +	balloon_pde->read_proc  = balloon_read;
   5.543 +	balloon_pde->write_proc = balloon_write;
   5.544      
   5.545 -    /* Initialise the balloon with excess memory space. */
   5.546 -    for ( pfn = xen_start_info.nr_pages; pfn < max_pfn; pfn++ )
   5.547 -    {
   5.548 -        page = &mem_map[pfn];
   5.549 -        if ( !PageReserved(page) )
   5.550 -            balloon_append(page);
   5.551 -    }
   5.552 +	/* Initialise the balloon with excess memory space. */
   5.553 +	for (pfn = xen_start_info.nr_pages; pfn < max_pfn; pfn++) {
   5.554 +		page = &mem_map[pfn];
   5.555 +		if (!PageReserved(page))
   5.556 +			balloon_append(page);
   5.557 +	}
   5.558  
   5.559 -    target_watch.callback = watch_target;
   5.560 -    xenstore_notifier.notifier_call = balloon_init_watcher;
   5.561 +	target_watch.callback = watch_target;
   5.562 +	xenstore_notifier.notifier_call = balloon_init_watcher;
   5.563  
   5.564 -    register_xenstore_notifier(&xenstore_notifier);
   5.565 +	register_xenstore_notifier(&xenstore_notifier);
   5.566      
   5.567 -    return 0;
   5.568 +	return 0;
   5.569  }
   5.570  
   5.571  subsys_initcall(balloon_init);
   5.572  
   5.573  void balloon_update_driver_allowance(long delta)
   5.574  {
   5.575 -    unsigned long flags;
   5.576 -    balloon_lock(flags);
   5.577 -    driver_pages += delta; /* non-atomic update */
   5.578 -    balloon_unlock(flags);
   5.579 +	unsigned long flags;
   5.580 +	balloon_lock(flags);
   5.581 +	driver_pages += delta; /* non-atomic update */
   5.582 +	balloon_unlock(flags);
   5.583  }
   5.584  
   5.585 -void balloon_put_pages(unsigned long *mfn_list, unsigned long nr_mfns)
   5.586 +static int dealloc_pte_fn(
   5.587 +	pte_t *pte, struct page *pte_page, unsigned long addr, void *data)
   5.588  {
   5.589 -    unsigned long flags;
   5.590 +	unsigned long mfn = pte_mfn(*pte);
   5.591 +	set_pte(pte, __pte_ma(0));
   5.592 +	phys_to_machine_mapping[__pa(addr) >> PAGE_SHIFT] =
   5.593 +		INVALID_P2M_ENTRY;
   5.594 +	BUG_ON(HYPERVISOR_dom_mem_op(
   5.595 +		MEMOP_decrease_reservation, &mfn, 1, 0) != 1);
   5.596 +	return 0;
   5.597 +}
   5.598  
   5.599 -    balloon_lock(flags);
   5.600 -    if ( HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation, 
   5.601 -                               mfn_list, nr_mfns, 0) != nr_mfns )
   5.602 -        BUG();
   5.603 -    current_pages -= nr_mfns; /* non-atomic update */
   5.604 -    balloon_unlock(flags);
   5.605 +struct page *balloon_alloc_empty_page_range(unsigned long nr_pages)
   5.606 +{
   5.607 +	unsigned long vstart, flags;
   5.608 +	unsigned int  order = get_order(nr_pages * PAGE_SIZE);
   5.609  
   5.610 -    schedule_work(&balloon_worker);
   5.611 +	vstart = __get_free_pages(GFP_KERNEL, order);
   5.612 +	if (vstart == 0)
   5.613 +		return NULL;
   5.614 +
   5.615 +	scrub_pages(vstart, 1 << order);
   5.616 +
   5.617 +	balloon_lock(flags);
   5.618 +	BUG_ON(generic_page_range(
   5.619 +		&init_mm, vstart, PAGE_SIZE << order, dealloc_pte_fn, NULL));
   5.620 +	current_pages -= 1UL << order;
   5.621 +	balloon_unlock(flags);
   5.622 +
   5.623 +	schedule_work(&balloon_worker);
   5.624 +
   5.625 +	flush_tlb_all();
   5.626 +
   5.627 +	return virt_to_page(vstart);
   5.628 +}
   5.629 +
   5.630 +void balloon_dealloc_empty_page_range(
   5.631 +	struct page *page, unsigned long nr_pages)
   5.632 +{
   5.633 +	unsigned long i, flags;
   5.634 +	unsigned int  order = get_order(nr_pages * PAGE_SIZE);
   5.635 +
   5.636 +	balloon_lock(flags);
   5.637 +	for (i = 0; i < (1UL << order); i++)
   5.638 +		balloon_append(page + i);
   5.639 +	balloon_unlock(flags);
   5.640 +
   5.641 +	schedule_work(&balloon_worker);
   5.642  }
   5.643  
   5.644  EXPORT_SYMBOL(balloon_update_driver_allowance);
   5.645 -EXPORT_SYMBOL(balloon_put_pages);
   5.646 +EXPORT_SYMBOL(balloon_alloc_empty_page_range);
   5.647 +EXPORT_SYMBOL(balloon_dealloc_empty_page_range);
   5.648 +
   5.649 +/*
   5.650 + * Local variables:
   5.651 + *  c-file-style: "linux"
   5.652 + *  indent-tabs-mode: t
   5.653 + *  c-indent-level: 8
   5.654 + *  c-basic-offset: 8
   5.655 + *  tab-width: 8
   5.656 + * End:
   5.657 + */
     6.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c	Tue Aug 23 08:40:50 2005 +0000
     6.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c	Tue Aug 23 08:40:58 2005 +0000
     6.3 @@ -569,6 +569,7 @@ void blkif_deschedule(blkif_t *blkif)
     6.4  static int __init blkif_init(void)
     6.5  {
     6.6      int i;
     6.7 +    struct page *page;
     6.8  
     6.9      if ( !(xen_start_info.flags & SIF_INITDOMAIN) &&
    6.10           !(xen_start_info.flags & SIF_BLK_BE_DOMAIN) )
    6.11 @@ -576,8 +577,9 @@ static int __init blkif_init(void)
    6.12  
    6.13      blkif_interface_init();
    6.14  
    6.15 -    if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
    6.16 -        BUG();
    6.17 +    page = balloon_alloc_empty_page_range(MMAP_PAGES);
    6.18 +    BUG_ON(page == NULL);
    6.19 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
    6.20  
    6.21      pending_cons = 0;
    6.22      pending_prod = MAX_PENDING_REQS;
     7.1 --- a/linux-2.6-xen-sparse/drivers/xen/blktap/blktap_userdev.c	Tue Aug 23 08:40:50 2005 +0000
     7.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blktap/blktap_userdev.c	Tue Aug 23 08:40:58 2005 +0000
     7.3 @@ -775,9 +775,11 @@ static struct miscdevice blktap_miscdev 
     7.4  int blktap_init(void)
     7.5  {
     7.6      int err, i, j;
     7.7 +    struct page *page;
     7.8  
     7.9 -    if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
    7.10 -        BUG();
    7.11 +    page = balloon_alloc_empty_page_range(MMAP_PAGES);
    7.12 +    BUG_ON(page == NULL);
    7.13 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
    7.14  
    7.15  #ifdef CONFIG_XEN_BLKDEV_GRANT
    7.16      for (i=0; i<MAX_PENDING_REQS ; i++)
     8.1 --- a/linux-2.6-xen-sparse/drivers/xen/netback/netback.c	Tue Aug 23 08:40:50 2005 +0000
     8.2 +++ b/linux-2.6-xen-sparse/drivers/xen/netback/netback.c	Tue Aug 23 08:40:58 2005 +0000
     8.3 @@ -968,8 +968,9 @@ static int __init netback_init(void)
     8.4      
     8.5      netif_interface_init();
     8.6  
     8.7 -    mmap_vstart = allocate_empty_lowmem_region(MAX_PENDING_REQS);
     8.8 -    BUG_ON(mmap_vstart == 0);
     8.9 +    page = balloon_alloc_empty_page_range(MAX_PENDING_REQS);
    8.10 +    BUG_ON(page == NULL);
    8.11 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
    8.12  
    8.13      for ( i = 0; i < MAX_PENDING_REQS; i++ )
    8.14      {
     9.1 --- a/linux-2.6-xen-sparse/drivers/xen/usbback/usbback.c	Tue Aug 23 08:40:50 2005 +0000
     9.2 +++ b/linux-2.6-xen-sparse/drivers/xen/usbback/usbback.c	Tue Aug 23 08:40:58 2005 +0000
     9.3 @@ -1027,13 +1027,15 @@ void usbif_release_ports(usbif_priv_t *u
     9.4  static int __init usbif_init(void)
     9.5  {
     9.6      int i;
     9.7 +    struct page *page;
     9.8  
     9.9      if ( !(xen_start_info.flags & SIF_INITDOMAIN) &&
    9.10           !(xen_start_info.flags & SIF_USB_BE_DOMAIN) )
    9.11          return 0;
    9.12 -    
    9.13 -    if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
    9.14 -        BUG();
    9.15 +
    9.16 +    page = balloon_alloc_empty_page_range(MMAP_PAGES);
    9.17 +    BUG_ON(page == NULL);
    9.18 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
    9.19  
    9.20      pending_cons = 0;
    9.21      pending_prod = MAX_PENDING_REQS;
    10.1 --- a/linux-2.6-xen-sparse/include/asm-xen/balloon.h	Tue Aug 23 08:40:50 2005 +0000
    10.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/balloon.h	Tue Aug 23 08:40:58 2005 +0000
    10.3 @@ -35,10 +35,19 @@
    10.4   * Inform the balloon driver that it should allow some slop for device-driver
    10.5   * memory activities.
    10.6   */
    10.7 -extern void balloon_update_driver_allowance(long delta);
    10.8 +extern void
    10.9 +balloon_update_driver_allowance(
   10.10 +	long delta);
   10.11  
   10.12 -/* Give up unmapped pages to the balloon driver. */
   10.13 -extern void balloon_put_pages(unsigned long *mfn_list, unsigned long nr_mfns);
   10.14 +/* Allocate an empty low-memory page range. */
   10.15 +extern struct page *
   10.16 +balloon_alloc_empty_page_range(
   10.17 +	unsigned long nr_pages);
   10.18 +
   10.19 +/* Deallocate an empty page range, adding to the balloon. */
   10.20 +extern void
   10.21 +balloon_dealloc_empty_page_range(
   10.22 +	struct page *page, unsigned long nr_pages);
   10.23  
   10.24  /*
   10.25   * Prevent the balloon driver from changing the memory reservation during
    11.1 --- a/linux-2.6-xen-sparse/include/asm-xen/hypervisor.h	Tue Aug 23 08:40:50 2005 +0000
    11.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/hypervisor.h	Tue Aug 23 08:40:58 2005 +0000
    11.3 @@ -137,9 +137,6 @@ void xen_invlpg_mask(cpumask_t *mask, un
    11.4  void xen_create_contiguous_region(unsigned long vstart, unsigned int order);
    11.5  void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order);
    11.6  
    11.7 -/* Allocate a contiguous empty region of low memory. Return virtual start. */
    11.8 -unsigned long allocate_empty_lowmem_region(unsigned long pages);
    11.9 -
   11.10  #include <asm/hypercall.h>
   11.11  
   11.12  #if defined(CONFIG_X86_64)
    12.1 --- a/tools/Makefile	Tue Aug 23 08:40:50 2005 +0000
    12.2 +++ b/tools/Makefile	Tue Aug 23 08:40:58 2005 +0000
    12.3 @@ -14,6 +14,7 @@ SUBDIRS += xcutils
    12.4  SUBDIRS += firmware
    12.5  SUBDIRS += security
    12.6  SUBDIRS += console
    12.7 +SUBDIRS += xenstat
    12.8  
    12.9  .PHONY: all install clean check check_clean ioemu eioemuinstall ioemuclean
   12.10  
    13.1 --- a/tools/Rules.mk	Tue Aug 23 08:40:50 2005 +0000
    13.2 +++ b/tools/Rules.mk	Tue Aug 23 08:40:58 2005 +0000
    13.3 @@ -6,6 +6,7 @@ XEN_XC             = $(XEN_ROOT)/tools/p
    13.4  XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
    13.5  XEN_XCS            = $(XEN_ROOT)/tools/xcs
    13.6  XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
    13.7 +XEN_LIBXENSTAT     = $(XEN_ROOT)/tools/xenstat/libxenstat/src
    13.8  
    13.9  ifeq ($(XEN_TARGET_ARCH),x86_32)
   13.10  CFLAGS  += -m32 -march=i686
    14.1 --- a/tools/python/xen/xm/main.py	Tue Aug 23 08:40:50 2005 +0000
    14.2 +++ b/tools/python/xen/xm/main.py	Tue Aug 23 08:40:58 2005 +0000
    14.3 @@ -49,6 +49,7 @@ xm common subcommands:
    14.4      restore <File>          create a domain from a saved state file
    14.5      save <DomId> <File>     save domain state (and config) to file
    14.6      shutdown <DomId>        shutdown a domain
    14.7 +    top                     monitor system and domains in real-time
    14.8      unpause <DomId>         unpause a paused domain
    14.9  
   14.10  For a complete list of subcommands run 'xm help --long'
   14.11 @@ -87,6 +88,7 @@ xm full list of subcommands:
   14.12      dmesg   [--clear]         read or clear Xen's message buffer
   14.13      info                      get information about the xen host
   14.14      log                       print the xend log
   14.15 +    top                       monitor system and domains in real-time
   14.16  
   14.17    Scheduler Commands:
   14.18      bvt <options>             set BVT scheduler parameters
   14.19 @@ -457,6 +459,9 @@ def xm_console(args):
   14.20      os.execvp('/usr/libexec/xen/xenconsole', cmd.split())
   14.21      console = sxp.child(info, "console")
   14.22  
   14.23 +def xm_top(args):
   14.24 +    os.execv('/usr/sbin/xentop', ['/usr/sbin/xentop'])
   14.25 +
   14.26  def xm_dmesg(args):
   14.27      
   14.28      gopts = Opts(use="""[-c|--clear]
   14.29 @@ -545,6 +550,8 @@ def xm_block_destroy(args):
   14.30  commands = {
   14.31      # console commands
   14.32      "console": xm_console,
   14.33 +    # xenstat commands
   14.34 +    "top": xm_top,
   14.35      # domain commands
   14.36      "domid": xm_domid,
   14.37      "domname": xm_domname,
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/tools/xenstat/Makefile	Tue Aug 23 08:40:58 2005 +0000
    15.3 @@ -0,0 +1,13 @@
    15.4 +XEN_ROOT = ../..
    15.5 +include $(XEN_ROOT)/tools/Rules.mk
    15.6 +
    15.7 +SUBDIRS :=
    15.8 +SUBDIRS += libxenstat
    15.9 +SUBDIRS += xentop
   15.10 +
   15.11 +.PHONY: all install clean
   15.12 +
   15.13 +all install clean:
   15.14 +	@set -e; for subdir in $(SUBDIRS); do \
   15.15 +		$(MAKE) -C $$subdir $@; \
   15.16 +	done
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/tools/xenstat/libxenstat/COPYING	Tue Aug 23 08:40:58 2005 +0000
    16.3 @@ -0,0 +1,510 @@
    16.4 +
    16.5 +                  GNU LESSER GENERAL PUBLIC LICENSE
    16.6 +                       Version 2.1, February 1999
    16.7 +
    16.8 + Copyright (C) 1991, 1999 Free Software Foundation, Inc.
    16.9 +	51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   16.10 + Everyone is permitted to copy and distribute verbatim copies
   16.11 + of this license document, but changing it is not allowed.
   16.12 +
   16.13 +[This is the first released version of the Lesser GPL.  It also counts
   16.14 + as the successor of the GNU Library Public License, version 2, hence
   16.15 + the version number 2.1.]
   16.16 +
   16.17 +                            Preamble
   16.18 +
   16.19 +  The licenses for most software are designed to take away your
   16.20 +freedom to share and change it.  By contrast, the GNU General Public
   16.21 +Licenses are intended to guarantee your freedom to share and change
   16.22 +free software--to make sure the software is free for all its users.
   16.23 +
   16.24 +  This license, the Lesser General Public License, applies to some
   16.25 +specially designated software packages--typically libraries--of the
   16.26 +Free Software Foundation and other authors who decide to use it.  You
   16.27 +can use it too, but we suggest you first think carefully about whether
   16.28 +this license or the ordinary General Public License is the better
   16.29 +strategy to use in any particular case, based on the explanations
   16.30 +below.
   16.31 +
   16.32 +  When we speak of free software, we are referring to freedom of use,
   16.33 +not price.  Our General Public Licenses are designed to make sure that
   16.34 +you have the freedom to distribute copies of free software (and charge
   16.35 +for this service if you wish); that you receive source code or can get
   16.36 +it if you want it; that you can change the software and use pieces of
   16.37 +it in new free programs; and that you are informed that you can do
   16.38 +these things.
   16.39 +
   16.40 +  To protect your rights, we need to make restrictions that forbid
   16.41 +distributors to deny you these rights or to ask you to surrender these
   16.42 +rights.  These restrictions translate to certain responsibilities for
   16.43 +you if you distribute copies of the library or if you modify it.
   16.44 +
   16.45 +  For example, if you distribute copies of the library, whether gratis
   16.46 +or for a fee, you must give the recipients all the rights that we gave
   16.47 +you.  You must make sure that they, too, receive or can get the source
   16.48 +code.  If you link other code with the library, you must provide
   16.49 +complete object files to the recipients, so that they can relink them
   16.50 +with the library after making changes to the library and recompiling
   16.51 +it.  And you must show them these terms so they know their rights.
   16.52 +
   16.53 +  We protect your rights with a two-step method: (1) we copyright the
   16.54 +library, and (2) we offer you this license, which gives you legal
   16.55 +permission to copy, distribute and/or modify the library.
   16.56 +
   16.57 +  To protect each distributor, we want to make it very clear that
   16.58 +there is no warranty for the free library.  Also, if the library is
   16.59 +modified by someone else and passed on, the recipients should know
   16.60 +that what they have is not the original version, so that the original
   16.61 +author's reputation will not be affected by problems that might be
   16.62 +introduced by others.
   16.63 +
   16.64 +  Finally, software patents pose a constant threat to the existence of
   16.65 +any free program.  We wish to make sure that a company cannot
   16.66 +effectively restrict the users of a free program by obtaining a
   16.67 +restrictive license from a patent holder.  Therefore, we insist that
   16.68 +any patent license obtained for a version of the library must be
   16.69 +consistent with the full freedom of use specified in this license.
   16.70 +
   16.71 +  Most GNU software, including some libraries, is covered by the
   16.72 +ordinary GNU General Public License.  This license, the GNU Lesser
   16.73 +General Public License, applies to certain designated libraries, and
   16.74 +is quite different from the ordinary General Public License.  We use
   16.75 +this license for certain libraries in order to permit linking those
   16.76 +libraries into non-free programs.
   16.77 +
   16.78 +  When a program is linked with a library, whether statically or using
   16.79 +a shared library, the combination of the two is legally speaking a
   16.80 +combined work, a derivative of the original library.  The ordinary
   16.81 +General Public License therefore permits such linking only if the
   16.82 +entire combination fits its criteria of freedom.  The Lesser General
   16.83 +Public License permits more lax criteria for linking other code with
   16.84 +the library.
   16.85 +
   16.86 +  We call this license the "Lesser" General Public License because it
   16.87 +does Less to protect the user's freedom than the ordinary General
   16.88 +Public License.  It also provides other free software developers Less
   16.89 +of an advantage over competing non-free programs.  These disadvantages
   16.90 +are the reason we use the ordinary General Public License for many
   16.91 +libraries.  However, the Lesser license provides advantages in certain
   16.92 +special circumstances.
   16.93 +
   16.94 +  For example, on rare occasions, there may be a special need to
   16.95 +encourage the widest possible use of a certain library, so that it
   16.96 +becomes a de-facto standard.  To achieve this, non-free programs must
   16.97 +be allowed to use the library.  A more frequent case is that a free
   16.98 +library does the same job as widely used non-free libraries.  In this
   16.99 +case, there is little to gain by limiting the free library to free
  16.100 +software only, so we use the Lesser General Public License.
  16.101 +
  16.102 +  In other cases, permission to use a particular library in non-free
  16.103 +programs enables a greater number of people to use a large body of
  16.104 +free software.  For example, permission to use the GNU C Library in
  16.105 +non-free programs enables many more people to use the whole GNU
  16.106 +operating system, as well as its variant, the GNU/Linux operating
  16.107 +system.
  16.108 +
  16.109 +  Although the Lesser General Public License is Less protective of the
  16.110 +users' freedom, it does ensure that the user of a program that is
  16.111 +linked with the Library has the freedom and the wherewithal to run
  16.112 +that program using a modified version of the Library.
  16.113 +
  16.114 +  The precise terms and conditions for copying, distribution and
  16.115 +modification follow.  Pay close attention to the difference between a
  16.116 +"work based on the library" and a "work that uses the library".  The
  16.117 +former contains code derived from the library, whereas the latter must
  16.118 +be combined with the library in order to run.
  16.119 +
  16.120 +                  GNU LESSER GENERAL PUBLIC LICENSE
  16.121 +   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  16.122 +
  16.123 +  0. This License Agreement applies to any software library or other
  16.124 +program which contains a notice placed by the copyright holder or
  16.125 +other authorized party saying it may be distributed under the terms of
  16.126 +this Lesser General Public License (also called "this License").
  16.127 +Each licensee is addressed as "you".
  16.128 +
  16.129 +  A "library" means a collection of software functions and/or data
  16.130 +prepared so as to be conveniently linked with application programs
  16.131 +(which use some of those functions and data) to form executables.
  16.132 +
  16.133 +  The "Library", below, refers to any such software library or work
  16.134 +which has been distributed under these terms.  A "work based on the
  16.135 +Library" means either the Library or any derivative work under
  16.136 +copyright law: that is to say, a work containing the Library or a
  16.137 +portion of it, either verbatim or with modifications and/or translated
  16.138 +straightforwardly into another language.  (Hereinafter, translation is
  16.139 +included without limitation in the term "modification".)
  16.140 +
  16.141 +  "Source code" for a work means the preferred form of the work for
  16.142 +making modifications to it.  For a library, complete source code means
  16.143 +all the source code for all modules it contains, plus any associated
  16.144 +interface definition files, plus the scripts used to control
  16.145 +compilation and installation of the library.
  16.146 +
  16.147 +  Activities other than copying, distribution and modification are not
  16.148 +covered by this License; they are outside its scope.  The act of
  16.149 +running a program using the Library is not restricted, and output from
  16.150 +such a program is covered only if its contents constitute a work based
  16.151 +on the Library (independent of the use of the Library in a tool for
  16.152 +writing it).  Whether that is true depends on what the Library does
  16.153 +and what the program that uses the Library does.
  16.154 +
  16.155 +  1. You may copy and distribute verbatim copies of the Library's
  16.156 +complete source code as you receive it, in any medium, provided that
  16.157 +you conspicuously and appropriately publish on each copy an
  16.158 +appropriate copyright notice and disclaimer of warranty; keep intact
  16.159 +all the notices that refer to this License and to the absence of any
  16.160 +warranty; and distribute a copy of this License along with the
  16.161 +Library.
  16.162 +
  16.163 +  You may charge a fee for the physical act of transferring a copy,
  16.164 +and you may at your option offer warranty protection in exchange for a
  16.165 +fee.
  16.166 +
  16.167 +  2. You may modify your copy or copies of the Library or any portion
  16.168 +of it, thus forming a work based on the Library, and copy and
  16.169 +distribute such modifications or work under the terms of Section 1
  16.170 +above, provided that you also meet all of these conditions:
  16.171 +
  16.172 +    a) The modified work must itself be a software library.
  16.173 +
  16.174 +    b) You must cause the files modified to carry prominent notices
  16.175 +    stating that you changed the files and the date of any change.
  16.176 +
  16.177 +    c) You must cause the whole of the work to be licensed at no
  16.178 +    charge to all third parties under the terms of this License.
  16.179 +
  16.180 +    d) If a facility in the modified Library refers to a function or a
  16.181 +    table of data to be supplied by an application program that uses
  16.182 +    the facility, other than as an argument passed when the facility
  16.183 +    is invoked, then you must make a good faith effort to ensure that,
  16.184 +    in the event an application does not supply such function or
  16.185 +    table, the facility still operates, and performs whatever part of
  16.186 +    its purpose remains meaningful.
  16.187 +
  16.188 +    (For example, a function in a library to compute square roots has
  16.189 +    a purpose that is entirely well-defined independent of the
  16.190 +    application.  Therefore, Subsection 2d requires that any
  16.191 +    application-supplied function or table used by this function must
  16.192 +    be optional: if the application does not supply it, the square
  16.193 +    root function must still compute square roots.)
  16.194 +
  16.195 +These requirements apply to the modified work as a whole.  If
  16.196 +identifiable sections of that work are not derived from the Library,
  16.197 +and can be reasonably considered independent and separate works in
  16.198 +themselves, then this License, and its terms, do not apply to those
  16.199 +sections when you distribute them as separate works.  But when you
  16.200 +distribute the same sections as part of a whole which is a work based
  16.201 +on the Library, the distribution of the whole must be on the terms of
  16.202 +this License, whose permissions for other licensees extend to the
  16.203 +entire whole, and thus to each and every part regardless of who wrote
  16.204 +it.
  16.205 +
  16.206 +Thus, it is not the intent of this section to claim rights or contest
  16.207 +your rights to work written entirely by you; rather, the intent is to
  16.208 +exercise the right to control the distribution of derivative or
  16.209 +collective works based on the Library.
  16.210 +
  16.211 +In addition, mere aggregation of another work not based on the Library
  16.212 +with the Library (or with a work based on the Library) on a volume of
  16.213 +a storage or distribution medium does not bring the other work under
  16.214 +the scope of this License.
  16.215 +
  16.216 +  3. You may opt to apply the terms of the ordinary GNU General Public
  16.217 +License instead of this License to a given copy of the Library.  To do
  16.218 +this, you must alter all the notices that refer to this License, so
  16.219 +that they refer to the ordinary GNU General Public License, version 2,
  16.220 +instead of to this License.  (If a newer version than version 2 of the
  16.221 +ordinary GNU General Public License has appeared, then you can specify
  16.222 +that version instead if you wish.)  Do not make any other change in
  16.223 +these notices.
  16.224 +
  16.225 +  Once this change is made in a given copy, it is irreversible for
  16.226 +that copy, so the ordinary GNU General Public License applies to all
  16.227 +subsequent copies and derivative works made from that copy.
  16.228 +
  16.229 +  This option is useful when you wish to copy part of the code of
  16.230 +the Library into a program that is not a library.
  16.231 +
  16.232 +  4. You may copy and distribute the Library (or a portion or
  16.233 +derivative of it, under Section 2) in object code or executable form
  16.234 +under the terms of Sections 1 and 2 above provided that you accompany
  16.235 +it with the complete corresponding machine-readable source code, which
  16.236 +must be distributed under the terms of Sections 1 and 2 above on a
  16.237 +medium customarily used for software interchange.
  16.238 +
  16.239 +  If distribution of object code is made by offering access to copy
  16.240 +from a designated place, then offering equivalent access to copy the
  16.241 +source code from the same place satisfies the requirement to
  16.242 +distribute the source code, even though third parties are not
  16.243 +compelled to copy the source along with the object code.
  16.244 +
  16.245 +  5. A program that contains no derivative of any portion of the
  16.246 +Library, but is designed to work with the Library by being compiled or
  16.247 +linked with it, is called a "work that uses the Library".  Such a
  16.248 +work, in isolation, is not a derivative work of the Library, and
  16.249 +therefore falls outside the scope of this License.
  16.250 +
  16.251 +  However, linking a "work that uses the Library" with the Library
  16.252 +creates an executable that is a derivative of the Library (because it
  16.253 +contains portions of the Library), rather than a "work that uses the
  16.254 +library".  The executable is therefore covered by this License.
  16.255 +Section 6 states terms for distribution of such executables.
  16.256 +
  16.257 +  When a "work that uses the Library" uses material from a header file
  16.258 +that is part of the Library, the object code for the work may be a
  16.259 +derivative work of the Library even though the source code is not.
  16.260 +Whether this is true is especially significant if the work can be
  16.261 +linked without the Library, or if the work is itself a library.  The
  16.262 +threshold for this to be true is not precisely defined by law.
  16.263 +
  16.264 +  If such an object file uses only numerical parameters, data
  16.265 +structure layouts and accessors, and small macros and small inline
  16.266 +functions (ten lines or less in length), then the use of the object
  16.267 +file is unrestricted, regardless of whether it is legally a derivative
  16.268 +work.  (Executables containing this object code plus portions of the
  16.269 +Library will still fall under Section 6.)
  16.270 +
  16.271 +  Otherwise, if the work is a derivative of the Library, you may
  16.272 +distribute the object code for the work under the terms of Section 6.
  16.273 +Any executables containing that work also fall under Section 6,
  16.274 +whether or not they are linked directly with the Library itself.
  16.275 +
  16.276 +  6. As an exception to the Sections above, you may also combine or
  16.277 +link a "work that uses the Library" with the Library to produce a
  16.278 +work containing portions of the Library, and distribute that work
  16.279 +under terms of your choice, provided that the terms permit
  16.280 +modification of the work for the customer's own use and reverse
  16.281 +engineering for debugging such modifications.
  16.282 +
  16.283 +  You must give prominent notice with each copy of the work that the
  16.284 +Library is used in it and that the Library and its use are covered by
  16.285 +this License.  You must supply a copy of this License.  If the work
  16.286 +during execution displays copyright notices, you must include the
  16.287 +copyright notice for the Library among them, as well as a reference
  16.288 +directing the user to the copy of this License.  Also, you must do one
  16.289 +of these things:
  16.290 +
  16.291 +    a) Accompany the work with the complete corresponding
  16.292 +    machine-readable source code for the Library including whatever
  16.293 +    changes were used in the work (which must be distributed under
  16.294 +    Sections 1 and 2 above); and, if the work is an executable linked
  16.295 +    with the Library, with the complete machine-readable "work that
  16.296 +    uses the Library", as object code and/or source code, so that the
  16.297 +    user can modify the Library and then relink to produce a modified
  16.298 +    executable containing the modified Library.  (It is understood
  16.299 +    that the user who changes the contents of definitions files in the
  16.300 +    Library will not necessarily be able to recompile the application
  16.301 +    to use the modified definitions.)
  16.302 +
  16.303 +    b) Use a suitable shared library mechanism for linking with the
  16.304 +    Library.  A suitable mechanism is one that (1) uses at run time a
  16.305 +    copy of the library already present on the user's computer system,
  16.306 +    rather than copying library functions into the executable, and (2)
  16.307 +    will operate properly with a modified version of the library, if
  16.308 +    the user installs one, as long as the modified version is
  16.309 +    interface-compatible with the version that the work was made with.
  16.310 +
  16.311 +    c) Accompany the work with a written offer, valid for at least
  16.312 +    three years, to give the same user the materials specified in
  16.313 +    Subsection 6a, above, for a charge no more than the cost of
  16.314 +    performing this distribution.
  16.315 +
  16.316 +    d) If distribution of the work is made by offering access to copy
  16.317 +    from a designated place, offer equivalent access to copy the above
  16.318 +    specified materials from the same place.
  16.319 +
  16.320 +    e) Verify that the user has already received a copy of these
  16.321 +    materials or that you have already sent this user a copy.
  16.322 +
  16.323 +  For an executable, the required form of the "work that uses the
  16.324 +Library" must include any data and utility programs needed for
  16.325 +reproducing the executable from it.  However, as a special exception,
  16.326 +the materials to be distributed need not include anything that is
  16.327 +normally distributed (in either source or binary form) with the major
  16.328 +components (compiler, kernel, and so on) of the operating system on
  16.329 +which the executable runs, unless that component itself accompanies
  16.330 +the executable.
  16.331 +
  16.332 +  It may happen that this requirement contradicts the license
  16.333 +restrictions of other proprietary libraries that do not normally
  16.334 +accompany the operating system.  Such a contradiction means you cannot
  16.335 +use both them and the Library together in an executable that you
  16.336 +distribute.
  16.337 +
  16.338 +  7. You may place library facilities that are a work based on the
  16.339 +Library side-by-side in a single library together with other library
  16.340 +facilities not covered by this License, and distribute such a combined
  16.341 +library, provided that the separate distribution of the work based on
  16.342 +the Library and of the other library facilities is otherwise
  16.343 +permitted, and provided that you do these two things:
  16.344 +
  16.345 +    a) Accompany the combined library with a copy of the same work
  16.346 +    based on the Library, uncombined with any other library
  16.347 +    facilities.  This must be distributed under the terms of the
  16.348 +    Sections above.
  16.349 +
  16.350 +    b) Give prominent notice with the combined library of the fact
  16.351 +    that part of it is a work based on the Library, and explaining
  16.352 +    where to find the accompanying uncombined form of the same work.
  16.353 +
  16.354 +  8. You may not copy, modify, sublicense, link with, or distribute
  16.355 +the Library except as expressly provided under this License.  Any
  16.356 +attempt otherwise to copy, modify, sublicense, link with, or
  16.357 +distribute the Library is void, and will automatically terminate your
  16.358 +rights under this License.  However, parties who have received copies,
  16.359 +or rights, from you under this License will not have their licenses
  16.360 +terminated so long as such parties remain in full compliance.
  16.361 +
  16.362 +  9. You are not required to accept this License, since you have not
  16.363 +signed it.  However, nothing else grants you permission to modify or
  16.364 +distribute the Library or its derivative works.  These actions are
  16.365 +prohibited by law if you do not accept this License.  Therefore, by
  16.366 +modifying or distributing the Library (or any work based on the
  16.367 +Library), you indicate your acceptance of this License to do so, and
  16.368 +all its terms and conditions for copying, distributing or modifying
  16.369 +the Library or works based on it.
  16.370 +
  16.371 +  10. Each time you redistribute the Library (or any work based on the
  16.372 +Library), the recipient automatically receives a license from the
  16.373 +original licensor to copy, distribute, link with or modify the Library
  16.374 +subject to these terms and conditions.  You may not impose any further
  16.375 +restrictions on the recipients' exercise of the rights granted herein.
  16.376 +You are not responsible for enforcing compliance by third parties with
  16.377 +this License.
  16.378 +
  16.379 +  11. If, as a consequence of a court judgment or allegation of patent
  16.380 +infringement or for any other reason (not limited to patent issues),
  16.381 +conditions are imposed on you (whether by court order, agreement or
  16.382 +otherwise) that contradict the conditions of this License, they do not
  16.383 +excuse you from the conditions of this License.  If you cannot
  16.384 +distribute so as to satisfy simultaneously your obligations under this
  16.385 +License and any other pertinent obligations, then as a consequence you
  16.386 +may not distribute the Library at all.  For example, if a patent
  16.387 +license would not permit royalty-free redistribution of the Library by
  16.388 +all those who receive copies directly or indirectly through you, then
  16.389 +the only way you could satisfy both it and this License would be to
  16.390 +refrain entirely from distribution of the Library.
  16.391 +
  16.392 +If any portion of this section is held invalid or unenforceable under
  16.393 +any particular circumstance, the balance of the section is intended to
  16.394 +apply, and the section as a whole is intended to apply in other
  16.395 +circumstances.
  16.396 +
  16.397 +It is not the purpose of this section to induce you to infringe any
  16.398 +patents or other property right claims or to contest validity of any
  16.399 +such claims; this section has the sole purpose of protecting the
  16.400 +integrity of the free software distribution system which is
  16.401 +implemented by public license practices.  Many people have made
  16.402 +generous contributions to the wide range of software distributed
  16.403 +through that system in reliance on consistent application of that
  16.404 +system; it is up to the author/donor to decide if he or she is willing
  16.405 +to distribute software through any other system and a licensee cannot
  16.406 +impose that choice.
  16.407 +
  16.408 +This section is intended to make thoroughly clear what is believed to
  16.409 +be a consequence of the rest of this License.
  16.410 +
  16.411 +  12. If the distribution and/or use of the Library is restricted in
  16.412 +certain countries either by patents or by copyrighted interfaces, the
  16.413 +original copyright holder who places the Library under this License
  16.414 +may add an explicit geographical distribution limitation excluding those
  16.415 +countries, so that distribution is permitted only in or among
  16.416 +countries not thus excluded.  In such case, this License incorporates
  16.417 +the limitation as if written in the body of this License.
  16.418 +
  16.419 +  13. The Free Software Foundation may publish revised and/or new
  16.420 +versions of the Lesser General Public License from time to time.
  16.421 +Such new versions will be similar in spirit to the present version,
  16.422 +but may differ in detail to address new problems or concerns.
  16.423 +
  16.424 +Each version is given a distinguishing version number.  If the Library
  16.425 +specifies a version number of this License which applies to it and
  16.426 +"any later version", you have the option of following the terms and
  16.427 +conditions either of that version or of any later version published by
  16.428 +the Free Software Foundation.  If the Library does not specify a
  16.429 +license version number, you may choose any version ever published by
  16.430 +the Free Software Foundation.
  16.431 +
  16.432 +  14. If you wish to incorporate parts of the Library into other free
  16.433 +programs whose distribution conditions are incompatible with these,
  16.434 +write to the author to ask for permission.  For software which is
  16.435 +copyrighted by the Free Software Foundation, write to the Free
  16.436 +Software Foundation; we sometimes make exceptions for this.  Our
  16.437 +decision will be guided by the two goals of preserving the free status
  16.438 +of all derivatives of our free software and of promoting the sharing
  16.439 +and reuse of software generally.
  16.440 +
  16.441 +                            NO WARRANTY
  16.442 +
  16.443 +  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
  16.444 +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
  16.445 +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
  16.446 +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
  16.447 +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
  16.448 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16.449 +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
  16.450 +LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
  16.451 +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  16.452 +
  16.453 +  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
  16.454 +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
  16.455 +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
  16.456 +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
  16.457 +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
  16.458 +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
  16.459 +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
  16.460 +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
  16.461 +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16.462 +DAMAGES.
  16.463 +
  16.464 +                     END OF TERMS AND CONDITIONS
  16.465 +
  16.466 +           How to Apply These Terms to Your New Libraries
  16.467 +
  16.468 +  If you develop a new library, and you want it to be of the greatest
  16.469 +possible use to the public, we recommend making it free software that
  16.470 +everyone can redistribute and change.  You can do so by permitting
  16.471 +redistribution under these terms (or, alternatively, under the terms
  16.472 +of the ordinary General Public License).
  16.473 +
  16.474 +  To apply these terms, attach the following notices to the library.
  16.475 +It is safest to attach them to the start of each source file to most
  16.476 +effectively convey the exclusion of warranty; and each file should
  16.477 +have at least the "copyright" line and a pointer to where the full
  16.478 +notice is found.
  16.479 +
  16.480 +
  16.481 +    <one line to give the library's name and a brief idea of what it does.>
  16.482 +    Copyright (C) <year>  <name of author>
  16.483 +
  16.484 +    This library is free software; you can redistribute it and/or
  16.485 +    modify it under the terms of the GNU Lesser General Public
  16.486 +    License as published by the Free Software Foundation; either
  16.487 +    version 2.1 of the License, or (at your option) any later version.
  16.488 +
  16.489 +    This library is distributed in the hope that it will be useful,
  16.490 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.491 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.492 +    Lesser General Public License for more details.
  16.493 +
  16.494 +    You should have received a copy of the GNU Lesser General Public
  16.495 +    License along with this library; if not, write to the Free Software
  16.496 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  16.497 +
  16.498 +Also add information on how to contact you by electronic and paper mail.
  16.499 +
  16.500 +You should also get your employer (if you work as a programmer) or
  16.501 +your school, if any, to sign a "copyright disclaimer" for the library,
  16.502 +if necessary.  Here is a sample; alter the names:
  16.503 +
  16.504 +  Yoyodyne, Inc., hereby disclaims all copyright interest in the
  16.505 +  library `Frob' (a library for tweaking knobs) written by James
  16.506 +  Random Hacker.
  16.507 +
  16.508 +  <signature of Ty Coon>, 1 April 1990
  16.509 +  Ty Coon, President of Vice
  16.510 +
  16.511 +That's all there is to it!
  16.512 +
  16.513 +
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/tools/xenstat/libxenstat/Makefile	Tue Aug 23 08:40:58 2005 +0000
    17.3 @@ -0,0 +1,142 @@
    17.4 +# libxenstat: statistics-collection library for Xen
    17.5 +# Copyright (C) International Business Machines Corp., 2005
    17.6 +# Author: Josh Triplett <josht@us.ibm.com>
    17.7 +# 
    17.8 +# This library is free software; you can redistribute it and/or
    17.9 +# modify it under the terms of the GNU Lesser General Public
   17.10 +# License as published by the Free Software Foundation; either
   17.11 +# version 2.1 of the License, or (at your option) any later version.
   17.12 +# 
   17.13 +# This library is distributed in the hope that it will be useful,
   17.14 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   17.15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   17.16 +# Lesser General Public License for more details.
   17.17 +
   17.18 +XEN_ROOT=../../..
   17.19 +include $(XEN_ROOT)/tools/Rules.mk
   17.20 +LINUX_ROOT := $(XEN_ROOT)/linux-2.6-xen-sparse
   17.21 +
   17.22 +INSTALL         = install
   17.23 +INSTALL_PROG    = $(INSTALL) -m0755 -D
   17.24 +INSTALL_DATA    = $(INSTALL) -m0644 -D
   17.25 +
   17.26 +prefix=/usr
   17.27 +includedir=$(prefix)/include
   17.28 +libdir=$(prefix)/lib
   17.29 +
   17.30 +LDCONFIG=ldconfig
   17.31 +MAKE_LINK=ln -sf
   17.32 +
   17.33 +MAJOR=0
   17.34 +MINOR=0
   17.35 +
   17.36 +LIB=src/libxenstat.a
   17.37 +SHLIB=src/libxenstat.so.$(MAJOR).$(MINOR)
   17.38 +SHLIB_LINKS=src/libxenstat.so.$(MAJOR) src/libxenstat.so
   17.39 +OBJECTS=src/xenstat.o src/xen-interface.o
   17.40 +SONAME_FLAGS=-Wl,-soname -Wl,libxenstat.so.$(MAJOR)
   17.41 +
   17.42 +WARN_FLAGS=-Wall -Werror
   17.43 +
   17.44 +CFLAGS+=-Isrc
   17.45 +CFLAGS+=-I$(XEN_ROOT)/xen/include/public
   17.46 +CFLAGS+=-I$(LINUX_ROOT)/include/asm-xen/linux-public/
   17.47 +LDFLAGS+=-Lsrc
   17.48 +
   17.49 +all: $(LIB)
   17.50 +
   17.51 +$(LIB): $(OBJECTS)
   17.52 +	$(AR) rc $@ $^
   17.53 +	$(RANLIB) $@
   17.54 +
   17.55 +$(SHLIB): $(OBJECTS)
   17.56 +	$(CC) $(LDFLAGS) $(SONAME_FLAGS) -shared -o $@ $(OBJECTS)
   17.57 +
   17.58 +src/xenstat.o: src/xenstat.c src/xenstat.h src/xen-interface.h
   17.59 +	$(CC) $(CFLAGS) $(WARN_FLAGS) -c -o $@ $<
   17.60 +
   17.61 +src/xen-interface.o: src/xen-interface.c src/xen-interface.h
   17.62 +	$(CC) $(CFLAGS) $(WARN_FLAGS) -c -o $@ $<
   17.63 +
   17.64 +src/libxenstat.so.$(MAJOR): $(LIB)
   17.65 +	$(MAKE_LINK) $(<F) $@
   17.66 +
   17.67 +src/libxenstat.so: src/libxenstat.so.$(MAJOR)
   17.68 +	$(MAKE_LINK) $(<F) $@
   17.69 +
   17.70 +install: all
   17.71 +#install: all
   17.72 +#	$(INSTALL_DATA) src/xenstat.h $(DESTDIR)$(includedir)/xenstat.h
   17.73 +#	$(INSTALL_PROG) $(LIB) $(DESTDIR)$(libdir)/libxenstat.a
   17.74 +#	$(INSTALL_PROG) $(SHLIB) \
   17.75 +#	                $(DESTDIR)$(libdir)/libxenstat.so.$(MAJOR).$(MINOR)
   17.76 +#	$(MAKE_LINK) libxenstat.so.$(MAJOR).$(MINOR) \
   17.77 +#	             $(DESTDIR)$(libdir)/libxenstat.so.$(MAJOR)
   17.78 +#	$(MAKE_LINK) libxenstat.so.$(MAJOR) \
   17.79 +#	             $(DESTDIR)$(libdir)/libxenstat.so
   17.80 +#	-$(LDCONFIG)
   17.81 +
   17.82 +PYLIB=bindings/swig/python/_xenstat.so
   17.83 +PYMOD=bindings/swig/python/xenstat.py
   17.84 +PYSRC=bindings/swig/python/_xenstat.c
   17.85 +PERLLIB=bindings/swig/perl/xenstat.so
   17.86 +PERLMOD=bindings/swig/perl/xenstat.pm
   17.87 +PERLSRC=bindings/swig/perl/xenstat.c
   17.88 +BINDINGS=$(PYLIB) $(PYMOD) $(PERLLIB) $(PERLMOD)
   17.89 +BINDINGSRC=$(PYSRC) $(PERLSRC)
   17.90 +
   17.91 +# The all-bindings target builds all the language bindings
   17.92 +all-bindings: perl-bindings python-bindings
   17.93 +
   17.94 +# The install-bindings target installs all the language bindings
   17.95 +install-bindings: install-perl-bindings install-python-bindings
   17.96 +
   17.97 +$(BINDINGS): $(SHLIB) $(SHLIB_LINKS) src/xenstat.h
   17.98 +
   17.99 +SWIG_FLAGS=-module xenstat -Isrc
  17.100 +
  17.101 +# Python bindings
  17.102 +PYTHON_VERSION=2.3
  17.103 +PYTHON_FLAGS=-I/usr/include/python$(PYTHON_VERSION) -lpython$(PYTHON_VERSION)
  17.104 +$(PYSRC) $(PYMOD): bindings/swig/xenstat.i
  17.105 +	swig -python $(SWIG_FLAGS) -outdir $(@D) -o $(PYSRC) $<
  17.106 +
  17.107 +$(PYLIB): $(PYSRC)
  17.108 +	$(CC) $(CFLAGS) $(LDFLAGS) $(PYTHON_FLAGS) -shared -lxenstat -o $@ $<
  17.109 +
  17.110 +python-bindings: $(PYLIB) $(PYMOD)
  17.111 +
  17.112 +pythonlibdir=$(prefix)/lib/python$(PYTHON_VERSION)/site-packages
  17.113 +install-python-bindings: $(PYLIB) $(PYMOD)
  17.114 +	$(INSTALL_PROG) $(PYLIB) $(DESTDIR)$(pythonlibdir)/_xenstat.so
  17.115 +	$(INSTALL_PROG) $(PYMOD) $(DESTDIR)$(pythonlibdir)/xenstat.py
  17.116 +
  17.117 +ifeq ($(XENSTAT_PYTHON_BINDINGS),y)
  17.118 +all: python-bindings
  17.119 +install: install-python-bindings
  17.120 +endif
  17.121 +
  17.122 +# Perl bindings
  17.123 +PERL_FLAGS=`perl -MConfig -e 'print "$$Config{ccflags} -I$$Config{archlib}/CORE";'`
  17.124 +$(PERLSRC) $(PERLMOD): bindings/swig/xenstat.i
  17.125 +	swig -perl $(SWIG_FLAGS) -outdir $(@D) -o $(PERLSRC) $<
  17.126 +
  17.127 +$(PERLLIB): $(PERLSRC)
  17.128 +	$(CC) $(CFLAGS) $(LDFLAGS) $(PERL_FLAGS) -shared -lxenstat -o $@ $<
  17.129 +
  17.130 +perl-bindings: $(PERLLIB) $(PERLMOD)
  17.131 +
  17.132 +perllibdir=$(prefix)/lib/perl5
  17.133 +perlmoddir=$(prefix)/share/perl5
  17.134 +install-perl-bindings: $(PERLLIB) $(PERLMOD)
  17.135 +	$(INSTALL_PROG) $(PERLLIB) $(DESTDIR)$(perllibdir)/xenstat.so
  17.136 +	$(INSTALL_PROG) $(PERLMOD) $(DESTDIR)$(perlmoddir)/xenstat.pm
  17.137 +
  17.138 +ifeq ($(XENSTAT_PERL_BINDINGS),y)
  17.139 +all: perl-bindings
  17.140 +install: install-perl-bindings
  17.141 +endif
  17.142 +
  17.143 +clean:
  17.144 +	rm -f $(LIB) $(SHLIB) $(SHLIB_LINKS) $(OBJECTS) \
  17.145 +	      $(BINDINGS) $(BINDINGSRC)
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/tools/xenstat/libxenstat/bindings/swig/perl/.empty	Tue Aug 23 08:40:58 2005 +0000
    18.3 @@ -0,0 +1,1 @@
    18.4 +This directory is empty; this file is included to prevent version control systems from removing the directory.
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/tools/xenstat/libxenstat/bindings/swig/python/.empty	Tue Aug 23 08:40:58 2005 +0000
    19.3 @@ -0,0 +1,1 @@
    19.4 +This directory is empty; this file is included to prevent version control systems from removing the directory.
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/tools/xenstat/libxenstat/bindings/swig/xenstat.i	Tue Aug 23 08:40:58 2005 +0000
    20.3 @@ -0,0 +1,8 @@
    20.4 +%module xenstat_swig
    20.5 +%{
    20.6 +/* Includes the header in the wrapper code */
    20.7 +#include "xenstat.h"
    20.8 +%}
    20.9 +
   20.10 +/* Parse the header file to generate wrappers */
   20.11 +%include "xenstat.h"
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/tools/xenstat/libxenstat/src/xen-interface.c	Tue Aug 23 08:40:58 2005 +0000
    21.3 @@ -0,0 +1,144 @@
    21.4 +/* xen-interface.c
    21.5 + *
    21.6 + * Copyright (C) International Business Machines Corp., 2005
    21.7 + * Authors: Josh Triplett <josht@us.ibm.com>
    21.8 + *          Judy Fischbach <jfisch@us.ibm.com>
    21.9 + *
   21.10 + * This library is free software; you can redistribute it and/or
   21.11 + * modify it under the terms of the GNU Lesser General Public
   21.12 + * License as published by the Free Software Foundation; either
   21.13 + * version 2.1 of the License, or (at your option) any later version.
   21.14 + *
   21.15 + * This library is distributed in the hope that it will be useful,
   21.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   21.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   21.18 + * Lesser General Public License for more details.
   21.19 + */
   21.20 +
   21.21 +#include "xen-interface.h"
   21.22 +#include <fcntl.h>
   21.23 +#include <sys/ioctl.h>
   21.24 +#include <sys/mman.h>
   21.25 +#include <stdio.h>
   21.26 +#include <stdlib.h>
   21.27 +#include <unistd.h>
   21.28 +#include "privcmd.h"
   21.29 +#include "xen.h"
   21.30 +
   21.31 +struct xi_handle {
   21.32 +	int fd;
   21.33 +};
   21.34 +
   21.35 +/* Initialize for xen-interface.  Returns a handle to be used with subsequent
   21.36 + * calls to the xen-interface functions or NULL if an error occurs. */
   21.37 +xi_handle *xi_init()
   21.38 +{
   21.39 +	xi_handle *handle;
   21.40 +
   21.41 +	handle = (xi_handle *)calloc(1, sizeof(xi_handle));
   21.42 +	if (handle == NULL)
   21.43 +		return NULL;
   21.44 +
   21.45 +	handle->fd = open("/proc/xen/privcmd", O_RDWR);
   21.46 +	if (handle->fd < 0) {
   21.47 +		perror("Couldn't open /proc/xen/privcmd");
   21.48 +		free(handle);
   21.49 +		return NULL;
   21.50 +	}
   21.51 +
   21.52 +	return handle;
   21.53 +}
   21.54 +
   21.55 +/* Release the handle to libxc, free resources, etc. */
   21.56 +void xi_uninit(xi_handle *handle)
   21.57 +{
   21.58 +	close (handle->fd);
   21.59 +	free (handle);
   21.60 +}
   21.61 +
   21.62 +/* Make Xen hypervisor call */
   21.63 +int xi_make_dom0_op(xi_handle *handle, dom0_op_t *op, int opcode)
   21.64 +{
   21.65 +	privcmd_hypercall_t privcmd;
   21.66 +	int ret = 0;
   21.67 +
   21.68 +	/* set up for doing hypercall */
   21.69 +	privcmd.op = __HYPERVISOR_dom0_op;
   21.70 +	privcmd.arg[0] = (unsigned long)op;
   21.71 +	op->cmd = opcode;
   21.72 +	op->interface_version = DOM0_INTERFACE_VERSION;
   21.73 +
   21.74 +	if (mlock( &privcmd, sizeof(privcmd_hypercall_t)) < 0) {
   21.75 +		perror("Failed to mlock privcmd structure");
   21.76 +		return -1;
   21.77 +	}
   21.78 +
   21.79 +	if (mlock( op, sizeof(dom0_op_t)) < 0) {
   21.80 +		perror("Failed to mlock dom0_op structure");
   21.81 +		munlock( &privcmd, sizeof(privcmd_hypercall_t));
   21.82 +		return -1;
   21.83 +	}
   21.84 +
   21.85 +	if (ioctl( handle->fd, IOCTL_PRIVCMD_HYPERCALL, &privcmd) < 0) {
   21.86 +		perror("Hypercall failed");
   21.87 +		ret = -1;
   21.88 +	}
   21.89 +
   21.90 +	munlock( &privcmd, sizeof(privcmd_hypercall_t));
   21.91 +	munlock( op, sizeof(dom0_op_t));
   21.92 +
   21.93 +	return ret;
   21.94 +}
   21.95 +
   21.96 +/* Obtain domain data from dom0 */
   21.97 +int xi_get_physinfo(xi_handle *handle, dom0_physinfo_t *physinfo)
   21.98 +{
   21.99 +	dom0_op_t op;
  21.100 +
  21.101 +	if (xi_make_dom0_op(handle, &op, DOM0_PHYSINFO) < 0) {
  21.102 +		perror("DOM0_PHYSINFO Hypercall failed");
  21.103 +		return -1;
  21.104 +	}
  21.105 +
  21.106 +	*physinfo = op.u.physinfo;
  21.107 +	return 0;
  21.108 +}
  21.109 +
  21.110 +/* Obtain domain data from dom0 */
  21.111 +int xi_get_domaininfolist(xi_handle *handle, dom0_getdomaininfo_t *info,
  21.112 +                          unsigned int first_domain, unsigned int max_domains)
  21.113 +{
  21.114 +	dom0_op_t op;
  21.115 +	op.u.getdomaininfolist.first_domain = first_domain;
  21.116 +	op.u.getdomaininfolist.max_domains = max_domains;
  21.117 +	op.u.getdomaininfolist.buffer = info;
  21.118 +
  21.119 +	if (mlock( info, max_domains * sizeof(dom0_getdomaininfo_t)) < 0) {
  21.120 +		perror("Failed to mlock domaininfo array");
  21.121 +		return -1;
  21.122 +	}
  21.123 +
  21.124 +	if (xi_make_dom0_op(handle, &op, DOM0_GETDOMAININFOLIST) < 0) {
  21.125 +		perror("DOM0_GETDOMAININFOLIST Hypercall failed");
  21.126 +		return -1;
  21.127 +	}
  21.128 +
  21.129 +	return op.u.getdomaininfolist.num_domains;
  21.130 +}
  21.131 +
  21.132 +/* Returns cpu usage data from dom0 */
  21.133 +long long xi_get_vcpu_usage(xi_handle *handle, unsigned int domain,
  21.134 +                            unsigned int vcpu)
  21.135 +{
  21.136 +	dom0_op_t op;
  21.137 +	op.u.getvcpucontext.domain = domain;
  21.138 +	op.u.getvcpucontext.vcpu = vcpu;
  21.139 +	op.u.getvcpucontext.ctxt = NULL;
  21.140 +
  21.141 +	if (xi_make_dom0_op(handle, &op, DOM0_GETVCPUCONTEXT) < 0) {
  21.142 +		perror("DOM0_GETVCPUCONTEXT Hypercall failed");
  21.143 +		return -1;
  21.144 +	}
  21.145 +
  21.146 +	return op.u.getvcpucontext.cpu_time;
  21.147 +}
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/tools/xenstat/libxenstat/src/xen-interface.h	Tue Aug 23 08:40:58 2005 +0000
    22.3 @@ -0,0 +1,49 @@
    22.4 +/* xen-interface.h
    22.5 + *
    22.6 + * Copyright (C) International Business Machines Corp., 2005
    22.7 + * Authors: Josh Triplett <josht@us.ibm.com>
    22.8 + *          Judy Fischbach <jfisch@us.ibm.com>
    22.9 + *
   22.10 + * This library is free software; you can redistribute it and/or
   22.11 + * modify it under the terms of the GNU Lesser General Public
   22.12 + * License as published by the Free Software Foundation; either
   22.13 + * version 2.1 of the License, or (at your option) any later version.
   22.14 + *
   22.15 + * This library is distributed in the hope that it will be useful,
   22.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   22.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   22.18 + * Lesser General Public License for more details.
   22.19 + */
   22.20 +
   22.21 +#include <stdint.h>
   22.22 +
   22.23 +typedef int8_t   s8;
   22.24 +typedef int16_t  s16;
   22.25 +typedef int32_t  s32;
   22.26 +typedef int64_t  s64;
   22.27 +typedef uint8_t  u8;
   22.28 +typedef uint16_t u16;
   22.29 +typedef uint32_t u32;
   22.30 +typedef uint64_t u64;
   22.31 +
   22.32 +#include "dom0_ops.h"
   22.33 +
   22.34 +/* Opaque handles */
   22.35 +typedef struct xi_handle xi_handle;
   22.36 +
   22.37 +/* Initialize for xen-interface.  Returns a handle to be used with subsequent
   22.38 + * calls to the xen-interface functions or NULL if an error occurs. */
   22.39 +xi_handle *xi_init();
   22.40 +
   22.41 +/* Release the handle to libxc, free resources, etc. */
   22.42 +void xi_uninit(xi_handle *handle);
   22.43 +
   22.44 +/* Obtain physinfo data from dom0 */
   22.45 +int xi_get_physinfo(xi_handle *, dom0_physinfo_t *);
   22.46 +
   22.47 +/* Obtain domain data from dom0 */
   22.48 +int xi_get_domaininfolist(xi_handle *, dom0_getdomaininfo_t *, unsigned int,
   22.49 +                          unsigned int);
   22.50 +
   22.51 +/* Returns cpu usage data from dom0 */
   22.52 +long long xi_get_vcpu_usage(xi_handle *, unsigned int, unsigned int);
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/tools/xenstat/libxenstat/src/xenstat.c	Tue Aug 23 08:40:58 2005 +0000
    23.3 @@ -0,0 +1,620 @@
    23.4 +/* libxenstat: statistics-collection library for Xen
    23.5 + * Copyright (C) International Business Machines Corp., 2005
    23.6 + * Authors: Josh Triplett <josht@us.ibm.com>
    23.7 + *          Judy Fischbach <jfisch@us.ibm.com>
    23.8 + *          David Hendricks <dhendrix@us.ibm.com>
    23.9 + *
   23.10 + * This library is free software; you can redistribute it and/or
   23.11 + * modify it under the terms of the GNU Lesser General Public
   23.12 + * License as published by the Free Software Foundation; either
   23.13 + * version 2.1 of the License, or (at your option) any later version.
   23.14 + *
   23.15 + * This library is distributed in the hope that it will be useful,
   23.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   23.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   23.18 + * Lesser General Public License for more details.
   23.19 + */
   23.20 +
   23.21 +#include <limits.h>
   23.22 +#include <stdlib.h>
   23.23 +#include <stdio.h>
   23.24 +#include <string.h>
   23.25 +#include <unistd.h>
   23.26 +#include <xen-interface.h>
   23.27 +#include "xenstat.h"
   23.28 +
   23.29 +/*
   23.30 + * Types
   23.31 + */
   23.32 +struct xenstat_handle {
   23.33 +	xi_handle *xihandle;
   23.34 +	int page_size;
   23.35 +	FILE *procnetdev;
   23.36 +};
   23.37 +
   23.38 +struct xenstat_node {
   23.39 +	unsigned int flags;
   23.40 +	unsigned long long cpu_hz;
   23.41 +	unsigned int num_cpus;
   23.42 +	unsigned long long tot_mem;
   23.43 +	unsigned long long free_mem;
   23.44 +	unsigned int num_domains;
   23.45 +	xenstat_domain *domains;	/* Array of length num_domains */
   23.46 +};
   23.47 +
   23.48 +struct xenstat_domain {
   23.49 +	unsigned int id;
   23.50 +	unsigned int state;
   23.51 +	unsigned long long cpu_ns;
   23.52 +	unsigned int num_vcpus;
   23.53 +	xenstat_vcpu *vcpus;	/* Array of length num_vcpus */
   23.54 +	unsigned long long cur_mem;	/* Current memory reservation */
   23.55 +	unsigned long long max_mem;	/* Total memory allowed */
   23.56 +	unsigned int ssid;
   23.57 +	unsigned int num_networks;
   23.58 +	xenstat_network *networks;	/* Array of length num_networks */
   23.59 +};
   23.60 +
   23.61 +struct xenstat_vcpu {
   23.62 +	unsigned long long ns;
   23.63 +};
   23.64 +
   23.65 +struct xenstat_network {
   23.66 +	unsigned int id;
   23.67 +	/* Received */
   23.68 +	unsigned long long rbytes;
   23.69 +	unsigned long long rpackets;
   23.70 +	unsigned long long rerrs;
   23.71 +	unsigned long long rdrop;
   23.72 +	/* Transmitted */
   23.73 +	unsigned long long tbytes;
   23.74 +	unsigned long long tpackets;
   23.75 +	unsigned long long terrs;
   23.76 +	unsigned long long tdrop;
   23.77 +};
   23.78 +
   23.79 +/*
   23.80 + * Data-collection types
   23.81 + */
   23.82 +/* Called to collect the information for the node and all the domains on
   23.83 + * it. When called, the domain information has already been collected. */
   23.84 +typedef int (*xenstat_collect_func)(xenstat_handle * handle,
   23.85 +				    xenstat_node * node);
   23.86 +/* Called to free the information collected by the collect function.  The free
   23.87 + * function will only be called on a xenstat_node if that node includes
   23.88 + * information collected by the corresponding collector. */
   23.89 +typedef void (*xenstat_free_func)(xenstat_node * node);
   23.90 +/* Called to free any information stored in the handle.  Note the lack of a
   23.91 + * matching init function; the collect functions should initialize on first
   23.92 + * use.  Also, the uninit function must handle the case that the collector has
   23.93 + * never been initialized. */
   23.94 +typedef void (*xenstat_uninit_func)(xenstat_handle * handle);
   23.95 +typedef struct xenstat_collector {
   23.96 +	unsigned int flag;
   23.97 +	xenstat_collect_func collect;
   23.98 +	xenstat_free_func free;
   23.99 +	xenstat_uninit_func uninit;
  23.100 +} xenstat_collector;
  23.101 +
  23.102 +static int  xenstat_collect_vcpus(xenstat_handle * handle,
  23.103 +				  xenstat_node * node);
  23.104 +static int  xenstat_collect_networks(xenstat_handle * handle,
  23.105 +				    xenstat_node * node);
  23.106 +static void xenstat_free_vcpus(xenstat_node * node);
  23.107 +static void xenstat_free_networks(xenstat_node * node);
  23.108 +static void xenstat_uninit_vcpus(xenstat_handle * handle);
  23.109 +static void xenstat_uninit_networks(xenstat_handle * handle);
  23.110 +
  23.111 +static xenstat_collector collectors[] = {
  23.112 +	{ XENSTAT_VCPU, xenstat_collect_vcpus,
  23.113 +	  xenstat_free_vcpus, xenstat_uninit_vcpus },
  23.114 +	{ XENSTAT_NETWORK, xenstat_collect_networks,
  23.115 +	  xenstat_free_networks, xenstat_uninit_networks }
  23.116 +};
  23.117 +
  23.118 +#define NUM_COLLECTORS (sizeof(collectors)/sizeof(xenstat_collector))
  23.119 +
  23.120 +/*
  23.121 + * libxenstat API
  23.122 + */
  23.123 +xenstat_handle *xenstat_init()
  23.124 +{
  23.125 +	xenstat_handle *handle;
  23.126 +
  23.127 +	handle = (xenstat_handle *) calloc(1, sizeof(xenstat_handle));
  23.128 +	if (handle == NULL)
  23.129 +		return NULL;
  23.130 +
  23.131 +#if defined(PAGESIZE)
  23.132 +	handle->page_size = PAGESIZE;
  23.133 +#elif defined(PAGE_SIZE)
  23.134 +	handle->page_size = PAGE_SIZE;
  23.135 +#else
  23.136 +	handle->page_size = sysconf(_SC_PAGE_SIZE);
  23.137 +	if (handle->page_size < 0) {
  23.138 +		perror("Failed to retrieve page size.");
  23.139 +		free(handle);
  23.140 +		return NULL;
  23.141 +	}
  23.142 +#endif
  23.143 +
  23.144 +	handle->xihandle = xi_init();
  23.145 +	if (handle->xihandle == NULL) {
  23.146 +		perror("xi_init");
  23.147 +		free(handle);
  23.148 +		return NULL;
  23.149 +	}
  23.150 +
  23.151 +	return handle;
  23.152 +}
  23.153 +
  23.154 +void xenstat_uninit(xenstat_handle * handle)
  23.155 +{
  23.156 +	unsigned int i;
  23.157 +	if (handle) {
  23.158 +		for (i = 0; i < NUM_COLLECTORS; i++)
  23.159 +			collectors[i].uninit(handle);
  23.160 +		xi_uninit(handle->xihandle);
  23.161 +		free(handle);
  23.162 +	}
  23.163 +}
  23.164 +
  23.165 +xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags)
  23.166 +{
  23.167 +#define DOMAIN_CHUNK_SIZE 256
  23.168 +	xenstat_node *node;
  23.169 +	dom0_physinfo_t physinfo;
  23.170 +	dom0_getdomaininfo_t domaininfo[DOMAIN_CHUNK_SIZE];
  23.171 +	unsigned int num_domains, new_domains;
  23.172 +	unsigned int i;
  23.173 +
  23.174 +	/* Create the node */
  23.175 +	node = (xenstat_node *) calloc(1, sizeof(xenstat_node));
  23.176 +	if (node == NULL)
  23.177 +		return NULL;
  23.178 +
  23.179 +	/* Get information about the physical system */
  23.180 +	if (xi_get_physinfo(handle->xihandle, &physinfo) < 0) {
  23.181 +		free(node);
  23.182 +		return NULL;
  23.183 +	}
  23.184 +
  23.185 +	node->cpu_hz = ((unsigned long long)physinfo.cpu_khz) * 1000ULL;
  23.186 +	node->num_cpus =
  23.187 +	    (physinfo.threads_per_core * physinfo.cores_per_socket *
  23.188 +	     physinfo.sockets_per_node * physinfo.nr_nodes);
  23.189 +	node->tot_mem = ((unsigned long long)physinfo.total_pages)
  23.190 +	    * handle->page_size;
  23.191 +	node->free_mem = ((unsigned long long)physinfo.free_pages)
  23.192 +	    * handle->page_size;
  23.193 +
  23.194 +	/* malloc(0) is not portable, so allocate a single domain.  This will
  23.195 +	 * be resized below. */
  23.196 +	node->domains = malloc(sizeof(xenstat_domain));
  23.197 +	if (node->domains == NULL) {
  23.198 +		free(node);
  23.199 +		return NULL;
  23.200 +	}
  23.201 +
  23.202 +	num_domains = 0;
  23.203 +	do {
  23.204 +		xenstat_domain *domain;
  23.205 +
  23.206 +		new_domains = xi_get_domaininfolist(handle->xihandle,
  23.207 +		                                    domaininfo, num_domains,
  23.208 +		                                    DOMAIN_CHUNK_SIZE);
  23.209 +
  23.210 +		node->domains = realloc(node->domains,
  23.211 +					(num_domains + new_domains)
  23.212 +					* sizeof(xenstat_domain));
  23.213 +		if (node->domains == NULL) {
  23.214 +			free(node);
  23.215 +			return NULL;
  23.216 +		}
  23.217 +
  23.218 +		domain = node->domains + num_domains;
  23.219 +
  23.220 +		for (i = 0; i < new_domains; i++) {
  23.221 +			/* Fill in domain using domaininfo[i] */
  23.222 +			domain->id = domaininfo[i].domain;
  23.223 +			domain->state = domaininfo[i].flags;
  23.224 +			domain->cpu_ns = domaininfo[i].cpu_time;
  23.225 +			domain->num_vcpus = domaininfo[i].n_vcpu;
  23.226 +			domain->vcpus = NULL;
  23.227 +			domain->cur_mem =
  23.228 +			    ((unsigned long long)domaininfo[i].tot_pages)
  23.229 +			    * handle->page_size;
  23.230 +			domain->max_mem =
  23.231 +			    domaininfo[i].max_pages == UINT_MAX
  23.232 +			    ? (unsigned long long)-1
  23.233 +			    : (unsigned long long)(domaininfo[i].max_pages
  23.234 +						   * handle->page_size);
  23.235 +			domain->ssid = domaininfo[i].ssidref;
  23.236 +			domain->num_networks = 0;
  23.237 +			domain->networks = NULL;
  23.238 +
  23.239 +			domain++;
  23.240 +		}
  23.241 +		num_domains += new_domains;
  23.242 +	} while (new_domains == DOMAIN_CHUNK_SIZE);
  23.243 +	node->num_domains = num_domains;
  23.244 +
  23.245 +	/* Run all the extra data collectors requested */
  23.246 +	node->flags = 0;
  23.247 +	for (i = 0; i < NUM_COLLECTORS; i++) {
  23.248 +		if ((flags & collectors[i].flag) == collectors[i].flag) {
  23.249 +			node->flags |= collectors[i].flag;
  23.250 +			if(collectors[i].collect(handle, node) == 0) {
  23.251 +				xenstat_free_node(node);
  23.252 +				return NULL;
  23.253 +                        }
  23.254 +                }
  23.255 +	}
  23.256 +
  23.257 +	return node;
  23.258 +}
  23.259 +
  23.260 +void xenstat_free_node(xenstat_node * node)
  23.261 +{
  23.262 +	int i;
  23.263 +
  23.264 +	if (node) {
  23.265 +		if (node->domains) {
  23.266 +			for (i = 0; i < NUM_COLLECTORS; i++)
  23.267 +				if((node->flags & collectors[i].flag)
  23.268 +				   == collectors[i].flag)
  23.269 +					collectors[i].free(node);
  23.270 +			free(node->domains);
  23.271 +		}
  23.272 +		free(node);
  23.273 +	}
  23.274 +}
  23.275 +
  23.276 +xenstat_domain *xenstat_node_domain(xenstat_node * node, unsigned int domid)
  23.277 +{
  23.278 +	unsigned int i;
  23.279 +
  23.280 +	/* FIXME: binary search */
  23.281 +	/* Find the appropriate domain entry in the node struct. */
  23.282 +	for (i = 0; i < node->num_domains; i++) {
  23.283 +		if (node->domains[i].id == domid)
  23.284 +			return &(node->domains[i]);
  23.285 +	}
  23.286 +	return NULL;
  23.287 +}
  23.288 +
  23.289 +xenstat_domain *xenstat_node_domain_by_index(xenstat_node * node,
  23.290 +					     unsigned int index)
  23.291 +{
  23.292 +	if (0 <= index && index < node->num_domains)
  23.293 +		return &(node->domains[index]);
  23.294 +	return NULL;
  23.295 +}
  23.296 +
  23.297 +unsigned long long xenstat_node_tot_mem(xenstat_node * node)
  23.298 +{
  23.299 +	return node->tot_mem;
  23.300 +}
  23.301 +
  23.302 +unsigned long long xenstat_node_free_mem(xenstat_node * node)
  23.303 +{
  23.304 +	return node->free_mem;
  23.305 +}
  23.306 +
  23.307 +unsigned int xenstat_node_num_domains(xenstat_node * node)
  23.308 +{
  23.309 +	return node->num_domains;
  23.310 +}
  23.311 +
  23.312 +unsigned int xenstat_node_num_cpus(xenstat_node * node)
  23.313 +{
  23.314 +	return node->num_cpus;
  23.315 +}
  23.316 +
  23.317 +/* Get information about the CPU speed */
  23.318 +unsigned long long xenstat_node_cpu_hz(xenstat_node * node)
  23.319 +{
  23.320 +	return node->cpu_hz;
  23.321 +}
  23.322 +
  23.323 +/* Get the domain ID for this domain */
  23.324 +unsigned xenstat_domain_id(xenstat_domain * domain)
  23.325 +{
  23.326 +	return domain->id;
  23.327 +}
  23.328 +
  23.329 +/* Get information about how much CPU time has been used */
  23.330 +unsigned long long xenstat_domain_cpu_ns(xenstat_domain * domain)
  23.331 +{
  23.332 +	return domain->cpu_ns;
  23.333 +}
  23.334 +
  23.335 +/* Find the number of VCPUs allocated to a domain */
  23.336 +unsigned int xenstat_domain_num_vcpus(xenstat_domain * domain)
  23.337 +{
  23.338 +	return domain->num_vcpus;
  23.339 +}
  23.340 +
  23.341 +xenstat_vcpu *xenstat_domain_vcpu(xenstat_domain * domain, unsigned int vcpu)
  23.342 +{
  23.343 +	if (0 <= vcpu && vcpu < domain->num_vcpus)
  23.344 +		return &(domain->vcpus[vcpu]);
  23.345 +	return NULL;
  23.346 +}
  23.347 +
  23.348 +/* Find the current memory reservation for this domain */
  23.349 +unsigned long long xenstat_domain_cur_mem(xenstat_domain * domain)
  23.350 +{
  23.351 +	return domain->cur_mem;
  23.352 +}
  23.353 +
  23.354 +/* Find the maximum memory reservation for this domain */
  23.355 +unsigned long long xenstat_domain_max_mem(xenstat_domain * domain)
  23.356 +{
  23.357 +	return domain->max_mem;
  23.358 +}
  23.359 +
  23.360 +/* Find the domain's SSID */
  23.361 +unsigned int xenstat_domain_ssid(xenstat_domain * domain)
  23.362 +{
  23.363 +	return domain->ssid;
  23.364 +}
  23.365 +
  23.366 +/* Get domain states */
  23.367 +unsigned int xenstat_domain_dying(xenstat_domain * domain)
  23.368 +{
  23.369 +	return (domain->state & DOMFLAGS_DYING) == DOMFLAGS_DYING;
  23.370 +}
  23.371 +
  23.372 +unsigned int xenstat_domain_crashed(xenstat_domain * domain)
  23.373 +{
  23.374 +	return ((domain->state & DOMFLAGS_SHUTDOWN) == DOMFLAGS_SHUTDOWN)
  23.375 +	    && (((domain->state >> DOMFLAGS_SHUTDOWNSHIFT)
  23.376 +		 & DOMFLAGS_SHUTDOWNMASK) == SHUTDOWN_crash);
  23.377 +}
  23.378 +
  23.379 +unsigned int xenstat_domain_shutdown(xenstat_domain * domain)
  23.380 +{
  23.381 +	return ((domain->state & DOMFLAGS_SHUTDOWN) == DOMFLAGS_SHUTDOWN)
  23.382 +	    && (((domain->state >> DOMFLAGS_SHUTDOWNSHIFT)
  23.383 +		 & DOMFLAGS_SHUTDOWNMASK) != SHUTDOWN_crash);
  23.384 +}
  23.385 +
  23.386 +unsigned int xenstat_domain_paused(xenstat_domain * domain)
  23.387 +{
  23.388 +	return (domain->state & DOMFLAGS_PAUSED) == DOMFLAGS_PAUSED;
  23.389 +}
  23.390 +
  23.391 +unsigned int xenstat_domain_blocked(xenstat_domain * domain)
  23.392 +{
  23.393 +	return (domain->state & DOMFLAGS_BLOCKED) == DOMFLAGS_BLOCKED;
  23.394 +}
  23.395 +
  23.396 +unsigned int xenstat_domain_running(xenstat_domain * domain)
  23.397 +{
  23.398 +	return (domain->state & DOMFLAGS_RUNNING) == DOMFLAGS_RUNNING;
  23.399 +}
  23.400 +
  23.401 +/* Get the number of networks for a given domain */
  23.402 +unsigned int xenstat_domain_num_networks(xenstat_domain * domain)
  23.403 +{
  23.404 +	return domain->num_networks;
  23.405 +}
  23.406 +
  23.407 +/* Get the network handle to obtain network stats */
  23.408 +xenstat_network *xenstat_domain_network(xenstat_domain * domain,
  23.409 +					unsigned int network)
  23.410 +{
  23.411 +	if (domain->networks && 0 <= network && network < domain->num_networks)
  23.412 +		return &(domain->networks[network]);
  23.413 +	return NULL;
  23.414 +}
  23.415 +
  23.416 +/*
  23.417 + * VCPU functions
  23.418 + */
  23.419 +/* Collect information about VCPUs */
  23.420 +static int xenstat_collect_vcpus(xenstat_handle * handle, xenstat_node * node)
  23.421 +{
  23.422 +	unsigned int i, vcpu;
  23.423 +	/* Fill in VCPU information */
  23.424 +	for (i = 0; i < node->num_domains; i++) {
  23.425 +		node->domains[i].vcpus = malloc(node->domains[i].num_vcpus
  23.426 +						* sizeof(xenstat_vcpu));
  23.427 +		if (node->domains[i].vcpus == NULL)
  23.428 +			return 0;
  23.429 +
  23.430 +		for (vcpu = 0; vcpu < node->domains[i].num_vcpus; vcpu++) {
  23.431 +			/* FIXME: need to be using a more efficient mechanism*/
  23.432 +			long long vcpu_time;
  23.433 +			vcpu_time =
  23.434 +			    xi_get_vcpu_usage(handle->xihandle,
  23.435 +					      node->domains[i].id,
  23.436 +					      vcpu);
  23.437 +			if (vcpu_time < 0)
  23.438 +				return 0;
  23.439 +			node->domains[i].vcpus[vcpu].ns = vcpu_time;
  23.440 +		}
  23.441 +	}
  23.442 +	return 1;
  23.443 +}
  23.444 +
  23.445 +/* Free VCPU information */
  23.446 +static void xenstat_free_vcpus(xenstat_node * node)
  23.447 +{
  23.448 +	unsigned int i;
  23.449 +	for (i = 0; i < node->num_domains; i++)
  23.450 +		free(node->domains[i].vcpus);
  23.451 +}
  23.452 +
  23.453 +/* Free VCPU information in handle - nothing to do */
  23.454 +static void xenstat_uninit_vcpus(xenstat_handle * handle)
  23.455 +{
  23.456 +}
  23.457 +
  23.458 +/* Get VCPU usage */
  23.459 +unsigned long long xenstat_vcpu_ns(xenstat_vcpu * vcpu)
  23.460 +{
  23.461 +	return vcpu->ns;
  23.462 +}
  23.463 +
  23.464 +/*
  23.465 + * Network functions
  23.466 + */
  23.467 +
  23.468 +/* Expected format of /proc/net/dev */
  23.469 +static const char PROCNETDEV_HEADER[] =
  23.470 +    "Inter-|   Receive                                                |"
  23.471 +    "  Transmit\n"
  23.472 +    " face |bytes    packets errs drop fifo frame compressed multicast|"
  23.473 +    "bytes    packets errs drop fifo colls carrier compressed\n";
  23.474 +
  23.475 +/* Collect information about networks */
  23.476 +static int xenstat_collect_networks(xenstat_handle * handle,
  23.477 +				    xenstat_node * node)
  23.478 +{
  23.479 +	/* Open and validate /proc/net/dev if we haven't already */
  23.480 +	if (handle->procnetdev == NULL) {
  23.481 +		char header[sizeof(PROCNETDEV_HEADER)];
  23.482 +		handle->procnetdev = fopen("/proc/net/dev", "r");
  23.483 +		if (handle->procnetdev == NULL) {
  23.484 +			perror("Error opening /proc/net/dev");
  23.485 +			return 1;
  23.486 +		}
  23.487 +
  23.488 +		/* Validate the format of /proc/net/dev */
  23.489 +		if (fread(header, sizeof(PROCNETDEV_HEADER) - 1, 1,
  23.490 +			  handle->procnetdev) != 1) {
  23.491 +			perror("Error reading /proc/net/dev header");
  23.492 +			return 1;
  23.493 +		}
  23.494 +		header[sizeof(PROCNETDEV_HEADER) - 1] = '\0';
  23.495 +		if (strcmp(header, PROCNETDEV_HEADER) != 0) {
  23.496 +			fprintf(stderr,
  23.497 +				"Unexpected /proc/net/dev format\n");
  23.498 +			return 1;
  23.499 +		}
  23.500 +	}
  23.501 +
  23.502 +	/* Fill in networks */
  23.503 +	/* FIXME: optimize this */
  23.504 +	fseek(handle->procnetdev, sizeof(PROCNETDEV_HEADER) - 1, SEEK_SET);
  23.505 +	while (1) {
  23.506 +		xenstat_domain *domain;
  23.507 +		xenstat_network net;
  23.508 +		unsigned int domid;
  23.509 +		int ret = fscanf(handle->procnetdev,
  23.510 +				 "vif%u.%u:%llu%llu%llu%llu%*u%*u%*u%*u"
  23.511 +				 "%llu%llu%llu%llu%*u%*u%*u%*u",
  23.512 +				 &domid, &net.id,
  23.513 +				 &net.tbytes, &net.tpackets, &net.terrs,
  23.514 +				 &net.tdrop,
  23.515 +				 &net.rbytes, &net.rpackets, &net.rerrs,
  23.516 +				 &net.rdrop);
  23.517 +		if (ret == EOF)
  23.518 +			break;
  23.519 +		if (ret != 10) {
  23.520 +			unsigned int c;
  23.521 +			do {
  23.522 +				c = fgetc(handle->procnetdev);
  23.523 +			} while (c != '\n' && c != EOF);
  23.524 +			if (c == EOF)
  23.525 +				break;
  23.526 +			continue;
  23.527 +		}
  23.528 +
  23.529 +		/* FIXME: this does a search for the domid */
  23.530 +		domain = xenstat_node_domain(node, domid);
  23.531 +		if (domain == NULL) {
  23.532 +			fprintf(stderr,
  23.533 +				"Found interface vif%u.%u but domain %u"
  23.534 +				" does not exist.\n", domid, net.id,
  23.535 +				domid);
  23.536 +			continue;
  23.537 +		}
  23.538 +		if (domain->networks == NULL) {
  23.539 +			domain->num_networks = 1;
  23.540 +			domain->networks = malloc(sizeof(xenstat_network));
  23.541 +		} else {
  23.542 +			domain->num_networks++;
  23.543 +			domain->networks =
  23.544 +			    realloc(domain->networks,
  23.545 +				    domain->num_networks *
  23.546 +				    sizeof(xenstat_network));
  23.547 +		}
  23.548 +		if (domain->networks == NULL)
  23.549 +			return 1;
  23.550 +		domain->networks[domain->num_networks - 1] = net;
  23.551 +	}
  23.552 +
  23.553 +	return 1;
  23.554 +}
  23.555 +
  23.556 +/* Free network information */
  23.557 +static void xenstat_free_networks(xenstat_node * node)
  23.558 +{
  23.559 +	unsigned int i;
  23.560 +	for (i = 0; i < node->num_domains; i++)
  23.561 +		free(node->domains[i].networks);
  23.562 +}
  23.563 +
  23.564 +/* Free network information in handle */
  23.565 +static void xenstat_uninit_networks(xenstat_handle * handle)
  23.566 +{
  23.567 +	if(handle->procnetdev)
  23.568 +		fclose(handle->procnetdev);
  23.569 +}
  23.570 +
  23.571 +/* Get the network ID */
  23.572 +unsigned int xenstat_network_id(xenstat_network * network)
  23.573 +{
  23.574 +	return network->id;
  23.575 +}
  23.576 +
  23.577 +/* Get the number of receive bytes */
  23.578 +unsigned long long xenstat_network_rbytes(xenstat_network * network)
  23.579 +{
  23.580 +	return network->rbytes;
  23.581 +}
  23.582 +
  23.583 +/* Get the number of receive packets */
  23.584 +unsigned long long xenstat_network_rpackets(xenstat_network * network)
  23.585 +{
  23.586 +	return network->rpackets;
  23.587 +}
  23.588 +
  23.589 +/* Get the number of receive errors */
  23.590 +unsigned long long xenstat_network_rerrs(xenstat_network * network)
  23.591 +{
  23.592 +	return network->rerrs;
  23.593 +}
  23.594 +
  23.595 +/* Get the number of receive drops */
  23.596 +unsigned long long xenstat_network_rdrop(xenstat_network * network)
  23.597 +{
  23.598 +	return network->rdrop;
  23.599 +}
  23.600 +
  23.601 +/* Get the number of transmit bytes */
  23.602 +unsigned long long xenstat_network_tbytes(xenstat_network * network)
  23.603 +{
  23.604 +	return network->tbytes;
  23.605 +}
  23.606 +
  23.607 +/* Get the number of transmit packets */
  23.608 +unsigned long long xenstat_network_tpackets(xenstat_network * network)
  23.609 +{
  23.610 +	return network->tpackets;
  23.611 +}
  23.612 +
  23.613 +/* Get the number of transmit errors */
  23.614 +unsigned long long xenstat_network_terrs(xenstat_network * network)
  23.615 +{
  23.616 +	return network->terrs;
  23.617 +}
  23.618 +
  23.619 +/* Get the number of transmit dropped packets */
  23.620 +unsigned long long xenstat_network_tdrop(xenstat_network * network)
  23.621 +{
  23.622 +	return network->tdrop;
  23.623 +}
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/tools/xenstat/libxenstat/src/xenstat.h	Tue Aug 23 08:40:58 2005 +0000
    24.3 @@ -0,0 +1,148 @@
    24.4 +/* libxenstat: statistics-collection library for Xen
    24.5 + * Copyright (C) International Business Machines Corp., 2005
    24.6 + * Authors: Josh Triplett <josht@us.ibm.com>
    24.7 + *          Judy Fischbach <jfisch@us.ibm.com>
    24.8 + *          David Hendricks <dhendrix@us.ibm.com>
    24.9 + *
   24.10 + * This library is free software; you can redistribute it and/or
   24.11 + * modify it under the terms of the GNU Lesser General Public
   24.12 + * License as published by the Free Software Foundation; either
   24.13 + * version 2.1 of the License, or (at your option) any later version.
   24.14 + *
   24.15 + * This library is distributed in the hope that it will be useful,
   24.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   24.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   24.18 + * Lesser General Public License for more details.
   24.19 + */
   24.20 +
   24.21 +/* libxenstat API */
   24.22 +
   24.23 +/* Opaque handles */
   24.24 +typedef struct xenstat_handle xenstat_handle;
   24.25 +typedef struct xenstat_domain xenstat_domain;
   24.26 +typedef struct xenstat_node xenstat_node;
   24.27 +typedef struct xenstat_vcpu xenstat_vcpu;
   24.28 +typedef struct xenstat_network xenstat_network;
   24.29 +
   24.30 +/* Initialize the xenstat library.  Returns a handle to be used with
   24.31 + * subsequent calls to the xenstat library, or NULL if an error occurs. */
   24.32 +xenstat_handle *xenstat_init();
   24.33 +
   24.34 +/* Release the handle to libxc, free resources, etc. */
   24.35 +void xenstat_uninit(xenstat_handle * handle);
   24.36 +
   24.37 +/* Get all available information about a node */
   24.38 +#define XENSTAT_VCPU 0x1
   24.39 +#define XENSTAT_NETWORK 0x2
   24.40 +#define XENSTAT_ALL (XENSTAT_VCPU|XENSTAT_NETWORK)
   24.41 +xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags);
   24.42 +
   24.43 +/* Free the information */
   24.44 +void xenstat_free_node(xenstat_node * node);
   24.45 +
   24.46 +/*
   24.47 + * Node functions - extract information from a xenstat_node
   24.48 + */
   24.49 +
   24.50 +/* Get information about the domain with the given domain ID */
   24.51 +xenstat_domain *xenstat_node_domain(xenstat_node * node,
   24.52 +				    unsigned int domid);
   24.53 +
   24.54 +/* Get the domain with the given index; used to loop over all domains. */
   24.55 +xenstat_domain *xenstat_node_domain_by_index(xenstat_node * node,
   24.56 +					     unsigned index);
   24.57 +
   24.58 +/* Get amount of total memory on a node */
   24.59 +unsigned long long xenstat_node_tot_mem(xenstat_node * node);
   24.60 +
   24.61 +/* Get amount of free memory on a node */
   24.62 +unsigned long long xenstat_node_free_mem(xenstat_node * node);
   24.63 +
   24.64 +/* Find the number of domains existing on a node */
   24.65 +unsigned int xenstat_node_num_domains(xenstat_node * node);
   24.66 +
   24.67 +/* Find the number of CPUs existing on a node */
   24.68 +unsigned int xenstat_node_num_cpus(xenstat_node * node);
   24.69 +
   24.70 +/* Get information about the CPU speed */
   24.71 +unsigned long long xenstat_node_cpu_hz(xenstat_node * node);
   24.72 +
   24.73 +/*
   24.74 + * Domain functions - extract information from a xenstat_domain
   24.75 + */
   24.76 +
   24.77 +/* Get the domain ID for this domain */
   24.78 +unsigned xenstat_domain_id(xenstat_domain * domain);
   24.79 +
   24.80 +/* Get information about how much CPU time has been used */
   24.81 +unsigned long long xenstat_domain_cpu_ns(xenstat_domain * domain);
   24.82 +
   24.83 +/* Find the number of VCPUs allocated to a domain */
   24.84 +unsigned int xenstat_domain_num_vcpus(xenstat_domain * domain);
   24.85 +
   24.86 +/* Get the VCPU handle to obtain VCPU stats */
   24.87 +xenstat_vcpu *xenstat_domain_vcpu(xenstat_domain * domain,
   24.88 +				  unsigned int vcpu);
   24.89 +
   24.90 +/* Find the current memory reservation for this domain */
   24.91 +unsigned long long xenstat_domain_cur_mem(xenstat_domain * domain);
   24.92 +
   24.93 +/* Find the maximum memory reservation for this domain */
   24.94 +unsigned long long xenstat_domain_max_mem(xenstat_domain * domain);
   24.95 +
   24.96 +/* Find the domain's SSID */
   24.97 +unsigned int xenstat_domain_ssid(xenstat_domain * domain);
   24.98 +
   24.99 +/* Get domain states */
  24.100 +unsigned int xenstat_domain_dying(xenstat_domain * domain);
  24.101 +unsigned int xenstat_domain_crashed(xenstat_domain * domain);
  24.102 +unsigned int xenstat_domain_shutdown(xenstat_domain * domain);
  24.103 +unsigned int xenstat_domain_paused(xenstat_domain * domain);
  24.104 +unsigned int xenstat_domain_blocked(xenstat_domain * domain);
  24.105 +unsigned int xenstat_domain_running(xenstat_domain * domain);
  24.106 +
  24.107 +/* Get the number of networks for a given domain */
  24.108 +unsigned int xenstat_domain_num_networks(xenstat_domain *);
  24.109 +
  24.110 +/* Get the network handle to obtain network stats */
  24.111 +xenstat_network *xenstat_domain_network(xenstat_domain * domain,
  24.112 +					unsigned int network);
  24.113 +
  24.114 +/*
  24.115 + * VCPU functions - extract information from a xenstat_vcpu
  24.116 + */
  24.117 +
  24.118 +/* Get VCPU usage */
  24.119 +unsigned long long xenstat_vcpu_ns(xenstat_vcpu * vcpu);
  24.120 +
  24.121 +
  24.122 +/*
  24.123 + * Network functions - extract information from a xenstat_network
  24.124 + */
  24.125 +
  24.126 +/* Get the ID for this network */
  24.127 +unsigned int xenstat_network_id(xenstat_network * network);
  24.128 +
  24.129 +/* Get the number of receive bytes for this network */
  24.130 +unsigned long long xenstat_network_rbytes(xenstat_network * network);
  24.131 +
  24.132 +/* Get the number of receive packets for this network */
  24.133 +unsigned long long xenstat_network_rpackets(xenstat_network * network);
  24.134 +
  24.135 +/* Get the number of receive errors for this network */
  24.136 +unsigned long long xenstat_network_rerrs(xenstat_network * network);
  24.137 +
  24.138 +/* Get the number of receive drops for this network */
  24.139 +unsigned long long xenstat_network_rdrop(xenstat_network * network);
  24.140 +
  24.141 +/* Get the number of transmit bytes for this network */
  24.142 +unsigned long long xenstat_network_tbytes(xenstat_network * network);
  24.143 +
  24.144 +/* Get the number of transmit packets for this network */
  24.145 +unsigned long long xenstat_network_tpackets(xenstat_network * network);
  24.146 +
  24.147 +/* Get the number of transmit errors for this network */
  24.148 +unsigned long long xenstat_network_terrs(xenstat_network * network);
  24.149 +
  24.150 +/* Get the number of transmit drops for this network */
  24.151 +unsigned long long xenstat_network_tdrop(xenstat_network * network);
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/tools/xenstat/xentop/Makefile	Tue Aug 23 08:40:58 2005 +0000
    25.3 @@ -0,0 +1,44 @@
    25.4 +# Copyright (C) International Business Machines Corp., 2005
    25.5 +# Author: Josh Triplett <josht@us.ibm.com>
    25.6 +# 
    25.7 +# This program is free software; you can redistribute it and/or modify
    25.8 +# it under the terms of the GNU General Public License as published by
    25.9 +# the Free Software Foundation; under version 2 of the License.
   25.10 +#
   25.11 +# This program is distributed in the hope that it will be useful,
   25.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   25.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   25.14 +# GNU General Public License for more details.
   25.15 +
   25.16 +XEN_ROOT=../../..
   25.17 +include $(XEN_ROOT)/tools/Rules.mk
   25.18 +
   25.19 +ifneq ($(XENSTAT_XENTOP),y)
   25.20 +all install xentop:
   25.21 +else
   25.22 +
   25.23 +INSTALL         = install
   25.24 +INSTALL_PROG    = $(INSTALL) -m0755 -D
   25.25 +INSTALL_DATA    = $(INSTALL) -m0644 -D
   25.26 +
   25.27 +prefix=/usr
   25.28 +mandir=$(prefix)/share/man
   25.29 +man1dir=$(mandir)/man1
   25.30 +sbindir=$(prefix)/sbin
   25.31 +
   25.32 +CFLAGS += -DGCC_PRINTF -Wall -Werror -I$(XEN_LIBXENSTAT)
   25.33 +LDFLAGS += -L$(XEN_LIBXENSTAT)
   25.34 +LDLIBS += -lxenstat -lcurses
   25.35 +
   25.36 +all: xentop
   25.37 +
   25.38 +xentop: xentop.o
   25.39 +
   25.40 +install: xentop xentop.1
   25.41 +	$(INSTALL_PROG) xentop $(DESTDIR)$(sbindir)/xentop
   25.42 +	$(INSTALL_DATA) xentop.1 $(DESTDIR)$(man1dir)/xentop.1
   25.43 +
   25.44 +endif
   25.45 +
   25.46 +clean:
   25.47 +	rm -f xentop xentop.o
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/tools/xenstat/xentop/TODO	Tue Aug 23 08:40:58 2005 +0000
    26.3 @@ -0,0 +1,34 @@
    26.4 +Display error messages on the help line after bad input at a prompt.
    26.5 +Fractional delay times
    26.6 +Use prompting to search for domains
    26.7 +Better line editing?
    26.8 +
    26.9 +* Make CPU in % more accurate
   26.10 +* Domain total network TX % and RX %
   26.11 +
   26.12 +Like Top, f feature, field select of domain columns, toggle the display of
   26.13 +field by typing the letter associated with field, if displayed it shows in
   26.14 +bold and the letter is Capitalized along with a leading asterisk for the
   26.15 +field, if not selected for display letter is lowercase, no leading asterisk
   26.16 +and field is not bolded.
   26.17 +
   26.18 +Like Top, ordering of domain columns, o feature Capital letter shifts left,
   26.19 +lowercase letter shifts right?
   26.20 +
   26.21 +Color
   26.22 +Full management: pause, destroy, create domains
   26.23 +
   26.24 +Add support for Virtual Block Devices (vbd)
   26.25 +
   26.26 +To think about:
   26.27 +Support for one than one node display (distributed monitoring 
   26.28 +from any node of all other nodes in a cluster)
   26.29 +Bottom line option (Switch node, Search node [tab completion?])
   26.30 +
   26.31 +Capture/Logging of resource information generated during a time interval.
   26.32 +-b batch mode dump snapshots to standard output (used with -n)
   26.33 +-n number of iterations to dump to standard output (unlimited if not specified)
   26.34 +-d monitor DomIDs as -dD1,-dD2 or -dD1,D2...
   26.35 +   Monitor only domains with specified domain IDs
   26.36 +-m monitor nodeIDs as -mN1,-mN2 or -mN1,N2...
   26.37 +   Monitor only domains with specified node IDs
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/tools/xenstat/xentop/xentop.1	Tue Aug 23 08:40:58 2005 +0000
    27.3 @@ -0,0 +1,88 @@
    27.4 +.\" Copyright (C) International Business Machines  Corp., 2005
    27.5 +.\" Author: Josh Triplett <josht@us.ibm.com>
    27.6 +.\"
    27.7 +.\" This program is free software; you can redistribute it and/or modify
    27.8 +.\" it under the terms of the GNU General Public License as published by
    27.9 +.\" the Free Software Foundation; under version 2 of the License.
   27.10 +.\"
   27.11 +.\" This program is distributed in the hope that it will be useful,
   27.12 +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
   27.13 +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   27.14 +.\" GNU General Public License for more details.
   27.15 +.\"
   27.16 +.\" You should have received a copy of the GNU General Public License
   27.17 +.\" along with this program; if not, write to the Free Software
   27.18 +.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   27.19 +.TH xentop 1 "August 2005"
   27.20 +.SH NAME
   27.21 +\fBxentop\fR \- displays real-time information about a Xen system and domains
   27.22 +
   27.23 +.SH SYNOPSIS
   27.24 +.B xentop
   27.25 +[\fB\-h\fR]
   27.26 +[\fB\-V\fR]
   27.27 +[\fB\-d\fRSECONDS]
   27.28 +[\fB\-n\fR]
   27.29 +[\fB\-r\fR]
   27.30 +[\fB\-v\fR]
   27.31 +
   27.32 +.SH DESCRIPTION
   27.33 +\fBxentop\fR displays information about the Xen system and domains, in a
   27.34 +continually-updating manner.  Command-line options and interactive commands
   27.35 +can change the detail and format of the information displayed by \fBxentop\fR.
   27.36 +
   27.37 +.SH OPTIONS
   27.38 +.TP
   27.39 +\fB\-h\fR, \fB\-\-help\fR
   27.40 +display help and exit
   27.41 +.TP
   27.42 +\fB\-V\fR, \fB\-\-version\fR
   27.43 +output version information and exit
   27.44 +.TP
   27.45 +\fB\-d\fR, \fB\-\-delay\fR=\fISECONDS\fR
   27.46 +seconds between updates (default 1)
   27.47 +.TP
   27.48 +\fB\-n\fR, \fB\-\-networks\fR
   27.49 +output network information
   27.50 +.TP
   27.51 +\fB\-r\fR, \fB\-\-repeat\-header\fR
   27.52 +repeat table header before each domain
   27.53 +.TP
   27.54 +\fB\-v\fR, \fB\-\-vcpus\fR
   27.55 +output VCPU data
   27.56 +
   27.57 +.SH "INTERACTIVE COMMANDS"
   27.58 +All interactive commands are case-insensitive.
   27.59 +.TP
   27.60 +.B D
   27.61 +set delay between updates
   27.62 +.TP
   27.63 +.B N
   27.64 +toggle display of network information
   27.65 +.TP
   27.66 +.B Q, Esc
   27.67 +quit
   27.68 +.TP
   27.69 +.B R
   27.70 +toggle table header before each domain
   27.71 +.TP
   27.72 +.B S
   27.73 +cycle sort order
   27.74 +.TP
   27.75 +.B V
   27.76 +toggle display of VCPU information
   27.77 +.TP
   27.78 +.B Arrows
   27.79 +scroll domain display
   27.80 +
   27.81 +.SH AUTHORS
   27.82 +Written by Judy Fischbach, David Hendricks, and Josh Triplett
   27.83 +
   27.84 +.SH "REPORTING BUGS"
   27.85 +Report bugs to <dsteklof@us.ibm.com>.
   27.86 +
   27.87 +.SH COPYRIGHT
   27.88 +Copyright \(co 2005  International Business Machines  Corp
   27.89 +.br
   27.90 +This is free software; see the source for copying conditions.  There is NO
   27.91 +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/tools/xenstat/xentop/xentop.c	Tue Aug 23 08:40:58 2005 +0000
    28.3 @@ -0,0 +1,876 @@
    28.4 +/*
    28.5 + *  Copyright (C) International Business Machines  Corp., 2005
    28.6 + *  Author(s): Judy Fischbach <jfisch@us.ibm.com>
    28.7 + *             David Hendricks <dhendrix@us.ibm.com>
    28.8 + *             Josh Triplett <josht@us.ibm.com>
    28.9 + *    based on code from Anthony Liguori <aliguori@us.ibm.com>
   28.10 + *
   28.11 + *  This program is free software; you can redistribute it and/or modify
   28.12 + *  it under the terms of the GNU General Public License as published by
   28.13 + *  the Free Software Foundation; under version 2 of the License.
   28.14 + *
   28.15 + *  This program is distributed in the hope that it will be useful,
   28.16 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   28.17 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   28.18 + *  GNU General Public License for more details.
   28.19 + *
   28.20 + *  You should have received a copy of the GNU General Public License
   28.21 + *  along with this program; if not, write to the Free Software
   28.22 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   28.23 + */
   28.24 +#include <curses.h>
   28.25 +#include <ctype.h>
   28.26 +#include <errno.h>
   28.27 +#include <stdio.h>
   28.28 +#include <stdlib.h>
   28.29 +#include <string.h>
   28.30 +#include <sys/time.h>
   28.31 +#include <time.h>
   28.32 +#include <unistd.h>
   28.33 +
   28.34 +#include <xenstat.h>
   28.35 +
   28.36 +#define XENTOP_VERSION "1.0"
   28.37 +
   28.38 +#define XENTOP_DISCLAIMER \
   28.39 +"Copyright (C) 2005  International Business Machines  Corp\n"\
   28.40 +"This is free software; see the source for copying conditions.There is NO\n"\
   28.41 +"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
   28.42 +#define XENTOP_BUGSTO "Report bugs to <dsteklof@us.ibm.com>.\n"
   28.43 +
   28.44 +#define _GNU_SOURCE
   28.45 +#include <getopt.h>
   28.46 +
   28.47 +#if !defined(__GNUC__) && !defined(__GNUG__)
   28.48 +#define __attribute__(arg) /* empty */
   28.49 +#endif
   28.50 +
   28.51 +#define KEY_ESCAPE '\x1B'
   28.52 +
   28.53 +/*
   28.54 + * Function prototypes
   28.55 + */
   28.56 +/* Utility functions */
   28.57 +static void usage(const char *);
   28.58 +static void version(void);
   28.59 +static void cleanup(void);
   28.60 +static void fail(const char *);
   28.61 +static int current_row(void);
   28.62 +static int lines(void);
   28.63 +static void print(const char *, ...) __attribute__((format(printf,1,2)));
   28.64 +static void attr_addstr(int attr, const char *str);
   28.65 +static void set_delay(char *value);
   28.66 +static void set_prompt(char *new_prompt, void (*func)(char *));
   28.67 +static int handle_key(int);
   28.68 +static int compare(unsigned long long, unsigned long long);
   28.69 +static int compare_domains(xenstat_domain **, xenstat_domain **);
   28.70 +static unsigned long long tot_net_bytes( xenstat_domain *, int);
   28.71 +
   28.72 +/* Field functions */
   28.73 +static int compare_domid(xenstat_domain *domain1, xenstat_domain *domain2);
   28.74 +static void print_domid(xenstat_domain *domain);
   28.75 +static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2);
   28.76 +static void print_state(xenstat_domain *domain);
   28.77 +static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2);
   28.78 +static void print_cpu(xenstat_domain *domain);
   28.79 +static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2);
   28.80 +static void print_cpu_pct(xenstat_domain *domain);
   28.81 +static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2);
   28.82 +static void print_mem(xenstat_domain *domain);
   28.83 +static void print_mem_pct(xenstat_domain *domain);
   28.84 +static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2);
   28.85 +static void print_maxmem(xenstat_domain *domain);
   28.86 +static void print_max_pct(xenstat_domain *domain);
   28.87 +static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2);
   28.88 +static void print_vcpus(xenstat_domain *domain);
   28.89 +static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2);
   28.90 +static void print_nets(xenstat_domain *domain);
   28.91 +static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2);
   28.92 +static void print_net_tx(xenstat_domain *domain);
   28.93 +static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2);
   28.94 +static void print_net_rx(xenstat_domain *domain);
   28.95 +static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2);
   28.96 +static void print_ssid(xenstat_domain *domain);
   28.97 +
   28.98 +/* Section printing functions */
   28.99 +static void do_summary(void);
  28.100 +static void do_header(void);
  28.101 +static void do_bottom_line(void);
  28.102 +static void do_domain(xenstat_domain *);
  28.103 +static void do_vcpu(xenstat_domain *);
  28.104 +static void do_network(xenstat_domain *);
  28.105 +static void top(void);
  28.106 +
  28.107 +/* Field types */
  28.108 +typedef enum field_id {
  28.109 +	FIELD_DOMID,
  28.110 +	FIELD_STATE,
  28.111 +	FIELD_CPU,
  28.112 +	FIELD_CPU_PCT,
  28.113 +	FIELD_MEM,
  28.114 +	FIELD_MEM_PCT,
  28.115 +	FIELD_MAXMEM,
  28.116 +	FIELD_MAX_PCT,
  28.117 +	FIELD_VCPUS,
  28.118 +	FIELD_NETS,
  28.119 +	FIELD_NET_TX,
  28.120 +	FIELD_NET_RX,
  28.121 +	FIELD_SSID
  28.122 +} field_id;
  28.123 +
  28.124 +typedef struct field {
  28.125 +	field_id num;
  28.126 +	const char *header;
  28.127 +	unsigned int default_width;
  28.128 +	int (*compare)(xenstat_domain *domain1, xenstat_domain *domain2);
  28.129 +	void (*print)(xenstat_domain *domain);
  28.130 +} field;
  28.131 +
  28.132 +field fields[] = {
  28.133 +	{ FIELD_DOMID,   "DOMID",      5, compare_domid,   print_domid   },
  28.134 +	{ FIELD_STATE,   "STATE",      6, compare_state,   print_state   },
  28.135 +	{ FIELD_CPU,     "CPU(sec)",  10, compare_cpu,     print_cpu     },
  28.136 +	{ FIELD_CPU_PCT, "CPU(%)",     6, compare_cpu_pct, print_cpu_pct },
  28.137 +	{ FIELD_MEM,     "MEM(k)",    10, compare_mem,     print_mem     },
  28.138 +	{ FIELD_MEM_PCT, "MEM(%)",     6, compare_mem,     print_mem_pct },
  28.139 +	{ FIELD_MAXMEM,  "MAXMEM(k)", 10, compare_maxmem,  print_maxmem  },
  28.140 +	{ FIELD_MAX_PCT, "MAXMEM(%)",  9, compare_maxmem,  print_max_pct },
  28.141 +	{ FIELD_VCPUS,   "VCPUS",      5, compare_vcpus,   print_vcpus   },
  28.142 +	{ FIELD_NETS,    "NETS",       4, compare_nets,    print_nets    },
  28.143 +	{ FIELD_NET_TX,  "NETTX(k)",   8, compare_net_tx,  print_net_tx  },
  28.144 +	{ FIELD_NET_RX,  "NETRX(k)",   8, compare_net_rx,  print_net_rx  },
  28.145 +	{ FIELD_SSID,    "SSID",       4, compare_ssid,    print_ssid    }
  28.146 +};
  28.147 +
  28.148 +const unsigned int NUM_FIELDS = sizeof(fields)/sizeof(field);
  28.149 +
  28.150 +/* Globals */
  28.151 +struct timeval curtime, oldtime;
  28.152 +xenstat_handle *xhandle = NULL;
  28.153 +xenstat_node *prev_node = NULL;
  28.154 +xenstat_node *cur_node = NULL;
  28.155 +field_id sort_field = FIELD_DOMID;
  28.156 +unsigned int first_domain_index = 0;
  28.157 +unsigned int delay = 1;
  28.158 +int show_vcpus = 0;
  28.159 +int show_networks = 0;
  28.160 +int repeat_header = 0;
  28.161 +#define PROMPT_VAL_LEN 80
  28.162 +char *prompt = NULL;
  28.163 +char prompt_val[PROMPT_VAL_LEN];
  28.164 +int prompt_val_len = 0;
  28.165 +void (*prompt_complete_func)(char *);
  28.166 +
  28.167 +/*
  28.168 + * Function definitions
  28.169 + */
  28.170 +
  28.171 +/* Utility functions */
  28.172 +
  28.173 +/* Print usage message, using given program name */
  28.174 +static void usage(const char *program)
  28.175 +{
  28.176 +	printf("Usage: %s [OPTION]\n"
  28.177 +	       "Displays ongoing information about xen vm resources \n\n"
  28.178 +	       "-h, --help           display this help and exit\n"
  28.179 +	       "-V, --version        output version information and exit\n"
  28.180 +	       "-d, --delay=SECONDS  seconds between updates (default 1)\n"
  28.181 +	       "-n, --networks       output vif network data\n"
  28.182 +	       "-r, --repeat-header  repeat table header before each domain\n"
  28.183 +	       "-v, --vcpus          output vcpu data\n"
  28.184 +	       "\n" XENTOP_BUGSTO,
  28.185 +	       program);
  28.186 +	return;
  28.187 +}
  28.188 +
  28.189 +/* Print program version information */
  28.190 +static void version(void)
  28.191 +{
  28.192 +	printf("xentop " XENTOP_VERSION "\n"
  28.193 +	       "Written by Judy Fischbach, David Hendricks, Josh Triplett\n"
  28.194 +	       "\n" XENTOP_DISCLAIMER);
  28.195 +}
  28.196 +
  28.197 +/* Clean up any open resources */
  28.198 +static void cleanup(void)
  28.199 +{
  28.200 +	if(!isendwin())
  28.201 +		endwin();
  28.202 +	if(prev_node != NULL)
  28.203 +		xenstat_free_node(prev_node);
  28.204 +	if(cur_node != NULL)
  28.205 +		xenstat_free_node(cur_node);
  28.206 +	if(xhandle != NULL)
  28.207 +		xenstat_uninit(xhandle);
  28.208 +}
  28.209 +
  28.210 +/* Display the given message and gracefully exit */
  28.211 +static void fail(const char *str)
  28.212 +{
  28.213 +	if(!isendwin())
  28.214 +		endwin();
  28.215 +	fprintf(stderr, str);
  28.216 +	exit(1);
  28.217 +}
  28.218 +
  28.219 +/* Return the row containing the cursor. */
  28.220 +static int current_row(void)
  28.221 +{
  28.222 +	int y, x;
  28.223 +	getyx(stdscr, y, x);
  28.224 +	return y;
  28.225 +}
  28.226 +
  28.227 +/* Return the number of lines on the screen. */
  28.228 +static int lines(void)
  28.229 +{
  28.230 +	int y, x;
  28.231 +	getmaxyx(stdscr, y, x);
  28.232 +	return y;
  28.233 +}
  28.234 +
  28.235 +/* printf-style print function which calls printw, but only if the cursor is
  28.236 + * not on the last line. */
  28.237 +static void print(const char *fmt, ...)
  28.238 +{
  28.239 +	va_list args;
  28.240 +
  28.241 +	if(current_row() < lines()-1) {
  28.242 +		va_start(args, fmt);
  28.243 +		vw_printw(stdscr, fmt, args);
  28.244 +		va_end(args);
  28.245 +	}
  28.246 +}
  28.247 +
  28.248 +/* Print a string with the given attributes set. */
  28.249 +static void attr_addstr(int attr, const char *str)
  28.250 +{
  28.251 +	attron(attr);
  28.252 +	addstr(str);
  28.253 +	attroff(attr);
  28.254 +}
  28.255 +
  28.256 +/* Handle setting the delay from the user-supplied value in prompt_val */
  28.257 +static void set_delay(char *value)
  28.258 +{
  28.259 +	int new_delay;
  28.260 +	new_delay = atoi(prompt_val);
  28.261 +	if(new_delay > 0)
  28.262 +		delay = new_delay;
  28.263 +}
  28.264 +
  28.265 +/* Enable prompting mode with the given prompt string; call the given function
  28.266 + * when a value is available. */
  28.267 +static void set_prompt(char *new_prompt, void (*func)(char *))
  28.268 +{
  28.269 +	prompt = new_prompt;
  28.270 +	prompt_val[0] = '\0';
  28.271 +	prompt_val_len = 0;
  28.272 +	prompt_complete_func = func;
  28.273 +}
  28.274 +
  28.275 +/* Handle user input, return 0 if the program should quit, or 1 if not */
  28.276 +static int handle_key(int ch)
  28.277 +{
  28.278 +	if(prompt == NULL) {
  28.279 +		/* Not prompting for input; handle interactive commands */
  28.280 +		switch(ch) {
  28.281 +		case 'n': case 'N':
  28.282 +			show_networks ^= 1;
  28.283 +			break;
  28.284 +		case 'r': case 'R':
  28.285 +			repeat_header ^= 1;
  28.286 +			break;
  28.287 +		case 's': case 'S':
  28.288 +			sort_field = (sort_field + 1) % NUM_FIELDS;
  28.289 +			break;
  28.290 +		case 'v': case 'V':
  28.291 +			show_vcpus ^= 1;
  28.292 +			break;
  28.293 +		case KEY_DOWN:
  28.294 +			first_domain_index++;
  28.295 +			break;
  28.296 +		case KEY_UP:
  28.297 +			if(first_domain_index > 0)
  28.298 +				first_domain_index--;
  28.299 +			break;
  28.300 +		case 'd': case 'D':
  28.301 +			set_prompt("Delay(sec)", set_delay);
  28.302 +			break;
  28.303 +		case 'q': case 'Q': case KEY_ESCAPE:
  28.304 +			return 0;
  28.305 +		}
  28.306 +	} else {
  28.307 +		/* Prompting for input; handle line editing */
  28.308 +		switch(ch) {
  28.309 +		case '\r':
  28.310 +			prompt_complete_func(prompt_val);
  28.311 +			set_prompt(NULL, NULL);
  28.312 +			break;
  28.313 +		case KEY_ESCAPE:
  28.314 +			set_prompt(NULL, NULL);
  28.315 +			break;
  28.316 +		case KEY_BACKSPACE:
  28.317 +			if(prompt_val_len > 0)
  28.318 +				prompt_val[--prompt_val_len] = '\0';
  28.319 +		default:
  28.320 +			if((prompt_val_len+1) < PROMPT_VAL_LEN
  28.321 +			   && isprint(ch)) {
  28.322 +				prompt_val[prompt_val_len++] = (char)ch;
  28.323 +				prompt_val[prompt_val_len] = '\0';
  28.324 +			}
  28.325 +		}
  28.326 +	}
  28.327 +
  28.328 +	return 1;
  28.329 +}
  28.330 +
  28.331 +/* Compares two integers, returning -1,0,1 for <,=,> */
  28.332 +static int compare(unsigned long long i1, unsigned long long i2)
  28.333 +{
  28.334 +	if(i1 < i2)
  28.335 +		return -1;
  28.336 +	if(i1 > i2)
  28.337 +		return 1;
  28.338 +	return 0;
  28.339 +}
  28.340 +
  28.341 +/* Comparison function for use with qsort.  Compares two domains using the
  28.342 + * current sort field. */
  28.343 +static int compare_domains(xenstat_domain **domain1, xenstat_domain **domain2)
  28.344 +{
  28.345 +	return fields[sort_field].compare(*domain1, *domain2);
  28.346 +}
  28.347 +
  28.348 +/* Field functions */
  28.349 +
  28.350 +/* Compares domain ids of two domains, returning -1,0,1 for <,=,> */
  28.351 +int compare_domid(xenstat_domain *domain1, xenstat_domain *domain2)
  28.352 +{
  28.353 +	return compare(xenstat_domain_id(domain1), xenstat_domain_id(domain2));
  28.354 +}
  28.355 +
  28.356 +/* Prints domain identification number */
  28.357 +void print_domid(xenstat_domain *domain)
  28.358 +{
  28.359 +	print("%5u", xenstat_domain_id(domain));
  28.360 +}
  28.361 +
  28.362 +struct {
  28.363 +	unsigned int (*get)(xenstat_domain *);
  28.364 +	char ch;
  28.365 +} state_funcs[] = {
  28.366 +	{ xenstat_domain_dying,    'd' },
  28.367 +	{ xenstat_domain_shutdown, 's' },
  28.368 +	{ xenstat_domain_blocked,  'b' },
  28.369 +	{ xenstat_domain_crashed,  'c' },
  28.370 +	{ xenstat_domain_paused,   'p' },
  28.371 +	{ xenstat_domain_running,  'r' }
  28.372 +};
  28.373 +const unsigned int NUM_STATES = sizeof(state_funcs)/sizeof(*state_funcs);
  28.374 +
  28.375 +/* Compare states of two domains, returning -1,0,1 for <,=,> */
  28.376 +static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2)
  28.377 +{
  28.378 +	unsigned int i, d1s, d2s;
  28.379 +	for(i = 0; i < NUM_STATES; i++) {
  28.380 +		d1s = state_funcs[i].get(domain1);
  28.381 +		d2s = state_funcs[i].get(domain2);
  28.382 +		if(d1s && !d2s)
  28.383 +			return -1;
  28.384 +		if(d2s && !d1s)
  28.385 +			return 1;
  28.386 +	}
  28.387 +	return 0;
  28.388 +}
  28.389 +
  28.390 +/* Prints domain state in abbreviated letter format */
  28.391 +static void print_state(xenstat_domain *domain)
  28.392 +{
  28.393 +	unsigned int i;
  28.394 +	for(i = 0; i < NUM_STATES; i++)
  28.395 +		print("%c", state_funcs[i].get(domain) ? state_funcs[i].ch
  28.396 +		                                       : '-');
  28.397 +}
  28.398 +
  28.399 +/* Compares cpu usage of two domains, returning -1,0,1 for <,=,> */
  28.400 +static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2)
  28.401 +{
  28.402 +	return -compare(xenstat_domain_cpu_ns(domain1),
  28.403 +			xenstat_domain_cpu_ns(domain2));
  28.404 +}
  28.405 +
  28.406 +/* Prints domain cpu usage in seconds */
  28.407 +static void print_cpu(xenstat_domain *domain)
  28.408 +{
  28.409 +	print("%10llu", xenstat_domain_cpu_ns(domain)/1000000000);
  28.410 +}
  28.411 +
  28.412 +/* Computes the CPU percentage used for a specified domain */
  28.413 +static double get_cpu_pct(xenstat_domain *domain)
  28.414 +{
  28.415 +	xenstat_domain *old_domain;
  28.416 +	double us_elapsed;
  28.417 +
  28.418 +	/* Can't calculate CPU percentage without a previous sample. */
  28.419 +	if(prev_node == NULL)
  28.420 +		return 0.0;
  28.421 +
  28.422 +	old_domain = xenstat_node_domain(prev_node, xenstat_domain_id(domain));
  28.423 +	if(old_domain == NULL)
  28.424 +		return 0.0;
  28.425 +
  28.426 +	/* Calculate the time elapsed in microseconds */
  28.427 +	us_elapsed = ((curtime.tv_sec-oldtime.tv_sec)*1000000.0
  28.428 +		      +(curtime.tv_usec - oldtime.tv_usec));
  28.429 +
  28.430 +	/* In the following, nanoseconds must be multiplied by 1000.0 to
  28.431 +	 * convert to microseconds, then divided by 100.0 to get a percentage,
  28.432 +	 * resulting in a multiplication by 10.0 */
  28.433 +	return ((xenstat_domain_cpu_ns(domain)
  28.434 +		 -xenstat_domain_cpu_ns(old_domain))/10.0)/us_elapsed;
  28.435 +}
  28.436 +
  28.437 +static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2)
  28.438 +{
  28.439 +	return -compare(get_cpu_pct(domain1), get_cpu_pct(domain2));
  28.440 +}
  28.441 +
  28.442 +/* Prints cpu percentage statistic */
  28.443 +static void print_cpu_pct(xenstat_domain *domain)
  28.444 +{
  28.445 +	print("%6.1f", get_cpu_pct(domain));
  28.446 +}
  28.447 +
  28.448 +/* Compares current memory of two domains, returning -1,0,1 for <,=,> */
  28.449 +static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2)
  28.450 +{
  28.451 +	return -compare(xenstat_domain_cur_mem(domain1),
  28.452 +	                xenstat_domain_cur_mem(domain2));
  28.453 +}
  28.454 +
  28.455 +/* Prints current memory statistic */
  28.456 +static void print_mem(xenstat_domain *domain)
  28.457 +{
  28.458 +	print("%10llu", xenstat_domain_cur_mem(domain)/1024);
  28.459 +}
  28.460 +
  28.461 +/* Prints memory percentage statistic, ratio of current domain memory to total
  28.462 + * node memory */
  28.463 +static void print_mem_pct(xenstat_domain *domain)
  28.464 +{
  28.465 +	print("%6.1f", (double)xenstat_domain_cur_mem(domain) /
  28.466 +	               (double)xenstat_node_tot_mem(cur_node) * 100);
  28.467 +}
  28.468 +
  28.469 +/* Compares maximum memory of two domains, returning -1,0,1 for <,=,> */
  28.470 +static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2)
  28.471 +{
  28.472 +	return -compare(xenstat_domain_max_mem(domain1),
  28.473 +	                xenstat_domain_max_mem(domain2));
  28.474 +}
  28.475 +
  28.476 +/* Prints maximum domain memory statistic in KB */
  28.477 +static void print_maxmem(xenstat_domain *domain)
  28.478 +{
  28.479 +	unsigned long long max_mem = xenstat_domain_max_mem(domain);
  28.480 +	if(max_mem == ((unsigned long long)-1))
  28.481 +		print("%10s", "no limit");
  28.482 +	else
  28.483 +		print("%10llu", max_mem/1024);
  28.484 +}
  28.485 +
  28.486 +/* Prints memory percentage statistic, ratio of current domain memory to total
  28.487 + * node memory */
  28.488 +static void print_max_pct(xenstat_domain *domain)
  28.489 +{
  28.490 +	if (xenstat_domain_max_mem(domain) == (unsigned long long)-1)
  28.491 +		print("%9s", "n/a");
  28.492 +	else
  28.493 +		print("%9.1f", (double)xenstat_domain_max_mem(domain) /
  28.494 +		               (double)xenstat_node_tot_mem(cur_node) * 100);
  28.495 +}
  28.496 +
  28.497 +/* Compares number of virtual CPUs of two domains, returning -1,0,1 for
  28.498 + * <,=,> */
  28.499 +static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2)
  28.500 +{
  28.501 +	return -compare(xenstat_domain_num_vcpus(domain1),
  28.502 +	                xenstat_domain_num_vcpus(domain2));
  28.503 +}
  28.504 +
  28.505 +/* Prints number of virtual CPUs statistic */
  28.506 +static void print_vcpus(xenstat_domain *domain)
  28.507 +{
  28.508 +	print("%5u", xenstat_domain_num_vcpus(domain));
  28.509 +}
  28.510 +
  28.511 +/* Compares number of virtual networks of two domains, returning -1,0,1 for
  28.512 + * <,=,> */
  28.513 +static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2)
  28.514 +{
  28.515 +	return -compare(xenstat_domain_num_networks(domain1),
  28.516 +	                xenstat_domain_num_networks(domain2));
  28.517 +}
  28.518 +
  28.519 +/* Prints number of virtual networks statistic */
  28.520 +static void print_nets(xenstat_domain *domain)
  28.521 +{
  28.522 +	print("%4u", xenstat_domain_num_networks(domain));
  28.523 +}
  28.524 +
  28.525 +/* Compares number of total network tx bytes of two domains, returning -1,0,1 for
  28.526 + * <,=,> */
  28.527 +static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2)
  28.528 +{
  28.529 +	return -compare(tot_net_bytes(domain1, FALSE),
  28.530 +	                tot_net_bytes(domain2, FALSE));
  28.531 +}
  28.532 +
  28.533 +/* Prints number of total network tx bytes statistic */
  28.534 +static void print_net_tx(xenstat_domain *domain)
  28.535 +{
  28.536 +	print("%8llu", tot_net_bytes(domain, FALSE)/1024);
  28.537 +}
  28.538 +
  28.539 +/* Compares number of total network rx bytes of two domains, returning -1,0,1 for
  28.540 + * <,=,> */
  28.541 +static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2)
  28.542 +{
  28.543 +	return -compare(tot_net_bytes(domain1, TRUE),
  28.544 +	                tot_net_bytes(domain2, TRUE));
  28.545 +}
  28.546 +
  28.547 +/* Prints number of total network rx bytes statistic */
  28.548 +static void print_net_rx(xenstat_domain *domain)
  28.549 +{
  28.550 +	print("%8llu", tot_net_bytes(domain, TRUE)/1024);
  28.551 +}
  28.552 +
  28.553 +/* Gets number of total network bytes statistic, if rx true, then rx bytes
  28.554 + * otherwise tx bytes
  28.555 + */
  28.556 +static unsigned long long tot_net_bytes(xenstat_domain *domain, int rx_flag)
  28.557 +{
  28.558 +	int i = 0;
  28.559 +	xenstat_network *network;
  28.560 +	unsigned num_networks = 0;
  28.561 +        unsigned long long total = 0;
  28.562 +
  28.563 +	/* How many networks? */
  28.564 +	num_networks = xenstat_domain_num_networks(domain);
  28.565 +
  28.566 +	/* Dump information for each network */
  28.567 +	for (i=0; i < num_networks; i++) {
  28.568 +		/* Next get the network information */
  28.569 +		network = xenstat_domain_network(domain,i);
  28.570 +                if (rx_flag) 
  28.571 +			total += xenstat_network_rbytes(network);
  28.572 +                else 
  28.573 +			total += xenstat_network_tbytes(network);
  28.574 +	}
  28.575 +        return (total);
  28.576 +}
  28.577 +
  28.578 +/* Compares security id (ssid) of two domains, returning -1,0,1 for <,=,> */
  28.579 +static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2)
  28.580 +{
  28.581 +	return compare(xenstat_domain_ssid(domain1),
  28.582 +		       xenstat_domain_ssid(domain2));
  28.583 +}
  28.584 +
  28.585 +/* Prints ssid statistic */
  28.586 +static void print_ssid(xenstat_domain *domain)
  28.587 +{
  28.588 +	print("%4u", xenstat_domain_ssid(domain));
  28.589 +}
  28.590 +
  28.591 +/* Section printing functions */
  28.592 +/* Prints the top summary, above the domain table */
  28.593 +void do_summary(void)
  28.594 +{
  28.595 +#define TIME_STR_LEN 9
  28.596 +	const char *TIME_STR_FORMAT = "%H:%M:%S";
  28.597 +	char time_str[TIME_STR_LEN];
  28.598 +	unsigned run = 0, block = 0, pause = 0,
  28.599 +	         crash = 0, dying = 0, shutdown = 0;
  28.600 +	unsigned i, num_domains = 0;
  28.601 +	unsigned long long used = 0;
  28.602 +	xenstat_domain *domain;
  28.603 +
  28.604 +	/* Print program name, current time, and number of domains */
  28.605 +	strftime(time_str, TIME_STR_LEN, TIME_STR_FORMAT,
  28.606 +	         localtime(&curtime.tv_sec));
  28.607 +	num_domains = xenstat_node_num_domains(cur_node);
  28.608 +	print("xentop - %s\n", time_str);
  28.609 +
  28.610 +	/* Tabulate what states domains are in for summary */
  28.611 +	for (i=0; i < num_domains; i++) {
  28.612 +		domain = xenstat_node_domain_by_index(cur_node,i);
  28.613 +		if (xenstat_domain_running(domain)) run++;
  28.614 +		else if (xenstat_domain_blocked(domain)) block++;
  28.615 +		else if (xenstat_domain_paused(domain)) pause++;
  28.616 +		else if (xenstat_domain_shutdown(domain)) shutdown++;
  28.617 +		else if (xenstat_domain_crashed(domain)) crash++;
  28.618 +		else if (xenstat_domain_dying(domain)) dying++;
  28.619 +	}
  28.620 +
  28.621 +	print("%u domains: %u running, %u blocked, %u paused, "
  28.622 +	      "%u crashed, %u dying, %u shutdown \n",
  28.623 +	      num_domains, run, block, pause, crash, dying, shutdown);
  28.624 +
  28.625 +	used = xenstat_node_tot_mem(cur_node)-xenstat_node_free_mem(cur_node);
  28.626 +
  28.627 +	/* Dump node memory and cpu information */
  28.628 +	print("Mem: %lluk total, %lluk used, %lluk free    "
  28.629 +	      "CPUs: %u @ %lluMHz\n",
  28.630 +	      xenstat_node_tot_mem(cur_node)/1024, used/1024,
  28.631 +	      xenstat_node_free_mem(cur_node)/1024,
  28.632 +	      xenstat_node_num_cpus(cur_node),
  28.633 +	      xenstat_node_cpu_hz(cur_node)/1000000);
  28.634 +}
  28.635 +
  28.636 +/* Display the top header for the domain table */
  28.637 +void do_header(void)
  28.638 +{
  28.639 +	field_id i;
  28.640 +
  28.641 +	/* Turn on REVERSE highlight attribute for headings */
  28.642 +	attron(A_REVERSE);
  28.643 +	for(i = 0; i < NUM_FIELDS; i++) {
  28.644 +		if(i != 0)
  28.645 +			print(" ");
  28.646 +		/* The BOLD attribute is turned on for the sort column */
  28.647 +		if(i == sort_field)
  28.648 +			attron(A_BOLD);
  28.649 +		print("%*s", fields[i].default_width, fields[i].header);
  28.650 +		if(i == sort_field)
  28.651 +			attroff(A_BOLD);
  28.652 +	}
  28.653 +	attroff(A_REVERSE);
  28.654 +	print("\n");
  28.655 +}
  28.656 +
  28.657 +/* Displays bottom status line or current prompt */
  28.658 +void do_bottom_line(void)
  28.659 +{
  28.660 +	move(lines()-1, 2);
  28.661 +
  28.662 +	if (prompt != NULL) {
  28.663 +		printw("%s: %s", prompt, prompt_val);
  28.664 +	} else {
  28.665 +		addch(A_REVERSE | 'D'); addstr("elay  ");
  28.666 +
  28.667 +		/* network */
  28.668 +		addch(A_REVERSE | 'N');
  28.669 +		attr_addstr(show_networks ? COLOR_PAIR(1) : 0, "etworks");
  28.670 +		addstr("  ");
  28.671 +
  28.672 +		/* vcpus */
  28.673 +		addch(A_REVERSE | 'V');
  28.674 +		attr_addstr(show_vcpus ? COLOR_PAIR(1) : 0, "CPUs");
  28.675 +		addstr("  ");
  28.676 +
  28.677 +		/* repeat */
  28.678 +		addch(A_REVERSE | 'R');
  28.679 +		attr_addstr(repeat_header ? COLOR_PAIR(1) : 0, "epeat header");
  28.680 +		addstr("  ");
  28.681 +
  28.682 +		/* sort order */
  28.683 +		addch(A_REVERSE | 'S'); addstr("ort order  ");
  28.684 +
  28.685 +		addch(A_REVERSE | 'Q'); addstr("uit  ");
  28.686 +	}
  28.687 +}
  28.688 +
  28.689 +/* Prints Domain information */
  28.690 +void do_domain(xenstat_domain *domain)
  28.691 +{
  28.692 +	unsigned int i;
  28.693 +	for(i = 0; i < NUM_FIELDS; i++) {
  28.694 +		if(i != 0)
  28.695 +			print(" ");
  28.696 +		if(i == sort_field)
  28.697 +			attron(A_BOLD);
  28.698 +		fields[i].print(domain);
  28.699 +		if(i == sort_field)
  28.700 +			attroff(A_BOLD);
  28.701 +	}
  28.702 +	print("\n");
  28.703 +}
  28.704 +
  28.705 +/* Output all vcpu information */
  28.706 +void do_vcpu(xenstat_domain *domain)
  28.707 +{
  28.708 +	int i = 0;
  28.709 +	unsigned num_vcpus = 0;
  28.710 +	xenstat_vcpu *vcpu;
  28.711 +
  28.712 +	print("VCPUs(sec): ");
  28.713 +
  28.714 +	num_vcpus = xenstat_domain_num_vcpus(domain);
  28.715 +
  28.716 +	/* for all vcpus dump out values */
  28.717 +	for (i=0; i< num_vcpus; i++) {
  28.718 +		vcpu = xenstat_domain_vcpu(domain,i);
  28.719 +
  28.720 +		if (i != 0 && (i%5)==0)
  28.721 +			print("\n        ");
  28.722 +		print(" %2u: %10llus", i, xenstat_vcpu_ns(vcpu)/1000000000);
  28.723 +	}
  28.724 +	print("\n");
  28.725 +}
  28.726 +
  28.727 +/* Output all network information */
  28.728 +void do_network(xenstat_domain *domain)
  28.729 +{
  28.730 +	int i = 0;
  28.731 +	xenstat_network *network;
  28.732 +	unsigned num_networks = 0;
  28.733 +
  28.734 +	/* How many networks? */
  28.735 +	num_networks = xenstat_domain_num_networks(domain);
  28.736 +
  28.737 +	/* Dump information for each network */
  28.738 +	for (i=0; i < num_networks; i++) {
  28.739 +		/* Next get the network information */
  28.740 +		network = xenstat_domain_network(domain,i);
  28.741 +
  28.742 +		print("Net%d RX: %8llubytes %8llupkts %8lluerr %8lludrop  ",
  28.743 +		      i,
  28.744 +		      xenstat_network_rbytes(network),
  28.745 +		      xenstat_network_rpackets(network),
  28.746 +		      xenstat_network_rerrs(network),
  28.747 +		      xenstat_network_rdrop(network));
  28.748 +
  28.749 +		print("TX: %8llubytes %8llupkts %8lluerr %8lludrop\n",
  28.750 +		      xenstat_network_tbytes(network),
  28.751 +		      xenstat_network_tpackets(network),
  28.752 +		      xenstat_network_terrs(network),
  28.753 +		      xenstat_network_tdrop(network));
  28.754 +	}
  28.755 +}
  28.756 +
  28.757 +static void top(void)
  28.758 +{
  28.759 +	xenstat_domain **domains;
  28.760 +	unsigned int i, num_domains = 0;
  28.761 +
  28.762 +	/* Now get the node information */
  28.763 +	if (prev_node != NULL)
  28.764 +		xenstat_free_node(prev_node);
  28.765 +	prev_node = cur_node;
  28.766 +	cur_node = xenstat_get_node(xhandle, XENSTAT_ALL);
  28.767 +	if (cur_node == NULL)
  28.768 +		fail("Failed to retrieve statistics from libxenstat\n");
  28.769 +
  28.770 +	/* dump summary top information */
  28.771 +	do_summary();
  28.772 +
  28.773 +	/* Count the number of domains for which to report data */
  28.774 +	num_domains = xenstat_node_num_domains(cur_node);
  28.775 +
  28.776 +	domains = malloc(num_domains*sizeof(xenstat_domain *));
  28.777 +	if(domains == NULL)
  28.778 +		fail("Failed to allocate memory\n");
  28.779 +
  28.780 +	for (i=0; i < num_domains; i++)
  28.781 +		domains[i] = xenstat_node_domain_by_index(cur_node, i);
  28.782 +
  28.783 +	/* Sort */
  28.784 +	qsort(domains, num_domains, sizeof(xenstat_domain *),
  28.785 +	      (int(*)(const void *, const void *))compare_domains);
  28.786 +
  28.787 +	if(first_domain_index >= num_domains)
  28.788 +		first_domain_index = num_domains-1;
  28.789 +
  28.790 +	for (i = first_domain_index; i < num_domains; i++) {
  28.791 +		if(current_row() == lines()-1)
  28.792 +			break;
  28.793 +		if (i == first_domain_index || repeat_header)
  28.794 +			do_header();
  28.795 +		do_domain(domains[i]);
  28.796 +		if (show_vcpus)
  28.797 +			do_vcpu(domains[i]);
  28.798 +		if (show_networks)
  28.799 +			do_network(domains[i]);
  28.800 +	}
  28.801 +
  28.802 +	do_bottom_line();
  28.803 +}
  28.804 +
  28.805 +int main(int argc, char **argv)
  28.806 +{
  28.807 +	int opt, optind = 0;
  28.808 +	int ch = ERR;
  28.809 +
  28.810 +	struct option lopts[] = {
  28.811 +		{ "help",          no_argument,       NULL, 'h' },
  28.812 +		{ "version",       no_argument,       NULL, 'V' },
  28.813 +		{ "networks",      no_argument,       NULL, 'n' },
  28.814 +		{ "repeat-header", no_argument,       NULL, 'r' },
  28.815 +		{ "vcpus",         no_argument,       NULL, 'v' },
  28.816 +		{ "delay",         required_argument, NULL, 'd' },
  28.817 +		{ 0, 0, 0, 0 },
  28.818 +	};
  28.819 +	const char *sopts = "hVbnvd:";
  28.820 +
  28.821 +	if (atexit(cleanup) != 0)
  28.822 +		fail("Failed to install cleanup handler.\n");
  28.823 +
  28.824 +	while ((opt = getopt_long(argc, argv, sopts, lopts, &optind)) != -1) {
  28.825 +		switch (opt) {
  28.826 +		case 'h':
  28.827 +		case '?':
  28.828 +		default:
  28.829 +			usage(argv[0]);
  28.830 +			exit(0);
  28.831 +		case 'V':
  28.832 +			version();
  28.833 +			exit(0);
  28.834 +		case 'n':
  28.835 +			show_networks = 1;
  28.836 +			break;
  28.837 +		case 'r':
  28.838 +			repeat_header = 1;
  28.839 +			break;
  28.840 +		case 'v':
  28.841 +			show_vcpus = 1;
  28.842 +			break;
  28.843 +		case 'd':
  28.844 +			delay = atoi(optarg);
  28.845 +			break;
  28.846 +		}
  28.847 +	}
  28.848 +
  28.849 +	/* Get xenstat handle */
  28.850 +	xhandle = xenstat_init();
  28.851 +	if (xhandle == NULL)
  28.852 +		fail("Failed to initialize xenstat library\n");
  28.853 +
  28.854 +	/* Begin curses stuff */
  28.855 +	initscr();
  28.856 +	start_color();
  28.857 +	cbreak();
  28.858 +	noecho();
  28.859 +	nonl();
  28.860 +	keypad(stdscr, TRUE);
  28.861 +	halfdelay(5);
  28.862 +	use_default_colors();
  28.863 +	init_pair(1, -1, COLOR_YELLOW);
  28.864 +
  28.865 +	do {
  28.866 +		gettimeofday(&curtime, NULL);
  28.867 +		if(ch != ERR || (curtime.tv_sec - oldtime.tv_sec) >= delay) {
  28.868 +			clear();
  28.869 +			top();
  28.870 +			oldtime = curtime;
  28.871 +			refresh();
  28.872 +		}
  28.873 +		ch = getch();
  28.874 +	} while (handle_key(ch));
  28.875 +
  28.876 +	/* Cleanup occurs in cleanup(), so no work to do here. */
  28.877 +
  28.878 +	return 0;
  28.879 +}