direct-io.hg

changeset 6343: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
     2.1 --- a/Config.mk	Tue Aug 23 08:40:50 2005 +0000
     2.2 +++ b/Config.mk	Tue Aug 23 08:40:58 2005 +0000
     2.3 @@ -14,6 +14,7 @@ LD         = $(CROSS_COMPILE)ld
     2.4  CC         = $(CROSS_COMPILE)gcc
     2.5  CPP        = $(CROSS_COMPILE)gcc -E
     2.6  AR         = $(CROSS_COMPILE)ar
     2.7 +RANLIB     = $(CROSS_COMPILE)ranlib
     2.8  NM         = $(CROSS_COMPILE)nm
     2.9  STRIP      = $(CROSS_COMPILE)strip
    2.10  OBJCOPY    = $(CROSS_COMPILE)objcopy
    2.11 @@ -43,3 +44,7 @@ KERNEL_REPO = http://www.kernel.org
    2.12  #	ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY
    2.13  #	ACM_CHINESE_WALL_AND_SIMPLE_TYPE_ENFORCEMENT_POLICY
    2.14  ACM_USE_SECURITY_POLICY ?= ACM_NULL_POLICY
    2.15 +
    2.16 +# Optional components
    2.17 +XENSTAT_XENTOP ?= y
    2.18 +
    31.1 --- a/linux-2.6-xen-sparse/arch/xen/i386/mm/hypervisor.c	Tue Aug 23 08:40:50 2005 +0000
    31.2 +++ b/linux-2.6-xen-sparse/arch/xen/i386/mm/hypervisor.c	Tue Aug 23 08:40:58 2005 +0000
    31.3 @@ -405,54 +405,6 @@ void xen_destroy_contiguous_region(unsig
    31.4  	balloon_unlock(flags);
    31.5  }
    31.6  
    31.7 -
    31.8 -unsigned long allocate_empty_lowmem_region(unsigned long pages)
    31.9 -{
   31.10 -	pgd_t         *pgd;
   31.11 -	pud_t         *pud; 
   31.12 -	pmd_t         *pmd;
   31.13 -	pte_t         *pte;
   31.14 -	unsigned long *pfn_array;
   31.15 -	unsigned long  vstart;
   31.16 -	unsigned long  i;
   31.17 -	unsigned int   order = get_order(pages*PAGE_SIZE);
   31.18 -
   31.19 -	vstart = __get_free_pages(GFP_KERNEL, order);
   31.20 -	if (vstart == 0)
   31.21 -		return 0UL;
   31.22 -
   31.23 -	scrub_pages(vstart, 1 << order);
   31.24 -
   31.25 -	pfn_array = vmalloc((1<<order) * sizeof(*pfn_array));
   31.26 -	BUG_ON(pfn_array == NULL);
   31.27 -
   31.28 -	for (i = 0; i < (1<<order); i++) {
   31.29 -		pgd = pgd_offset_k(   (vstart + (i*PAGE_SIZE)));
   31.30 -		pud = pud_offset(pgd, (vstart + (i*PAGE_SIZE)));
   31.31 -		pmd = pmd_offset(pud, (vstart + (i*PAGE_SIZE)));
   31.32 -		pte = pte_offset_kernel(pmd, (vstart + (i*PAGE_SIZE))); 
   31.33 -		pfn_array[i] = pte_mfn(*pte);
   31.34 -#ifdef CONFIG_X86_64
   31.35 -		xen_l1_entry_update(pte, __pte(0));
   31.36 -#else
   31.37 -		BUG_ON(HYPERVISOR_update_va_mapping(vstart + (i*PAGE_SIZE), 
   31.38 -						    __pte_ma(0), 0));
   31.39 -#endif
   31.40 -		phys_to_machine_mapping[(__pa(vstart)>>PAGE_SHIFT)+i] =
   31.41 -			INVALID_P2M_ENTRY;
   31.42 -	}
   31.43 -
   31.44 -	flush_tlb_all();
   31.45 -
   31.46 -	balloon_put_pages(pfn_array, 1 << order);
   31.47 -
   31.48 -	vfree(pfn_array);
   31.49 -
   31.50 -	return vstart;
   31.51 -}
   31.52 -
   31.53 -EXPORT_SYMBOL(allocate_empty_lowmem_region);
   31.54 -
   31.55  /*
   31.56   * Local variables:
   31.57   *  c-file-style: "linux"
    33.1 --- a/linux-2.6-xen-sparse/arch/xen/i386/mm/ioremap.c	Tue Aug 23 08:40:50 2005 +0000
    33.2 +++ b/linux-2.6-xen-sparse/arch/xen/i386/mm/ioremap.c	Tue Aug 23 08:40:58 2005 +0000
    33.3 @@ -368,35 +368,37 @@ int direct_remap_area_pages(struct mm_st
    33.4  
    33.5  EXPORT_SYMBOL(direct_remap_area_pages);
    33.6  
    33.7 +static int lookup_pte_fn(
    33.8 +	pte_t *pte, struct page *pte_page, unsigned long addr, void *data)
    33.9 +{
   33.10 +	unsigned long *ptep = (unsigned long *)data;
   33.11 +	if (ptep)
   33.12 +		*ptep = (pfn_to_mfn(page_to_pfn(pte_page)) <<
   33.13 +			 PAGE_SHIFT) |
   33.14 +			((unsigned long)pte & ~PAGE_MASK);
   33.15 +	return 0;
   33.16 +}
   33.17 +
   33.18  int create_lookup_pte_addr(struct mm_struct *mm, 
   33.19  			   unsigned long address,
   33.20  			   unsigned long *ptep)
   33.21  {
   33.22 -	int f(pte_t *pte, struct page *pte_page, unsigned long addr,
   33.23 -	      void *data) {
   33.24 -		unsigned long *ptep = (unsigned long *)data;
   33.25 -		if (ptep)
   33.26 -			*ptep = (pfn_to_mfn(page_to_pfn(pte_page)) <<
   33.27 -				 PAGE_SHIFT) |
   33.28 -				((unsigned long)pte & ~PAGE_MASK);
   33.29 -		return 0;
   33.30 -	}
   33.31 -
   33.32 -	return generic_page_range(mm, address, PAGE_SIZE, f, ptep);
   33.33 +	return generic_page_range(mm, address, PAGE_SIZE, lookup_pte_fn, ptep);
   33.34  }
   33.35  
   33.36  EXPORT_SYMBOL(create_lookup_pte_addr);
   33.37  
   33.38 +static int noop_fn(
   33.39 +	pte_t *pte, struct page *pte_page, unsigned long addr, void *data)
   33.40 +{
   33.41 +	return 0;
   33.42 +}
   33.43 +
   33.44  int touch_pte_range(struct mm_struct *mm,
   33.45  		    unsigned long address,
   33.46  		    unsigned long size)
   33.47  {
   33.48 -	int f(pte_t *pte, struct page *pte_page, unsigned long addr,
   33.49 -	      void *data) {
   33.50 -		return 0;
   33.51 -	}
   33.52 -
   33.53 -	return generic_page_range(mm, address, size, f, NULL);
   33.54 +	return generic_page_range(mm, address, size, noop_fn, NULL);
   33.55  } 
   33.56  
   33.57  EXPORT_SYMBOL(touch_pte_range);
    54.1 --- a/linux-2.6-xen-sparse/arch/xen/x86_64/mm/ioremap.c	Tue Aug 23 08:40:50 2005 +0000
    54.2 +++ b/linux-2.6-xen-sparse/arch/xen/x86_64/mm/ioremap.c	Tue Aug 23 08:40:58 2005 +0000
    54.3 @@ -465,33 +465,35 @@ int direct_remap_area_pages(struct mm_st
    54.4  
    54.5  EXPORT_SYMBOL(direct_remap_area_pages);
    54.6  
    54.7 +static int lookup_pte_fn(
    54.8 +    pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
    54.9 +{
   54.10 +    unsigned long *ptep = (unsigned long *)data;
   54.11 +    if (ptep) *ptep = (pfn_to_mfn(page_to_pfn(pte_page)) << PAGE_SHIFT)
   54.12 +                  | ((unsigned long)pte & ~PAGE_MASK);
   54.13 +    return 0;
   54.14 +}
   54.15 +
   54.16  int create_lookup_pte_addr(struct mm_struct *mm, 
   54.17                             unsigned long address,
   54.18                             unsigned long *ptep)
   54.19  {
   54.20 -    int f(pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
   54.21 -    {
   54.22 -        unsigned long *ptep = (unsigned long *)data;
   54.23 -        if (ptep) *ptep = (pfn_to_mfn(page_to_pfn(pte_page)) << PAGE_SHIFT)
   54.24 -                       | ((unsigned long)pte & ~PAGE_MASK);
   54.25 -        return 0;
   54.26 -    }
   54.27 -
   54.28 -    return generic_page_range(mm, address, PAGE_SIZE, f, ptep);
   54.29 +    return generic_page_range(mm, address, PAGE_SIZE, lookup_pte_fn, ptep);
   54.30  }
   54.31  
   54.32  EXPORT_SYMBOL(create_lookup_pte_addr);
   54.33  
   54.34 +static int noop_fn(
   54.35 +    pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
   54.36 +{
   54.37 +    return 0;
   54.38 +}
   54.39 +
   54.40  int touch_pte_range(struct mm_struct *mm,
   54.41                      unsigned long address,
   54.42                      unsigned long size)
   54.43  {
   54.44 -    int f(pte_t *pte, struct page *pte_page, unsigned long addr, void *data) 
   54.45 -    {
   54.46 -        return 0;
   54.47 -    }
   54.48 -
   54.49 -    return generic_page_range(mm, address, size, f, NULL);
   54.50 -}                 
   54.51 +    return generic_page_range(mm, address, size, noop_fn, NULL);
   54.52 +}
   54.53  
   54.54  EXPORT_SYMBOL(touch_pte_range);
    56.1 --- a/linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c	Tue Aug 23 08:40:50 2005 +0000
    56.2 +++ b/linux-2.6-xen-sparse/drivers/xen/balloon/balloon.c	Tue Aug 23 08:40:58 2005 +0000
    56.3 @@ -83,12 +83,15 @@ static struct timer_list balloon_timer;
    56.4  
    56.5  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
    56.6  /* Use the private and mapping fields of struct page as a list. */
    56.7 -#define PAGE_TO_LIST(p) ( (struct list_head *)&p->private )
    56.8 -#define LIST_TO_PAGE(l) ( list_entry( ((unsigned long *)l),   \
    56.9 -                                      struct page, private ) )
   56.10 -#define UNLIST_PAGE(p)  do { list_del(PAGE_TO_LIST(p));       \
   56.11 -                             p->mapping = NULL;               \
   56.12 -                             p->private = 0; } while(0)
   56.13 +#define PAGE_TO_LIST(p) ((struct list_head *)&p->private)
   56.14 +#define LIST_TO_PAGE(l)				\
   56.15 +	(list_entry(((unsigned long *)l), struct page, private))
   56.16 +#define UNLIST_PAGE(p)				\
   56.17 +	do {					\
   56.18 +		list_del(PAGE_TO_LIST(p));	\
   56.19 +		p->mapping = NULL;		\
   56.20 +		p->private = 0;			\
   56.21 +	} while(0)
   56.22  #else
   56.23  /* There's a dedicated list field in struct page we can use.    */
   56.24  #define PAGE_TO_LIST(p) ( &p->list )
   56.25 @@ -104,56 +107,53 @@ static struct timer_list balloon_timer;
   56.26  #endif
   56.27  
   56.28  #define IPRINTK(fmt, args...) \
   56.29 -    printk(KERN_INFO "xen_mem: " fmt, ##args)
   56.30 +	printk(KERN_INFO "xen_mem: " fmt, ##args)
   56.31  #define WPRINTK(fmt, args...) \
   56.32 -    printk(KERN_WARNING "xen_mem: " fmt, ##args)
   56.33 +	printk(KERN_WARNING "xen_mem: " fmt, ##args)
   56.34  
   56.35  /* balloon_append: add the given page to the balloon. */
   56.36  static void balloon_append(struct page *page)
   56.37  {
   56.38 -    /* Low memory is re-populated first, so highmem pages go at list tail. */
   56.39 -    if ( PageHighMem(page) )
   56.40 -    {
   56.41 -        list_add_tail(PAGE_TO_LIST(page), &ballooned_pages);
   56.42 -        balloon_high++;
   56.43 -    }
   56.44 -    else
   56.45 -    {
   56.46 -        list_add(PAGE_TO_LIST(page), &ballooned_pages);
   56.47 -        balloon_low++;
   56.48 -    }
   56.49 +	/* Lowmem is re-populated first, so highmem pages go at list tail. */
   56.50 +	if (PageHighMem(page)) {
   56.51 +		list_add_tail(PAGE_TO_LIST(page), &ballooned_pages);
   56.52 +		balloon_high++;
   56.53 +	} else {
   56.54 +		list_add(PAGE_TO_LIST(page), &ballooned_pages);
   56.55 +		balloon_low++;
   56.56 +	}
   56.57  }
   56.58  
   56.59  /* balloon_retrieve: rescue a page from the balloon, if it is not empty. */
   56.60  static struct page *balloon_retrieve(void)
   56.61  {
   56.62 -    struct page *page;
   56.63 +	struct page *page;
   56.64  
   56.65 -    if ( list_empty(&ballooned_pages) )
   56.66 -        return NULL;
   56.67 +	if (list_empty(&ballooned_pages))
   56.68 +		return NULL;
   56.69  
   56.70 -    page = LIST_TO_PAGE(ballooned_pages.next);
   56.71 -    UNLIST_PAGE(page);
   56.72 +	page = LIST_TO_PAGE(ballooned_pages.next);
   56.73 +	UNLIST_PAGE(page);
   56.74  
   56.75 -    if ( PageHighMem(page) )
   56.76 -        balloon_high--;
   56.77 -    else
   56.78 -        balloon_low--;
   56.79 +	if (PageHighMem(page))
   56.80 +		balloon_high--;
   56.81 +	else
   56.82 +		balloon_low--;
   56.83  
   56.84 -    return page;
   56.85 +	return page;
   56.86  }
   56.87  
   56.88  static void balloon_alarm(unsigned long unused)
   56.89  {
   56.90 -    schedule_work(&balloon_worker);
   56.91 +	schedule_work(&balloon_worker);
   56.92  }
   56.93  
   56.94  static unsigned long current_target(void)
   56.95  {
   56.96 -    unsigned long target = min(target_pages, hard_limit);
   56.97 -    if ( target > (current_pages + balloon_low + balloon_high) )
   56.98 -        target = current_pages + balloon_low + balloon_high;
   56.99 -    return target;
  56.100 +	unsigned long target = min(target_pages, hard_limit);
  56.101 +	if (target > (current_pages + balloon_low + balloon_high))
  56.102 +		target = current_pages + balloon_low + balloon_high;
  56.103 +	return target;
  56.104  }
  56.105  
  56.106  /*
  56.107 @@ -164,161 +164,147 @@ static unsigned long current_target(void
  56.108   */
  56.109  static void balloon_process(void *unused)
  56.110  {
  56.111 -    unsigned long *mfn_list, pfn, i, flags;
  56.112 -    struct page   *page;
  56.113 -    long           credit, debt, rc;
  56.114 -    void          *v;
  56.115 +	unsigned long *mfn_list, pfn, i, flags;
  56.116 +	struct page   *page;
  56.117 +	long           credit, debt, rc;
  56.118 +	void          *v;
  56.119  
  56.120 -    down(&balloon_mutex);
  56.121 +	down(&balloon_mutex);
  56.122  
  56.123   retry:
  56.124 -    mfn_list = NULL;
  56.125 +	mfn_list = NULL;
  56.126  
  56.127 -    if ( (credit = current_target() - current_pages) > 0 )
  56.128 -    {
  56.129 -        mfn_list = (unsigned long *)vmalloc(credit * sizeof(*mfn_list));
  56.130 -        if ( mfn_list == NULL )
  56.131 -            goto out;
  56.132 +	if ((credit = current_target() - current_pages) > 0) {
  56.133 +		mfn_list = vmalloc(credit * sizeof(*mfn_list));
  56.134 +		if (mfn_list == NULL)
  56.135 +			goto out;
  56.136  
  56.137 -        balloon_lock(flags);
  56.138 -        rc = HYPERVISOR_dom_mem_op(
  56.139 -            MEMOP_increase_reservation, mfn_list, credit, 0);
  56.140 -        balloon_unlock(flags);
  56.141 -        if ( rc < credit )
  56.142 -        {
  56.143 -            /* We hit the Xen hard limit: reprobe. */
  56.144 -            if ( HYPERVISOR_dom_mem_op(
  56.145 -                MEMOP_decrease_reservation, mfn_list, rc, 0) != rc )
  56.146 -                BUG();
  56.147 -            hard_limit = current_pages + rc - driver_pages;
  56.148 -            vfree(mfn_list);
  56.149 -            goto retry;
  56.150 -        }
  56.151 +		balloon_lock(flags);
  56.152 +		rc = HYPERVISOR_dom_mem_op(
  56.153 +			MEMOP_increase_reservation, mfn_list, credit, 0);
  56.154 +		balloon_unlock(flags);
  56.155 +		if (rc < credit) {
  56.156 +			/* We hit the Xen hard limit: reprobe. */
  56.157 +			BUG_ON(HYPERVISOR_dom_mem_op(
  56.158 +				MEMOP_decrease_reservation,
  56.159 +				mfn_list, rc, 0) != rc);
  56.160 +			hard_limit = current_pages + rc - driver_pages;
  56.161 +			vfree(mfn_list);
  56.162 +			goto retry;
  56.163 +		}
  56.164  
  56.165 -        for ( i = 0; i < credit; i++ )
  56.166 -        {
  56.167 -            if ( (page = balloon_retrieve()) == NULL )
  56.168 -                BUG();
  56.169 +		for (i = 0; i < credit; i++) {
  56.170 +			page = balloon_retrieve();
  56.171 +			BUG_ON(page == NULL);
  56.172  
  56.173 -            pfn = page - mem_map;
  56.174 -            if ( phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY )
  56.175 -                BUG();
  56.176 +			pfn = page - mem_map;
  56.177 +			if (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY)
  56.178 +				BUG();
  56.179  
  56.180 -            /* Update P->M and M->P tables. */
  56.181 -            phys_to_machine_mapping[pfn] = mfn_list[i];
  56.182 -            xen_machphys_update(mfn_list[i], pfn);
  56.183 +			/* Update P->M and M->P tables. */
  56.184 +			phys_to_machine_mapping[pfn] = mfn_list[i];
  56.185 +			xen_machphys_update(mfn_list[i], pfn);
  56.186              
  56.187 -            /* Link back into the page tables if it's not a highmem page. */
  56.188 -            if ( pfn < max_low_pfn )
  56.189 -            {
  56.190 -                BUG_ON(HYPERVISOR_update_va_mapping(
  56.191 -                    (unsigned long)__va(pfn << PAGE_SHIFT),
  56.192 -                    pfn_pte_ma(mfn_list[i], PAGE_KERNEL), 0));
  56.193 -            }
  56.194 +			/* Link back into the page tables if not highmem. */
  56.195 +			if (pfn < max_low_pfn)
  56.196 +				BUG_ON(HYPERVISOR_update_va_mapping(
  56.197 +					(unsigned long)__va(pfn << PAGE_SHIFT),
  56.198 +					pfn_pte_ma(mfn_list[i], PAGE_KERNEL),
  56.199 +					0));
  56.200  
  56.201 -            /* Finally, relinquish the memory back to the system allocator. */
  56.202 -            ClearPageReserved(page);
  56.203 -            set_page_count(page, 1);
  56.204 -            __free_page(page);
  56.205 -        }
  56.206 +			/* Relinquish the page back to the allocator. */
  56.207 +			ClearPageReserved(page);
  56.208 +			set_page_count(page, 1);
  56.209 +			__free_page(page);
  56.210 +		}
  56.211  
  56.212 -        current_pages += credit;
  56.213 -    }
  56.214 -    else if ( credit < 0 )
  56.215 -    {
  56.216 -        debt = -credit;
  56.217 +		current_pages += credit;
  56.218 +	} else if (credit < 0) {
  56.219 +		debt = -credit;
  56.220  
  56.221 -        mfn_list = (unsigned long *)vmalloc(debt * sizeof(*mfn_list));
  56.222 -        if ( mfn_list == NULL )
  56.223 -            goto out;
  56.224 +		mfn_list = vmalloc(debt * sizeof(*mfn_list));
  56.225 +		if (mfn_list == NULL)
  56.226 +			goto out;
  56.227  
  56.228 -        for ( i = 0; i < debt; i++ )
  56.229 -        {
  56.230 -            if ( (page = alloc_page(GFP_HIGHUSER)) == NULL )
  56.231 -            {
  56.232 -                debt = i;
  56.233 -                break;
  56.234 -            }
  56.235 +		for (i = 0; i < debt; i++) {
  56.236 +			if ((page = alloc_page(GFP_HIGHUSER)) == NULL) {
  56.237 +				debt = i;
  56.238 +				break;
  56.239 +			}
  56.240  
  56.241 -            pfn = page - mem_map;
  56.242 -            mfn_list[i] = phys_to_machine_mapping[pfn];
  56.243 +			pfn = page - mem_map;
  56.244 +			mfn_list[i] = phys_to_machine_mapping[pfn];
  56.245  
  56.246 -            if ( !PageHighMem(page) )
  56.247 -            {
  56.248 -                v = phys_to_virt(pfn << PAGE_SHIFT);
  56.249 -                scrub_pages(v, 1);
  56.250 -                BUG_ON(HYPERVISOR_update_va_mapping(
  56.251 -                    (unsigned long)v, __pte_ma(0), 0));
  56.252 -            }
  56.253 +			if (!PageHighMem(page)) {
  56.254 +				v = phys_to_virt(pfn << PAGE_SHIFT);
  56.255 +				scrub_pages(v, 1);
  56.256 +				BUG_ON(HYPERVISOR_update_va_mapping(
  56.257 +					(unsigned long)v, __pte_ma(0), 0));
  56.258 +			}
  56.259  #ifdef CONFIG_XEN_SCRUB_PAGES
  56.260 -            else
  56.261 -            {
  56.262 -                v = kmap(page);
  56.263 -                scrub_pages(v, 1);
  56.264 -                kunmap(page);
  56.265 -            }
  56.266 +			else {
  56.267 +				v = kmap(page);
  56.268 +				scrub_pages(v, 1);
  56.269 +				kunmap(page);
  56.270 +			}
  56.271  #endif
  56.272 -        }
  56.273 +		}
  56.274  
  56.275 -        /* Ensure that ballooned highmem pages don't have cached mappings. */
  56.276 -        kmap_flush_unused();
  56.277 -        flush_tlb_all();
  56.278 +		/* Ensure that ballooned highmem pages don't have kmaps. */
  56.279 +		kmap_flush_unused();
  56.280 +		flush_tlb_all();
  56.281  
  56.282 -        /* No more mappings: invalidate pages in P2M and add to balloon. */
  56.283 -        for ( i = 0; i < debt; i++ )
  56.284 -        {
  56.285 -            pfn = mfn_to_pfn(mfn_list[i]);
  56.286 -            phys_to_machine_mapping[pfn] = INVALID_P2M_ENTRY;
  56.287 -            balloon_append(pfn_to_page(pfn));
  56.288 -        }
  56.289 +		/* No more mappings: invalidate P2M and add to balloon. */
  56.290 +		for (i = 0; i < debt; i++) {
  56.291 +			pfn = mfn_to_pfn(mfn_list[i]);
  56.292 +			phys_to_machine_mapping[pfn] = INVALID_P2M_ENTRY;
  56.293 +			balloon_append(pfn_to_page(pfn));
  56.294 +		}
  56.295  
  56.296 -        if ( HYPERVISOR_dom_mem_op(
  56.297 -            MEMOP_decrease_reservation, mfn_list, debt, 0) != debt )
  56.298 -            BUG();
  56.299 +		BUG_ON(HYPERVISOR_dom_mem_op(
  56.300 +			MEMOP_decrease_reservation,mfn_list, debt, 0) != debt);
  56.301  
  56.302 -        current_pages -= debt;
  56.303 -    }
  56.304 +		current_pages -= debt;
  56.305 +	}
  56.306  
  56.307   out:
  56.308 -    if ( mfn_list != NULL )
  56.309 -        vfree(mfn_list);
  56.310 +	if (mfn_list != NULL)
  56.311 +		vfree(mfn_list);
  56.312  
  56.313 -    /* Schedule more work if there is some still to be done. */
  56.314 -    if ( current_target() != current_pages )
  56.315 -        mod_timer(&balloon_timer, jiffies + HZ);
  56.316 +	/* Schedule more work if there is some still to be done. */
  56.317 +	if (current_target() != current_pages)
  56.318 +		mod_timer(&balloon_timer, jiffies + HZ);
  56.319  
  56.320 -    up(&balloon_mutex);
  56.321 +	up(&balloon_mutex);
  56.322  }
  56.323  
  56.324  /* Resets the Xen limit, sets new target, and kicks off processing. */
  56.325  static void set_new_target(unsigned long target)
  56.326  {
  56.327 -    /* No need for lock. Not read-modify-write updates. */
  56.328 -    hard_limit   = ~0UL;
  56.329 -    target_pages = target;
  56.330 -    schedule_work(&balloon_worker);
  56.331 +	/* No need for lock. Not read-modify-write updates. */
  56.332 +	hard_limit   = ~0UL;
  56.333 +	target_pages = target;
  56.334 +	schedule_work(&balloon_worker);
  56.335  }
  56.336  
  56.337  static struct xenbus_watch target_watch =
  56.338  {
  56.339 -    .node = "memory/target"
  56.340 +	.node = "memory/target"
  56.341  };
  56.342  
  56.343  /* React to a change in the target key */
  56.344  static void watch_target(struct xenbus_watch *watch, const char *node)
  56.345  {
  56.346 -    unsigned long new_target;
  56.347 -    int err;
  56.348 +	unsigned long new_target;
  56.349 +	int err;
  56.350  
  56.351 -    err = xenbus_scanf("memory", "target", "%lu", &new_target);
  56.352 +	err = xenbus_scanf("memory", "target", "%lu", &new_target);
  56.353 +	if (err != 1) {
  56.354 +		printk(KERN_ERR "Unable to read memory/target\n");
  56.355 +		return;
  56.356 +	} 
  56.357          
  56.358 -    if(err != 1) 
  56.359 -    {
  56.360 -        printk(KERN_ERR "Unable to read memory/target\n");
  56.361 -        return;
  56.362 -    } 
  56.363 -        
  56.364 -    set_new_target(new_target >> PAGE_SHIFT);
  56.365 +	set_new_target(new_target >> PAGE_SHIFT);
  56.366      
  56.367  }
  56.368  
  56.369 @@ -329,141 +315,185 @@ int balloon_init_watcher(struct notifier
  56.370                           unsigned long event,
  56.371                           void *data)
  56.372  {
  56.373 -    int err;
  56.374 +	int err;
  56.375  
  56.376 -    BUG_ON(down_trylock(&xenbus_lock) == 0);
  56.377 -
  56.378 -    err = register_xenbus_watch(&target_watch);
  56.379 +	BUG_ON(down_trylock(&xenbus_lock) == 0);
  56.380  
  56.381 -    if (err) {
  56.382 -        printk(KERN_ERR "Failed to set balloon watcher\n");
  56.383 -    }
  56.384 +	err = register_xenbus_watch(&target_watch);
  56.385 +	if (err)
  56.386 +		printk(KERN_ERR "Failed to set balloon watcher\n");
  56.387  
  56.388 -    return NOTIFY_DONE;
  56.389 +	return NOTIFY_DONE;
  56.390      
  56.391  }
  56.392  
  56.393  static int balloon_write(struct file *file, const char __user *buffer,
  56.394                           unsigned long count, void *data)
  56.395  {
  56.396 -    char memstring[64], *endchar;
  56.397 -    unsigned long long target_bytes;
  56.398 +	char memstring[64], *endchar;
  56.399 +	unsigned long long target_bytes;
  56.400  
  56.401 -    if ( !capable(CAP_SYS_ADMIN) )
  56.402 -        return -EPERM;
  56.403 +	if (!capable(CAP_SYS_ADMIN))
  56.404 +		return -EPERM;
  56.405  
  56.406 -    if ( count <= 1 )
  56.407 -        return -EBADMSG; /* runt */
  56.408 -    if ( count > sizeof(memstring) )
  56.409 -        return -EFBIG;   /* too long */
  56.410 +	if (count <= 1)
  56.411 +		return -EBADMSG; /* runt */
  56.412 +	if (count > sizeof(memstring))
  56.413 +		return -EFBIG;   /* too long */
  56.414  
  56.415 -    if ( copy_from_user(memstring, buffer, count) )
  56.416 -        return -EFAULT;
  56.417 -    memstring[sizeof(memstring)-1] = '\0';
  56.418 +	if (copy_from_user(memstring, buffer, count))
  56.419 +		return -EFAULT;
  56.420 +	memstring[sizeof(memstring)-1] = '\0';
  56.421  
  56.422 -    target_bytes = memparse(memstring, &endchar);
  56.423 -    set_new_target(target_bytes >> PAGE_SHIFT);
  56.424 +	target_bytes = memparse(memstring, &endchar);
  56.425 +	set_new_target(target_bytes >> PAGE_SHIFT);
  56.426  
  56.427 -    return count;
  56.428 +	return count;
  56.429  }
  56.430  
  56.431  static int balloon_read(char *page, char **start, off_t off,
  56.432                          int count, int *eof, void *data)
  56.433  {
  56.434 -    int len;
  56.435 +	int len;
  56.436  
  56.437 -    len = sprintf(
  56.438 -        page,
  56.439 -        "Current allocation: %8lu kB\n"
  56.440 -        "Requested target:   %8lu kB\n"
  56.441 -        "Low-mem balloon:    %8lu kB\n"
  56.442 -        "High-mem balloon:   %8lu kB\n"
  56.443 -        "Xen hard limit:     ",
  56.444 -        PAGES2KB(current_pages), PAGES2KB(target_pages), 
  56.445 -        PAGES2KB(balloon_low), PAGES2KB(balloon_high));
  56.446 +	len = sprintf(
  56.447 +		page,
  56.448 +		"Current allocation: %8lu kB\n"
  56.449 +		"Requested target:   %8lu kB\n"
  56.450 +		"Low-mem balloon:    %8lu kB\n"
  56.451 +		"High-mem balloon:   %8lu kB\n"
  56.452 +		"Xen hard limit:     ",
  56.453 +		PAGES2KB(current_pages), PAGES2KB(target_pages), 
  56.454 +		PAGES2KB(balloon_low), PAGES2KB(balloon_high));
  56.455  
  56.456 -    if ( hard_limit != ~0UL )
  56.457 -        len += sprintf(
  56.458 -            page + len, 
  56.459 -            "%8lu kB (inc. %8lu kB driver headroom)\n",
  56.460 -            PAGES2KB(hard_limit), PAGES2KB(driver_pages));
  56.461 -    else
  56.462 -        len += sprintf(
  56.463 -            page + len,
  56.464 -            "     ??? kB\n");
  56.465 +	if (hard_limit != ~0UL) {
  56.466 +		len += sprintf(
  56.467 +			page + len, 
  56.468 +			"%8lu kB (inc. %8lu kB driver headroom)\n",
  56.469 +			PAGES2KB(hard_limit), PAGES2KB(driver_pages));
  56.470 +	} else {
  56.471 +		len += sprintf(
  56.472 +			page + len,
  56.473 +			"     ??? kB\n");
  56.474 +	}
  56.475  
  56.476 -    *eof = 1;
  56.477 -    return len;
  56.478 +	*eof = 1;
  56.479 +	return len;
  56.480  }
  56.481  
  56.482  static struct notifier_block xenstore_notifier;
  56.483  
  56.484  static int __init balloon_init(void)
  56.485  {
  56.486 -    unsigned long pfn;
  56.487 -    struct page *page;
  56.488 +	unsigned long pfn;
  56.489 +	struct page *page;
  56.490  
  56.491 -    IPRINTK("Initialising balloon driver.\n");
  56.492 +	IPRINTK("Initialising balloon driver.\n");
  56.493  
  56.494 -    current_pages = min(xen_start_info.nr_pages, max_pfn);
  56.495 -    target_pages  = current_pages;
  56.496 -    balloon_low   = 0;
  56.497 -    balloon_high  = 0;
  56.498 -    driver_pages  = 0UL;
  56.499 -    hard_limit    = ~0UL;
  56.500 +	current_pages = min(xen_start_info.nr_pages, max_pfn);
  56.501 +	target_pages  = current_pages;
  56.502 +	balloon_low   = 0;
  56.503 +	balloon_high  = 0;
  56.504 +	driver_pages  = 0UL;
  56.505 +	hard_limit    = ~0UL;
  56.506  
  56.507 -    init_timer(&balloon_timer);
  56.508 -    balloon_timer.data = 0;
  56.509 -    balloon_timer.function = balloon_alarm;
  56.510 +	init_timer(&balloon_timer);
  56.511 +	balloon_timer.data = 0;
  56.512 +	balloon_timer.function = balloon_alarm;
  56.513      
  56.514 -    if ( (balloon_pde = create_xen_proc_entry("balloon", 0644)) == NULL )
  56.515 -    {
  56.516 -        WPRINTK("Unable to create /proc/xen/balloon.\n");
  56.517 -        return -1;
  56.518 -    }
  56.519 +	if ((balloon_pde = create_xen_proc_entry("balloon", 0644)) == NULL) {
  56.520 +		WPRINTK("Unable to create /proc/xen/balloon.\n");
  56.521 +		return -1;
  56.522 +	}
  56.523  
  56.524 -    balloon_pde->read_proc  = balloon_read;
  56.525 -    balloon_pde->write_proc = balloon_write;
  56.526 +	balloon_pde->read_proc  = balloon_read;
  56.527 +	balloon_pde->write_proc = balloon_write;
  56.528      
  56.529 -    /* Initialise the balloon with excess memory space. */
  56.530 -    for ( pfn = xen_start_info.nr_pages; pfn < max_pfn; pfn++ )
  56.531 -    {
  56.532 -        page = &mem_map[pfn];
  56.533 -        if ( !PageReserved(page) )
  56.534 -            balloon_append(page);
  56.535 -    }
  56.536 +	/* Initialise the balloon with excess memory space. */
  56.537 +	for (pfn = xen_start_info.nr_pages; pfn < max_pfn; pfn++) {
  56.538 +		page = &mem_map[pfn];
  56.539 +		if (!PageReserved(page))
  56.540 +			balloon_append(page);
  56.541 +	}
  56.542  
  56.543 -    target_watch.callback = watch_target;
  56.544 -    xenstore_notifier.notifier_call = balloon_init_watcher;
  56.545 +	target_watch.callback = watch_target;
  56.546 +	xenstore_notifier.notifier_call = balloon_init_watcher;
  56.547  
  56.548 -    register_xenstore_notifier(&xenstore_notifier);
  56.549 +	register_xenstore_notifier(&xenstore_notifier);
  56.550      
  56.551 -    return 0;
  56.552 +	return 0;
  56.553  }
  56.554  
  56.555  subsys_initcall(balloon_init);
  56.556  
  56.557  void balloon_update_driver_allowance(long delta)
  56.558  {
  56.559 -    unsigned long flags;
  56.560 -    balloon_lock(flags);
  56.561 -    driver_pages += delta; /* non-atomic update */
  56.562 -    balloon_unlock(flags);
  56.563 +	unsigned long flags;
  56.564 +	balloon_lock(flags);
  56.565 +	driver_pages += delta; /* non-atomic update */
  56.566 +	balloon_unlock(flags);
  56.567 +}
  56.568 +
  56.569 +static int dealloc_pte_fn(
  56.570 +	pte_t *pte, struct page *pte_page, unsigned long addr, void *data)
  56.571 +{
  56.572 +	unsigned long mfn = pte_mfn(*pte);
  56.573 +	set_pte(pte, __pte_ma(0));
  56.574 +	phys_to_machine_mapping[__pa(addr) >> PAGE_SHIFT] =
  56.575 +		INVALID_P2M_ENTRY;
  56.576 +	BUG_ON(HYPERVISOR_dom_mem_op(
  56.577 +		MEMOP_decrease_reservation, &mfn, 1, 0) != 1);
  56.578 +	return 0;
  56.579  }
  56.580  
  56.581 -void balloon_put_pages(unsigned long *mfn_list, unsigned long nr_mfns)
  56.582 +struct page *balloon_alloc_empty_page_range(unsigned long nr_pages)
  56.583  {
  56.584 -    unsigned long flags;
  56.585 +	unsigned long vstart, flags;
  56.586 +	unsigned int  order = get_order(nr_pages * PAGE_SIZE);
  56.587 +
  56.588 +	vstart = __get_free_pages(GFP_KERNEL, order);
  56.589 +	if (vstart == 0)
  56.590 +		return NULL;
  56.591 +
  56.592 +	scrub_pages(vstart, 1 << order);
  56.593 +
  56.594 +	balloon_lock(flags);
  56.595 +	BUG_ON(generic_page_range(
  56.596 +		&init_mm, vstart, PAGE_SIZE << order, dealloc_pte_fn, NULL));
  56.597 +	current_pages -= 1UL << order;
  56.598 +	balloon_unlock(flags);
  56.599  
  56.600 -    balloon_lock(flags);
  56.601 -    if ( HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation, 
  56.602 -                               mfn_list, nr_mfns, 0) != nr_mfns )
  56.603 -        BUG();
  56.604 -    current_pages -= nr_mfns; /* non-atomic update */
  56.605 -    balloon_unlock(flags);
  56.606 +	schedule_work(&balloon_worker);
  56.607 +
  56.608 +	flush_tlb_all();
  56.609 +
  56.610 +	return virt_to_page(vstart);
  56.611 +}
  56.612  
  56.613 -    schedule_work(&balloon_worker);
  56.614 +void balloon_dealloc_empty_page_range(
  56.615 +	struct page *page, unsigned long nr_pages)
  56.616 +{
  56.617 +	unsigned long i, flags;
  56.618 +	unsigned int  order = get_order(nr_pages * PAGE_SIZE);
  56.619 +
  56.620 +	balloon_lock(flags);
  56.621 +	for (i = 0; i < (1UL << order); i++)
  56.622 +		balloon_append(page + i);
  56.623 +	balloon_unlock(flags);
  56.624 +
  56.625 +	schedule_work(&balloon_worker);
  56.626  }
  56.627  
  56.628  EXPORT_SYMBOL(balloon_update_driver_allowance);
  56.629 -EXPORT_SYMBOL(balloon_put_pages);
  56.630 +EXPORT_SYMBOL(balloon_alloc_empty_page_range);
  56.631 +EXPORT_SYMBOL(balloon_dealloc_empty_page_range);
  56.632 +
  56.633 +/*
  56.634 + * Local variables:
  56.635 + *  c-file-style: "linux"
  56.636 + *  indent-tabs-mode: t
  56.637 + *  c-indent-level: 8
  56.638 + *  c-basic-offset: 8
  56.639 + *  tab-width: 8
  56.640 + * End:
  56.641 + */
    58.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c	Tue Aug 23 08:40:50 2005 +0000
    58.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c	Tue Aug 23 08:40:58 2005 +0000
    58.3 @@ -569,6 +569,7 @@ void blkif_deschedule(blkif_t *blkif)
    58.4  static int __init blkif_init(void)
    58.5  {
    58.6      int i;
    58.7 +    struct page *page;
    58.8  
    58.9      if ( !(xen_start_info.flags & SIF_INITDOMAIN) &&
   58.10           !(xen_start_info.flags & SIF_BLK_BE_DOMAIN) )
   58.11 @@ -576,8 +577,9 @@ static int __init blkif_init(void)
   58.12  
   58.13      blkif_interface_init();
   58.14  
   58.15 -    if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
   58.16 -        BUG();
   58.17 +    page = balloon_alloc_empty_page_range(MMAP_PAGES);
   58.18 +    BUG_ON(page == NULL);
   58.19 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
   58.20  
   58.21      pending_cons = 0;
   58.22      pending_prod = MAX_PENDING_REQS;
    67.1 --- a/linux-2.6-xen-sparse/drivers/xen/blktap/blktap_userdev.c	Tue Aug 23 08:40:50 2005 +0000
    67.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blktap/blktap_userdev.c	Tue Aug 23 08:40:58 2005 +0000
    67.3 @@ -775,9 +775,11 @@ static struct miscdevice blktap_miscdev 
    67.4  int blktap_init(void)
    67.5  {
    67.6      int err, i, j;
    67.7 +    struct page *page;
    67.8  
    67.9 -    if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
   67.10 -        BUG();
   67.11 +    page = balloon_alloc_empty_page_range(MMAP_PAGES);
   67.12 +    BUG_ON(page == NULL);
   67.13 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
   67.14  
   67.15  #ifdef CONFIG_XEN_BLKDEV_GRANT
   67.16      for (i=0; i<MAX_PENDING_REQS ; i++)
    71.1 --- a/linux-2.6-xen-sparse/drivers/xen/netback/netback.c	Tue Aug 23 08:40:50 2005 +0000
    71.2 +++ b/linux-2.6-xen-sparse/drivers/xen/netback/netback.c	Tue Aug 23 08:40:58 2005 +0000
    71.3 @@ -968,8 +968,9 @@ static int __init netback_init(void)
    71.4      
    71.5      netif_interface_init();
    71.6  
    71.7 -    mmap_vstart = allocate_empty_lowmem_region(MAX_PENDING_REQS);
    71.8 -    BUG_ON(mmap_vstart == 0);
    71.9 +    page = balloon_alloc_empty_page_range(MAX_PENDING_REQS);
   71.10 +    BUG_ON(page == NULL);
   71.11 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
   71.12  
   71.13      for ( i = 0; i < MAX_PENDING_REQS; i++ )
   71.14      {
    74.1 --- a/linux-2.6-xen-sparse/drivers/xen/usbback/usbback.c	Tue Aug 23 08:40:50 2005 +0000
    74.2 +++ b/linux-2.6-xen-sparse/drivers/xen/usbback/usbback.c	Tue Aug 23 08:40:58 2005 +0000
    74.3 @@ -1027,13 +1027,15 @@ void usbif_release_ports(usbif_priv_t *u
    74.4  static int __init usbif_init(void)
    74.5  {
    74.6      int i;
    74.7 +    struct page *page;
    74.8  
    74.9      if ( !(xen_start_info.flags & SIF_INITDOMAIN) &&
   74.10           !(xen_start_info.flags & SIF_USB_BE_DOMAIN) )
   74.11          return 0;
   74.12 -    
   74.13 -    if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
   74.14 -        BUG();
   74.15 +
   74.16 +    page = balloon_alloc_empty_page_range(MMAP_PAGES);
   74.17 +    BUG_ON(page == NULL);
   74.18 +    mmap_vstart = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
   74.19  
   74.20      pending_cons = 0;
   74.21      pending_prod = MAX_PENDING_REQS;
    97.1 --- a/linux-2.6-xen-sparse/include/asm-xen/balloon.h	Tue Aug 23 08:40:50 2005 +0000
    97.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/balloon.h	Tue Aug 23 08:40:58 2005 +0000
    97.3 @@ -35,10 +35,19 @@
    97.4   * Inform the balloon driver that it should allow some slop for device-driver
    97.5   * memory activities.
    97.6   */
    97.7 -extern void balloon_update_driver_allowance(long delta);
    97.8 +extern void
    97.9 +balloon_update_driver_allowance(
   97.10 +	long delta);
   97.11  
   97.12 -/* Give up unmapped pages to the balloon driver. */
   97.13 -extern void balloon_put_pages(unsigned long *mfn_list, unsigned long nr_mfns);
   97.14 +/* Allocate an empty low-memory page range. */
   97.15 +extern struct page *
   97.16 +balloon_alloc_empty_page_range(
   97.17 +	unsigned long nr_pages);
   97.18 +
   97.19 +/* Deallocate an empty page range, adding to the balloon. */
   97.20 +extern void
   97.21 +balloon_dealloc_empty_page_range(
   97.22 +	struct page *page, unsigned long nr_pages);
   97.23  
   97.24  /*
   97.25   * Prevent the balloon driver from changing the memory reservation during
    98.1 --- a/linux-2.6-xen-sparse/include/asm-xen/hypervisor.h	Tue Aug 23 08:40:50 2005 +0000
    98.2 +++ b/linux-2.6-xen-sparse/include/asm-xen/hypervisor.h	Tue Aug 23 08:40:58 2005 +0000
    98.3 @@ -137,9 +137,6 @@ void xen_invlpg_mask(cpumask_t *mask, un
    98.4  void xen_create_contiguous_region(unsigned long vstart, unsigned int order);
    98.5  void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order);
    98.6  
    98.7 -/* Allocate a contiguous empty region of low memory. Return virtual start. */
    98.8 -unsigned long allocate_empty_lowmem_region(unsigned long pages);
    98.9 -
   98.10  #include <asm/hypercall.h>
   98.11  
   98.12  #if defined(CONFIG_X86_64)
   104.1 --- a/tools/Makefile	Tue Aug 23 08:40:50 2005 +0000
   104.2 +++ b/tools/Makefile	Tue Aug 23 08:40:58 2005 +0000
   104.3 @@ -14,6 +14,7 @@ SUBDIRS += xcutils
   104.4  SUBDIRS += firmware
   104.5  SUBDIRS += security
   104.6  SUBDIRS += console
   104.7 +SUBDIRS += xenstat
   104.8  
   104.9  .PHONY: all install clean check check_clean ioemu eioemuinstall ioemuclean
  104.10  
   105.1 --- a/tools/Rules.mk	Tue Aug 23 08:40:50 2005 +0000
   105.2 +++ b/tools/Rules.mk	Tue Aug 23 08:40:58 2005 +0000
   105.3 @@ -6,6 +6,7 @@ XEN_XC             = $(XEN_ROOT)/tools/p
   105.4  XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
   105.5  XEN_XCS            = $(XEN_ROOT)/tools/xcs
   105.6  XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
   105.7 +XEN_LIBXENSTAT     = $(XEN_ROOT)/tools/xenstat/libxenstat/src
   105.8  
   105.9  ifeq ($(XEN_TARGET_ARCH),x86_32)
  105.10  CFLAGS  += -m32 -march=i686
   152.1 --- a/tools/python/xen/xm/main.py	Tue Aug 23 08:40:50 2005 +0000
   152.2 +++ b/tools/python/xen/xm/main.py	Tue Aug 23 08:40:58 2005 +0000
   152.3 @@ -49,6 +49,7 @@ xm common subcommands:
   152.4      restore <File>          create a domain from a saved state file
   152.5      save <DomId> <File>     save domain state (and config) to file
   152.6      shutdown <DomId>        shutdown a domain
   152.7 +    top                     monitor system and domains in real-time
   152.8      unpause <DomId>         unpause a paused domain
   152.9  
  152.10  For a complete list of subcommands run 'xm help --long'
  152.11 @@ -87,6 +88,7 @@ xm full list of subcommands:
  152.12      dmesg   [--clear]         read or clear Xen's message buffer
  152.13      info                      get information about the xen host
  152.14      log                       print the xend log
  152.15 +    top                       monitor system and domains in real-time
  152.16  
  152.17    Scheduler Commands:
  152.18      bvt <options>             set BVT scheduler parameters
  152.19 @@ -457,6 +459,9 @@ def xm_console(args):
  152.20      os.execvp('/usr/libexec/xen/xenconsole', cmd.split())
  152.21      console = sxp.child(info, "console")
  152.22  
  152.23 +def xm_top(args):
  152.24 +    os.execv('/usr/sbin/xentop', ['/usr/sbin/xentop'])
  152.25 +
  152.26  def xm_dmesg(args):
  152.27      
  152.28      gopts = Opts(use="""[-c|--clear]
  152.29 @@ -545,6 +550,8 @@ def xm_block_destroy(args):
  152.30  commands = {
  152.31      # console commands
  152.32      "console": xm_console,
  152.33 +    # xenstat commands
  152.34 +    "top": xm_top,
  152.35      # domain commands
  152.36      "domid": xm_domid,
  152.37      "domname": xm_domname,
   155.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   155.2 +++ b/tools/xenstat/Makefile	Tue Aug 23 08:40:58 2005 +0000
   155.3 @@ -0,0 +1,13 @@
   155.4 +XEN_ROOT = ../..
   155.5 +include $(XEN_ROOT)/tools/Rules.mk
   155.6 +
   155.7 +SUBDIRS :=
   155.8 +SUBDIRS += libxenstat
   155.9 +SUBDIRS += xentop
  155.10 +
  155.11 +.PHONY: all install clean
  155.12 +
  155.13 +all install clean:
  155.14 +	@set -e; for subdir in $(SUBDIRS); do \
  155.15 +		$(MAKE) -C $$subdir $@; \
  155.16 +	done
   156.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   156.2 +++ b/tools/xenstat/libxenstat/COPYING	Tue Aug 23 08:40:58 2005 +0000
   156.3 @@ -0,0 +1,510 @@
   156.4 +
   156.5 +                  GNU LESSER GENERAL PUBLIC LICENSE
   156.6 +                       Version 2.1, February 1999
   156.7 +
   156.8 + Copyright (C) 1991, 1999 Free Software Foundation, Inc.
   156.9 +	51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  156.10 + Everyone is permitted to copy and distribute verbatim copies
  156.11 + of this license document, but changing it is not allowed.
  156.12 +
  156.13 +[This is the first released version of the Lesser GPL.  It also counts
  156.14 + as the successor of the GNU Library Public License, version 2, hence
  156.15 + the version number 2.1.]
  156.16 +
  156.17 +                            Preamble
  156.18 +
  156.19 +  The licenses for most software are designed to take away your
  156.20 +freedom to share and change it.  By contrast, the GNU General Public
  156.21 +Licenses are intended to guarantee your freedom to share and change
  156.22 +free software--to make sure the software is free for all its users.
  156.23 +
  156.24 +  This license, the Lesser General Public License, applies to some
  156.25 +specially designated software packages--typically libraries--of the
  156.26 +Free Software Foundation and other authors who decide to use it.  You
  156.27 +can use it too, but we suggest you first think carefully about whether
  156.28 +this license or the ordinary General Public License is the better
  156.29 +strategy to use in any particular case, based on the explanations
  156.30 +below.
  156.31 +
  156.32 +  When we speak of free software, we are referring to freedom of use,
  156.33 +not price.  Our General Public Licenses are designed to make sure that
  156.34 +you have the freedom to distribute copies of free software (and charge
  156.35 +for this service if you wish); that you receive source code or can get
  156.36 +it if you want it; that you can change the software and use pieces of
  156.37 +it in new free programs; and that you are informed that you can do
  156.38 +these things.
  156.39 +
  156.40 +  To protect your rights, we need to make restrictions that forbid
  156.41 +distributors to deny you these rights or to ask you to surrender these
  156.42 +rights.  These restrictions translate to certain responsibilities for
  156.43 +you if you distribute copies of the library or if you modify it.
  156.44 +
  156.45 +  For example, if you distribute copies of the library, whether gratis
  156.46 +or for a fee, you must give the recipients all the rights that we gave
  156.47 +you.  You must make sure that they, too, receive or can get the source
  156.48 +code.  If you link other code with the library, you must provide
  156.49 +complete object files to the recipients, so that they can relink them
  156.50 +with the library after making changes to the library and recompiling
  156.51 +it.  And you must show them these terms so they know their rights.
  156.52 +
  156.53 +  We protect your rights with a two-step method: (1) we copyright the
  156.54 +library, and (2) we offer you this license, which gives you legal
  156.55 +permission to copy, distribute and/or modify the library.
  156.56 +
  156.57 +  To protect each distributor, we want to make it very clear that
  156.58 +there is no warranty for the free library.  Also, if the library is
  156.59 +modified by someone else and passed on, the recipients should know
  156.60 +that what they have is not the original version, so that the original
  156.61 +author's reputation will not be affected by problems that might be
  156.62 +introduced by others.
  156.63 +
  156.64 +  Finally, software patents pose a constant threat to the existence of
  156.65 +any free program.  We wish to make sure that a company cannot
  156.66 +effectively restrict the users of a free program by obtaining a
  156.67 +restrictive license from a patent holder.  Therefore, we insist that
  156.68 +any patent license obtained for a version of the library must be
  156.69 +consistent with the full freedom of use specified in this license.
  156.70 +
  156.71 +  Most GNU software, including some libraries, is covered by the
  156.72 +ordinary GNU General Public License.  This license, the GNU Lesser
  156.73 +General Public License, applies to certain designated libraries, and
  156.74 +is quite different from the ordinary General Public License.  We use
  156.75 +this license for certain libraries in order to permit linking those
  156.76 +libraries into non-free programs.
  156.77 +
  156.78 +  When a program is linked with a library, whether statically or using
  156.79 +a shared library, the combination of the two is legally speaking a
  156.80 +combined work, a derivative of the original library.  The ordinary
  156.81 +General Public License therefore permits such linking only if the
  156.82 +entire combination fits its criteria of freedom.  The Lesser General
  156.83 +Public License permits more lax criteria for linking other code with
  156.84 +the library.
  156.85 +
  156.86 +  We call this license the "Lesser" General Public License because it
  156.87 +does Less to protect the user's freedom than the ordinary General
  156.88 +Public License.  It also provides other free software developers Less
  156.89 +of an advantage over competing non-free programs.  These disadvantages
  156.90 +are the reason we use the ordinary General Public License for many
  156.91 +libraries.  However, the Lesser license provides advantages in certain
  156.92 +special circumstances.
  156.93 +
  156.94 +  For example, on rare occasions, there may be a special need to
  156.95 +encourage the widest possible use of a certain library, so that it
  156.96 +becomes a de-facto standard.  To achieve this, non-free programs must
  156.97 +be allowed to use the library.  A more frequent case is that a free
  156.98 +library does the same job as widely used non-free libraries.  In this
  156.99 +case, there is little to gain by limiting the free library to free
 156.100 +software only, so we use the Lesser General Public License.
 156.101 +
 156.102 +  In other cases, permission to use a particular library in non-free
 156.103 +programs enables a greater number of people to use a large body of
 156.104 +free software.  For example, permission to use the GNU C Library in
 156.105 +non-free programs enables many more people to use the whole GNU
 156.106 +operating system, as well as its variant, the GNU/Linux operating
 156.107 +system.
 156.108 +
 156.109 +  Although the Lesser General Public License is Less protective of the
 156.110 +users' freedom, it does ensure that the user of a program that is
 156.111 +linked with the Library has the freedom and the wherewithal to run
 156.112 +that program using a modified version of the Library.
 156.113 +
 156.114 +  The precise terms and conditions for copying, distribution and
 156.115 +modification follow.  Pay close attention to the difference between a
 156.116 +"work based on the library" and a "work that uses the library".  The
 156.117 +former contains code derived from the library, whereas the latter must
 156.118 +be combined with the library in order to run.
 156.119 +
 156.120 +                  GNU LESSER GENERAL PUBLIC LICENSE
 156.121 +   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 156.122 +
 156.123 +  0. This License Agreement applies to any software library or other
 156.124 +program which contains a notice placed by the copyright holder or
 156.125 +other authorized party saying it may be distributed under the terms of
 156.126 +this Lesser General Public License (also called "this License").
 156.127 +Each licensee is addressed as "you".
 156.128 +
 156.129 +  A "library" means a collection of software functions and/or data
 156.130 +prepared so as to be conveniently linked with application programs
 156.131 +(which use some of those functions and data) to form executables.
 156.132 +
 156.133 +  The "Library", below, refers to any such software library or work
 156.134 +which has been distributed under these terms.  A "work based on the
 156.135 +Library" means either the Library or any derivative work under
 156.136 +copyright law: that is to say, a work containing the Library or a
 156.137 +portion of it, either verbatim or with modifications and/or translated
 156.138 +straightforwardly into another language.  (Hereinafter, translation is
 156.139 +included without limitation in the term "modification".)
 156.140 +
 156.141 +  "Source code" for a work means the preferred form of the work for
 156.142 +making modifications to it.  For a library, complete source code means
 156.143 +all the source code for all modules it contains, plus any associated
 156.144 +interface definition files, plus the scripts used to control
 156.145 +compilation and installation of the library.
 156.146 +
 156.147 +  Activities other than copying, distribution and modification are not
 156.148 +covered by this License; they are outside its scope.  The act of
 156.149 +running a program using the Library is not restricted, and output from
 156.150 +such a program is covered only if its contents constitute a work based
 156.151 +on the Library (independent of the use of the Library in a tool for
 156.152 +writing it).  Whether that is true depends on what the Library does
 156.153 +and what the program that uses the Library does.
 156.154 +
 156.155 +  1. You may copy and distribute verbatim copies of the Library's
 156.156 +complete source code as you receive it, in any medium, provided that
 156.157 +you conspicuously and appropriately publish on each copy an
 156.158 +appropriate copyright notice and disclaimer of warranty; keep intact
 156.159 +all the notices that refer to this License and to the absence of any
 156.160 +warranty; and distribute a copy of this License along with the
 156.161 +Library.
 156.162 +
 156.163 +  You may charge a fee for the physical act of transferring a copy,
 156.164 +and you may at your option offer warranty protection in exchange for a
 156.165 +fee.
 156.166 +
 156.167 +  2. You may modify your copy or copies of the Library or any portion
 156.168 +of it, thus forming a work based on the Library, and copy and
 156.169 +distribute such modifications or work under the terms of Section 1
 156.170 +above, provided that you also meet all of these conditions:
 156.171 +
 156.172 +    a) The modified work must itself be a software library.
 156.173 +
 156.174 +    b) You must cause the files modified to carry prominent notices
 156.175 +    stating that you changed the files and the date of any change.
 156.176 +
 156.177 +    c) You must cause the whole of the work to be licensed at no
 156.178 +    charge to all third parties under the terms of this License.
 156.179 +
 156.180 +    d) If a facility in the modified Library refers to a function or a
 156.181 +    table of data to be supplied by an application program that uses
 156.182 +    the facility, other than as an argument passed when the facility
 156.183 +    is invoked, then you must make a good faith effort to ensure that,
 156.184 +    in the event an application does not supply such function or
 156.185 +    table, the facility still operates, and performs whatever part of
 156.186 +    its purpose remains meaningful.
 156.187 +
 156.188 +    (For example, a function in a library to compute square roots has
 156.189 +    a purpose that is entirely well-defined independent of the
 156.190 +    application.  Therefore, Subsection 2d requires that any
 156.191 +    application-supplied function or table used by this function must
 156.192 +    be optional: if the application does not supply it, the square
 156.193 +    root function must still compute square roots.)
 156.194 +
 156.195 +These requirements apply to the modified work as a whole.  If
 156.196 +identifiable sections of that work are not derived from the Library,
 156.197 +and can be reasonably considered independent and separate works in
 156.198 +themselves, then this License, and its terms, do not apply to those
 156.199 +sections when you distribute them as separate works.  But when you
 156.200 +distribute the same sections as part of a whole which is a work based
 156.201 +on the Library, the distribution of the whole must be on the terms of
 156.202 +this License, whose permissions for other licensees extend to the
 156.203 +entire whole, and thus to each and every part regardless of who wrote
 156.204 +it.
 156.205 +
 156.206 +Thus, it is not the intent of this section to claim rights or contest
 156.207 +your rights to work written entirely by you; rather, the intent is to
 156.208 +exercise the right to control the distribution of derivative or
 156.209 +collective works based on the Library.
 156.210 +
 156.211 +In addition, mere aggregation of another work not based on the Library
 156.212 +with the Library (or with a work based on the Library) on a volume of
 156.213 +a storage or distribution medium does not bring the other work under
 156.214 +the scope of this License.
 156.215 +
 156.216 +  3. You may opt to apply the terms of the ordinary GNU General Public
 156.217 +License instead of this License to a given copy of the Library.  To do
 156.218 +this, you must alter all the notices that refer to this License, so
 156.219 +that they refer to the ordinary GNU General Public License, version 2,
 156.220 +instead of to this License.  (If a newer version than version 2 of the
 156.221 +ordinary GNU General Public License has appeared, then you can specify
 156.222 +that version instead if you wish.)  Do not make any other change in
 156.223 +these notices.
 156.224 +
 156.225 +  Once this change is made in a given copy, it is irreversible for
 156.226 +that copy, so the ordinary GNU General Public License applies to all
 156.227 +subsequent copies and derivative works made from that copy.
 156.228 +
 156.229 +  This option is useful when you wish to copy part of the code of
 156.230 +the Library into a program that is not a library.
 156.231 +
 156.232 +  4. You may copy and distribute the Library (or a portion or
 156.233 +derivative of it, under Section 2) in object code or executable form
 156.234 +under the terms of Sections 1 and 2 above provided that you accompany
 156.235 +it with the complete corresponding machine-readable source code, which
 156.236 +must be distributed under the terms of Sections 1 and 2 above on a
 156.237 +medium customarily used for software interchange.
 156.238 +
 156.239 +  If distribution of object code is made by offering access to copy
 156.240 +from a designated place, then offering equivalent access to copy the
 156.241 +source code from the same place satisfies the requirement to
 156.242 +distribute the source code, even though third parties are not
 156.243 +compelled to copy the source along with the object code.
 156.244 +
 156.245 +  5. A program that contains no derivative of any portion of the
 156.246 +Library, but is designed to work with the Library by being compiled or
 156.247 +linked with it, is called a "work that uses the Library".  Such a
 156.248 +work, in isolation, is not a derivative work of the Library, and
 156.249 +therefore falls outside the scope of this License.
 156.250 +
 156.251 +  However, linking a "work that uses the Library" with the Library
 156.252 +creates an executable that is a derivative of the Library (because it
 156.253 +contains portions of the Library), rather than a "work that uses the
 156.254 +library".  The executable is therefore covered by this License.
 156.255 +Section 6 states terms for distribution of such executables.
 156.256 +
 156.257 +  When a "work that uses the Library" uses material from a header file
 156.258 +that is part of the Library, the object code for the work may be a
 156.259 +derivative work of the Library even though the source code is not.
 156.260 +Whether this is true is especially significant if the work can be
 156.261 +linked without the Library, or if the work is itself a library.  The
 156.262 +threshold for this to be true is not precisely defined by law.
 156.263 +
 156.264 +  If such an object file uses only numerical parameters, data
 156.265 +structure layouts and accessors, and small macros and small inline
 156.266 +functions (ten lines or less in length), then the use of the object
 156.267 +file is unrestricted, regardless of whether it is legally a derivative
 156.268 +work.  (Executables containing this object code plus portions of the
 156.269 +Library will still fall under Section 6.)
 156.270 +
 156.271 +  Otherwise, if the work is a derivative of the Library, you may
 156.272 +distribute the object code for the work under the terms of Section 6.
 156.273 +Any executables containing that work also fall under Section 6,
 156.274 +whether or not they are linked directly with the Library itself.
 156.275 +
 156.276 +  6. As an exception to the Sections above, you may also combine or
 156.277 +link a "work that uses the Library" with the Library to produce a
 156.278 +work containing portions of the Library, and distribute that work
 156.279 +under terms of your choice, provided that the terms permit
 156.280 +modification of the work for the customer's own use and reverse
 156.281 +engineering for debugging such modifications.
 156.282 +
 156.283 +  You must give prominent notice with each copy of the work that the
 156.284 +Library is used in it and that the Library and its use are covered by
 156.285 +this License.  You must supply a copy of this License.  If the work
 156.286 +during execution displays copyright notices, you must include the
 156.287 +copyright notice for the Library among them, as well as a reference
 156.288 +directing the user to the copy of this License.  Also, you must do one
 156.289 +of these things:
 156.290 +
 156.291 +    a) Accompany the work with the complete corresponding
 156.292 +    machine-readable source code for the Library including whatever
 156.293 +    changes were used in the work (which must be distributed under
 156.294 +    Sections 1 and 2 above); and, if the work is an executable linked
 156.295 +    with the Library, with the complete machine-readable "work that
 156.296 +    uses the Library", as object code and/or source code, so that the
 156.297 +    user can modify the Library and then relink to produce a modified
 156.298 +    executable containing the modified Library.  (It is understood
 156.299 +    that the user who changes the contents of definitions files in the
 156.300 +    Library will not necessarily be able to recompile the application
 156.301 +    to use the modified definitions.)
 156.302 +
 156.303 +    b) Use a suitable shared library mechanism for linking with the
 156.304 +    Library.  A suitable mechanism is one that (1) uses at run time a
 156.305 +    copy of the library already present on the user's computer system,
 156.306 +    rather than copying library functions into the executable, and (2)
 156.307 +    will operate properly with a modified version of the library, if
 156.308 +    the user installs one, as long as the modified version is
 156.309 +    interface-compatible with the version that the work was made with.
 156.310 +
 156.311 +    c) Accompany the work with a written offer, valid for at least
 156.312 +    three years, to give the same user the materials specified in
 156.313 +    Subsection 6a, above, for a charge no more than the cost of
 156.314 +    performing this distribution.
 156.315 +
 156.316 +    d) If distribution of the work is made by offering access to copy
 156.317 +    from a designated place, offer equivalent access to copy the above
 156.318 +    specified materials from the same place.
 156.319 +
 156.320 +    e) Verify that the user has already received a copy of these
 156.321 +    materials or that you have already sent this user a copy.
 156.322 +
 156.323 +  For an executable, the required form of the "work that uses the
 156.324 +Library" must include any data and utility programs needed for
 156.325 +reproducing the executable from it.  However, as a special exception,
 156.326 +the materials to be distributed need not include anything that is
 156.327 +normally distributed (in either source or binary form) with the major
 156.328 +components (compiler, kernel, and so on) of the operating system on
 156.329 +which the executable runs, unless that component itself accompanies
 156.330 +the executable.
 156.331 +
 156.332 +  It may happen that this requirement contradicts the license
 156.333 +restrictions of other proprietary libraries that do not normally
 156.334 +accompany the operating system.  Such a contradiction means you cannot
 156.335 +use both them and the Library together in an executable that you
 156.336 +distribute.
 156.337 +
 156.338 +  7. You may place library facilities that are a work based on the
 156.339 +Library side-by-side in a single library together with other library
 156.340 +facilities not covered by this License, and distribute such a combined
 156.341 +library, provided that the separate distribution of the work based on
 156.342 +the Library and of the other library facilities is otherwise
 156.343 +permitted, and provided that you do these two things:
 156.344 +
 156.345 +    a) Accompany the combined library with a copy of the same work
 156.346 +    based on the Library, uncombined with any other library
 156.347 +    facilities.  This must be distributed under the terms of the
 156.348 +    Sections above.
 156.349 +
 156.350 +    b) Give prominent notice with the combined library of the fact
 156.351 +    that part of it is a work based on the Library, and explaining
 156.352 +    where to find the accompanying uncombined form of the same work.
 156.353 +
 156.354 +  8. You may not copy, modify, sublicense, link with, or distribute
 156.355 +the Library except as expressly provided under this License.  Any
 156.356 +attempt otherwise to copy, modify, sublicense, link with, or
 156.357 +distribute the Library is void, and will automatically terminate your
 156.358 +rights under this License.  However, parties who have received copies,
 156.359 +or rights, from you under this License will not have their licenses
 156.360 +terminated so long as such parties remain in full compliance.
 156.361 +
 156.362 +  9. You are not required to accept this License, since you have not
 156.363 +signed it.  However, nothing else grants you permission to modify or
 156.364 +distribute the Library or its derivative works.  These actions are
 156.365 +prohibited by law if you do not accept this License.  Therefore, by
 156.366 +modifying or distributing the Library (or any work based on the
 156.367 +Library), you indicate your acceptance of this License to do so, and
 156.368 +all its terms and conditions for copying, distributing or modifying
 156.369 +the Library or works based on it.
 156.370 +
 156.371 +  10. Each time you redistribute the Library (or any work based on the
 156.372 +Library), the recipient automatically receives a license from the
 156.373 +original licensor to copy, distribute, link with or modify the Library
 156.374 +subject to these terms and conditions.  You may not impose any further
 156.375 +restrictions on the recipients' exercise of the rights granted herein.
 156.376 +You are not responsible for enforcing compliance by third parties with
 156.377 +this License.
 156.378 +
 156.379 +  11. If, as a consequence of a court judgment or allegation of patent
 156.380 +infringement or for any other reason (not limited to patent issues),
 156.381 +conditions are imposed on you (whether by court order, agreement or
 156.382 +otherwise) that contradict the conditions of this License, they do not
 156.383 +excuse you from the conditions of this License.  If you cannot
 156.384 +distribute so as to satisfy simultaneously your obligations under this
 156.385 +License and any other pertinent obligations, then as a consequence you
 156.386 +may not distribute the Library at all.  For example, if a patent
 156.387 +license would not permit royalty-free redistribution of the Library by
 156.388 +all those who receive copies directly or indirectly through you, then
 156.389 +the only way you could satisfy both it and this License would be to
 156.390 +refrain entirely from distribution of the Library.
 156.391 +
 156.392 +If any portion of this section is held invalid or unenforceable under
 156.393 +any particular circumstance, the balance of the section is intended to
 156.394 +apply, and the section as a whole is intended to apply in other
 156.395 +circumstances.
 156.396 +
 156.397 +It is not the purpose of this section to induce you to infringe any
 156.398 +patents or other property right claims or to contest validity of any
 156.399 +such claims; this section has the sole purpose of protecting the
 156.400 +integrity of the free software distribution system which is
 156.401 +implemented by public license practices.  Many people have made
 156.402 +generous contributions to the wide range of software distributed
 156.403 +through that system in reliance on consistent application of that
 156.404 +system; it is up to the author/donor to decide if he or she is willing
 156.405 +to distribute software through any other system and a licensee cannot
 156.406 +impose that choice.
 156.407 +
 156.408 +This section is intended to make thoroughly clear what is believed to
 156.409 +be a consequence of the rest of this License.
 156.410 +
 156.411 +  12. If the distribution and/or use of the Library is restricted in
 156.412 +certain countries either by patents or by copyrighted interfaces, the
 156.413 +original copyright holder who places the Library under this License
 156.414 +may add an explicit geographical distribution limitation excluding those
 156.415 +countries, so that distribution is permitted only in or among
 156.416 +countries not thus excluded.  In such case, this License incorporates
 156.417 +the limitation as if written in the body of this License.
 156.418 +
 156.419 +  13. The Free Software Foundation may publish revised and/or new
 156.420 +versions of the Lesser General Public License from time to time.
 156.421 +Such new versions will be similar in spirit to the present version,
 156.422 +but may differ in detail to address new problems or concerns.
 156.423 +
 156.424 +Each version is given a distinguishing version number.  If the Library
 156.425 +specifies a version number of this License which applies to it and
 156.426 +"any later version", you have the option of following the terms and
 156.427 +conditions either of that version or of any later version published by
 156.428 +the Free Software Foundation.  If the Library does not specify a
 156.429 +license version number, you may choose any version ever published by
 156.430 +the Free Software Foundation.
 156.431 +
 156.432 +  14. If you wish to incorporate parts of the Library into other free
 156.433 +programs whose distribution conditions are incompatible with these,
 156.434 +write to the author to ask for permission.  For software which is
 156.435 +copyrighted by the Free Software Foundation, write to the Free
 156.436 +Software Foundation; we sometimes make exceptions for this.  Our
 156.437 +decision will be guided by the two goals of preserving the free status
 156.438 +of all derivatives of our free software and of promoting the sharing
 156.439 +and reuse of software generally.
 156.440 +
 156.441 +                            NO WARRANTY
 156.442 +
 156.443 +  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
 156.444 +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
 156.445 +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
 156.446 +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
 156.447 +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
 156.448 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 156.449 +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
 156.450 +LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
 156.451 +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 156.452 +
 156.453 +  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
 156.454 +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
 156.455 +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
 156.456 +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
 156.457 +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
 156.458 +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
 156.459 +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
 156.460 +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
 156.461 +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 156.462 +DAMAGES.
 156.463 +
 156.464 +                     END OF TERMS AND CONDITIONS
 156.465 +
 156.466 +           How to Apply These Terms to Your New Libraries
 156.467 +
 156.468 +  If you develop a new library, and you want it to be of the greatest
 156.469 +possible use to the public, we recommend making it free software that
 156.470 +everyone can redistribute and change.  You can do so by permitting
 156.471 +redistribution under these terms (or, alternatively, under the terms
 156.472 +of the ordinary General Public License).
 156.473 +
 156.474 +  To apply these terms, attach the following notices to the library.
 156.475 +It is safest to attach them to the start of each source file to most
 156.476 +effectively convey the exclusion of warranty; and each file should
 156.477 +have at least the "copyright" line and a pointer to where the full
 156.478 +notice is found.
 156.479 +
 156.480 +
 156.481 +    <one line to give the library's name and a brief idea of what it does.>
 156.482 +    Copyright (C) <year>  <name of author>
 156.483 +
 156.484 +    This library is free software; you can redistribute it and/or
 156.485 +    modify it under the terms of the GNU Lesser General Public
 156.486 +    License as published by the Free Software Foundation; either
 156.487 +    version 2.1 of the License, or (at your option) any later version.
 156.488 +
 156.489 +    This library is distributed in the hope that it will be useful,
 156.490 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
 156.491 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 156.492 +    Lesser General Public License for more details.
 156.493 +
 156.494 +    You should have received a copy of the GNU Lesser General Public
 156.495 +    License along with this library; if not, write to the Free Software
 156.496 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 156.497 +
 156.498 +Also add information on how to contact you by electronic and paper mail.
 156.499 +
 156.500 +You should also get your employer (if you work as a programmer) or
 156.501 +your school, if any, to sign a "copyright disclaimer" for the library,
 156.502 +if necessary.  Here is a sample; alter the names:
 156.503 +
 156.504 +  Yoyodyne, Inc., hereby disclaims all copyright interest in the
 156.505 +  library `Frob' (a library for tweaking knobs) written by James
 156.506 +  Random Hacker.
 156.507 +
 156.508 +  <signature of Ty Coon>, 1 April 1990
 156.509 +  Ty Coon, President of Vice
 156.510 +
 156.511 +That's all there is to it!
 156.512 +
 156.513 +
   157.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   157.2 +++ b/tools/xenstat/libxenstat/Makefile	Tue Aug 23 08:40:58 2005 +0000
   157.3 @@ -0,0 +1,142 @@
   157.4 +# libxenstat: statistics-collection library for Xen
   157.5 +# Copyright (C) International Business Machines Corp., 2005
   157.6 +# Author: Josh Triplett <josht@us.ibm.com>
   157.7 +# 
   157.8 +# This library is free software; you can redistribute it and/or
   157.9 +# modify it under the terms of the GNU Lesser General Public
  157.10 +# License as published by the Free Software Foundation; either
  157.11 +# version 2.1 of the License, or (at your option) any later version.
  157.12 +# 
  157.13 +# This library is distributed in the hope that it will be useful,
  157.14 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  157.15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  157.16 +# Lesser General Public License for more details.
  157.17 +
  157.18 +XEN_ROOT=../../..
  157.19 +include $(XEN_ROOT)/tools/Rules.mk
  157.20 +LINUX_ROOT := $(XEN_ROOT)/linux-2.6-xen-sparse
  157.21 +
  157.22 +INSTALL         = install
  157.23 +INSTALL_PROG    = $(INSTALL) -m0755 -D
  157.24 +INSTALL_DATA    = $(INSTALL) -m0644 -D
  157.25 +
  157.26 +prefix=/usr
  157.27 +includedir=$(prefix)/include
  157.28 +libdir=$(prefix)/lib
  157.29 +
  157.30 +LDCONFIG=ldconfig
  157.31 +MAKE_LINK=ln -sf
  157.32 +
  157.33 +MAJOR=0
  157.34 +MINOR=0
  157.35 +
  157.36 +LIB=src/libxenstat.a
  157.37 +SHLIB=src/libxenstat.so.$(MAJOR).$(MINOR)
  157.38 +SHLIB_LINKS=src/libxenstat.so.$(MAJOR) src/libxenstat.so
  157.39 +OBJECTS=src/xenstat.o src/xen-interface.o
  157.40 +SONAME_FLAGS=-Wl,-soname -Wl,libxenstat.so.$(MAJOR)
  157.41 +
  157.42 +WARN_FLAGS=-Wall -Werror
  157.43 +
  157.44 +CFLAGS+=-Isrc
  157.45 +CFLAGS+=-I$(XEN_ROOT)/xen/include/public
  157.46 +CFLAGS+=-I$(LINUX_ROOT)/include/asm-xen/linux-public/
  157.47 +LDFLAGS+=-Lsrc
  157.48 +
  157.49 +all: $(LIB)
  157.50 +
  157.51 +$(LIB): $(OBJECTS)
  157.52 +	$(AR) rc $@ $^
  157.53 +	$(RANLIB) $@
  157.54 +
  157.55 +$(SHLIB): $(OBJECTS)
  157.56 +	$(CC) $(LDFLAGS) $(SONAME_FLAGS) -shared -o $@ $(OBJECTS)
  157.57 +
  157.58 +src/xenstat.o: src/xenstat.c src/xenstat.h src/xen-interface.h
  157.59 +	$(CC) $(CFLAGS) $(WARN_FLAGS) -c -o $@ $<
  157.60 +
  157.61 +src/xen-interface.o: src/xen-interface.c src/xen-interface.h
  157.62 +	$(CC) $(CFLAGS) $(WARN_FLAGS) -c -o $@ $<
  157.63 +
  157.64 +src/libxenstat.so.$(MAJOR): $(LIB)
  157.65 +	$(MAKE_LINK) $(<F) $@
  157.66 +
  157.67 +src/libxenstat.so: src/libxenstat.so.$(MAJOR)
  157.68 +	$(MAKE_LINK) $(<F) $@
  157.69 +
  157.70 +install: all
  157.71 +#install: all
  157.72 +#	$(INSTALL_DATA) src/xenstat.h $(DESTDIR)$(includedir)/xenstat.h
  157.73 +#	$(INSTALL_PROG) $(LIB) $(DESTDIR)$(libdir)/libxenstat.a
  157.74 +#	$(INSTALL_PROG) $(SHLIB) \
  157.75 +#	                $(DESTDIR)$(libdir)/libxenstat.so.$(MAJOR).$(MINOR)
  157.76 +#	$(MAKE_LINK) libxenstat.so.$(MAJOR).$(MINOR) \
  157.77 +#	             $(DESTDIR)$(libdir)/libxenstat.so.$(MAJOR)
  157.78 +#	$(MAKE_LINK) libxenstat.so.$(MAJOR) \
  157.79 +#	             $(DESTDIR)$(libdir)/libxenstat.so
  157.80 +#	-$(LDCONFIG)
  157.81 +
  157.82 +PYLIB=bindings/swig/python/_xenstat.so
  157.83 +PYMOD=bindings/swig/python/xenstat.py
  157.84 +PYSRC=bindings/swig/python/_xenstat.c
  157.85 +PERLLIB=bindings/swig/perl/xenstat.so
  157.86 +PERLMOD=bindings/swig/perl/xenstat.pm
  157.87 +PERLSRC=bindings/swig/perl/xenstat.c
  157.88 +BINDINGS=$(PYLIB) $(PYMOD) $(PERLLIB) $(PERLMOD)
  157.89 +BINDINGSRC=$(PYSRC) $(PERLSRC)
  157.90 +
  157.91 +# The all-bindings target builds all the language bindings
  157.92 +all-bindings: perl-bindings python-bindings
  157.93 +
  157.94 +# The install-bindings target installs all the language bindings
  157.95 +install-bindings: install-perl-bindings install-python-bindings
  157.96 +
  157.97 +$(BINDINGS): $(SHLIB) $(SHLIB_LINKS) src/xenstat.h
  157.98 +
  157.99 +SWIG_FLAGS=-module xenstat -Isrc
 157.100 +
 157.101 +# Python bindings
 157.102 +PYTHON_VERSION=2.3
 157.103 +PYTHON_FLAGS=-I/usr/include/python$(PYTHON_VERSION) -lpython$(PYTHON_VERSION)
 157.104 +$(PYSRC) $(PYMOD): bindings/swig/xenstat.i
 157.105 +	swig -python $(SWIG_FLAGS) -outdir $(@D) -o $(PYSRC) $<
 157.106 +
 157.107 +$(PYLIB): $(PYSRC)
 157.108 +	$(CC) $(CFLAGS) $(LDFLAGS) $(PYTHON_FLAGS) -shared -lxenstat -o $@ $<
 157.109 +
 157.110 +python-bindings: $(PYLIB) $(PYMOD)
 157.111 +
 157.112 +pythonlibdir=$(prefix)/lib/python$(PYTHON_VERSION)/site-packages
 157.113 +install-python-bindings: $(PYLIB) $(PYMOD)
 157.114 +	$(INSTALL_PROG) $(PYLIB) $(DESTDIR)$(pythonlibdir)/_xenstat.so
 157.115 +	$(INSTALL_PROG) $(PYMOD) $(DESTDIR)$(pythonlibdir)/xenstat.py
 157.116 +
 157.117 +ifeq ($(XENSTAT_PYTHON_BINDINGS),y)
 157.118 +all: python-bindings
 157.119 +install: install-python-bindings
 157.120 +endif
 157.121 +
 157.122 +# Perl bindings
 157.123 +PERL_FLAGS=`perl -MConfig -e 'print "$$Config{ccflags} -I$$Config{archlib}/CORE";'`
 157.124 +$(PERLSRC) $(PERLMOD): bindings/swig/xenstat.i
 157.125 +	swig -perl $(SWIG_FLAGS) -outdir $(@D) -o $(PERLSRC) $<
 157.126 +
 157.127 +$(PERLLIB): $(PERLSRC)
 157.128 +	$(CC) $(CFLAGS) $(LDFLAGS) $(PERL_FLAGS) -shared -lxenstat -o $@ $<
 157.129 +
 157.130 +perl-bindings: $(PERLLIB) $(PERLMOD)
 157.131 +
 157.132 +perllibdir=$(prefix)/lib/perl5
 157.133 +perlmoddir=$(prefix)/share/perl5
 157.134 +install-perl-bindings: $(PERLLIB) $(PERLMOD)
 157.135 +	$(INSTALL_PROG) $(PERLLIB) $(DESTDIR)$(perllibdir)/xenstat.so
 157.136 +	$(INSTALL_PROG) $(PERLMOD) $(DESTDIR)$(perlmoddir)/xenstat.pm
 157.137 +
 157.138 +ifeq ($(XENSTAT_PERL_BINDINGS),y)
 157.139 +all: perl-bindings
 157.140 +install: install-perl-bindings
 157.141 +endif
 157.142 +
 157.143 +clean:
 157.144 +	rm -f $(LIB) $(SHLIB) $(SHLIB_LINKS) $(OBJECTS) \
 157.145 +	      $(BINDINGS) $(BINDINGSRC)
   158.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   158.2 +++ b/tools/xenstat/libxenstat/bindings/swig/perl/.empty	Tue Aug 23 08:40:58 2005 +0000
   158.3 @@ -0,0 +1,1 @@
   158.4 +This directory is empty; this file is included to prevent version control systems from removing the directory.
   159.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   159.2 +++ b/tools/xenstat/libxenstat/bindings/swig/python/.empty	Tue Aug 23 08:40:58 2005 +0000
   159.3 @@ -0,0 +1,1 @@
   159.4 +This directory is empty; this file is included to prevent version control systems from removing the directory.
   160.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   160.2 +++ b/tools/xenstat/libxenstat/bindings/swig/xenstat.i	Tue Aug 23 08:40:58 2005 +0000
   160.3 @@ -0,0 +1,8 @@
   160.4 +%module xenstat_swig
   160.5 +%{
   160.6 +/* Includes the header in the wrapper code */
   160.7 +#include "xenstat.h"
   160.8 +%}
   160.9 +
  160.10 +/* Parse the header file to generate wrappers */
  160.11 +%include "xenstat.h"
   161.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   161.2 +++ b/tools/xenstat/libxenstat/src/xen-interface.c	Tue Aug 23 08:40:58 2005 +0000
   161.3 @@ -0,0 +1,144 @@
   161.4 +/* xen-interface.c
   161.5 + *
   161.6 + * Copyright (C) International Business Machines Corp., 2005
   161.7 + * Authors: Josh Triplett <josht@us.ibm.com>
   161.8 + *          Judy Fischbach <jfisch@us.ibm.com>
   161.9 + *
  161.10 + * This library is free software; you can redistribute it and/or
  161.11 + * modify it under the terms of the GNU Lesser General Public
  161.12 + * License as published by the Free Software Foundation; either
  161.13 + * version 2.1 of the License, or (at your option) any later version.
  161.14 + *
  161.15 + * This library is distributed in the hope that it will be useful,
  161.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  161.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
  161.18 + * Lesser General Public License for more details.
  161.19 + */
  161.20 +
  161.21 +#include "xen-interface.h"
  161.22 +#include <fcntl.h>
  161.23 +#include <sys/ioctl.h>
  161.24 +#include <sys/mman.h>
  161.25 +#include <stdio.h>
  161.26 +#include <stdlib.h>
  161.27 +#include <unistd.h>
  161.28 +#include "privcmd.h"
  161.29 +#include "xen.h"
  161.30 +
  161.31 +struct xi_handle {
  161.32 +	int fd;
  161.33 +};
  161.34 +
  161.35 +/* Initialize for xen-interface.  Returns a handle to be used with subsequent
  161.36 + * calls to the xen-interface functions or NULL if an error occurs. */
  161.37 +xi_handle *xi_init()
  161.38 +{
  161.39 +	xi_handle *handle;
  161.40 +
  161.41 +	handle = (xi_handle *)calloc(1, sizeof(xi_handle));
  161.42 +	if (handle == NULL)
  161.43 +		return NULL;
  161.44 +
  161.45 +	handle->fd = open("/proc/xen/privcmd", O_RDWR);
  161.46 +	if (handle->fd < 0) {
  161.47 +		perror("Couldn't open /proc/xen/privcmd");
  161.48 +		free(handle);
  161.49 +		return NULL;
  161.50 +	}
  161.51 +
  161.52 +	return handle;
  161.53 +}
  161.54 +
  161.55 +/* Release the handle to libxc, free resources, etc. */
  161.56 +void xi_uninit(xi_handle *handle)
  161.57 +{
  161.58 +	close (handle->fd);
  161.59 +	free (handle);
  161.60 +}
  161.61 +
  161.62 +/* Make Xen hypervisor call */
  161.63 +int xi_make_dom0_op(xi_handle *handle, dom0_op_t *op, int opcode)
  161.64 +{
  161.65 +	privcmd_hypercall_t privcmd;
  161.66 +	int ret = 0;
  161.67 +
  161.68 +	/* set up for doing hypercall */
  161.69 +	privcmd.op = __HYPERVISOR_dom0_op;
  161.70 +	privcmd.arg[0] = (unsigned long)op;
  161.71 +	op->cmd = opcode;
  161.72 +	op->interface_version = DOM0_INTERFACE_VERSION;
  161.73 +
  161.74 +	if (mlock( &privcmd, sizeof(privcmd_hypercall_t)) < 0) {
  161.75 +		perror("Failed to mlock privcmd structure");
  161.76 +		return -1;
  161.77 +	}
  161.78 +
  161.79 +	if (mlock( op, sizeof(dom0_op_t)) < 0) {
  161.80 +		perror("Failed to mlock dom0_op structure");
  161.81 +		munlock( &privcmd, sizeof(privcmd_hypercall_t));
  161.82 +		return -1;
  161.83 +	}
  161.84 +
  161.85 +	if (ioctl( handle->fd, IOCTL_PRIVCMD_HYPERCALL, &privcmd) < 0) {
  161.86 +		perror("Hypercall failed");
  161.87 +		ret = -1;
  161.88 +	}
  161.89 +
  161.90 +	munlock( &privcmd, sizeof(privcmd_hypercall_t));
  161.91 +	munlock( op, sizeof(dom0_op_t));
  161.92 +
  161.93 +	return ret;
  161.94 +}
  161.95 +
  161.96 +/* Obtain domain data from dom0 */
  161.97 +int xi_get_physinfo(xi_handle *handle, dom0_physinfo_t *physinfo)
  161.98 +{
  161.99 +	dom0_op_t op;
 161.100 +
 161.101 +	if (xi_make_dom0_op(handle, &op, DOM0_PHYSINFO) < 0) {
 161.102 +		perror("DOM0_PHYSINFO Hypercall failed");
 161.103 +		return -1;
 161.104 +	}
 161.105 +
 161.106 +	*physinfo = op.u.physinfo;
 161.107 +	return 0;
 161.108 +}
 161.109 +
 161.110 +/* Obtain domain data from dom0 */
 161.111 +int xi_get_domaininfolist(xi_handle *handle, dom0_getdomaininfo_t *info,
 161.112 +                          unsigned int first_domain, unsigned int max_domains)
 161.113 +{
 161.114 +	dom0_op_t op;
 161.115 +	op.u.getdomaininfolist.first_domain = first_domain;
 161.116 +	op.u.getdomaininfolist.max_domains = max_domains;
 161.117 +	op.u.getdomaininfolist.buffer = info;
 161.118 +
 161.119 +	if (mlock( info, max_domains * sizeof(dom0_getdomaininfo_t)) < 0) {
 161.120 +		perror("Failed to mlock domaininfo array");
 161.121 +		return -1;
 161.122 +	}
 161.123 +
 161.124 +	if (xi_make_dom0_op(handle, &op, DOM0_GETDOMAININFOLIST) < 0) {
 161.125 +		perror("DOM0_GETDOMAININFOLIST Hypercall failed");
 161.126 +		return -1;
 161.127 +	}
 161.128 +
 161.129 +	return op.u.getdomaininfolist.num_domains;
 161.130 +}
 161.131 +
 161.132 +/* Returns cpu usage data from dom0 */
 161.133 +long long xi_get_vcpu_usage(xi_handle *handle, unsigned int domain,
 161.134 +                            unsigned int vcpu)
 161.135 +{
 161.136 +	dom0_op_t op;
 161.137 +	op.u.getvcpucontext.domain = domain;
 161.138 +	op.u.getvcpucontext.vcpu = vcpu;
 161.139 +	op.u.getvcpucontext.ctxt = NULL;
 161.140 +
 161.141 +	if (xi_make_dom0_op(handle, &op, DOM0_GETVCPUCONTEXT) < 0) {
 161.142 +		perror("DOM0_GETVCPUCONTEXT Hypercall failed");
 161.143 +		return -1;
 161.144 +	}
 161.145 +
 161.146 +	return op.u.getvcpucontext.cpu_time;
 161.147 +}
   162.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   162.2 +++ b/tools/xenstat/libxenstat/src/xen-interface.h	Tue Aug 23 08:40:58 2005 +0000
   162.3 @@ -0,0 +1,49 @@
   162.4 +/* xen-interface.h
   162.5 + *
   162.6 + * Copyright (C) International Business Machines Corp., 2005
   162.7 + * Authors: Josh Triplett <josht@us.ibm.com>
   162.8 + *          Judy Fischbach <jfisch@us.ibm.com>
   162.9 + *
  162.10 + * This library is free software; you can redistribute it and/or
  162.11 + * modify it under the terms of the GNU Lesser General Public
  162.12 + * License as published by the Free Software Foundation; either
  162.13 + * version 2.1 of the License, or (at your option) any later version.
  162.14 + *
  162.15 + * This library is distributed in the hope that it will be useful,
  162.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  162.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  162.18 + * Lesser General Public License for more details.
  162.19 + */
  162.20 +
  162.21 +#include <stdint.h>
  162.22 +
  162.23 +typedef int8_t   s8;
  162.24 +typedef int16_t  s16;
  162.25 +typedef int32_t  s32;
  162.26 +typedef int64_t  s64;
  162.27 +typedef uint8_t  u8;
  162.28 +typedef uint16_t u16;
  162.29 +typedef uint32_t u32;
  162.30 +typedef uint64_t u64;
  162.31 +
  162.32 +#include "dom0_ops.h"
  162.33 +
  162.34 +/* Opaque handles */
  162.35 +typedef struct xi_handle xi_handle;
  162.36 +
  162.37 +/* Initialize for xen-interface.  Returns a handle to be used with subsequent
  162.38 + * calls to the xen-interface functions or NULL if an error occurs. */
  162.39 +xi_handle *xi_init();
  162.40 +
  162.41 +/* Release the handle to libxc, free resources, etc. */
  162.42 +void xi_uninit(xi_handle *handle);
  162.43 +
  162.44 +/* Obtain physinfo data from dom0 */
  162.45 +int xi_get_physinfo(xi_handle *, dom0_physinfo_t *);
  162.46 +
  162.47 +/* Obtain domain data from dom0 */
  162.48 +int xi_get_domaininfolist(xi_handle *, dom0_getdomaininfo_t *, unsigned int,
  162.49 +                          unsigned int);
  162.50 +
  162.51 +/* Returns cpu usage data from dom0 */
  162.52 +long long xi_get_vcpu_usage(xi_handle *, unsigned int, unsigned int);
   163.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   163.2 +++ b/tools/xenstat/libxenstat/src/xenstat.c	Tue Aug 23 08:40:58 2005 +0000
   163.3 @@ -0,0 +1,620 @@
   163.4 +/* libxenstat: statistics-collection library for Xen
   163.5 + * Copyright (C) International Business Machines Corp., 2005
   163.6 + * Authors: Josh Triplett <josht@us.ibm.com>
   163.7 + *          Judy Fischbach <jfisch@us.ibm.com>
   163.8 + *          David Hendricks <dhendrix@us.ibm.com>
   163.9 + *
  163.10 + * This library is free software; you can redistribute it and/or
  163.11 + * modify it under the terms of the GNU Lesser General Public
  163.12 + * License as published by the Free Software Foundation; either
  163.13 + * version 2.1 of the License, or (at your option) any later version.
  163.14 + *
  163.15 + * This library is distributed in the hope that it will be useful,
  163.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  163.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  163.18 + * Lesser General Public License for more details.
  163.19 + */
  163.20 +
  163.21 +#include <limits.h>
  163.22 +#include <stdlib.h>
  163.23 +#include <stdio.h>
  163.24 +#include <string.h>
  163.25 +#include <unistd.h>
  163.26 +#include <xen-interface.h>
  163.27 +#include "xenstat.h"
  163.28 +
  163.29 +/*
  163.30 + * Types
  163.31 + */
  163.32 +struct xenstat_handle {
  163.33 +	xi_handle *xihandle;
  163.34 +	int page_size;
  163.35 +	FILE *procnetdev;
  163.36 +};
  163.37 +
  163.38 +struct xenstat_node {
  163.39 +	unsigned int flags;
  163.40 +	unsigned long long cpu_hz;
  163.41 +	unsigned int num_cpus;
  163.42 +	unsigned long long tot_mem;
  163.43 +	unsigned long long free_mem;
  163.44 +	unsigned int num_domains;
  163.45 +	xenstat_domain *domains;	/* Array of length num_domains */
  163.46 +};
  163.47 +
  163.48 +struct xenstat_domain {
  163.49 +	unsigned int id;
  163.50 +	unsigned int state;
  163.51 +	unsigned long long cpu_ns;
  163.52 +	unsigned int num_vcpus;
  163.53 +	xenstat_vcpu *vcpus;	/* Array of length num_vcpus */
  163.54 +	unsigned long long cur_mem;	/* Current memory reservation */
  163.55 +	unsigned long long max_mem;	/* Total memory allowed */
  163.56 +	unsigned int ssid;
  163.57 +	unsigned int num_networks;
  163.58 +	xenstat_network *networks;	/* Array of length num_networks */
  163.59 +};
  163.60 +
  163.61 +struct xenstat_vcpu {
  163.62 +	unsigned long long ns;
  163.63 +};
  163.64 +
  163.65 +struct xenstat_network {
  163.66 +	unsigned int id;
  163.67 +	/* Received */
  163.68 +	unsigned long long rbytes;
  163.69 +	unsigned long long rpackets;
  163.70 +	unsigned long long rerrs;
  163.71 +	unsigned long long rdrop;
  163.72 +	/* Transmitted */
  163.73 +	unsigned long long tbytes;
  163.74 +	unsigned long long tpackets;
  163.75 +	unsigned long long terrs;
  163.76 +	unsigned long long tdrop;
  163.77 +};
  163.78 +
  163.79 +/*
  163.80 + * Data-collection types
  163.81 + */
  163.82 +/* Called to collect the information for the node and all the domains on
  163.83 + * it. When called, the domain information has already been collected. */
  163.84 +typedef int (*xenstat_collect_func)(xenstat_handle * handle,
  163.85 +				    xenstat_node * node);
  163.86 +/* Called to free the information collected by the collect function.  The free
  163.87 + * function will only be called on a xenstat_node if that node includes
  163.88 + * information collected by the corresponding collector. */
  163.89 +typedef void (*xenstat_free_func)(xenstat_node * node);
  163.90 +/* Called to free any information stored in the handle.  Note the lack of a
  163.91 + * matching init function; the collect functions should initialize on first
  163.92 + * use.  Also, the uninit function must handle the case that the collector has
  163.93 + * never been initialized. */
  163.94 +typedef void (*xenstat_uninit_func)(xenstat_handle * handle);
  163.95 +typedef struct xenstat_collector {
  163.96 +	unsigned int flag;
  163.97 +	xenstat_collect_func collect;
  163.98 +	xenstat_free_func free;
  163.99 +	xenstat_uninit_func uninit;
 163.100 +} xenstat_collector;
 163.101 +
 163.102 +static int  xenstat_collect_vcpus(xenstat_handle * handle,
 163.103 +				  xenstat_node * node);
 163.104 +static int  xenstat_collect_networks(xenstat_handle * handle,
 163.105 +				    xenstat_node * node);
 163.106 +static void xenstat_free_vcpus(xenstat_node * node);
 163.107 +static void xenstat_free_networks(xenstat_node * node);
 163.108 +static void xenstat_uninit_vcpus(xenstat_handle * handle);
 163.109 +static void xenstat_uninit_networks(xenstat_handle * handle);
 163.110 +
 163.111 +static xenstat_collector collectors[] = {
 163.112 +	{ XENSTAT_VCPU, xenstat_collect_vcpus,
 163.113 +	  xenstat_free_vcpus, xenstat_uninit_vcpus },
 163.114 +	{ XENSTAT_NETWORK, xenstat_collect_networks,
 163.115 +	  xenstat_free_networks, xenstat_uninit_networks }
 163.116 +};
 163.117 +
 163.118 +#define NUM_COLLECTORS (sizeof(collectors)/sizeof(xenstat_collector))
 163.119 +
 163.120 +/*
 163.121 + * libxenstat API
 163.122 + */
 163.123 +xenstat_handle *xenstat_init()
 163.124 +{
 163.125 +	xenstat_handle *handle;
 163.126 +
 163.127 +	handle = (xenstat_handle *) calloc(1, sizeof(xenstat_handle));
 163.128 +	if (handle == NULL)
 163.129 +		return NULL;
 163.130 +
 163.131 +#if defined(PAGESIZE)
 163.132 +	handle->page_size = PAGESIZE;
 163.133 +#elif defined(PAGE_SIZE)
 163.134 +	handle->page_size = PAGE_SIZE;
 163.135 +#else
 163.136 +	handle->page_size = sysconf(_SC_PAGE_SIZE);
 163.137 +	if (handle->page_size < 0) {
 163.138 +		perror("Failed to retrieve page size.");
 163.139 +		free(handle);
 163.140 +		return NULL;
 163.141 +	}
 163.142 +#endif
 163.143 +
 163.144 +	handle->xihandle = xi_init();
 163.145 +	if (handle->xihandle == NULL) {
 163.146 +		perror("xi_init");
 163.147 +		free(handle);
 163.148 +		return NULL;
 163.149 +	}
 163.150 +
 163.151 +	return handle;
 163.152 +}
 163.153 +
 163.154 +void xenstat_uninit(xenstat_handle * handle)
 163.155 +{
 163.156 +	unsigned int i;
 163.157 +	if (handle) {
 163.158 +		for (i = 0; i < NUM_COLLECTORS; i++)
 163.159 +			collectors[i].uninit(handle);
 163.160 +		xi_uninit(handle->xihandle);
 163.161 +		free(handle);
 163.162 +	}
 163.163 +}
 163.164 +
 163.165 +xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags)
 163.166 +{
 163.167 +#define DOMAIN_CHUNK_SIZE 256
 163.168 +	xenstat_node *node;
 163.169 +	dom0_physinfo_t physinfo;
 163.170 +	dom0_getdomaininfo_t domaininfo[DOMAIN_CHUNK_SIZE];
 163.171 +	unsigned int num_domains, new_domains;
 163.172 +	unsigned int i;
 163.173 +
 163.174 +	/* Create the node */
 163.175 +	node = (xenstat_node *) calloc(1, sizeof(xenstat_node));
 163.176 +	if (node == NULL)
 163.177 +		return NULL;
 163.178 +
 163.179 +	/* Get information about the physical system */
 163.180 +	if (xi_get_physinfo(handle->xihandle, &physinfo) < 0) {
 163.181 +		free(node);
 163.182 +		return NULL;
 163.183 +	}
 163.184 +
 163.185 +	node->cpu_hz = ((unsigned long long)physinfo.cpu_khz) * 1000ULL;
 163.186 +	node->num_cpus =
 163.187 +	    (physinfo.threads_per_core * physinfo.cores_per_socket *
 163.188 +	     physinfo.sockets_per_node * physinfo.nr_nodes);
 163.189 +	node->tot_mem = ((unsigned long long)physinfo.total_pages)
 163.190 +	    * handle->page_size;
 163.191 +	node->free_mem = ((unsigned long long)physinfo.free_pages)
 163.192 +	    * handle->page_size;
 163.193 +
 163.194 +	/* malloc(0) is not portable, so allocate a single domain.  This will
 163.195 +	 * be resized below. */
 163.196 +	node->domains = malloc(sizeof(xenstat_domain));
 163.197 +	if (node->domains == NULL) {
 163.198 +		free(node);
 163.199 +		return NULL;
 163.200 +	}
 163.201 +
 163.202 +	num_domains = 0;
 163.203 +	do {
 163.204 +		xenstat_domain *domain;
 163.205 +
 163.206 +		new_domains = xi_get_domaininfolist(handle->xihandle,
 163.207 +		                                    domaininfo, num_domains,
 163.208 +		                                    DOMAIN_CHUNK_SIZE);
 163.209 +
 163.210 +		node->domains = realloc(node->domains,
 163.211 +					(num_domains + new_domains)
 163.212 +					* sizeof(xenstat_domain));
 163.213 +		if (node->domains == NULL) {
 163.214 +			free(node);
 163.215 +			return NULL;
 163.216 +		}
 163.217 +
 163.218 +		domain = node->domains + num_domains;
 163.219 +
 163.220 +		for (i = 0; i < new_domains; i++) {
 163.221 +			/* Fill in domain using domaininfo[i] */
 163.222 +			domain->id = domaininfo[i].domain;
 163.223 +			domain->state = domaininfo[i].flags;
 163.224 +			domain->cpu_ns = domaininfo[i].cpu_time;
 163.225 +			domain->num_vcpus = domaininfo[i].n_vcpu;
 163.226 +			domain->vcpus = NULL;
 163.227 +			domain->cur_mem =
 163.228 +			    ((unsigned long long)domaininfo[i].tot_pages)
 163.229 +			    * handle->page_size;
 163.230 +			domain->max_mem =
 163.231 +			    domaininfo[i].max_pages == UINT_MAX
 163.232 +			    ? (unsigned long long)-1
 163.233 +			    : (unsigned long long)(domaininfo[i].max_pages
 163.234 +						   * handle->page_size);
 163.235 +			domain->ssid = domaininfo[i].ssidref;
 163.236 +			domain->num_networks = 0;
 163.237 +			domain->networks = NULL;
 163.238 +
 163.239 +			domain++;
 163.240 +		}
 163.241 +		num_domains += new_domains;
 163.242 +	} while (new_domains == DOMAIN_CHUNK_SIZE);
 163.243 +	node->num_domains = num_domains;
 163.244 +
 163.245 +	/* Run all the extra data collectors requested */
 163.246 +	node->flags = 0;
 163.247 +	for (i = 0; i < NUM_COLLECTORS; i++) {
 163.248 +		if ((flags & collectors[i].flag) == collectors[i].flag) {
 163.249 +			node->flags |= collectors[i].flag;
 163.250 +			if(collectors[i].collect(handle, node) == 0) {
 163.251 +				xenstat_free_node(node);
 163.252 +				return NULL;
 163.253 +                        }
 163.254 +                }
 163.255 +	}
 163.256 +
 163.257 +	return node;
 163.258 +}
 163.259 +
 163.260 +void xenstat_free_node(xenstat_node * node)
 163.261 +{
 163.262 +	int i;
 163.263 +
 163.264 +	if (node) {
 163.265 +		if (node->domains) {
 163.266 +			for (i = 0; i < NUM_COLLECTORS; i++)
 163.267 +				if((node->flags & collectors[i].flag)
 163.268 +				   == collectors[i].flag)
 163.269 +					collectors[i].free(node);
 163.270 +			free(node->domains);
 163.271 +		}
 163.272 +		free(node);
 163.273 +	}
 163.274 +}
 163.275 +
 163.276 +xenstat_domain *xenstat_node_domain(xenstat_node * node, unsigned int domid)
 163.277 +{
 163.278 +	unsigned int i;
 163.279 +
 163.280 +	/* FIXME: binary search */
 163.281 +	/* Find the appropriate domain entry in the node struct. */
 163.282 +	for (i = 0; i < node->num_domains; i++) {
 163.283 +		if (node->domains[i].id == domid)
 163.284 +			return &(node->domains[i]);
 163.285 +	}
 163.286 +	return NULL;
 163.287 +}
 163.288 +
 163.289 +xenstat_domain *xenstat_node_domain_by_index(xenstat_node * node,
 163.290 +					     unsigned int index)
 163.291 +{
 163.292 +	if (0 <= index && index < node->num_domains)
 163.293 +		return &(node->domains[index]);
 163.294 +	return NULL;
 163.295 +}
 163.296 +
 163.297 +unsigned long long xenstat_node_tot_mem(xenstat_node * node)
 163.298 +{
 163.299 +	return node->tot_mem;
 163.300 +}
 163.301 +
 163.302 +unsigned long long xenstat_node_free_mem(xenstat_node * node)
 163.303 +{
 163.304 +	return node->free_mem;
 163.305 +}
 163.306 +
 163.307 +unsigned int xenstat_node_num_domains(xenstat_node * node)
 163.308 +{
 163.309 +	return node->num_domains;
 163.310 +}
 163.311 +
 163.312 +unsigned int xenstat_node_num_cpus(xenstat_node * node)
 163.313 +{
 163.314 +	return node->num_cpus;
 163.315 +}
 163.316 +
 163.317 +/* Get information about the CPU speed */
 163.318 +unsigned long long xenstat_node_cpu_hz(xenstat_node * node)
 163.319 +{
 163.320 +	return node->cpu_hz;
 163.321 +}
 163.322 +
 163.323 +/* Get the domain ID for this domain */
 163.324 +unsigned xenstat_domain_id(xenstat_domain * domain)
 163.325 +{
 163.326 +	return domain->id;
 163.327 +}
 163.328 +
 163.329 +/* Get information about how much CPU time has been used */
 163.330 +unsigned long long xenstat_domain_cpu_ns(xenstat_domain * domain)
 163.331 +{
 163.332 +	return domain->cpu_ns;
 163.333 +}
 163.334 +
 163.335 +/* Find the number of VCPUs allocated to a domain */
 163.336 +unsigned int xenstat_domain_num_vcpus(xenstat_domain * domain)
 163.337 +{
 163.338 +	return domain->num_vcpus;
 163.339 +}
 163.340 +
 163.341 +xenstat_vcpu *xenstat_domain_vcpu(xenstat_domain * domain, unsigned int vcpu)
 163.342 +{
 163.343 +	if (0 <= vcpu && vcpu < domain->num_vcpus)
 163.344 +		return &(domain->vcpus[vcpu]);
 163.345 +	return NULL;
 163.346 +}
 163.347 +
 163.348 +/* Find the current memory reservation for this domain */
 163.349 +unsigned long long xenstat_domain_cur_mem(xenstat_domain * domain)
 163.350 +{
 163.351 +	return domain->cur_mem;
 163.352 +}
 163.353 +
 163.354 +/* Find the maximum memory reservation for this domain */
 163.355 +unsigned long long xenstat_domain_max_mem(xenstat_domain * domain)
 163.356 +{
 163.357 +	return domain->max_mem;
 163.358 +}
 163.359 +
 163.360 +/* Find the domain's SSID */
 163.361 +unsigned int xenstat_domain_ssid(xenstat_domain * domain)
 163.362 +{
 163.363 +	return domain->ssid;
 163.364 +}
 163.365 +
 163.366 +/* Get domain states */
 163.367 +unsigned int xenstat_domain_dying(xenstat_domain * domain)
 163.368 +{
 163.369 +	return (domain->state & DOMFLAGS_DYING) == DOMFLAGS_DYING;
 163.370 +}
 163.371 +
 163.372 +unsigned int xenstat_domain_crashed(xenstat_domain * domain)
 163.373 +{
 163.374 +	return ((domain->state & DOMFLAGS_SHUTDOWN) == DOMFLAGS_SHUTDOWN)
 163.375 +	    && (((domain->state >> DOMFLAGS_SHUTDOWNSHIFT)
 163.376 +		 & DOMFLAGS_SHUTDOWNMASK) == SHUTDOWN_crash);
 163.377 +}
 163.378 +
 163.379 +unsigned int xenstat_domain_shutdown(xenstat_domain * domain)
 163.380 +{
 163.381 +	return ((domain->state & DOMFLAGS_SHUTDOWN) == DOMFLAGS_SHUTDOWN)
 163.382 +	    && (((domain->state >> DOMFLAGS_SHUTDOWNSHIFT)
 163.383 +		 & DOMFLAGS_SHUTDOWNMASK) != SHUTDOWN_crash);
 163.384 +}
 163.385 +
 163.386 +unsigned int xenstat_domain_paused(xenstat_domain * domain)
 163.387 +{
 163.388 +	return (domain->state & DOMFLAGS_PAUSED) == DOMFLAGS_PAUSED;
 163.389 +}
 163.390 +
 163.391 +unsigned int xenstat_domain_blocked(xenstat_domain * domain)
 163.392 +{
 163.393 +	return (domain->state & DOMFLAGS_BLOCKED) == DOMFLAGS_BLOCKED;
 163.394 +}
 163.395 +
 163.396 +unsigned int xenstat_domain_running(xenstat_domain * domain)
 163.397 +{
 163.398 +	return (domain->state & DOMFLAGS_RUNNING) == DOMFLAGS_RUNNING;
 163.399 +}
 163.400 +
 163.401 +/* Get the number of networks for a given domain */
 163.402 +unsigned int xenstat_domain_num_networks(xenstat_domain * domain)
 163.403 +{
 163.404 +	return domain->num_networks;
 163.405 +}
 163.406 +
 163.407 +/* Get the network handle to obtain network stats */
 163.408 +xenstat_network *xenstat_domain_network(xenstat_domain * domain,
 163.409 +					unsigned int network)
 163.410 +{
 163.411 +	if (domain->networks && 0 <= network && network < domain->num_networks)
 163.412 +		return &(domain->networks[network]);
 163.413 +	return NULL;
 163.414 +}
 163.415 +
 163.416 +/*
 163.417 + * VCPU functions
 163.418 + */
 163.419 +/* Collect information about VCPUs */
 163.420 +static int xenstat_collect_vcpus(xenstat_handle * handle, xenstat_node * node)
 163.421 +{
 163.422 +	unsigned int i, vcpu;
 163.423 +	/* Fill in VCPU information */
 163.424 +	for (i = 0; i < node->num_domains; i++) {
 163.425 +		node->domains[i].vcpus = malloc(node->domains[i].num_vcpus
 163.426 +						* sizeof(xenstat_vcpu));
 163.427 +		if (node->domains[i].vcpus == NULL)
 163.428 +			return 0;
 163.429 +
 163.430 +		for (vcpu = 0; vcpu < node->domains[i].num_vcpus; vcpu++) {
 163.431 +			/* FIXME: need to be using a more efficient mechanism*/
 163.432 +			long long vcpu_time;
 163.433 +			vcpu_time =
 163.434 +			    xi_get_vcpu_usage(handle->xihandle,
 163.435 +					      node->domains[i].id,
 163.436 +					      vcpu);
 163.437 +			if (vcpu_time < 0)
 163.438 +				return 0;
 163.439 +			node->domains[i].vcpus[vcpu].ns = vcpu_time;
 163.440 +		}
 163.441 +	}
 163.442 +	return 1;
 163.443 +}
 163.444 +
 163.445 +/* Free VCPU information */
 163.446 +static void xenstat_free_vcpus(xenstat_node * node)
 163.447 +{
 163.448 +	unsigned int i;
 163.449 +	for (i = 0; i < node->num_domains; i++)
 163.450 +		free(node->domains[i].vcpus);
 163.451 +}
 163.452 +
 163.453 +/* Free VCPU information in handle - nothing to do */
 163.454 +static void xenstat_uninit_vcpus(xenstat_handle * handle)
 163.455 +{
 163.456 +}
 163.457 +
 163.458 +/* Get VCPU usage */
 163.459 +unsigned long long xenstat_vcpu_ns(xenstat_vcpu * vcpu)
 163.460 +{
 163.461 +	return vcpu->ns;
 163.462 +}
 163.463 +
 163.464 +/*
 163.465 + * Network functions
 163.466 + */
 163.467 +
 163.468 +/* Expected format of /proc/net/dev */
 163.469 +static const char PROCNETDEV_HEADER[] =
 163.470 +    "Inter-|   Receive                                                |"
 163.471 +    "  Transmit\n"
 163.472 +    " face |bytes    packets errs drop fifo frame compressed multicast|"
 163.473 +    "bytes    packets errs drop fifo colls carrier compressed\n";
 163.474 +
 163.475 +/* Collect information about networks */
 163.476 +static int xenstat_collect_networks(xenstat_handle * handle,
 163.477 +				    xenstat_node * node)
 163.478 +{
 163.479 +	/* Open and validate /proc/net/dev if we haven't already */
 163.480 +	if (handle->procnetdev == NULL) {
 163.481 +		char header[sizeof(PROCNETDEV_HEADER)];
 163.482 +		handle->procnetdev = fopen("/proc/net/dev", "r");
 163.483 +		if (handle->procnetdev == NULL) {
 163.484 +			perror("Error opening /proc/net/dev");
 163.485 +			return 1;
 163.486 +		}
 163.487 +
 163.488 +		/* Validate the format of /proc/net/dev */
 163.489 +		if (fread(header, sizeof(PROCNETDEV_HEADER) - 1, 1,
 163.490 +			  handle->procnetdev) != 1) {
 163.491 +			perror("Error reading /proc/net/dev header");
 163.492 +			return 1;
 163.493 +		}
 163.494 +		header[sizeof(PROCNETDEV_HEADER) - 1] = '\0';
 163.495 +		if (strcmp(header, PROCNETDEV_HEADER) != 0) {
 163.496 +			fprintf(stderr,
 163.497 +				"Unexpected /proc/net/dev format\n");
 163.498 +			return 1;
 163.499 +		}
 163.500 +	}
 163.501 +
 163.502 +	/* Fill in networks */
 163.503 +	/* FIXME: optimize this */
 163.504 +	fseek(handle->procnetdev, sizeof(PROCNETDEV_HEADER) - 1, SEEK_SET);
 163.505 +	while (1) {
 163.506 +		xenstat_domain *domain;
 163.507 +		xenstat_network net;
 163.508 +		unsigned int domid;
 163.509 +		int ret = fscanf(handle->procnetdev,
 163.510 +				 "vif%u.%u:%llu%llu%llu%llu%*u%*u%*u%*u"
 163.511 +				 "%llu%llu%llu%llu%*u%*u%*u%*u",
 163.512 +				 &domid, &net.id,
 163.513 +				 &net.tbytes, &net.tpackets, &net.terrs,
 163.514 +				 &net.tdrop,
 163.515 +				 &net.rbytes, &net.rpackets, &net.rerrs,
 163.516 +				 &net.rdrop);
 163.517 +		if (ret == EOF)
 163.518 +			break;
 163.519 +		if (ret != 10) {
 163.520 +			unsigned int c;
 163.521 +			do {
 163.522 +				c = fgetc(handle->procnetdev);
 163.523 +			} while (c != '\n' && c != EOF);
 163.524 +			if (c == EOF)
 163.525 +				break;
 163.526 +			continue;
 163.527 +		}
 163.528 +
 163.529 +		/* FIXME: this does a search for the domid */
 163.530 +		domain = xenstat_node_domain(node, domid);
 163.531 +		if (domain == NULL) {
 163.532 +			fprintf(stderr,
 163.533 +				"Found interface vif%u.%u but domain %u"
 163.534 +				" does not exist.\n", domid, net.id,
 163.535 +				domid);
 163.536 +			continue;
 163.537 +		}
 163.538 +		if (domain->networks == NULL) {
 163.539 +			domain->num_networks = 1;
 163.540 +			domain->networks = malloc(sizeof(xenstat_network));
 163.541 +		} else {
 163.542 +			domain->num_networks++;
 163.543 +			domain->networks =
 163.544 +			    realloc(domain->networks,
 163.545 +				    domain->num_networks *
 163.546 +				    sizeof(xenstat_network));
 163.547 +		}
 163.548 +		if (domain->networks == NULL)
 163.549 +			return 1;
 163.550 +		domain->networks[domain->num_networks - 1] = net;
 163.551 +	}
 163.552 +
 163.553 +	return 1;
 163.554 +}
 163.555 +
 163.556 +/* Free network information */
 163.557 +static void xenstat_free_networks(xenstat_node * node)
 163.558 +{
 163.559 +	unsigned int i;
 163.560 +	for (i = 0; i < node->num_domains; i++)
 163.561 +		free(node->domains[i].networks);
 163.562 +}
 163.563 +
 163.564 +/* Free network information in handle */
 163.565 +static void xenstat_uninit_networks(xenstat_handle * handle)
 163.566 +{
 163.567 +	if(handle->procnetdev)
 163.568 +		fclose(handle->procnetdev);
 163.569 +}
 163.570 +
 163.571 +/* Get the network ID */
 163.572 +unsigned int xenstat_network_id(xenstat_network * network)
 163.573 +{
 163.574 +	return network->id;
 163.575 +}
 163.576 +
 163.577 +/* Get the number of receive bytes */
 163.578 +unsigned long long xenstat_network_rbytes(xenstat_network * network)
 163.579 +{
 163.580 +	return network->rbytes;
 163.581 +}
 163.582 +
 163.583 +/* Get the number of receive packets */
 163.584 +unsigned long long xenstat_network_rpackets(xenstat_network * network)
 163.585 +{
 163.586 +	return network->rpackets;
 163.587 +}
 163.588 +
 163.589 +/* Get the number of receive errors */
 163.590 +unsigned long long xenstat_network_rerrs(xenstat_network * network)
 163.591 +{
 163.592 +	return network->rerrs;
 163.593 +}
 163.594 +
 163.595 +/* Get the number of receive drops */
 163.596 +unsigned long long xenstat_network_rdrop(xenstat_network * network)
 163.597 +{
 163.598 +	return network->rdrop;
 163.599 +}
 163.600 +
 163.601 +/* Get the number of transmit bytes */
 163.602 +unsigned long long xenstat_network_tbytes(xenstat_network * network)
 163.603 +{
 163.604 +	return network->tbytes;
 163.605 +}
 163.606 +
 163.607 +/* Get the number of transmit packets */
 163.608 +unsigned long long xenstat_network_tpackets(xenstat_network * network)
 163.609 +{
 163.610 +	return network->tpackets;
 163.611 +}
 163.612 +
 163.613 +/* Get the number of transmit errors */
 163.614 +unsigned long long xenstat_network_terrs(xenstat_network * network)
 163.615 +{
 163.616 +	return network->terrs;
 163.617 +}
 163.618 +
 163.619 +/* Get the number of transmit dropped packets */
 163.620 +unsigned long long xenstat_network_tdrop(xenstat_network * network)
 163.621 +{
 163.622 +	return network->tdrop;
 163.623 +}
   164.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   164.2 +++ b/tools/xenstat/libxenstat/src/xenstat.h	Tue Aug 23 08:40:58 2005 +0000
   164.3 @@ -0,0 +1,148 @@
   164.4 +/* libxenstat: statistics-collection library for Xen
   164.5 + * Copyright (C) International Business Machines Corp., 2005
   164.6 + * Authors: Josh Triplett <josht@us.ibm.com>
   164.7 + *          Judy Fischbach <jfisch@us.ibm.com>
   164.8 + *          David Hendricks <dhendrix@us.ibm.com>
   164.9 + *
  164.10 + * This library is free software; you can redistribute it and/or
  164.11 + * modify it under the terms of the GNU Lesser General Public
  164.12 + * License as published by the Free Software Foundation; either
  164.13 + * version 2.1 of the License, or (at your option) any later version.
  164.14 + *
  164.15 + * This library is distributed in the hope that it will be useful,
  164.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  164.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  164.18 + * Lesser General Public License for more details.
  164.19 + */
  164.20 +
  164.21 +/* libxenstat API */
  164.22 +
  164.23 +/* Opaque handles */
  164.24 +typedef struct xenstat_handle xenstat_handle;
  164.25 +typedef struct xenstat_domain xenstat_domain;
  164.26 +typedef struct xenstat_node xenstat_node;
  164.27 +typedef struct xenstat_vcpu xenstat_vcpu;
  164.28 +typedef struct xenstat_network xenstat_network;
  164.29 +
  164.30 +/* Initialize the xenstat library.  Returns a handle to be used with
  164.31 + * subsequent calls to the xenstat library, or NULL if an error occurs. */
  164.32 +xenstat_handle *xenstat_init();
  164.33 +
  164.34 +/* Release the handle to libxc, free resources, etc. */
  164.35 +void xenstat_uninit(xenstat_handle * handle);
  164.36 +
  164.37 +/* Get all available information about a node */
  164.38 +#define XENSTAT_VCPU 0x1
  164.39 +#define XENSTAT_NETWORK 0x2
  164.40 +#define XENSTAT_ALL (XENSTAT_VCPU|XENSTAT_NETWORK)
  164.41 +xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags);
  164.42 +
  164.43 +/* Free the information */
  164.44 +void xenstat_free_node(xenstat_node * node);
  164.45 +
  164.46 +/*
  164.47 + * Node functions - extract information from a xenstat_node
  164.48 + */
  164.49 +
  164.50 +/* Get information about the domain with the given domain ID */
  164.51 +xenstat_domain *xenstat_node_domain(xenstat_node * node,
  164.52 +				    unsigned int domid);
  164.53 +
  164.54 +/* Get the domain with the given index; used to loop over all domains. */
  164.55 +xenstat_domain *xenstat_node_domain_by_index(xenstat_node * node,
  164.56 +					     unsigned index);
  164.57 +
  164.58 +/* Get amount of total memory on a node */
  164.59 +unsigned long long xenstat_node_tot_mem(xenstat_node * node);
  164.60 +
  164.61 +/* Get amount of free memory on a node */
  164.62 +unsigned long long xenstat_node_free_mem(xenstat_node * node);
  164.63 +
  164.64 +/* Find the number of domains existing on a node */
  164.65 +unsigned int xenstat_node_num_domains(xenstat_node * node);
  164.66 +
  164.67 +/* Find the number of CPUs existing on a node */
  164.68 +unsigned int xenstat_node_num_cpus(xenstat_node * node);
  164.69 +
  164.70 +/* Get information about the CPU speed */
  164.71 +unsigned long long xenstat_node_cpu_hz(xenstat_node * node);
  164.72 +
  164.73 +/*
  164.74 + * Domain functions - extract information from a xenstat_domain
  164.75 + */
  164.76 +
  164.77 +/* Get the domain ID for this domain */
  164.78 +unsigned xenstat_domain_id(xenstat_domain * domain);
  164.79 +
  164.80 +/* Get information about how much CPU time has been used */
  164.81 +unsigned long long xenstat_domain_cpu_ns(xenstat_domain * domain);
  164.82 +
  164.83 +/* Find the number of VCPUs allocated to a domain */
  164.84 +unsigned int xenstat_domain_num_vcpus(xenstat_domain * domain);
  164.85 +
  164.86 +/* Get the VCPU handle to obtain VCPU stats */
  164.87 +xenstat_vcpu *xenstat_domain_vcpu(xenstat_domain * domain,
  164.88 +				  unsigned int vcpu);
  164.89 +
  164.90 +/* Find the current memory reservation for this domain */
  164.91 +unsigned long long xenstat_domain_cur_mem(xenstat_domain * domain);
  164.92 +
  164.93 +/* Find the maximum memory reservation for this domain */
  164.94 +unsigned long long xenstat_domain_max_mem(xenstat_domain * domain);
  164.95 +
  164.96 +/* Find the domain's SSID */
  164.97 +unsigned int xenstat_domain_ssid(xenstat_domain * domain);
  164.98 +
  164.99 +/* Get domain states */
 164.100 +unsigned int xenstat_domain_dying(xenstat_domain * domain);
 164.101 +unsigned int xenstat_domain_crashed(xenstat_domain * domain);
 164.102 +unsigned int xenstat_domain_shutdown(xenstat_domain * domain);
 164.103 +unsigned int xenstat_domain_paused(xenstat_domain * domain);
 164.104 +unsigned int xenstat_domain_blocked(xenstat_domain * domain);
 164.105 +unsigned int xenstat_domain_running(xenstat_domain * domain);
 164.106 +
 164.107 +/* Get the number of networks for a given domain */
 164.108 +unsigned int xenstat_domain_num_networks(xenstat_domain *);
 164.109 +
 164.110 +/* Get the network handle to obtain network stats */
 164.111 +xenstat_network *xenstat_domain_network(xenstat_domain * domain,
 164.112 +					unsigned int network);
 164.113 +
 164.114 +/*
 164.115 + * VCPU functions - extract information from a xenstat_vcpu
 164.116 + */
 164.117 +
 164.118 +/* Get VCPU usage */
 164.119 +unsigned long long xenstat_vcpu_ns(xenstat_vcpu * vcpu);
 164.120 +
 164.121 +
 164.122 +/*
 164.123 + * Network functions - extract information from a xenstat_network
 164.124 + */
 164.125 +
 164.126 +/* Get the ID for this network */
 164.127 +unsigned int xenstat_network_id(xenstat_network * network);
 164.128 +
 164.129 +/* Get the number of receive bytes for this network */
 164.130 +unsigned long long xenstat_network_rbytes(xenstat_network * network);
 164.131 +
 164.132 +/* Get the number of receive packets for this network */
 164.133 +unsigned long long xenstat_network_rpackets(xenstat_network * network);
 164.134 +
 164.135 +/* Get the number of receive errors for this network */
 164.136 +unsigned long long xenstat_network_rerrs(xenstat_network * network);
 164.137 +
 164.138 +/* Get the number of receive drops for this network */
 164.139 +unsigned long long xenstat_network_rdrop(xenstat_network * network);
 164.140 +
 164.141 +/* Get the number of transmit bytes for this network */
 164.142 +unsigned long long xenstat_network_tbytes(xenstat_network * network);
 164.143 +
 164.144 +/* Get the number of transmit packets for this network */
 164.145 +unsigned long long xenstat_network_tpackets(xenstat_network * network);
 164.146 +
 164.147 +/* Get the number of transmit errors for this network */
 164.148 +unsigned long long xenstat_network_terrs(xenstat_network * network);
 164.149 +
 164.150 +/* Get the number of transmit drops for this network */
 164.151 +unsigned long long xenstat_network_tdrop(xenstat_network * network);
   165.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   165.2 +++ b/tools/xenstat/xentop/Makefile	Tue Aug 23 08:40:58 2005 +0000
   165.3 @@ -0,0 +1,44 @@
   165.4 +# Copyright (C) International Business Machines Corp., 2005
   165.5 +# Author: Josh Triplett <josht@us.ibm.com>
   165.6 +# 
   165.7 +# This program is free software; you can redistribute it and/or modify
   165.8 +# it under the terms of the GNU General Public License as published by
   165.9 +# the Free Software Foundation; under version 2 of the License.
  165.10 +#
  165.11 +# This program is distributed in the hope that it will be useful,
  165.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  165.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  165.14 +# GNU General Public License for more details.
  165.15 +
  165.16 +XEN_ROOT=../../..
  165.17 +include $(XEN_ROOT)/tools/Rules.mk
  165.18 +
  165.19 +ifneq ($(XENSTAT_XENTOP),y)
  165.20 +all install xentop:
  165.21 +else
  165.22 +
  165.23 +INSTALL         = install
  165.24 +INSTALL_PROG    = $(INSTALL) -m0755 -D
  165.25 +INSTALL_DATA    = $(INSTALL) -m0644 -D
  165.26 +
  165.27 +prefix=/usr
  165.28 +mandir=$(prefix)/share/man
  165.29 +man1dir=$(mandir)/man1
  165.30 +sbindir=$(prefix)/sbin
  165.31 +
  165.32 +CFLAGS += -DGCC_PRINTF -Wall -Werror -I$(XEN_LIBXENSTAT)
  165.33 +LDFLAGS += -L$(XEN_LIBXENSTAT)
  165.34 +LDLIBS += -lxenstat -lcurses
  165.35 +
  165.36 +all: xentop
  165.37 +
  165.38 +xentop: xentop.o
  165.39 +
  165.40 +install: xentop xentop.1
  165.41 +	$(INSTALL_PROG) xentop $(DESTDIR)$(sbindir)/xentop
  165.42 +	$(INSTALL_DATA) xentop.1 $(DESTDIR)$(man1dir)/xentop.1
  165.43 +
  165.44 +endif
  165.45 +
  165.46 +clean:
  165.47 +	rm -f xentop xentop.o
   166.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   166.2 +++ b/tools/xenstat/xentop/TODO	Tue Aug 23 08:40:58 2005 +0000
   166.3 @@ -0,0 +1,34 @@
   166.4 +Display error messages on the help line after bad input at a prompt.
   166.5 +Fractional delay times
   166.6 +Use prompting to search for domains
   166.7 +Better line editing?
   166.8 +
   166.9 +* Make CPU in % more accurate
  166.10 +* Domain total network TX % and RX %
  166.11 +
  166.12 +Like Top, f feature, field select of domain columns, toggle the display of
  166.13 +field by typing the letter associated with field, if displayed it shows in
  166.14 +bold and the letter is Capitalized along with a leading asterisk for the
  166.15 +field, if not selected for display letter is lowercase, no leading asterisk
  166.16 +and field is not bolded.
  166.17 +
  166.18 +Like Top, ordering of domain columns, o feature Capital letter shifts left,
  166.19 +lowercase letter shifts right?
  166.20 +
  166.21 +Color
  166.22 +Full management: pause, destroy, create domains
  166.23 +
  166.24 +Add support for Virtual Block Devices (vbd)
  166.25 +
  166.26 +To think about:
  166.27 +Support for one than one node display (distributed monitoring 
  166.28 +from any node of all other nodes in a cluster)
  166.29 +Bottom line option (Switch node, Search node [tab completion?])
  166.30 +
  166.31 +Capture/Logging of resource information generated during a time interval.
  166.32 +-b batch mode dump snapshots to standard output (used with -n)
  166.33 +-n number of iterations to dump to standard output (unlimited if not specified)
  166.34 +-d monitor DomIDs as -dD1,-dD2 or -dD1,D2...
  166.35 +   Monitor only domains with specified domain IDs
  166.36 +-m monitor nodeIDs as -mN1,-mN2 or -mN1,N2...
  166.37 +   Monitor only domains with specified node IDs
   167.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   167.2 +++ b/tools/xenstat/xentop/xentop.1	Tue Aug 23 08:40:58 2005 +0000
   167.3 @@ -0,0 +1,88 @@
   167.4 +.\" Copyright (C) International Business Machines  Corp., 2005
   167.5 +.\" Author: Josh Triplett <josht@us.ibm.com>
   167.6 +.\"
   167.7 +.\" This program is free software; you can redistribute it and/or modify
   167.8 +.\" it under the terms of the GNU General Public License as published by
   167.9 +.\" the Free Software Foundation; under version 2 of the License.
  167.10 +.\"
  167.11 +.\" This program is distributed in the hope that it will be useful,
  167.12 +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
  167.13 +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  167.14 +.\" GNU General Public License for more details.
  167.15 +.\"
  167.16 +.\" You should have received a copy of the GNU General Public License
  167.17 +.\" along with this program; if not, write to the Free Software
  167.18 +.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  167.19 +.TH xentop 1 "August 2005"
  167.20 +.SH NAME
  167.21 +\fBxentop\fR \- displays real-time information about a Xen system and domains
  167.22 +
  167.23 +.SH SYNOPSIS
  167.24 +.B xentop
  167.25 +[\fB\-h\fR]
  167.26 +[\fB\-V\fR]
  167.27 +[\fB\-d\fRSECONDS]
  167.28 +[\fB\-n\fR]
  167.29 +[\fB\-r\fR]
  167.30 +[\fB\-v\fR]
  167.31 +
  167.32 +.SH DESCRIPTION
  167.33 +\fBxentop\fR displays information about the Xen system and domains, in a
  167.34 +continually-updating manner.  Command-line options and interactive commands
  167.35 +can change the detail and format of the information displayed by \fBxentop\fR.
  167.36 +
  167.37 +.SH OPTIONS
  167.38 +.TP
  167.39 +\fB\-h\fR, \fB\-\-help\fR
  167.40 +display help and exit
  167.41 +.TP
  167.42 +\fB\-V\fR, \fB\-\-version\fR
  167.43 +output version information and exit
  167.44 +.TP
  167.45 +\fB\-d\fR, \fB\-\-delay\fR=\fISECONDS\fR
  167.46 +seconds between updates (default 1)
  167.47 +.TP
  167.48 +\fB\-n\fR, \fB\-\-networks\fR
  167.49 +output network information
  167.50 +.TP
  167.51 +\fB\-r\fR, \fB\-\-repeat\-header\fR
  167.52 +repeat table header before each domain
  167.53 +.TP
  167.54 +\fB\-v\fR, \fB\-\-vcpus\fR
  167.55 +output VCPU data
  167.56 +
  167.57 +.SH "INTERACTIVE COMMANDS"
  167.58 +All interactive commands are case-insensitive.
  167.59 +.TP
  167.60 +.B D
  167.61 +set delay between updates
  167.62 +.TP
  167.63 +.B N
  167.64 +toggle display of network information
  167.65 +.TP
  167.66 +.B Q, Esc
  167.67 +quit
  167.68 +.TP
  167.69 +.B R
  167.70 +toggle table header before each domain
  167.71 +.TP
  167.72 +.B S
  167.73 +cycle sort order
  167.74 +.TP
  167.75 +.B V
  167.76 +toggle display of VCPU information
  167.77 +.TP
  167.78 +.B Arrows
  167.79 +scroll domain display
  167.80 +
  167.81 +.SH AUTHORS
  167.82 +Written by Judy Fischbach, David Hendricks, and Josh Triplett
  167.83 +
  167.84 +.SH "REPORTING BUGS"
  167.85 +Report bugs to <dsteklof@us.ibm.com>.
  167.86 +
  167.87 +.SH COPYRIGHT
  167.88 +Copyright \(co 2005  International Business Machines  Corp
  167.89 +.br
  167.90 +This is free software; see the source for copying conditions.  There is NO
  167.91 +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   168.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   168.2 +++ b/tools/xenstat/xentop/xentop.c	Tue Aug 23 08:40:58 2005 +0000
   168.3 @@ -0,0 +1,876 @@
   168.4 +/*
   168.5 + *  Copyright (C) International Business Machines  Corp., 2005
   168.6 + *  Author(s): Judy Fischbach <jfisch@us.ibm.com>
   168.7 + *             David Hendricks <dhendrix@us.ibm.com>
   168.8 + *             Josh Triplett <josht@us.ibm.com>
   168.9 + *    based on code from Anthony Liguori <aliguori@us.ibm.com>
  168.10 + *
  168.11 + *  This program is free software; you can redistribute it and/or modify
  168.12 + *  it under the terms of the GNU General Public License as published by
  168.13 + *  the Free Software Foundation; under version 2 of the License.
  168.14 + *
  168.15 + *  This program is distributed in the hope that it will be useful,
  168.16 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  168.17 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  168.18 + *  GNU General Public License for more details.
  168.19 + *
  168.20 + *  You should have received a copy of the GNU General Public License
  168.21 + *  along with this program; if not, write to the Free Software
  168.22 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  168.23 + */
  168.24 +#include <curses.h>
  168.25 +#include <ctype.h>
  168.26 +#include <errno.h>
  168.27 +#include <stdio.h>
  168.28 +#include <stdlib.h>
  168.29 +#include <string.h>
  168.30 +#include <sys/time.h>
  168.31 +#include <time.h>
  168.32 +#include <unistd.h>
  168.33 +
  168.34 +#include <xenstat.h>
  168.35 +
  168.36 +#define XENTOP_VERSION "1.0"
  168.37 +
  168.38 +#define XENTOP_DISCLAIMER \
  168.39 +"Copyright (C) 2005  International Business Machines  Corp\n"\
  168.40 +"This is free software; see the source for copying conditions.There is NO\n"\
  168.41 +"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
  168.42 +#define XENTOP_BUGSTO "Report bugs to <dsteklof@us.ibm.com>.\n"
  168.43 +
  168.44 +#define _GNU_SOURCE
  168.45 +#include <getopt.h>
  168.46 +
  168.47 +#if !defined(__GNUC__) && !defined(__GNUG__)
  168.48 +#define __attribute__(arg) /* empty */
  168.49 +#endif
  168.50 +
  168.51 +#define KEY_ESCAPE '\x1B'
  168.52 +
  168.53 +/*
  168.54 + * Function prototypes
  168.55 + */
  168.56 +/* Utility functions */
  168.57 +static void usage(const char *);
  168.58 +static void version(void);
  168.59 +static void cleanup(void);
  168.60 +static void fail(const char *);
  168.61 +static int current_row(void);
  168.62 +static int lines(void);
  168.63 +static void print(const char *, ...) __attribute__((format(printf,1,2)));
  168.64 +static void attr_addstr(int attr, const char *str);
  168.65 +static void set_delay(char *value);
  168.66 +static void set_prompt(char *new_prompt, void (*func)(char *));
  168.67 +static int handle_key(int);
  168.68 +static int compare(unsigned long long, unsigned long long);
  168.69 +static int compare_domains(xenstat_domain **, xenstat_domain **);
  168.70 +static unsigned long long tot_net_bytes( xenstat_domain *, int);
  168.71 +
  168.72 +/* Field functions */
  168.73 +static int compare_domid(xenstat_domain *domain1, xenstat_domain *domain2);
  168.74 +static void print_domid(xenstat_domain *domain);
  168.75 +static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2);
  168.76 +static void print_state(xenstat_domain *domain);
  168.77 +static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2);
  168.78 +static void print_cpu(xenstat_domain *domain);
  168.79 +static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2);
  168.80 +static void print_cpu_pct(xenstat_domain *domain);
  168.81 +static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2);
  168.82 +static void print_mem(xenstat_domain *domain);
  168.83 +static void print_mem_pct(xenstat_domain *domain);
  168.84 +static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2);
  168.85 +static void print_maxmem(xenstat_domain *domain);
  168.86 +static void print_max_pct(xenstat_domain *domain);
  168.87 +static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2);
  168.88 +static void print_vcpus(xenstat_domain *domain);
  168.89 +static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2);
  168.90 +static void print_nets(xenstat_domain *domain);
  168.91 +static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2);
  168.92 +static void print_net_tx(xenstat_domain *domain);
  168.93 +static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2);
  168.94 +static void print_net_rx(xenstat_domain *domain);
  168.95 +static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2);
  168.96 +static void print_ssid(xenstat_domain *domain);
  168.97 +
  168.98 +/* Section printing functions */
  168.99 +static void do_summary(void);
 168.100 +static void do_header(void);
 168.101 +static void do_bottom_line(void);
 168.102 +static void do_domain(xenstat_domain *);
 168.103 +static void do_vcpu(xenstat_domain *);
 168.104 +static void do_network(xenstat_domain *);
 168.105 +static void top(void);
 168.106 +
 168.107 +/* Field types */
 168.108 +typedef enum field_id {
 168.109 +	FIELD_DOMID,
 168.110 +	FIELD_STATE,
 168.111 +	FIELD_CPU,
 168.112 +	FIELD_CPU_PCT,
 168.113 +	FIELD_MEM,
 168.114 +	FIELD_MEM_PCT,
 168.115 +	FIELD_MAXMEM,
 168.116 +	FIELD_MAX_PCT,
 168.117 +	FIELD_VCPUS,
 168.118 +	FIELD_NETS,
 168.119 +	FIELD_NET_TX,
 168.120 +	FIELD_NET_RX,
 168.121 +	FIELD_SSID
 168.122 +} field_id;
 168.123 +
 168.124 +typedef struct field {
 168.125 +	field_id num;
 168.126 +	const char *header;
 168.127 +	unsigned int default_width;
 168.128 +	int (*compare)(xenstat_domain *domain1, xenstat_domain *domain2);
 168.129 +	void (*print)(xenstat_domain *domain);
 168.130 +} field;
 168.131 +
 168.132 +field fields[] = {
 168.133 +	{ FIELD_DOMID,   "DOMID",      5, compare_domid,   print_domid   },
 168.134 +	{ FIELD_STATE,   "STATE",      6, compare_state,   print_state   },
 168.135 +	{ FIELD_CPU,     "CPU(sec)",  10, compare_cpu,     print_cpu     },
 168.136 +	{ FIELD_CPU_PCT, "CPU(%)",     6, compare_cpu_pct, print_cpu_pct },
 168.137 +	{ FIELD_MEM,     "MEM(k)",    10, compare_mem,     print_mem     },
 168.138 +	{ FIELD_MEM_PCT, "MEM(%)",     6, compare_mem,     print_mem_pct },
 168.139 +	{ FIELD_MAXMEM,  "MAXMEM(k)", 10, compare_maxmem,  print_maxmem  },
 168.140 +	{ FIELD_MAX_PCT, "MAXMEM(%)",  9, compare_maxmem,  print_max_pct },
 168.141 +	{ FIELD_VCPUS,   "VCPUS",      5, compare_vcpus,   print_vcpus   },
 168.142 +	{ FIELD_NETS,    "NETS",       4, compare_nets,    print_nets    },
 168.143 +	{ FIELD_NET_TX,  "NETTX(k)",   8, compare_net_tx,  print_net_tx  },
 168.144 +	{ FIELD_NET_RX,  "NETRX(k)",   8, compare_net_rx,  print_net_rx  },
 168.145 +	{ FIELD_SSID,    "SSID",       4, compare_ssid,    print_ssid    }
 168.146 +};
 168.147 +
 168.148 +const unsigned int NUM_FIELDS = sizeof(fields)/sizeof(field);
 168.149 +
 168.150 +/* Globals */
 168.151 +struct timeval curtime, oldtime;
 168.152 +xenstat_handle *xhandle = NULL;
 168.153 +xenstat_node *prev_node = NULL;
 168.154 +xenstat_node *cur_node = NULL;
 168.155 +field_id sort_field = FIELD_DOMID;
 168.156 +unsigned int first_domain_index = 0;
 168.157 +unsigned int delay = 1;
 168.158 +int show_vcpus = 0;
 168.159 +int show_networks = 0;
 168.160 +int repeat_header = 0;
 168.161 +#define PROMPT_VAL_LEN 80
 168.162 +char *prompt = NULL;
 168.163 +char prompt_val[PROMPT_VAL_LEN];
 168.164 +int prompt_val_len = 0;
 168.165 +void (*prompt_complete_func)(char *);
 168.166 +
 168.167 +/*
 168.168 + * Function definitions
 168.169 + */
 168.170 +
 168.171 +/* Utility functions */
 168.172 +
 168.173 +/* Print usage message, using given program name */
 168.174 +static void usage(const char *program)
 168.175 +{
 168.176 +	printf("Usage: %s [OPTION]\n"
 168.177 +	       "Displays ongoing information about xen vm resources \n\n"
 168.178 +	       "-h, --help           display this help and exit\n"
 168.179 +	       "-V, --version        output version information and exit\n"
 168.180 +	       "-d, --delay=SECONDS  seconds between updates (default 1)\n"
 168.181 +	       "-n, --networks       output vif network data\n"
 168.182 +	       "-r, --repeat-header  repeat table header before each domain\n"
 168.183 +	       "-v, --vcpus          output vcpu data\n"
 168.184 +	       "\n" XENTOP_BUGSTO,
 168.185 +	       program);
 168.186 +	return;
 168.187 +}
 168.188 +
 168.189 +/* Print program version information */
 168.190 +static void version(void)
 168.191 +{
 168.192 +	printf("xentop " XENTOP_VERSION "\n"
 168.193 +	       "Written by Judy Fischbach, David Hendricks, Josh Triplett\n"
 168.194 +	       "\n" XENTOP_DISCLAIMER);
 168.195 +}
 168.196 +
 168.197 +/* Clean up any open resources */
 168.198 +static void cleanup(void)
 168.199 +{
 168.200 +	if(!isendwin())
 168.201 +		endwin();
 168.202 +	if(prev_node != NULL)
 168.203 +		xenstat_free_node(prev_node);
 168.204 +	if(cur_node != NULL)
 168.205 +		xenstat_free_node(cur_node);
 168.206 +	if(xhandle != NULL)
 168.207 +		xenstat_uninit(xhandle);
 168.208 +}
 168.209 +
 168.210 +/* Display the given message and gracefully exit */
 168.211 +static void fail(const char *str)
 168.212 +{
 168.213 +	if(!isendwin())
 168.214 +		endwin();
 168.215 +	fprintf(stderr, str);
 168.216 +	exit(1);
 168.217 +}
 168.218 +
 168.219 +/* Return the row containing the cursor. */
 168.220 +static int current_row(void)
 168.221 +{
 168.222 +	int y, x;
 168.223 +	getyx(stdscr, y, x);
 168.224 +	return y;
 168.225 +}
 168.226 +
 168.227 +/* Return the number of lines on the screen. */
 168.228 +static int lines(void)
 168.229 +{
 168.230 +	int y, x;
 168.231 +	getmaxyx(stdscr, y, x);
 168.232 +	return y;
 168.233 +}
 168.234 +
 168.235 +/* printf-style print function which calls printw, but only if the cursor is
 168.236 + * not on the last line. */
 168.237 +static void print(const char *fmt, ...)
 168.238 +{
 168.239 +	va_list args;
 168.240 +
 168.241 +	if(current_row() < lines()-1) {
 168.242 +		va_start(args, fmt);
 168.243 +		vw_printw(stdscr, fmt, args);
 168.244 +		va_end(args);
 168.245 +	}
 168.246 +}
 168.247 +
 168.248 +/* Print a string with the given attributes set. */
 168.249 +static void attr_addstr(int attr, const char *str)
 168.250 +{
 168.251 +	attron(attr);
 168.252 +	addstr(str);
 168.253 +	attroff(attr);
 168.254 +}
 168.255 +
 168.256 +/* Handle setting the delay from the user-supplied value in prompt_val */
 168.257 +static void set_delay(char *value)
 168.258 +{
 168.259 +	int new_delay;
 168.260 +	new_delay = atoi(prompt_val);
 168.261 +	if(new_delay > 0)
 168.262 +		delay = new_delay;
 168.263 +}
 168.264 +
 168.265 +/* Enable prompting mode with the given prompt string; call the given function
 168.266 + * when a value is available. */
 168.267 +static void set_prompt(char *new_prompt, void (*func)(char *))
 168.268 +{
 168.269 +	prompt = new_prompt;
 168.270 +	prompt_val[0] = '\0';
 168.271 +	prompt_val_len = 0;
 168.272 +	prompt_complete_func = func;
 168.273 +}
 168.274 +
 168.275 +/* Handle user input, return 0 if the program should quit, or 1 if not */
 168.276 +static int handle_key(int ch)
 168.277 +{
 168.278 +	if(prompt == NULL) {
 168.279 +		/* Not prompting for input; handle interactive commands */
 168.280 +		switch(ch) {
 168.281 +		case 'n': case 'N':
 168.282 +			show_networks ^= 1;
 168.283 +			break;
 168.284 +		case 'r': case 'R':
 168.285 +			repeat_header ^= 1;
 168.286 +			break;
 168.287 +		case 's': case 'S':
 168.288 +			sort_field = (sort_field + 1) % NUM_FIELDS;
 168.289 +			break;
 168.290 +		case 'v': case 'V':
 168.291 +			show_vcpus ^= 1;
 168.292 +			break;
 168.293 +		case KEY_DOWN:
 168.294 +			first_domain_index++;
 168.295 +			break;
 168.296 +		case KEY_UP:
 168.297 +			if(first_domain_index > 0)
 168.298 +				first_domain_index--;
 168.299 +			break;
 168.300 +		case 'd': case 'D':
 168.301 +			set_prompt("Delay(sec)", set_delay);
 168.302 +			break;
 168.303 +		case 'q': case 'Q': case KEY_ESCAPE:
 168.304 +			return 0;
 168.305 +		}
 168.306 +	} else {
 168.307 +		/* Prompting for input; handle line editing */
 168.308 +		switch(ch) {
 168.309 +		case '\r':
 168.310 +			prompt_complete_func(prompt_val);
 168.311 +			set_prompt(NULL, NULL);
 168.312 +			break;
 168.313 +		case KEY_ESCAPE:
 168.314 +			set_prompt(NULL, NULL);
 168.315 +			break;
 168.316 +		case KEY_BACKSPACE:
 168.317 +			if(prompt_val_len > 0)
 168.318 +				prompt_val[--prompt_val_len] = '\0';
 168.319 +		default:
 168.320 +			if((prompt_val_len+1) < PROMPT_VAL_LEN
 168.321 +			   && isprint(ch)) {
 168.322 +				prompt_val[prompt_val_len++] = (char)ch;
 168.323 +				prompt_val[prompt_val_len] = '\0';
 168.324 +			}
 168.325 +		}
 168.326 +	}
 168.327 +
 168.328 +	return 1;
 168.329 +}
 168.330 +
 168.331 +/* Compares two integers, returning -1,0,1 for <,=,> */
 168.332 +static int compare(unsigned long long i1, unsigned long long i2)
 168.333 +{
 168.334 +	if(i1 < i2)
 168.335 +		return -1;
 168.336 +	if(i1 > i2)
 168.337 +		return 1;
 168.338 +	return 0;
 168.339 +}
 168.340 +
 168.341 +/* Comparison function for use with qsort.  Compares two domains using the
 168.342 + * current sort field. */
 168.343 +static int compare_domains(xenstat_domain **domain1, xenstat_domain **domain2)
 168.344 +{
 168.345 +	return fields[sort_field].compare(*domain1, *domain2);
 168.346 +}
 168.347 +
 168.348 +/* Field functions */
 168.349 +
 168.350 +/* Compares domain ids of two domains, returning -1,0,1 for <,=,> */
 168.351 +int compare_domid(xenstat_domain *domain1, xenstat_domain *domain2)
 168.352 +{
 168.353 +	return compare(xenstat_domain_id(domain1), xenstat_domain_id(domain2));
 168.354 +}
 168.355 +
 168.356 +/* Prints domain identification number */
 168.357 +void print_domid(xenstat_domain *domain)
 168.358 +{
 168.359 +	print("%5u", xenstat_domain_id(domain));
 168.360 +}
 168.361 +
 168.362 +struct {
 168.363 +	unsigned int (*get)(xenstat_domain *);
 168.364 +	char ch;
 168.365 +} state_funcs[] = {
 168.366 +	{ xenstat_domain_dying,    'd' },
 168.367 +	{ xenstat_domain_shutdown, 's' },
 168.368 +	{ xenstat_domain_blocked,  'b' },
 168.369 +	{ xenstat_domain_crashed,  'c' },
 168.370 +	{ xenstat_domain_paused,   'p' },
 168.371 +	{ xenstat_domain_running,  'r' }
 168.372 +};
 168.373 +const unsigned int NUM_STATES = sizeof(state_funcs)/sizeof(*state_funcs);
 168.374 +
 168.375 +/* Compare states of two domains, returning -1,0,1 for <,=,> */
 168.376 +static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2)
 168.377 +{
 168.378 +	unsigned int i, d1s, d2s;
 168.379 +	for(i = 0; i < NUM_STATES; i++) {
 168.380 +		d1s = state_funcs[i].get(domain1);
 168.381 +		d2s = state_funcs[i].get(domain2);
 168.382 +		if(d1s && !d2s)
 168.383 +			return -1;
 168.384 +		if(d2s && !d1s)
 168.385 +			return 1;
 168.386 +	}
 168.387 +	return 0;
 168.388 +}
 168.389 +
 168.390 +/* Prints domain state in abbreviated letter format */
 168.391 +static void print_state(xenstat_domain *domain)
 168.392 +{
 168.393 +	unsigned int i;
 168.394 +	for(i = 0; i < NUM_STATES; i++)
 168.395 +		print("%c", state_funcs[i].get(domain) ? state_funcs[i].ch
 168.396 +		                                       : '-');
 168.397 +}
 168.398 +
 168.399 +/* Compares cpu usage of two domains, returning -1,0,1 for <,=,> */
 168.400 +static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2)
 168.401 +{
 168.402 +	return -compare(xenstat_domain_cpu_ns(domain1),
 168.403 +			xenstat_domain_cpu_ns(domain2));
 168.404 +}
 168.405 +
 168.406 +/* Prints domain cpu usage in seconds */
 168.407 +static void print_cpu(xenstat_domain *domain)
 168.408 +{
 168.409 +	print("%10llu", xenstat_domain_cpu_ns(domain)/1000000000);
 168.410 +}
 168.411 +
 168.412 +/* Computes the CPU percentage used for a specified domain */
 168.413 +static double get_cpu_pct(xenstat_domain *domain)
 168.414 +{
 168.415 +	xenstat_domain *old_domain;
 168.416 +	double us_elapsed;
 168.417 +
 168.418 +	/* Can't calculate CPU percentage without a previous sample. */
 168.419 +	if(prev_node == NULL)
 168.420 +		return 0.0;
 168.421 +
 168.422 +	old_domain = xenstat_node_domain(prev_node, xenstat_domain_id(domain));
 168.423 +	if(old_domain == NULL)
 168.424 +		return 0.0;
 168.425 +
 168.426 +	/* Calculate the time elapsed in microseconds */
 168.427 +	us_elapsed = ((curtime.tv_sec-oldtime.tv_sec)*1000000.0
 168.428 +		      +(curtime.tv_usec - oldtime.tv_usec));
 168.429 +
 168.430 +	/* In the following, nanoseconds must be multiplied by 1000.0 to
 168.431 +	 * convert to microseconds, then divided by 100.0 to get a percentage,
 168.432 +	 * resulting in a multiplication by 10.0 */
 168.433 +	return ((xenstat_domain_cpu_ns(domain)
 168.434 +		 -xenstat_domain_cpu_ns(old_domain))/10.0)/us_elapsed;
 168.435 +}
 168.436 +
 168.437 +static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2)
 168.438 +{
 168.439 +	return -compare(get_cpu_pct(domain1), get_cpu_pct(domain2));
 168.440 +}
 168.441 +
 168.442 +/* Prints cpu percentage statistic */
 168.443 +static void print_cpu_pct(xenstat_domain *domain)
 168.444 +{
 168.445 +	print("%6.1f", get_cpu_pct(domain));
 168.446 +}
 168.447 +
 168.448 +/* Compares current memory of two domains, returning -1,0,1 for <,=,> */
 168.449 +static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2)
 168.450 +{
 168.451 +	return -compare(xenstat_domain_cur_mem(domain1),
 168.452 +	                xenstat_domain_cur_mem(domain2));
 168.453 +}
 168.454 +
 168.455 +/* Prints current memory statistic */
 168.456 +static void print_mem(xenstat_domain *domain)
 168.457 +{
 168.458 +	print("%10llu", xenstat_domain_cur_mem(domain)/1024);
 168.459 +}
 168.460 +
 168.461 +/* Prints memory percentage statistic, ratio of current domain memory to total
 168.462 + * node memory */
 168.463 +static void print_mem_pct(xenstat_domain *domain)
 168.464 +{
 168.465 +	print("%6.1f", (double)xenstat_domain_cur_mem(domain) /
 168.466 +	               (double)xenstat_node_tot_mem(cur_node) * 100);
 168.467 +}
 168.468 +
 168.469 +/* Compares maximum memory of two domains, returning -1,0,1 for <,=,> */
 168.470 +static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2)
 168.471 +{
 168.472 +	return -compare(xenstat_domain_max_mem(domain1),
 168.473 +	                xenstat_domain_max_mem(domain2));
 168.474 +}
 168.475 +
 168.476 +/* Prints maximum domain memory statistic in KB */
 168.477 +static void print_maxmem(xenstat_domain *domain)
 168.478 +{
 168.479 +	unsigned long long max_mem = xenstat_domain_max_mem(domain);
 168.480 +	if(max_mem == ((unsigned long long)-1))
 168.481 +		print("%10s", "no limit");
 168.482 +	else
 168.483 +		print("%10llu", max_mem/1024);
 168.484 +}
 168.485 +
 168.486 +/* Prints memory percentage statistic, ratio of current domain memory to total
 168.487 + * node memory */
 168.488 +static void print_max_pct(xenstat_domain *domain)
 168.489 +{
 168.490 +	if (xenstat_domain_max_mem(domain) == (unsigned long long)-1)
 168.491 +		print("%9s", "n/a");
 168.492 +	else
 168.493 +		print("%9.1f", (double)xenstat_domain_max_mem(domain) /
 168.494 +		               (double)xenstat_node_tot_mem(cur_node) * 100);
 168.495 +}
 168.496 +
 168.497 +/* Compares number of virtual CPUs of two domains, returning -1,0,1 for
 168.498 + * <,=,> */
 168.499 +static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2)
 168.500 +{
 168.501 +	return -compare(xenstat_domain_num_vcpus(domain1),
 168.502 +	                xenstat_domain_num_vcpus(domain2));
 168.503 +}
 168.504 +
 168.505 +/* Prints number of virtual CPUs statistic */
 168.506 +static void print_vcpus(xenstat_domain *domain)
 168.507 +{
 168.508 +	print("%5u", xenstat_domain_num_vcpus(domain));
 168.509 +}
 168.510 +
 168.511 +/* Compares number of virtual networks of two domains, returning -1,0,1 for
 168.512 + * <,=,> */
 168.513 +static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2)
 168.514 +{
 168.515 +	return -compare(xenstat_domain_num_networks(domain1),
 168.516 +	                xenstat_domain_num_networks(domain2));
 168.517 +}
 168.518 +
 168.519 +/* Prints number of virtual networks statistic */
 168.520 +static void print_nets(xenstat_domain *domain)
 168.521 +{
 168.522 +	print("%4u", xenstat_domain_num_networks(domain));
 168.523 +}
 168.524 +
 168.525 +/* Compares number of total network tx bytes of two domains, returning -1,0,1 for
 168.526 + * <,=,> */
 168.527 +static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2)
 168.528 +{
 168.529 +	return -compare(tot_net_bytes(domain1, FALSE),
 168.530 +	                tot_net_bytes(domain2, FALSE));
 168.531 +}
 168.532 +
 168.533 +/* Prints number of total network tx bytes statistic */
 168.534 +static void print_net_tx(xenstat_domain *domain)
 168.535 +{
 168.536 +	print("%8llu", tot_net_bytes(domain, FALSE)/1024);
 168.537 +}
 168.538 +
 168.539 +/* Compares number of total network rx bytes of two domains, returning -1,0,1 for
 168.540 + * <,=,> */
 168.541 +static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2)
 168.542 +{
 168.543 +	return -compare(tot_net_bytes(domain1, TRUE),
 168.544 +	                tot_net_bytes(domain2, TRUE));
 168.545 +}
 168.546 +
 168.547 +/* Prints number of total network rx bytes statistic */
 168.548 +static void print_net_rx(xenstat_domain *domain)
 168.549 +{
 168.550 +	print("%8llu", tot_net_bytes(domain, TRUE)/1024);
 168.551 +}
 168.552 +
 168.553 +/* Gets number of total network bytes statistic, if rx true, then rx bytes
 168.554 + * otherwise tx bytes
 168.555 + */
 168.556 +static unsigned long long tot_net_bytes(xenstat_domain *domain, int rx_flag)
 168.557 +{
 168.558 +	int i = 0;
 168.559 +	xenstat_network *network;
 168.560 +	unsigned num_networks = 0;
 168.561 +        unsigned long long total = 0;
 168.562 +
 168.563 +	/* How many networks? */
 168.564 +	num_networks = xenstat_domain_num_networks(domain);
 168.565 +
 168.566 +	/* Dump information for each network */
 168.567 +	for (i=0; i < num_networks; i++) {
 168.568 +		/* Next get the network information */
 168.569 +		network = xenstat_domain_network(domain,i);
 168.570 +                if (rx_flag) 
 168.571 +			total += xenstat_network_rbytes(network);
 168.572 +                else 
 168.573 +			total += xenstat_network_tbytes(network);
 168.574 +	}
 168.575 +        return (total);
 168.576 +}
 168.577 +
 168.578 +/* Compares security id (ssid) of two domains, returning -1,0,1 for <,=,> */
 168.579 +static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2)
 168.580 +{
 168.581 +	return compare(xenstat_domain_ssid(domain1),
 168.582 +		       xenstat_domain_ssid(domain2));
 168.583 +}
 168.584 +
 168.585 +/* Prints ssid statistic */
 168.586 +static void print_ssid(xenstat_domain *domain)
 168.587 +{
 168.588 +	print("%4u", xenstat_domain_ssid(domain));
 168.589 +}
 168.590 +
 168.591 +/* Section printing functions */
 168.592 +/* Prints the top summary, above the domain table */
 168.593 +void do_summary(void)
 168.594 +{
 168.595 +#define TIME_STR_LEN 9
 168.596 +	const char *TIME_STR_FORMAT = "%H:%M:%S";
 168.597 +	char time_str[TIME_STR_LEN];
 168.598 +	unsigned run = 0, block = 0, pause = 0,
 168.599 +	         crash = 0, dying = 0, shutdown = 0;
 168.600 +	unsigned i, num_domains = 0;
 168.601 +	unsigned long long used = 0;
 168.602 +	xenstat_domain *domain;
 168.603 +
 168.604 +	/* Print program name, current time, and number of domains */
 168.605 +	strftime(time_str, TIME_STR_LEN, TIME_STR_FORMAT,
 168.606 +	         localtime(&curtime.tv_sec));
 168.607 +	num_domains = xenstat_node_num_domains(cur_node);
 168.608 +	print("xentop - %s\n", time_str);
 168.609 +
 168.610 +	/* Tabulate what states domains are in for summary */
 168.611 +	for (i=0; i < num_domains; i++) {
 168.612 +		domain = xenstat_node_domain_by_index(cur_node,i);
 168.613 +		if (xenstat_domain_running(domain)) run++;
 168.614 +		else if (xenstat_domain_blocked(domain)) block++;
 168.615 +		else if (xenstat_domain_paused(domain)) pause++;
 168.616 +		else if (xenstat_domain_shutdown(domain)) shutdown++;
 168.617 +		else if (xenstat_domain_crashed(domain)) crash++;
 168.618 +		else if (xenstat_domain_dying(domain)) dying++;
 168.619 +	}
 168.620 +
 168.621 +	print("%u domains: %u running, %u blocked, %u paused, "
 168.622 +	      "%u crashed, %u dying, %u shutdown \n",
 168.623 +	      num_domains, run, block, pause, crash, dying, shutdown);
 168.624 +
 168.625 +	used = xenstat_node_tot_mem(cur_node)-xenstat_node_free_mem(cur_node);
 168.626 +
 168.627 +	/* Dump node memory and cpu information */
 168.628 +	print("Mem: %lluk total, %lluk used, %lluk free    "
 168.629 +	      "CPUs: %u @ %lluMHz\n",
 168.630 +	      xenstat_node_tot_mem(cur_node)/1024, used/1024,
 168.631 +	      xenstat_node_free_mem(cur_node)/1024,
 168.632 +	      xenstat_node_num_cpus(cur_node),
 168.633 +	      xenstat_node_cpu_hz(cur_node)/1000000);
 168.634 +}
 168.635 +
 168.636 +/* Display the top header for the domain table */
 168.637 +void do_header(void)
 168.638 +{
 168.639 +	field_id i;
 168.640 +
 168.641 +	/* Turn on REVERSE highlight attribute for headings */
 168.642 +	attron(A_REVERSE);
 168.643 +	for(i = 0; i < NUM_FIELDS; i++) {
 168.644 +		if(i != 0)
 168.645 +			print(" ");
 168.646 +		/* The BOLD attribute is turned on for the sort column */
 168.647 +		if(i == sort_field)
 168.648 +			attron(A_BOLD);
 168.649 +		print("%*s", fields[i].default_width, fields[i].header);
 168.650 +		if(i == sort_field)
 168.651 +			attroff(A_BOLD);
 168.652 +	}
 168.653 +	attroff(A_REVERSE);
 168.654 +	print("\n");
 168.655 +}
 168.656 +
 168.657 +/* Displays bottom status line or current prompt */
 168.658 +void do_bottom_line(void)
 168.659 +{
 168.660 +	move(lines()-1, 2);
 168.661 +
 168.662 +	if (prompt != NULL) {
 168.663 +		printw("%s: %s", prompt, prompt_val);
 168.664 +	} else {
 168.665 +		addch(A_REVERSE | 'D'); addstr("elay  ");
 168.666 +
 168.667 +		/* network */
 168.668 +		addch(A_REVERSE | 'N');
 168.669 +		attr_addstr(show_networks ? COLOR_PAIR(1) : 0, "etworks");
 168.670 +		addstr("  ");
 168.671 +
 168.672 +		/* vcpus */
 168.673 +		addch(A_REVERSE | 'V');
 168.674 +		attr_addstr(show_vcpus ? COLOR_PAIR(1) : 0, "CPUs");
 168.675 +		addstr("  ");
 168.676 +
 168.677 +		/* repeat */
 168.678 +		addch(A_REVERSE | 'R');
 168.679 +		attr_addstr(repeat_header ? COLOR_PAIR(1) : 0, "epeat header");
 168.680 +		addstr("  ");
 168.681 +
 168.682 +		/* sort order */
 168.683 +		addch(A_REVERSE | 'S'); addstr("ort order  ");
 168.684 +
 168.685 +		addch(A_REVERSE | 'Q'); addstr("uit  ");
 168.686 +	}
 168.687 +}
 168.688 +
 168.689 +/* Prints Domain information */
 168.690 +void do_domain(xenstat_domain *domain)
 168.691 +{
 168.692 +	unsigned int i;
 168.693 +	for(i = 0; i < NUM_FIELDS; i++) {
 168.694 +		if(i != 0)
 168.695 +			print(" ");
 168.696 +		if(i == sort_field)
 168.697 +			attron(A_BOLD);
 168.698 +		fields[i].print(domain);
 168.699 +		if(i == sort_field)
 168.700 +			attroff(A_BOLD);
 168.701 +	}
 168.702 +	print("\n");
 168.703 +}
 168.704 +
 168.705 +/* Output all vcpu information */
 168.706 +void do_vcpu(xenstat_domain *domain)
 168.707 +{
 168.708 +	int i = 0;
 168.709 +	unsigned num_vcpus = 0;
 168.710 +	xenstat_vcpu *vcpu;
 168.711 +
 168.712 +	print("VCPUs(sec): ");
 168.713 +
 168.714 +	num_vcpus = xenstat_domain_num_vcpus(domain);
 168.715 +
 168.716 +	/* for all vcpus dump out values */
 168.717 +	for (i=0; i< num_vcpus; i++) {
 168.718 +		vcpu = xenstat_domain_vcpu(domain,i);
 168.719 +
 168.720 +		if (i != 0 && (i%5)==0)
 168.721 +			print("\n        ");
 168.722 +		print(" %2u: %10llus", i, xenstat_vcpu_ns(vcpu)/1000000000);
 168.723 +	}
 168.724 +	print("\n");
 168.725 +}
 168.726 +
 168.727 +/* Output all network information */
 168.728 +void do_network(xenstat_domain *domain)
 168.729 +{
 168.730 +	int i = 0;
 168.731 +	xenstat_network *network;
 168.732 +	unsigned num_networks = 0;
 168.733 +
 168.734 +	/* How many networks? */
 168.735 +	num_networks = xenstat_domain_num_networks(domain);
 168.736 +
 168.737 +	/* Dump information for each network */
 168.738 +	for (i=0; i < num_networks; i++) {
 168.739 +		/* Next get the network information */
 168.740 +		network = xenstat_domain_network(domain,i);
 168.741 +
 168.742 +		print("Net%d RX: %8llubytes %8llupkts %8lluerr %8lludrop  ",
 168.743 +		      i,
 168.744 +		      xenstat_network_rbytes(network),
 168.745 +		      xenstat_network_rpackets(network),
 168.746 +		      xenstat_network_rerrs(network),
 168.747 +		      xenstat_network_rdrop(network));
 168.748 +
 168.749 +		print("TX: %8llubytes %8llupkts %8lluerr %8lludrop\n",
 168.750 +		      xenstat_network_tbytes(network),
 168.751 +		      xenstat_network_tpackets(network),
 168.752 +		      xenstat_network_terrs(network),
 168.753 +		      xenstat_network_tdrop(network));
 168.754 +	}
 168.755 +}
 168.756 +
 168.757 +static void top(void)
 168.758 +{
 168.759 +	xenstat_domain **domains;
 168.760 +	unsigned int i, num_domains = 0;
 168.761 +
 168.762 +	/* Now get the node information */
 168.763 +	if (prev_node != NULL)
 168.764 +		xenstat_free_node(prev_node);
 168.765 +	prev_node = cur_node;
 168.766 +	cur_node = xenstat_get_node(xhandle, XENSTAT_ALL);
 168.767 +	if (cur_node == NULL)
 168.768 +		fail("Failed to retrieve statistics from libxenstat\n");
 168.769 +
 168.770 +	/* dump summary top information */
 168.771 +	do_summary();
 168.772 +
 168.773 +	/* Count the number of domains for which to report data */
 168.774 +	num_domains = xenstat_node_num_domains(cur_node);
 168.775 +
 168.776 +	domains = malloc(num_domains*sizeof(xenstat_domain *));
 168.777 +	if(domains == NULL)
 168.778 +		fail("Failed to allocate memory\n");
 168.779 +
 168.780 +	for (i=0; i < num_domains; i++)
 168.781 +		domains[i] = xenstat_node_domain_by_index(cur_node, i);
 168.782 +
 168.783 +	/* Sort */
 168.784 +	qsort(domains, num_domains, sizeof(xenstat_domain *),
 168.785 +	      (int(*)(const void *, const void *))compare_domains);
 168.786 +
 168.787 +	if(first_domain_index >= num_domains)
 168.788 +		first_domain_index = num_domains-1;
 168.789 +
 168.790 +	for (i = first_domain_index; i < num_domains; i++) {
 168.791 +		if(current_row() == lines()-1)
 168.792 +			break;
 168.793 +		if (i == first_domain_index || repeat_header)
 168.794 +			do_header();
 168.795 +		do_domain(domains[i]);
 168.796 +		if (show_vcpus)
 168.797 +			do_vcpu(domains[i]);
 168.798 +		if (show_networks)
 168.799 +			do_network(domains[i]);
 168.800 +	}
 168.801 +
 168.802 +	do_bottom_line();
 168.803 +}
 168.804 +
 168.805 +int main(int argc, char **argv)
 168.806 +{
 168.807 +	int opt, optind = 0;
 168.808 +	int ch = ERR;
 168.809 +
 168.810 +	struct option lopts[] = {
 168.811 +		{ "help",          no_argument,       NULL, 'h' },
 168.812 +		{ "version",       no_argument,       NULL, 'V' },
 168.813 +		{ "networks",      no_argument,       NULL, 'n' },
 168.814 +		{ "repeat-header", no_argument,       NULL, 'r' },
 168.815 +		{ "vcpus",         no_argument,       NULL, 'v' },
 168.816 +		{ "delay",         required_argument, NULL, 'd' },
 168.817 +		{ 0, 0, 0, 0 },
 168.818 +	};
 168.819 +	const char *sopts = "hVbnvd:";
 168.820 +
 168.821 +	if (atexit(cleanup) != 0)
 168.822 +		fail("Failed to install cleanup handler.\n");
 168.823 +
 168.824 +	while ((opt = getopt_long(argc, argv, sopts, lopts, &optind)) != -1) {
 168.825 +		switch (opt) {
 168.826 +		case 'h':
 168.827 +		case '?':
 168.828 +		default:
 168.829 +			usage(argv[0]);
 168.830 +			exit(0);
 168.831 +		case 'V':
 168.832 +			version();
 168.833 +			exit(0);
 168.834 +		case 'n':
 168.835 +			show_networks = 1;
 168.836 +			break;
 168.837 +		case 'r':
 168.838 +			repeat_header = 1;
 168.839 +			break;
 168.840 +		case 'v':
 168.841 +			show_vcpus = 1;
 168.842 +			break;
 168.843 +		case 'd':
 168.844 +			delay = atoi(optarg);
 168.845 +			break;
 168.846 +		}
 168.847 +	}
 168.848 +
 168.849 +	/* Get xenstat handle */
 168.850 +	xhandle = xenstat_init();
 168.851 +	if (xhandle == NULL)
 168.852 +		fail("Failed to initialize xenstat library\n");
 168.853 +
 168.854 +	/* Begin curses stuff */
 168.855 +	initscr();
 168.856 +	start_color();
 168.857 +	cbreak();
 168.858 +	noecho();
 168.859 +	nonl();
 168.860 +	keypad(stdscr, TRUE);
 168.861 +	halfdelay(5);
 168.862 +	use_default_colors();
 168.863 +	init_pair(1, -1, COLOR_YELLOW);
 168.864 +
 168.865 +	do {
 168.866 +		gettimeofday(&curtime, NULL);
 168.867 +		if(ch != ERR || (curtime.tv_sec - oldtime.tv_sec) >= delay) {
 168.868 +			clear();
 168.869 +			top();
 168.870 +			oldtime = curtime;
 168.871 +			refresh();
 168.872 +		}
 168.873 +		ch = getch();
 168.874 +	} while (handle_key(ch));
 168.875 +
 168.876 +	/* Cleanup occurs in cleanup(), so no work to do here. */
 168.877 +
 168.878 +	return 0;
 168.879 +}