Beispiel #1
0
    def print_table(self):
        table = PrettyTable()
        table.title = (
            f"Cache Table {self.correspondance} {self.nway_size} - " +
            f"{self.replace_criteria}")

        table.field_names = [
            "Index", "Section", "Valid", "Tag", "Direction", "Order", "Access",
            "Time"
        ]

        table.align["Tag"] = "l"

        for index, entry in enumerate(self.table):
            if entry is None:
                continue

            tag = f"{entry.tag} ({binary_to_decimal(entry.tag + '0')})"
            direction = binary_to_decimal(
                decimal_to_binary(entry.section) + entry.tag + "0")

            table.add_row([
                index, entry.section,
                int(entry.valid), tag, direction, entry.fifo_order,
                entry.access_counter, entry.last_access
            ])

        print(table)
Beispiel #2
0
    def invalidate_range(self, range_min, range_max):
        for entry in self.table:
            if entry is None:
                continue

            section_binary = decimal_to_binary(entry.section,
                                               self.bits_sections)
            dir_decimal = binary_to_decimal(section_binary + entry.tag + "0")
            if range_min <= dir_decimal < range_max:
                entry.valid = False
Beispiel #3
0
    def process_memory_access(self, memory_access):
        memory_access_bin = decimal_to_binary(memory_access,
                                              self.bits_address - 1)
        if self.cache.read(memory_access_bin):
            #self.curr_stats.mark_memory_write()
            # Happy path!
            # print("Cache Hit!")
            self.curr_stats.mark_cache_hit()
            return

        self.curr_stats.mark_cache_miss()
        self.curr_stats.mark_memory_read()  # Aumenta read count
        self.cache.write(memory_access_bin)
        self.curr_stats.mark_cache_write()
def brute_force(file):
	# Open the file specified by the user and read all the records.
	fhandler = open(file, "r")

	# Find the correct keys for the first pair of ciphertext and plaintext.
	first_line = fhandler.readline()
	first_pt = first_line[:8]
	first_ct = first_line[9:17]
	possible_keys = []
	for x in range(pow(2,10)):
		key = decimal_to_binary(x)[-10:]
		key = ''.join([str(elem) for elem in key])
		output = transform(ENCRYPTION, key, first_pt, False)

		if first_ct == output:
			possible_keys.append(key)

	print(possible_keys)
	
	key_found = False
	final_key = ""
	for key in possible_keys:
		for line in fhandler:
			plaintext = line[:8]
			real_ciphertext = line[9:17]
			ciphertext = transform(ENCRYPTION, key, plaintext, False)
			# print(line)
			# print("Real cipher {} and cipher {}".format(real_ciphertext,ciphertext))

			if real_ciphertext != ciphertext:
				key_found = False
				final_key = ""
				break
			elif real_ciphertext == ciphertext and key_found is False:
				key_found = True
				final_key = key
				continue
		if key_found is True:
			break
	
	print("The perfect key is {}".format(final_key))
Beispiel #5
0
    def memory_address_from_frame_offset(self, frame, offset):
        frame_bin = decimal_to_binary(frame, self.bits_page)
        offset_bin = decimal_to_binary(offset,
                                       self.bits_address - self.bits_page)

        return binary_to_decimal(frame_bin + offset_bin)
Beispiel #6
0
    def process_virtual_access(self, virtual_access):
        program = self.curr_program

        binary_address = decimal_to_binary(virtual_access, self.bits_address)

        page_binary = binary_address[:self.bits_page]
        offset_binary = binary_address[self.bits_page:]

        page_decimal = binary_to_decimal(page_binary)
        offset_decimal = binary_to_decimal(offset_binary)

        # print(f"Pagina {page_decimal} | Offset: {offset_decimal}")

        if self.tlb.read(page_decimal):
            self.curr_stats.mark_tlb_hit()
            frame = self.tlb.get_element(page_decimal)
            # print(f"TLB Hit: {page_decimal} -> {frame}")
            return self.memory_address_from_frame_offset(frame, offset_decimal)

        # In case not in TLB
        self.curr_stats.mark_tlb_miss()
        page_table = program.page_table

        if page_table.map_present(page_decimal):
            self.curr_stats.mark_page_hit()
            mapped_frame = page_table.get_frame(page_decimal)
            # print(f"Page Table hit: {page_decimal} -> {mapped_frame}")
            self.tlb.write(page_decimal, mapped_frame)
            self.curr_stats.mark_tlb_write()
            return self.memory_address_from_frame_offset(
                mapped_frame, offset_decimal)

        self.curr_stats.mark_page_fault()
        # Request allocation

        if page_table.is_in_disk(page_decimal):
            # print("Page table in Disk")
            self.curr_stats.mark_swap_in()
            self.disk.pop(program.pid, page_decimal)
            page_table.mark_not_disk(page_decimal)

        # Store in memory the program variables
        frame, previous = self.memory.request_allocation(
            program.id, page_decimal)
        self.curr_stats.mark_memory_write()

        if previous is not None:
            ## Invalidar direcciones de cache!
            self.cache_invalidate_frame(frame)
            # print(f"Invalidating cache directions associated with frame {frame}")
            pid, program_page = previous
            prev_program = self.program_map[pid]
            prev_program.page_table.invalidate(frame)
            # print(f"Invalidating {prev_program.name} entry of Page Table")
            self.curr_stats.mark_swap_out()
            self.store_in_disk(pid, program_page)
            # print(f"Swaping out from frame {frame} and storing {self.program_map[pid].name}:{pid}, Pagina {program_page}")

        # Write page_table
        page_table.add_entry(page_decimal, frame)
        self.tlb.write(page_decimal, frame)
        self.curr_stats.mark_tlb_write()

        return self.memory_address_from_frame_offset(frame, offset_decimal)