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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "Job.h"
#include "Debug.h"
#include "File.h"
#include <QFile>
#include <unistd.h>
//____________________________________________________________
Job::Job( long pid, long ppid ):
Counter( "Job" ),
mask_( 0 ),
id_( 0 ),
parentId_( 0 ),
treeParentId_( 0 ),
userId_( 0 ),
cpu_( 0 ),
memory_( 0 ),
priority_( 0 ),
nice_( 0 ),
virtualMemory_(0),
residentMemory_(0),
sharedMemory_(0),
userTime_(0),
systemTime_(0),
elapsedTime_(0),
threads_(0),
iconInitialized_(false)
{
if( pid ) setId( pid );
if( ppid ) setParentId( ppid );
}
//____________________________________________________________
Job::Job( void ):
Counter( "Job" ),
mask_( 0 ),
id_( 0 ),
parentId_( 0 ),
cpu_( 0 ),
memory_( 0 ),
priority_( 0 ),
nice_( 0 ),
virtualMemory_(0),
residentMemory_(0),
sharedMemory_(0),
userTime_(0),
systemTime_(0),
elapsedTime_(0),
threads_(0),
iconInitialized_(false)
{}
//____________________________________________________________
void Job::updateFrom( const Job& other )
{
// check id
Q_ASSERT( id_ == other.id_ );
// update based on mask
if( other.mask_ & ParentId ) setParentId( other.parentId_ );
if( other.mask_ & TreeParentId ) setParentId( other.treeParentId_ );
if( other.mask_ & Name ) setName( other.name_ );
if( other.mask_ & LongName ) setLongName( other.longName_ );
if( other.mask_ & Command ) setCommand( other.command_ );
if( other.mask_ & User ) setUser( other.user_ );
if( other.mask_ & UserId ) setUserId( other.userId_ );
if( other.mask_ & State ) setState( other.state_ );
if( other.mask_ & Cpu ) setCpu( other.cpu_ );
if( other.mask_ & Memory ) setMemory( other.memory_ );
if( other.mask_ & Priority ) setPriority( other.priority_ );
if( other.mask_ & Nice ) setNice( other.nice_ );
if( other.mask_ & VirtualMemory ) setVirtualMemory( other.virtualMemory_ );
if( other.mask_ & ResidentMemory ) setResidentMemory( other.residentMemory_ );
if( other.mask_ & SharedMemory ) setSharedMemory( other.sharedMemory_ );
// update cpu
if( (other.mask_ & UserTime) && (other.mask_ & SystemTime) && (other.mask_ & ElapsedTime) && other.elapsedTime_ > elapsedTime_ )
{
setCpu( 100*(other.cpuTime() - cpuTime() )/(other.elapsedTime_ - elapsedTime_ ) );
} else if( other.mask_ & Cpu ) setCpu( other.cpu_ );
if( other.mask_ & UserTime ) setUserTime( other.userTime_ );
if( other.mask_ & SystemTime ) setSystemTime( other.systemTime_ );
if( other.mask_ & ElapsedTime ) setElapsedTime( other.elapsedTime_ );
if( other.mask_ & StartTime ) setStartTime( other.startTime_ );
if( other.mask_ & Threads ) setThreads( other.threads_ );
if( other.mask_ & Icon ) setIcon( other.icon_ );
}
//____________________________________________________________
void Job::readFromCommandLine( void )
{
// check if not already set
if( !longName_.isEmpty() ) return;
QFile file( QString( "/proc/%1/cmdline" ).arg( id_ ) );
if( !file.open(QIODevice::ReadOnly) )
{
setLongName( name_ );
return;
}
QTextStream in(&file);
QString commandLine( in.readAll() );
//cmdline separates parameters with the NULL character
if( !commandLine.isEmpty() )
{
//extract non-truncated name from cmdline
const int zeroIndex = commandLine.indexOf(QChar('\0'));
int processNameStart = commandLine.lastIndexOf(QChar('/'), zeroIndex);
if(processNameStart == -1) processNameStart = 0;
else processNameStart++;
const QString nameFromCmdLine = commandLine.mid(processNameStart, zeroIndex - processNameStart);
if( nameFromCmdLine.startsWith( name_ ) ) setLongName( nameFromCmdLine );
else setLongName( name_ );
// assign command line
commandLine.replace('\0', ' ');
setCommand( commandLine );
} else setLongName( name_ );
file.close();
return;
}
//____________________________________________________________
bool Job::readFromStatus( void )
{
QFile file( QString( "/proc/%1/status" ).arg( id_ ) );
if( !file.open(QIODevice::ReadOnly) ) return false;
// QTextStream in(&file);
QString buffer( file.readAll() );
QTextStream in( &buffer );
while( !in.atEnd() )
{
// read one line and split
const QString line( in.readLine() );
if( line.startsWith( "Uid:" ) )
{
// parent id
const QStringList values( line.split( QRegExp( "\\s+" ), QString::SkipEmptyParts ) );
if( values.size() >= 2 ) setUserId( values[1].toLongLong() );
// stop here
break;
}
}
return true;
}
//____________________________________________________________
bool Job::readFromStat( void )
{
QFile file( QString( "/proc/%1/stat" ).arg( id_ ) );
if( !file.open(QIODevice::ReadOnly) ) return false;
QTextStream in(&file);
const QString data( in.readAll() );
const QStringList dataList( data.split( " ", QString::SkipEmptyParts ) );
if( dataList.size() < 24 ) return false;
// check pid
if( dataList[0].toLong() != id_ ) return false;
// name
setName( dataList[1].mid(1, dataList[1].size()-2) );
setState( dataList[2].at(0) );
setParentId( dataList[3].toLong() );
setUserTime( 10*dataList[13].toLongLong() );
setSystemTime( 10*dataList[14].toLongLong() );
setPriority( dataList[17].toInt() );
setNice( dataList[18].toInt() );
setThreads( dataList[19].toInt() );
return true;
}
//____________________________________________________________
bool Job::readFromStatM( void )
{
QFile file( QString( "/proc/%1/statm" ).arg( id_ ) );
if( !file.open(QIODevice::ReadOnly) ) return false;
QTextStream in(&file);
const QString data( in.readAll() );
const QStringList dataList( data.split( " ", QString::SkipEmptyParts ) );
if( dataList.size() < 3 ) return false;
setVirtualMemory( dataList[0].toLongLong() * sysconf(_SC_PAGESIZE) / 1024 );
setResidentMemory( dataList[1].toLongLong() * sysconf(_SC_PAGESIZE) / 1024 );
setSharedMemory( dataList[2].toLongLong() * sysconf(_SC_PAGESIZE) / 1024 );
return true;
}
//____________________________________________________________
bool Job::readStartTime( void )
{
const File file( QString( "/proc/%1" ).arg( id_ ) );
if( !file.exists() ) return false;
setStartTime( TimeStamp( file.created() ) );
return true;
}
//____________________________________________________________
QString Job::_timeString( int time )
{
const int hours( time/(3600*1000) );
const int minutes( time/(60*1000) - hours*60 );
const int seconds( (time/1000) % 60 );
int msec( time % 1000 );
QString timeString;
if( minutes > 0 || hours > 0 )
{
if( hours > 0 ) timeString = QString( QObject::tr( "%1h " ) ).arg( hours );
if( minutes > 0 ) timeString += QString( QObject::tr( "%1m " ) ).arg( minutes );
if( seconds > 0 ) timeString += QString( QObject::tr( "%1s" ) ).arg( seconds );
} else if( msec > 0 ) {
msec /= 10;
if( msec > 9 ) timeString = QString( QObject::tr( "%1.%2s" ) ).arg( seconds ).arg( msec );
else timeString = QString( QObject::tr( "%1.0%2s" ) ).arg( seconds ).arg( msec );
} else timeString = QString( QObject::tr( "%1s" ) ).arg( seconds );
return timeString;
}
|