def test_get_emptiest_package(self): t0 = Thread(0) t1 = Thread(1) t2 = Thread(2) t3 = Thread(3) t4 = Thread(4) t5 = Thread(5) t6 = Thread(6) t7 = Thread(7) p0 = Package(0, [Core(0, [t0, t4]), Core(1, [t1, t5])]) p1 = Package(1, [Core(0, [t2, t6]), Core(1, [t3, t7])]) cpu = Cpu([p0, p1]) # The first package should be the emptiest self.assertEqual(p0, cpu.get_emptiest_package()) # The second package should be the emptiest after we claim a thread on the first t5.claim(uuid.uuid4()) self.assertEqual(p1, cpu.get_emptiest_package()) # The first package should be the emptiest again, after we release the claimed thread t5.clear() self.assertEqual(p0, cpu.get_emptiest_package()) # The first package should be emptiest when we claim a thread on the second t3.claim(uuid.uuid4()) self.assertEqual(p0, cpu.get_emptiest_package()) # When an equal number of threads are claimed on both packages, the first should be returned t4.claim(uuid.uuid4()) self.assertEqual(p0, cpu.get_emptiest_package())
def is_core_below_threshold(threshold: float, core: Core, workload_usage: Dict[str, float], workload_map: Dict[str, Workload]): workload_ids = [] for t in core.get_threads(): workload_ids += t.get_workload_ids() static_usage = 0 static_workload_ids = [] for w_id in workload_ids: workload = workload_map.get(w_id, None) if workload is not None and workload.get_type() == STATIC: static_usage += \ workload_usage.get(w_id, workload.get_thread_count()) / workload.get_thread_count() static_workload_ids.append(w_id) is_free = static_usage <= threshold if is_free: log.info("Core: {} with usage: {} is UNDER threshold: {}".format( core.get_id(), static_usage, threshold)) else: log.info("Core: {} with usage: {} is OVER threshold: {}".format( core.get_id(), static_usage, threshold)) return is_free
def test_construction(self): core0 = Core(0, [Thread(0), Thread(2)]) core1 = Core(1, [Thread(1), Thread(3)]) cores = [core0, core1] package = Package(0, cores) self.assertEqual(2, len(package.get_cores())) self.assertEqual(cores, package.get_cores())
def test_construction(self): thread0 = Thread(0) thread1 = Thread(1) core = Core(42, [thread0, thread1]) self.assertEqual(42, core.get_id()) self.assertEqual(2, len(core.get_threads())) self.assertEqual(thread0, core.get_threads()[0]) self.assertEqual(thread1, core.get_threads()[1])
def test_construction(self): p0 = Package(0, [ Core(0, [Thread(0), Thread(4)]), Core(1, [Thread(1), Thread(5)])]) p1 = Package(1, [ Core(0, [Thread(2), Thread(6)]), Core(1, [Thread(3), Thread(7)])]) packages = [p0, p1] cpu = Cpu(packages) self.assertEqual(packages, cpu.get_packages())
def test_get_empty_threads(self): t0 = Thread(0) t1 = Thread(1) t2 = Thread(2) t3 = Thread(3) c0 = Core(0, [t0, t2]) c1 = Core(1, [t1, t3]) p = Package(1, [c0, c1]) self.assertEqual([t0, t2, t1, t3], p.get_empty_threads()) t1.claim(uuid.uuid4()) self.assertEqual([t0, t2, t3], p.get_empty_threads()) t1.clear() self.assertEqual([t0, t2, t1, t3], p.get_empty_threads())
def test_equality(self): t_0_0 = Thread(0) t_0_1 = Thread(1) c_x = Core(0, [t_0_0, t_0_1]) t_1_0 = Thread(0) t_1_1 = Thread(1) c_y = Core(0, [t_1_0, t_1_1]) self.assertEqual(c_x, c_y) t_0_1.claim("a") self.assertNotEqual(c_x, c_y) t_1_1.claim("a") self.assertEqual(c_x, c_y) t_0_0.claim("b") t_1_0.claim("b") self.assertEqual(c_x, c_y)
def is_core_below_threshold(threshold: float, core: Core, workload_usage: Dict[str, float], workload_map: Dict[str, Workload]): # Get all workload IDs running on this core workload_ids = [] for t in core.get_threads(): workload_ids += t.get_workload_ids() # Measure the usage of all _static_ workloads running on this core in proportion to their requested thread count. # # e.g. # w_id | usage # a | 2% # b | 3% # c | 1% # ------------- # total | 6% usage = 0 for w_id in workload_ids: workload = workload_map.get(w_id, None) if workload is not None and workload.get_type() == STATIC: usage += workload_usage.get( w_id, workload.get_thread_count()) / workload.get_thread_count() # If we set the threshold to 10% and continue the example above # # e.g. # is_free = 6% <= 10% # is_free = true is_free = usage <= threshold if is_free: log.debug("Core: {} with usage: {} is UNDER threshold: {}".format( core.get_id(), usage, threshold)) else: log.debug("Core: {} with usage: {} is OVER threshold: {}".format( core.get_id(), usage, threshold)) return is_free
def test_equality(self): t_0_0 = Thread(0) t_0_1 = Thread(1) c_x = Core(0, [t_0_0, t_0_1]) p_x = Package(0, [c_x]) cpu_x = Cpu([p_x]) t_1_0 = Thread(0) t_1_1 = Thread(1) c_y = Core(0, [t_1_0, t_1_1]) p_y = Package(0, [c_y]) cpu_y = Cpu([p_y]) self.assertEqual(cpu_x, cpu_y) t_0_1.claim("a") self.assertNotEqual(cpu_x, cpu_y) t_1_1.claim("a") self.assertEqual(cpu_x, cpu_y) t_0_0.claim("b") t_1_0.claim("b") self.assertEqual(cpu_x, cpu_y)
def parse_cpu(cpu_dict: dict) -> Cpu: packages = [] for p in cpu_dict["packages"]: cores = [] for c in p["cores"]: threads = [] for t in c["threads"]: thread = Thread(t["id"]) for w_id in t["workload_id"]: thread.claim(w_id) threads.append(thread) cores.append(Core(c["id"], threads)) packages.append(Package(p["id"], cores)) return Cpu(packages)
def get_cpu(package_count=DEFAULT_PACKAGE_COUNT, cores_per_package=DEFAULT_CORE_COUNT, threads_per_core=DEFAULT_THREAD_COUNT): packages = [] for p_i in range(package_count): cores = [] for c_i in range(cores_per_package): cores.append( Core( c_i, __get_threads(p_i, c_i, package_count, cores_per_package, threads_per_core))) packages.append(Package(p_i, cores)) return Cpu(packages)
def get_cpu( package_count=DEFAULT_PACKAGE_COUNT, cores_per_package=DEFAULT_CORE_COUNT, threads_per_core=DEFAULT_THREAD_COUNT): print("package count: " + str(package_count) + " cores_per_package:" + str(cores_per_package) + " threads_per_core:" + str(threads_per_core)) packages = [] for p_i in range(package_count): cores = [] for c_i in range(cores_per_package): cores.append( Core(c_i, __get_threads(p_i, c_i, package_count, cores_per_package, threads_per_core))) packages.append(Package(p_i, cores)) print("-------------") print(Cpu(packages)) print(Cpu(packages).get_threads()) print(len(Cpu(packages).get_threads())) print("-------------") return Cpu(packages)
def test_get_emptiest_core(self): c0 = Core(0, [Thread(0), Thread(2)]) c1 = Core(1, [Thread(1), Thread(3)]) # The first core should be the emptiest p = Package(0, [c0, c1]) self.assertEqual(c0, get_emptiest_core(p)) # The second core should be emptiest after we claim a thread on the first c0.get_threads()[0].claim(uuid.uuid4()) self.assertEqual(c1, get_emptiest_core(p)) # The first core should be the emptiest again, after we release the claimed thread c0.get_threads()[0].clear() self.assertEqual(c0, get_emptiest_core(p)) # The first core should be emptiest when we claim a thread on the second c1.get_threads()[1].claim(uuid.uuid4()) self.assertEqual(c0, get_emptiest_core(p)) # When an equal number of threads are claimed on both cores, the first should be returned c0.get_threads()[1].claim(uuid.uuid4()) self.assertEqual(c0, get_emptiest_core(p))
def test_get_empty_threads(self): c = Core(1, [Thread(0), Thread(1)]) self.assertEqual(c.get_threads(), c.get_empty_threads()) t0 = c.get_threads()[0] t1 = c.get_threads()[1] c.get_threads()[0].claim(uuid.uuid4()) self.assertEqual([t1], c.get_empty_threads()) t0.clear() self.assertEqual(c.get_threads(), c.get_empty_threads()) t1.claim(uuid.uuid4()) self.assertEqual([t0], c.get_empty_threads()) t0.claim(uuid.uuid4()) self.assertEqual([], c.get_empty_threads())
def test_invalid_core(self): with self.assertRaises(ValueError): Core(0, [])