Skip to content

Commit

Permalink
Compact Update
Browse files Browse the repository at this point in the history
  • Loading branch information
R0STUS committed Oct 18, 2024
1 parent 491898f commit 0621895
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
Binary file modified crystaller
Binary file not shown.
2 changes: 2 additions & 0 deletions settings.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
maxMem=3062
ignoreName=vesktop
111 changes: 94 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <tuple>

std::vector<std::pair<long, long>> get_process_memory_usage() {
std::vector<std::pair<long, long>> result;
FILE* fp = popen("ps -e --no-headers -o pid,rss", "r");
bool isRunningAsRoot() {
return getuid() == 0;
}

std::vector<std::tuple<long, long, std::string>> get_process_memory_usage() {
std::vector<std::tuple<long, long, std::string>> result;
FILE* fp = popen("ps -e --no-headers -o pid,rss,comm", "r");
if (fp == nullptr) {
std::cerr << "Failed to open channel!" << std::endl;
return result;
Expand All @@ -18,49 +23,121 @@ std::vector<std::pair<long, long>> get_process_memory_usage() {
while (fgets(buffer, sizeof(buffer), fp) != nullptr) {
std::istringstream iss(buffer);
long pid, rss;
iss >> pid >> rss;
result.push_back({pid, rss});
}
int status = pclose(fp);
if (status == -1) {
std::cerr << "Failed to close channel!" << std::endl;
std::string comm;
if (iss >> pid >> rss >> comm) {
result.push_back({pid, rss, comm});
}
}
std::sort(result.begin(), result.end(), [](const auto& a, const auto& b) { return a.first > b.first; });
pclose(fp);
std::sort(result.begin(), result.end(), [](const auto& a, const auto& b) {
return std::get<1>(a) > std::get<1>(b);
});
return result;
}


int main() {
if (isRunningAsRoot()) {
std::cout << "Do NOT run this in root" << std::endl;
return 1;
}
system("clear");
long proccesMemNow;
long proccesNameNow;
std::string proccesNameStrNow;
std::string proccesPidNow;
std::string command = "kill ";
long long checkNum = 0;
long maxMem = 2560;
long maxMem;
std::string tmp;
std::ifstream settingsFile;
settingsFile.open("settings.file");
std::string sType;
std::vector<std::string> ignoringNames;
bool isSkipPID;
settingsFile.open("settings.properties");
if (!settingsFile) {
std::cout << "Warining! Cannot open settings file, starting in default mode..." << std::endl;
maxMem = 2560;
}
else {
settingsFile >> tmp;
maxMem = stol(tmp);
while (std::getline(settingsFile, tmp)) {
sType = "";
for (char c : tmp) {
if (c != '=') {
sType += c;
}
else if (c == '=' && !sType.empty()) {
if (sType == "maxMem") {
bool getLong = false;
sType = "";
for (char c2 : tmp) {
if (getLong == true && std::isdigit(c2)) {
sType += c2;
}
if (c2 == '=') {
getLong = true;
}
}
if (!sType.empty()) {
maxMem = std::__cxx11::stol(sType);
}
else
maxMem = 2560;
break;
}
if (sType == "ignoreName") {
bool getName = false;
sType = "";
for (char c2 : tmp) {
if (getName == true) {
sType += c2;
}
if (c2 == '=') {
getName = true;
}
}
if (!sType.empty()) {
ignoringNames.push_back(sType);
std::cout << sType << " ignoring." << std::endl;
}
break;
}
}
else
break;
}
}
}
settingsFile.clear();
settingsFile.close();
tmp.clear();
tmp.shrink_to_fit();
std::cout << "Max. Memory for proccess: " << maxMem << std::endl;
std::string checkerStr;
while (true) {
checkNum++;
std::cout << "Checking... [" << checkNum << "]" << std::endl;
std::vector<std::pair<long, long>> procceses = get_process_memory_usage();
std::cout << "\r" << "Checking... [" << checkNum << "]" << std::flush;
std::vector<std::tuple<long, long, std::string>> procceses = get_process_memory_usage();
for (int i = 0; i < procceses.size(); i++) {
isSkipPID = false;
std::string command = "kill ";
proccesNameNow = std::get<0>(procceses[i]);
proccesMemNow = std::get<1>(procceses[i]);
proccesNameStrNow = std::get<2>(procceses[i]);
proccesPidNow = std::to_string(proccesNameNow);
command += proccesPidNow;
if (((proccesMemNow / 1024)) > maxMem) {
for (auto nStr : ignoringNames) {
if (proccesNameStrNow == nStr) {
isSkipPID = true;
}
}
if (((proccesMemNow / 1024)) > maxMem && isSkipPID != true) {
std::cout << std::string(checkerStr.length(), '\b');
checkerStr.clear();
if (proccesPidNow == "1") {
std::cout << "Attention! Kernel's memory is within limits! Self-killing..." << std::endl;
return -1;
}
std::cout << "Killing PID " << proccesPidNow << std::endl;
system(command.c_str());
}
Expand Down

0 comments on commit 0621895

Please sign in to comment.