前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cloudsim仿真_虚拟机cpu怎么分配

cloudsim仿真_虚拟机cpu怎么分配

作者头像
全栈程序员站长
发布2022-11-04 10:19:36
1.3K0
发布2022-11-04 10:19:36
举报
文章被收录于专栏:全栈程序员必看

CloudSim源码分析之虚拟机分配

原文出处:http://blog.csdn.net/chhaj5236/article/details/6422425

虚拟机分配指的是,选择满足特定条件(内存、软件环境配置等)的主机创建虚拟机的过程,这个过程由Datacenter对象负责。VmAllocationPolicy这个抽象类代表的就是这个过程。用户可以通过继承该类实现自己的分配策略,CloudSim中,作者实现了一种简单的分配策略——VmAllocationPolicySimple。方法allocateHostForVm(Vm vm)是该类的核心,它实现了从主机列表中选择一台主机,并在其上创建虚拟机vm。主要实现过程的描述如下: (1) 记录下所有主机可用的处理器核心数。 (2) 从中选出可用处理器核心数最多的第一台主机,并尝试在其上创建虚拟机。 (3) 如果(2)失败了且还有主机没有尝试过,就排除当前选择的这台主机,重做(2)。 (4) 根据虚拟机是否创建成功,返回true或false。 源代码(版本2.1.1)分析如下:

[java] view plain copy

  1. /// VmAllocationPolicySimple源码分析///
  2. /*
  3. * Title: CloudSim Toolkit
  4. * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and
  5. * Simulation of Clouds
  6. * Licence: GPL – http://www.gnu.org/copyleft/gpl.html
  7. *
  8. * Copyright (c) 2009-2010, The University of Melbourne, Australia
  9. */
  10. package org.cloudbus.cloudsim;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. import org.cloudbus.cloudsim.core.CloudSim;
  16. /**
  17. * VmAllocationPolicySimple is an VmAllocationPolicy that
  18. * chooses, as the host for a VM, the host with
  19. * less PEs in use.
  20. *
  21. * @author Rodrigo N. Calheiros
  22. * @author Anton Beloglazov
  23. * @since CloudSim Toolkit 1.0
  24. */
  25. public class VmAllocationPolicySimple extends VmAllocationPolicy {
  26. /** The vm table.记录虚拟机被分配到哪台主机 */
  27. private Map<String, Host> vmTable;
  28. /** The used pes.记录虚拟机占用了几个处理器核心 */
  29. private Map<String, Integer> usedPes;
  30. /** The free pes.记录每台主机可用的处理器核心数 */
  31. private List<Integer> freePes;
  32. /**
  33. * Creates the new VmAllocationPolicySimple object.
  34. *
  35. * @param list the list
  36. *
  37. * @pre $none
  38. * @post $none
  39. */
  40. public VmAllocationPolicySimple(List<? extends Host> list) {
  41. super(list); //初始化主机列表hostList(继承自父类的成员)
  42. //初始化每台主机可用的处理器核心数freePes
  43. setFreePes(new ArrayList<Integer>());
  44. for (Host host : getHostList()) {
  45. getFreePes().add(host.getPesNumber());
  46. }
  47. //初始化vmTable和usedPes
  48. setVmTable(new HashMap<String, Host>());
  49. setUsedPes(new HashMap<String, Integer>());
  50. }
  51. /**
  52. * Allocates a host for a given VM.
  53. *
  54. * @param vm VM specification
  55. *
  56. * @return true if the host could be allocated; false otherwise
  57. *
  58. * @pre $none
  59. * @post $none
  60. */
  61. @Override
  62. public boolean allocateHostForVm(Vm vm) {
  63. int requiredPes = vm.getPesNumber();//创建vm所需的处理器核心数
  64. boolean result = false;
  65. int tries = 0; //尝试次数
  66. List<Integer> freePesTmp = new ArrayList<Integer>();
  67. for (Integer freePes : getFreePes()) {
  68. freePesTmp.add(freePes);
  69. }
  70. //如果当前虚拟机还未创建
  71. if (!getVmTable().containsKey(vm.getUid())) {
  72. do { //尝试创建虚拟机直到创建成功或所有的主机都已经尝试过
  73. int moreFree = Integer.MIN_VALUE; //当前最大可用核心数
  74. int idx = –1; //当前最大可用核心数对应主机的下标
  75. //找到可用处理器核心数最大的第一台主机
  76. for (int i=0; i < freePesTmp.size(); i++) {
  77. if (freePesTmp.get(i) > moreFree) {
  78. moreFree = freePesTmp.get(i);
  79. idx = i;
  80. }
  81. }
  82. Host host = getHostList().get(idx);
  83. result = host.vmCreate(vm); //尝试创建虚拟机
  84. if (result) { //如果虚拟机创建成功
  85. //更新映射关系及主机可用的处理器核心数
  86. getVmTable().put(vm.getUid(), host);
  87. getUsedPes().put(vm.getUid(), requiredPes);
  88. getFreePes().set(idx,getFreePes().get(idx) – requiredPes);
  89. result = true;
  90. break;
  91. } else { //如果创建失败
  92. //将当前主机的可用处理器核心数暂时设成最小值,从而排除该主机
  93. freePesTmp.set(idx, Integer.MIN_VALUE);
  94. }
  95. tries++;
  96. } while (!result && tries < getFreePes().size());
  97. }
  98. return result;
  99. }
  100. /**
  101. * Releases the host used by a VM.
  102. *
  103. * @param vm the vm
  104. *
  105. * @pre $none
  106. * @post none
  107. */
  108. @Override
  109. public void deallocateHostForVm(Vm vm) {
  110. //删除虚拟机相应的映射关系,通过主机销毁虚拟机并更新可用的处理器核心数
  111. Host host = getVmTable().remove(vm.getUid());
  112. int idx = getHostList().indexOf(host);
  113. int pes = getUsedPes().remove(vm.getUid());
  114. if (host != null) {
  115. host.vmDestroy(vm);
  116. getFreePes().set(idx, getFreePes().get(idx) + pes);
  117. }
  118. }
  119. /**
  120. * Gets the host that is executing the given VM belonging to the
  121. * given user.
  122. *
  123. * @param vm the vm
  124. *
  125. * @return the Host with the given vmID and userID; $null if not found
  126. *
  127. * @pre $none
  128. * @post $none
  129. */
  130. @Override
  131. public Host getHost(Vm vm) {
  132. return getVmTable().get(vm.getUid());
  133. }
  134. /**
  135. * Gets the host that is executing the given VM belonging to the
  136. * given user.
  137. *
  138. * @param vmId the vm id
  139. * @param userId the user id
  140. *
  141. * @return the Host with the given vmID and userID; $null if not found
  142. *
  143. * @pre $none
  144. * @post $none
  145. */
  146. @Override
  147. public Host getHost(int vmId, int userId) {
  148. return getVmTable().get(Vm.getUid(userId, vmId));
  149. }
  150. /**
  151. * Gets the vm table.
  152. *
  153. * @return the vm table
  154. */
  155. public Map<String, Host> getVmTable() {
  156. return vmTable;
  157. }
  158. /**
  159. * Sets the vm table.
  160. *
  161. * @param vmTable the vm table
  162. */
  163. protected void setVmTable(Map<String, Host> vmTable) {
  164. this.vmTable = vmTable;
  165. }
  166. /**
  167. * Gets the used pes.
  168. *
  169. * @return the used pes
  170. */
  171. protected Map<String, Integer> getUsedPes() {
  172. return usedPes;
  173. }
  174. /**
  175. * Sets the used pes.
  176. *
  177. * @param usedPes the used pes
  178. */
  179. protected void setUsedPes(Map<String, Integer> usedPes) {
  180. this.usedPes = usedPes;
  181. }
  182. /**
  183. * Gets the free pes.
  184. *
  185. * @return the free pes
  186. */
  187. protected List<Integer> getFreePes() {
  188. return freePes;
  189. }
  190. /**
  191. * Sets the free pes.
  192. *
  193. * @param freePes the new free pes
  194. */
  195. protected void setFreePes(List<Integer> freePes) {
  196. this.freePes = freePes;
  197. }
  198. //该方法暂未找到具体的实现
  199. @Override
  200. public List<Map<String, Object>> optimizeAllocation(
  201. List<? extends Vm> vmList) {
  202. // TODO Auto-generated method stub
  203. return null;
  204. }
  205. //将虚拟机分配给指定的主机
  206. @Override
  207. public boolean allocateHostForVm(Vm vm, Host host) {
  208. if (host.vmCreate(vm)) { //如果虚拟机创建成功,更新vmTable,并返回true
  209. getVmTable().put(vm.getUid(), host);
  210. Log.formatLine(“%.2f: VM #” + vm.getId() +
  211. ” has been allocated to the host #” +
  212. host.getId(), CloudSim.clock());
  213. return true;
  214. }
  215. return false;
  216. }
  217. }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/182265.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CloudSim源码分析之虚拟机分配
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档