| 67 | } |
| 68 | |
| 69 | void test_balance_imbalanced_load(void* /*arg*/) { |
| 70 | klog::Info("=== Balance: Imbalanced Load Test ==="); |
| 71 | |
| 72 | g_imbalance_done = 0; |
| 73 | g_cores_used_mask = 0; |
| 74 | bool passed = true; |
| 75 | |
| 76 | auto& tm = TaskManagerSingleton::instance(); |
| 77 | auto* self = tm.GetCurrentTask(); |
| 78 | |
| 79 | // All workers are unpinned (default affinity = UINT64_MAX). |
| 80 | // AddTask() places them on the caller's current core, creating a heavy |
| 81 | // imbalance that Balance() should correct. |
| 82 | for (int i = 0; i < kImbalanceWorkerCount; ++i) { |
| 83 | auto task = kstd::make_unique<TaskControlBlock>("BalWorker", 10, |
| 84 | imbalance_worker, nullptr); |
| 85 | task->aux->parent_pid = self->pid; |
| 86 | task->aux->pgid = self->aux->pgid; |
| 87 | tm.AddTask(std::move(task)); |
| 88 | } |
| 89 | |
| 90 | // Wait for all workers to finish |
| 91 | int timeout = 600; |
| 92 | while (timeout > 0 && g_imbalance_done.load(std::memory_order_acquire) < |
| 93 | kImbalanceWorkerCount) { |
| 94 | (void)sys_sleep(50); |
| 95 | timeout--; |
| 96 | } |
| 97 | |
| 98 | if (g_imbalance_done.load() != kImbalanceWorkerCount) { |
| 99 | klog::Err( |
| 100 | "test_balance_imbalanced_load: FAIL — only {}/{} workers finished", |
| 101 | g_imbalance_done.load(), kImbalanceWorkerCount); |
| 102 | passed = false; |
| 103 | } |
| 104 | |
| 105 | // With 8 workers all initially on one core and Balance() active, |
| 106 | // tasks should have been migrated to run on more than one core. |
| 107 | uint64_t mask = g_cores_used_mask.load(std::memory_order_acquire); |
| 108 | int cores_used = __builtin_popcountll(mask); |
| 109 | if (cores_used < 2) { |
| 110 | klog::Err( |
| 111 | "test_balance_imbalanced_load: FAIL — tasks only used {} core(s), " |
| 112 | "expected >= 2 (mask={:#x})", |
| 113 | cores_used, mask); |
| 114 | passed = false; |
| 115 | } else { |
| 116 | klog::Info( |
| 117 | "test_balance_imbalanced_load: tasks used {} core(s) (mask={:#x})", |
| 118 | cores_used, mask); |
| 119 | } |
| 120 | |
| 121 | if (passed) { |
| 122 | klog::Info("Balance: Imbalanced Load Test: PASSED"); |
| 123 | } else { |
| 124 | klog::Err("Balance: Imbalanced Load Test: FAILED"); |
| 125 | g_tests_failed++; |
| 126 | } |