def modify(self, index=None): if index is None: print(red("尝试修改全部")) [pi.modify() for pi in self.phy_table] else: self.phy_table[index].modify()
def get_level_message(self, record): separator = ': ' if record.levelno == logging.WARNING: return colors.yellow(record.levelname) + separator if record.levelno == logging.ERROR: return colors.red(record.levelname) + separator return ''
def parallel_execute(objects, func, get_name, msg, get_deps=None, limit=None): """Runs func on objects in parallel while ensuring that func is ran on object only after it is ran on all its dependencies. get_deps called on object must return a collection with its dependencies. get_name called on object must return its name. """ objects = list(objects) stream = get_output_stream(sys.stderr) writer = ParallelStreamWriter(stream, msg) for obj in objects: writer.add_object(get_name(obj)) writer.write_initial() events = parallel_execute_iter(objects, func, get_deps, limit) errors = {} results = [] error_to_reraise = None for obj, result, exception in events: if exception is None: writer.write(get_name(obj), green('done')) results.append(result) elif isinstance(exception, APIError): errors[get_name(obj)] = exception.explanation writer.write(get_name(obj), red('error')) elif isinstance(exception, (OperationFailedError, HealthCheckFailed, NoHealthCheckConfigured)): errors[get_name(obj)] = exception.msg writer.write(get_name(obj), red('error')) elif isinstance(exception, UpstreamError): writer.write(get_name(obj), red('error')) else: errors[get_name(obj)] = exception error_to_reraise = exception for obj_name, error in errors.items(): stream.write("\nERROR: for {} {}\n".format(obj_name, error)) if error_to_reraise: raise error_to_reraise return results, errors
def main(): use_clock = False mem, page_reqs = load_test_data() print(red('####程序开始####')) print('初始内存状态为:') print(mem) choice = input('请选择调度算法 0.Clock 1.改进型Clock :') if choice == '0': use_clock = True print('您选择了', 'Clock' if use_clock else '改进型Clock') for req in page_reqs: # 遍历请求 print('-' * 10) print(f"\n{magenta('->')} requst: {req}\n") phy_no = None phyaddr: PhyAddr for p in mem.phy_table: if req.get_page_no() == p.page_no: # 该地址在内存中, 直接访问即可 # 获取对应物理块号 phy_no = mem.phy_table.index(p) mem.handle_req(req, phy_no) if phy_no is not None: phyaddr = mem.get_phy_addr(req) print(blue("命中内存")) print(f"{green(phyaddr)}") else: print(f"该地址不在内存块中, {red('发生缺页中断')}") if mem.load_free(req.get_page_no()): print(blue("成功载入空闲内存")) phyaddr = mem.get_phy_addr(req) print(f"{green(phyaddr)}") mem.handle_req(req, phyaddr.Block) else: # 尝试替换 # clock if use_clock: to_replace: int = clock(mem) if to_replace < 0: to_replace = clock(mem) else: to_replace: int = clock_advanced(mem) if to_replace < 0: to_replace = clock_advanced(mem) print(blue(f"成功替换物理块{to_replace}")) mem.replace(to_replace, req.get_page_no()) mem.handle_req(req, to_replace) # print(mem) phyaddr = mem.get_phy_addr(req) print(f"{green(phyaddr)}") print('当前内存状态为:') print(mem)
def replace(self, to_replace: int, new_page: int): if self.phy_table[to_replace].page_no is None: print(red("不能替换空闲页!")) return False replace_item = self.phy_table[to_replace] # 让队头指针指向需要替换页面的下一个页面, 下一次就从那个页面开始替换 # 然后原来第一个页面就自动变成队列的尾部(因为是循环队列/链表) self.replace_ptr = replace_item.pointer # print(magenta(self)) # 将新页面换入, 并让其指向队头(就是原来的下一个) self.phy_table[to_replace] = PageItem(page_no=new_page, accessed=False, modified=False, pointer=replace_item.pointer) return True
def error(msg): # type: (str) -> None print (colors.red(msg))
def test_format_unicode_error(self): message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95' output = self.formatter.format(make_log_record(logging.ERROR, message)) expected = colors.red('ERROR') + ': ' assert output == '{0}{1}'.format(expected, message.decode('utf-8'))
def test_format_error(self): output = self.formatter.format(make_log_record(logging.ERROR)) expected = colors.red('ERROR') + ': ' assert output == expected + MESSAGE
def access(self, index=None): if index is None: print(red("尝试访问全部")) [pi.access() for pi in self.phy_table] else: self.phy_table[index].access()
def error(msg): print(colors.red(msg))