Skip to content

Commit

Permalink
simplify proctable implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Aug 31, 2024
1 parent 940e7e5 commit 03d97a2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 76 deletions.
123 changes: 53 additions & 70 deletions src/process_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@
#include "process_group.h"
#include "list.h"
#include "util.h"
#include "uthash.h"

static void clear_proctable(struct process_group *pgroup)
{
struct process *proc, *tmp;
HASH_ITER(h_proctable, pgroup->proctable, proc, tmp)
{
HASH_DELETE(h_proctable, pgroup->proctable, proc);
free(proc);
}
}

static struct process *dump_process(struct process *proc)
{
struct process *p = (struct process *)malloc(sizeof(struct process));
if (p == NULL)
{
exit(-1);
}
memcpy(p, proc, sizeof(struct process));
return p;
}

static void add_to_proctable(struct process_group *pgroup, struct process *proc)
{
HASH_ADD(h_proctable, pgroup->proctable, pid, sizeof(pid_t), proc);
}

/* look for a process by pid
search_pid : pid of the wanted process
Expand Down Expand Up @@ -113,8 +140,7 @@ pid_t find_process_by_name(char *process_name)

int init_process_group(struct process_group *pgroup, pid_t target_pid, int include_children)
{
/* hashtable initialization */
memset(&pgroup->proctable, 0, sizeof(pgroup->proctable));
clear_proctable(pgroup);
pgroup->target_pid = target_pid;
pgroup->include_children = include_children;
pgroup->proclist = (struct list *)malloc(sizeof(struct list));
Expand All @@ -133,18 +159,7 @@ int init_process_group(struct process_group *pgroup, pid_t target_pid, int inclu

int close_process_group(struct process_group *pgroup)
{
int i;
int size = sizeof(pgroup->proctable) / sizeof(struct process *);
for (i = 0; i < size; i++)
{
if (pgroup->proctable[i] != NULL)
{
/* free() history for each process */
destroy_list(pgroup->proctable[i]);
free(pgroup->proctable[i]);
pgroup->proctable[i] = NULL;
}
}
clear_proctable(pgroup);
clear_list(pgroup->proclist);
free(pgroup->proclist);
pgroup->proclist = NULL;
Expand All @@ -158,7 +173,7 @@ int close_process_group(struct process_group *pgroup)
void update_process_group(struct process_group *pgroup)
{
struct process_iterator it;
struct process tmp_process;
struct process tmp_process, *p, *p_new;
struct process_filter filter;
struct timespec now;
double dt;
Expand All @@ -176,63 +191,33 @@ void update_process_group(struct process_group *pgroup)

while (get_next_process(&it, &tmp_process) != -1)
{
int hashkey = pid_hashfn(tmp_process.pid);
if (pgroup->proctable[hashkey] == NULL)
HASH_FIND(h_proctable, pgroup->proctable, &(tmp_process.pid), sizeof(pid_t), p);
if (p == NULL)
{
/* empty bucket */
struct process *new_process = (struct process *)malloc(sizeof(struct process));
if (new_process == NULL)
{
exit(-1);
}
pgroup->proctable[hashkey] = (struct list *)malloc(sizeof(struct list));
if (pgroup->proctable[hashkey] == NULL)
{
exit(-1);
}
tmp_process.cpu_usage = -1;
memcpy(new_process, &tmp_process, sizeof(struct process));
init_list(pgroup->proctable[hashkey], sizeof(pid_t));
add_elem(pgroup->proctable[hashkey], new_process);
add_elem(pgroup->proclist, new_process);
p_new = dump_process(&tmp_process);
add_to_proctable(pgroup, p_new);
add_elem(pgroup->proclist, p_new);
}
else
{
/* existing bucket */
struct process *p = (struct process *)locate_elem(pgroup->proctable[hashkey], &tmp_process);
if (p == NULL)
double sample;
add_elem(pgroup->proclist, p);
if (dt < MIN_DT)
continue;
/* process exists. update CPU usage */
sample = (tmp_process.cputime - p->cputime) / dt;
if (p->cpu_usage < 0)
{
/* process is new. add it */
struct process *new_process = (struct process *)malloc(sizeof(struct process));
if (new_process == NULL)
{
exit(-1);
}
tmp_process.cpu_usage = -1;
memcpy(new_process, &tmp_process, sizeof(struct process));
add_elem(pgroup->proctable[hashkey], new_process);
add_elem(pgroup->proclist, new_process);
/* initialization */
p->cpu_usage = sample;
}
else
{
double sample;
add_elem(pgroup->proclist, p);
if (dt < MIN_DT)
continue;
/* process exists. update CPU usage */
sample = (tmp_process.cputime - p->cputime) / dt;
if (p->cpu_usage < 0)
{
/* initialization */
p->cpu_usage = sample;
}
else
{
/* usage adjustment */
p->cpu_usage = (1.0 - ALPHA) * p->cpu_usage + ALPHA * sample;
}
p->cputime = tmp_process.cputime;
/* usage adjustment */
p->cpu_usage = (1.0 - ALPHA) * p->cpu_usage + ALPHA * sample;
}
p->cputime = tmp_process.cputime;
}
}
close_process_iterator(&it);
Expand All @@ -243,13 +228,11 @@ void update_process_group(struct process_group *pgroup)

int remove_process(struct process_group *pgroup, pid_t pid)
{
int hashkey = pid_hashfn(pid);
struct list_node *node;
if (pgroup->proctable[hashkey] == NULL)
return 1; /* nothing to delete */
node = (struct list_node *)locate_node(pgroup->proctable[hashkey], &pid);
if (node == NULL)
return 2;
delete_node(pgroup->proctable[hashkey], node);
struct process *p;
HASH_FIND(h_proctable, pgroup->proctable, &pid, sizeof(pid_t), p);
if (p == NULL)
return 1;
HASH_DELETE(h_proctable, pgroup->proctable, p);
free(p);
return 0;
}
7 changes: 1 addition & 6 deletions src/process_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@
#include <string.h>

#include "process_iterator.h"

#include "list.h"

#define PIDHASH_SZ 1024
#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))

struct process_group
{
/* hashtable with all the processes (array of struct list of struct process) */
struct list *proctable[PIDHASH_SZ];
struct process *proctable;
struct list *proclist;
pid_t target_pid;
int include_children;
Expand Down
4 changes: 4 additions & 0 deletions src/process_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <kvm.h>
#endif

#include "uthash.h"

/* process descriptor */
struct process
{
Expand All @@ -53,6 +55,8 @@ struct process
char command[PATH_MAX + 1];
/* maximum command length */
int max_cmd_len;

UT_hash_handle h_proctable;
};

struct process_filter
Expand Down

0 comments on commit 03d97a2

Please sign in to comment.