add more spacing
[personal-kdebase.git] / workspace / libs / ksysguard / processcore / processes_netbsd_p.cpp
blob7996f03375ab8f0a1dcce8e19a619e24ec1026e9
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Manolo Valdes <nolis71cu@gmail.com>
3 Copyright (C) 2007 Mark Davies <mark@mcs.vuw.ac.nz>
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.
21 #include "processes_local_p.h"
22 #include "process.h"
24 #include <klocale.h>
26 #include <QSet>
28 #include <kvm.h>
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 #include <sys/types.h>
32 #include <sys/user.h>
33 #include <sys/stat.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <sched.h>
41 namespace KSysGuard
44 class ProcessesLocal::Private
46 public:
47 Private() { kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");}
48 ~Private() { kvm_close(kd);}
49 inline bool readProc(long pid, struct kinfo_proc2 **p, int *num);
50 inline void readProcStatus(struct kinfo_proc2 *p, Process *process);
51 inline void readProcStat(struct kinfo_proc2 *p, Process *process);
52 inline void readProcStatm(struct kinfo_proc2 *p, Process *process);
53 inline bool readProcCmdline(struct kinfo_proc2 *p, Process *process);
55 kvm_t *kd;
58 #ifndef _SC_NPROCESSORS_ONLN
59 long int KSysGuard::ProcessesLocal::numberProcessorCores()
61 int mib[2];
62 int ncpu;
63 size_t len;
65 mib[0] = CTL_HW;
66 mib[1] = HW_NCPU;
67 len = sizeof(ncpu);
69 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1 || !len)
70 return 1;
71 return len;
73 #endif
75 bool ProcessesLocal::Private::readProc(long pid, struct kinfo_proc2 **p, int *num)
77 int len;
78 int op, arg;
80 if (pid == 0) {
81 op = KERN_PROC_ALL;
82 arg = 0;
83 } else {
84 op = KERN_PROC_PID;
85 arg = pid;
87 *p = kvm_getproc2(kd, op, arg, sizeof(struct kinfo_proc2), &len);
89 if (len < 1)
90 return false;
92 if (num != NULL)
93 *num = len;
94 return true;
97 void ProcessesLocal::Private::readProcStatus(struct kinfo_proc2 *p, Process *process)
99 process->setUid(p->p_ruid);
100 process->setEuid(p->p_uid);
101 process->setGid(p->p_rgid);
102 process->setEgid(p->p_gid);
103 process->setTracerpid(0);
105 process->setName(QString(p->p_comm ? p->p_comm : "????"));
108 void ProcessesLocal::Private::readProcStat(struct kinfo_proc2 *p, Process *ps)
110 const char *ttname;
111 dev_t dev;
113 ps->setUserTime(p->p_uutime_sec*100+p->p_uutime_usec/10000);
114 ps->setSysTime(p->p_ustime_sec*100+p->p_ustime_usec/10000);
116 ps->setUserUsage(100.0 * ((double)(p->p_pctcpu) / FSCALE));
117 ps->setSysUsage(0);
119 ps->setNiceLevel(p->p_nice - NZERO);
120 ps->setVmSize((p->p_vm_tsize + p->p_vm_dsize + p->p_vm_ssize)
121 * getpagesize());
122 ps->setVmRSS(p->p_vm_rssize * getpagesize());
124 // "idle","run","sleep","stop","zombie"
125 switch( p->p_stat ) {
126 case LSRUN:
127 ps->setStatus(Process::Running);
128 break;
129 case LSSLEEP:
130 ps->setStatus(Process::Sleeping);
131 break;
132 case LSSTOP:
133 ps->setStatus(Process::Stopped);
134 break;
135 case LSZOMB:
136 ps->setStatus(Process::Zombie);
137 break;
138 case LSONPROC:
139 ps->setStatus(Process::Running);
140 break;
141 default:
142 ps->setStatus(Process::OtherStatus);
143 break;
146 dev = p->p_tdev;
147 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
148 ps->setTty(QByteArray());
149 } else {
150 ps->setTty(QByteArray(ttname));
154 void ProcessesLocal::Private::readProcStatm(struct kinfo_proc2 *p, Process *process)
156 // TODO
158 // unsigned long shared;
159 // process->vmURSS = process->vmRSS - (shared * sysconf(_SC_PAGESIZE) / 1024);
160 process->setVmURSS(-1);
163 bool ProcessesLocal::Private::readProcCmdline(struct kinfo_proc2 *p, Process *process)
165 char **argv;
167 if ((argv = kvm_getargv2(kd, p, 256)) == NULL)
168 return false;
170 QString command = QString("");
172 while (*argv) {
173 command += *argv;
174 command += " ";
175 argv++;
177 process->setCommand(command.trimmed());
179 return true;
182 ProcessesLocal::ProcessesLocal() : d(new Private())
187 long ProcessesLocal::getParentPid(long pid) {
188 long long ppid = 0;
189 struct kinfo_proc2 *p;
190 if(d->readProc(pid, &p, 0))
192 ppid = p->p_ppid;
194 return ppid;
197 bool ProcessesLocal::updateProcessInfo( long pid, Process *process)
199 struct kinfo_proc2 *p;
200 if(!d->readProc(pid, &p, NULL)) return false;
201 d->readProcStat(p, process);
202 d->readProcStatus(p, process);
203 d->readProcStatm(p, process);
204 if(!d->readProcCmdline(p, process)) return false;
206 return true;
209 QSet<long> ProcessesLocal::getAllPids( )
211 QSet<long> pids;
212 int len;
213 int num;
214 struct kinfo_proc2 *p;
216 d->readProc(0, &p, &len);
218 for (num = 0; num < len; num++)
220 long pid = p[num].p_pid;
221 long long ppid = p[num].p_ppid;
223 //skip all process with parent id = 0 but init
224 if(ppid == 0 && pid != 1)
225 continue;
226 pids.insert(pid);
228 return pids;
231 bool ProcessesLocal::sendSignal(long pid, int sig) {
232 if ( kill( (pid_t)pid, sig ) ) {
233 //Kill failed
234 return false;
236 return true;
239 bool ProcessesLocal::setNiceness(long pid, int priority) {
240 if ( setpriority( PRIO_PROCESS, pid, priority ) ) {
241 //set niceness failed
242 return false;
244 return true;
247 bool ProcessesLocal::setScheduler(long pid, int priorityClass, int priority)
249 if(priorityClass == KSysGuard::Process::Other || priorityClass == KSysGuard::Process::Batch)
250 priority = 0;
251 if(pid <= 0) return false; // check the parameters
252 struct sched_param params;
253 params.sched_priority = priority;
254 switch(priorityClass) {
255 case (KSysGuard::Process::Other):
256 return (sched_setscheduler( pid, SCHED_OTHER, &params) == 0);
257 case (KSysGuard::Process::RoundRobin):
258 return (sched_setscheduler( pid, SCHED_RR, &params) == 0);
259 case (KSysGuard::Process::Fifo):
260 return (sched_setscheduler( pid, SCHED_FIFO, &params) == 0);
261 #ifdef SCHED_BATCH
262 case (KSysGuard::Process::Batch):
263 return (sched_setscheduler( pid, SCHED_BATCH, &params) == 0);
264 #endif
265 default:
266 return false;
270 bool ProcessesLocal::setIoNiceness(long pid, int priorityClass, int priority) {
271 return false; //Not yet supported
274 bool ProcessesLocal::supportsIoNiceness() {
275 return false;
278 long long ProcessesLocal::totalPhysicalMemory() {
280 size_t Total;
281 size_t len;
282 len = sizeof (Total);
283 sysctlbyname("hw.physmem", &Total, &len, NULL, 0);
284 return Total /= 1024;
287 ProcessesLocal::~ProcessesLocal()
289 delete d;