| 34 | } |
| 35 | |
| 36 | void test_many_tasks(void* /*arg*/) { |
| 37 | klog::Info("=== Stress: Many Tasks Test ==="); |
| 38 | |
| 39 | g_many_tasks_counter = 0; |
| 40 | |
| 41 | auto& tm = TaskManagerSingleton::instance(); |
| 42 | auto* self = tm.GetCurrentTask(); |
| 43 | if (!self) { |
| 44 | klog::Err("test_many_tasks: Cannot get current task"); |
| 45 | g_tests_failed++; |
| 46 | g_tests_completed++; |
| 47 | sys_exit(1); |
| 48 | } |
| 49 | |
| 50 | constexpr int kTaskCount = 20; |
| 51 | Pid pids[kTaskCount]; |
| 52 | |
| 53 | // Spawn all 20 tasks |
| 54 | for (int i = 0; i < kTaskCount; ++i) { |
| 55 | auto task = kstd::make_unique<TaskControlBlock>("StressWorker", 10, |
| 56 | many_tasks_work, nullptr); |
| 57 | task->aux->parent_pid = self->pid; |
| 58 | task->aux->pgid = self->aux->pgid; |
| 59 | auto* raw = task.get(); |
| 60 | tm.AddTask(std::move(task)); |
| 61 | pids[i] = raw->pid; |
| 62 | } |
| 63 | |
| 64 | // Wait for each task by PID |
| 65 | bool passed = true; |
| 66 | for (int i = 0; i < kTaskCount; ++i) { |
| 67 | int status = 0; |
| 68 | int timeout = 100; |
| 69 | while (timeout-- > 0) { |
| 70 | auto result = tm.Wait(pids[i], &status, false, false); |
| 71 | if (result.has_value() && result.value() == pids[i]) { |
| 72 | break; |
| 73 | } |
| 74 | (void)sys_sleep(10); |
| 75 | } |
| 76 | if (timeout <= 0) { |
| 77 | klog::Err("test_many_tasks: timed out waiting for task {}", pids[i]); |
| 78 | passed = false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (g_many_tasks_counter.load() != kTaskCount) { |
| 83 | klog::Err("test_many_tasks: FAIL - counter={}, expected {}", |
| 84 | g_many_tasks_counter.load(), kTaskCount); |
| 85 | passed = false; |
| 86 | } |
| 87 | |
| 88 | if (passed) { |
| 89 | klog::Info("Stress: Many Tasks Test: PASSED"); |
| 90 | } else { |
| 91 | klog::Err("Stress: Many Tasks Test: FAILED"); |
| 92 | g_tests_failed++; |
| 93 | } |