LLVM API Documentation
00001 //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file provides the generic Unix implementation of the Process class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "Unix.h" 00015 #ifdef HAVE_SYS_TIME_H 00016 #include <sys/time.h> 00017 #endif 00018 #ifdef HAVE_SYS_RESOURCE_H 00019 #include <sys/resource.h> 00020 #endif 00021 #ifdef HAVE_MALLOC_H 00022 #include <malloc.h> 00023 #endif 00024 #ifdef HAVE_MALLOC_MALLOC_H 00025 #include <malloc/malloc.h> 00026 #endif 00027 #ifdef HAVE_SYS_IOCTL_H 00028 # include <sys/ioctl.h> 00029 #endif 00030 #ifdef HAVE_TERMIOS_H 00031 # include <termios.h> 00032 #endif 00033 00034 //===----------------------------------------------------------------------===// 00035 //=== WARNING: Implementation here must contain only generic UNIX code that 00036 //=== is guaranteed to work on *all* UNIX variants. 00037 //===----------------------------------------------------------------------===// 00038 00039 using namespace llvm; 00040 using namespace sys; 00041 00042 unsigned 00043 Process::GetPageSize() 00044 { 00045 #if defined(__CYGWIN__) 00046 // On Cygwin, getpagesize() returns 64k but the page size for the purposes of 00047 // memory protection and mmap() is 4k. 00048 // See http://www.cygwin.com/ml/cygwin/2009-01/threads.html#00492 00049 const int page_size = 0x1000; 00050 #elif defined(HAVE_GETPAGESIZE) 00051 const int page_size = ::getpagesize(); 00052 #elif defined(HAVE_SYSCONF) 00053 long page_size = ::sysconf(_SC_PAGE_SIZE); 00054 #else 00055 #warning Cannot get the page size on this machine 00056 #endif 00057 return static_cast<unsigned>(page_size); 00058 } 00059 00060 size_t Process::GetMallocUsage() { 00061 #if defined(HAVE_MALLINFO) 00062 struct mallinfo mi; 00063 mi = ::mallinfo(); 00064 return mi.uordblks; 00065 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) 00066 malloc_statistics_t Stats; 00067 malloc_zone_statistics(malloc_default_zone(), &Stats); 00068 return Stats.size_in_use; // darwin 00069 #elif defined(HAVE_SBRK) 00070 // Note this is only an approximation and more closely resembles 00071 // the value returned by mallinfo in the arena field. 00072 static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0)); 00073 char *EndOfMemory = (char*)sbrk(0); 00074 if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1)) 00075 return EndOfMemory - StartOfMemory; 00076 else 00077 return 0; 00078 #else 00079 #warning Cannot get malloc info on this platform 00080 return 0; 00081 #endif 00082 } 00083 00084 size_t 00085 Process::GetTotalMemoryUsage() 00086 { 00087 #if defined(HAVE_MALLINFO) 00088 struct mallinfo mi = ::mallinfo(); 00089 return mi.uordblks + mi.hblkhd; 00090 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) 00091 malloc_statistics_t Stats; 00092 malloc_zone_statistics(malloc_default_zone(), &Stats); 00093 return Stats.size_allocated; // darwin 00094 #elif defined(HAVE_GETRUSAGE) 00095 struct rusage usage; 00096 ::getrusage(RUSAGE_SELF, &usage); 00097 return usage.ru_maxrss; 00098 #else 00099 #warning Cannot get total memory size on this platform 00100 return 0; 00101 #endif 00102 } 00103 00104 void 00105 Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time, 00106 TimeValue& sys_time) 00107 { 00108 elapsed = TimeValue::now(); 00109 #if defined(HAVE_GETRUSAGE) 00110 struct rusage usage; 00111 ::getrusage(RUSAGE_SELF, &usage); 00112 user_time = TimeValue( 00113 static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ), 00114 static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec * 00115 TimeValue::NANOSECONDS_PER_MICROSECOND ) ); 00116 sys_time = TimeValue( 00117 static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ), 00118 static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec * 00119 TimeValue::NANOSECONDS_PER_MICROSECOND ) ); 00120 #else 00121 #warning Cannot get usage times on this platform 00122 user_time.seconds(0); 00123 user_time.microseconds(0); 00124 sys_time.seconds(0); 00125 sys_time.microseconds(0); 00126 #endif 00127 } 00128 00129 int Process::GetCurrentUserId() { 00130 return getuid(); 00131 } 00132 00133 int Process::GetCurrentGroupId() { 00134 return getgid(); 00135 } 00136 00137 #ifdef HAVE_MACH_MACH_H 00138 #include <mach/mach.h> 00139 #endif 00140 00141 // Some LLVM programs such as bugpoint produce core files as a normal part of 00142 // their operation. To prevent the disk from filling up, this function 00143 // does what's necessary to prevent their generation. 00144 void Process::PreventCoreFiles() { 00145 #if HAVE_SETRLIMIT 00146 struct rlimit rlim; 00147 rlim.rlim_cur = rlim.rlim_max = 0; 00148 setrlimit(RLIMIT_CORE, &rlim); 00149 #endif 00150 00151 #ifdef HAVE_MACH_MACH_H 00152 // Disable crash reporting on Mac OS X 10.0-10.4 00153 00154 // get information about the original set of exception ports for the task 00155 mach_msg_type_number_t Count = 0; 00156 exception_mask_t OriginalMasks[EXC_TYPES_COUNT]; 00157 exception_port_t OriginalPorts[EXC_TYPES_COUNT]; 00158 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT]; 00159 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT]; 00160 kern_return_t err = 00161 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks, 00162 &Count, OriginalPorts, OriginalBehaviors, 00163 OriginalFlavors); 00164 if (err == KERN_SUCCESS) { 00165 // replace each with MACH_PORT_NULL. 00166 for (unsigned i = 0; i != Count; ++i) 00167 task_set_exception_ports(mach_task_self(), OriginalMasks[i], 00168 MACH_PORT_NULL, OriginalBehaviors[i], 00169 OriginalFlavors[i]); 00170 } 00171 00172 // Disable crash reporting on Mac OS X 10.5 00173 signal(SIGABRT, _exit); 00174 signal(SIGILL, _exit); 00175 signal(SIGFPE, _exit); 00176 signal(SIGSEGV, _exit); 00177 signal(SIGBUS, _exit); 00178 #endif 00179 } 00180 00181 bool Process::StandardInIsUserInput() { 00182 #if HAVE_ISATTY 00183 return isatty(0); 00184 #endif 00185 // If we don't have isatty, just return false. 00186 return false; 00187 } 00188 00189 bool Process::StandardOutIsDisplayed() { 00190 #if HAVE_ISATTY 00191 return isatty(1); 00192 #endif 00193 // If we don't have isatty, just return false. 00194 return false; 00195 } 00196 00197 bool Process::StandardErrIsDisplayed() { 00198 #if HAVE_ISATTY 00199 return isatty(2); 00200 #endif 00201 // If we don't have isatty, just return false. 00202 return false; 00203 } 00204 00205 static unsigned getColumns(int FileID) { 00206 // If COLUMNS is defined in the environment, wrap to that many columns. 00207 if (const char *ColumnsStr = std::getenv("COLUMNS")) { 00208 int Columns = std::atoi(ColumnsStr); 00209 if (Columns > 0) 00210 return Columns; 00211 } 00212 00213 unsigned Columns = 0; 00214 00215 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) 00216 // Try to determine the width of the terminal. 00217 struct winsize ws; 00218 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) 00219 Columns = ws.ws_col; 00220 #endif 00221 00222 return Columns; 00223 } 00224 00225 unsigned Process::StandardOutColumns() { 00226 if (!StandardOutIsDisplayed()) 00227 return 0; 00228 00229 return getColumns(1); 00230 } 00231 00232 unsigned Process::StandardErrColumns() { 00233 if (!StandardErrIsDisplayed()) 00234 return 0; 00235 00236 return getColumns(2); 00237 } 00238 00239 static bool terminalHasColors() { 00240 if (const char *term = std::getenv("TERM")) { 00241 // Most modern terminals support ANSI escape sequences for colors. 00242 // We could check terminfo, or have a list of known terms that support 00243 // colors, but that would be overkill. 00244 // The user can always ask for no colors by setting TERM to dumb, or 00245 // using a commandline flag. 00246 return strcmp(term, "dumb") != 0; 00247 } 00248 return false; 00249 } 00250 00251 bool Process::StandardOutHasColors() { 00252 if (!StandardOutIsDisplayed()) 00253 return false; 00254 return terminalHasColors(); 00255 } 00256 00257 bool Process::StandardErrHasColors() { 00258 if (!StandardErrIsDisplayed()) 00259 return false; 00260 return terminalHasColors(); 00261 } 00262 00263 bool Process::ColorNeedsFlush() { 00264 // No, we use ANSI escape sequences. 00265 return false; 00266 } 00267 00268 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" 00269 00270 #define ALLCOLORS(FGBG,BOLD) {\ 00271 COLOR(FGBG, "0", BOLD),\ 00272 COLOR(FGBG, "1", BOLD),\ 00273 COLOR(FGBG, "2", BOLD),\ 00274 COLOR(FGBG, "3", BOLD),\ 00275 COLOR(FGBG, "4", BOLD),\ 00276 COLOR(FGBG, "5", BOLD),\ 00277 COLOR(FGBG, "6", BOLD),\ 00278 COLOR(FGBG, "7", BOLD)\ 00279 } 00280 00281 static const char* colorcodes[2][2][8] = { 00282 { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, 00283 { ALLCOLORS("4",""), ALLCOLORS("4","1;") } 00284 }; 00285 00286 const char *Process::OutputColor(char code, bool bold, bool bg) { 00287 return colorcodes[bg?1:0][bold?1:0][code&7]; 00288 } 00289 00290 const char *Process::OutputBold(bool bg) { 00291 return "\033[1m"; 00292 } 00293 00294 const char *Process::ResetColor() { 00295 return "\033[0m"; 00296 }