// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
*/
#define _GNU_SOURCE
#ifdef HAVE_LIBCPUPOWER_SUPPORT
#include <cpuidle.h>
#endif /* HAVE_LIBCPUPOWER_SUPPORT */
#include <dirent.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include "utils.h"
#define MAX_MSG_LENGTH 1024
int config_debug;
/*
* err_msg - print an error message to the stderr
*/
void err_msg(const char *fmt, ...)
{
char message[MAX_MSG_LENGTH];
va_list ap;
va_start(ap, fmt);
vsnprintf(message, sizeof(message), fmt, ap);
va_end(ap);
fprintf(stderr, "%s", message);
}
/*
* debug_msg - print a debug message to stderr if debug is set
*/
void debug_msg(const char *fmt, ...)
{
char message[MAX_MSG_LENGTH];
va_list ap;
if (!config_debug)
return;
va_start(ap, fmt);
vsnprintf(message, sizeof(message), fmt, ap);
va_end(ap);
fprintf(stderr, "%s", message);
}
/*
* get_llong_from_str - get a long long int from a string
*/
long long get_llong_from_str(char *start)
{
long long value;
char *end;
errno = 0;
value = strtoll(start, &end, 10);
if (errno || start == end)
return -1;
return value;
}
/*
* get_duration - fill output with a human readable duration since start_time
*/
void get_duration(time_t start_time, char *output, int output_size)
{
time_t now = time(NULL);
struct tm *tm_info;
time_t duration;
duration = difftime(now, start_time);
tm_info = gmtime(&duration);
snprintf(output, output_size, "%3d %02d:%02d:%02d",
tm_info->tm_yday,
tm_info->tm_hour,
tm_info->tm_min,
tm_info->tm_sec);
}
/*
* parse_cpu_set - parse a cpu_list filling cpu_set_t argument
*
* Receives a cpu list, like 1-3,5 (cpus 1, 2, 3, 5), and then set
* filling cpu_set_t argument.
*
* Returns 1 on success, 0 otherwise.
*/
int parse_cpu_set(char *cpu_list, cpu_set_t *set)
{
const char *p;
int end_cpu;
int nr_cpus;
int cpu;
int i;
CPU_ZERO(set);
nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
for (p = cpu_list; *p; ) {
cpu = atoi(p);
if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
goto err;
while (isdigit(*p))
p++;
if (*p == '-') {
p++;
end_cpu = atoi(p);
if (end_cpu < cpu || (!end_cpu && *p != '0') || end_cpu >= nr_cpus)
goto err;
while (isdigit(*p))
p++;
} else
end_cpu = cpu;
if (cpu == end_cpu) {
debug_msg("cpu_set: adding cpu %d\n", cpu);
CPU_SET(cpu, set);
} else {
for (i = cpu; i <= end_cpu; i++) {
debug_msg("cpu_set: adding cpu %d\n", i);
CPU_SET(i, set);
}
}
if (*p == ',')
p++;
}
return 0;
err:
debug_msg("Error parsing the cpu set %s\n", cpu_list);
return 1;
}
/*
* parse_duration - parse duration with s/m/h/d suffix converting it to seconds
*/
long parse_seconds_duration(char *val)
{
char *end;
long t;
t = strtol(val, &end, 10);
if (end) {
switch (*end) {
case 's':
case 'S':
break;
case 'm':
case 'M':
t *= 60;
break;
case 'h':
case 'H':
t *= 60 * 60;
break;
case 'd':
case 'D':
t *= 24 * 60 * 60;
break;
}
}
return t;
}
/*
* parse_ns_duration - parse duration with ns/us/ms/s converting it to nanoseconds
*/
long parse_ns_duration(char *val)
{
char *end;
long t;
t = strtol(val, &end, 10);
if (end) {
if (!strncmp(end, "ns", 2)) {
return t;
} else if (!strncmp(end, "us", 2)) {
t *= 1000;
return t;
} else if (!strncmp(end, "ms", 2)) {
t *= 1000 * 1000;
return t;
} else if (!strncmp(end, "s", 1)) {
t *= 1000 * 1000 * 1000;
return t;
}
return -1;
}
return t;
}
/*
* This is a set of helper functions to use SCHED_DEADLINE.
*/
#ifndef __NR_sched_setattr
# ifdef __x86_64__
# define __NR_sched_setattr 314
# elif __i386__
# define __NR_sched_setattr 351
# elif __arm__
# define __NR_sched_setattr 380
# elif __aarch64__ || __riscv
# define __NR_sched_setattr 274
# elif __powerpc__
# define __NR_sched_setattr 355
# elif __s390x__
# define __NR_sched_setattr 345
# endif
#endif
#define SCHED_DEADLINE 6
static inline int syscall_sched_setattr(pid_t pid, const struct sched_attr *attr,
unsigned int flags) {
return syscall(__NR_sched_setattr, pid, attr, flags);
}
int __set_sched_attr(int pid, struct sched_attr *attr)
{
int flags = 0;
int retval;
retval = syscall_sched_setattr(pid, attr, flags);
if (retval < 0) {
err_msg(