From 9268165d71b6abe4fd3aeaa23896368856028936 Mon Sep 17 00:00:00 2001 From: Magliano Andrea Date: Thu, 28 Jun 2012 19:04:31 +0200 Subject: [PATCH] Usable state reached (use top.cc + minor changes) * process* management left to top.cc * compile with -Wall -Werror (lots of wasted time for stupid errors) * unify find_process() and new_process(), as used always together Signed-off-by: Pavel Labath --- src/common.cc | 2 +- src/core.cc | 4 ++-- src/linux.cc | 11 ++------- src/top.cc | 64 ++++++++++++++++++++++++++++++--------------------- src/top.h | 3 +-- 5 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/common.cc b/src/common.cc index 36ea4a10..b9e0c1ee 100644 --- a/src/common.cc +++ b/src/common.cc @@ -113,7 +113,7 @@ int update_uname(void) strcpy(info.uname_v, strsep(&start, " ")); } - if (errno == ENOMEM) printf("desc_n %d\n", desc_n); + if (errno == ENOMEM) printf("desc_n %lu\n", desc_n); } #endif diff --git a/src/core.cc b/src/core.cc index 7c2b881e..ebc0fbe5 100644 --- a/src/core.cc +++ b/src/core.cc @@ -1209,7 +1209,7 @@ struct text_object *construct_text_object(char *s, const char *arg, extract_object_args_to_sub(obj, arg); obj->callbacks.print = &print_pid_write; #ifdef __DragonFly__ - END OBJ(processes, &update_tmp_top) + END OBJ(processes, &update_top) #else END OBJ(processes, &update_total_processes) #endif @@ -1226,7 +1226,7 @@ struct text_object *construct_text_object(char *s, const char *arg, obj->callbacks.print = &print_running_threads; #else #if defined(__DragonFly__) - END OBJ(running_processes, &update_tmp_top) + END OBJ(running_processes, &update_top) obj->callbacks.print = &print_running_processes; #else END OBJ(running_processes, &update_running_processes) diff --git a/src/linux.cc b/src/linux.cc index 99844eef..30c31f72 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -2812,15 +2812,8 @@ static void update_process_table(void) } if (sscanf(entry->d_name, "%d", &pid) > 0) { - struct process *p; - - p = find_process(pid); - if (!p) { - p = new_process(pid); - } - - /* compute each process cpu usage */ - calculate_stats(p); + /* compute each process cpu usage */ + calculate_stats(get_process(pid)); } } diff --git a/src/top.cc b/src/top.cc index 9fe1dfe5..abe4535a 100644 --- a/src/top.cc +++ b/src/top.cc @@ -75,7 +75,6 @@ static void unhash_process(struct process *p) /* get the bucket head */ phe = &proc_hash_table[p->pid & (HTABSIZE - 1)]; - /* find the entry pointing to p and drop it */ while (phe->next) { if (phe->next->proc == p) { @@ -138,7 +137,7 @@ struct process *get_process_by_name(const char *name) return 0; } -struct process *find_process(pid_t pid) +static struct process *find_process(pid_t pid) { struct proc_hash_entry *phe; @@ -148,43 +147,56 @@ struct process *find_process(pid_t pid) return phe->next->proc; phe = phe->next; } - return 0; + return NULL; } -/* Create a new process object and insert it into the process list */ -struct process *new_process(int p) +static struct process *new_process(pid_t pid) { - struct process *process; - process = (struct process *) malloc(sizeof(struct process)); - - // clean up memory first - memset(process, 0, sizeof(struct process)); + struct process *p = (struct process *) malloc(sizeof(struct process)); /* Do stitching necessary for doubly linked list */ - process->name = 0; - process->previous = 0; - process->next = first_process; - if (process->next) { - process->next->previous = process; + p->previous = NULL; + p->next = first_process; + if (p->next) { + p->next->previous = p; } - first_process = process; + first_process = p; - process->pid = p; - process->time_stamp = 0; - process->previous_user_time = ULONG_MAX; - process->previous_kernel_time = ULONG_MAX; + p->pid = pid; + p->name = 0; + p->amount = 0; + p->user_time = 0; + p->total = 0; + p->kernel_time = 0; + p->previous_user_time = ULONG_MAX; + p->previous_kernel_time = ULONG_MAX; + p->total_cpu_time = 0; + p->vsize = 0; + p->rss = 0; #ifdef BUILD_IOSTATS - process->previous_read_bytes = ULLONG_MAX; - process->previous_write_bytes = ULLONG_MAX; + p->read_bytes = 0; + p->previous_read_bytes = ULLONG_MAX; + p->write_bytes = 0; + p->previous_write_bytes = ULLONG_MAX; + p->io_perc = 0; #endif /* BUILD_IOSTATS */ - process->counted = 1; + p->time_stamp = 0; + p->counted = 1; + p->changed = 0; - /* process_find_name(process); */ + /* process_find_name(p); */ /* add the process to the hash table */ - hash_process(process); + hash_process(p); - return process; + return p; +} + +/* Get / create a new process object and insert it into the process list */ +struct process *get_process(pid_t pid) +{ + struct process *p = find_process(pid); + return p ? p : new_process(pid); } /****************************************** diff --git a/src/top.h b/src/top.h index 60010e6d..e9aabe61 100644 --- a/src/top.h +++ b/src/top.h @@ -128,7 +128,6 @@ void get_top_info(void); extern struct process *first_process; extern unsigned long g_time; -struct process *find_process(pid_t pid); -struct process *new_process(int p); +struct process *get_process(pid_t pid); #endif /* _top_h_ */