File indexing completed on 2024-04-28 16:49:53

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