Skip to content

Commit

Permalink
Sorry about the mega-patch.
Browse files Browse the repository at this point in the history
This is a work-in-progress, code is currently broken.
(Some actions, and notably, the header, are missing.)
  • Loading branch information
hishamhm committed Jan 22, 2015
1 parent 36b7832 commit 3383d8e
Show file tree
Hide file tree
Showing 51 changed files with 1,982 additions and 1,748 deletions.
490 changes: 486 additions & 4 deletions Action.c

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ in the source distribution for its full text.

#include "IncSet.h"
#include "Settings.h"
#include "Header.h"
#include "UsersTable.h"
#include "ProcessList.h"
#include "Panel.h"

typedef enum {
HTOP_OK = 0x00,
Expand All @@ -31,6 +34,9 @@ typedef struct State_ {
IncSet* inc;
Settings* settings;
UsersTable* ut;
ProcessList* pl;
Panel* panel;
Header* header;
} State;

typedef bool(*Action_ForeachProcessFn)(Process*, size_t);
Expand All @@ -40,6 +46,15 @@ int Action_selectedPid(Panel* panel);

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);
Object* Action_pickFromVector(State* st, Panel* list, int x, const char** keyLabels);

// ----------------------------------------

bool Action_setUserOnly(const char* userName, uid_t* userId);

// ----------------------------------------

void Action_setBindings(Htop_Action* keys);


#endif
71 changes: 70 additions & 1 deletion Affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ in the source distribution for its full text.

#include <stdlib.h>

#ifdef HAVE_LIBHWLOC
#include <hwloc/linux.h>
#elif HAVE_NATIVE_AFFINITY
#include <sched.h>
#endif

/*{
#include "Process.h"
#include "ProcessList.h"
typedef struct Affinity_ {
ProcessList* pl;
int size;
int used;
int* cpus;
} Affinity;
}*/

Affinity* Affinity_new() {
Affinity* Affinity_new(ProcessList* pl) {
Affinity* this = calloc(1, sizeof(Affinity));
this->size = 8;
this->cpus = calloc(this->size, sizeof(int));
this->pl = pl;
return this;
}

Expand All @@ -40,3 +50,62 @@ void Affinity_add(Affinity* this, int id) {
this->used++;
}


#ifdef HAVE_LIBHWLOC

Affinity* Affinity_get(Process* proc, ProcessList* pl) {
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
bool ok = (hwloc_linux_get_tid_cpubind(pl->topology, proc->pid, cpuset) == 0);
Affinity* affinity = NULL;
if (ok) {
affinity = Affinity_new(pl);
if (hwloc_bitmap_last(cpuset) == -1) {
for (int i = 0; i < pl->cpuCount; i++) {
Affinity_add(affinity, i);
}
} else {
unsigned int id;
hwloc_bitmap_foreach_begin(id, cpuset);
Affinity_add(affinity, id);
hwloc_bitmap_foreach_end();
}
}
hwloc_bitmap_free(cpuset);
return affinity;
}

bool Affinity_set(Process* proc, Affinity* this) {
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
for (int i = 0; i < affinity->used; i++) {
hwloc_bitmap_set(cpuset, affinity->cpus[i]);
}
bool ok = (hwloc_linux_set_tid_cpubind(this->pl->topology, proc->pid, cpuset) == 0);
hwloc_bitmap_free(cpuset);
return ok;
}

#elif HAVE_NATIVE_AFFINITY

Affinity* Affinity_get(Process* proc, ProcessList* pl) {
cpu_set_t cpuset;
bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0);
if (!ok) return NULL;
Affinity* affinity = Affinity_new(pl);
for (int i = 0; i < pl->cpuCount; i++) {
if (CPU_ISSET(i, &cpuset))
Affinity_add(affinity, i);
}
return affinity;
}

bool Affinity_set(Process* proc, Affinity* this) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (int i = 0; i < this->used; i++) {
CPU_SET(this->cpus[i], &cpuset);
}
bool ok = (sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset) == 0);
return ok;
}

#endif
22 changes: 21 additions & 1 deletion Affinity.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,39 @@ Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#ifdef HAVE_LIBHWLOC
#elif HAVE_NATIVE_AFFINITY
#endif

#include "Process.h"
#include "ProcessList.h"

typedef struct Affinity_ {
ProcessList* pl;
int size;
int used;
int* cpus;
} Affinity;


Affinity* Affinity_new();
Affinity* Affinity_new(ProcessList* pl);

void Affinity_delete(Affinity* this);

void Affinity_add(Affinity* this, int id);

#ifdef HAVE_LIBHWLOC

Affinity* Affinity_get(Process* proc, ProcessList* pl);

bool Affinity_set(Process* proc, Affinity* this);

#elif HAVE_NATIVE_AFFINITY

Affinity* Affinity_get(Process* proc, ProcessList* pl);

bool Affinity_set(Process* proc, Affinity* this);

#endif

#endif
6 changes: 3 additions & 3 deletions AffinityPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) {
int curCpu = 0;
for (int i = 0; i < pl->cpuCount; i++) {
char number[10];
snprintf(number, 9, "%d", ProcessList_cpuId(pl, i));
snprintf(number, 9, "%d", Settings_cpuId(pl->settings, i));
bool mode;
if (curCpu < affinity->used && affinity->cpus[curCpu] == i) {
mode = true;
Expand All @@ -63,8 +63,8 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) {
return this;
}

Affinity* AffinityPanel_getAffinity(Panel* this) {
Affinity* affinity = Affinity_new();
Affinity* AffinityPanel_getAffinity(Panel* this, ProcessList* pl) {
Affinity* affinity = Affinity_new(pl);
int size = Panel_size(this);
for (int i = 0; i < size; i++) {
if (CheckItem_get((CheckItem*)Panel_get(this, i)))
Expand Down
2 changes: 1 addition & 1 deletion AffinityPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ extern PanelClass AffinityPanel_class;

Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity);

Affinity* AffinityPanel_getAffinity(Panel* this);
Affinity* AffinityPanel_getAffinity(Panel* this, ProcessList* pl);

#endif
22 changes: 9 additions & 13 deletions AvailableColumnsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ in the source distribution for its full text.
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

/*{
#include "Panel.h"
#include "Settings.h"
#include "ScreenManager.h"
typedef struct AvailableColumnsPanel_ {
Panel super;
Panel* columns;
Settings* settings;
ScreenManager* scr;
} AvailableColumnsPanel;
}*/
Expand All @@ -38,7 +34,7 @@ static void AvailableColumnsPanel_delete(Object* object) {

static HandlerResult AvailableColumnsPanel_eventHandler(Panel* super, int ch) {
AvailableColumnsPanel* this = (AvailableColumnsPanel*) super;
char* text = ((ListItem*) Panel_getSelected(super))->value;
int key = ((ListItem*) Panel_getSelected(super))->key;
HandlerResult result = IGNORED;

switch(ch) {
Expand All @@ -47,7 +43,7 @@ static HandlerResult AvailableColumnsPanel_eventHandler(Panel* super, int ch) {
case KEY_F(5):
{
int at = Panel_getSelectedIndex(this->columns);
Panel_insert(this->columns, at, (Object*) ListItem_new(text, 0));
Panel_insert(this->columns, at, (Object*) ListItem_new(Process_fields[key].name, key));
Panel_setSelected(this->columns, at+1);
ColumnsPanel_update(this->columns);
result = HANDLED;
Expand All @@ -71,19 +67,19 @@ PanelClass AvailableColumnsPanel_class = {
.eventHandler = AvailableColumnsPanel_eventHandler
};

AvailableColumnsPanel* AvailableColumnsPanel_new(Settings* settings, Panel* columns, ScreenManager* scr) {
AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns) {
AvailableColumnsPanel* this = AllocThis(AvailableColumnsPanel);
Panel* super = (Panel*) this;
Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);

this->settings = settings;
this->scr = scr;

Panel_setHeader(super, "Available Columns");

for (int i = 1; i < LAST_PROCESSFIELD; i++) {
if (i != COMM)
Panel_add(super, (Object*) ListItem_new(Process_fieldNames[i], 0));
if (i != COMM && Process_fields[i].description) {
char description[256];
snprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description);
Panel_add(super, (Object*) ListItem_new(description, i));
}
}
this->columns = columns;
return this;
Expand Down
7 changes: 1 addition & 6 deletions AvailableColumnsPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ in the source distribution for its full text.
*/

#include "Panel.h"
#include "Settings.h"
#include "ScreenManager.h"

typedef struct AvailableColumnsPanel_ {
Panel super;
Panel* columns;

Settings* settings;
ScreenManager* scr;
} AvailableColumnsPanel;


extern PanelClass AvailableColumnsPanel_class;

AvailableColumnsPanel* AvailableColumnsPanel_new(Settings* settings, Panel* columns, ScreenManager* scr);
AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns);

#endif
19 changes: 11 additions & 8 deletions AvailableMetersPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ in the source distribution for its full text.
#include "Settings.h"
#include "Panel.h"
#include "ScreenManager.h"
#include "ProcessList.h"
typedef struct AvailableMetersPanel_ {
Panel super;
ScreenManager* scr;
Settings* settings;
Header* header;
Panel* leftPanel;
Panel* rightPanel;
ScreenManager* scr;
} AvailableMetersPanel;
}*/
Expand All @@ -38,14 +40,14 @@ static void AvailableMetersPanel_delete(Object* object) {
free(this);
}

static inline void AvailableMetersPanel_addHeader(Header* header, Panel* panel, MeterClass* type, int param, HeaderSide side) {
Meter* meter = (Meter*) Header_addMeter(header, type, param, side);
static inline void AvailableMetersPanel_addMeter(Header* header, Panel* panel, MeterClass* type, int param, int column) {
Meter* meter = (Meter*) Header_addMeterByClass(header, type, param, column);
Panel_add(panel, (Object*) Meter_toListItem(meter));
}

static HandlerResult AvailableMetersPanel_eventHandler(Panel* super, int ch) {
AvailableMetersPanel* this = (AvailableMetersPanel*) super;
Header* header = this->settings->header;
Header* header = this->header;

ListItem* selected = (ListItem*) Panel_getSelected(super);
int param = selected->key & 0xff;
Expand All @@ -57,15 +59,15 @@ static HandlerResult AvailableMetersPanel_eventHandler(Panel* super, int ch) {
case 'l':
case 'L':
{
AvailableMetersPanel_addHeader(header, this->leftPanel, Platform_meterTypes[type], param, LEFT_HEADER);
AvailableMetersPanel_addMeter(header, this->leftPanel, Platform_meterTypes[type], param, 0);
result = HANDLED;
break;
}
case KEY_F(6):
case 'r':
case 'R':
{
AvailableMetersPanel_addHeader(header, this->rightPanel, Platform_meterTypes[type], param, RIGHT_HEADER);
AvailableMetersPanel_addMeter(header, this->rightPanel, Platform_meterTypes[type], param, 1);
result = HANDLED;
break;
}
Expand All @@ -87,12 +89,13 @@ PanelClass AvailableMetersPanel_class = {
.eventHandler = AvailableMetersPanel_eventHandler
};

AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr) {
AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Header* header, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr, ProcessList* pl) {
AvailableMetersPanel* this = AllocThis(AvailableMetersPanel);
Panel* super = (Panel*) this;
Panel_init(super, 1, 1, 1, 1, Class(ListItem), true);

this->settings = settings;
this->header = header;
this->leftPanel = leftMeters;
this->rightPanel = rightMeters;
this->scr = scr;
Expand All @@ -105,7 +108,7 @@ AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Panel* leftMe
}
}
MeterClass* type = &CPUMeter_class;
int cpus = settings->pl->cpuCount;
int cpus = pl->cpuCount;
if (cpus > 1) {
Panel_add(super, (Object*) ListItem_new("CPU average", 0));
for (int i = 1; i <= cpus; i++) {
Expand Down
6 changes: 4 additions & 2 deletions AvailableMetersPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ in the source distribution for its full text.
#include "Settings.h"
#include "Panel.h"
#include "ScreenManager.h"
#include "ProcessList.h"

typedef struct AvailableMetersPanel_ {
Panel super;
ScreenManager* scr;

Settings* settings;
Header* header;
Panel* leftPanel;
Panel* rightPanel;
ScreenManager* scr;
} AvailableMetersPanel;


extern PanelClass AvailableMetersPanel_class;

AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr);
AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Header* header, Panel* leftMeters, Panel* rightMeters, ScreenManager* scr, ProcessList* pl);

#endif
Loading

0 comments on commit 3383d8e

Please sign in to comment.