Skip to content

Commit

Permalink
linux: support for monitoring syscall
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <sanpeqf@gmail.com>
  • Loading branch information
sanpeqf committed Jun 1, 2024
1 parent 58efa4e commit 078a3e7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions linux/LinuxProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
#endif
[GPU_TIME] = { .name = "GPU_TIME", .title = "GPU_TIME ", .description = "Total GPU time", .flags = PROCESS_FLAG_LINUX_GPU, .defaultSortDesc = true, },
[GPU_PERCENT] = { .name = "GPU_PERCENT", .title = " GPU% ", .description = "Percentage of the GPU time the process used in the last sampling", .flags = PROCESS_FLAG_LINUX_GPU, .defaultSortDesc = true, },
[SYSCALL] = { .name = "SYSCALL", .title = "SYSCALL", .description = "Current syscall of the process", .flags = PROCESS_FLAG_LINUX_SYSCALL, .autoWidth = true, },
};

Process* LinuxProcess_new(const Machine* host) {
Expand All @@ -131,6 +132,7 @@ void Process_delete(Object* cast) {
free(this->ctid);
#endif
free(this->secattr);
free(this->syscall);
free(this);
}

Expand Down Expand Up @@ -362,6 +364,18 @@ static void LinuxProcess_rowWriteField(const Row* super, RichString* str, Proces
xSnprintf(buffer, n, "N/A ");
}
break;
case SYSCALL: {
const char* syscall;
if (lp->syscall) {
syscall = lp->syscall;
} else {
attr = CRT_colors[PROCESS_SHADOW];
syscall = "N/A";
}
xSnprintf(buffer, n, "%-*.*s ", Row_fieldWidths[SYSCALL], Row_fieldWidths[SYSCALL], syscall);
RichString_appendWide(str, attr, buffer);
return;
}
default:
Process_writeField(this, str, field);
return;
Expand Down Expand Up @@ -466,6 +480,8 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce
return SPACESHIP_NUMBER(p1->gpu_time, p2->gpu_time);
case ISCONTAINER:
return SPACESHIP_NUMBER(v1->isRunningInContainer, v2->isRunningInContainer);
case SYSCALL:
return SPACESHIP_NULLSTR(p1->syscall, p2->syscall);
default:
return Process_compareByKey_Base(v1, v2, key);
}
Expand Down
3 changes: 3 additions & 0 deletions linux/LinuxProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ in the source distribution for its full text.
#define PROCESS_FLAG_LINUX_AUTOGROUP 0x00080000
#define PROCESS_FLAG_LINUX_GPU 0x00100000
#define PROCESS_FLAG_LINUX_CONTAINER 0x00200000
#define PROCESS_FLAG_LINUX_SYSCALL 0x00400000

typedef struct LinuxProcess_ {
Process super;
Expand Down Expand Up @@ -118,6 +119,8 @@ typedef struct LinuxProcess_ {
/* Autogroup scheduling (CFS) information */
long int autogroup_id;
int autogroup_nice;

char* syscall;
} LinuxProcess;

extern int pageSize;
Expand Down
27 changes: 27 additions & 0 deletions linux/LinuxProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,29 @@ static void LinuxProcessTable_readCwd(LinuxProcess* process, openat_arg_t procFd
free_and_xStrdup(&process->super.procCwd, pathBuffer);
}

/*
* Read /proc/<pid>/syscall (thread-specific data)
*/
static void LinuxProcessTable_readSyscall(LinuxProcess* process, openat_arg_t procFd) {
char buffer[1024];

ssize_t r = xReadfileat(procFd, "syscall", buffer, sizeof(buffer));
if (r <= 0) {
free(process->syscall);
process->syscall = NULL;
return;
}

size_t offset = strcspn(buffer, " \n");
if (buffer[offset]) {
buffer[offset] = '\0';
}

Row_updateFieldWidth(SYSCALL, strlen(buffer));

free_and_xStrdup(&process->syscall, buffer);
}

/*
* Read /proc/<pid>/exe (process-shared data)
*/
Expand Down Expand Up @@ -1694,6 +1717,10 @@ static bool LinuxProcessTable_recurseProcTree(LinuxProcessTable* this, openat_ar
LinuxProcessTable_readCwd(lp, procFd, mainTask);
}

if (ss->flags & PROCESS_FLAG_LINUX_SYSCALL) {
LinuxProcessTable_readSyscall(lp, procFd);
}

if ((ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) && this->haveAutogroup) {
LinuxProcessTable_readAutogroup(lp, procFd, mainTask);
}
Expand Down
1 change: 1 addition & 0 deletions linux/ProcessField.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ in the source distribution for its full text.
GPU_TIME = 132, \
GPU_PERCENT = 133, \
ISCONTAINER = 134, \
SYSCALL = 135, \
// End of list


Expand Down

0 comments on commit 078a3e7

Please sign in to comment.