From 1b805a31720727008b32b1129a167758519fd4db Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 2 May 2022 16:04:21 +0200 Subject: New upstream version 3.2.0 --- linux/LinuxProcessList.c | 83 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 28 deletions(-) (limited to 'linux/LinuxProcessList.c') diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 3bfe7db..5e18f6d 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -174,21 +174,24 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { LinuxProcessList* this = (LinuxProcessList*) super; unsigned int existing = 0, active = 0; - DIR* dir = opendir("/sys/devices/system/cpu"); - if (!dir) { - this->cpuData = xReallocArrayZero(this->cpuData, super->existingCPUs ? (super->existingCPUs + 1) : 0, 2, sizeof(CPUData)); + // Initialize the cpuData array before anything else. + if (!this->cpuData) { + this->cpuData = xCalloc(2, sizeof(CPUData)); this->cpuData[0].online = true; /* average is always "online" */ this->cpuData[1].online = true; super->activeCPUs = 1; super->existingCPUs = 1; - return; } + DIR* dir = opendir("/sys/devices/system/cpu"); + if (!dir) + return; + unsigned int currExisting = super->existingCPUs; const struct dirent* entry; while ((entry = readdir(dir)) != NULL) { - if (entry->d_type != DT_DIR) + if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) continue; if (!String_startsWith(entry->d_name, "cpu")) @@ -233,6 +236,10 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { closedir(dir); + // return if no CPU is found + if (existing < 1) + return; + #ifdef HAVE_SENSORS_SENSORS_H /* When started with offline CPUs, libsensors does not monitor those, * even when they become online. */ @@ -493,6 +500,8 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, openat_arg_t proc unsigned long long last_read = process->io_read_bytes; unsigned long long last_write = process->io_write_bytes; + unsigned long long time_delta = realtimeMs > process->io_last_scan_time_ms ? realtimeMs - process->io_last_scan_time_ms : 0; + char* buf = buffer; const char* line; while ((line = strsep(&buf, "\n")) != NULL) { @@ -502,7 +511,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, openat_arg_t proc process->io_rchar = strtoull(line + 7, NULL, 10); } else if (String_startsWith(line + 1, "ead_bytes: ")) { process->io_read_bytes = strtoull(line + 12, NULL, 10); - process->io_rate_read_bps = (process->io_read_bytes - last_read) * /*ms to s*/1000 / (realtimeMs - process->io_last_scan_time_ms); + process->io_rate_read_bps = time_delta ? (process->io_read_bytes - last_read) * /*ms to s*/1000. / time_delta : NAN; } break; case 'w': @@ -510,7 +519,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, openat_arg_t proc process->io_wchar = strtoull(line + 7, NULL, 10); } else if (String_startsWith(line + 1, "rite_bytes: ")) { process->io_write_bytes = strtoull(line + 13, NULL, 10); - process->io_rate_write_bps = (process->io_write_bytes - last_write) * /*ms to s*/1000 / (realtimeMs - process->io_last_scan_time_ms); + process->io_rate_write_bps = time_delta ? (process->io_write_bytes - last_write) * /*ms to s*/1000. / time_delta : NAN; } break; case 's': @@ -900,16 +909,27 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t bool changed = !process->cgroup || !String_eq(process->cgroup, output); + Process_updateFieldWidth(CGROUP, strlen(output)); free_and_xStrdup(&process->cgroup, output); - if (!changed) + if (!changed) { + if(process->cgroup_short) { + Process_updateFieldWidth(CCGROUP, strlen(process->cgroup_short)); + } else { + //CCGROUP is alias to normal CGROUP if shortening fails + Process_updateFieldWidth(CCGROUP, strlen(process->cgroup)); + } return; + } char* cgroup_short = CGroup_filterName(process->cgroup); if (cgroup_short) { + Process_updateFieldWidth(CCGROUP, strlen(cgroup_short)); free_and_xStrdup(&process->cgroup_short, cgroup_short); free(cgroup_short); } else { + //CCGROUP is alias to normal CGROUP if shortening fails + Process_updateFieldWidth(CCGROUP, strlen(process->cgroup)); free(process->cgroup_short); process->cgroup_short = NULL; } @@ -1027,6 +1047,9 @@ static void LinuxProcessList_readSecattrData(LinuxProcess* process, openat_arg_t if (newline) { *newline = '\0'; } + + Process_updateFieldWidth(SECATTR, strlen(buffer)); + if (process->secattr && String_eq(process->secattr, buffer)) { return; } @@ -1370,6 +1393,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ ProcessList* pl = (ProcessList*) this; const struct dirent* entry; const Settings* settings = pl->settings; + const ScreenSettings* ss = settings->ss; #ifdef HAVE_OPENAT int dirFd = openat(parentFd, dirname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); @@ -1463,7 +1487,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ continue; } - if (settings->flags & PROCESS_FLAG_IO) + if (ss->flags & PROCESS_FLAG_IO) LinuxProcessList_readIoFile(lp, procFd, pl->realtimeMs); if (!LinuxProcessList_readStatmFile(lp, procFd)) @@ -1472,8 +1496,9 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ { bool prev = proc->usesDeletedLib; - if ((settings->flags & PROCESS_FLAG_LINUX_LRS_FIX) || - (settings->highlightDeletedExe && !proc->procExeDeleted && !proc->isKernelThread && !proc->isUserlandThread)) { + if (!proc->isKernelThread && !proc->isUserlandThread && + ((ss->flags & PROCESS_FLAG_LINUX_LRS_FIX) || (settings->highlightDeletedExe && !proc->procExeDeleted))) { + // Check if we really should recalculate the M_LRS value for this process uint64_t passedTimeInMs = pl->realtimeMs - lp->last_mlrs_calctime; @@ -1481,17 +1506,18 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ if (passedTimeInMs > recheck) { lp->last_mlrs_calctime = pl->realtimeMs; - LinuxProcessList_readMaps(lp, procFd, settings->flags & PROCESS_FLAG_LINUX_LRS_FIX, settings->highlightDeletedExe); + LinuxProcessList_readMaps(lp, procFd, ss->flags & PROCESS_FLAG_LINUX_LRS_FIX, settings->highlightDeletedExe); } } else { /* Copy from process structure in threads and reset if setting got disabled */ proc->usesDeletedLib = (proc->isUserlandThread && parent) ? parent->usesDeletedLib : false; + lp->m_lrs = (proc->isUserlandThread && parent) ? ((const LinuxProcess*)parent)->m_lrs : 0; } proc->mergedCommand.exeChanged |= prev ^ proc->usesDeletedLib; } - if ((settings->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) { + if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) { if (!parent) { // Read smaps file of each process only every second pass to improve performance static int smaps_flag = 0; @@ -1521,7 +1547,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ proc->tty_name = LinuxProcessList_updateTtyDevice(this->ttyDrivers, proc->tty_nr); } - if (settings->flags & PROCESS_FLAG_LINUX_IOPRIO) { + if (ss->flags & PROCESS_FLAG_LINUX_IOPRIO) { LinuxProcess_updateIOPriority(lp); } @@ -1529,6 +1555,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ float percent_cpu = (period < 1E-6) ? 0.0F : ((lp->utime + lp->stime - lasttimes) / period * 100.0); proc->percent_cpu = CLAMP(percent_cpu, 0.0F, activeCPUs * 100.0F); proc->percent_mem = proc->m_resident / (double)(pl->totalMem) * 100.0; + Process_updateCPUFieldWidths(proc->percent_cpu); if (! LinuxProcessList_updateUser(pl, proc, procFd)) goto errorReadingProcess; @@ -1536,13 +1563,13 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ if (!preExisting) { #ifdef HAVE_OPENVZ - if (settings->flags & PROCESS_FLAG_LINUX_OPENVZ) { + if (ss->flags & PROCESS_FLAG_LINUX_OPENVZ) { LinuxProcessList_readOpenVZData(lp, procFd); } #endif #ifdef HAVE_VSERVER - if (settings->flags & PROCESS_FLAG_LINUX_VSERVER) { + if (ss->flags & PROCESS_FLAG_LINUX_VSERVER) { LinuxProcessList_readVServerData(lp, procFd); } #endif @@ -1567,32 +1594,32 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_ } #ifdef HAVE_DELAYACCT - if (settings->flags & PROCESS_FLAG_LINUX_DELAYACCT) { + if (ss->flags & PROCESS_FLAG_LINUX_DELAYACCT) { LinuxProcessList_readDelayAcctData(this, lp); } #endif - if (settings->flags & PROCESS_FLAG_LINUX_CGROUP) { + if (ss->flags & PROCESS_FLAG_LINUX_CGROUP) { LinuxProcessList_readCGroupFile(lp, procFd); } - if (settings->flags & PROCESS_FLAG_LINUX_OOM) { + if (ss->flags & PROCESS_FLAG_LINUX_OOM) { LinuxProcessList_readOomData(lp, procFd); } - if (settings->flags & PROCESS_FLAG_LINUX_CTXT) { + if (ss->flags & PROCESS_FLAG_LINUX_CTXT) { LinuxProcessList_readCtxtData(lp, procFd); } - if (settings->flags & PROCESS_FLAG_LINUX_SECATTR) { + if (ss->flags & PROCESS_FLAG_LINUX_SECATTR) { LinuxProcessList_readSecattrData(lp, procFd); } - if (settings->flags & PROCESS_FLAG_CWD) { + if (ss->flags & PROCESS_FLAG_CWD) { LinuxProcessList_readCwd(lp, procFd); } - if ((settings->flags & PROCESS_FLAG_LINUX_AUTOGROUP) && this->haveAutogroup) { + if ((ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) && this->haveAutogroup) { LinuxProcessList_readAutogroup(lp, procFd); } @@ -1994,7 +2021,7 @@ static inline double LinuxProcessList_scanCPUTime(ProcessList* super) { return period; } -static int scanCPUFreqencyFromSysCPUFreq(LinuxProcessList* this) { +static int scanCPUFrequencyFromSysCPUFreq(LinuxProcessList* this) { unsigned int existingCPUs = this->super.existingCPUs; int numCPUsWithFrequency = 0; unsigned long totalFrequency = 0; @@ -2057,7 +2084,7 @@ static int scanCPUFreqencyFromSysCPUFreq(LinuxProcessList* this) { return 0; } -static void scanCPUFreqencyFromCPUinfo(LinuxProcessList* this) { +static void scanCPUFrequencyFromCPUinfo(LinuxProcessList* this) { FILE* file = fopen(PROCCPUINFOFILE, "r"); if (file == NULL) return; @@ -2114,11 +2141,11 @@ static void LinuxProcessList_scanCPUFrequency(LinuxProcessList* this) { this->cpuData[i].frequency = NAN; } - if (scanCPUFreqencyFromSysCPUFreq(this) == 0) { + if (scanCPUFrequencyFromSysCPUFreq(this) == 0) { return; } - scanCPUFreqencyFromCPUinfo(this); + scanCPUFrequencyFromCPUinfo(this); } void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { @@ -2146,7 +2173,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { return; } - if (settings->flags & PROCESS_FLAG_LINUX_AUTOGROUP) { + if (settings->ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) { // Refer to sched(7) 'autogroup feature' section // The kernel feature can be enabled/disabled through procfs at // any time, so check for it at the start of each sample - only -- cgit v1.2.3