[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] /proc/xen/memory_target patch



This patch to the balloon driver eliminates the need for a user-space
program to slosh memory between domains and xen.   This uses a proc
file at /proc/xen/memory_target.  When read it reports memory the domain
owns in bytes.   Writing a new value to the memory_target proc file
will cause the domain to exchange memory with xen to reach the target.
A xenolinux domain cannot grow bigger than it was when created.
Must be done by root.

Target requests are made in bytes, or with a k m or g suffix.  The
request is parsed with the same function that parses the mem= boottime
kernel param.

Memory sizes in bytes are internally truncated to pages.  Minimum change
is PAGE_SIZE (4k).

        dragonfly:~# cat /proc/xen/memory_target 
        134217728l
        dragonfly:~# echo 50m > /proc/xen/memory_target 
        Relinquish 78MB to xen. Domain now has 50MB
        dragonfly:~# echo 100m > /proc/xen/memory_target 
        Reclaim 50MB from xen. Domain now has 100MB
        dragonfly:~# cat /proc/xen/memory_target 
        104857600l

Daivd

(btw, updating twisted to 1.3 solved my console problems)




*** xeno-unstable.bk/linux-2.4.26-xen-sparse/arch/xen/drivers/balloon/balloon.c 
Tue Jul  6 15:09:30 2004
--- 
obj-unstable/xeno-unstable.bk/linux-2.4.26-xen-sparse/arch/xen/drivers/balloon/balloon.c
    Fri Jul 16 15:12:11 2004
***************
*** 39,44 ****
--- 39,45 ----
  
  static struct proc_dir_entry *balloon_pde;
  unsigned long credit;
+ static unsigned long current_pages, max_pages;
  
  static inline pte_t *get_ptep(unsigned long addr)
  {
***************
*** 221,270 ****
      return ret;
  }
  
  static int balloon_write(struct file *file, const char *buffer,
                           u_long count, void *data)
  {
!     user_balloon_op_t bop;
  
      /* Only admin can play with the balloon :) */
      if ( !capable(CAP_SYS_ADMIN) )
          return -EPERM;
  
!     if ( copy_from_user(&bop, buffer, sizeof(bop)) )
          return -EFAULT;
  
!     switch ( bop.op )
!     {
!     case USER_INFLATE_BALLOON:
!         if ( inflate_balloon(bop.size) < bop.size )
!             return -EAGAIN;
!         break;
!         
!     case USER_DEFLATE_BALLOON:
!         deflate_balloon(bop.size);
!         break;
  
!     default:
!         printk("Unknown command to balloon driver.");
!         return -EFAULT;
      }
  
!     return sizeof(bop);
  }
  
  static int __init init_module(void)
  {
      printk(KERN_ALERT "Starting Xen Balloon driver\n");
  
!     credit = 0;
! 
!     if ( (balloon_pde = create_xen_proc_entry("balloon", 0600)) == NULL )
      {
          printk(KERN_ALERT "Unable to create balloon driver proc entry!");
          return -1;
      }
  
      balloon_pde->write_proc = balloon_write;
  
      return 0;
  }
--- 222,307 ----
      return ret;
  }
  
+ #define PAGE_TO_MB_SHIFT 8
+ 
  static int balloon_write(struct file *file, const char *buffer,
                           u_long count, void *data)
  {
!     char memstring[64], *endchar;
!     int len, i, pages;
!     unsigned long long target;
  
      /* Only admin can play with the balloon :) */
      if ( !capable(CAP_SYS_ADMIN) )
          return -EPERM;
  
!     if (count>sizeof memstring) {
!           return -EFBIG;
!     }
! 
!     len = strnlen_user(buffer, count);
!     if (len==0) return -EBADMSG;
!     if (len==1) return 1; /* input starts with a NUL char */
!     if ( strncpy_from_user(memstring, buffer, len) < 0)
          return -EFAULT;
  
!     endchar = memstring;
!     for(i=0; i<len; ++i,++endchar) {
!           if ('0'>memstring[i] || memstring[i]>'9') break;
!     }
!     if (i==0) return -EBADMSG;
  
!     target = memparse(memstring,&endchar);
!     pages = target >> PAGE_SHIFT;
! 
!     if (pages < current_pages) {
!           int change = inflate_balloon(current_pages-pages);
!           if (change<0) return change;
! 
!           current_pages -= change;
!           printk("Relinquish %dMB to xen. Domain now has %dMB\n",
!                   change>>PAGE_TO_MB_SHIFT, current_pages>>PAGE_TO_MB_SHIFT);
      }
+     else if (pages > current_pages) {
+           int change = deflate_balloon(min(pages,max_pages) - current_pages);
+           if (change<0) return change;
  
!           current_pages += change;
!           printk("Reclaim %dMB from xen. Domain now has %dMB\n",
!                   change>>PAGE_TO_MB_SHIFT, current_pages>>PAGE_TO_MB_SHIFT);
!     }
! 
!     return len;
! }
! 
! 
! static int balloon_read(char *page, char **start, off_t off,
!         int count, int *eof, void *data)
! {
!       int len;
!       len = sprintf(page,"%ul\n",current_pages<<PAGE_SHIFT);
! 
!       if (len <= off+count) *eof = 1;
!       *start = page + off;
!       len -= off;
!       if (len>count) len = count;
!       if (len<0) len = 0;
!       return len;
  }
  
  static int __init init_module(void)
  {
      printk(KERN_ALERT "Starting Xen Balloon driver\n");
  
!     max_pages = current_pages = start_info.nr_pages;
!     if ( (balloon_pde = create_xen_proc_entry("memory_target", 0644)) == NULL 
)
      {
          printk(KERN_ALERT "Unable to create balloon driver proc entry!");
          return -1;
      }
  
      balloon_pde->write_proc = balloon_write;
+     balloon_pde->read_proc = balloon_read;
  
      return 0;
  }


-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.