-
Notifications
You must be signed in to change notification settings - Fork 1
/
getters.cpp
48 lines (44 loc) · 1.18 KB
/
getters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <ctime>
#include <vector>
#include <cuda_runtime_api.h>
#include <nvml.h>
#include <unistd.h>
std::string getHostName(){
const size_t bufferSize = 1024; // Define the buffer size as 1 kB
std::vector<char> buffer(bufferSize);
// Call gethostname and check for errors
if (gethostname(buffer.data(), bufferSize) != 0) {
std::cerr << "Error getting hostname" << std::endl;
exit(1);
}
std::string hostname(buffer.data());
return hostname;
}
std::string getProcessName(unsigned int pid) {
std::stringstream ss;
ss << "/proc/" << pid << "/comm";
std::ifstream commFile(ss.str().c_str());
std::string processName;
if (commFile.good()) {
std::getline(commFile, processName);
} else {
processName = "Unknown";
}
return processName;
}
std::string getCommandName(unsigned int pid) {
std::stringstream ss;
ss << "/proc/" << pid << "/cmdline";
std::ifstream commFile(ss.str().c_str());
std::string commandName;
if (commFile.good()) {
std::getline(commFile, commandName);
} else {
commandName = "Unknown";
}
return commandName;
}