00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <Basics.H>
00027 #include "OS.H"
00028
00029 #if defined(__digital__)
00030 #include <mach/machine/vm_param.h>
00031 #include <fcntl.h>
00032 #include <sys/stat.h>
00033 #include <sys/procfs.h>
00034 #elif defined(__linux__)
00035 #include <asm/page.h>
00036 #endif
00037
00038 using std::ifstream;
00039
00040 static Basics::mutex inet_ntoa_lock;
00041
00042 Text inet_ntoa_r(in_addr &in)
00043 {
00044 Text t;
00045 inet_ntoa_lock.lock();
00046 t = inet_ntoa(in);
00047 inet_ntoa_lock.unlock();
00048 return t;
00049 }
00050
00051 void OS::GetProcessSize( unsigned long &total,
00052 unsigned long &resident) throw()
00053 {
00054
00055 total = 0;
00056 resident = 0;
00057 #if defined(__digital__)
00058 pid_t pid = getpid();
00059 char procname[20];
00060 sprintf(procname, "/proc/%05d", pid);
00061 int fd = open(procname, O_RDONLY, 0 );
00062 if (fd >= 0) {
00063 struct prpsinfo psinfo;
00064 int ioctl_res = ioctl(fd, PIOCPSINFO, &psinfo);
00065 if (ioctl_res >= 0) {
00066 total = (int)(PAGE_SIZE * psinfo.pr_size);
00067 resident = (int)(PAGE_SIZE * psinfo.pr_rssize);
00068 }
00069 (void)close(fd);
00070 }
00071 #elif defined(__linux__)
00072 pid_t pid = getpid();
00073 char procname[30];
00074 sprintf(procname, "/proc/%d/statm", pid);
00075 {
00076 ifstream l_statm(procname);
00077 if(!l_statm.bad())
00078 {
00079 l_statm >> total;
00080 total *= PAGE_SIZE;
00081 l_statm >> resident;
00082 resident *= PAGE_SIZE;
00083 }
00084 }
00085 #else
00086 #warning Need virtualSize/physicalSize code for this platform
00087 #warning OS::GetProcessSize will return zeroes
00088 total = 0;
00089 resident = 0;
00090 #endif
00091
00092 }
00093
00094 void OS::setenv(const Text &var, const Text &val) throw()
00095 {
00096 char* ev = NEW_PTRFREE_ARRAY(char, var.Length() + 1 + val.Length() + 1);
00097 sprintf(ev, "%s=%s", var.cchars(), val.cchars());
00098 putenv(ev);
00099 }