Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating to the latest stalld v1.13.0. #246

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/tuned/stalld/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME := stalld
VERSION := 1.12
VERSION := 1.13

INSTALL = install
CC := gcc
Expand Down
83 changes: 61 additions & 22 deletions assets/tuned/stalld/src/stalld.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ char *config_monitored_cpus;
size_t config_buffer_size;

/*
* auto-detected task format from /proc/sched_debug
* auto-detected task format from sched_debug.
*/
int config_task_format;

Expand Down Expand Up @@ -121,7 +121,14 @@ long page_size;
/*
* config single threaded: uses less CPU, but has a lower precision.
*/
int config_single_threaded = 0;
int config_single_threaded = 1;

/*
* config adaptive multi-threaded: use a single thread when nothing
* is happening, but dispatches a per-cpu thread after a starving
* thread is waiting for half of the config_starving_threshold.
*/
int config_adaptive_multi_threaded = 0;

/*
* check the idle time before parsing sched_debug
Expand All @@ -141,6 +148,11 @@ regex_t *compiled_regex_thread = NULL;
unsigned int nr_process_ignore = 0;
regex_t *compiled_regex_process = NULL;

/*
* store the current sched_debug file path.
*/
char *config_sched_debug_path = NULL;

/*
* API to fetch process name from process group ID
*/
Expand Down Expand Up @@ -227,8 +239,7 @@ int get_tgid(int pid) {
}

/*
* read the content of /proc/sched_debug into the
* input buffer.
* read the content of sched_debug into the input buffer.
*/
int read_sched_stat(char *buffer, int size)
{
Expand Down Expand Up @@ -409,16 +420,15 @@ int get_cpu_busy_list(struct cpu_info *cpus, int nr_cpus, char *busy_cpu_list)
return busy_count;
}
/*
* read the contents of /proc/sched_debug into
* the input buffer
* read the contents of sched_debug into the input buffer.
*/
int read_sched_debug(char *buffer, int size)
{
int position = 0;
int retval;
int fd;

fd = open("/proc/sched_debug", O_RDONLY);
fd = open(config_sched_debug_path, O_RDONLY);

if (!fd)
goto out_error;
Expand Down Expand Up @@ -544,11 +554,11 @@ static inline char *nextline(char *str)
#define TASK_MARKER "runnable tasks:"

/*
* read /proc/sched_debug and figure out if it's old or new format
* read sched_debug and figure out if it's old or new format
* done once so if we fail just exit the program
*
* NOTE: A side effect of this call is to set the initial value for
* config_buffer_size used when reading /proc/sched_debug for
* config_buffer_size used when reading sched_debug for
* parsing
*/
int detect_task_format(void)
Expand All @@ -567,15 +577,15 @@ int detect_task_format(void)
buffer = malloc(bufsiz);

if (buffer == NULL)
die("unable to allocate %d bytes to read /proc/sched_debug");
die("unable to allocate %d bytes to read sched_debug");

if ((fd = open("/proc/sched_debug", O_RDONLY)) < 0)
die("error opening /proc/sched_debug for reading: %s\n", strerror(errno));
if ((fd = open(config_sched_debug_path, O_RDONLY)) < 0)
die("error opening sched_debug for reading: %s\n", strerror(errno));

ptr = buffer;
while ((status = read(fd, ptr, bufincrement))) {
if (status < 0)
die ("error reading /proc/sched_debug: %s\n", strerror(errno));
die ("error reading sched_debug: %s\n", strerror(errno));
if (status == 0)
break;
size += status;
Expand Down Expand Up @@ -714,7 +724,7 @@ int parse_new_task_format(char *buffer, struct task_info *task_info, int nr_entr
}

/*
* old format of /proc/sched_debug doesn't contain state information so we have
* old format of sched_debug doesn't contain state information so we have
* to pick up the pid and then open /proc/<pid>/stat to get the process state.
*/

Expand Down Expand Up @@ -966,6 +976,7 @@ struct cpu_starving_task_info {
struct task_info task;
int pid;
time_t since;
int overloaded;
};

struct cpu_starving_task_info *cpu_starving_vector;
Expand All @@ -974,6 +985,13 @@ void update_cpu_starving_vector(int cpu, int pid, time_t since, struct task_info
{
struct cpu_starving_task_info *cpu_info = &cpu_starving_vector[cpu];

/*
* If there is another thread already here, mark this cpu as
* overloaded.
*/
if (cpu_info->pid)
cpu_info->overloaded = 1;

/*
* If there is no thread in the vector, or if the in the
* vector has an earlier since (time stamp), update it.
Expand Down Expand Up @@ -1477,7 +1495,7 @@ void conservative_main(struct cpu_info *cpus, int nr_cpus)
has_busy_cpu = get_cpu_busy_list(cpus, nr_cpus, busy_cpu_list);
if (!has_busy_cpu) {
if (config_verbose)
log_msg("all CPUs had idle time, skipping /proc/sched_debug parse\n");
log_msg("all CPUs had idle time, skipping sched_debug parse\n");
goto skipped;
}
}
Expand Down Expand Up @@ -1545,8 +1563,8 @@ int boost_cpu_starving_vector(struct cpu_starving_task_info *vector, int nr_cpus

cpu = &cpu_starving_vector[i];

if (config_verbose)
log_msg("boosting cpu %d: pid: %d starving for %llu\n", i, cpu->pid, (now - cpu->since));
if (config_verbose && cpu->pid)
log_msg("\t cpu %d: pid: %d starving for %llu\n", i, cpu->pid, (now - cpu->since));

if (cpu->pid != 0 && (now - cpu->since) > config_starving_threshold) {
/*
Expand Down Expand Up @@ -1604,6 +1622,7 @@ void single_threaded_main(struct cpu_info *cpus, int nr_cpus)
struct cpu_info *cpu;
char *buffer = NULL;
size_t buffer_size = 0;
int overloaded = 0;
int has_busy_cpu;
int boosted = 0;
int retval;
Expand All @@ -1629,6 +1648,7 @@ void single_threaded_main(struct cpu_info *cpus, int nr_cpus)
cpus[i].thread_running = 0;
cpu_starving_vector[i].pid = 0;
cpu_starving_vector[i].since = 0;
cpu_starving_vector[i].overloaded = 0;
memset(&cpu_starving_vector[i].task, 0, sizeof(struct task_info));
}

Expand All @@ -1653,7 +1673,7 @@ void single_threaded_main(struct cpu_info *cpus, int nr_cpus)
has_busy_cpu = get_cpu_busy_list(cpus, nr_cpus, busy_cpu_list);
if (!has_busy_cpu) {
if (config_verbose)
log_msg("all CPUs had idle time, skipping /proc/sched_debug parse\n");
log_msg("all CPUs had idle time, skipping sched_debug parse\n");

goto skipped;
}
Expand Down Expand Up @@ -1696,6 +1716,20 @@ void single_threaded_main(struct cpu_info *cpus, int nr_cpus)
memset(&(cpu_starving_vector[i].task), 0, sizeof(struct task_info));
cpu_starving_vector[i].pid = 0;
cpu_starving_vector[i].since = 0;
if (cpu_starving_vector[i].overloaded)
overloaded = 1;
cpu_starving_vector[i].overloaded = 0;
}

/*
* If any CPU had more than one thread starving, the system is overloaded.
* Re-run the loop without sleeping for two reasons: to boost the other
* thread, and to detect other starving threads on other CPUs, given
* that the system seems to be overloaded.
*/
if (overloaded) {
overloaded = 0;
continue;
}

skipped:
Expand Down Expand Up @@ -1796,6 +1830,8 @@ int main(int argc, char **argv)

parse_args(argc, argv);

find_sched_debug_path();

/*
* check RT throttling
* if --systemd was specified then RT throttling should already be off
Expand Down Expand Up @@ -1852,12 +1888,15 @@ int main(int argc, char **argv)

write_pidfile();

if (config_single_threaded)
single_threaded_main(cpus, nr_cpus);
else if (config_aggressive)
/*
* The less likely first.
*/
if (config_aggressive)
aggressive_main(cpus, nr_cpus);
else
else if (config_adaptive_multi_threaded)
conservative_main(cpus, nr_cpus);
else
single_threaded_main(cpus, nr_cpus);

cleanup_regex(&nr_thread_ignore, &compiled_regex_thread);
cleanup_regex(&nr_process_ignore, &compiled_regex_process);
Expand Down
4 changes: 4 additions & 0 deletions assets/tuned/stalld/src/stalld.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ int parse_args(int argc, char **argv);
int rt_throttling_is_off(void);
int turn_off_rt_throttling(void);
void cleanup_regex();
void find_sched_debug_path(void);

/*
* shared variables
Expand All @@ -174,9 +175,12 @@ extern int config_systemd;
extern long config_granularity;
extern int config_idle_detection;
extern int config_single_threaded;
extern int config_adaptive_multi_threaded;
extern char pidfile[];
extern unsigned int nr_thread_ignore;
extern unsigned int nr_process_ignore;
extern regex_t *compiled_regex_thread;
extern regex_t *compiled_regex_process;
extern char *config_sched_debug_path;

#endif /* __STALLD_H__ */