myxce/src/worker/include/xcecd_cgroups.h

106 lines
No EOL
3.5 KiB
C

#ifndef _XCECD_CGROUPS_H_
#define _XCECD_CGROUPS_H_
#include "xcecd_utils.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <unistd.h>
typedef struct {
// CPU limit
uint64_t
cpu_quota; // CPU quota in microseconds (e.g. 200000 = 20%, cpu_limit)
uint64_t cpu_period; // CPU period in microseconds (e.g. 1000000 = 1 second)
uint32_t cpu_weight; // CPU weight (e.g. 100 = default)
uint64_t cpu_limit; // CPU limit (run time limit in seconds)
uint64_t core_limit; // Core limit (in number of cores, e.g. 2 = 2 cores)
char* cpu_affinity; // CPU affinity list (e.g. "0,1,2")
// Memory limit
uint64_t memory_max; // Maximum memory usage (rss_limit)
uint64_t memory_high; // Memory high usage level (in bytes)
uint64_t memory_swap_max; // Maximum swap space usage (swap_limit)
// IO limit
uint64_t io_weight; // IO weight (e.g. 100 = default)
uint64_t io_read_bps_max; // Maximum read speed (bytes/second)
uint64_t io_write_bps_max; // Maximum write speed (bytes/second)
char* io_device_path; // IO device path (e.g. "/dev/sda")
// Tasks limit
uint64_t tasks_max; // Max number of threads (thread_limit)
uint64_t run_limit; // Max run time (in seconds)
uint64_t core_size_limit; // Max core file size (in bytes)
uint64_t data_limit; // Max data size (in bytes)
uint64_t fsize_limit; // Max file size (in bytes)
uint64_t stack_limit; // Max stack size (in bytes)
uint64_t process_limit; // Max number of processes
// OOM score
int32_t oom_score_adjust; // OOM (-1000 to 1000)
char* user_name; // User name
} res_limits_t;
/**
* start worker with resource limits
* @param command command with arguments
* @param limits resource limits (NULL if no limits)
* @param unit_name_prefix unit name prefix, NULL for default prefix
* @param job_id job id
* @param extra_args extra command line arguments (NULL if none)
* @return unit name if success, NULL if failed
*/
char* start_systemd_job(const char* command, const res_limits_t* limits,
const char* unit_name_prefix, uint64_t job_id,
char* extra_args);
/**
* Get PID of a systemd job
* @param unit_name unit name
* @return PID if success, -1 if failed
*/
pid_t get_systemd_job_pid(const char* unit_name);
/**
* Stop a systemd job
* @param unit_name unit name
* @return 0 if success, -1 if failed
*/
int stop_systemd_job(const char* unit_name);
/**
* Check if a systemd job is active
* @param unit_name unit name
* @return true if active, false if not active
*/
bool is_systemd_job_active(const char* unit_name);
/**
* Get systemd job exit code
* @param unit_name unit name
* @return exit code if success, -1 if failed
*/
int get_systemd_job_exit_code(const char* unit_name);
/**
* @brief Get systemd job status text
* @param unit_name unit name
* @return status text if success, NULL if failed.
* Caller must free the returned string.
*/
char* get_systemd_job_status_text(const char* unit_name);
/**
* @brief Collect resource usage statistics for a systemd job.
* @param job_id The ID of the job.
* @return A pointer to an xce_job_rusages_t struct, or NULL on failure.
* The caller is responsible for freeing the returned struct
* using free_xce_job_rusages().
*/
xce_job_rusages_t* collect_job_rusages(uint64_t job_id);
#endif // _XCECD_CGROUPS_H_