Chinaunix首页 | 论坛 | 博客
  • 博客访问: 190971
  • 博文数量: 19
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1062
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-10 15:52
个人简介

经历过才能真的感受,做一个靠谱的人!

文章分类

全部博文(19)

文章存档

2015年(1)

2013年(18)

我的朋友

分类: 云计算

2013-09-16 09:37:14

 

                            CloudSim源码分析之虚拟机分配策略(VmAllocationPolicy

虚拟机分配是指根据特定的分配策略在满足一定条件的物理机上创建虚拟机。CloudSim只是实现了简单的虚拟机分配策略,默认情况下直接使用FCFS策略为主机分配虚拟机。VmAllocationPolicy是虚拟机分配策略的抽象类,CloudSim开发者推荐研究人员根据需求的特定场景实现自己的虚拟机分配策略,需要实现VmAllocationPolicy抽象类。

目前CloudSim通过VmAllocationPolicySimple类实现虚拟机分配, 方法allocateHostForVm(Vm vm)是该类的核心,它实现了从主机列表中选择一台物理机,并在其上创建虚拟机vm。主要实现过程的描述如下:

(1) 记录下所有物理机可用的处理器核心数。

(2) 从中选出可用处理器核心数最多的第一台物理机,并尝试在其上创建虚拟机。

(3) 如果(2)失败了且还有物理机没有尝试过,就排除当前选择的这台物理机,重做(2)

(4) 根据虚拟机是否创建成功,返回truefalse

(5) 如果返回true,更新虚拟机ID与物理机映射,更新虚拟机ID与使用的PE数映射,更新物理机空闲PE数。

点击(此处)折叠或打开

  1. /*******VmAllocationPolicySimple源码分析******/
  2. public class VmAllocationPolicySimple extends VmAllocationPolicy {

  3.     /** The vm table. */
  4.     // 保存虚拟机ID与物理机的映射

  5.     private Map<String, Host> vmTable;

  6.     /** The used pes. */
  7.     // 保存虚拟机ID与分配的PE数映射

  8.     private Map<String, Integer> usedPes;

  9.     /** The free pes. */
  10.     // 保存每台物理机可用的PE数

  11.     private List<Integer> freePes;

  12.     /**
  13.      * Creates the new VmAllocationPolicySimple object.
  14.      *
  15.      * @param list the list
  16.      * @pre $none
  17.      * @post $none
  18.      */
  19.     public VmAllocationPolicySimple(List<? extends Host> list) {
  20.         super(list); // 设置物理机列表


  21.         setFreePes(new ArrayList<Integer>());
  22.         // 获取每台主机的可用PE数

  23.         for (Host host : getHostList()) {
  24.             getFreePes().add(host.getNumberOfPes());

  25.         }

  26.         // 初始化VmTable和usedPes

  27.         setVmTable(new HashMap<String, Host>());
  28.         setUsedPes(new HashMap<String, Integer>());
  29.     }

  30.     /**
  31.      * Allocates a host for a given VM.
  32.      *
  33.      * @param vm VM specification
  34.      * @return $true if the host could be allocated; $false otherwise
  35.      * @pre $none
  36.      * @post $none
  37.      */
  38.     // 为虚拟机分配一台物理机

  39.     @Override
  40.     public boolean allocateHostForVm(Vm vm) {
  41.         int requiredPes = vm.getNumberOfPes(); // 获取该虚拟机需要的PE数

  42.         boolean result = false;
  43.         int tries = 0; // 分配物理机的失败次数

  44.         List<Integer> freePesTmp = new ArrayList<Integer>();
  45.         for (Integer freePes : getFreePes()) { // 临时保存每台物理机可用的PE数

  46.             freePesTmp.add(freePes);
  47.         }

  48.         // 如果该虚拟机没有分配物理机,尝试为其分配物理机

  49.         if (!getVmTable().containsKey(vm.getUid())) { // if this vm was not created

  50.             do {// we still trying until we find a host or until we try all of them

  51.                 int moreFree = Integer.MIN_VALUE; // 最小整数(为了保存可用PE数最大值)

  52.                 int idx = -1; // 保存物理机ID


  53.                 // we want the host with less pes in use

  54.                 // 获取可用PE数最大的物理机ID

  55.                 for (int i = 0; i < freePesTmp.size(); i++) {
  56.                     if (freePesTmp.get(i) > moreFree) {
  57.                         moreFree = freePesTmp.get(i);
  58.                         idx = i;
  59.                     }
  60.                 }

  61.                 // 获取可用PE数最大的物理机,并尝试在其上创建虚拟机

  62.                 Host host = getHostList().get(idx);
  63.                 result = host.vmCreate(vm); // 虚拟创建(后续会深入分析)


  64.                 if (result) { // if vm were succesfully created in the host

  65.                     // 如果虚拟机创建成功,更新虚拟机ID与物理机映射,更新虚拟机ID与使用的PE数映射,更新物理机空闲PE数

  66.                     getVmTable().put(vm.getUid(), host);
  67.                     getUsedPes().put(vm.getUid(), requiredPes);
  68.                     getFreePes().set(idx, getFreePes().get(idx) - requiredPes);
  69.                     result = true;
  70.                     break;
  71.                 } else {
  72.                     // 如果分配失败(还没看完其他代码,猜测可能失败的情况有:虽然物理机的PE数满足该虚拟机需求,但是其他资源可能不满足,如内存、存储等。),

  73.                     // 则将该物理机的可用PE数设置为整数最小值(只是在临时列表中设置),从而排除该物理机。

  74.                     freePesTmp.set(idx, Integer.MIN_VALUE);
  75.                 }
  76.                 tries++;
  77.             } while (!result && tries < getFreePes().size());

  78.         }

  79.         return result;
  80.     }

  81.     /**
  82.      * Releases the host used by a VM.
  83.      *
  84.      * @param vm the vm
  85.      * @pre $none
  86.      * @post none
  87.      */
  88.     // 销毁虚拟机

  89.     @Override
  90.     public void deallocateHostForVm(Vm vm) {
  91.         Host host = getVmTable().remove(vm.getUid()); // 删除该虚拟机与物理机的映射关系

  92.         int idx = getHostList().indexOf(host);
  93.         int pes = getUsedPes().remove(vm.getUid()); // 删除该虚拟机与使用PE数的映射关系

  94.         if (host != null) {
  95.             host.vmDestroy(vm); // 销毁虚拟机(后续会深入分析)

  96.             getFreePes().set(idx, getFreePes().get(idx) + pes); // 更新物理机可用PE数

  97.         }
  98.     }

  99.     /**
  100.      * Gets the host that is executing the given VM belonging to the given user.
  101.      *
  102.      * @param vm the vm
  103.      * @return the Host with the given vmID and userID; $null if not found
  104.      * @pre $none
  105.      * @post $none
  106.      */
  107.     @Override
  108.     public Host getHost(Vm vm) {
  109.         return getVmTable().get(vm.getUid());
  110.     }

  111.     /**
  112.      * Gets the host that is executing the given VM belonging to the given user.
  113.      *
  114.      * @param vmId the vm id
  115.      * @param userId the user id
  116.      * @return the Host with the given vmID and userID; $null if not found
  117.      * @pre $none
  118.      * @post $none
  119.      */
  120.     @Override
  121.     public Host getHost(int vmId, int userId) {
  122.         return getVmTable().get(Vm.getUid(userId, vmId));
  123.     }

  124.     /**
  125.      * Gets the vm table.
  126.      *
  127.      * @return the vm table
  128.      */
  129.     public Map<String, Host> getVmTable() {
  130.         return vmTable;
  131.     }

  132.     /**
  133.      * Sets the vm table.
  134.      *
  135.      * @param vmTable the vm table
  136.      */
  137.     protected void setVmTable(Map<String, Host> vmTable) {
  138.         this.vmTable = vmTable;
  139.     }

  140.     /**
  141.      * Gets the used pes.
  142.      *
  143.      * @return the used pes
  144.      */
  145.     protected Map<String, Integer> getUsedPes() {
  146.         return usedPes;
  147.     }

  148.     /**
  149.      * Sets the used pes.
  150.      *
  151.      * @param usedPes the used pes
  152.      */
  153.     protected void setUsedPes(Map<String, Integer> usedPes) {
  154.         this.usedPes = usedPes;
  155.     }

  156.     /**
  157.      * Gets the free pes.
  158.      *
  159.      * @return the free pes
  160.      */
  161.     protected List<Integer> getFreePes() {
  162.         return freePes;
  163.     }

  164.     /**
  165.      * Sets the free pes.
  166.      *
  167.      * @param freePes the new free pes
  168.      */
  169.     protected void setFreePes(List<Integer> freePes) {
  170.         this.freePes = freePes;
  171.     }

  172.     /*
  173.      * (non-Javadoc)
  174.      * @see cloudsim.VmAllocationPolicy#optimizeAllocation(double, cloudsim.VmList, double)
  175.      */
  176.     // 最优分配,具体不清楚

  177.     @Override
  178.     public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
  179.         // TODO Auto-generated method stub

  180.         return null;
  181.     }

  182.     /*
  183.      * (non-Javadoc)
  184.      * @see org.cloudbus.cloudsim.VmAllocationPolicy#allocateHostForVm(org.cloudbus.cloudsim.Vm,
  185.      * org.cloudbus.cloudsim.Host)
  186.      */
  187.     // 将虚拟机分配给特定的物理机

  188.     @Override
  189.     public boolean allocateHostForVm(Vm vm, Host host) {
  190.         if (host.vmCreate(vm)) { // if vm has been succesfully created in the host

  191.             // 如果虚拟机在指定物理机上被成功创建,更新相关数据结构,分析见allocateHostForVm(Vm vm)

  192.             getVmTable().put(vm.getUid(), host);

  193.             int requiredPes = vm.getNumberOfPes();
  194.             int idx = getHostList().indexOf(host);
  195.             getUsedPes().put(vm.getUid(), requiredPes);
  196.             getFreePes().set(idx, getFreePes().get(idx) - requiredPes);

  197.             Log.formatLine(
  198.                     "%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(),
  199.                     CloudSim.clock()); // clock()没搞清楚

  200.             return true;
  201.         }

  202.         return false;
  203.     }
  204. }


阅读(6988) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~