Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsaou committed Oct 2, 2024
1 parent 8c1e3c7 commit c7a6d8c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
16 changes: 10 additions & 6 deletions src/collectors/apps.plugin/apps_aggregations.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,17 @@ static void assign_a_target_to_all_processes(void) {
// assign targets from their parents, if they have
for(struct pid_stat *p = root_of_pids(); p ; p = p->next) {
if(!p->target) {
for(struct pid_stat *pp = p->parent ; pp ; pp = pp->parent) {
if(pp->target) {
if(pp->matched_by_config) {
// we are only interested about app_groups.conf matches
p->target = pp->target;
if(!p->is_manager) {
for (struct pid_stat *pp = p->parent; pp; pp = pp->parent) {
if(pp->is_manager) break;

if (pp->target) {
if (pp->matched_by_config) {
// we are only interested about app_groups.conf matches
p->target = pp->target;
}
break;
}
break;
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/collectors/apps.plugin/apps_groups.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Linux process managers
#managers: init systemd containerd-shim-runc-v2 dumb-init gnome-shell docker-init
#managers: openrc-run.sh crond
#managers: openrc-run.sh crond plasmashell xfwm4

## FreeBSD process managers
#managers: init
Expand Down Expand Up @@ -218,12 +218,6 @@ ceph: ceph cephadm osdmaptool crushtool rados rbd
cifs: smbd nmbd winbindd ctdbd ctdb-* ctdb_*
cifs: cifsd cifscreds cifs.upcall

## Fuse
fuse: fuse* fuseiso sshfs curlftpfs

## tmpfs
tmpfs: kdevtmpfs

## Apple Filling Protocol (AFP)
afp: netatalk afpd cnid_dbd cnid_metad

Expand All @@ -237,7 +231,16 @@ systemd: systemd systemd-*
desktop: gnome-* gsd-* gjs goa-* gcr-* gvfs-* *xdg-*-gnome* passimd gvfsd*
desktop: at-spi-* at-spi2-* dconf-service gcr-*

## KDE
desktop: plasmashell kwin-* kde* *-kde-* klauncher kactivitymanagerd krunner
desktop: kdeconnectd ksmserver kglobalaccel5 plasma-* *org.kde.*
desktop: sddm* kwalletd5 knotify5 kmix kscreen kwayland-*

## XFCE4
desktop: xfce4-* xfwm4 xfdesktop xfce4-panel xfsettingsd xfconfd
desktop: lightdm lightdm-*

## Generic tools related to desktop
desktop: gdm gdm-* dbus-* xdg-* ibus-* evolution-* accounts-daemon colord
desktop: geoclue pulse* pipewire* wireplumber jack* touchegg
desktop: geoclue pulse* pipewire* wireplumber jack* touchegg pulseaudio
desktop: Xwayland Xorg
3 changes: 3 additions & 0 deletions src/collectors/apps.plugin/apps_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ void update_pid_comm(struct pid_stat *p, const char *comm) {
string_freez(p->comm);
p->comm = comm_from_cmdline(buf, p->cmdline);

p->is_manager = is_process_manager(p);
p->is_aggregator = is_process_aggregator(p);

// the process changed comm, we may have to reassign it to
// an apps_groups.conf target.
p->target = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/collectors/apps.plugin/apps_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ int main(int argc, char **argv) {
procfile_adaptive_initial_allocation = 1;
os_get_system_HZ();
os_get_system_cpus_uncached();
apps_orchestrators_and_aggregators_init(); // before parsing args!
apps_managers_and_aggregators_init(); // before parsing args!
parse_args(argc, argv);

#if !defined(OS_WINDOWS)
Expand Down
5 changes: 4 additions & 1 deletion src/collectors/apps.plugin/apps_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ struct pid_stat {
bool updated:1; // true when the process is currently running
bool merged:1; // true when it has been merged to its parent
bool keep:1; // true when we need to keep this process in memory even after it exited
bool is_manager:1; // true when this pid is a process manager
bool is_aggregator:1; // true when this pid is a process aggregator

bool matched_by_config:1;

Expand Down Expand Up @@ -632,7 +634,7 @@ bool managed_log(struct pid_stat *p, PID_LOG log, bool status);
#define pid_incremental_cpu(type, idx, value) \
incremental_rate(p->values[idx], p->raw[idx], value, p->type##_collected_usec, p->last_##type##_collected_usec, CPU_TO_NANOSECONDCORES)

void apps_orchestrators_and_aggregators_init(void);
void apps_managers_and_aggregators_init(void);
void apps_users_and_groups_init(void);
void apps_pids_init(void);

Expand Down Expand Up @@ -677,6 +679,7 @@ void del_pid_entry(pid_t pid);
void update_pid_comm(struct pid_stat *p, const char *comm);

bool is_process_manager(struct pid_stat *p);
bool is_process_aggregator(struct pid_stat *p);

// --------------------------------------------------------------------------------------------------------------------
// targets management
Expand Down
27 changes: 15 additions & 12 deletions src/collectors/apps.plugin/apps_targets.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct target *find_target_by_name(struct target *base, const char *name) {
}

// --------------------------------------------------------------------------------------------------------------------
// Tree
// Process managers and aggregators

struct comm_list {
STRING *comm;
Expand Down Expand Up @@ -83,7 +83,7 @@ static void managed_list_add(struct managed_list *list, const char *s) {

static STRING *KernelAggregator = NULL;

void apps_orchestrators_and_aggregators_init(void) {
void apps_managers_and_aggregators_init(void) {
KernelAggregator = string_strdupz("kernel");

managed_list_clear(&tree.managers);
Expand All @@ -96,6 +96,8 @@ void apps_orchestrators_and_aggregators_init(void) {
managed_list_add(&tree.managers, "openrc-run.sh"); // openrc
managed_list_add(&tree.managers, "crond"); // linux crond
managed_list_add(&tree.managers, "gnome-shell"); // gnome user applications
managed_list_add(&tree.managers, "plasmashell"); // kde user applications
managed_list_add(&tree.managers, "xfwm4"); // xfce4 user applications
#elif defined(OS_WINDOWS)
managed_list_add(&tree.managers, "System");
managed_list_add(&tree.managers, "services");
Expand Down Expand Up @@ -126,7 +128,7 @@ bool is_process_manager(struct pid_stat *p) {
return false;
}

static inline bool is_aggregator(struct pid_stat *p) {
bool is_process_aggregator(struct pid_stat *p) {
for(size_t c = 0; c < tree.aggregators.used ; c++) {
if(p->comm == tree.aggregators.array[c].comm ||
p->comm_orig == tree.aggregators.array[c].comm)
Expand All @@ -136,31 +138,32 @@ static inline bool is_aggregator(struct pid_stat *p) {
return false;
}

// --------------------------------------------------------------------------------------------------------------------
// Tree

struct target *get_tree_target(struct pid_stat *p) {
// // skip fast all the children that are more than 3 levels down
// while(p->parent && p->parent->pid != INIT_PID && p->parent->parent && p->parent->parent->parent)
// p = p->parent;

// keep the children of INIT_PID, and process orchestrators
while(p->parent && p->parent->pid != INIT_PID && p->parent->pid != 0 && !is_process_manager(p->parent))
while(p->parent && p->parent->pid != INIT_PID && p->parent->pid != 0 && !p->parent->is_manager)
p = p->parent;

// merge all processes into process aggregators
STRING *search_for = string_dup(p->comm);
bool aggregator = false;
if((p->ppid == 0 && p->pid != INIT_PID) || (p->parent && is_aggregator(p->parent))) {
aggregator = true;
STRING *search_for = NULL;
if((p->ppid == 0 && p->pid != INIT_PID) || (p->parent && p->parent->is_aggregator)) {
search_for = string_dup(KernelAggregator);
}

if(!aggregator) {
else {
#if (PROCESSES_HAVE_COMM_AND_NAME == 1)
search_for = sanitize_chart_meta_string(p->name ? p->name : p->comm);
search_for = string_dup(p->name ? p->name : p->comm);
#else
search_for = string_dup(p->comm);
#endif
}

// find an existing target with the required name
struct target *w;
for(w = apps_groups_root_target; w ; w = w->next) {
if (w->name == search_for) {
Expand All @@ -172,7 +175,7 @@ struct target *get_tree_target(struct pid_stat *p) {
w = callocz(sizeof(struct target), 1);
w->type = TARGET_TYPE_TREE;
w->starts_with = w->ends_with = false;
w->ag.compare = string_dup(p->comm);
w->ag.compare = string_dup(search_for);
w->id = search_for;
w->name = string_dup(search_for);
w->clean_name = get_clean_name(w->name);
Expand Down

0 comments on commit c7a6d8c

Please sign in to comment.