mirror of
https://github.com/aristocratos/btop.git
synced 2024-09-29 08:41:19 +13:00
Switch /proc/pid/stat to string parsing to avoid bad charaters in comm field
This commit is contained in:
parent
098a914f4b
commit
d17c6d475a
2 changed files with 11 additions and 19 deletions
|
@ -130,7 +130,6 @@ namespace Proc {
|
||||||
vector<uint> c_pids;
|
vector<uint> c_pids;
|
||||||
c_pids.reserve((numpids + 10));
|
c_pids.reserve((numpids + 10));
|
||||||
numpids = 0;
|
numpids = 0;
|
||||||
uint parenthesis = 0;
|
|
||||||
|
|
||||||
//* Update uid_user map if /etc/passwd changed since last run
|
//* Update uid_user map if /etc/passwd changed since last run
|
||||||
if (!passwd_path.empty() && fs::last_write_time(passwd_path) != passwd_time) {
|
if (!passwd_path.empty() && fs::last_write_time(passwd_path) != passwd_time) {
|
||||||
|
@ -209,46 +208,38 @@ namespace Proc {
|
||||||
|
|
||||||
//* Get cpu usage, cpu cumulative and threads from /proc/[pid]/stat
|
//* Get cpu usage, cpu cumulative and threads from /proc/[pid]/stat
|
||||||
if (fs::exists((string)d.path() + "/stat")) {
|
if (fs::exists((string)d.path() + "/stat")) {
|
||||||
pread.clear(); instr.clear(); pstat.clear(); pstat.reserve(40); pstat.resize(3, "");
|
pread.clear(); instr.clear(); pstat.clear();
|
||||||
ifstream pread((string)d.path() + "/stat");
|
ifstream pread((string)d.path() + "/stat");
|
||||||
if (pread.good()) {
|
if (pread.good() && getline(pread, instr)) {
|
||||||
parenthesis = 1;
|
pstat = ssplit(instr.substr(instr.rfind(')') + 1), " ", 38);
|
||||||
pread.ignore(numeric_limits<streamsize>::max(), '(');
|
|
||||||
while (parenthesis > 0 && !pread.eof()) {
|
|
||||||
instr = pread.get();
|
|
||||||
if (instr == "(") ++parenthesis;
|
|
||||||
else if (instr == ")") --parenthesis;
|
|
||||||
}
|
|
||||||
pread.ignore(1);
|
|
||||||
while (getline(pread, instr, ' ') && pstat.size() < 40) pstat.push_back(instr);
|
|
||||||
}
|
}
|
||||||
pread.close();
|
pread.close();
|
||||||
|
|
||||||
if (pstat.size() < 22) continue;
|
if (pstat.size() < 22) continue;
|
||||||
|
|
||||||
//? Process state
|
//? Process state
|
||||||
state = pstat[3][0];
|
state = pstat[0][0];
|
||||||
|
|
||||||
//? Process parent pid
|
//? Process parent pid
|
||||||
ppid = stoul(pstat[4]);
|
ppid = stoul(pstat[1]);
|
||||||
|
|
||||||
//? Process nice value
|
//? Process nice value
|
||||||
p_nice = stoi(pstat[19]);
|
p_nice = stoi(pstat[16]);
|
||||||
|
|
||||||
//? Process number of threads
|
//? Process number of threads
|
||||||
threads = stoul(pstat[20]);
|
threads = stoul(pstat[17]);
|
||||||
|
|
||||||
//? Process utime + stime
|
//? Process utime + stime
|
||||||
cpu_t = stoull(pstat[14]) + stoull(pstat[15]);
|
cpu_t = stoull(pstat[11]) + stoull(pstat[12]);
|
||||||
|
|
||||||
//? Cache cpu times and cpu seconds
|
//? Cache cpu times and cpu seconds
|
||||||
if (new_cache) {
|
if (new_cache) {
|
||||||
cache[pid].cpu_t = cpu_t;
|
cache[pid].cpu_t = cpu_t;
|
||||||
cache[pid].cpu_s = stoull(pstat[22]);
|
cache[pid].cpu_s = stoull(pstat[19]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//? CPU number last executed on
|
//? CPU number last executed on
|
||||||
if (pstat.size() > 39) cpu_n = stoi(pstat[39]);
|
if (pstat.size() > 39) cpu_n = stoi(pstat[36]);
|
||||||
|
|
||||||
|
|
||||||
//? Process cpu usage since last update, 100'000 because (100 percent * 1000 milliseconds) for correct conversion
|
//? Process cpu usage since last update, 100'000 because (100 percent * 1000 milliseconds) for correct conversion
|
||||||
|
|
|
@ -262,6 +262,7 @@ namespace Tools {
|
||||||
//* Split <string> at <delim> <time> number of times (0 for unlimited) and return vector
|
//* Split <string> at <delim> <time> number of times (0 for unlimited) and return vector
|
||||||
vector<string> ssplit(string str, string delim = " ", int times = 0){
|
vector<string> ssplit(string str, string delim = " ", int times = 0){
|
||||||
vector<string> out;
|
vector<string> out;
|
||||||
|
if (times > 0) out.reserve(times);
|
||||||
if (!str.empty() && !delim.empty()){
|
if (!str.empty() && !delim.empty()){
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
int x = 0;
|
int x = 0;
|
||||||
|
|
Loading…
Reference in a new issue