def compute_pm_util(vms, hosts): clear_hypervisors_util(hosts) for vm in vms: host = tools_mod.search_dictionaries("id", vm["host"], hosts)[0] host["util"] = host["util"] + vm["util_abs"] host["no_vms"] = host["no_vms"] + 1 host["ram_used"] = host["ram_used"] + vm["ram"]
def consolidate(env): vms = env["vms"] hosts = env["hosts"] sorted_hosts = tools_mod.sort_dictionaries("util", hosts) actions = [] # going through the sorted host asc = 0 for host in sorted_hosts: number_of_hosts = len(sorted_hosts) vms_on_host = tools_mod.search_dictionaries("host", host["id"], vms) sorted_vms_on_host_dsc = reversed( tools_mod.sort_dictionaries( "util_abs", vms_on_host)) # going through the vms on host for vm in sorted_vms_on_host_dsc: dsc = number_of_hosts - 1 # searching for a new host for a VM for dest_host in reversed(sorted_hosts): if asc < dsc: # found suitable host if(dest_host["util"] + vm["util_abs"] <= dest_host["capacity"] and dest_host["ram_used"] + vm["ram"] <= dest_host["ram_capacity"]): mig_mod.migrate_sim(vm, host, dest_host) actions.append({"action": "live-migrate", "vm_id": vm["id"], "src": host["id"], "dst": dest_host["id"]}) break dsc = dsc - 1 else: break asc = asc + 1 return actions
def parse_environment(input_file_path): environment = {} with open(input_file_path, "r") as f: lines = f.readlines() lines_array = [] # print lines for line in lines: if not line.startswith("#"): if line.split(): lines_array.append(line.split()) # print lines_array no_hypervisors = len(lines_array[0]) hypervisors = [] # setup hypervisors id, idle power consumption, max power consumption for i in range(no_hypervisors): hypervisors.append( { "id": "pm" + str(i), "consumption_max": float( lines_array[0][i]), "consumption_idle": float( lines_array[1][i]) * float( lines_array[0][i]), "capacity": float( lines_array[4][i]), "ram_capacity": float( lines_array[5][i]), "util": 0.0, "ram_used": 0.0, "power_consumption": 0.0, "no_vms": 0, "cpu": - 1, "host_ip": ""}) # print hypervisors no_vms = len(lines_array[2]) vms = [] for i in range(no_vms): vms.append({"id": "vm" + str(i), "util_abs": float(lines_array[2][i]), "ram": float(lines_array[3][i]), "host": ""}) pli = 8 for i in range(no_hypervisors): for j in range(no_vms): if int(lines_array[pli + i][j]) == 1: tools_mod.search_dictionaries( "id", "vm" + str(j), vms)[0]["host"] = "pm" + str(i) # print vms host_mod.compute_pm_util(vms, hypervisors) environment["vms"] = vms environment["hosts"] = hypervisors return environment
def print_env(env): print "Printing VM placement:" vms = env["vms"] hosts = env["hosts"] for host in hosts: vms_on_host = tools_mod.search_dictionaries("host", host["id"], vms) string = host["id"] + ": " + str(len(vms_on_host)) + "VMs (" for vm in vms_on_host: string = string + vm["id"] + ", " string = string + ")" print string
def split(env): vms = env["vms"] hosts = env["hosts"] sorted_hosts = tools_mod.sort_dictionaries("util", hosts) # sorted_hosts_dsc = reversed(sort_dictionaries("util", hosts)) actions = [] for host in reversed(sorted_hosts): # print "host" + host["id"] if host["util"] > host["capacity"]: vms_on_host = tools_mod.search_dictionaries( "host", host["id"], vms) sorted_vms_on_host = tools_mod.sort_dictionaries( "util_abs", vms_on_host) for vm in sorted_vms_on_host: # print len(sorted_vms_on_host) # print host["util"] if host["util"] <= host["capacity"]: # print "util: " + str(host["util"]) # print "capacity " + str(host["capacity"]) break # print "sorted host len: " + str(len(sorted_hosts)) for dest_host in reversed(sorted_hosts): # print "dest host: " + dest_host["id"] if (dest_host["util"] + vm["util_abs"] <= dest_host["capacity"] and dest_host["ram_used"] + vm["ram"] <= dest_host["ram_capacity"]): # print "dest host fits: " + dest_host["id"] mig_mod.migrate_sim(vm, host, dest_host) actions.append({ "action": "live-migrate", "vm_id": vm["id"], "src": host["id"], "dst": dest_host["id"] }) break return actions
def consolidate(env): vms = env["vms"] hosts = env["hosts"] sorted_hosts = tools_mod.sort_dictionaries("util", hosts) actions = [] # going through the sorted host asc = 0 for host in sorted_hosts: number_of_hosts = len(sorted_hosts) vms_on_host = tools_mod.search_dictionaries("host", host["id"], vms) sorted_vms_on_host_dsc = reversed( tools_mod.sort_dictionaries("util_abs", vms_on_host)) # going through the vms on host for vm in sorted_vms_on_host_dsc: dsc = number_of_hosts - 1 # searching for a new host for a VM for dest_host in reversed(sorted_hosts): if asc < dsc: # found suitable host if (dest_host["util"] + vm["util_abs"] <= dest_host["capacity"] and dest_host["ram_used"] + vm["ram"] <= dest_host["ram_capacity"]): mig_mod.migrate_sim(vm, host, dest_host) actions.append({ "action": "live-migrate", "vm_id": vm["id"], "src": host["id"], "dst": dest_host["id"] }) break dsc = dsc - 1 else: break asc = asc + 1 return actions
def split(env): vms = env["vms"] hosts = env["hosts"] sorted_hosts = tools_mod.sort_dictionaries("util", hosts) # sorted_hosts_dsc = reversed(sort_dictionaries("util", hosts)) actions = [] for host in reversed(sorted_hosts): # print "host" + host["id"] if host["util"] > host["capacity"]: vms_on_host = tools_mod.search_dictionaries( "host", host["id"], vms) sorted_vms_on_host = tools_mod.sort_dictionaries( "util_abs", vms_on_host) for vm in sorted_vms_on_host: # print len(sorted_vms_on_host) # print host["util"] if host["util"] <= host["capacity"]: # print "util: " + str(host["util"]) # print "capacity " + str(host["capacity"]) break # print "sorted host len: " + str(len(sorted_hosts)) for dest_host in reversed(sorted_hosts): # print "dest host: " + dest_host["id"] if(dest_host["util"] + vm["util_abs"] <= dest_host["capacity"] and dest_host["ram_used"] + vm["ram"] <= dest_host["ram_capacity"]): # print "dest host fits: " + dest_host["id"] mig_mod.migrate_sim(vm, host, dest_host) actions.append({"action": "live-migrate", "vm_id": vm["id"], "src": host["id"], "dst": dest_host["id"]}) break return actions
def get_unused_hypervisors(env): # Returns list of unused hypervisors (no_vms = 0) return tools_mod.search_dictionaries("no_vms", 0, env["hosts"])