Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 1 | //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the generic Unix implementation of the Process class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 14 | #include "Unix.h" |
| 15 | #ifdef HAVE_SYS_TIME_H |
| 16 | #include <sys/time.h> |
| 17 | #endif |
| 18 | #ifdef HAVE_SYS_RESOURCE_H |
| 19 | #include <sys/resource.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_MALLOC_H |
| 22 | #include <malloc.h> |
| 23 | #endif |
Chris Lattner | 513a954 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 24 | #ifdef HAVE_MALLOC_MALLOC_H |
| 25 | #include <malloc/malloc.h> |
| 26 | #endif |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_IOCTL_H |
| 28 | # include <sys/ioctl.h> |
| 29 | #endif |
Douglas Gregor | 071d73d | 2009-05-18 17:21:34 +0000 | [diff] [blame^] | 30 | #ifdef HAVE_TERMIOS_H |
| 31 | # include <termios.h> |
| 32 | #endif |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 36 | //=== is guaranteed to work on *all* UNIX variants. |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 40 | using namespace sys; |
| 41 | |
| 42 | unsigned |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 43 | Process::GetPageSize() |
| 44 | { |
| 45 | #if defined(HAVE_GETPAGESIZE) |
| 46 | static const int page_size = ::getpagesize(); |
| 47 | #elif defined(HAVE_SYSCONF) |
| 48 | static long page_size = ::sysconf(_SC_PAGE_SIZE); |
| 49 | #else |
| 50 | #warning Cannot get the page size on this machine |
| 51 | #endif |
Reid Spencer | 27dafe1 | 2004-09-11 04:56:56 +0000 | [diff] [blame] | 52 | return static_cast<unsigned>(page_size); |
| 53 | } |
| 54 | |
Chris Lattner | 513a954 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 55 | size_t Process::GetMallocUsage() { |
Reid Spencer | bc1ee84 | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 56 | #if defined(HAVE_MALLINFO) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 57 | struct mallinfo mi; |
| 58 | mi = ::mallinfo(); |
| 59 | return mi.uordblks; |
Chris Lattner | c590e9a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 60 | #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) |
| 61 | malloc_statistics_t Stats; |
| 62 | malloc_zone_statistics(malloc_default_zone(), &Stats); |
| 63 | return Stats.size_in_use; // darwin |
Reid Spencer | bc1ee84 | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 64 | #elif defined(HAVE_SBRK) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 65 | // Note this is only an approximation and more closely resembles |
| 66 | // the value returned by mallinfo in the arena field. |
Chris Lattner | 513a954 | 2005-11-14 07:00:29 +0000 | [diff] [blame] | 67 | static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0)); |
| 68 | char *EndOfMemory = (char*)sbrk(0); |
| 69 | if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1)) |
| 70 | return EndOfMemory - StartOfMemory; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 71 | else |
| 72 | return 0; |
| 73 | #else |
| 74 | #warning Cannot get malloc info on this platform |
| 75 | return 0; |
| 76 | #endif |
| 77 | } |
| 78 | |
Jeff Cohen | e269a1a | 2005-01-08 20:15:57 +0000 | [diff] [blame] | 79 | size_t |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 80 | Process::GetTotalMemoryUsage() |
| 81 | { |
Reid Spencer | bc1ee84 | 2004-12-20 16:06:44 +0000 | [diff] [blame] | 82 | #if defined(HAVE_MALLINFO) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 83 | struct mallinfo mi = ::mallinfo(); |
| 84 | return mi.uordblks + mi.hblkhd; |
Chris Lattner | c590e9a | 2005-11-14 07:27:56 +0000 | [diff] [blame] | 85 | #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) |
| 86 | malloc_statistics_t Stats; |
| 87 | malloc_zone_statistics(malloc_default_zone(), &Stats); |
| 88 | return Stats.size_allocated; // darwin |
Reid Spencer | ed5e7bf | 2004-12-20 16:33:37 +0000 | [diff] [blame] | 89 | #elif defined(HAVE_GETRUSAGE) |
| 90 | struct rusage usage; |
| 91 | ::getrusage(RUSAGE_SELF, &usage); |
| 92 | return usage.ru_maxrss; |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 93 | #else |
| 94 | #warning Cannot get total memory size on this platform |
| 95 | return 0; |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time, |
| 101 | TimeValue& sys_time) |
| 102 | { |
| 103 | elapsed = TimeValue::now(); |
Chris Lattner | 095cc37 | 2006-05-12 18:20:39 +0000 | [diff] [blame] | 104 | #if defined(HAVE_GETRUSAGE) |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 105 | struct rusage usage; |
| 106 | ::getrusage(RUSAGE_SELF, &usage); |
Reid Spencer | 8b66289 | 2004-12-20 21:43:33 +0000 | [diff] [blame] | 107 | user_time = TimeValue( |
| 108 | static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ), |
| 109 | static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec * |
| 110 | TimeValue::NANOSECONDS_PER_MICROSECOND ) ); |
| 111 | sys_time = TimeValue( |
| 112 | static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ), |
| 113 | static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec * |
| 114 | TimeValue::NANOSECONDS_PER_MICROSECOND ) ); |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 115 | #else |
| 116 | #warning Cannot get usage times on this platform |
| 117 | user_time.seconds(0); |
| 118 | user_time.microseconds(0); |
| 119 | sys_time.seconds(0); |
| 120 | sys_time.microseconds(0); |
| 121 | #endif |
| 122 | } |
| 123 | |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 124 | int Process::GetCurrentUserId() { |
Reid Spencer | 93b3473 | 2005-04-21 16:12:57 +0000 | [diff] [blame] | 125 | return getuid(); |
| 126 | } |
| 127 | |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 128 | int Process::GetCurrentGroupId() { |
Reid Spencer | 93b3473 | 2005-04-21 16:12:57 +0000 | [diff] [blame] | 129 | return getgid(); |
| 130 | } |
| 131 | |
Nate Begeman | e956398 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 132 | #ifdef HAVE_MACH_MACH_H |
| 133 | #include <mach/mach.h> |
| 134 | #endif |
| 135 | |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 136 | // Some LLVM programs such as bugpoint produce core files as a normal part of |
| 137 | // their operation. To prevent the disk from filling up, this function |
| 138 | // does what's necessary to prevent their generation. |
| 139 | void Process::PreventCoreFiles() { |
| 140 | #if HAVE_SETRLIMIT |
| 141 | struct rlimit rlim; |
| 142 | rlim.rlim_cur = rlim.rlim_max = 0; |
Chris Lattner | 505f9c8 | 2006-05-14 18:53:09 +0000 | [diff] [blame] | 143 | setrlimit(RLIMIT_CORE, &rlim); |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 144 | #endif |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 5f1d013 | 2006-09-14 06:21:59 +0000 | [diff] [blame] | 146 | #ifdef HAVE_MACH_MACH_H |
Nate Begeman | e956398 | 2008-04-12 00:47:46 +0000 | [diff] [blame] | 147 | // Disable crash reporting on Mac OS X 10.0-10.4 |
| 148 | |
| 149 | // get information about the original set of exception ports for the task |
| 150 | mach_msg_type_number_t Count = 0; |
| 151 | exception_mask_t OriginalMasks[EXC_TYPES_COUNT]; |
| 152 | exception_port_t OriginalPorts[EXC_TYPES_COUNT]; |
| 153 | exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT]; |
| 154 | thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT]; |
| 155 | kern_return_t err = |
| 156 | task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks, |
| 157 | &Count, OriginalPorts, OriginalBehaviors, |
| 158 | OriginalFlavors); |
| 159 | if (err == KERN_SUCCESS) { |
| 160 | // replace each with MACH_PORT_NULL. |
| 161 | for (unsigned i = 0; i != Count; ++i) |
| 162 | task_set_exception_ports(mach_task_self(), OriginalMasks[i], |
| 163 | MACH_PORT_NULL, OriginalBehaviors[i], |
| 164 | OriginalFlavors[i]); |
| 165 | } |
| 166 | |
| 167 | // Disable crash reporting on Mac OS X 10.5 |
Nate Begeman | 82b53cd | 2008-03-31 22:19:25 +0000 | [diff] [blame] | 168 | signal(SIGABRT, _exit); |
| 169 | signal(SIGILL, _exit); |
| 170 | signal(SIGFPE, _exit); |
| 171 | signal(SIGSEGV, _exit); |
| 172 | signal(SIGBUS, _exit); |
Chris Lattner | 26b6c0b | 2006-09-14 06:01:41 +0000 | [diff] [blame] | 173 | #endif |
Reid Spencer | 68fdcc1 | 2004-12-27 06:17:27 +0000 | [diff] [blame] | 174 | } |
Reid Spencer | 721d9aa | 2004-12-20 00:59:28 +0000 | [diff] [blame] | 175 | |
Reid Spencer | a01aade | 2005-01-01 22:29:26 +0000 | [diff] [blame] | 176 | bool Process::StandardInIsUserInput() { |
| 177 | #if HAVE_ISATTY |
| 178 | return isatty(0); |
| 179 | #endif |
| 180 | // If we don't have isatty, just return false. |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | bool Process::StandardOutIsDisplayed() { |
| 185 | #if HAVE_ISATTY |
| 186 | return isatty(1); |
| 187 | #endif |
| 188 | // If we don't have isatty, just return false. |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | bool Process::StandardErrIsDisplayed() { |
| 193 | #if HAVE_ISATTY |
| 194 | return isatty(2); |
| 195 | #endif |
| 196 | // If we don't have isatty, just return false. |
| 197 | return false; |
| 198 | } |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 199 | |
| 200 | static unsigned getColumns(int FileID) { |
| 201 | // If COLUMNS is defined in the environment, wrap to that many columns. |
| 202 | if (const char *ColumnsStr = std::getenv("COLUMNS")) { |
| 203 | int Columns = std::atoi(ColumnsStr); |
| 204 | if (Columns > 0) |
| 205 | return Columns; |
| 206 | } |
| 207 | |
| 208 | unsigned Columns = 0; |
| 209 | |
Douglas Gregor | 071d73d | 2009-05-18 17:21:34 +0000 | [diff] [blame^] | 210 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) |
Douglas Gregor | 0174674 | 2009-05-11 18:05:52 +0000 | [diff] [blame] | 211 | // Try to determine the width of the terminal. |
| 212 | struct winsize ws; |
| 213 | if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) |
| 214 | Columns = ws.ws_col; |
| 215 | #endif |
| 216 | |
| 217 | return Columns; |
| 218 | } |
| 219 | |
| 220 | unsigned Process::StandardOutColumns() { |
| 221 | if (!StandardOutIsDisplayed()) |
| 222 | return 0; |
| 223 | |
| 224 | return getColumns(1); |
| 225 | } |
| 226 | |
| 227 | unsigned Process::StandardErrColumns() { |
| 228 | if (!StandardErrIsDisplayed()) |
| 229 | return 0; |
| 230 | |
| 231 | return getColumns(2); |
| 232 | } |