blob: b1286b7bfcd5e98050120d8837e0878a53d34512 [file] [log] [blame]
Reid Spencer27dafe12004-09-11 04:56:56 +00001//===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer27dafe12004-09-11 04:56:56 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the generic Unix implementation of the Process class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer721d9aa2004-12-20 00:59:28 +000014#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 Lattner513a9542005-11-14 07:00:29 +000024#ifdef HAVE_MALLOC_MALLOC_H
25#include <malloc/malloc.h>
26#endif
Douglas Gregor01746742009-05-11 18:05:52 +000027#ifdef HAVE_SYS_IOCTL_H
28# include <sys/ioctl.h>
29#endif
Reid Spencer27dafe12004-09-11 04:56:56 +000030
31//===----------------------------------------------------------------------===//
32//=== WARNING: Implementation here must contain only generic UNIX code that
33//=== is guaranteed to work on *all* UNIX variants.
34//===----------------------------------------------------------------------===//
35
Chris Lattner26b6c0b2006-09-14 06:01:41 +000036using namespace llvm;
Reid Spencer27dafe12004-09-11 04:56:56 +000037using namespace sys;
38
39unsigned
Reid Spencer721d9aa2004-12-20 00:59:28 +000040Process::GetPageSize()
41{
42#if defined(HAVE_GETPAGESIZE)
43 static const int page_size = ::getpagesize();
44#elif defined(HAVE_SYSCONF)
45 static long page_size = ::sysconf(_SC_PAGE_SIZE);
46#else
47#warning Cannot get the page size on this machine
48#endif
Reid Spencer27dafe12004-09-11 04:56:56 +000049 return static_cast<unsigned>(page_size);
50}
51
Chris Lattner513a9542005-11-14 07:00:29 +000052size_t Process::GetMallocUsage() {
Reid Spencerbc1ee842004-12-20 16:06:44 +000053#if defined(HAVE_MALLINFO)
Reid Spencer721d9aa2004-12-20 00:59:28 +000054 struct mallinfo mi;
55 mi = ::mallinfo();
56 return mi.uordblks;
Chris Lattnerc590e9a2005-11-14 07:27:56 +000057#elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
58 malloc_statistics_t Stats;
59 malloc_zone_statistics(malloc_default_zone(), &Stats);
60 return Stats.size_in_use; // darwin
Reid Spencerbc1ee842004-12-20 16:06:44 +000061#elif defined(HAVE_SBRK)
Reid Spencer721d9aa2004-12-20 00:59:28 +000062 // Note this is only an approximation and more closely resembles
63 // the value returned by mallinfo in the arena field.
Chris Lattner513a9542005-11-14 07:00:29 +000064 static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0));
65 char *EndOfMemory = (char*)sbrk(0);
66 if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1))
67 return EndOfMemory - StartOfMemory;
Reid Spencer721d9aa2004-12-20 00:59:28 +000068 else
69 return 0;
70#else
71#warning Cannot get malloc info on this platform
72 return 0;
73#endif
74}
75
Jeff Cohene269a1a2005-01-08 20:15:57 +000076size_t
Reid Spencer721d9aa2004-12-20 00:59:28 +000077Process::GetTotalMemoryUsage()
78{
Reid Spencerbc1ee842004-12-20 16:06:44 +000079#if defined(HAVE_MALLINFO)
Reid Spencer721d9aa2004-12-20 00:59:28 +000080 struct mallinfo mi = ::mallinfo();
81 return mi.uordblks + mi.hblkhd;
Chris Lattnerc590e9a2005-11-14 07:27:56 +000082#elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
83 malloc_statistics_t Stats;
84 malloc_zone_statistics(malloc_default_zone(), &Stats);
85 return Stats.size_allocated; // darwin
Reid Spencered5e7bf2004-12-20 16:33:37 +000086#elif defined(HAVE_GETRUSAGE)
87 struct rusage usage;
88 ::getrusage(RUSAGE_SELF, &usage);
89 return usage.ru_maxrss;
Reid Spencer721d9aa2004-12-20 00:59:28 +000090#else
91#warning Cannot get total memory size on this platform
92 return 0;
93#endif
94}
95
96void
97Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time,
98 TimeValue& sys_time)
99{
100 elapsed = TimeValue::now();
Chris Lattner095cc372006-05-12 18:20:39 +0000101#if defined(HAVE_GETRUSAGE)
Reid Spencer721d9aa2004-12-20 00:59:28 +0000102 struct rusage usage;
103 ::getrusage(RUSAGE_SELF, &usage);
Reid Spencer8b662892004-12-20 21:43:33 +0000104 user_time = TimeValue(
105 static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ),
106 static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec *
107 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
108 sys_time = TimeValue(
109 static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ),
110 static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec *
111 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
Reid Spencer721d9aa2004-12-20 00:59:28 +0000112#else
113#warning Cannot get usage times on this platform
114 user_time.seconds(0);
115 user_time.microseconds(0);
116 sys_time.seconds(0);
117 sys_time.microseconds(0);
118#endif
119}
120
Chris Lattner26b6c0b2006-09-14 06:01:41 +0000121int Process::GetCurrentUserId() {
Reid Spencer93b34732005-04-21 16:12:57 +0000122 return getuid();
123}
124
Chris Lattner26b6c0b2006-09-14 06:01:41 +0000125int Process::GetCurrentGroupId() {
Reid Spencer93b34732005-04-21 16:12:57 +0000126 return getgid();
127}
128
Nate Begemane9563982008-04-12 00:47:46 +0000129#ifdef HAVE_MACH_MACH_H
130#include <mach/mach.h>
131#endif
132
Reid Spencer68fdcc12004-12-27 06:17:27 +0000133// Some LLVM programs such as bugpoint produce core files as a normal part of
134// their operation. To prevent the disk from filling up, this function
135// does what's necessary to prevent their generation.
136void Process::PreventCoreFiles() {
137#if HAVE_SETRLIMIT
138 struct rlimit rlim;
139 rlim.rlim_cur = rlim.rlim_max = 0;
Chris Lattner505f9c82006-05-14 18:53:09 +0000140 setrlimit(RLIMIT_CORE, &rlim);
Reid Spencer68fdcc12004-12-27 06:17:27 +0000141#endif
Chris Lattner26b6c0b2006-09-14 06:01:41 +0000142
Chris Lattner5f1d0132006-09-14 06:21:59 +0000143#ifdef HAVE_MACH_MACH_H
Nate Begemane9563982008-04-12 00:47:46 +0000144 // Disable crash reporting on Mac OS X 10.0-10.4
145
146 // get information about the original set of exception ports for the task
147 mach_msg_type_number_t Count = 0;
148 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
149 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
150 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
151 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
152 kern_return_t err =
153 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
154 &Count, OriginalPorts, OriginalBehaviors,
155 OriginalFlavors);
156 if (err == KERN_SUCCESS) {
157 // replace each with MACH_PORT_NULL.
158 for (unsigned i = 0; i != Count; ++i)
159 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
160 MACH_PORT_NULL, OriginalBehaviors[i],
161 OriginalFlavors[i]);
162 }
163
164 // Disable crash reporting on Mac OS X 10.5
Nate Begeman82b53cd2008-03-31 22:19:25 +0000165 signal(SIGABRT, _exit);
166 signal(SIGILL, _exit);
167 signal(SIGFPE, _exit);
168 signal(SIGSEGV, _exit);
169 signal(SIGBUS, _exit);
Chris Lattner26b6c0b2006-09-14 06:01:41 +0000170#endif
Reid Spencer68fdcc12004-12-27 06:17:27 +0000171}
Reid Spencer721d9aa2004-12-20 00:59:28 +0000172
Reid Spencera01aade2005-01-01 22:29:26 +0000173bool Process::StandardInIsUserInput() {
174#if HAVE_ISATTY
175 return isatty(0);
176#endif
177 // If we don't have isatty, just return false.
178 return false;
179}
180
181bool Process::StandardOutIsDisplayed() {
182#if HAVE_ISATTY
183 return isatty(1);
184#endif
185 // If we don't have isatty, just return false.
186 return false;
187}
188
189bool Process::StandardErrIsDisplayed() {
190#if HAVE_ISATTY
191 return isatty(2);
192#endif
193 // If we don't have isatty, just return false.
194 return false;
195}
Douglas Gregor01746742009-05-11 18:05:52 +0000196
197static unsigned getColumns(int FileID) {
198 // If COLUMNS is defined in the environment, wrap to that many columns.
199 if (const char *ColumnsStr = std::getenv("COLUMNS")) {
200 int Columns = std::atoi(ColumnsStr);
201 if (Columns > 0)
202 return Columns;
203 }
204
205 unsigned Columns = 0;
206
207#ifdef HAVE_SYS_IOCTL_H
208 // Try to determine the width of the terminal.
209 struct winsize ws;
210 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
211 Columns = ws.ws_col;
212#endif
213
214 return Columns;
215}
216
217unsigned Process::StandardOutColumns() {
218 if (!StandardOutIsDisplayed())
219 return 0;
220
221 return getColumns(1);
222}
223
224unsigned Process::StandardErrColumns() {
225 if (!StandardErrIsDisplayed())
226 return 0;
227
228 return getColumns(2);
229}