summaryrefslogtreecommitdiff
path: root/tools/perf/tests/workloads/context_switch_loop.c
blob: 5431af6147e6b7ff761d2adab30529c3e4f64ae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

// SPDX-License-Identifier: GPL-2.0

#include <linux/compiler.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>

#include "../tests.h"

static int loops = 100;
static char buf;
int context_switch_loop_work = 1234;

#define write_block(fd) \
	do { \
		if (write(fd, &buf, 1) <= 0) \
			return 1; \
	} while (0)

#define read_block(fd) \
	do { \
		if (read(fd, &buf, 1) <= 0) \
			return 1; \
	} while (0)

/* Not static to avoid LTO clobbering the function name */
int context_switch_loop_proc1(int in_fd, int out_fd);
int context_switch_loop_proc1(int in_fd, int out_fd)
{
	for (int i = 0; i < loops; i++) {
		read_block(in_fd);
		context_switch_loop_work += i * 3;
		write_block(out_fd);
	}
	return 0;
}

int context_switch_loop_proc2(int in_fd, int out_fd);
int context_switch_loop_proc2(int in_fd, int out_fd)
{
	for (int i = 0; i < loops; i++) {
		write_block(out_fd);
		context_switch_loop_work += i * 7;
		read_block(in_fd);
	}
	return 0;
}

/*
 * Launches two processes that take turns to execute a multiplication N times
 */
static int context_switch_loop(int argc, const char **argv)
{
	int a_to_b[2], b_to_a[2];
	pid_t proc1_pid;
	int status;
	int ret;

	if (argc > 0) {
		loops = atoi(argv[0]);
		if (loops < 0) {
			fprintf(stderr, "Invalid number of loops: %s\n", argv[0]);
			return 1;
		}
	}

	if (pipe(a_to_b) || pipe(b_to_a)) {
		perror("Pipe error");
		return 1;
	}

	proc1_pid = fork();
	if (proc1_pid < 0) {
		perror("Fork error");
		return 1;
	}

	if (!proc1_pid) {
		close(a_to_b[0]);
		close(b_to_a[1]);
		prctl(PR_SET_NAME, "proc1", 0, 0, 0);
		ret = context_switch_loop_proc1(b_to_a[0], a_to_b[1]);
		close(a_to_b[1]);
		close(b_to_a[0]);
		exit(ret);
	}

	close(a_to_b[1]);
	close(b_to_a[0]);
	prctl(PR_SET_NAME, "proc2", 0, 0, 0);
	ret = context_switch_loop_proc2(a_to_b[0], b_to_a[1]);
	close(a_to_b[0]);
	close(b_to_a[1]);

	if (ret) {
		kill(proc1_pid, SIGKILL);
		return ret;
	}

	if (waitpid(proc1_pid, &status, 0) != proc1_pid || !WIFEXITED(status) ||
	    WEXITSTATUS(status))
		return 1;

	return 0;
}

DEFINE_WORKLOAD(context_switch_loop);