tree-wide: trivial sysconf() invocations tweaks (#42837)

This commit is contained in:
Luca Boccassi
2026-07-01 19:42:52 +01:00
committed by GitHub
7 changed files with 21 additions and 24 deletions
+1 -1
View File
@@ -555,7 +555,7 @@ char* strv_env_get_n(char * const *l, const char *name, size_t k, ReplaceEnvFlag
const char *t;
/* Safety check that the name is not overly long, before we do a stack allocation */
if (k > (size_t) sysconf(_SC_ARG_MAX) - 2)
if (k > sc_arg_max() - 2)
return NULL;
t = strndupa_safe(name, k);
+3 -4
View File
@@ -8,15 +8,14 @@
size_t page_size(void) {
static thread_local size_t pgsz = 0;
long r;
if (_likely_(pgsz > 0))
return pgsz;
r = sysconf(_SC_PAGESIZE);
assert(r > 0);
long p = sysconf(_SC_PAGESIZE);
assert(p > 0);
pgsz = (size_t) r;
pgsz = (size_t) p;
return pgsz;
}
+3 -7
View File
@@ -131,7 +131,6 @@ int procfs_cpu_get_usage(nsec_t *ret) {
_cleanup_free_ char *first_line = NULL;
unsigned long user_ticks, nice_ticks, system_ticks, irq_ticks, softirq_ticks,
guest_ticks = 0, guest_nice_ticks = 0;
long ticks_per_second;
uint64_t sum, gcd, a, b;
const char *p;
int r;
@@ -156,10 +155,7 @@ int procfs_cpu_get_usage(nsec_t *ret) {
&guest_nice_ticks) < 5) /* we only insist on the first five fields */
return -EINVAL;
ticks_per_second = sysconf(_SC_CLK_TCK);
if (ticks_per_second < 0)
return -errno;
assert(ticks_per_second > 0);
uint64_t ticks_per_second = sysconf_clock_ticks_cached();
sum = (uint64_t) user_ticks + (uint64_t) nice_ticks + (uint64_t) system_ticks +
(uint64_t) irq_ticks + (uint64_t) softirq_ticks +
@@ -168,8 +164,8 @@ int procfs_cpu_get_usage(nsec_t *ret) {
/* Let's reduce this fraction before we apply it to avoid overflows when converting this to μsec */
gcd = calc_gcd64(NSEC_PER_SEC, ticks_per_second);
a = (uint64_t) NSEC_PER_SEC / gcd;
b = (uint64_t) ticks_per_second / gcd;
a = NSEC_PER_SEC / gcd;
b = ticks_per_second / gcd;
*ret = DIV_ROUND_UP((nsec_t) sum * (nsec_t) a, (nsec_t) b);
return 0;
+9 -10
View File
@@ -1766,27 +1766,26 @@ int localtime_or_gmtime_usec(
return 0;
}
static uint32_t sysconf_clock_ticks_cached(void) {
static thread_local uint32_t hz = 0;
long r;
uint64_t sysconf_clock_ticks_cached(void) {
static thread_local uint64_t hz = 0;
if (hz == 0) {
r = sysconf(_SC_CLK_TCK);
if (hz != 0)
return hz;
assert(r > 0);
hz = r;
}
long t = sysconf(_SC_CLK_TCK);
assert(t > 0);
hz = (uint64_t) t;
return hz;
}
uint32_t usec_to_jiffies(usec_t u) {
uint32_t hz = sysconf_clock_ticks_cached();
uint64_t hz = sysconf_clock_ticks_cached();
return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
}
usec_t jiffies_to_usec(uint32_t j) {
uint32_t hz = sysconf_clock_ticks_cached();
uint64_t hz = sysconf_clock_ticks_cached();
return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
}
+2
View File
@@ -201,6 +201,8 @@ static inline int parse_birth_date(const char *s, struct tm *ret) {
return parse_calendar_date_full(s, /* allow_pre_epoch= */ true, NULL, ret);
}
uint64_t sysconf_clock_ticks_cached(void);
uint32_t usec_to_jiffies(usec_t usec);
usec_t jiffies_to_usec(uint32_t jiffies);
+1 -1
View File
@@ -922,7 +922,7 @@ static int get_supplementary_groups(
* be positive, otherwise fail.
*/
errno = 0;
int ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
long ngroups_max = sysconf(_SC_NGROUPS_MAX);
if (ngroups_max <= 0)
return errno_or_else(EOPNOTSUPP);
+2 -1
View File
@@ -1037,7 +1037,8 @@ TEST(condition_test_group) {
Condition *condition;
char gid[DECIMAL_STR_MAX(uint32_t)];
gid_t *gids, max_gid;
int ngroups_max, ngroups, r, i;
int ngroups, r, i;
long ngroups_max;
xsprintf(gid, "%u", UINT32_C(0xFFFF));
ASSERT_NOT_NULL((condition = condition_new(CONDITION_GROUP, gid, false, false)));