Added caching for kernel processes.

This commit is contained in:
0xJoeMama 2022-05-24 12:01:33 +03:00
parent 3e097faecc
commit 8ed8d43f5a
No known key found for this signature in database
GPG key ID: 6E0A65853073294E

View file

@ -16,6 +16,7 @@ indent = tab
tab-size = 4
*/
#include "robin_hood.h"
#include <fstream>
#include <ranges>
#include <cmath>
@ -1258,8 +1259,21 @@ namespace Proc {
int filter_found = 0;
detail_container detailed;
static robin_hood::unordered_set<size_t> kernels_procs{};
constexpr size_t KTHREADD = 2;
// Clearing the cache is used in the event of a pid wrap around.
// In that event processes that acquire old kernel pids would also be filtered out so we need to manually clean the cache every now and then.
static void clear_kernel_cache() {
static size_t latest_clear_time = 0;
if (latest_clear_time >= static_cast<size_t>(Config::getI("update_ms") * 100)) {
kernels_procs.clear();
latest_clear_time = 0;
}
latest_clear_time++;
}
//* Generate process tree list
void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<std::reference_wrapper<proc_info>>& out_procs, int cur_depth, const bool collapsed, const string& filter, bool found=false, const bool no_update=false, const bool should_filter=false) {
auto cur_pos = out_procs.size();
@ -1453,6 +1467,8 @@ namespace Proc {
//* ---------------------------------------------Collection start----------------------------------------------
else {
should_filter = true;
// First make sure kernel proc cache is cleared.
clear_kernel_cache();
auto totalMem = Mem::get_totalMem();
int totalMem_len = to_string(totalMem >> 10).size();
@ -1500,7 +1516,7 @@ namespace Proc {
const size_t pid = stoul(pid_str);
if (should_filter_kernel && pid == KTHREADD) {
if (should_filter_kernel && (pid == KTHREADD || kernels_procs.contains(pid))) {
continue;
}
found.push_back(pid);
@ -1637,8 +1653,10 @@ namespace Proc {
pread.close();
if (should_filter_kernel && new_proc.ppid == KTHREADD) {
kernels_procs.emplace(new_proc.pid);
current_procs.pop_back();
found.pop_back();
continue;
}
if (x-offset < 24) continue;