add time&memory usage writing

This commit is contained in:
root 2024-06-22 23:54:21 +05:00
parent 538cd56a01
commit 45ba0ff6c3
3 changed files with 59 additions and 17 deletions

View file

@ -18,7 +18,7 @@ void prepare_cgroup(struct limits* limits) {
mkdir(cgroup_name,0755); mkdir(cgroup_name,0755);
chdir(cgroup_name); chdir(cgroup_name);
char* memory_string=NULL; char* memory_string=NULL;
asprintf(&memory_string,"%d\n",limits->memory); asprintf(&memory_string,"%ld\n",limits->memory);
write_file("memory.max",memory_string); write_file("memory.max",memory_string);
free(memory_string); free(memory_string);
char* cpus_string=NULL; char* cpus_string=NULL;
@ -42,10 +42,27 @@ void add_to_cgroup(int pid) {
chdir(cwd); chdir(cwd);
} }
int check_mem() {
char cwd[PATH_MAX];
if(getcwd(cwd,sizeof(cwd))==NULL) die("getcwd error: %m");
chdir(cgroups_path);
chdir(cgroup_name);
int fdmax = open("memory.max",O_RDONLY);
int fdpeak = open("memory.peak",O_RDONLY);
char max[101],peak[101];
read(fdmax,max,100);
read(fdpeak,peak,100);
chdir(cwd);
close(fdmax);
close(fdpeak);
if(strcmp(max,peak) == 0) {return -1;}
return atoi(peak);
}
void remove_cgroup() { void remove_cgroup() {
char cwd[PATH_MAX]; char cwd[PATH_MAX];
if(getcwd(cwd,sizeof(cwd))==NULL) die("getcwd error: %m"); getcwd(cwd,sizeof(cwd));
if(chdir(cgroups_path)) die("chdir error: %m"); chdir(cgroups_path);
if(rmdir(cgroup_name)) die("rmdir error: %m"); rmdir(cgroup_name);
if(chdir(cwd)) die("chdir error: %m"); chdir(cwd);
} }

View file

@ -10,8 +10,10 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
#include <sys/time.h>
#include <limits.h> #include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h>
#include "util.h" #include "util.h"
@ -33,21 +35,29 @@ struct killparams{
int pid; int pid;
}; };
#define READ_SIZE 100
char read_buf[READ_SIZE + 1];
#include "ns_exec.c" #include "ns_exec.c"
#include "cgroup_prepare.c" #include "cgroup_prepare.c"
//bool TL_achieve=false;
uint64_t time_start;
//get time in milliseconds
uint64_t getmstime() {
struct timeval tm;
gettimeofday(&tm,NULL);
return (uint64_t)tm.tv_sec*1000+(uint64_t)tm.tv_usec/1000;
}
//time limit realisation //time limit realisation
static int killafter(void *arg) { static int killafter(void *arg) {
//die when parent dies //die when parent dies
if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n"); if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n");
struct killparams* params = (struct killparams*) arg; struct killparams* params = (struct killparams*) arg;
struct timespec tw = {params -> time / 1000, ((params->time) % 1000) * 1000000}; struct timespec tw = {params -> time / 1000, ((params->time) % 1000+5) * 1000000};
struct timespec tr; struct timespec tr;
nanosleep(&tw, &tr); nanosleep(&tw, &tr);
kill(params->pid, SIGKILL); kill(params->pid, SIGKILL);
return 0;
} }
#define STACK_SIZE 1000000 #define STACK_SIZE 1000000
@ -115,16 +125,34 @@ int main(int argc,char** argv) {
if (pipe(params.fd) < 0) die("can't open pipe: %m");//a pipe to report readiness if (pipe(params.fd) < 0) die("can't open pipe: %m");//a pipe to report readiness
int clone_flags = SIGCHLD | CLONE_NEWUTS | CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWIPC | CLONE_NEWCGROUP; int clone_flags = SIGCHLD | CLONE_NEWUTS | CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWNET | CLONE_NEWIPC | CLONE_NEWCGROUP;
int nsrun_pid = clone(nsrun, nmstack + STACK_SIZE, clone_flags, &params);//make new namespace int nsrun_pid = clone(nsrun, nmstack + STACK_SIZE, clone_flags, &params);//make new namespace
int pipe = params.fd[1]; //int pipe = params.fd[1];
prepare_userns(nsrun_pid); prepare_userns(nsrun_pid);
if (nsrun_pid < 0) die("faled to clone"); if (nsrun_pid < 0) die("faled to clone");
add_to_cgroup(nsrun_pid); add_to_cgroup(nsrun_pid);
if (write(pipe, "OK", 2) != 2) die("Failed to write to pipe: %m");//report readiness if (write(params.fd[1], "OK", 2) != 2) die("Failed to write to pipe: %m");//report readiness
struct killparams killparams; struct killparams killparams;
killparams.time = limits.time; killparams.time = limits.time;
killparams.pid = nsrun_pid; killparams.pid = nsrun_pid;
clone(killafter, killstack + STACK_SIZE, SIGCHLD, &killparams); int kill_pid = clone(killafter, killstack + STACK_SIZE, SIGCHLD, &killparams);
time_start = getmstime();
if (waitpid(nsrun_pid, NULL, 0) == -1) die("Failed to wait pid %d: %m\n", nsrun_pid); if (waitpid(nsrun_pid, NULL, 0) == -1) die("Failed to wait pid %d: %m\n", nsrun_pid);
kill(kill_pid,SIGKILL);//kill killer
int time = getmstime()-time_start;
chdir(params.shared_folder);
if(time>=limits.time) {
write_file("time", "-1");
}
else {
char* timestr = NULL;
asprintf(&timestr, "%d", time);
write_file("time", timestr);
free(timestr);
}
int memory = check_mem();
char* memstr = NULL;
asprintf(&memstr, "%d", memory);
write_file("memory", memstr);
free(memstr);
remove_cgroup(); remove_cgroup();
return 0; return 0;
} }

View file

@ -1,13 +1,12 @@
//#ifndef ISOLATE_UTIL_H void remove_cgroup();
//#define ISOLATE_UTIL_H
static void die(const char *fmt, ...) static void die(const char *fmt, ...)
{ {
va_list params; va_list params;
va_start(params, fmt); va_start(params, fmt);
vfprintf(stderr, fmt, params); vfprintf(stderr, fmt, params);
va_end(params); va_end(params);
remove_cgroup();
exit(1); exit(1);
} }
@ -18,5 +17,3 @@ static void write_file(char* path, char* line)
if (fwrite(line, 1, strlen(line), f) < 0) {die("Failed to write to file %s:\n", path);} if (fwrite(line, 1, strlen(line), f) < 0) {die("Failed to write to file %s:\n", path);}
if (fclose(f) != 0) {die("Failed to close file %s: %m\n", path);} if (fclose(f) != 0) {die("Failed to close file %s: %m\n", path);}
} }
//#endif //ISOLATE_UTIL_H