xref: /linux/tools/tracing/rtla/src/common.c (revision 263d7eacf8d17d66078700ff6b4b540d66f56278)
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 
4 #include <unistd.h>
5 #include "common.h"
6 
7 /*
8  * common_apply_config - apply common configs to the initialized tool
9  */
10 int
11 common_apply_config(struct osnoise_tool *tool, struct common_params *params)
12 {
13 	int retval, i;
14 
15 	if (!params->sleep_time)
16 		params->sleep_time = 1;
17 
18 	retval = osnoise_set_cpus(tool->context, params->cpus ? params->cpus : "all");
19 	if (retval) {
20 		err_msg("Failed to apply CPUs config\n");
21 		goto out_err;
22 	}
23 
24 	if (!params->cpus) {
25 		for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
26 			CPU_SET(i, &params->monitored_cpus);
27 	}
28 
29 	if (params->hk_cpus) {
30 		retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
31 					   &params->hk_cpu_set);
32 		if (retval == -1) {
33 			err_msg("Failed to set rtla to the house keeping CPUs\n");
34 			goto out_err;
35 		}
36 	} else if (params->cpus) {
37 		/*
38 		 * Even if the user do not set a house-keeping CPU, try to
39 		 * move rtla to a CPU set different to the one where the user
40 		 * set the workload to run.
41 		 *
42 		 * No need to check results as this is an automatic attempt.
43 		 */
44 		auto_house_keeping(&params->monitored_cpus);
45 	}
46 
47 	/*
48 	 * Set workload according to type of thread if the kernel supports it.
49 	 * On kernels without support, user threads will have already failed
50 	 * on missing fd, and kernel threads do not need it.
51 	 */
52 	retval = osnoise_set_workload(tool->context, params->kernel_workload);
53 	if (retval < -1) {
54 		err_msg("Failed to set OSNOISE_WORKLOAD option\n");
55 		goto out_err;
56 	}
57 
58 	return 0;
59 
60 out_err:
61 	return -1;
62 }
63 
64