Skip to contentSkip to conversation

controllers/cpuacct: Dynamically adjust cpuacct_task execution time

PATCH In review Wake Liu via ltp 3 participants 8 messages Aug 10 – Aug 28

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 controllers/cpuacct: Dynamically adjust cpuacct_task execution time

The test helper cpuacct_task currently runs for 10ms (virtual time) before exiting. On some systems (e.g. fast virtual machines or under heavy load), this short duration can lead to 0 usage being recorded in cpuacct.usage cgroup file due to timing resolution or scheduling delay, especially when running small configurations like cpuacct_1_1.

However, unconditionally increasing the duration for all runs (e.g. to 100ms) would cause large stress tests like cpuacct_100_100 (which runs 10,000 tasks) to timeout on systems with few CPUs.

Resolve this by:
1. Modifying cpuacct_task to accept an optional duration argument.
2. Modifying cpuacct.sh to calculate the duration dynamically based on nbprocess, ensuring each subgroup gets at least 100ms of aggregate CPU time, while clamping the per-process duration to a minimum of 10ms to avoid timeouts in large stress tests.

---
 .../kernel/controllers/cpuacct/cpuacct.sh     |  7 ++++++-
 .../kernel/controllers/cpuacct/cpuacct_task.c | 20 ++++++++++++++++---
 2 files changed, 23 insertions(+), 4 deletions(-)
testcases/kernel/controllers/cpuacct/cpuacct.sh +6 -1
index 97a395cd5..efabe6cab 100755
@@ -139,10 +139,15 @@ do_test()
 {
 	tst_res TINFO "Creating $max subgroups each with $nbprocess processes"
 
+	local duration=$((100000 / nbprocess))
+	if [ $duration -lt 10000 ]; then
+		duration=10000
+	fi
+
 	# create and attach process to subgroups
 	for i in `seq 1 $max`; do
 		for j in `seq 1 $nbprocess`; do
-			cpuacct_task $testpath/subgroup_$i/tasks &
+			cpuacct_task $testpath/subgroup_$i/tasks $duration &
 			echo $! >> task_pids
 		done
 	done
testcases/kernel/controllers/cpuacct/cpuacct_task.c +17 -3
index 677d6d401..caeecf064 100644
@@ -38,12 +38,21 @@ int main(int argc, char **argv)
 {
 	FILE *f;
 	struct sigaction sa;
+	int duration_us = 10000;
 
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s /cgroup/.../tasks\n", argv[0]);
+	if (argc < 2 || argc > 3) {
+		fprintf(stderr, "Usage: %s /cgroup/.../tasks [duration_us]\n", argv[0]);
 		return 1;
 	}
 
+	if (argc == 3) {
+		duration_us = atoi(argv[2]);
+		if (duration_us <= 0) {
+			fprintf(stderr, "Invalid duration: %s\n", argv[2]);
+			return 1;
+		}
+	}
+
 	f = fopen(argv[1], "a");
 	if (!f) {
 		perror("fopen failed");
@@ -62,7 +71,12 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	struct itimerval it = {.it_value = {.tv_sec = 0, .tv_usec = 10000}};
+	struct itimerval it = {
+		.it_value = {
+			.tv_sec = duration_us / 1000000,
+			.tv_usec = duration_us % 1000000
+		}
+	};
 
 	setitimer(ITIMER_VIRTUAL, &it, NULL);
 	for (;;);
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Wake,

testcases/kernel/controllers/cpuacct/cpuacct.sh do_test() · line 142

Quoted 3 lines · Wake Liu

controllers/cpuacct: Dynamically adjust cpuacct_task execution time

testcases/kernel/controllers/cpuacct/cpuacct.sh +1 -0
+	local duration=$((100000 / nbprocess))

Could nbprocess be validated as a positive integer at the start of setup()? It comes directly from the second command-line argument, while TST_POS_ARGS checks only the argument count. With cpuacct.sh 1 0 (or nonnumeric input), dash treats the arithmetic error as fatal, so execution bypasses _tst_do_cleanup() after setup has created and possibly mounted the cgroup hierarchy, leaving those resources behind.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH 1/1 controllers/cpuacct: Dynamically adjust cpuacct_task execution time

In reply to Wake Liu via ltp

The test helper cpuacct_task currently runs for 10ms (virtual time) before exiting. On some systems (e.g. fast virtual machines or under heavy load), this short duration can lead to 0 usage being recorded in cpuacct.usage cgroup file due to timing resolution or scheduling delay, especially when running small configurations like cpuacct_1_1.

However, unconditionally increasing the duration for all runs (e.g. to 100ms) would cause large stress tests like cpuacct_100_100 (which runs 10,000 tasks) to timeout on systems with few CPUs.

Resolve this by:
1. Modifying cpuacct_task to accept an optional duration argument.
2. Modifying cpuacct.sh to calculate the duration dynamically based on nbprocess, ensuring each subgroup gets at least 100ms of aggregate CPU time, while clamping the per-process duration to a minimum of 10ms to avoid timeouts in large stress tests.
3. Validating nsubgroup and nprocess arguments in cpuacct.sh setup() to ensure they are positive integers, avoiding division by zero or fatal arithmetic errors in shell.

---
 .../kernel/controllers/cpuacct/cpuacct.sh     | 17 +++++++++++++++-
 .../kernel/controllers/cpuacct/cpuacct_task.c | 20 ++++++++++++++++---
 2 files changed, 33 insertions(+), 4 deletions(-)
testcases/kernel/controllers/cpuacct/cpuacct.sh +16 -1
index 97a395cd5..deb103a30 100755
@@ -84,6 +84,16 @@ check_limits()
 
 setup()
 {
+	case "$max" in
+		""|*[!0-9]*) tst_brk TBROK "nsubgroup must be a positive integer" ;;
+		*) [ "$max" -gt 0 ] || tst_brk TBROK "nsubgroup must be a positive integer" ;;
+	esac
+
+	case "$nbprocess" in
+		""|*[!0-9]*) tst_brk TBROK "nprocess must be a positive integer" ;;
+		*) [ "$nbprocess" -gt 0 ] || tst_brk TBROK "nprocess must be a positive integer" ;;
+	esac
+
 	if ! grep -q -w cpuacct /proc/cgroups; then
 		tst_brk TCONF "cpuacct not supported on this system"
 	fi
@@ -139,10 +149,15 @@ do_test()
 {
 	tst_res TINFO "Creating $max subgroups each with $nbprocess processes"
 
+	local duration=$((100000 / nbprocess))
+	if [ "$duration" -lt 10000 ]; then
+		duration=10000
+	fi
+
 	# create and attach process to subgroups
 	for i in `seq 1 $max`; do
 		for j in `seq 1 $nbprocess`; do
-			cpuacct_task $testpath/subgroup_$i/tasks &
+			cpuacct_task "$testpath/subgroup_$i/tasks" "$duration" &
 			echo $! >> task_pids
 		done
 	done
testcases/kernel/controllers/cpuacct/cpuacct_task.c +17 -3
index 677d6d401..caeecf064 100644
@@ -38,12 +38,21 @@ int main(int argc, char **argv)
 {
 	FILE *f;
 	struct sigaction sa;
+	int duration_us = 10000;
 
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s /cgroup/.../tasks\n", argv[0]);
+	if (argc < 2 || argc > 3) {
+		fprintf(stderr, "Usage: %s /cgroup/.../tasks [duration_us]\n", argv[0]);
 		return 1;
 	}
 
+	if (argc == 3) {
+		duration_us = atoi(argv[2]);
+		if (duration_us <= 0) {
+			fprintf(stderr, "Invalid duration: %s\n", argv[2]);
+			return 1;
+		}
+	}
+
 	f = fopen(argv[1], "a");
 	if (!f) {
 		perror("fopen failed");
@@ -62,7 +71,12 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	struct itimerval it = {.it_value = {.tv_sec = 0, .tv_usec = 10000}};
+	struct itimerval it = {
+		.it_value = {
+			.tv_sec = duration_us / 1000000,
+			.tv_usec = duration_us % 1000000
+		}
+	};
 
 	setitimer(ITIMER_VIRTUAL, &it, NULL);
 	for (;;);
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Wake,

Quoted 1 line · Wake Liu

controllers/cpuacct: Dynamically adjust cpuacct_task execution time

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v3 1/1 controllers/cpuacct: Dynamically adjust cpuacct_task execution time

The test helper cpuacct_task currently runs for 10ms (virtual time) before exiting. On some systems (e.g. fast virtual machines or under heavy load), this short duration can lead to 0 usage being recorded in cpuacct.usage cgroup file due to timing resolution or scheduling delay, especially when running small configurations like cpuacct_1_1.

However, unconditionally increasing the duration for all runs (e.g. to 100ms) would cause large stress tests like cpuacct_100_100 (which runs 10,000 tasks) to timeout on systems with few CPUs.

Resolve this by:
1. Modifying cpuacct_task to accept an optional duration argument.
2. Modifying cpuacct.sh to calculate the duration dynamically based on nbprocess, ensuring each subgroup gets at least 100ms of aggregate CPU time, while clamping the per-process duration to a minimum of 10ms to avoid timeouts in large stress tests.
3. Validating nsubgroup and nprocess arguments in cpuacct.sh setup() to ensure they are positive integers, avoiding division by zero or fatal arithmetic errors in shell.

---
 .../kernel/controllers/cpuacct/cpuacct.sh     | 17 +++++++++++++++-
 .../kernel/controllers/cpuacct/cpuacct_task.c | 20 ++++++++++++++++---
 2 files changed, 33 insertions(+), 4 deletions(-)
testcases/kernel/controllers/cpuacct/cpuacct.sh +16 -1
index 97a395cd5..deb103a30 100755
@@ -84,6 +84,16 @@ check_limits()
 
 setup()
 {
+	case "$max" in
+		""|*[!0-9]*) tst_brk TBROK "nsubgroup must be a positive integer" ;;
+		*) [ "$max" -gt 0 ] || tst_brk TBROK "nsubgroup must be a positive integer" ;;
+	esac
+
+	case "$nbprocess" in
+		""|*[!0-9]*) tst_brk TBROK "nprocess must be a positive integer" ;;
+		*) [ "$nbprocess" -gt 0 ] || tst_brk TBROK "nprocess must be a positive integer" ;;
+	esac
+
 	if ! grep -q -w cpuacct /proc/cgroups; then
 		tst_brk TCONF "cpuacct not supported on this system"
 	fi
@@ -139,10 +149,15 @@ do_test()
 {
 	tst_res TINFO "Creating $max subgroups each with $nbprocess processes"
 
+	local duration=$((100000 / nbprocess))
+	if [ "$duration" -lt 10000 ]; then
+		duration=10000
+	fi
+
 	# create and attach process to subgroups
 	for i in `seq 1 $max`; do
 		for j in `seq 1 $nbprocess`; do
-			cpuacct_task $testpath/subgroup_$i/tasks &
+			cpuacct_task "$testpath/subgroup_$i/tasks" "$duration" &
 			echo $! >> task_pids
 		done
 	done
testcases/kernel/controllers/cpuacct/cpuacct_task.c +17 -3
index 677d6d401..caeecf064 100644
@@ -38,12 +38,21 @@ int main(int argc, char **argv)
 {
 	FILE *f;
 	struct sigaction sa;
+	int duration_us = 10000;
 
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s /cgroup/.../tasks\n", argv[0]);
+	if (argc < 2 || argc > 3) {
+		fprintf(stderr, "Usage: %s /cgroup/.../tasks [duration_us]\n", argv[0]);
 		return 1;
 	}
 
+	if (argc == 3) {
+		duration_us = atoi(argv[2]);
+		if (duration_us <= 0) {
+			fprintf(stderr, "Invalid duration: %s\n", argv[2]);
+			return 1;
+		}
+	}
+
 	f = fopen(argv[1], "a");
 	if (!f) {
 		perror("fopen failed");
@@ -62,7 +71,12 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	struct itimerval it = {.it_value = {.tv_sec = 0, .tv_usec = 10000}};
+	struct itimerval it = {
+		.it_value = {
+			.tv_sec = duration_us / 1000000,
+			.tv_usec = duration_us % 1000000
+		}
+	};
 
 	setitimer(ITIMER_VIRTUAL, &it, NULL);
 	for (;;);
Signature · 5 lines
2.55.0.691.gc56d675ccc-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi Wake,

Quoted 1 line · Wake Liu

controllers/cpuacct: Dynamically adjust cpuacct_task execution time

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
In reply to Wake Liu via ltp

Hi!

Quoted 5 lines

The test helper cpuacct_task currently runs for 10ms (virtual time) before exiting. On some systems (e.g. fast virtual machines or under heavy load), this short duration can lead to 0 usage being recorded in cpuacct.usage cgroup file due to timing resolution or scheduling delay, especially when running small configurations like cpuacct_1_1.

I was starting at the test for a while and I do not see how this could happen.

The test uses ITMER_VIRTUAL which is counts user CPU time, so the kernel guarantees that the loop would consume CPU for 10ms, regardless of the CPU speed.

At the same time the cpuacct.sh script waits for all the processes before it checks the usage counters. So under heavy load the test should run longer but shouldn't fail.

All in all it should be guaranteed that the usage should be >= 10ms.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp