Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

OS.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Sat May 28 16:39:20 EDT 2005 by ken@xorian.net        
00020 //      modified on Thu Apr 21 09:19:09 EDT 2005 by irina.furman@intel.com
00021 //      modified on Mon May  3 15:54:15 PDT 1999 by heydon
00022 //      modified on Fri May 19 08:51:44 PDT 1995 by levin
00023 
00024 // This file implements various miscellanous routines.
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(/*OUT*/ unsigned long &total,
00052                         /*OUT*/ unsigned long &resident) throw()
00053 {
00054   // in event of error, report zero for process sizes
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, /*mode=*/ 0 /*ignored*/);
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 }

Generated on Mon May 8 00:48:30 2006 for Vesta by  doxygen 1.4.2