Skip to content

Commit

Permalink
Fix #89 - Test cases for rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Apr 23, 2024
1 parent beae23d commit 735d877
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@ fn command_line() -> Commands {
break;
}
}
if rollup && batchless {

#[cfg(debug_assertions)]
let allow_incompatible = std::env::var("SONARTEST_ROLLUP").is_ok();

#[cfg(not(debug_assertions))]
let allow_incompatible = false;

if rollup && batchless && !allow_incompatible {
eprintln!("--rollup and --batchless are incompatible");
std::process::exit(USAGE_ERROR);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rollup
rollup2
rollupchild
rollupchild2
7 changes: 7 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CFLAGS=-Wall -std=c89
.PHONY: all

all: rollup rollup2 rollupchild rollupchild2

rollupchild2: rollupchild
cp rollupchild rollupchild2
64 changes: 64 additions & 0 deletions tests/rollup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Run this with SONARTEST_ROLLUP=1 and --rollup --batchless.
If you grep the sonar output for ',cmd=rollup,' there should be 23 lines.
Of those, there should be eight that have ',rolledup=1' and none that have any other rollup fields.
(This code is -std=c89, try to keep it that way.)
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char** argv) {
long depth;
pid_t c1, c2;
if (argc < 2) {
fprintf(stderr, "Usage: %s depth\n", argv[0]);
exit(1);
}
errno = 0;
depth = strtol(argv[1], NULL, 10);
if (errno != 0 || depth < 0 || depth > 10) {
fprintf(stderr, "Bad depth\n");
exit(1);
}
again:
/* in Parent */
c1 = fork();
if (c1 == -1) {
perror("Forking child");
exit(1);
}
if (c1 > 0) {
/* in Parent */
c2 = fork();
if (c2 == -1) {
perror("Forking child");
exit(1);
}
if (c2 > 0) {
/* in Parent */
/* printf("Waiting %d\n", getpid()); */
wait(NULL);
wait(NULL);
} else {
/* in C2 */
if (depth-- > 0) {
goto again;
}
/* printf("Sleeping %d\n", getpid()); */
sleep(10);
}
} else {
/* in C1 */
if (depth-- > 0) {
goto again;
}
/* printf("Sleeping %d\n", getpid()); */
sleep(10);
}
return 0;
}
36 changes: 36 additions & 0 deletions tests/rollup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
#
# Test these aspects of the process rollup algorithm:
# - only leaf processes are rolled up
# - only siblings of the same parent are rolled up
#
# To do this on a non-slurm system we run --rollup --batchless with an override to allow that.
#
# This requires a (probably) 1.6x or later Rust/Cargo toolchain to build Sonar and `make` + any C89
# or later C compiler to build the C code.

set -e

( cd .. ; cargo build )
make --quiet

echo "This takes about 10s"
./rollup 3 &
sleep 3
output=$(SONARTEST_ROLLUP=1 ../target/debug/sonar ps --rollup --batchless --exclude-system-jobs)
matches=$(grep ,cmd=rollup, <<< $output)
rolled=$(grep ,rolledup=1 <<< $matches)
rolled2=$(grep ,rolledup= <<< $matches)
if [[ $(wc -l <<< $matches) != 23 ]]; then
echo "Bad number of matching lines"
exit 1
fi
if [[ $(wc -l <<< $rolled) != 8 ]]; then
echo "Bad number of rolled-up lines with value 1"
exit 1
fi
if [[ $(wc -l <<< $rolled2) != 8 ]]; then
echo "Bad number of rolled-up lines - some have a value other than 1"
exit 1
fi
echo Success
44 changes: 44 additions & 0 deletions tests/rollup2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Run this with SONARTEST_ROLLUP=1 and --rollup --batchless.
This will fork off a 9 child processes (rollupchild x 5 and rollupchild2 x 4) that are the
same except for the name, all of which will wait 10s. Sonar, run meanwhile, should rollup
the children with the same name only. Since the rollup field represents n-1 processes,
the count for rollupchild should be 4 and for rollupchild2 should be 3.
(This code is -std=c89, try to keep it that way.)
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TYPE1 5
#define TYPE2 4

int main(int argc, char** argv) {
int i;
for ( i=0 ; i < TYPE1+TYPE2 ; i++ ) {
switch (fork()) {
case -1:
perror("Forking child");
exit(1);
case 0:
if (i < TYPE1) {
execl("rollupchild", "rollupchild", NULL);
fprintf(stderr, "Failed to exec child\n");
exit(1);
} else {
execl("rollupchild2", "rollupchild2", NULL);
fprintf(stderr, "Failed to exec child 2\n");
exit(1);
}
}
}
for ( i=0 ; i < TYPE1+TYPE2 ; i++ ) {
wait(NULL);
}
return 0;
}
23 changes: 23 additions & 0 deletions tests/rollup2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#
# Test these aspects of the process rollup algorithm:
# - only siblings with the same name are rolled up
#
# To do this on a non-slurm system we run --rollup --batchless with an override to allow that.
#
# This requires a (probably) 1.6x or later Rust/Cargo toolchain to build Sonar and `make` + any C89
# or later C compiler to build the C code.

set -e

( cd .. ; cargo build )
make --quiet

echo "This takes about 10s"
./rollup2 3 &
sleep 3
output=$(SONARTEST_ROLLUP=1 ../target/debug/sonar ps --rollup --batchless --exclude-system-jobs)
# Grep will exit with code 1 if no lines are matched
matches1=$(grep -E ',cmd=rollupchild,.*,rolledup=4' <<< $output)
matches2=$(grep -E ',cmd=rollupchild2,.*,rolledup=3' <<< $output)
echo Success
8 changes: 8 additions & 0 deletions tests/rollupchild.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* See rollup2.c */

#include <unistd.h>

int main(int argc, char** argv) {
sleep(10);
return 0;
}

0 comments on commit 735d877

Please sign in to comment.