File: processes.h

package info (click to toggle)
libksysguard 4%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,596 kB
  • sloc: cpp: 13,691; xml: 297; sh: 23; makefile: 11
file content (233 lines) | stat: -rw-r--r-- 7,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
    SPDX-FileCopyrightText: 2007 John Tapsell <tapsell@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#ifndef PROCESSES_H_
#define PROCESSES_H_

#include "process.h"
#include <QHash>
#include <QObject>
#include <QVariant>

#include "processcore_export.h"

namespace KSysGuard
{
/**
 * This class retrieves the processes currently running in an OS independent way.
 *
 * To use, do something like:
 *
 * \code
 *   #include "processes.h>
 *   #include "process.h>
 *
 *   KSysGuard::Processes *processes = new KSysGuard::Processes()
 *   QHash<long, Process *> processlist = processes->getProcesses();
 *   foreach( Process * process, processlist) {
 *     kDebug() << "Process with pid " << process->pid() << " is called " << process->name;
 *   }
 *   delete processes;
 *   processes = NULL;
 * \endcode
 *
 * @author John Tapsell <tapsell@kde.org>
 */
#ifdef Q_WS_WIN
class Processes : public QObject
#else
class PROCESSCORE_EXPORT Processes : public QObject
#endif
{
    Q_OBJECT

public:
    Processes(QObject *parent = nullptr);
    ~Processes() override;
    enum UpdateFlag {
        StandardInformation = 1,
        IOStatistics = 2,
        XMemory = 4,
        Smaps = 8,
    };
    Q_DECLARE_FLAGS(UpdateFlags, UpdateFlag)

    enum Error { Unknown = 0, InvalidPid, InvalidParameter, InsufficientPermissions, ProcessDoesNotExistOrZombie, NotSupported, NoError };

    /**
     *  Update all the process information.  After calling this, /proc or equivalent is scanned and
     *  the signals processChanged, etc  are emitted.
     *
     *  Set updateDuration to whatever time period that you update, in milliseconds.
     *  For example, if you update every 2000ms, set this to 2000.  That way it won't update
     *  more often than needed.
     */
    void updateAllProcesses(long updateDurationMS = 0, Processes::UpdateFlags updateFlags = {});

    /**
     *  Return information for one specific process.  Call getProcess(0) to get the
     *  fake process used as the top most parent for all processes.
     *  This doesn't fetch any new information and so returns almost instantly.
     *  Call updateAllProcesses() to actually fetch the process information.
     */
    Process *getProcess(long pid) const;

    /**
     *  Get the error code for the last command that failed.
     */
    Error lastError() const;

    /**
     *  Kill the specified process.  You may not have the privilege to kill the process.
     *  The process may also chose to ignore the command.  Send the SIGKILL signal to kill
     *  the process immediately.  You may lose any unsaved data.
     *
     *  @returns Successful or not in killing the process
     */
    bool killProcess(long pid);

    /**
     *  Send the specified named POSIX signal to the process given.
     *
     *  For example, to indicate for process 324 to STOP do:
     *  \code
     *    #include <signals.h>
     *     ...
     *
     *    KSysGuard::Processes::sendSignal(23, SIGSTOP);
     *  \endcode
     *
     */
    bool sendSignal(long pid, int sig);

    /**
     *  Set the priority for a process.  This is from 19 (very nice, lowest priority) to
     *    -20 (highest priority).  The default value for a process is 0.
     *
     *  @return false if you do not have permission to set the priority
     */
    bool setNiceness(long pid, int priority);

    /**
     *  Set the scheduler for a process.  This is defined according to POSIX.1-2001
     *  See "man sched_setscheduler" for more information.
     *
     *  @p priorityClass One of SCHED_FIFO, SCHED_RR, SCHED_OTHER, and SCHED_BATCH
     *  @p priority Set to 0 for SCHED_OTHER and SCHED_BATCH.  Between 1 and 99 for SCHED_FIFO and SCHED_RR
     *  @return false if you do not have permission to set the priority
     */
    bool setScheduler(long pid, KSysGuard::Process::Scheduler priorityClass, int priority);

    /**
     *  Set the io priority for a process.  This is from 7 (very nice, lowest io priority) to
     *  0 (highest priority).  The default value is determined as: io_nice = (cpu_nice + 20) / 5.
     *
     *  @return false if you do not have permission to set the priority
     */
    bool setIoNiceness(long pid, KSysGuard::Process::IoPriorityClass priorityClass, int priority);

    /**
     *  Returns true if ionice is supported on this system
     */
    bool supportsIoNiceness();

    /**
     *  Return the internal pointer of all the processes.  The order of the processes
     *  is guaranteed to never change.  Call updateAllProcesses() first to actually
     *  update the information.
     */
    const QList<Process *> &getAllProcesses() const;

    /**
     *  Return the number of processes.  Call updateAllProcesses() to actually
     *  update the information.
     *
     *  This is equivalent to getAllProcesses().count()
     */
    int processCount() const;

    /**
     *  Return the total amount of physical memory in KB.  This is fast (just a system call)
     *  Returns 0 on error
     */
    long long totalPhysicalMemory();

    /*
     * The total amount of swap memory in KB.
     *
     * Returns 0 on error.
     */
    long long totalSwapMemory();

    /**
     *  Return the number of processor cores enabled.
     *  (A system can disable processors.  Disabled processors are not counted here).
     *  This is fast (just a system call) */
    long numberProcessorCores();

    /** Update/add process for given pid immediately */
    bool updateOrAddProcess(long pid);

public Q_SLOTS:
    /** The abstract processes has updated its list of processes */
    void processesUpdated();
    void processUpdated(long pid, const Process::Updates &changes);

Q_SIGNALS:
    /** The data for a process has changed.
     *  if @p onlyTotalCpu is set, only the total cpu usage has been updated.
     *  process->changes  contains a bit field indicating what has changed since the last time this was emitted
     *  for this process
     */
    void processChanged(KSysGuard::Process *process, bool onlyTotalCpu);

    /**
     *  This indicates we are about to add a process in the model.
     *  The process already has the pid, ppid and tree_parent set up.
     */
    void beginAddProcess(KSysGuard::Process *process);

    /**
     *  We have finished inserting a process
     */
    void endAddProcess();
    /**
     *  This indicates we are about to remove a process in the model.  Emit the appropriate signals
     */

    void beginRemoveProcess(KSysGuard::Process *process);

    /**
     *  We have finished removing a process
     */
    void endRemoveProcess();

    /**
     *  This indicates we are about move a process from one parent to another.
     */
    void beginMoveProcess(KSysGuard::Process *process, KSysGuard::Process *new_parent);

    /**
     *  We have finished moving the process
     */
    void endMoveProcess();

    void updated();

protected:
    class Private;
    Private *d;

private:
    inline void deleteProcess(long pid);
    bool updateProcess(Process *process, long ppid);
    bool updateProcessInfo(Process *ps);
    bool addProcess(long pid, long ppid);
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Processes::UpdateFlags)
}

#endif