|
| 1 | +// Copyright(C) Microsoft Corporation.All rights reserved. |
| 2 | + |
| 3 | + |
| 4 | +#include <stdlib.h> |
| 5 | +#include <inttypes.h> |
| 6 | + |
| 7 | +#include "windows.h" |
| 8 | + |
| 9 | +#include "macro_utils/macro_utils.h" |
| 10 | + |
| 11 | +#include "testrunnerswitcher.h" |
| 12 | + |
| 13 | +#include "c_pal/gballoc_hl.h" |
| 14 | +#include "c_pal/gballoc_hl_redirect.h" |
| 15 | +#include "c_pal/job_object_helper.h" |
| 16 | +#include "c_pal/uuid.h" |
| 17 | + |
| 18 | +/* Complete the opaque struct definition so integration tests can access |
| 19 | + the internal job_object HANDLE for QueryInformationJobObject calls |
| 20 | + (needed for unnamed job objects where OpenJobObjectA is not available). */ |
| 21 | +struct JOB_OBJECT_HELPER_TAG { |
| 22 | + HANDLE job_object; |
| 23 | +}; |
| 24 | + |
| 25 | + |
| 26 | +#define MEGABYTE ((size_t)1024 * 1024) |
| 27 | +#define TEST_JOB_NAME_PREFIX "job_test_reconfig_" |
| 28 | +#define INITIAL_CPU_PERCENT 50 |
| 29 | +#define INITIAL_MEMORY_PERCENT 1 |
| 30 | +#define RECONFIGURED_CPU_PERCENT 30 |
| 31 | +#define RECONFIGURED_MEMORY_PERCENT 2 |
| 32 | + |
| 33 | +BEGIN_TEST_SUITE(TEST_SUITE_NAME_FROM_CMAKE) |
| 34 | + |
| 35 | +TEST_SUITE_INITIALIZE(suite_init) |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +TEST_SUITE_CLEANUP(suite_cleanup) |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +TEST_FUNCTION_INITIALIZE(init) |
| 44 | +{ |
| 45 | +} |
| 46 | + |
| 47 | +TEST_FUNCTION_CLEANUP(cleanup) |
| 48 | +{ |
| 49 | + job_object_helper_deinit_for_test(); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_FUNCTION(job_object_helper_set_job_limits_to_current_process_reconfigures_limits_for_named_job_object) |
| 53 | +{ |
| 54 | + /* This test verifies that: |
| 55 | + * 1. A job object is created with initial limits |
| 56 | + * 2. The limits can be reconfigured in-place via a second call |
| 57 | + * 3. The reconfigured limits are applied to the existing job object |
| 58 | + */ |
| 59 | + |
| 60 | + UUID_T job_name_uuid; |
| 61 | + (void)uuid_produce(&job_name_uuid); |
| 62 | + |
| 63 | + char job_name[64]; |
| 64 | + (void)snprintf(job_name, sizeof(job_name), TEST_JOB_NAME_PREFIX "%" PRI_UUID_T "", UUID_T_VALUES(job_name_uuid)); |
| 65 | + LogInfo("Running reconfigure test with job name: %s...", job_name); |
| 66 | + |
| 67 | + // Step 1: Create the singleton with initial limits (50% CPU, 1% memory) |
| 68 | + THANDLE(JOB_OBJECT_HELPER) initial_job_object_helper = job_object_helper_set_job_limits_to_current_process(job_name, INITIAL_CPU_PERCENT, INITIAL_MEMORY_PERCENT); |
| 69 | + ASSERT_IS_NOT_NULL(initial_job_object_helper); |
| 70 | + |
| 71 | + // Verify initial CPU rate |
| 72 | + HANDLE job_object = OpenJobObjectA(JOB_OBJECT_QUERY, FALSE, job_name); |
| 73 | + ASSERT_IS_NOT_NULL(job_object, "Failed to open job object"); |
| 74 | + |
| 75 | + JOBOBJECT_CPU_RATE_CONTROL_INFORMATION cpu_info = { 0 }; |
| 76 | + DWORD return_length = 0; |
| 77 | + BOOL query_result = QueryInformationJobObject(job_object, JobObjectCpuRateControlInformation, &cpu_info, sizeof(cpu_info), &return_length); |
| 78 | + ASSERT_IS_TRUE(query_result, "Failed to query CPU rate info"); |
| 79 | + ASSERT_ARE_EQUAL(uint32_t, INITIAL_CPU_PERCENT * 100, cpu_info.CpuRate, "Initial CPU rate should be %" PRIu32 "", INITIAL_CPU_PERCENT * 100); |
| 80 | + LogInfo("Initial CPU rate verified: %" PRIu32 "", cpu_info.CpuRate); |
| 81 | + |
| 82 | + // Verify initial memory limit |
| 83 | + MEMORYSTATUSEX mem_status; |
| 84 | + mem_status.dwLength = sizeof(mem_status); |
| 85 | + ASSERT_IS_TRUE(GlobalMemoryStatusEx(&mem_status)); |
| 86 | + SIZE_T expected_initial_memory_in_MB = INITIAL_MEMORY_PERCENT * mem_status.ullTotalPhys / 100 / MEGABYTE; |
| 87 | + |
| 88 | + JOBOBJECT_EXTENDED_LIMIT_INFORMATION ext_info = { 0 }; |
| 89 | + query_result = QueryInformationJobObject(job_object, JobObjectExtendedLimitInformation, &ext_info, sizeof(ext_info), &return_length); |
| 90 | + ASSERT_IS_TRUE(query_result, "Failed to query extended limit info"); |
| 91 | + ASSERT_ARE_EQUAL(size_t, expected_initial_memory_in_MB, ext_info.JobMemoryLimit / MEGABYTE, "Initial job memory limit should match"); |
| 92 | + LogInfo("Initial memory limit verified: %zu MB", ext_info.JobMemoryLimit / MEGABYTE); |
| 93 | + |
| 94 | + (void)CloseHandle(job_object); |
| 95 | + |
| 96 | + // Step 2: Reconfigure to new limits (30% CPU, 2% memory) |
| 97 | + THANDLE(JOB_OBJECT_HELPER) reconfigured_job_object_helper = job_object_helper_set_job_limits_to_current_process(job_name, RECONFIGURED_CPU_PERCENT, RECONFIGURED_MEMORY_PERCENT); |
| 98 | + ASSERT_IS_NOT_NULL(reconfigured_job_object_helper, "Reconfigure should succeed"); |
| 99 | + ASSERT_ARE_EQUAL(void_ptr, initial_job_object_helper, reconfigured_job_object_helper, "Reconfigured call should return same singleton"); |
| 100 | + |
| 101 | + // Step 3: Verify reconfigured CPU rate |
| 102 | + job_object = OpenJobObjectA(JOB_OBJECT_QUERY, FALSE, job_name); |
| 103 | + ASSERT_IS_NOT_NULL(job_object, "Failed to open job object after reconfigure"); |
| 104 | + |
| 105 | + (void)memset(&cpu_info, 0, sizeof(cpu_info)); |
| 106 | + query_result = QueryInformationJobObject(job_object, JobObjectCpuRateControlInformation, &cpu_info, sizeof(cpu_info), &return_length); |
| 107 | + ASSERT_IS_TRUE(query_result, "Failed to query CPU rate info after reconfigure"); |
| 108 | + ASSERT_ARE_EQUAL(uint32_t, RECONFIGURED_CPU_PERCENT * 100, cpu_info.CpuRate, "Reconfigured CPU rate should be %" PRIu32 "", RECONFIGURED_CPU_PERCENT * 100); |
| 109 | + LogInfo("Reconfigured CPU rate verified: %" PRIu32 "", cpu_info.CpuRate); |
| 110 | + |
| 111 | + // Verify reconfigured memory limit |
| 112 | + SIZE_T expected_reconfigured_memory_in_MB = RECONFIGURED_MEMORY_PERCENT * mem_status.ullTotalPhys / 100 / MEGABYTE; |
| 113 | + |
| 114 | + (void)memset(&ext_info, 0, sizeof(ext_info)); |
| 115 | + query_result = QueryInformationJobObject(job_object, JobObjectExtendedLimitInformation, &ext_info, sizeof(ext_info), &return_length); |
| 116 | + ASSERT_IS_TRUE(query_result, "Failed to query extended limit info after reconfigure"); |
| 117 | + ASSERT_ARE_EQUAL(size_t, expected_reconfigured_memory_in_MB, ext_info.JobMemoryLimit / MEGABYTE, "Reconfigured job memory limit should match"); |
| 118 | + LogInfo("Reconfigured memory limit verified: %zu MB", ext_info.JobMemoryLimit / MEGABYTE); |
| 119 | + |
| 120 | + (void)CloseHandle(job_object); |
| 121 | + |
| 122 | + // Cleanup |
| 123 | + THANDLE_ASSIGN(JOB_OBJECT_HELPER)(&initial_job_object_helper, NULL); |
| 124 | + THANDLE_ASSIGN(JOB_OBJECT_HELPER)(&reconfigured_job_object_helper, NULL); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_FUNCTION(job_object_helper_set_job_limits_to_current_process_reconfigures_limits_for_unnamed_job_object) |
| 128 | +{ |
| 129 | + /* This test verifies reconfiguration works for unnamed job objects (NULL job_name). |
| 130 | + * Since OpenJobObjectA cannot be used with unnamed objects, we access the |
| 131 | + * internal job_object HANDLE from the THANDLE to query limits directly. |
| 132 | + */ |
| 133 | + |
| 134 | + LogInfo("Running reconfigure test with NULL job name (unnamed job object)..."); |
| 135 | + |
| 136 | + // Step 1: Create the singleton with initial limits |
| 137 | + THANDLE(JOB_OBJECT_HELPER) initial_job_object_helper = job_object_helper_set_job_limits_to_current_process(NULL, INITIAL_CPU_PERCENT, INITIAL_MEMORY_PERCENT); |
| 138 | + ASSERT_IS_NOT_NULL(initial_job_object_helper); |
| 139 | + |
| 140 | + // Verify initial CPU rate via internal handle |
| 141 | + JOBOBJECT_CPU_RATE_CONTROL_INFORMATION cpu_info = { 0 }; |
| 142 | + DWORD return_length = 0; |
| 143 | + BOOL query_result = QueryInformationJobObject(initial_job_object_helper->job_object, JobObjectCpuRateControlInformation, &cpu_info, sizeof(cpu_info), &return_length); |
| 144 | + ASSERT_IS_TRUE(query_result, "Failed to query CPU rate info"); |
| 145 | + ASSERT_ARE_EQUAL(uint32_t, INITIAL_CPU_PERCENT * 100, cpu_info.CpuRate, "Initial CPU rate should be %" PRIu32 "", INITIAL_CPU_PERCENT * 100); |
| 146 | + LogInfo("Initial CPU rate verified: %" PRIu32 "", cpu_info.CpuRate); |
| 147 | + |
| 148 | + // Verify initial memory limit |
| 149 | + MEMORYSTATUSEX mem_status; |
| 150 | + mem_status.dwLength = sizeof(mem_status); |
| 151 | + ASSERT_IS_TRUE(GlobalMemoryStatusEx(&mem_status)); |
| 152 | + SIZE_T expected_initial_memory_in_MB = INITIAL_MEMORY_PERCENT * mem_status.ullTotalPhys / 100 / MEGABYTE; |
| 153 | + |
| 154 | + JOBOBJECT_EXTENDED_LIMIT_INFORMATION ext_info = { 0 }; |
| 155 | + query_result = QueryInformationJobObject(initial_job_object_helper->job_object, JobObjectExtendedLimitInformation, &ext_info, sizeof(ext_info), &return_length); |
| 156 | + ASSERT_IS_TRUE(query_result, "Failed to query extended limit info"); |
| 157 | + ASSERT_ARE_EQUAL(size_t, expected_initial_memory_in_MB, ext_info.JobMemoryLimit / MEGABYTE, "Initial job memory limit should match"); |
| 158 | + LogInfo("Initial memory limit verified: %zu MB", ext_info.JobMemoryLimit / MEGABYTE); |
| 159 | + |
| 160 | + // Step 2: Reconfigure to new limits |
| 161 | + THANDLE(JOB_OBJECT_HELPER) reconfigured_job_object_helper = job_object_helper_set_job_limits_to_current_process(NULL, RECONFIGURED_CPU_PERCENT, RECONFIGURED_MEMORY_PERCENT); |
| 162 | + ASSERT_IS_NOT_NULL(reconfigured_job_object_helper, "Reconfigure should succeed"); |
| 163 | + ASSERT_ARE_EQUAL(void_ptr, initial_job_object_helper, reconfigured_job_object_helper, "Reconfigured call should return same singleton"); |
| 164 | + |
| 165 | + // Step 3: Verify reconfigured CPU rate |
| 166 | + (void)memset(&cpu_info, 0, sizeof(cpu_info)); |
| 167 | + query_result = QueryInformationJobObject(reconfigured_job_object_helper->job_object, JobObjectCpuRateControlInformation, &cpu_info, sizeof(cpu_info), &return_length); |
| 168 | + ASSERT_IS_TRUE(query_result, "Failed to query CPU rate info after reconfigure"); |
| 169 | + ASSERT_ARE_EQUAL(uint32_t, RECONFIGURED_CPU_PERCENT * 100, cpu_info.CpuRate, "Reconfigured CPU rate should be %" PRIu32 "", RECONFIGURED_CPU_PERCENT * 100); |
| 170 | + LogInfo("Reconfigured CPU rate verified: %" PRIu32 "", cpu_info.CpuRate); |
| 171 | + |
| 172 | + // Verify reconfigured memory limit |
| 173 | + SIZE_T expected_reconfigured_memory_in_MB = RECONFIGURED_MEMORY_PERCENT * mem_status.ullTotalPhys / 100 / MEGABYTE; |
| 174 | + |
| 175 | + (void)memset(&ext_info, 0, sizeof(ext_info)); |
| 176 | + query_result = QueryInformationJobObject(reconfigured_job_object_helper->job_object, JobObjectExtendedLimitInformation, &ext_info, sizeof(ext_info), &return_length); |
| 177 | + ASSERT_IS_TRUE(query_result, "Failed to query extended limit info after reconfigure"); |
| 178 | + ASSERT_ARE_EQUAL(size_t, expected_reconfigured_memory_in_MB, ext_info.JobMemoryLimit / MEGABYTE, "Reconfigured job memory limit should match"); |
| 179 | + LogInfo("Reconfigured memory limit verified: %zu MB", ext_info.JobMemoryLimit / MEGABYTE); |
| 180 | + |
| 181 | + // Cleanup |
| 182 | + THANDLE_ASSIGN(JOB_OBJECT_HELPER)(&initial_job_object_helper, NULL); |
| 183 | + THANDLE_ASSIGN(JOB_OBJECT_HELPER)(&reconfigured_job_object_helper, NULL); |
| 184 | +} |
| 185 | + |
| 186 | +END_TEST_SUITE(TEST_SUITE_NAME_FROM_CMAKE) |
0 commit comments