1 /* This file is part of the KDE project
3 Copyright (C) 2007 John Tapsell <tapsell@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
25 #include <kdemacros.h>
28 #include <QtCore/QHash>
32 class AbstractProcesses
;
34 * This class retrieves the processes currently running in an OS independent way.
36 * To use, do something like:
39 * #include <ksysguard/processes.h>
40 * #include <ksysguard/process.h>
42 * KSysGuard::Processes *processes = KSysGuard::Processes::getInstance();
43 * QHash<long, Process *> processlist = processes->getProcesses();
44 * foreach( Process * process, processlist) {
45 * kDebug() << "Process with pid " << process->pid << " is called " << process->name;
47 * KSysGuard::Processes::returnInstance(processes);
51 * @author John Tapsell <tapsell@kde.org>
53 class KDE_EXPORT Processes
: public QObject
59 * Singleton pattern to return the instance associated with @p host.
60 * Leave as the default for the current machine
62 static Processes
*getInstance(const QString
&host
= QString());
64 * Call when you are finished with the Processes pointer from getInstance.
65 * The pointer from getInstance may not be valid after calling this.
66 * This is reference counted - once all the instances are returned, the object is deleted
68 static void returnInstance(const QString
&host
= QString());
71 * Update all the process information. After calling this, /proc or equivalent is scanned and
72 * the signals processChanged, etc are emitted.
74 * Set updateDuration to whatever time period that you update, in milliseconds.
75 * For example, if you update every 2000ms, set this to 2000. That way it won't update
76 * more often than needed
78 void updateAllProcesses(long updateDurationMS
= 0);
80 * Return information for one specific process. call getProcess(0) to get the
81 * fake process used as the top most parent for all processes.
82 * This doesn't fetch any new information and so returns almost instantly.
83 * Call updateAllProcesses() to actually fetch the process information.
85 Process
*getProcess(long pid
) const;
88 * Kill the specified process. You may not have the privillage to kill the process.
89 * The process may also chose to ignore the command. Send the SIGKILL signal to kill
90 * the process immediately. You may lose any unsaved data.
92 * @returns Successful or not in killing the process
94 bool killProcess(long pid
);
97 * Send the specified named POSIX signal to the process given.
99 * For example, to indicate for process 324 to STOP do:
101 * #include <signals.h>
104 * KSysGuard::Processes::sendSignal(23, SIGSTOP);
108 bool sendSignal(long pid
, int sig
);
111 * Set the priority for a process. This is from 19 (very nice, lowest priority) to
112 * -20 (highest priority). The default value for a process is 0.
114 * @return false if you do not have permission to set the priority
116 bool setNiceness(long pid
, int priority
);
119 * Set the scheduler for a process. This is defined according to POSIX.1-2001
120 * See "man sched_setscheduler" for more information.
122 * @p priorityClass One of SCHED_FIFO, SCHED_RR, SCHED_OTHER, and SCHED_BATCH
123 * @p priority Set to 0 for SCHED_OTHER and SCHED_BATCH. Between 1 and 99 for SCHED_FIFO and SCHED_RR
124 * @return false if you do not have permission to set the priority
126 bool setScheduler(long pid
, KSysGuard::Process::Scheduler priorityClass
, int priority
);
129 * Set the io priority for a process. This is from 7 (very nice, lowest io priority) to
130 * 0 (highest priority). The default value is determined as: io_nice = (cpu_nice + 20) / 5.
132 * @return false if you do not have permission to set the priority
134 bool setIoNiceness(long pid
, KSysGuard::Process::IoPriorityClass priorityClass
, int priority
);
137 * Returns true if ionice is supported on this system
139 bool supportsIoNiceness();
142 * Return the internal pointer of all the processes. The order of the processes
143 * is guaranteed to never change. Call updateAllProcesses first to actually
144 * update the information.
146 QList
< Process
*> getAllProcesses() const;
149 * Return the total amount of physical memory in KB. This is fast (just a system call)
152 long long totalPhysicalMemory();
155 * Return the number of processor cores enabled.
156 * (A system can disable procesors. Disabled processors are not counted here).
157 * This is fast (just a system call) */
158 long numberProcessorCores();
161 /** The abstract processes has updated its list of processes */
162 void processesUpdated();
165 /** The data for a process has changed.
166 * if @p onlyTotalCpu is set, only the total cpu usage has been updated.
167 * process->changes contains a bit field indicating what has changed since the last time this was emitted
170 void processChanged( KSysGuard::Process
*process
, bool onlyTotalCpu
);
172 * This indicates we are about to add a process in the model.
173 * The process already has the pid, ppid and tree_parent set up.
175 void beginAddProcess( KSysGuard::Process
*process
);
177 * We have finished inserting a process
179 void endAddProcess();
181 * This indicates we are about to remove a process in the model. Emit the appropriate signals
183 void beginRemoveProcess( KSysGuard::Process
*process
);
185 * We have finished removing a process
187 void endRemoveProcess();
189 * This indicates we are about move a process from one parent to another.
191 void beginMoveProcess(KSysGuard::Process
*process
, KSysGuard::Process
*new_parent
);
193 * We have finished moving the process
195 void endMoveProcess();
197 Processes(AbstractProcesses
*abstractProcesses
);
202 static StaticPrivate
*d2
;
204 bool updateOrAddProcess( long pid
);
205 inline void deleteProcess(long pid
);
206 bool updateProcess( Process
*process
, long ppid
, bool onlyReparent
= false);
207 bool addProcess(long pid
, long ppid
);
211 /** For a remote machine, we rely on being able to communicate with ksysguardd.
212 * This must be dealt with by the program including this widget. It must listen to our
213 * 'runCommand' signal, and run the given command, with the given id. */
214 void runCommand(const QString
&command
, int id
);
217 /** For a remote machine, we rely on being able to communicate with ksysguardd.
218 * The programming using this must call this slot when an answer is received from ksysguardd,
219 * in response to a runCommand request. The id identifies the answer */
220 void answerReceived( int id
, const QList
<QByteArray
>& answer
);