Commit 5d48ab8c authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Performance improvement hackathon: improve process comparison routines,

disable useless code in release builds such as runtime type-checking on
dynamic data structures and process fields that are not being computed,
faster(?) method for verifying the process owner (still need to ensure
correctness), don't destroy and create process objects for hidden kernel
threads over and over. Phew. I shouldn't be doing all this today, but I
could not resist.
parent 4c41e78b
What's new in version 0.6.3 What's new in version 0.6.3
* Performance improvements; uses much less CPU than the
previous release with the default setup.
* Use 64-bit values when storing processor times to * Use 64-bit values when storing processor times to
avoid overflow. avoid overflow.
* Memory consumption improvements, compensating storage
of 64-bit values.
* Internal change: rename TypedVector to Vector and * Internal change: rename TypedVector to Vector and
ListBox (and related classes) to Panel. ListBox (and related classes) to Panel.
......
...@@ -21,11 +21,15 @@ typedef struct CheckItem_ { ...@@ -21,11 +21,15 @@ typedef struct CheckItem_ {
}*/ }*/
#ifdef DEBUG
char* CHECKITEM_CLASS = "CheckItem"; char* CHECKITEM_CLASS = "CheckItem";
#else
#define CHECKITEM_CLASS NULL
#endif
CheckItem* CheckItem_new(char* text, bool* value) { CheckItem* CheckItem_new(char* text, bool* value) {
CheckItem* this = malloc(sizeof(CheckItem)); CheckItem* this = malloc(sizeof(CheckItem));
((Object*)this)->class = CHECKITEM_CLASS; Object_setClass(this, CHECKITEM_CLASS);
((Object*)this)->display = CheckItem_display; ((Object*)this)->display = CheckItem_display;
((Object*)this)->delete = CheckItem_delete; ((Object*)this)->delete = CheckItem_delete;
this->text = text; this->text = text;
......
...@@ -22,7 +22,11 @@ typedef struct CheckItem_ { ...@@ -22,7 +22,11 @@ typedef struct CheckItem_ {
} CheckItem; } CheckItem;
#ifdef DEBUG
extern char* CHECKITEM_CLASS; extern char* CHECKITEM_CLASS;
#else
#define CHECKITEM_CLASS NULL
#endif
CheckItem* CheckItem_new(char* text, bool* value); CheckItem* CheckItem_new(char* text, bool* value);
......
...@@ -30,7 +30,11 @@ typedef struct FunctionBar_ { ...@@ -30,7 +30,11 @@ typedef struct FunctionBar_ {
}*/ }*/
#ifdef DEBUG
char* FUNCTIONBAR_CLASS = "FunctionBar"; char* FUNCTIONBAR_CLASS = "FunctionBar";
#else
#define FUNCTIONBAR_CLASS NULL
#endif
static char* FunctionBar_FKeys[10] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10"}; static char* FunctionBar_FKeys[10] = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10"};
...@@ -40,7 +44,7 @@ static int FunctionBar_FEvents[10] = {KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KE ...@@ -40,7 +44,7 @@ static int FunctionBar_FEvents[10] = {KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KE
FunctionBar* FunctionBar_new(int size, char** functions, char** keys, int* events) { FunctionBar* FunctionBar_new(int size, char** functions, char** keys, int* events) {
FunctionBar* this = malloc(sizeof(FunctionBar)); FunctionBar* this = malloc(sizeof(FunctionBar));
((Object*) this)->class = FUNCTIONBAR_CLASS; Object_setClass(this, FUNCTIONBAR_CLASS);
((Object*) this)->delete = FunctionBar_delete; ((Object*) this)->delete = FunctionBar_delete;
this->functions = functions; this->functions = functions;
this->size = size; this->size = size;
......
...@@ -31,7 +31,11 @@ typedef struct FunctionBar_ { ...@@ -31,7 +31,11 @@ typedef struct FunctionBar_ {
} FunctionBar; } FunctionBar;
#ifdef DEBUG
extern char* FUNCTIONBAR_CLASS; extern char* FUNCTIONBAR_CLASS;
#else
#define FUNCTIONBAR_CLASS NULL
#endif
FunctionBar* FunctionBar_new(int size, char** functions, char** keys, int* events); FunctionBar* FunctionBar_new(int size, char** functions, char** keys, int* events);
......
...@@ -55,7 +55,7 @@ Hashtable* Hashtable_new(int size, bool owner) { ...@@ -55,7 +55,7 @@ Hashtable* Hashtable_new(int size, bool owner) {
} }
int Hashtable_hashAlgorithm(Hashtable* this, int key) { int Hashtable_hashAlgorithm(Hashtable* this, int key) {
return (key % this->size); return key % this->size;
} }
void Hashtable_delete(Hashtable* this) { void Hashtable_delete(Hashtable* this) {
...@@ -116,17 +116,20 @@ void* Hashtable_remove(Hashtable* this, int key) { ...@@ -116,17 +116,20 @@ void* Hashtable_remove(Hashtable* this, int key) {
} else } else
bucketPtr = &((*bucketPtr)->next); bucketPtr = &((*bucketPtr)->next);
} }
//#include <stdio.h>
inline void* Hashtable_get(Hashtable* this, int key) { inline void* Hashtable_get(Hashtable* this, int key) {
int index = this->hashAlgorithm(this, key); int index = this->hashAlgorithm(this, key);
HashtableItem* bucketPtr = this->buckets[index]; HashtableItem* bucketPtr = this->buckets[index];
while (true) // fprintf(stderr, "%d -> %d\n", key, index);
while (true) {
if (bucketPtr == NULL) { if (bucketPtr == NULL) {
return NULL; return NULL;
} else if (bucketPtr->key == key) { } else if (bucketPtr->key == key) {
return bucketPtr->value; return bucketPtr->value;
} else } else
bucketPtr = bucketPtr->next; bucketPtr = bucketPtr->next;
// fprintf(stderr, "*\n");
}
} }
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) { void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) {
......
...@@ -47,7 +47,7 @@ inline int Hashtable_size(Hashtable* this); ...@@ -47,7 +47,7 @@ inline int Hashtable_size(Hashtable* this);
void Hashtable_put(Hashtable* this, int key, void* value); void Hashtable_put(Hashtable* this, int key, void* value);
void* Hashtable_remove(Hashtable* this, int key); void* Hashtable_remove(Hashtable* this, int key);
//#include <stdio.h>
inline void* Hashtable_get(Hashtable* this, int key); inline void* Hashtable_get(Hashtable* this, int key);
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData); void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData);
......
...@@ -35,8 +35,8 @@ typedef struct Header_ { ...@@ -35,8 +35,8 @@ typedef struct Header_ {
Header* Header_new(ProcessList* pl) { Header* Header_new(ProcessList* pl) {
Header* this = malloc(sizeof(Header)); Header* this = malloc(sizeof(Header));
this->leftMeters = Vector_new(METER_CLASS, true, DEFAULT_SIZE); this->leftMeters = Vector_new(METER_CLASS, true, DEFAULT_SIZE, NULL);
this->rightMeters = Vector_new(METER_CLASS, true, DEFAULT_SIZE); this->rightMeters = Vector_new(METER_CLASS, true, DEFAULT_SIZE, NULL);
this->margin = true; this->margin = true;
this->pl = pl; this->pl = pl;
return this; return this;
......
...@@ -23,14 +23,17 @@ typedef struct ListItem_ { ...@@ -23,14 +23,17 @@ typedef struct ListItem_ {
}*/ }*/
#ifdef DEBUG
char* LISTITEM_CLASS = "ListItem"; char* LISTITEM_CLASS = "ListItem";
#else
#define LISTITEM_CLASS NULL
#endif
ListItem* ListItem_new(char* value, int key) { ListItem* ListItem_new(char* value, int key) {
ListItem* this = malloc(sizeof(ListItem)); ListItem* this = malloc(sizeof(ListItem));
((Object*)this)->class = LISTITEM_CLASS; Object_setClass(this, LISTITEM_CLASS);
((Object*)this)->display = ListItem_display; ((Object*)this)->display = ListItem_display;
((Object*)this)->delete = ListItem_delete; ((Object*)this)->delete = ListItem_delete;
((Object*)this)->compare = ListItem_compare;
this->value = String_copy(value); this->value = String_copy(value);
this->key = key; this->key = key;
return this; return this;
...@@ -62,7 +65,7 @@ const char* ListItem_getRef(ListItem* this) { ...@@ -62,7 +65,7 @@ const char* ListItem_getRef(ListItem* this) {
return this->value; return this->value;
} }
int ListItem_compare(const Object* cast1, const Object* cast2) { int ListItem_compare(const void* cast1, const void* cast2) {
ListItem* obj1 = (ListItem*) cast1; ListItem* obj1 = (ListItem*) cast1;
ListItem* obj2 = (ListItem*) cast2; ListItem* obj2 = (ListItem*) cast2;
return strcmp(obj1->value, obj2->value); return strcmp(obj1->value, obj2->value);
......
...@@ -24,7 +24,11 @@ typedef struct ListItem_ { ...@@ -24,7 +24,11 @@ typedef struct ListItem_ {
} ListItem; } ListItem;
#ifdef DEBUG
extern char* LISTITEM_CLASS; extern char* LISTITEM_CLASS;
#else
#define LISTITEM_CLASS NULL
#endif
ListItem* ListItem_new(char* value, int key); ListItem* ListItem_new(char* value, int key);
...@@ -36,7 +40,7 @@ void ListItem_display(Object* cast, RichString* out); ...@@ -36,7 +40,7 @@ void ListItem_display(Object* cast, RichString* out);
const char* ListItem_getRef(ListItem* this); const char* ListItem_getRef(ListItem* this);
int ListItem_compare(const Object* cast1, const Object* cast2); int ListItem_compare(const void* cast1, const void* cast2);
#endif #endif
...@@ -27,6 +27,9 @@ ColorsPanel.c ColorsPanel.h TraceScreen.c TraceScreen.h \ ...@@ -27,6 +27,9 @@ ColorsPanel.c ColorsPanel.h TraceScreen.c TraceScreen.h \
AvailableColumnsPanel.c AvailableColumnsPanel.h ColumnsPanel.c \ AvailableColumnsPanel.c AvailableColumnsPanel.h ColumnsPanel.c \
ColumnsPanel.h ColumnsPanel.h
profile:
$(MAKE) all CFLAGS="-pg -O2"
debug: debug:
$(MAKE) all CFLAGS="-g -DDEBUG" $(MAKE) all CFLAGS="-g -DDEBUG"
......
...@@ -104,7 +104,11 @@ typedef enum { ...@@ -104,7 +104,11 @@ typedef enum {
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#endif #endif
#ifdef DEBUG
char* METER_CLASS = "Meter"; char* METER_CLASS = "Meter";
#else
#define METER_CLASS NULL
#endif
MeterType* Meter_types[] = { MeterType* Meter_types[] = {
&CPUMeter, &CPUMeter,
...@@ -162,6 +166,9 @@ static RichString Meter_stringBuffer; ...@@ -162,6 +166,9 @@ static RichString Meter_stringBuffer;
Meter* Meter_new(ProcessList* pl, int param, MeterType* type) { Meter* Meter_new(ProcessList* pl, int param, MeterType* type) {
Meter* this = calloc(sizeof(Meter), 1); Meter* this = calloc(sizeof(Meter), 1);
Object_setClass(this, METER_CLASS);
((Object*)this)->delete = Meter_delete;
((Object*)this)->display = type->display;
this->h = 1; this->h = 1;
this->type = type; this->type = type;
this->param = param; this->param = param;
...@@ -169,9 +176,6 @@ Meter* Meter_new(ProcessList* pl, int param, MeterType* type) { ...@@ -169,9 +176,6 @@ Meter* Meter_new(ProcessList* pl, int param, MeterType* type) {
this->values = calloc(sizeof(double), type->items); this->values = calloc(sizeof(double), type->items);
this->total = type->total; this->total = type->total;
this->caption = strdup(type->caption); this->caption = strdup(type->caption);
((Object*)this)->delete = Meter_delete;
((Object*)this)->class = METER_CLASS;
((Object*)this)->display = type->display;
Meter_setMode(this, type->mode); Meter_setMode(this, type->mode);
if (this->type->init) if (this->type->init)
this->type->init(this); this->type->init(this);
......
...@@ -96,6 +96,7 @@ typedef enum { ...@@ -96,6 +96,7 @@ typedef enum {
#include "TasksMeter.h" #include "TasksMeter.h"
#include "LoadAverageMeter.h" #include "LoadAverageMeter.h"
#include "UptimeMeter.h" #include "UptimeMeter.h"
#include "ClockMeter.h"
#ifndef MIN #ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b))
...@@ -104,7 +105,11 @@ typedef enum { ...@@ -104,7 +105,11 @@ typedef enum {
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#endif #endif
#ifdef DEBUG
extern char* METER_CLASS; extern char* METER_CLASS;
#else
#define METER_CLASS NULL
#endif
extern MeterType* Meter_types[]; extern MeterType* Meter_types[];
......
...@@ -15,37 +15,37 @@ in the source distribution for its full text. ...@@ -15,37 +15,37 @@ in the source distribution for its full text.
#include "debug.h" #include "debug.h"
/*{ /*{
#ifndef DEBUG
#define Object_setClass(obj, class)
#endif
typedef struct Object_ Object; typedef struct Object_ Object;
typedef void(*Object_Display)(Object*, RichString*); typedef void(*Object_Display)(Object*, RichString*);
typedef int(*Object_Compare)(const Object*, const Object*); typedef int(*Object_Compare)(const void*, const void*);
typedef void(*Object_Delete)(Object*); typedef void(*Object_Delete)(Object*);
struct Object_ { struct Object_ {
#ifdef DEBUG
char* class; char* class;
#endif
Object_Display display; Object_Display display;
Object_Compare compare;
Object_Delete delete; Object_Delete delete;
}; };
}*/ }*/
static char* OBJECT_CLASS = "Object"; #ifdef DEBUG
char* OBJECT_CLASS = "Object";
void Object_new() { #else
Object* this; #define OBJECT_CLASS NULL
this = malloc(sizeof(Object)); #endif
this->class = OBJECT_CLASS;
this->display = Object_display;
this->compare = Object_compare;
this->delete = Object_delete;
}
bool Object_instanceOf(Object* this, char* class) { #ifdef DEBUG
return this->class == class;
}
void Object_delete(Object* this) { void Object_setClass(void* this, char* class) {
free(this); ((Object*)this)->class = class;
} }
void Object_display(Object* this, RichString* out) { void Object_display(Object* this, RichString* out) {
...@@ -54,6 +54,4 @@ void Object_display(Object* this, RichString* out) { ...@@ -54,6 +54,4 @@ void Object_display(Object* this, RichString* out) {
RichString_write(out, CRT_colors[DEFAULT_COLOR], objAddress); RichString_write(out, CRT_colors[DEFAULT_COLOR], objAddress);
} }
int Object_compare(const Object* this, const Object* o) { #endif
return (this - o);
}
...@@ -17,27 +17,38 @@ in the source distribution for its full text. ...@@ -17,27 +17,38 @@ in the source distribution for its full text.
#include "debug.h" #include "debug.h"
#ifndef DEBUG
#define Object_setClass(obj, class)
#endif
typedef struct Object_ Object; typedef struct Object_ Object;
typedef void(*Object_Display)(Object*, RichString*); typedef void(*Object_Display)(Object*, RichString*);
typedef int(*Object_Compare)(const Object*, const Object*); typedef int(*Object_Compare)(const void*, const void*);
typedef void(*Object_Delete)(Object*); typedef void(*Object_Delete)(Object*);
struct Object_ { struct Object_ {
#ifdef DEBUG
char* class; char* class;
#endif
Object_Display display; Object_Display display;
Object_Compare compare;
Object_Delete delete; Object_Delete delete;
}; };
void Object_new(); #ifdef DEBUG
extern char* OBJECT_CLASS;
bool Object_instanceOf(Object* this, char* class); #else
#define OBJECT_CLASS NULL
#endif
#ifdef DEBUG
void Object_delete(Object* this); void Object_setClass(void* this, char* class);
void Object_display(Object* this, RichString* out); void Object_display(Object* this, RichString* out);
int Object_compare(const Object* this, const Object* o); #endif
#endif #endif
...@@ -10,6 +10,7 @@ in the source distribution for its full text. ...@@ -10,6 +10,7 @@ in the source distribution for its full text.
#include "Vector.h" #include "Vector.h"
#include "CRT.h" #include "CRT.h"
#include "RichString.h" #include "RichString.h"
#include "ListItem.h"
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -54,12 +55,18 @@ struct Panel_ { ...@@ -54,12 +55,18 @@ struct Panel_ {
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#endif #endif
#ifdef DEBUG
char* PANEL_CLASS = "Panel"; char* PANEL_CLASS = "Panel";
#else
#define PANEL_CLASS NULL
#endif
Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner) { Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner, Object_Compare compare) {
Panel* this; Panel* this;
this = malloc(sizeof(Panel)); this = malloc(sizeof(Panel));
Panel_init(this, x, y, w, h, type, owner); Panel_init(this, x, y, w, h, type, owner);
this->items->compare = compare;
return this; return this;
} }
...@@ -71,14 +78,14 @@ void Panel_delete(Object* cast) { ...@@ -71,14 +78,14 @@ void Panel_delete(Object* cast) {
void Panel_init(Panel* this, int x, int y, int w, int h, char* type, bool owner) { void Panel_init(Panel* this, int x, int y, int w, int h, char* type, bool owner) {
Object* super = (Object*) this; Object* super = (Object*) this;
super->class = PANEL_CLASS; Object_setClass(this, PANEL_CLASS);
super->delete = Panel_delete; super->delete = Panel_delete;
this->x = x; this->x = x;
this->y = y; this->y = y;
this->w = w; this->w = w;
this->h = h; this->h = h;
this->eventHandler = NULL; this->eventHandler = NULL;
this->items = Vector_new(type, owner, DEFAULT_SIZE); this->items = Vector_new(type, owner, DEFAULT_SIZE, ListItem_compare);
this->scrollV = 0; this->scrollV = 0;
this->scrollH = 0; this->scrollH = 0;
this->selected = 0; this->selected = 0;
......
...@@ -55,9 +55,14 @@ struct Panel_ { ...@@ -55,9 +55,14 @@ struct Panel_ {
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#endif #endif
#ifdef DEBUG
extern char* PANEL_CLASS; extern char* PANEL_CLASS;
#else
#define PANEL_CLASS NULL
#endif
Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner); Panel* Panel_new(int x, int y, int w, int h, char* type, bool owner, Object_Compare compare);
void Panel_delete(Object* cast); void Panel_delete(Object* cast);
......
...@@ -62,16 +62,19 @@ typedef struct Process_ { ...@@ -62,16 +62,19 @@ typedef struct Process_ {
int tty_nr; int tty_nr;
int tpgid; int tpgid;
unsigned long int flags; unsigned long int flags;
#ifdef DEBUG
unsigned long int minflt; unsigned long int minflt;
unsigned long int cminflt; unsigned long int cminflt;
unsigned long int majflt; unsigned long int majflt;
unsigned long int cmajflt; unsigned long int cmajflt;
#endif
unsigned long int utime; unsigned long int utime;
unsigned long int stime; unsigned long int stime;
long int cutime; long int cutime;
long int cstime; long int cstime;
long int priority; long int priority;
long int nice; long int nice;
#ifdef DEBUG
long int itrealvalue; long int itrealvalue;
unsigned long int starttime; unsigned long int starttime;
unsigned long int vsize; unsigned long int vsize;
...@@ -89,6 +92,7 @@ typedef struct Process_ { ...@@ -89,6 +92,7 @@ typedef struct Process_ {
unsigned long int wchan; unsigned long int wchan;
unsigned long int nswap; unsigned long int nswap;
unsigned long int cnswap; unsigned long int cnswap;
#endif
int exit_signal; int exit_signal;
int processor; int processor;
int m_size; int m_size;
...@@ -106,7 +110,11 @@ typedef struct Process_ { ...@@ -106,7 +110,11 @@ typedef struct Process_ {
}*/ }*/
#ifdef DEBUG
char* PROCESS_CLASS = "Process"; char* PROCESS_CLASS = "Process";
#else
#define PROCESS_CLASS NULL
#endif
char *Process_fieldNames[] = { char *Process_fieldNames[] = {
"", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION", "TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT", "UTIME", "STIME", "CUTIME", "CSTIME", "PRIORITY", "NICE", "ITREALVALUE", "STARTTIME", "VSIZE", "RSS", "RLIM", "STARTCODE", "ENDCODE", "STARTSTACK", "KSTKESP", "KSTKEIP", "SIGNAL", "BLOCKED", "SIGIGNORE", "SIGCATCH", "WCHAN", "NSWAP", "CNSWAP", "EXIT_SIGNAL", "PROCESSOR", "M_SIZE", "M_RESIDENT", "M_SHARE", "M_TRS", "M_DRS", "M_LRS", "M_DT", "ST_UID", "PERCENT_CPU", "PERCENT_MEM", "USER", "TIME", "*** report bug! ***" "", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION", "TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT", "UTIME", "STIME", "CUTIME", "CSTIME", "PRIORITY", "NICE", "ITREALVALUE", "STARTTIME", "VSIZE", "RSS", "RLIM", "STARTCODE", "ENDCODE", "STARTSTACK", "KSTKESP", "KSTKEIP", "SIGNAL", "BLOCKED", "SIGIGNORE", "SIGCATCH", "WCHAN", "NSWAP", "CNSWAP", "EXIT_SIGNAL", "PROCESSOR", "M_SIZE", "M_RESIDENT", "M_SHARE", "M_TRS", "M_DRS", "M_LRS", "M_DT", "ST_UID", "PERCENT_CPU", "PERCENT_MEM", "USER", "TIME", "*** report bug! ***"
...@@ -114,9 +122,8 @@ char *Process_fieldNames[] = { ...@@ -114,9 +122,8 @@ char *Process_fieldNames[] = {
Process* Process_new(struct ProcessList_ *pl) { Process* Process_new(struct ProcessList_ *pl) {
Process* this = malloc(sizeof(Process)); Process* this = malloc(sizeof(Process));
((Object*)this)->class = PROCESS_CLASS; Object_setClass(this, PROCESS_CLASS);
((Object*)this)->display = Process_display; ((Object*)this)->display = Process_display;
((Object*)this)->compare = Process_compare;
((Object*)this)->delete = Process_delete; ((Object*)this)->delete = Process_delete;
this->pl = pl; this->pl = pl;
this->tag = false; this->tag = false;
...@@ -345,44 +352,57 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { ...@@ -345,44 +352,57 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) {
return; return;
} }
int Process_compare(const Object* v1, const Object* v2) { int Process_pidCompare(const void* v1, const void* v2) {
Process* p1 = (Process*)v1; Process* p1 = (Process*)v1;
Process* p2 = (Process*)v2; Process* p2 = (Process*)v2;
int direction = p1->pl->direction; return (p1->pid - p2->pid);
switch (p1->pl->sortKey) { }
int Process_compare(const void* v1, const void* v2) {
Process *p1, *p2;
ProcessList *pl = ((Process*)v1)->pl;
if (pl->direction == 1) {
p1 = (Process*)v1;
p2 = (Process*)v2;
} else {
p2 = (Process*)v1;
p1 = (Process*)v2;
}
switch (pl->sortKey) {
case PID: case PID:
return (p2->pid - p1->pid) * direction; return (p1->pid - p2->pid);
case PPID: case PPID:
return (p2->ppid - p1->ppid) * direction; return (p1->ppid - p2->ppid);
case USER: case USER:
return strcmp(p2->user, p1->user) * direction; return strcmp(p1->user, p2->user);
case PRIORITY: case PRIORITY:
return (p2->priority - p1->priority) * direction; return (p1->priority - p2->priority);
case STATE: case STATE:
return (p2->state - p1->state) * direction; return (p1->state - p2->state);
case NICE: case NICE:
return (p2->nice - p1->nice) * direction; return (p1->nice - p2->nice);
case M_SIZE: case M_SIZE:
return (p1->m_size - p2->m_size) * direction; return (p2->m_size - p1->m_size);
case M_RESIDENT: case M_RESIDENT:
return (p1->m_resident - p2->m_resident) * direction; return (p2->m_resident - p1->m_resident);
case M_SHARE: case M_SHARE:
return (p1->m_share - p2->m_share) * direction; return (p2->m_share - p1->m_share);
case PERCENT_CPU: case PERCENT_CPU:
return (p1->percent_cpu < p2->percent_cpu ? -1 : 1) * direction; return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
case PERCENT_MEM: case PERCENT_MEM:
return (p1->percent_mem < p2->percent_mem ? -1 : 1) * direction; return (p2->m_resident - p1->m_resident);
case UTIME: case UTIME:
return (p1->utime - p2->utime) * direction; return (p2->utime - p1->utime);
case STIME: case STIME:
return (p1->stime - p2->stime) * direction; return (p2->stime - p1->stime);
case TIME: case TIME:
return ((p1->utime+p1->stime) - (p2->utime+p2->stime)) * direction; return ((p2->utime+p2->stime) - (p1->utime+p1->stime));
case COMM: case COMM:
return strcmp(p2->comm, p1->comm) * direction; return strcmp(p1->comm, p2->comm);
default: default:
return (p2->pid - p1->pid) * direction; return (p1->pid - p2->pid);
} }
} }
char* Process_printField(ProcessField field) { char* Process_printField(ProcessField field) {
......
...@@ -64,16 +64,19 @@ typedef struct Process_ { ...@@ -64,16 +64,19 @@ typedef struct Process_ {
int tty_nr; int tty_nr;
int tpgid; int tpgid;
unsigned long int flags; unsigned long int flags;
#ifdef DEBUG
unsigned long int minflt; unsigned long int minflt;
unsigned long int cminflt; unsigned long int cminflt;
unsigned long int majflt; unsigned long int majflt;
unsigned long int cmajflt; unsigned long int cmajflt;
#endif
unsigned long int utime; unsigned long int utime;
unsigned long int stime; unsigned long int stime;
long int cutime; long int cutime;
long int cstime; long int cstime;
long int priority; long int priority;
long int nice; long int nice;
#ifdef DEBUG
long int itrealvalue; long int itrealvalue;
unsigned long int starttime; unsigned long int starttime;
unsigned long int vsize; unsigned long int vsize;
...@@ -91,6 +94,7 @@ typedef struct Process_ { ...@@ -91,6 +94,7 @@ typedef struct Process_ {
unsigned long int wchan; unsigned long int wchan;
unsigned long int nswap; unsigned long int nswap;
unsigned long int cnswap; unsigned long int cnswap;
#endif
int exit_signal; int exit_signal;
int processor; int processor;
int m_size; int m_size;
...@@ -107,7 +111,11 @@ typedef struct Process_ { ...@@ -107,7 +111,11 @@ typedef struct Process_ {
} Process; } Process;
#ifdef DEBUG
extern char* PROCESS_CLASS; extern char* PROCESS_CLASS;
#else
#define PROCESS_CLASS NULL
#endif
extern char *Process_fieldNames[]; extern char *Process_fieldNames[];
...@@ -131,7 +139,9 @@ void Process_sendSignal(Process* this, int signal); ...@@ -131,7 +139,9 @@ void Process_sendSignal(Process* this, int signal);
void Process_writeField(Process* this, RichString* str, ProcessField field); void Process_writeField(Process* this, RichString* str, ProcessField field);
int Process_compare(const Object* v1, const Object* v2); int Process_pidCompare(const void* v1, const void* v2);
int Process_compare(const void* v1, const void* v2);
char* Process_printField(ProcessField field); char* Process_printField(ProcessField field);
......
...@@ -15,6 +15,7 @@ in the source distribution for its full text. ...@@ -15,6 +15,7 @@ in the source distribution for its full text.
#include "Vector.h" #include "Vector.h"
#include "UsersTable.h" #include "UsersTable.h"
#include "Hashtable.h" #include "Hashtable.h"
#include "String.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -48,13 +49,17 @@ in the source distribution for its full text. ...@@ -48,13 +49,17 @@ in the source distribution for its full text.
#endif #endif
#ifndef MAX_READ #ifndef MAX_READ
#define MAX_READ 8192 #define MAX_READ 2048
#endif #endif
}*/ }*/
/*{ /*{
#ifdef DEBUG
typedef int(*vxscanf)(void*, const char*, va_list);
#endif
typedef struct ProcessList_ { typedef struct ProcessList_ {
Vector* processes; Vector* processes;
Vector* processes2; Vector* processes2;
...@@ -108,8 +113,6 @@ static ProcessField defaultHeaders[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RE ...@@ -108,8 +113,6 @@ static ProcessField defaultHeaders[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RE
#ifdef DEBUG #ifdef DEBUG
typedef int(*vxscanf)(void*, const char*, va_list);
#define ProcessList_read(this, buffer, format, ...) ProcessList_xread(this, (vxscanf) vsscanf, buffer, format, ## __VA_ARGS__ ) #define ProcessList_read(this, buffer, format, ...) ProcessList_xread(this, (vxscanf) vsscanf, buffer, format, ## __VA_ARGS__ )
#define ProcessList_fread(this, file, format, ...) ProcessList_xread(this, (vxscanf) vfscanf, file, format, ## __VA_ARGS__ ) #define ProcessList_fread(this, file, format, ...) ProcessList_xread(this, (vxscanf) vfscanf, file, format, ## __VA_ARGS__ )
...@@ -173,13 +176,13 @@ static inline int ProcessList_xread(ProcessList* this, vxscanf fn, void* buffer, ...@@ -173,13 +176,13 @@ static inline int ProcessList_xread(ProcessList* this, vxscanf fn, void* buffer,
ProcessList* ProcessList_new(UsersTable* usersTable) { ProcessList* ProcessList_new(UsersTable* usersTable) {
ProcessList* this; ProcessList* this;
this = malloc(sizeof(ProcessList)); this = malloc(sizeof(ProcessList));
this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE); this->processes = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare);
this->processTable = Hashtable_new(20, false); this->processTable = Hashtable_new(20, false);
this->prototype = Process_new(this); this->prototype = Process_new(this);
this->usersTable = usersTable; this->usersTable = usersTable;
/* tree-view auxiliary buffers */ /* tree-view auxiliary buffers */
this->processes2 = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE); this->processes2 = Vector_new(PROCESS_CLASS, true, DEFAULT_SIZE, Process_compare);
#ifdef DEBUG #ifdef DEBUG
this->traceFile = fopen("/tmp/htop-proc-trace", "w"); this->traceFile = fopen("/tmp/htop-proc-trace", "w");
...@@ -286,11 +289,8 @@ void ProcessList_add(ProcessList* this, Process* p) { ...@@ -286,11 +289,8 @@ void ProcessList_add(ProcessList* this, Process* p) {
void ProcessList_remove(ProcessList* this, Process* p) { void ProcessList_remove(ProcessList* this, Process* p) {
Hashtable_remove(this->processTable, p->pid); Hashtable_remove(this->processTable, p->pid);
ProcessField pf = this->sortKey; int index = Vector_indexOf(this->processes, p, Process_pidCompare);
this->sortKey = PID;
int index = Vector_indexOf(this->processes, p);
Vector_remove(this->processes, index); Vector_remove(this->processes, index);
this->sortKey = pf;
} }
Process* ProcessList_get(ProcessList* this, int index) { Process* ProcessList_get(ProcessList* this, int index) {
...@@ -302,7 +302,7 @@ int ProcessList_size(ProcessList* this) { ...@@ -302,7 +302,7 @@ int ProcessList_size(ProcessList* this) {
} }
static void ProcessList_buildTree(ProcessList* this, int pid, int level, int indent, int direction) { static void ProcessList_buildTree(ProcessList* this, int pid, int level, int indent, int direction) {
Vector* children = Vector_new(PROCESS_CLASS, false, DEFAULT_SIZE); Vector* children = Vector_new(PROCESS_CLASS, false, DEFAULT_SIZE, Process_compare);
for (int i = 0; i < Vector_size(this->processes); i++) { for (int i = 0; i < Vector_size(this->processes); i++) {
Process* process = (Process*) (Vector_get(this->processes, i)); Process* process = (Process*) (Vector_get(this->processes, i));
...@@ -352,7 +352,7 @@ void ProcessList_sort(ProcessList* this) { ...@@ -352,7 +352,7 @@ void ProcessList_sort(ProcessList* this) {
static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, char *command) { static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, char *command) {
static char buf[MAX_READ]; static char buf[MAX_READ];
long int zero; unsigned long int zero;
int size = fread(buf, 1, MAX_READ, f); int size = fread(buf, 1, MAX_READ, f);
if(!size) return 0; if(!size) return 0;
...@@ -370,6 +370,7 @@ static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, c ...@@ -370,6 +370,7 @@ static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, c
command[commsize] = '\0'; command[commsize] = '\0';
location = end + 2; location = end + 2;
#ifdef DEBUG
int num = ProcessList_read(this, location, int num = ProcessList_read(this, location,
"%c %d %d %d %d %d %lu %lu %lu %lu " "%c %d %d %d %d %d %lu %lu %lu %lu "
"%lu %lu %lu %ld %ld %ld %ld %ld %ld " "%lu %lu %lu %ld %ld %ld %ld %ld %ld "
...@@ -377,14 +378,34 @@ static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, c ...@@ -377,14 +378,34 @@ static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, c
"%lu %lu %lu %lu %lu %lu %lu %lu " "%lu %lu %lu %lu %lu %lu %lu %lu "
"%d %d", "%d %d",
&proc->state, &proc->ppid, &proc->pgrp, &proc->session, &proc->tty_nr, &proc->state, &proc->ppid, &proc->pgrp, &proc->session, &proc->tty_nr,
&proc->tpgid, &proc->flags, &proc->minflt, &proc->cminflt, &proc->majflt, &proc->tpgid, &proc->flags,
&proc->cmajflt, &proc->utime, &proc->stime, &proc->cutime, &proc->cstime, &proc->minflt, &proc->cminflt, &proc->majflt, &proc->cmajflt,
&proc->utime, &proc->stime, &proc->cutime, &proc->cstime,
&proc->priority, &proc->nice, &zero, &proc->itrealvalue, &proc->priority, &proc->nice, &zero, &proc->itrealvalue,
&proc->starttime, &proc->vsize, &proc->rss, &proc->rlim, &proc->starttime, &proc->vsize, &proc->rss, &proc->rlim,
&proc->startcode, &proc->endcode, &proc->startstack, &proc->kstkesp, &proc->startcode, &proc->endcode, &proc->startstack, &proc->kstkesp,
&proc->kstkeip, &proc->signal, &proc->blocked, &proc->sigignore, &proc->kstkeip, &proc->signal, &proc->blocked, &proc->sigignore,
&proc->sigcatch, &proc->wchan, &proc->nswap, &proc->cnswap, &proc->sigcatch, &proc->wchan, &proc->nswap, &proc->cnswap,
&proc->exit_signal, &proc->processor); &proc->exit_signal, &proc->processor);
#else
long int uzero;
int num = ProcessList_read(this, location,
"%c %d %d %d %d %d %lu %lu %lu %lu "
"%lu %lu %lu %ld %ld %ld %ld %ld %ld "
"%lu %lu %ld %lu %lu %lu %lu %lu "
"%lu %lu %lu %lu %lu %lu %lu %lu "
"%d %d",
&proc->state, &proc->ppid, &proc->pgrp, &proc->session, &proc->tty_nr,
&proc->tpgid, &proc->flags,
&zero, &zero, &zero, &zero,
&proc->utime, &proc->stime, &proc->cutime, &proc->cstime,
&proc->priority, &proc->nice, &uzero, &uzero,
&zero, &zero, &uzero, &zero,
&zero, &zero, &zero, &zero,
&zero, &zero, &zero, &zero,
&zero, &zero, &zero, &zero,
&proc->exit_signal, &proc->processor);
#endif
// This assert is always valid on 2.4, but reportedly not always valid on 2.6. // This assert is always valid on 2.4, but reportedly not always valid on 2.6.
// TODO: Check if the semantics of this field has changed. // TODO: Check if the semantics of this field has changed.
...@@ -397,14 +418,15 @@ static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, c ...@@ -397,14 +418,15 @@ static int ProcessList_readStatFile(ProcessList* this, Process *proc, FILE *f, c
bool ProcessList_readStatusFile(ProcessList* this, Process* proc, char* dirname, char* name) { bool ProcessList_readStatusFile(ProcessList* this, Process* proc, char* dirname, char* name) {
char statusfilename[MAX_NAME+1]; char statusfilename[MAX_NAME+1];
statusfilename[MAX_NAME] = '\0'; statusfilename[MAX_NAME] = '\0';
/*
bool success = false;
char buffer[256];
buffer[255] = '\0';
snprintf(statusfilename, MAX_NAME, "%s/%s/status", dirname, name); snprintf(statusfilename, MAX_NAME, "%s/%s/status", dirname, name);
FILE* status = ProcessList_fopen(this, statusfilename, "r"); FILE* status = ProcessList_fopen(this, statusfilename, "r");
bool success = false;
if (status) { if (status) {
char buffer[1024];
buffer[1023] = '\0';
while (!feof(status)) { while (!feof(status)) {
char* ok = fgets(buffer, 1023, status); char* ok = fgets(buffer, 255, status);
if (!ok) if (!ok)
break; break;
if (String_startsWith(buffer, "Uid:")) { if (String_startsWith(buffer, "Uid:")) {
...@@ -421,14 +443,18 @@ bool ProcessList_readStatusFile(ProcessList* this, Process* proc, char* dirname, ...@@ -421,14 +443,18 @@ bool ProcessList_readStatusFile(ProcessList* this, Process* proc, char* dirname,
fclose(status); fclose(status);
} }
if (!success) { if (!success) {
snprintf(statusfilename, MAX_NAME, "%s/%s/stat", dirname, name); */
snprintf(statusfilename, MAX_NAME, "%s/%s", dirname, name);
struct stat sstat; struct stat sstat;
int statok = stat(statusfilename, &sstat); int statok = stat(statusfilename, &sstat);
if (statok == -1) if (statok == -1)
return false; return false;
proc->st_uid = sstat.st_uid; proc->st_uid = sstat.st_uid;
} return true;
return success; /*
} else
return true;
*/
} }
void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, float period) { void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, float period) {
...@@ -469,24 +495,24 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl ...@@ -469,24 +495,24 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl
char command[PROCESS_COMM_LEN + 1]; char command[PROCESS_COMM_LEN + 1];
Process* process; Process* process;
Process* existingProcess = (Process*) Hashtable_get(this->processTable, pid); Process* existingProcess = (Process*) Hashtable_get(this->processTable, pid);
if (!existingProcess) { if (existingProcess) {
process = Process_clone(prototype); process = existingProcess;
} else {
process = prototype;
process->comm = NULL;
process->pid = pid; process->pid = pid;
ProcessList_add(this, process);
if (! ProcessList_readStatusFile(this, process, dirname, name)) if (! ProcessList_readStatusFile(this, process, dirname, name))
goto errorReadingProcess; goto errorReadingProcess;
} else {
process = existingProcess;
}
process->updated = true;
char* username = UsersTable_getRef(this->usersTable, process->st_uid); char* username = UsersTable_getRef(this->usersTable, process->st_uid);
if (username) { if (username) {
strncpy(process->user, username, PROCESS_USER_LEN); strncpy(process->user, username, PROCESS_USER_LEN);
} else { } else {
snprintf(process->user, PROCESS_USER_LEN, "%d", process->st_uid); snprintf(process->user, PROCESS_USER_LEN, "%d", process->st_uid);
} }
}
process->updated = true;
int lasttimes = (process->utime + process->stime); int lasttimes = (process->utime + process->stime);
...@@ -498,13 +524,30 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl ...@@ -498,13 +524,30 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl
int success = ProcessList_readStatFile(this, process, status, command); int success = ProcessList_readStatFile(this, process, status, command);
fclose(status); fclose(status);
if(!success) { if(!success)
goto errorReadingProcess; goto errorReadingProcess;
}
process->percent_cpu = (process->utime + process->stime - lasttimes) / process->percent_cpu = (process->utime + process->stime - lasttimes) /
period * 100.0; period * 100.0;
snprintf(statusfilename, MAX_NAME, "%s/%s/statm", dirname, name);
status = ProcessList_fopen(this, statusfilename, "r");
if(!status) {
goto errorReadingProcess;
}
int num = ProcessList_fread(this, status, "%d %d %d %d %d %d %d",
&process->m_size, &process->m_resident, &process->m_share,
&process->m_trs, &process->m_drs, &process->m_lrs,
&process->m_dt);
fclose(status);
if(num != 7)
goto errorReadingProcess;
if (this->hideKernelThreads && process->m_size == 0)
goto errorReadingProcess;
if(!existingProcess) { if(!existingProcess) {
snprintf(statusfilename, MAX_NAME, "%s/%s/cmdline", dirname, name); snprintf(statusfilename, MAX_NAME, "%s/%s/cmdline", dirname, name);
status = ProcessList_fopen(this, statusfilename, "r"); status = ProcessList_fopen(this, statusfilename, "r");
...@@ -524,21 +567,6 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl ...@@ -524,21 +567,6 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl
fclose(status); fclose(status);
} }
snprintf(statusfilename, MAX_NAME, "%s/%s/statm", dirname, name);
status = ProcessList_fopen(this, statusfilename, "r");
if(!status) {
goto errorReadingProcess;
}
int num = ProcessList_fread(this, status, "%d %d %d %d %d %d %d",
&process->m_size, &process->m_resident, &process->m_share,
&process->m_trs, &process->m_drs, &process->m_lrs,
&process->m_dt);
fclose(status);
if(num != 7)
goto errorReadingProcess;
process->percent_mem = process->m_resident / process->percent_mem = process->m_resident /
(float)(this->usedMem - this->cachedMem - this->buffersMem) * (float)(this->usedMem - this->cachedMem - this->buffersMem) *
100.0; 100.0;
...@@ -548,17 +576,25 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl ...@@ -548,17 +576,25 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl
this->runningTasks++; this->runningTasks++;
} }
if (this->hideKernelThreads && process->m_size == 0) if (!existingProcess) {
ProcessList_remove(this, process); process = Process_clone(process);
ProcessList_add(this, process);
}
continue; continue;
// Exception handler. // Exception handler.
errorReadingProcess: { errorReadingProcess: {
if (existingProcess)
ProcessList_remove(this, process); ProcessList_remove(this, process);
else {
if (process->comm)
free(process->comm);
}
} }
} }
} }
prototype->comm = NULL;
closedir(dir); closedir(dir);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment