From 0b6c719c76d6bccc0b3c373e1b3d9476bbb5e972 Mon Sep 17 00:00:00 2001 From: damitha Date: Sat, 8 Mar 2025 08:14:55 +1100 Subject: [PATCH 1/4] . --- common/cpu.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/common/cpu.c b/common/cpu.c index fb7c07d53..8c35435cc 100644 --- a/common/cpu.c +++ b/common/cpu.c @@ -113,6 +113,79 @@ const x264_cpu_name_t x264_cpu_names[] = static sigjmp_buf jmpbuf; static volatile sig_atomic_t canjump = 0; +/** + * Detects CPU count from cgroups if available + * Returns the number of CPUs available according to cgroups limitations + * Returns 0 if cgroups detection fails or is not applicable + */ +static int x264_cpu_detect_cgroups( void ) +{ + int cpu_count = 0; + char buf[256]; + long quota = -1; + long period = -1; + int fd; + ssize_t len; + + // Try cgroup v2 first + fd = open( "/sys/fs/cgroup/cpu.max", O_RDONLY ); + if( fd >= 0 ) + { + // cgroup v2 format: "quota period" + len = read( fd, buf, sizeof(buf) - 1 ); + close( fd ); + if( len > 0 ) + { + buf[len] = '\0'; + if( sscanf( buf, "%ld %ld", "a, &period ) != 2 ) + { + quota = -1; + period = -1; + } + } + } + else + { + // Fall back to cgroup v1 + fd = open( "/sys/fs/cgroup/cpu/cpu.cfs_quota_us", O_RDONLY ); + if( fd >= 0 ) + { + len = read( fd, buf, sizeof(buf) - 1 ); + close( fd ); + if( len > 0 ) + { + buf[len] = '\0'; + quota = atol( buf ); + } + } + + fd = open( "/sys/fs/cgroup/cpu/cpu.cfs_period_us", O_RDONLY ); + if( fd >= 0 ) + { + len = read( fd, buf, sizeof(buf) - 1 ); + close( fd ); + if( len > 0 ) + { + buf[len] = '\0'; + period = atol( buf ); + } + } + } + + // Calculate CPU count based on quota and period + if( quota > 0 && period > 0 ) + { + cpu_count = (int)( quota / period ); + if( cpu_count < 1 ) + cpu_count = 1; + } + + return cpu_count; +} + +/** + + static void sigill_handler( int sig ) { if( !canjump ) @@ -502,6 +575,8 @@ uint32_t x264_cpu_detect( void ) int x264_cpu_num_processors( void ) { +int system_cpu_count = 0; +int cgroups_cpu_count = 0; #if !HAVE_THREAD return 1; @@ -547,4 +622,14 @@ int x264_cpu_num_processors( void ) #else return 1; #endif + + // Check for cgroups limitations + cgroups_cpu_count = x264_cpu_detect_cgroups(); + + // Use the lower of the two values if cgroups detection succeeded + if( cgroups_cpu_count > 0 && cgroups_cpu_count < system_cpu_count ) + return cgroups_cpu_count; + else + return system_cpu_count; + } From 7ca23fc23d8c3500a96fdf39a9c818224936d4ca Mon Sep 17 00:00:00 2001 From: damitha Date: Sat, 8 Mar 2025 08:24:28 +1100 Subject: [PATCH 2/4] . --- common/cpu.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/common/cpu.c b/common/cpu.c index 8c35435cc..a1e2289cf 100644 --- a/common/cpu.c +++ b/common/cpu.c @@ -596,7 +596,14 @@ int cgroups_cpu_count = 0; if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) ) return 1; #if HAVE_CPU_COUNT - return CPU_COUNT(&p_aff); + // Check for cgroups limitations + cgroups_cpu_count = x264_cpu_detect_cgroups(); + + // Use the lower of the two values if cgroups detection succeeded + if( cgroups_cpu_count > 0 ) + return cgroups_cpu_count; + else + return CPU_COUNT(&p_aff); #else int np = 0; for( size_t bit = 0; bit < 8 * sizeof(p_aff); bit++ ) @@ -623,13 +630,4 @@ int cgroups_cpu_count = 0; return 1; #endif - // Check for cgroups limitations - cgroups_cpu_count = x264_cpu_detect_cgroups(); - - // Use the lower of the two values if cgroups detection succeeded - if( cgroups_cpu_count > 0 && cgroups_cpu_count < system_cpu_count ) - return cgroups_cpu_count; - else - return system_cpu_count; - } From 5d32607721f11b17bf92a6122bb6470843f83cfd Mon Sep 17 00:00:00 2001 From: damitha Date: Sat, 8 Mar 2025 08:29:19 +1100 Subject: [PATCH 3/4] . --- common/cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/cpu.c b/common/cpu.c index a1e2289cf..c05ee02b7 100644 --- a/common/cpu.c +++ b/common/cpu.c @@ -118,7 +118,7 @@ static volatile sig_atomic_t canjump = 0; * Returns the number of CPUs available according to cgroups limitations * Returns 0 if cgroups detection fails or is not applicable */ -static int x264_cpu_detect_cgroups( void ) +int x264_cpu_detect_cgroups( void ) { int cpu_count = 0; char buf[256]; From c806f03516312318581558cfbf73a9c040ae9cf8 Mon Sep 17 00:00:00 2001 From: damitha Date: Sat, 8 Mar 2025 08:31:14 +1100 Subject: [PATCH 4/4] . --- common/cpu.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/cpu.c b/common/cpu.c index c05ee02b7..c7079cab9 100644 --- a/common/cpu.c +++ b/common/cpu.c @@ -112,6 +112,8 @@ const x264_cpu_name_t x264_cpu_names[] = #include static sigjmp_buf jmpbuf; static volatile sig_atomic_t canjump = 0; +int x264_cpu_detect_cgroups(void); + /** * Detects CPU count from cgroups if available