mirror of
https://github.com/diamante0018/MonitorRam.git
synced 2025-05-09 14:04:55 +00:00
Re-write this according to my current c-taste
This commit is contained in:
parent
d57e410587
commit
c3a4e27566
12
.clang-format
Normal file
12
.clang-format
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Left
|
||||
SortIncludes: false
|
||||
|
||||
# Regroup causes unnecessary noise due to clang-format bug.
|
||||
IncludeBlocks: Preserve
|
||||
|
||||
---
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2021, Edoardo Sanguineti
|
||||
Copyright (c) 2022, Edoardo Sanguineti
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
11
common.c
11
common.c
@ -1,15 +1,14 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// Prints the time
|
||||
void printTimeStamp(FILE *fp)
|
||||
{
|
||||
void get_time_stamp(char* buf) {
|
||||
time_t ltime;
|
||||
struct tm result;
|
||||
char stime[32];
|
||||
bzero(stime, sizeof(stime));
|
||||
|
||||
ltime = time(NULL);
|
||||
localtime_r(<ime, &result);
|
||||
asctime_r(&result, stime);
|
||||
fprintf(fp, "\nTimeStamp: %s\n", stime);
|
||||
asctime_r(&result, buf);
|
||||
}
|
||||
|
12
common.h
12
common.h
@ -1,16 +1,8 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void printTimeStamp(FILE *fp);
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
void get_time_stamp(char* buf);
|
||||
|
||||
#endif /* COMMON_H */
|
||||
|
29
main.c
29
main.c
@ -1,18 +1,16 @@
|
||||
#include "common.h"
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "monitor.h"
|
||||
|
||||
volatile sig_atomic_t sigint_received = 0;
|
||||
|
||||
void sigint_handler(int a)
|
||||
{
|
||||
sigint_received = 1;
|
||||
}
|
||||
void sigint_handler(int a) { sigint_received = 1; }
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
printf("Name of the program is missing\n");
|
||||
printf("Usage: <name of the program>\n");
|
||||
return 2;
|
||||
@ -22,11 +20,16 @@ int main(int argc, char *argv[])
|
||||
act.sa_handler = sigint_handler;
|
||||
sigaction(SIGINT, &act, NULL);
|
||||
|
||||
setup(argv[1]);
|
||||
int result = setup(argv[1]);
|
||||
if (result) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (!sigint_received) {
|
||||
if (analyse()) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
while (!sigint_received)
|
||||
{
|
||||
analyse();
|
||||
sleep(10);
|
||||
}
|
||||
|
||||
|
4
makefile
4
makefile
@ -1,7 +1,7 @@
|
||||
TARGET = prog
|
||||
TARGET = monitor
|
||||
LIBS = -lm
|
||||
CC = gcc
|
||||
CFLAGS = -O2 -g -Wall
|
||||
CFLAGS = -O2 -g -Wall -std=gnu11
|
||||
|
||||
.PHONY: clean all default
|
||||
|
||||
|
189
monitor.c
189
monitor.c
@ -1,165 +1,170 @@
|
||||
#include "common.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#include "monitor.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "utils.h"
|
||||
#include "monitor.h"
|
||||
|
||||
#define MAX_MEMORY 1048576
|
||||
#define MAX_TRACK_SIZE 30
|
||||
|
||||
FILE *popen(const char *command, const char *mode);
|
||||
int pclose(FILE *stream);
|
||||
char *strerror(int errnum);
|
||||
FILE* popen(const char* command, const char* type);
|
||||
int pclose(FILE* stream);
|
||||
char* strerror(int errnum);
|
||||
|
||||
void printGenericStatusError()
|
||||
{
|
||||
static void print_generic_status_error() {
|
||||
printf("Error while parsing file\n");
|
||||
printf("Processes may be monitored incorrectly\n");
|
||||
}
|
||||
|
||||
void checkStatus(FILE *file, int *currRealMem, int *peakRealMem,
|
||||
int *currVirtMem, int *peakVirtMem)
|
||||
{
|
||||
static void check_status(FILE* file, int* curr_real_mem, int* peak_real_mem,
|
||||
int* curr_virt_mem, int* peak_virt_mem) {
|
||||
|
||||
char result[BUFFER_SIZE];
|
||||
bzero(result, sizeof(result));
|
||||
memset(result, 0, sizeof result);
|
||||
|
||||
while (fscanf(file, " %1023s", result) == 1)
|
||||
{
|
||||
if (strcmp(result, "VmRSS:") == 0)
|
||||
{
|
||||
if (fscanf(file, " %d", currRealMem) == EOF)
|
||||
{
|
||||
printGenericStatusError();
|
||||
while (fscanf(file, " %1023s", result) == 1) {
|
||||
if (strcmp(result, "VmRSS:") == 0) {
|
||||
if (fscanf(file, " %d", curr_real_mem) == EOF) {
|
||||
print_generic_status_error();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(result, "VmHWM:") == 0)
|
||||
{
|
||||
if (fscanf(file, " %d", peakRealMem) == EOF)
|
||||
{
|
||||
printGenericStatusError();
|
||||
if (strcmp(result, "VmHWM:") == 0) {
|
||||
if (fscanf(file, " %d", peak_real_mem) == EOF) {
|
||||
print_generic_status_error();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(result, "VmSize:") == 0)
|
||||
{
|
||||
if (fscanf(file, " %d", currVirtMem) == EOF)
|
||||
{
|
||||
printGenericStatusError();
|
||||
if (strcmp(result, "VmSize:") == 0) {
|
||||
if (fscanf(file, " %d", curr_virt_mem) == EOF) {
|
||||
print_generic_status_error();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(result, "VmPeak:") == 0)
|
||||
{
|
||||
if (fscanf(file, " %d", peakVirtMem) == EOF)
|
||||
{
|
||||
printGenericStatusError();
|
||||
if (strcmp(result, "VmPeak:") == 0) {
|
||||
if (fscanf(file, " %d", peak_virt_mem) == EOF) {
|
||||
print_generic_status_error();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int isProcessWild(int currRealMem, int peakRealMem,
|
||||
int currVirtMem, int peakVirtMem, int ID)
|
||||
{
|
||||
if (currRealMem <= MAX_MEMORY)
|
||||
{
|
||||
static int is_process_wild(int curr_real_mem, int peak_real_mem,
|
||||
int curr_virt_mem, int peak_virt_mem, int id) {
|
||||
if (curr_real_mem <= MAX_MEMORY) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Sends a polite request to terminate
|
||||
printf("Terminating process: %d. Too much memory!\n", ID);
|
||||
int result = kill(ID, SIGTERM);
|
||||
if (result)
|
||||
{
|
||||
printf("Error while terminating process: %d\n", ID);
|
||||
printf("Error message: %s\n", strerror(errno));
|
||||
}
|
||||
printf("Terminating process: %d. Memory: %d\n", id, curr_real_mem);
|
||||
int result = kill(id, SIGTERM);
|
||||
|
||||
// Logs
|
||||
FILE *fp;
|
||||
fp = fopen("Diavolo.txt", "a");
|
||||
if (fp == NULL)
|
||||
{
|
||||
FILE* fp;
|
||||
fp = fopen("log.txt", "a");
|
||||
if (fp == NULL) {
|
||||
printf("Error while opening log.txt\n");
|
||||
perror("popen");
|
||||
exit(EXIT_FAILURE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char time[32];
|
||||
get_time_stamp(time);
|
||||
|
||||
fprintf(fp, "\nTimeStamp: %s\n", time);
|
||||
fprintf(fp, "WARNING: Exceeded %d KiB of Real Memory for process ID: %d\n",
|
||||
MAX_MEMORY, id);
|
||||
fprintf(fp,
|
||||
"Process ID: %d\ncurr_real_mem:%d KiB\npeak_real_mem:%d "
|
||||
"KiB\ncurr_virt_mem:%d KiB\npeak_virt_mem:%d KiB\n",
|
||||
id, curr_real_mem, peak_real_mem, curr_virt_mem, peak_virt_mem);
|
||||
|
||||
if (result != 0) {
|
||||
fprintf(fp, "Error while terminating process: %d\n", id);
|
||||
fprintf(fp, "Error message: %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
printTimeStamp(fp);
|
||||
fprintf(fp, "WARNING: Exceeded %d KiB of Real Memory for process ID: %d\n", MAX_MEMORY, ID);
|
||||
fprintf(fp, "Process ID: %d\ncurrRealMem:%d KiB\npeakRealMem:%d KiB\ncurrVirtMem:%d KiB\npeakVirtMem:%d KiB\n", ID, currRealMem, peakRealMem, currVirtMem, peakVirtMem);
|
||||
fclose(fp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int processID[MAX_TRACK_SIZE] = {0};
|
||||
struct process_monitor {
|
||||
int id;
|
||||
int active;
|
||||
};
|
||||
|
||||
void setup(const char *pid)
|
||||
{
|
||||
char *command = va("pgrep %s", pid);
|
||||
FILE *file = popen(command, "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
static struct process_monitor processes[MAX_TRACK_SIZE];
|
||||
|
||||
int setup(const char* pid) {
|
||||
char* command = va("pgrep %s", pid);
|
||||
FILE* file = popen(command, "r");
|
||||
if (file == NULL) {
|
||||
perror("popen");
|
||||
exit(EXIT_FAILURE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char result[BUFFER_SIZE];
|
||||
bzero(result, sizeof(result));
|
||||
memset(result, 0, sizeof result);
|
||||
int i = 0;
|
||||
|
||||
while (fgets(result, BUFFER_SIZE, file))
|
||||
{
|
||||
memset(&processes, 0, sizeof(struct process_monitor));
|
||||
while (fgets(result, BUFFER_SIZE, file) && i < MAX_TRACK_SIZE) {
|
||||
printf("Process ID: %s", result);
|
||||
processID[i] = atoi(result);
|
||||
i++;
|
||||
processes[i].id = atoi(result);
|
||||
processes[i].active = 1;
|
||||
|
||||
if (i >= MAX_TRACK_SIZE)
|
||||
{
|
||||
printf("Too many processes found\n");
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
if (i == 0) {
|
||||
printf("No process was found\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
pclose(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
pclose(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void analyse()
|
||||
{
|
||||
int analyse() {
|
||||
int i;
|
||||
FILE *status;
|
||||
int currRealMem = 0, peakRealMem = 0, currVirtMem = 0, peakVirtMem = 0;
|
||||
FILE* status;
|
||||
int curr_real_mem = 0, peak_real_mem = 0, curr_virt_mem = 0,
|
||||
peak_virt_mem = 0;
|
||||
|
||||
for (i = 0; i < MAX_TRACK_SIZE && processID[i] != 0; i++)
|
||||
{
|
||||
char *command = va("cat /proc/%d/status", processID[i]);
|
||||
for (i = 0; i < MAX_TRACK_SIZE && processes[i].active == 1; ++i) {
|
||||
char* command = va("cat /proc/%d/status", processes[i].id);
|
||||
status = popen(command, "r");
|
||||
if (status == NULL)
|
||||
{
|
||||
if (status == NULL) {
|
||||
perror("popen");
|
||||
exit(EXIT_FAILURE);
|
||||
pclose(status);
|
||||
return 1;
|
||||
}
|
||||
|
||||
checkStatus(status, &currRealMem, &peakRealMem, &currVirtMem, &peakVirtMem);
|
||||
if (isProcessWild(currRealMem, peakRealMem, currVirtMem, peakVirtMem, processID[i]))
|
||||
{
|
||||
processID[i] = 0;
|
||||
#ifdef _DEBUG
|
||||
printf("Checking %d\n", processes[i].id);
|
||||
#endif
|
||||
|
||||
check_status(status, &curr_real_mem, &peak_real_mem, &curr_virt_mem,
|
||||
&peak_virt_mem);
|
||||
if (is_process_wild(curr_real_mem, peak_real_mem, curr_virt_mem,
|
||||
peak_virt_mem, processes[i].id)) {
|
||||
processes[i].active = 0;
|
||||
}
|
||||
|
||||
pclose(status);
|
||||
currRealMem = 0, peakRealMem = 0, currVirtMem = 0, peakVirtMem = 0;
|
||||
curr_real_mem = 0, peak_real_mem = 0, curr_virt_mem = 0, peak_virt_mem = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef MONITOR_H
|
||||
#define MONITOR_H
|
||||
|
||||
void setup(const char *pid);
|
||||
void analyse();
|
||||
int setup(const char* pid);
|
||||
int analyse();
|
||||
|
||||
#endif /* MONITOR_H */
|
||||
|
28
utils.c
28
utils.c
@ -1,21 +1,29 @@
|
||||
#include "common.h"
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <threads.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#define VA_BUFFER_COUNT 4
|
||||
#define VA_BUFFER_SIZE 4096
|
||||
|
||||
char *va(const char *fmt, ...)
|
||||
{
|
||||
static char g_vaBuffer[VA_BUFFER_COUNT][VA_BUFFER_SIZE];
|
||||
static int g_vaNextBufferIndex = 0;
|
||||
thread_local struct {
|
||||
char va_string[VA_BUFFER_COUNT][VA_BUFFER_SIZE];
|
||||
int index;
|
||||
} va_info_t;
|
||||
|
||||
char* va(const char* fmt, ...) {
|
||||
va_list ap;
|
||||
char *dest = &g_vaBuffer[g_vaNextBufferIndex][0];
|
||||
bzero(dest, VA_BUFFER_SIZE);
|
||||
g_vaNextBufferIndex = (g_vaNextBufferIndex + 1) % VA_BUFFER_COUNT;
|
||||
|
||||
int index = va_info_t.index;
|
||||
va_info_t.index = (va_info_t.index + 1) % VA_BUFFER_COUNT;
|
||||
char* buf = va_info_t.va_string[index];
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(dest, VA_BUFFER_SIZE, fmt, ap);
|
||||
vsnprintf(buf, VA_BUFFER_SIZE, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return dest;
|
||||
buf[VA_BUFFER_SIZE - 1] = '\0';
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user