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

0001 /*
0002     SPDX-FileCopyrightText: 2007 John Tapsell <tapsell@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef PROCESSES_BASE_P_H
0008 #define PROCESSES_BASE_P_H
0009 
0010 #include "processes.h"
0011 #include <QObject>
0012 #include <QSet>
0013 
0014 namespace KSysGuard
0015 {
0016 class Process;
0017 /**
0018  * This class contains the specific code to get the processes from the given host.
0019  *
0020  * To port this to other operating systems you need to make a processes_(osname).cpp  file
0021  * which implements all of the function below.  If you need private functions/variables etc put them in
0022  * the Private class.
0023  *
0024  * @author John Tapsell <tapsell@kde.org>
0025  */
0026 class AbstractProcesses : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     AbstractProcesses()
0032     {
0033     }
0034     ~AbstractProcesses() override
0035     {
0036     }
0037 
0038     /** \brief Get a set of the currently running process PIDs.
0039      *
0040      *  To get information about processes, this will be the first function called.
0041      */
0042     virtual QSet<long> getAllPids() = 0;
0043 
0044     /** \brief Return the parent PID for the given process PID.
0045      *
0046      *  For each of the PIDs that getAllPids() returns, getParentPid will be called.
0047      *  This is used to setup the tree structure.
0048      *  For a particular PID, this is guaranteed to be called before updateProcessInfo for that PID.
0049      *  However this may be called several times in a row before the updateProcessInfo is called, so be careful
0050      *  if you want to try to preserve state in Private.
0051      */
0052     virtual long getParentPid(long pid) = 0;
0053 
0054     /** \brief Fill in the given Process class with information for given PID.
0055      *
0056      *  This will be called for every PID, after getParentPid() has been called for the same parameter.
0057      *
0058      *  The process->pid() process->ppid and process->parent  are all guaranteed
0059      *  to be filled in correctly and process->parent will be non null.
0060      */
0061     virtual bool updateProcessInfo(long pid, Process *process) = 0;
0062 
0063     /** \brief Send the specified named POSIX signal to the process given.
0064      *
0065      *  For example, to indicate for process 324 to STOP do:
0066      *  \code
0067      *    #include <signals.h>
0068      *     ...
0069      *
0070      *    KSysGuard::Processes::sendSignal(324, SIGSTOP);
0071      *  \endcode
0072      *  @return Error::NoError if successful
0073      *
0074      */
0075     virtual Processes::Error sendSignal(long pid, int sig) = 0;
0076 
0077     /** \brief Set the priority for a process.
0078      *
0079      *  For the normal scheduler, this is usually from 19
0080      *  (very nice, lowest priority) to -20 (highest priority).  The default value for a process is 0.
0081      *
0082      *  This has no effect if the scheduler is not the normal one (SCHED_OTHER in Linux).
0083      *
0084      *  @return Error::NoError if successful
0085      */
0086     virtual Processes::Error setNiceness(long pid, int priority) = 0;
0087 
0088     /** \brief Set the scheduler for a process.
0089      *
0090      * This is defined according to POSIX.1-2001
0091      *  See "man sched_setscheduler" for more information.
0092      *
0093      *  @p priorityClass One of SCHED_FIFO, SCHED_RR, SCHED_OTHER, and SCHED_BATCH
0094      *  @p priority Set to 0 for SCHED_OTHER and SCHED_BATCH.  Between 1 and 99 for SCHED_FIFO and SCHED_RR
0095      *  @return Error::NoError if successful
0096      */
0097     virtual Processes::Error setScheduler(long pid, int priorityClass, int priority) = 0;
0098 
0099     /** \brief Return the total amount of physical memory in KiB.
0100      *
0101      *  This is fast (just a system call in most OSes)
0102      *  Returns 0 on error
0103      */
0104     virtual long long totalPhysicalMemory() = 0;
0105 
0106     /** \brief Set the i/o priority for a process.
0107      *
0108      *  This is from 7 (very nice, lowest i/o priority) to
0109      *  0 (highest priority).  The default value is determined as: io_nice = (cpu_nice + 20) / 5.
0110      *
0111      *  @return Error::NoError if successful
0112      */
0113     virtual Processes::Error setIoNiceness(long pid, int priorityClass, int priority) = 0;
0114 
0115     /** \brief Returns true if ionice is supported on this system
0116      */
0117     virtual bool supportsIoNiceness() = 0;
0118 
0119     /** \brief Return the number of processor cores enabled.
0120      *
0121      *  (A system can disable processors.  Disabled processors are not counted here).
0122      *  This is fast (just a system call on most OSes) */
0123     virtual long numberProcessorCores() = 0;
0124 
0125     /** \brief Update the process information for all processes.
0126      *
0127      *  Get all the current process information from the machine.  When done, emit updateAllProcesses().
0128      */
0129     virtual void updateAllProcesses(Processes::UpdateFlags updateFlags) = 0;
0130 Q_SIGNALS:
0131     /** \brief This is emitted when the processes have been updated, and the view should be refreshed.
0132      */
0133     void processesUpdated();
0134 
0135     void processUpdated(long pid, const KSysGuard::Process::Updates &changes);
0136 };
0137 }
0138 
0139 #endif // PROCESSES_BASE_P_H