File indexing completed on 2024-04-28 05:31:37

0001 /*
0002     SPDX-FileCopyrightText: 2007 John Tapsell <tapsell@kde.org>
0003     SPDX-FileCopyrightText: 2015 Gregor Mi <codestruct@posteo.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef PROCESS_H
0009 #define PROCESS_H
0010 
0011 #include <QElapsedTimer>
0012 #include <QFlags>
0013 #include <QList>
0014 #include <QTime>
0015 #include <QVariant>
0016 
0017 #include "processcore_export.h"
0018 
0019 namespace KSysGuard
0020 {
0021 class ProcessPrivate; // forward decl d-ptr
0022 
0023 class PROCESSCORE_EXPORT Process
0024 {
0025 public:
0026     enum ProcessStatus { Running, Sleeping, DiskSleep, Zombie, Stopped, Paging, Ended, OtherStatus = 99 };
0027     enum IoPriorityClass { None, RealTime, BestEffort, Idle };
0028     enum Scheduler { Other = 0, Fifo, RoundRobin, Batch, SchedulerIdle, Interactive }; ///< Interactive is Solaris only
0029 
0030     Process();
0031     Process(qlonglong _pid, qlonglong _ppid, Process *_parent);
0032     virtual ~Process();
0033 
0034     long pid() const; ///< The system's ID for this process.  1 for init.  -1 for our virtual 'parent of init' process used just for convenience.
0035 
0036     long parentPid() const; ///< The system's ID for the parent of this process.  Set to -1 if it has no parent (e.g. 'init' on Linux).
0037     void setParentPid(long parent_pid);
0038 
0039     /** A guaranteed NON-NULL pointer for all real processes to the parent process except for the fake process with pid -1.
0040      *  The Parent's pid is the same value as the parent_pid.  The parent process will be also pointed
0041      *  to by ProcessModel::mPidToProcess to there is no need to worry about mem management in using parent.
0042      *  For process without a parent (such as 'init' on Linux, parent will point to a (fake) process with pid -1 to simplify things.
0043      *  For the fake process, this will point to NULL
0044      */
0045     Process *parent() const;
0046     void setParent(Process *parent);
0047 
0048     QList<Process *> &children() const; // REF, make non-ref later! ///< A list of all the direct children that the process has.  Children of children are not
0049                                         // listed here, so note that children_pids <= numChildren
0050 
0051     unsigned long &numChildren() const; // REF, make non-ref later!
0052 
0053     QString login() const;
0054     void setLogin(const QString &login); ///< The user login name.  Only used for processes on remote machines.  Otherwise use uid to get the name
0055 
0056     qlonglong uid() const;
0057     void setUid(qlonglong uid); ///< The user id that the process is running as
0058 
0059     qlonglong euid() const;
0060     void setEuid(qlonglong euid); ///< The effective user id that the process is running as
0061 
0062     qlonglong suid() const;
0063     void setSuid(qlonglong suid); ///< The set user id that the process is running as
0064 
0065     qlonglong fsuid() const;
0066     void setFsuid(qlonglong fsuid); ///< The file system user id that the process is running as.
0067 
0068     qlonglong gid() const;
0069     void setGid(qlonglong gid); ///< The process group id that the process is running as
0070 
0071     qlonglong egid() const;
0072     void setEgid(qlonglong egid); ///< The effective group id that the process is running as
0073 
0074     qlonglong sgid() const;
0075     void setSgid(qlonglong sgid); ///< The set group id that the process is running as
0076 
0077     qlonglong fsgid() const;
0078     void setFsgid(qlonglong fsgid); ///< The file system group id that the process is running as
0079 
0080     qlonglong tracerpid() const;
0081     void setTracerpid(qlonglong tracerpid); ///< If this is being debugged, this is the process that is debugging it, or 0 otherwise
0082 
0083     QByteArray tty() const;
0084     void setTty(const QByteArray &tty); ///< The name of the tty the process owns
0085 
0086     qlonglong userTime() const;
0087     void setUserTime(qlonglong userTime); ///< The time, in 100ths of a second, spent in total on user calls. -1 if not known
0088 
0089     qlonglong sysTime() const;
0090     void setSysTime(qlonglong sysTime); ///< The time, in 100ths of a second, spent in total on system calls.  -1 if not known
0091 
0092     /**
0093      * the value is expressed in clock ticks (since Linux 2.6; we only handle this case) since system boot
0094      */
0095     qlonglong startTime() const;
0096     void
0097     setStartTime(qlonglong startTime); /// The time the process started after system boot. Since Linux 2.6, the value is expressed in clock ticks. See man proc.
0098 
0099     int userUsage() const;
0100     void setUserUsage(int userUsage); ///< Percentage (0 to 100).  It might be more than 100% on multiple cpu core systems
0101 
0102     int sysUsage() const;
0103     void setSysUsage(int sysUsage); ///< Percentage (0 to 100).  It might be more than 100% on multiple cpu core systems
0104 
0105     int &totalUserUsage() const; // REF, make non-ref later!
0106     void setTotalUserUsage(int totalUserUsage); ///< Percentage (0 to 100) from the sum of itself and all its children recursively.  If there's no children,
0107                                                 ///< it's equal to userUsage.  It might be more than 100% on multiple cpu core systems
0108 
0109     int &totalSysUsage() const; // REF, make non-ref later!
0110     void setTotalSysUsage(int totalSysUsage); ///< Percentage (0 to 100) from the sum of itself and all its children recursively. If there's no children, it's
0111                                               ///< equal to sysUsage. It might be more than 100% on multiple cpu core systems
0112 
0113     int niceLevel() const;
0114     void setNiceLevel(int niceLevel); ///< If Scheduler = Other, niceLevel is the niceness (-20 to 20) of this process.  A lower number means a higher priority.
0115                                       ///< Otherwise sched priority (1 to 99)
0116 
0117     Scheduler scheduler() const;
0118     void setScheduler(Scheduler scheduler); ///< The scheduler this process is running in.  See man sched_getscheduler for more info
0119 
0120     IoPriorityClass ioPriorityClass() const;
0121     void setIoPriorityClass(IoPriorityClass ioPriorityClass); ///< The IO priority class.  See man ionice for detailed information.
0122 
0123     int ioniceLevel() const;
0124     void setIoniceLevel(int ioniceLevel); ///< IO Niceness (0 to 7) of this process.  A lower number means a higher io priority.  -1 if not known or not
0125                                           ///< applicable because ioPriorityClass is Idle or None
0126 
0127     qlonglong vmSize() const;
0128     void setVmSize(qlonglong vmSize); ///< Virtual memory size in KiloBytes, including memory used, mmap'ed files, graphics memory etc,
0129 
0130     qlonglong vmRSS() const;
0131     void setVmRSS(qlonglong vmRSS); ///< Physical memory used by the process and its shared libraries.  If the process and libraries are swapped to disk, this
0132                                     ///< could be as low as 0
0133 
0134     qlonglong vmURSS() const;
0135     void setVmURSS(qlonglong vmURSS); ///< Physical memory used only by the process, and not counting the code for shared libraries. Set to -1 if unknown
0136 
0137     qlonglong vmPSS() const;
0138     void setVmPSS(qlonglong vmPSS); ///< Proportional set size, the amount of private physical memory used by the process + the amount of shared memory used
0139                                     ///< divided over the number of processes using it.
0140 
0141     QString name() const;
0142     void setName(const QString &name); ///< The name (e.g. "ksysguard", "konversation", "init")
0143 
0144     QString &command() const; // REF, make non-ref later!
0145     void setCommand(const QString &command); ///< The command the process was launched with
0146 
0147     ProcessStatus status() const;
0148     void setStatus(ProcessStatus status); ///< Whether the process is running/sleeping/etc
0149 
0150     qlonglong ioCharactersRead() const;
0151     void setIoCharactersRead(qlonglong number); ///< The number of bytes which this task has caused to be read from storage
0152 
0153     qlonglong ioCharactersWritten() const;
0154     void setIoCharactersWritten(qlonglong number); ///< The number of bytes which this task has caused, or shall cause to be written to disk.
0155 
0156     qlonglong ioReadSyscalls() const;
0157     void setIoReadSyscalls(qlonglong number); ///< Number of read I/O operations, i.e. syscalls like read() and pread().
0158 
0159     qlonglong ioWriteSyscalls() const;
0160     void setIoWriteSyscalls(qlonglong number); ///< Number of write I/O operations, i.e. syscalls like write() and pwrite().
0161 
0162     qlonglong ioCharactersActuallyRead() const;
0163     void setIoCharactersActuallyRead(qlonglong number); ///< Number of bytes which this process really did cause to be fetched from the storage layer.
0164 
0165     qlonglong ioCharactersActuallyWritten() const;
0166     void setIoCharactersActuallyWritten(qlonglong number); ///< Attempt to count the number of bytes which this process caused to be sent to the storage layer.
0167 
0168     long ioCharactersReadRate() const;
0169     void setIoCharactersReadRate(long number); ///< The rate, in bytes per second, which this task has caused to be read from storage
0170 
0171     long ioCharactersWrittenRate() const;
0172     void setIoCharactersWrittenRate(long number); ///< The rate, in bytes per second, which this task has caused, or shall cause to be written to disk.
0173 
0174     long ioReadSyscallsRate() const;
0175     void setIoReadSyscallsRate(long number); ///< Number of read I/O operations per second, i.e. syscalls like read() and pread().
0176 
0177     long ioWriteSyscallsRate() const;
0178     void setIoWriteSyscallsRate(long number); ///< Number of write I/O operations per second, i.e. syscalls like write() and pwrite().
0179 
0180     long ioCharactersActuallyReadRate() const;
0181     void setIoCharactersActuallyReadRate(long number); ///< Number of bytes per second which this process really did cause to be fetched from the storage layer.
0182 
0183     long ioCharactersActuallyWrittenRate() const;
0184     void setIoCharactersActuallyWrittenRate(
0185         long number); ///< Attempt to count the number of bytes per second which this process caused to be sent to the storage layer.
0186 
0187     int numThreads() const; ///< Number of threads that this process has, including the main one.  0 if not known
0188     void setNumThreads(int number); ///< The number of threads that this process has, including this process.
0189 
0190     int noNewPrivileges() const;
0191     void setNoNewPrivileges(int number); ///< Linux process flag NoNewPrivileges
0192 
0193     int index() const; ///< Each process has a parent process.  Each sibling has a unique number to identify it under that parent.  This is that number.
0194     void setIndex(int index);
0195 
0196     qlonglong &vmSizeChange() const; // REF, make non-ref later!  ///< The change in vmSize since last update, in KiB
0197 
0198     qlonglong &vmRSSChange() const; // REF, make non-ref later!   ///< The change in vmRSS since last update, in KiB
0199 
0200     qlonglong &vmURSSChange() const; // REF, make non-ref later!  ///< The change in vmURSS since last update, in KiB
0201 
0202     qlonglong vmPSSChange() const; ///< The change in vmPSS since last update, in KiB.
0203 
0204     unsigned long &pixmapBytes() const; // REF, make non-ref later! ///< The number of bytes used for pixmaps/images and not counted by vmRSS or vmURSS
0205 
0206     bool &hasManagedGuiWindow() const; // REF, make non-ref later!
0207 
0208     QElapsedTimer
0209     timeKillWasSent() const; ///< This is usually a NULL time.  When trying to kill a process, this is the time that the kill signal was sent to the process.
0210 
0211     QString translatedStatus() const; ///< Returns a translated string of the status. e.g. "Running" etc
0212 
0213     QString niceLevelAsString() const; ///< Returns a simple translated string of the nice priority.  e.g. "Normal", "High", etc
0214 
0215     QString ioniceLevelAsString() const; ///< Returns a simple translated string of the io nice priority.  e.g. "Normal", "High", etc
0216 
0217     QString ioPriorityClassAsString() const; ///< Returns a translated string of the io nice class.  i.e. "None", "Real Time", "Best Effort", "Idle"
0218 
0219     QString schedulerAsString() const; ///< Returns a translated string of the scheduler class.  e.g. "FIFO", "Round Robin", "Batch"
0220 
0221     QString cGroup() const;
0222     void setCGroup(const QString &cGroup); ///< Linux Control Group (cgroup)
0223 
0224     QString macContext() const;
0225     void setMACContext(const QString &macContext); ///< Mandatory Access Control (SELinux or AppArmor) Context
0226 
0227     /** This is the number of 1/1000ths of a second since this
0228      *  particular process was last updated compared to when all the processes
0229      *  were updated. The purpose is to allow a more fine tracking of the time
0230      *  a process has been running for.
0231      *
0232      *  This is updated in processes.cpp and so shouldn't be touched by the
0233      *  OS dependent classes.
0234      */
0235     int elapsedTimeMilliSeconds() const;
0236     void setElapsedTimeMilliSeconds(int value);
0237 
0238     /** An enum to keep track of what changed since the last update.  Note that we
0239      * the maximum we can use is 0x4000, so some of the enums represent multiple variables
0240      */
0241     enum Change {
0242         Nothing = 0x0,
0243         Uids = 0x1,
0244         Gids = 0x2,
0245         Tracerpid = 0x4,
0246         Tty = 0x8,
0247         Usage = 0x10,
0248         TotalUsage = 0x20,
0249         NiceLevels = 0x40,
0250         VmSize = 0x80,
0251         VmRSS = 0x100,
0252         VmURSS = 0x200,
0253         Name = 0x400,
0254         Command = 0x800,
0255         Status = 0x1000,
0256         Login = 0x2000,
0257         IO = 0x4000,
0258         NumThreads = 0x8000,
0259         VmPSS = 0x10000,
0260     };
0261     Q_DECLARE_FLAGS(Changes, Change)
0262 
0263     Changes changes() const; /**< A QFlags representing what has changed */
0264     void setChanges(Change changes);
0265 
0266     using Updates = QList<QPair<Change, QVariant>>;
0267 
0268 private:
0269     void clear();
0270 
0271 private:
0272     ProcessPrivate *const d;
0273 };
0274 
0275 Q_DECLARE_OPERATORS_FOR_FLAGS(Process::Changes)
0276 }
0277 
0278 Q_DECLARE_METATYPE(KSysGuard::Process::Updates)
0279 
0280 #endif