-
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linux: add GPU meter and process column
Based on the DRM client usage stats[1] add statistics for GPU time spend and percentage utilization. [1]: https://www.kernel.org/doc/html/latest/gpu/drm-usage-stats.html
- Loading branch information
Showing
14 changed files
with
593 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
/* | ||
htop - GPU.c | ||
(C) 2023 htop dev team | ||
Released under the GNU GPLv2+, see the COPYING file | ||
in the source distribution for its full text. | ||
*/ | ||
|
||
#include "linux/GPU.h" | ||
|
||
#include <assert.h> | ||
#include <ctype.h> | ||
#include <dirent.h> | ||
#include <errno.h> | ||
#include <sys/types.h> | ||
|
||
#include "XUtils.h" | ||
|
||
#include "linux/LinuxMachine.h" | ||
|
||
|
||
#define INVALID_CLIENT_ID ((unsigned long long int)-1) | ||
|
||
|
||
struct client_id { | ||
char* pdev; | ||
unsigned long long int id; | ||
struct client_id* next; | ||
}; | ||
|
||
enum section_state { | ||
SECST_UNKNOWN, | ||
SECST_DUPLICATE, | ||
SECST_NEW, | ||
}; | ||
|
||
static bool null_str_eq(const char* s1, const char* s2) { | ||
if (s1 == NULL && s2 == NULL) | ||
return true; | ||
|
||
if (s1 && s2) | ||
return String_eq(s1, s2); | ||
|
||
return false; | ||
} | ||
|
||
static bool is_duplicate_client(const struct client_id* parsed, unsigned long long int id, const char* pdev) { | ||
for (; parsed; parsed = parsed->next) { | ||
if (id == parsed->id && null_str_eq(pdev, parsed->pdev)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
static void update_system(LinuxProcessList* lpl, unsigned long long int time, char* engine) { | ||
Machine* host = lpl->super.host; | ||
LinuxMachine* lhost = (LinuxMachine*) host; | ||
GPUEngineData** engineData = &lhost->gpuEngineData; | ||
|
||
while (*engineData) { | ||
if (String_eq((*engineData)->key, engine)) | ||
break; | ||
|
||
engineData = &((*engineData)->next); | ||
} | ||
|
||
if (!*engineData) { | ||
GPUEngineData* newData; | ||
|
||
newData = xMalloc(sizeof(*newData)); | ||
*newData = (GPUEngineData) { | ||
.prevTime = 0, | ||
.curTime = 0, | ||
.key = engine, | ||
.next = NULL, | ||
}; | ||
|
||
*engineData = newData; | ||
} else | ||
free(engine); | ||
|
||
(*engineData)->curTime += time; | ||
lhost->curGpuTime += time; | ||
} | ||
|
||
/* | ||
* Documentation reference: | ||
* https://www.kernel.org/doc/html/latest/gpu/drm-usage-stats.html | ||
*/ | ||
void GPU_readProcessData(LinuxProcessList* lpl, LinuxProcess* lp, openat_arg_t procFd) { | ||
int fdinfoFd = -1; | ||
DIR* fdinfoDir = NULL; | ||
struct client_id* parsed_ids = NULL; | ||
unsigned long long int new_gpu_time = 0; | ||
|
||
fdinfoFd = Compat_openat(procFd, "fdinfo", O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC); | ||
if (fdinfoFd == -1) | ||
goto out; | ||
|
||
fdinfoDir = fdopendir(fdinfoFd); | ||
if (!fdinfoDir) | ||
goto out; | ||
fdinfoFd = -1; | ||
|
||
#ifndef HAVE_OPENAT | ||
char fdinfoPathBuf[32]; | ||
xSnprintf(fdinfoPathBuf, sizeof(fdinfoPathBuf), "/proc/%u/fdinfo", lp->super.pid); | ||
#endif | ||
|
||
while (true) { | ||
const struct dirent* entry; | ||
const char* ename; | ||
FILE* fp; | ||
char* pdev = NULL; | ||
unsigned long long int client_id = INVALID_CLIENT_ID; | ||
enum section_state sstate = SECST_UNKNOWN; | ||
char buf[256]; | ||
|
||
entry = readdir(fdinfoDir); | ||
if (!entry) | ||
break; | ||
ename = entry->d_name; | ||
|
||
if (String_eq(ename, ".") || | ||
String_eq(ename, "..") || | ||
String_eq(ename, "0") || | ||
String_eq(ename, "1") || | ||
String_eq(ename, "2")) | ||
continue; | ||
|
||
#ifdef HAVE_OPENAT | ||
fp = Compat_fopenat(dirfd(fdinfoDir), ename, "r"); | ||
#else | ||
fp = Compat_fopenat(fdinfoPathBuf, ename, "r"); | ||
#endif | ||
if (!fp) | ||
continue; | ||
|
||
while (fgets(buf, sizeof(buf), fp)) { | ||
if (!String_startsWith(buf, "drm-")) | ||
continue; | ||
|
||
if (String_startsWith(buf, "drm-client-id:")) { | ||
char *endptr; | ||
|
||
if (sstate == SECST_NEW) { | ||
struct client_id* new; | ||
|
||
assert(client_id != INVALID_CLIENT_ID); | ||
|
||
new = xMalloc(sizeof(*new)); | ||
*new = (struct client_id) { | ||
.id = client_id, | ||
.pdev = pdev, | ||
.next = parsed_ids, | ||
}; | ||
pdev = NULL; | ||
|
||
parsed_ids = new; | ||
} | ||
|
||
sstate = SECST_UNKNOWN; | ||
|
||
errno = 0; | ||
client_id = strtoull(buf + strlen("drm-client-id:"), &endptr, 10); | ||
if (errno || *endptr != '\n') | ||
client_id = INVALID_CLIENT_ID; | ||
} else if (String_startsWith(buf, "drm-pdev:")) { | ||
const char* p; | ||
char* q; | ||
|
||
p = buf + strlen("drm-pdev:"); | ||
|
||
while (isspace((unsigned char)*p)) | ||
p++; | ||
|
||
q = strchr(p, '\n'); | ||
if (q) | ||
*q = '\0'; | ||
|
||
assert(!pdev || String_eq(pdev, p)); | ||
if (!pdev) | ||
pdev = xStrdup(p); | ||
} else if (String_startsWith(buf, "drm-engine-")) { | ||
const char* delim; | ||
const char* engineStart; | ||
char* endptr; | ||
unsigned long long int value; | ||
|
||
if (sstate == SECST_DUPLICATE) | ||
continue; | ||
|
||
if (String_startsWith(buf, "drm-engine-capacity-")) | ||
continue; | ||
|
||
delim = strchr(buf, ':'); | ||
engineStart = buf + strlen("drm-engine-"); | ||
|
||
errno = 0; | ||
value = strtoull(delim + 1, &endptr, 10); | ||
if (errno == 0 && String_startsWith(endptr, " ns")) { | ||
if (sstate == SECST_UNKNOWN) { | ||
if (client_id != INVALID_CLIENT_ID && !is_duplicate_client(parsed_ids, client_id, pdev)) | ||
sstate = SECST_NEW; | ||
else | ||
sstate = SECST_DUPLICATE; | ||
} | ||
|
||
if (sstate == SECST_NEW) { | ||
new_gpu_time += value; | ||
update_system(lpl, value, xStrndup(engineStart, delim - engineStart)); | ||
} | ||
} | ||
} | ||
} /* finished parsing lines */ | ||
|
||
fclose(fp); | ||
|
||
if (sstate == SECST_NEW) { | ||
struct client_id* new; | ||
|
||
assert(client_id != INVALID_CLIENT_ID); | ||
|
||
new = xMalloc(sizeof(*new)); | ||
*new = (struct client_id) { | ||
.id = client_id, | ||
.pdev = pdev, | ||
.next = parsed_ids, | ||
}; | ||
pdev = NULL; | ||
|
||
parsed_ids = new; | ||
} | ||
|
||
free(pdev); | ||
} /* finished parsing fdinfo entries */ | ||
|
||
if (new_gpu_time > 0) { | ||
const Machine* host = lp->super.host; | ||
unsigned long long int gputimeDelta; | ||
uint64_t monotonictimeDelta; | ||
|
||
Process_updateFieldWidth(GPU_TIME, ceil(log10(new_gpu_time))); | ||
|
||
gputimeDelta = saturatingSub(new_gpu_time, lp->gpu_time); | ||
monotonictimeDelta = host->monotonicMs - host->prevMonotonicMs; | ||
if (gputimeDelta == 0) | ||
lp->gpu_percent = 0.0f; | ||
else if (monotonictimeDelta == 0) | ||
lp->gpu_percent = -1.0f; | ||
else | ||
lp->gpu_percent = 100.0f * gputimeDelta / (1000 * 1000) / monotonictimeDelta; | ||
} else | ||
lp->gpu_percent = 0.0f; | ||
|
||
out: | ||
|
||
lp->gpu_time = new_gpu_time; | ||
|
||
while (parsed_ids) { | ||
struct client_id* next = parsed_ids->next; | ||
free(parsed_ids->pdev); | ||
free(parsed_ids); | ||
parsed_ids = next; | ||
} | ||
|
||
if (fdinfoDir) | ||
closedir(fdinfoDir); | ||
if (fdinfoFd != -1) | ||
close(fdinfoFd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef HEADER_GPU | ||
#define HEADER_GPU | ||
/* | ||
htop - GPU.h | ||
(C) 2023 htop dev team | ||
Released under the GNU GPLv2+, see the COPYING file | ||
in the source distribution for its full text. | ||
*/ | ||
|
||
#include "linux/LinuxProcess.h" | ||
#include "linux/LinuxProcessList.h" | ||
|
||
|
||
void GPU_readProcessData(LinuxProcessList* lpl, LinuxProcess* lp, openat_arg_t procFd); | ||
|
||
#endif /* HEADER_GPU */ |
Oops, something went wrong.