* GSWeb.framework/GSWProcFS.h/m: Make class more portable.

Replace all system dependent types with standard types.
        Replace usage of NSDebugFLog with NSDebugMLog in methods.
        ([GSWProcFSProcInfo filledProcInfo]): Use -processIdentifier
        instead of getpid.
        ([GSWProcFSProcInfo filledProcInfoWithPID:]): Update
        types. Use standard GC macro.
        ([GSWProcFSProcInfo initFilledWithPID:]): Update types.
        ([GSWProcFSProcInfo description]): Use NSPageSize instead of
        getpagesize.  Remove obsolete casts.
        ([GSWProcFSProcInfo contentOfProcFile:]): Use standard
        NSString API to read the file.
        ([GSWProcFSProcInfo contentOfPIDFile:]): Adapt to new types
        and reformat.
        ([GSWProcFSProcInfo residentMemory]),
        ([GSWProcFSProcInfo sharedMemory]): Adapt to new types.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@19366 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
ayers 2004-05-18 09:11:13 +00:00
parent 6cc2ee18c5
commit cd1944a9e5
3 changed files with 80 additions and 75 deletions

View file

@ -1,3 +1,22 @@
2004-05-18 David Ayers <d.ayers@inode.at>
* GSWeb.framework/GSWProcFS.h/m: Make class more portable.
Replace all system dependent types with standard types. Replace
usage of NSDebugFLog with NSDebugMLog in methods.
([GSWProcFSProcInfo filledProcInfo]): Use -processIdentifier
instead of getpid.
([GSWProcFSProcInfo filledProcInfoWithPID:]): Update types. Use
standard GC macro.
([GSWProcFSProcInfo initFilledWithPID:]): Update types.
([GSWProcFSProcInfo description]): Use NSPageSize instead of
getpagesize. Remove obsolete casts.
([GSWProcFSProcInfo contentOfProcFile:]): Use standard NSString
API to read the file.
([GSWProcFSProcInfo contentOfPIDFile:]): Adapt to new types and
reformat.
([GSWProcFSProcInfo residentMemory]),
([GSWProcFSProcInfo sharedMemory]): Adapt to new types.
2004-05-16 David Wetzel <dave@turbocat.de> 2004-05-16 David Wetzel <dave@turbocat.de>
* GSWeb.framework/GSWApplication.m ([GSWApplication * GSWeb.framework/GSWApplication.m ([GSWApplication

View file

@ -44,13 +44,13 @@ typedef enum _GSWProcState
NSString* _ttyc; // string representation of controlling tty device [/proc/#/stat] NSString* _ttyc; // string representation of controlling tty device [/proc/#/stat]
NSArray* _environ; // environment string vector (/proc/#/environ) NSArray* _environ; // environment string vector (/proc/#/environ)
NSArray* _commandLine; // command line string vector (/proc/#/cmdline) NSArray* _commandLine; // command line string vector (/proc/#/cmdline)
uid_t _uid; // user id int _uid; // user id
pid_t _pid; // process id [/proc/#/stat] int _pid; // process id [/proc/#/stat]
pid_t _ppid; // pid of parent process [/proc/#/stat] int _ppid; // pid of parent process [/proc/#/stat]
pid_t _pgrp; // process group id [/proc/#/stat] int _pgrp; // process group id [/proc/#/stat]
int _session; // session id [/proc/#/stat] int _session; // session id [/proc/#/stat]
int _tty; // full device number of controlling terminal [/proc/#/stat] int _tty; // full device number of controlling terminal [/proc/#/stat]
pid_t _tpgid; // terminal process group id [/proc/#/stat] int _tpgid; // terminal process group id [/proc/#/stat]
int _priority; // kernel scheduling priority [/proc/#/stat] int _priority; // kernel scheduling priority [/proc/#/stat]
int _nice; // standard unix nice level of process [/proc/#/stat] int _nice; // standard unix nice level of process [/proc/#/stat]
long long _signal; // mask of pending signals [/proc/#/stat] long long _signal; // mask of pending signals [/proc/#/stat]
@ -89,8 +89,8 @@ typedef enum _GSWProcState
}; };
+(GSWProcFSProcInfo*)filledProcInfo; +(GSWProcFSProcInfo*)filledProcInfo;
+(GSWProcFSProcInfo*)filledProcInfoWithPID:(pid_t)pid_; +(GSWProcFSProcInfo*)filledProcInfoWithPID: (int)processID;
-(id)initFilledWithPID:(pid_t)pid_; -(id)initFilledWithPID: (int)processID;
-(void)dealloc; -(void)dealloc;
-(BOOL)fill; -(BOOL)fill;
+(NSString*)contentOfProcFile:(NSString*)procFile; +(NSString*)contentOfProcFile:(NSString*)procFile;

View file

@ -37,43 +37,46 @@
RCS_ID("$Id$") RCS_ID("$Id$")
#include "GSWeb.h" #include "GSWeb.h"
#include <unistd.h>
#include <sys/time.h>
NSString* formattedByteSizeValue(unsigned int value) NSString* formattedByteSizeValue(unsigned int value)
{ {
if (value<1024) if (value<1024)
return [NSString stringWithFormat:@"%lu b",value]; return [NSString stringWithFormat:@"%lu b",value];
else if (value<1024L*1024L) else if (value<1024L*1024L)
return [NSString stringWithFormat:@"%.3f Kb",(((double)value)/1024)]; return [NSString stringWithFormat:@"%.3f Kb",
(((double)value)/1024)];
else if (value<1024L*1024L*1024L) else if (value<1024L*1024L*1024L)
return [NSString stringWithFormat:@"%.3f Mb",(((double)value)/(1024L*1024L))]; return [NSString stringWithFormat:@"%.3f Mb",
(((double)value)/(1024L*1024L))];
else else
return [NSString stringWithFormat:@"%.3f Gb",(((double)value)/(1024L*1024L*1024L))]; return [NSString stringWithFormat:@"%.3f Gb",
(((double)value)/(1024L*1024L*1024L))];
}; };
@implementation GSWProcFSProcInfo @implementation GSWProcFSProcInfo
+(GSWProcFSProcInfo*)filledProcInfo + (GSWProcFSProcInfo *)filledProcInfo
{ {
return [self filledProcInfoWithPID:getpid()]; int processID = [[NSProcessInfo processInfo] processIdentifier];
}; return [self filledProcInfoWithPID: processID];
}
+(GSWProcFSProcInfo*)filledProcInfoWithPID:(pid_t)pid_ + (GSWProcFSProcInfo *)filledProcInfoWithPID: (int)processID
{ {
GSWProcFSProcInfo* obj=[[[self alloc] initFilledWithPID:pid_]autorelease]; GSWProcFSProcInfo *obj
= AUTORELEASE([[self alloc] initFilledWithPID: processID]);
return obj; return obj;
}; }
-(id)initFilledWithPID:(pid_t)pid - (id)initFilledWithPID: (int)processID
{ {
if ((self=[super init])) if ((self = [super init]))
{ {
_pid=pid; _pid = processID;
[self fill]; [self fill];
}; };
return self; return self;
}; }
-(void)dealloc -(void)dealloc
{ {
@ -88,7 +91,7 @@ NSString* formattedByteSizeValue(unsigned int value)
-(NSString*)description -(NSString*)description
{ {
NSString* dscr=nil; NSString* dscr=nil;
size_t pageSize=getpagesize(); unsigned int pageSize=NSPageSize();
char* stateCString=NULL; char* stateCString=NULL;
switch(_state) switch(_state)
{ {
@ -120,13 +123,13 @@ NSString* formattedByteSizeValue(unsigned int value)
_environ, _environ,
_commandLine]; _commandLine];
dscr=[dscr stringByAppendingFormat:@"uid: %d\npid: %d\nppid: %d\npgrp: %d\nsession: %d\ntty: %d\ntpgid: %d\npriority: %d\nnice: %d\nsignal: %LX\nblocked: %LX\nsigIgnore: %LX\nsigCache: %LX\n", dscr=[dscr stringByAppendingFormat:@"uid: %d\npid: %d\nppid: %d\npgrp: %d\nsession: %d\ntty: %d\ntpgid: %d\npriority: %d\nnice: %d\nsignal: %LX\nblocked: %LX\nsigIgnore: %LX\nsigCache: %LX\n",
(int)_uid, _uid,
(int)_pid, _pid,
(int)_ppid, _ppid,
(int)_pgrp, _pgrp,
_session, _session,
_tty, _tty,
(int)_tpgid, _tpgid,
_priority, _priority,
_nice, _nice,
_signal, _signal,
@ -179,49 +182,31 @@ NSString* formattedByteSizeValue(unsigned int value)
return YES; return YES;
}; };
+(NSString*)contentOfProcFile:(NSString*)procFile + (NSString *)contentOfProcFile: (NSString *)procFile
{ {
NSString* content=nil; NSString *path;
char thePath[BUFSIZ*2]; NSString *content;
FILE *theFile = NULL;
NSString* path=[NSString stringWithFormat:@"/proc/%@",procFile]; path = [NSString stringWithFormat: @"/proc/%@", procFile];
if ([path getFileSystemRepresentation:thePath content = [NSString stringWithContentsOfFile: path];
maxLength:sizeof(thePath)-1] == NO)
if ([content length] == 0)
{ {
LOGSeriousError(@"Open (%@) attempt failed - bad path", LOGSeriousError(@"Read (%@) attempt failed", path);
path);
} }
else
{
theFile = fopen(thePath, "r");
if (theFile == NULL) // We failed to open the file.
{
LOGSeriousError(@"Open (%s) attempt failed - %s", thePath, strerror(errno));
}
else
{
char buff[1024]="";
if (!fgets(buff,1024,theFile))
{
LOGSeriousError(@"Read (%s) attempt failed",thePath);
}
else
{
content=[NSString stringWithCString:buff];
NSDebugMLog(@"content=%@",content);
};
fclose(theFile);
};
};
return content; return content;
}
};
-(NSString*)contentOfPIDFile:(NSString*)pidFile -(NSString*)contentOfPIDFile:(NSString*)pidFile
{ {
NSString* content=nil; NSString* content=nil;
NSString* path=[NSString stringWithFormat:@"%d/%@",(int)(_pid ? _pid : getpid()),pidFile]; NSString* path
content=[[self class] contentOfProcFile:path]; = [NSString stringWithFormat:@"%d/%@",
(_pid ? _pid
: [[NSProcessInfo processInfo] processIdentifier]),
pidFile];
content=[[self class] contentOfProcFile: path];
return content; return content;
}; };
@ -229,11 +214,11 @@ NSString* formattedByteSizeValue(unsigned int value)
{ {
BOOL ok=NO; BOOL ok=NO;
NSString* pidstat=[self contentOfPIDFile:@"statm"]; NSString* pidstat=[self contentOfPIDFile:@"statm"];
NSDebugFLog(@"pidstat=%@",pidstat); NSDebugMLog(@"pidstat=%@",pidstat);
if (pidstat) if (pidstat)
{ {
const char* statsChars=[pidstat cString]; const char* statsChars=[pidstat cString];
NSDebugFLog(@"pidstat=%@",pidstat); NSDebugMLog(@"pidstat=%@",pidstat);
if (sscanf(statsChars, "%ld %ld %ld %ld %ld %ld %ld", if (sscanf(statsChars, "%ld %ld %ld %ld %ld %ld %ld",
&_pagesNb,//size &_pagesNb,//size
&_residentPagesNb,//resident &_residentPagesNb,//resident
@ -251,15 +236,16 @@ NSString* formattedByteSizeValue(unsigned int value)
-(BOOL)fillStat -(BOOL)fillStat
{ {
BOOL ok=NO; BOOL ok=NO;
NSString* pidstat=[self contentOfPIDFile:@"stat"]; NSString* pidstat = [self contentOfPIDFile: @"stat"];
NSDebugFLog(@"pidstat=%@",pidstat);
NSDebugMLog(@"pidstat=%@",pidstat);
if (pidstat) if (pidstat)
{ {
NSRange cmdEnd=[pidstat rangeOfString:@") "]; NSRange cmdEnd=[pidstat rangeOfString:@") "];
if (cmdEnd.length>0) if (cmdEnd.length>0)
{ {
NSString* pid_cmd=[pidstat substringToIndex:cmdEnd.location]; NSString* pid_cmd=[pidstat substringToIndex:cmdEnd.location];
NSDebugFLog(@"pid_cmd=%@",pid_cmd); NSDebugMLog(@"pid_cmd=%@",pid_cmd);
if (cmdEnd.location+cmdEnd.length<[pidstat length]) if (cmdEnd.location+cmdEnd.length<[pidstat length])
{ {
NSString* stats=[pidstat substringFromIndex:cmdEnd.location+cmdEnd.length]; NSString* stats=[pidstat substringFromIndex:cmdEnd.location+cmdEnd.length];
@ -278,7 +264,7 @@ NSString* formattedByteSizeValue(unsigned int value)
long cstime; long cstime;
long startTime; long startTime;
NSDebugFLog(@"stats=%@",stats); NSDebugMLog(@"stats=%@",stats);
if (sscanf(statsChars, if (sscanf(statsChars,
"%c %d %d %d %d %d %lu %lu %lu %lu %lu %ld %ld %ld %ld %d " "%c %d %d %d %d %d %lu %lu %lu %lu %lu %ld %ld %ld %ld %d "
"%d %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %LX %LX %LX %LX %lu", "%d %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %LX %LX %LX %LX %lu",
@ -355,7 +341,7 @@ NSString* formattedByteSizeValue(unsigned int value)
if (linux_version_code < LINUX_VERSION(1,1,30) && P->tty != -1) if (linux_version_code < LINUX_VERSION(1,1,30) && P->tty != -1)
P->tty = 4*0x100 + P->tty; // when tty wasn't full devno P->tty = 4*0x100 + P->tty; // when tty wasn't full devno
*/ */
NSDebugFLog(@"residentMemorySize=%lu",_residentMemorySize); NSDebugMLog(@"residentMemorySize=%lu",_residentMemorySize);
}; };
}; };
}; };
@ -365,13 +351,13 @@ NSString* formattedByteSizeValue(unsigned int value)
-(unsigned int)residentMemory -(unsigned int)residentMemory
{ {
size_t pageSize=getpagesize(); unsigned int pageSize=NSPageSize();
return (unsigned int)(_residentPagesNb*pageSize); return (unsigned int)(_residentPagesNb*pageSize);
}; };
-(unsigned int)sharedMemory -(unsigned int)sharedMemory
{ {
size_t pageSize=getpagesize(); unsigned int pageSize=NSPageSize();
return (unsigned int)(_sharedPagesNb*pageSize); return (unsigned int)(_sharedPagesNb*pageSize);
}; };