Skip to content

Commit 078a3e7

Browse files
committed
linux: support for monitoring syscall
Signed-off-by: John Sanpe <sanpeqf@gmail.com>
1 parent 58efa4e commit 078a3e7

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

linux/LinuxProcess.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
112112
#endif
113113
[GPU_TIME] = { .name = "GPU_TIME", .title = "GPU_TIME ", .description = "Total GPU time", .flags = PROCESS_FLAG_LINUX_GPU, .defaultSortDesc = true, },
114114
[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, },
115+
[SYSCALL] = { .name = "SYSCALL", .title = "SYSCALL", .description = "Current syscall of the process", .flags = PROCESS_FLAG_LINUX_SYSCALL, .autoWidth = true, },
115116
};
116117

117118
Process* LinuxProcess_new(const Machine* host) {
@@ -131,6 +132,7 @@ void Process_delete(Object* cast) {
131132
free(this->ctid);
132133
#endif
133134
free(this->secattr);
135+
free(this->syscall);
134136
free(this);
135137
}
136138

@@ -362,6 +364,18 @@ static void LinuxProcess_rowWriteField(const Row* super, RichString* str, Proces
362364
xSnprintf(buffer, n, "N/A ");
363365
}
364366
break;
367+
case SYSCALL: {
368+
const char* syscall;
369+
if (lp->syscall) {
370+
syscall = lp->syscall;
371+
} else {
372+
attr = CRT_colors[PROCESS_SHADOW];
373+
syscall = "N/A";
374+
}
375+
xSnprintf(buffer, n, "%-*.*s ", Row_fieldWidths[SYSCALL], Row_fieldWidths[SYSCALL], syscall);
376+
RichString_appendWide(str, attr, buffer);
377+
return;
378+
}
365379
default:
366380
Process_writeField(this, str, field);
367381
return;
@@ -466,6 +480,8 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce
466480
return SPACESHIP_NUMBER(p1->gpu_time, p2->gpu_time);
467481
case ISCONTAINER:
468482
return SPACESHIP_NUMBER(v1->isRunningInContainer, v2->isRunningInContainer);
483+
case SYSCALL:
484+
return SPACESHIP_NULLSTR(p1->syscall, p2->syscall);
469485
default:
470486
return Process_compareByKey_Base(v1, v2, key);
471487
}

linux/LinuxProcess.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ in the source distribution for its full text.
3131
#define PROCESS_FLAG_LINUX_AUTOGROUP 0x00080000
3232
#define PROCESS_FLAG_LINUX_GPU 0x00100000
3333
#define PROCESS_FLAG_LINUX_CONTAINER 0x00200000
34+
#define PROCESS_FLAG_LINUX_SYSCALL 0x00400000
3435

3536
typedef struct LinuxProcess_ {
3637
Process super;
@@ -118,6 +119,8 @@ typedef struct LinuxProcess_ {
118119
/* Autogroup scheduling (CFS) information */
119120
long int autogroup_id;
120121
int autogroup_nice;
122+
123+
char* syscall;
121124
} LinuxProcess;
122125

123126
extern int pageSize;

linux/LinuxProcessTable.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,29 @@ static void LinuxProcessTable_readCwd(LinuxProcess* process, openat_arg_t procFd
10981098
free_and_xStrdup(&process->super.procCwd, pathBuffer);
10991099
}
11001100

1101+
/*
1102+
* Read /proc/<pid>/syscall (thread-specific data)
1103+
*/
1104+
static void LinuxProcessTable_readSyscall(LinuxProcess* process, openat_arg_t procFd) {
1105+
char buffer[1024];
1106+
1107+
ssize_t r = xReadfileat(procFd, "syscall", buffer, sizeof(buffer));
1108+
if (r <= 0) {
1109+
free(process->syscall);
1110+
process->syscall = NULL;
1111+
return;
1112+
}
1113+
1114+
size_t offset = strcspn(buffer, " \n");
1115+
if (buffer[offset]) {
1116+
buffer[offset] = '\0';
1117+
}
1118+
1119+
Row_updateFieldWidth(SYSCALL, strlen(buffer));
1120+
1121+
free_and_xStrdup(&process->syscall, buffer);
1122+
}
1123+
11011124
/*
11021125
* Read /proc/<pid>/exe (process-shared data)
11031126
*/
@@ -1694,6 +1717,10 @@ static bool LinuxProcessTable_recurseProcTree(LinuxProcessTable* this, openat_ar
16941717
LinuxProcessTable_readCwd(lp, procFd, mainTask);
16951718
}
16961719

1720+
if (ss->flags & PROCESS_FLAG_LINUX_SYSCALL) {
1721+
LinuxProcessTable_readSyscall(lp, procFd);
1722+
}
1723+
16971724
if ((ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) && this->haveAutogroup) {
16981725
LinuxProcessTable_readAutogroup(lp, procFd, mainTask);
16991726
}

linux/ProcessField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ in the source distribution for its full text.
5151
GPU_TIME = 132, \
5252
GPU_PERCENT = 133, \
5353
ISCONTAINER = 134, \
54+
SYSCALL = 135, \
5455
// End of list
5556

5657

0 commit comments

Comments
 (0)