Skip to content

Commit

Permalink
Show warnings on failures of interactive user actions
Browse files Browse the repository at this point in the history
  • Loading branch information
cgzones committed Apr 23, 2023
1 parent 999e75a commit 5577088
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
27 changes: 19 additions & 8 deletions Affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ in the source distribution for its full text.

#include "Affinity.h"

#include <errno.h>
#include <stdlib.h>

#include "ScreenWarning.h"
#include "XUtils.h"

#if defined(HAVE_LIBHWLOC)
Expand Down Expand Up @@ -54,9 +56,11 @@ void Affinity_add(Affinity* this, unsigned int id) {

Affinity* Affinity_get(const Process* proc, ProcessList* pl) {
hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
bool ok = (hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
int r = hwloc_get_proc_cpubind(pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG);
if (r < 0)
ScreenWarning_add("Failed to get affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno));
Affinity* affinity = NULL;
if (ok) {
if (r == 0) {
affinity = Affinity_new(pl);
if (hwloc_bitmap_last(cpuset) == -1) {
for (unsigned int i = 0; i < pl->existingCPUs; i++) {
Expand All @@ -79,18 +83,22 @@ bool Affinity_set(Process* proc, Arg arg) {
for (unsigned int i = 0; i < this->used; i++) {
hwloc_bitmap_set(cpuset, this->cpus[i]);
}
bool ok = (hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG) == 0);
int r = hwloc_set_proc_cpubind(this->pl->topology, proc->pid, cpuset, HTOP_HWLOC_CPUBIND_FLAG);
if (r < 0)
ScreenWarning_add("Failed to set affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno));
hwloc_bitmap_free(cpuset);
return ok;
return r == 0;
}

#elif defined(HAVE_AFFINITY)

Affinity* Affinity_get(const Process* proc, ProcessList* pl) {
cpu_set_t cpuset;
bool ok = (sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset) == 0);
if (!ok)
int r = sched_getaffinity(proc->pid, sizeof(cpu_set_t), &cpuset);
if (r < 0) {
ScreenWarning_add("Failed to get affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno));
return NULL;
}

Affinity* affinity = Affinity_new(pl);
for (unsigned int i = 0; i < pl->existingCPUs; i++) {
Expand All @@ -108,8 +116,11 @@ bool Affinity_set(Process* proc, Arg arg) {
for (unsigned 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;
int r = sched_setaffinity(proc->pid, sizeof(unsigned long), &cpuset);
if (r < 0)
ScreenWarning_add("Failed to set affinity for process %d (%s): %s", proc->pid, proc->procComm, strerror(errno));

return r == 0;
}

#endif
10 changes: 9 additions & 1 deletion Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ in the source distribution for its full text.
#include "Process.h"

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
Expand All @@ -29,6 +30,7 @@ in the source distribution for its full text.
#include "DynamicColumn.h"
#include "RichString.h"
#include "Scheduling.h"
#include "ScreenWarning.h"
#include "Settings.h"
#include "XUtils.h"

Expand Down Expand Up @@ -1134,6 +1136,8 @@ bool Process_setPriority(Process* this, int priority) {

int old_prio = getpriority(PRIO_PROCESS, this->pid);
int err = setpriority(PRIO_PROCESS, this->pid, priority);
if (err < 0)
ScreenWarning_add("Failed to set priority %d for process %d (%s): %s", priority, this->pid, this->procComm, strerror(errno));

if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
this->nice = priority;
Expand All @@ -1146,7 +1150,11 @@ bool Process_changePriorityBy(Process* this, Arg delta) {
}

bool Process_sendSignal(Process* this, Arg sgn) {
return kill(this->pid, sgn.i) == 0;
int r = kill(this->pid, sgn.i);
if (r < 0)
ScreenWarning_add("Failed to send signal %d to process %d (%s): %s", sgn.i, this->pid, this->procComm, strerror(errno));

return r == 0;
}

int Process_compare(const void* v1, const void* v2) {
Expand Down
3 changes: 3 additions & 0 deletions Scheduling.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ in the source distribution for its full text.
#include "Macros.h"
#include "Object.h"
#include "Panel.h"
#include "ScreenWarning.h"
#include "XUtils.h"


Expand Down Expand Up @@ -113,6 +114,8 @@ bool Scheduling_setPolicy(Process* proc, Arg arg) {
#endif

int r = sched_setscheduler(proc->pid, policy, &param);
if (r == -1)
ScreenWarning_add("Failed to set scheduling policy %d for process %d (%s): %s", policy, proc->pid, proc->procComm, strerror(errno));

/* POSIX says on success the previous scheduling policy should be returned,
* but Linux always returns 0. */
Expand Down
8 changes: 7 additions & 1 deletion linux/LinuxProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ in the source distribution for its full text.

#include "linux/LinuxProcess.h"

#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -20,6 +21,7 @@ in the source distribution for its full text.
#include "ProvideCurses.h"
#include "RichString.h"
#include "Scheduling.h"
#include "ScreenWarning.h"
#include "XUtils.h"
#include "linux/IOPriority.h"

Expand Down Expand Up @@ -159,7 +161,9 @@ IOPriority LinuxProcess_updateIOPriority(LinuxProcess* this) {
bool LinuxProcess_setIOPriority(Process* this, Arg ioprio) {
// Other OSes masquerading as Linux (NetBSD?) don't have this syscall
#ifdef SYS_ioprio_set
syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->pid, ioprio.i);
int r = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, this->pid, ioprio.i);
if (r < 0)
ScreenWarning_add("Failed to set IO priority %d for process %d (%s): %s", ioprio.i, this->pid, this->procComm, strerror(errno));
#endif
return (LinuxProcess_updateIOPriority((LinuxProcess*)this) == ioprio.i);
}
Expand Down Expand Up @@ -187,6 +191,8 @@ bool LinuxProcess_changeAutogroupPriorityBy(Process* this, Arg delta) {
rewind(file);
xSnprintf(buffer, sizeof(buffer), "%d", nice + delta.i);
success = fputs(buffer, file) > 0;
if (!success)
ScreenWarning_add("Failed to set autogroup for process %d (%s): %s", this->pid, this->procComm, strerror(errno));
} else {
success = false;
}
Expand Down

0 comments on commit 5577088

Please sign in to comment.