Comments (2)
I guess that it's hard for a process to know if it's running in a VM/container.
Maybe add a column showing the hostname
of the machine which is probably the cleanest way to show that information?
from nvtop.
Hmm I wonder if the cgroup path could be used?
I don’t really know C but a certain large language model suggested something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
// Function to trim newline character from strings
void trim_newline(char* string) {
if (string == NULL) return;
char* newline = strchr(string, '\n');
if (newline) *newline = 0;
}
// Function to get the container name from cgroup file
void get_container_name_from_pid(int pid, char* container_name, int max_len) {
char path[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
FILE* file;
snprintf(path, sizeof(path), "/proc/%d/cgroup", pid);
if ((file = fopen(path, "r")) == NULL) {
perror("Error opening cgroup file");
strcpy(container_name, "");
return;
}
while (fgets(buffer, sizeof(buffer), file) != NULL) {
// Docker containers are usually identified by "docker" in the cgroup path
if (strstr(buffer, "/docker/") != NULL) {
char* start = strrchr(buffer, '/');
if (start != NULL) {
start++; // Move past the '/'
trim_newline(start);
strncpy(container_name, start, max_len);
fclose(file);
return;
}
}
}
// If we reach here, no container name was found
strcpy(container_name, "");
fclose(file);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Usage: %s <pid>\n", argv[0]);
return 1;
}
int pid = atoi(argv[1]);
char container_name[BUFFER_SIZE];
get_container_name_from_pid(pid, container_name, sizeof(container_name));
if (strlen(container_name) > 0) {
printf("Container name: %s\n", container_name);
} else {
printf("The PID %d is not running in a Docker container.\n", pid);
}
return 0;
}
from nvtop.
Related Issues (20)
- RX 7600 does not display decode bar when decoding videos HOT 2
- Nvtop constantly shows 25% being used even when there are no processes which have that much total usage in the table below HOT 6
- Question: works via Termux on Android Adreno devices with kernel 6.1? like new Xiaomi 14’s
- error: use of undeclared identifier 'kIOMainPortDefault'
- Compilation error from macOS on Apple ARM HOT 1
- [Feature Request] Add plot height config?
- Getting nvtop Snap to show GPU processes on AMD/Intel
- Support for mouse click like htop.
- [Feature Request] Support for xe kernel module HOT 1
- Hardware Encoding/Decoding At Same Time Breaks NVTOP on AMD GPU
- 8 Nvidia GPUs: No GPU to monitor. HOT 1
- Ubuntu 24:04 : AppImage throws error "dlopen(): error loading libfuse.so.2"
- Option for Showing Only Compute/Graphical
- Show POW on graph
- Enable value above 100%
- core dumped Amd gpu HOT 5
- Assertion failed
- Weird characters on display when using AMD GPU. HOT 1
- Crash on FreeBSD 14.1 with LinuxKPI DRM 5.15.160 HOT 3
- Raspberry Pi GPU (Broadcom VideoCore) support is missing, but data is available
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nvtop.