コード例 #1
0
def piping_synthesis(vm_name):
    disk_url, mem_url, base_disk, base_mem, os_type = get_download_url(vm_name)
    prev = datetime.now()
    tmp_dir = '/tmp/'
    time_transfer = Queue()
    time_decomp = Queue()
    time_delta = Queue()

    print "[INFO] Chunk size : %d" % (CHUNK_SIZE)

    # handling disk overlay
    disk_download_queue = JoinableQueue()
    disk_decomp_queue = JoinableQueue()
    (disk_download_pipe_in, disk_download_pipe_out) = Pipe()
    (disk_decomp_pipe_in, disk_decomp_pipe_out) = Pipe()
    disk_out_filename = os.path.join(tmp_dir, disk_url.split("/")[-1] + ".recover")
    url = urllib2.urlopen(disk_url)
    disk_download_process = Process(target=network_worker, args=(url, disk_download_queue, time_transfer, CHUNK_SIZE))
    disk_decomp_process = Process(target=decomp_worker, args=(disk_download_queue, disk_decomp_queue, time_decomp))
    disk_delta_process = Process(target=delta_worker, args=(disk_decomp_queue, time_delta, base_disk, disk_out_filename))

    # handling memory overlay
    mem_download_queue = JoinableQueue()
    mem_decomp_queue = JoinableQueue()
    (mem_download_pipe_in, mem_download_pipe_out) = Pipe()
    (mem_decomp_pipe_in, mem_decomp_pipe_out) = Pipe()
    url = urllib2.urlopen(mem_url)
    mem_download_process = Process(target=network_worker, args=(url, mem_download_queue, time_transfer, CHUNK_SIZE))
    mem_decomp_process = Process(target=decomp_worker, args=(mem_download_queue, mem_decomp_queue, time_decomp))

    # memory snapshot result will be pipelined to KVM
    kvm_pipename = os.path.join(tmp_dir, mem_url.split("/")[-1] + ".fifo")
    if os.path.exists(kvm_pipename):
        os.unlink(kvm_pipename)
    os.mkfifo(kvm_pipename)
    mem_delta_process = Process(target=delta_worker_pipe, args=(mem_decomp_queue, time_delta, base_mem, kvm_pipename))
    
    # start processes
    disk_download_process.start()
    disk_download_process.join()
    disk_decomp_process.start()
    disk_delta_process.start()
    mem_download_process.start()
    mem_decomp_process.start()
    mem_delta_process.start()

    # Once disk is ready, start KVM
    # Memory snapshot will be completed by pipelining
    disk_delta_process.join()

    telnet_port = 9999
    vnc_port = 2
    exe_time = run_snapshot(disk_out_filename, kvm_pipename, telnet_port, vnc_port, wait_vnc_end=False, terminal_mode=True)
    print "[Time] VM Resume : %s" + exe_time
    print "\n[Time] Total Time except VM Resume : " + str(datetime.now()-prev)
    mem_delta_process.join()
コード例 #2
0
    def handle(self):
        # self.request is the TCP socket connected to the clinet
        data = self.request.recv(4)
        json_size = struct.unpack("!I", data)[0]

        # recv JSON header
        json_str = self.request.recv(json_size)
        json_data = json.loads(json_str)
        if 'VM' not in json_data or len(json_data['VM']) == 0:
            self.ret_fail("No VM Key at JSON")
            return

        vm_name = ''
        try:
            vm_name = json_data['VM'][0]['base_name']
            disk_size = int(json_data['VM'][0]['diskimg_size'])
            mem_size = int(json_data['VM'][0]['memory_snapshot_size'])
            #print "received info %s" % (vm_name)
        except KeyError:
            message = 'No key is in JSON'
            print message
            self.ret_fail(message)
            return

        print "[INFO] New client request %s VM (will transfer %d MB, %d MB)" % (vm_name, disk_size/1024/1024, mem_size/1024/1024)

        # check base VM
        base_disk_path = None
        base_mem_path = None
        for base_vm in BaseVM_list:
            if vm_name.lower() == base_vm['name'].lower():
                base_disk_path = base_vm['diskimg_path']
                base_mem_path = base_vm['memorysnapshot_path']
        if base_disk_path == None or base_mem_path == None:
            message = "Failed, No such base VM exist : %s" % (vm_name)
            self.wfile.write(message)            
            print message

        # read overlay files
        tmp_dir = tempfile.mkdtemp()
        time_transfer = Queue()
        time_decomp = Queue()
        time_delta = Queue()

        # check OS type
        # TODO: FIX this
        os_type = ''
        if base_disk_path.find('ubuntu') != -1:
            os_type = 'linux'
        else:
            os_type = 'window'

        start_time = datetime.now()
        # handling disk overlay
        disk_download_queue = JoinableQueue()
        disk_decomp_queue = JoinableQueue()
        (disk_download_pipe_in, disk_download_pipe_out) = Pipe()
        (disk_decomp_pipe_in, disk_decomp_pipe_out) = Pipe()
        disk_out_filename = os.path.join(tmp_dir, "disk.recover")
        disk_download_process = Process(target=network_worker, args=(self.rfile, disk_download_queue, time_transfer, CHUNK_SIZE, disk_size))
        disk_decomp_process = Process(target=decomp_worker, args=(disk_download_queue, disk_decomp_queue, time_decomp))
        disk_delta_process = Process(target=delta_worker, args=(disk_decomp_queue, time_delta, base_disk_path, disk_out_filename))

        # handling memory overlay
        mem_download_queue = JoinableQueue()
        mem_decomp_queue = JoinableQueue()
        (mem_download_pipe_in, mem_download_pipe_out) = Pipe()
        (mem_decomp_pipe_in, mem_decomp_pipe_out) = Pipe()
        mem_download_process = Process(target=network_worker, args=(self.rfile, mem_download_queue, time_transfer, CHUNK_SIZE, mem_size))
        mem_decomp_process = Process(target=decomp_worker, args=(mem_download_queue, mem_decomp_queue, time_decomp))
        # memory snapshot result will be pipelined to KVM
        kvm_pipename = os.path.join(tmp_dir, "mem.fifo")
        if os.path.exists(kvm_pipename):
            os.unlink(kvm_pipename)
        os.mkfifo(kvm_pipename)
        mem_delta_process = Process(target=delta_worker_pipe, args=(mem_decomp_queue, time_delta, base_mem_path, kvm_pipename))
        
        # start processes
        # wait for download disk first
        disk_download_process.start()
        disk_decomp_process.start()
        disk_delta_process.start()

        # Once disk is ready, start KVM
        # Memory snapshot will be completed by pipelining
        disk_delta_process.join()
        mem_download_process.start()
        mem_decomp_process.start()
        mem_delta_process.start()
        telnet_port = 9999
        vnc_port = 2
        exe_time = run_snapshot(disk_out_filename, kvm_pipename, telnet_port, vnc_port, wait_vnc_end=False, terminal_mode=True, os_type=os_type)
        kvm_end_time = datetime.now()

        mem_delta_process.join()

        # Print out Time Measurement
        disk_transfer_time = time_transfer.get()
        mem_transfer_time = time_transfer.get()
        disk_decomp_time = time_decomp.get()
        mem_decomp_time = time_decomp.get()
        disk_delta_time = time_delta.get()
        mem_delta_time = time_delta.get()
        disk_transfer_start_time = disk_transfer_time['start_time']
        disk_transfer_end_time = disk_transfer_time['end_time']
        disk_decomp_end_time = disk_decomp_time['end_time']
        disk_delta_end_time = disk_delta_time['end_time']
        mem_transfer_start_time = mem_transfer_time['start_time']
        mem_transfer_end_time = mem_transfer_time['end_time']
        mem_decomp_end_time = mem_decomp_time['end_time']
        mem_delta_end_time = mem_delta_time['end_time']

        transfer_diff = mem_transfer_end_time-disk_transfer_start_time
        decomp_diff = mem_decomp_end_time-mem_transfer_end_time
        delta_diff = mem_delta_end_time-mem_decomp_end_time
        kvm_diff = kvm_end_time-mem_delta_end_time
        total_diff = datetime.now()-start_time
        message = "\n"
        message += 'Transfer\tDecomp\tDelta\tBoot\tResume\tTotal\n'
        message += "%04d.%06d\t" % (transfer_diff.seconds, transfer_diff.microseconds)
        message += "%04d.%06d\t" % (decomp_diff.seconds, decomp_diff.microseconds)
        message += "%04d.%06d\t" % (delta_diff.seconds, delta_diff.microseconds)
        message += "%04d.%06d\t" % (kvm_diff.seconds, kvm_diff.microseconds)
        message += "%04d.%06d\t" % (total_diff.seconds, total_diff.microseconds)
        message += "\n"
        print message
        self.ret_success()