Builds on Linux again!

This commit is contained in:
Hisham Muhammad 2014-11-24 19:22:50 -02:00
parent 26422af608
commit aaaaf063a1
9 changed files with 105 additions and 82 deletions

View File

@ -8,6 +8,7 @@ in the source distribution for its full text.
#include "Process.h" #include "Process.h"
#include "Panel.h" #include "Panel.h"
#include "Action.h" #include "Action.h"
#include "ScreenManager.h"
/*{ /*{
@ -38,6 +39,14 @@ typedef bool(*Action_ForeachProcessFn)(Process*, size_t);
}*/ }*/
int Action_selectedPid(Panel* panel) {
Process* p = (Process*) Panel_getSelected(panel);
if (p) {
return p->pid;
}
return -1;
}
bool Action_foreachProcess(Panel* panel, Action_ForeachProcessFn fn, int arg, bool* wasAnyTagged) { bool Action_foreachProcess(Panel* panel, Action_ForeachProcessFn fn, int arg, bool* wasAnyTagged) {
bool ok = true; bool ok = true;
bool anyTagged = false; bool anyTagged = false;
@ -57,3 +66,35 @@ bool Action_foreachProcess(Panel* panel, Action_ForeachProcessFn fn, int arg, bo
return ok; return ok;
} }
Object* Action_pickFromVector(Panel* panel, Panel* list, int x, const char** keyLabels, Header* header) {
int y = panel->y;
const char* fuKeys[] = {"Enter", "Esc", NULL};
int fuEvents[] = {13, 27};
ScreenManager* scr = ScreenManager_new(0, y, 0, -1, HORIZONTAL, header, false);
scr->allowFocusChange = false;
ScreenManager_add(scr, list, FunctionBar_new(keyLabels, fuKeys, fuEvents), x - 1);
ScreenManager_add(scr, panel, NULL, -1);
Panel* panelFocus;
int ch;
bool unfollow = false;
int pid = Action_selectedPid(panel);
if (header->pl->following == -1) {
header->pl->following = pid;
unfollow = true;
}
ScreenManager_run(scr, &panelFocus, &ch);
if (unfollow) {
header->pl->following = -1;
}
ScreenManager_delete(scr);
Panel_move(panel, 0, y);
Panel_resize(panel, COLS, LINES-y-1);
if (panelFocus == list && ch == 13) {
Process* selected = (Process*)Panel_getSelected(panel);
if (selected && selected->pid == pid)
return Panel_getSelected(list);
else
beep();
}
return NULL;
}

View File

@ -36,7 +36,10 @@ typedef struct State_ {
typedef bool(*Action_ForeachProcessFn)(Process*, size_t); typedef bool(*Action_ForeachProcessFn)(Process*, size_t);
int Action_selectedPid(Panel* panel);
bool Action_foreachProcess(Panel* panel, Action_ForeachProcessFn fn, int arg, bool* wasAnyTagged); bool Action_foreachProcess(Panel* panel, Action_ForeachProcessFn fn, int arg, bool* wasAnyTagged);
Object* Action_pickFromVector(Panel* panel, Panel* list, int x, const char** keyLabels, Header* header);
#endif #endif

View File

@ -35,10 +35,10 @@ HostnameMeter.h OpenFilesScreen.h Affinity.h IncSet.h Action.h
if HTOP_LINUX if HTOP_LINUX
myhtopplatsources = linux/Platform.c linux/IOPriorityPanel.c linux/IOPriority.c \ myhtopplatsources = linux/Platform.c linux/IOPriorityPanel.c linux/IOPriority.c \
linux/LinuxProcessList.c linux/LinuxCRT.c linux/LinuxProcess.c linux/LinuxProcessList.c linux/LinuxCRT.c
myhtopplatheaders = linux/Platform.h linux/IOPriorityPanel.h linux/IOPriority.h \ myhtopplatheaders = linux/Platform.h linux/IOPriorityPanel.h linux/IOPriority.h \
linux/LinuxProcessList.h linux/LinuxCRT.h linux/LinuxProcess.h linux/LinuxProcessList.h linux/LinuxCRT.h
endif endif
if HTOP_UNSUPPORTED if HTOP_UNSUPPORTED

View File

@ -26,7 +26,6 @@ in the source distribution for its full text.
#include <sched.h> #include <sched.h>
#include <time.h> #include <time.h>
#include <assert.h> #include <assert.h>
#include <sys/syscall.h>
#ifdef HAVE_LIBHWLOC #ifdef HAVE_LIBHWLOC
#include <hwloc/linux.h> #include <hwloc/linux.h>

View File

@ -10,23 +10,8 @@ in the source distribution for its full text.
#include "CRT.h" #include "CRT.h"
#include "String.h" #include "String.h"
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <stdbool.h>
#include <stdarg.h>
#include <math.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
/*{ /*{
#include "Vector.h" #include "Vector.h"

51
htop.c
View File

@ -195,47 +195,6 @@ static bool changePriority(Panel* panel, int delta) {
return anyTagged; return anyTagged;
} }
static int selectedPid(Panel* panel) {
Process* p = (Process*) Panel_getSelected(panel);
if (p) {
return p->pid;
}
return -1;
}
static Object* pickFromVector(Panel* panel, Panel* list, int x, const char** keyLabels, Header* header) {
int y = panel->y;
const char* fuKeys[] = {"Enter", "Esc", NULL};
int fuEvents[] = {13, 27};
ScreenManager* scr = ScreenManager_new(0, y, 0, -1, HORIZONTAL, header, false);
scr->allowFocusChange = false;
ScreenManager_add(scr, list, FunctionBar_new(keyLabels, fuKeys, fuEvents), x - 1);
ScreenManager_add(scr, panel, NULL, -1);
Panel* panelFocus;
int ch;
bool unfollow = false;
int pid = selectedPid(panel);
if (header->pl->following == -1) {
header->pl->following = pid;
unfollow = true;
}
ScreenManager_run(scr, &panelFocus, &ch);
if (unfollow) {
header->pl->following = -1;
}
ScreenManager_delete(scr);
Panel_move(panel, 0, y);
Panel_resize(panel, COLS, LINES-y-1);
if (panelFocus == list && ch == 13) {
Process* selected = (Process*)Panel_getSelected(panel);
if (selected && selected->pid == pid)
return Panel_getSelected(list);
else
beep();
}
return NULL;
}
static void addUserToVector(int key, void* userCast, void* panelCast) { static void addUserToVector(int key, void* userCast, void* panelCast) {
char* user = (char*) userCast; char* user = (char*) userCast;
Panel* panel = (Panel*) panelCast; Panel* panel = (Panel*) panelCast;
@ -297,7 +256,7 @@ static Htop_Reaction sortBy(Panel* panel, ProcessList* pl, Header* header) {
Panel_setSelected(sortPanel, i); Panel_setSelected(sortPanel, i);
free(name); free(name);
} }
ListItem* field = (ListItem*) pickFromVector(panel, sortPanel, 15, fuFunctions, header); ListItem* field = (ListItem*) Action_pickFromVector(panel, sortPanel, 15, fuFunctions, header);
if (field) { if (field) {
reaction |= setSortKey(pl, field->key); reaction |= setSortKey(pl, field->key);
} }
@ -415,7 +374,7 @@ static Htop_Reaction actionSetAffinity(Panel* panel, ProcessList* pl, Header* he
Affinity_delete(affinity); Affinity_delete(affinity);
const char* fuFunctions[] = {"Set ", "Cancel ", NULL}; const char* fuFunctions[] = {"Set ", "Cancel ", NULL};
void* set = pickFromVector(panel, affinityPanel, 15, fuFunctions, header); void* set = Action_pickFromVector(panel, affinityPanel, 15, fuFunctions, header);
if (set) { if (set) {
Affinity* affinity = AffinityPanel_getAffinity(affinityPanel); Affinity* affinity = AffinityPanel_getAffinity(affinityPanel);
bool ok = Action_foreachProcess(panel, (Action_ForeachProcessFn) Process_setAffinity, (size_t) affinity, NULL); bool ok = Action_foreachProcess(panel, (Action_ForeachProcessFn) Process_setAffinity, (size_t) affinity, NULL);
@ -434,7 +393,7 @@ static Htop_Reaction actionKill(Panel* panel, ProcessList* pl, Header* header) {
(void) pl; (void) pl;
Panel* signalsPanel = (Panel*) SignalsPanel_new(); Panel* signalsPanel = (Panel*) SignalsPanel_new();
const char* fuFunctions[] = {"Send ", "Cancel ", NULL}; const char* fuFunctions[] = {"Send ", "Cancel ", NULL};
ListItem* sgn = (ListItem*) pickFromVector(panel, signalsPanel, 15, fuFunctions, header); ListItem* sgn = (ListItem*) Action_pickFromVector(panel, signalsPanel, 15, fuFunctions, header);
if (sgn) { if (sgn) {
if (sgn->key != 0) { if (sgn->key != 0) {
Panel_setHeader(panel, "Sending..."); Panel_setHeader(panel, "Sending...");
@ -456,7 +415,7 @@ static Htop_Reaction actionFilterByUser(Panel* panel, ProcessList* pl, Header* h
ListItem* allUsers = ListItem_new("All users", -1); ListItem* allUsers = ListItem_new("All users", -1);
Panel_insert(usersPanel, 0, (Object*) allUsers); Panel_insert(usersPanel, 0, (Object*) allUsers);
const char* fuFunctions[] = {"Show ", "Cancel ", NULL}; const char* fuFunctions[] = {"Show ", "Cancel ", NULL};
ListItem* picked = (ListItem*) pickFromVector(panel, usersPanel, 20, fuFunctions, header); ListItem* picked = (ListItem*) Action_pickFromVector(panel, usersPanel, 20, fuFunctions, header);
if (picked) { if (picked) {
if (picked == allUsers) { if (picked == allUsers) {
pl->userOnly = false; pl->userOnly = false;
@ -790,7 +749,7 @@ int main(int argc, char** argv) {
double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000); double newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
bool timeToRecalculate = (newTime - oldTime > settings->delay); bool timeToRecalculate = (newTime - oldTime > settings->delay);
if (newTime < oldTime) timeToRecalculate = true; // clock was adjusted? if (newTime < oldTime) timeToRecalculate = true; // clock was adjusted?
int following = follow ? selectedPid(panel) : -1; int following = follow ? Action_selectedPid(panel) : -1;
if (timeToRecalculate) { if (timeToRecalculate) {
Header_draw(header); Header_draw(header);
oldTime = newTime; oldTime = newTime;

View File

@ -6,9 +6,17 @@ in the source distribution for its full text.
*/ */
#include "Process.h" #include "Process.h"
#include "ProcessList.h"
#include "LinuxProcess.h"
#include "CRT.h"
#include <unistd.h>
#include <sys/syscall.h>
/*{ /*{
#include "IOPriority.h"
typedef struct LinuxProcess_ { typedef struct LinuxProcess_ {
Process super; Process super;
IOPriority ioPriority; IOPriority ioPriority;
@ -24,26 +32,29 @@ effort class. The priority within the best effort class will be
dynamically derived from the cpu nice level of the process: dynamically derived from the cpu nice level of the process:
io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page io_priority = (cpu_nice + 20) / 5. -- From ionice(1) man page
*/ */
#define LinuxProcess_effectiveIOPriority(p_) (IOPriority_class(p_->ioPriority) == IOPRIO_CLASS_NONE ? IOPriority_tuple(IOPRIO_CLASS_BE, (p_->nice + 20) / 5) : p_->ioPriority) #define LinuxProcess_effectiveIOPriority(p_) (IOPriority_class(p_->ioPriority) == IOPRIO_CLASS_NONE ? IOPriority_tuple(IOPRIO_CLASS_BE, (p_->super.nice + 20) / 5) : p_->ioPriority)
IOPriority LinuxProcess_updateIOPriority(Process* this) { IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) {
IOPriority ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, this->pid); IOPriority ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, this->super.pid);
this->ioPriority = ioprio; this->ioPriority = ioprio;
return ioprio; return ioprio;
} }
bool LinuxProcess_setIOPriority(Process* this, IOPriority ioprio) { bool LinuxProcess_setIOPriority(LinuxProcess* this, IOPriority ioprio) {
syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->pid, ioprio); syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->super.pid, ioprio);
return (Process_updateIOPriority(this) == ioprio); return (LinuxProcess_updateIOPriority(this) == ioprio);
} }
void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field) { void LinuxProcess_writeField(LinuxProcess* this, RichString* str, ProcessField field) {
char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR];
int n = sizeof(buffer) - 1;
switch (field) { switch (field) {
case IO_PRIORITY: { case IO_PRIORITY: {
int klass = IOPriority_class(this->ioPriority); int klass = IOPriority_class(this->ioPriority);
if (klass == IOPRIO_CLASS_NONE) { if (klass == IOPRIO_CLASS_NONE) {
// see note [1] above // see note [1] above
snprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5); snprintf(buffer, n, "B%1d ", (int) (this->super.nice + 20) / 5);
} else if (klass == IOPRIO_CLASS_BE) { } else if (klass == IOPRIO_CLASS_BE) {
snprintf(buffer, n, "B%1d ", IOPriority_data(this->ioPriority)); snprintf(buffer, n, "B%1d ", IOPriority_data(this->ioPriority));
} else if (klass == IOPRIO_CLASS_RT) { } else if (klass == IOPRIO_CLASS_RT) {
@ -62,7 +73,6 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field)
} }
RichString_append(str, attr, buffer); RichString_append(str, attr, buffer);
} }
}
long LinuxProcess_compare(const void* v1, const void* v2) { long LinuxProcess_compare(const void* v1, const void* v2) {
LinuxProcess *p1, *p2; LinuxProcess *p1, *p2;
@ -76,10 +86,8 @@ long LinuxProcess_compare(const void* v1, const void* v2) {
} }
switch (pl->sortKey) { switch (pl->sortKey) {
case IO_PRIORITY: case IO_PRIORITY:
return Process_effectiveIOPriority(p1) - Process_effectiveIOPriority(p2); return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2);
default: default:
return (p1->pid - p2->pid); return (p1->super.pid - p2->super.pid);
} }
test_diff:
return (diff > 0) ? 1 : (diff < 0 ? -1 : 0);
} }

View File

@ -5,10 +5,32 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text. in the source distribution for its full text.
*/ */
#include "ProcessList.h" #include "LinuxProcessList.h"
#include "LinuxProcess.h"
#include "CRT.h"
#include "String.h"
#include <errno.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <stdbool.h>
#include <stdarg.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <fcntl.h>
/*{ /*{
#include "ProcessList.h"
#ifndef PROCDIR #ifndef PROCDIR
#define PROCDIR "/proc" #define PROCDIR "/proc"
#endif #endif
@ -463,7 +485,7 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna
if (! LinuxProcessList_readStatFile(process, dirname, name, command)) if (! LinuxProcessList_readStatFile(process, dirname, name, command))
goto errorReadingProcess; goto errorReadingProcess;
if (this->flags & PROCESS_FLAG_IOPRIO) if (this->flags & PROCESS_FLAG_IOPRIO)
Process_updateIOPriority(process); LinuxProcess_updateIOPriority((LinuxProcess*)process);
float percent_cpu = (process->utime + process->stime - lasttimes) / period * 100.0; float percent_cpu = (process->utime + process->stime - lasttimes) / period * 100.0;
process->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0); process->percent_cpu = MAX(MIN(percent_cpu, cpus*100.0), 0.0);
if (isnan(process->percent_cpu)) process->percent_cpu = 0.0; if (isnan(process->percent_cpu)) process->percent_cpu = 0.0;

View File

@ -5,20 +5,26 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text. in the source distribution for its full text.
*/ */
#include "Platform.h"
#include "IOPriority.h" #include "IOPriority.h"
#include "IOPriorityPanel.h" #include "IOPriorityPanel.h"
#include "LinuxProcess.h"
/*{
#include "Action.h"
}*/
static Htop_Reaction Platform_actionSetIOPriority(Panel* panel, ProcessList* pl, Header* header) { static Htop_Reaction Platform_actionSetIOPriority(Panel* panel, ProcessList* pl, Header* header) {
(void) panel, (void) pl; (void) panel, (void) pl;
Process* p = (Process*) Panel_getSelected(panel); LinuxProcess* p = (LinuxProcess*) Panel_getSelected(panel);
if (!p) return HTOP_OK; if (!p) return HTOP_OK;
IOPriority ioprio = p->ioPriority; IOPriority ioprio = p->ioPriority;
Panel* ioprioPanel = IOPriorityPanel_new(ioprio); Panel* ioprioPanel = IOPriorityPanel_new(ioprio);
const char* fuFunctions[] = {"Set ", "Cancel ", NULL}; const char* fuFunctions[] = {"Set ", "Cancel ", NULL};
void* set = pickFromVector(panel, ioprioPanel, 21, fuFunctions, header); void* set = Action_pickFromVector(panel, ioprioPanel, 21, fuFunctions, header);
if (set) { if (set) {
IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel); IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel);
bool ok = Action_foreachProcess(panel, (Action_ForeachProcessFn) Process_setIOPriority, (size_t) ioprio, NULL); bool ok = Action_foreachProcess(panel, (Action_ForeachProcessFn) LinuxProcess_setIOPriority, (size_t) ioprio, NULL);
if (!ok) if (!ok)
beep(); beep();
} }