1 //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the generic Unix implementation of the Process class.
12 //===----------------------------------------------------------------------===//
15 #ifdef HAVE_SYS_TIME_H
18 #ifdef HAVE_SYS_RESOURCE_H
19 #include <sys/resource.h>
24 #ifdef HAVE_MALLOC_MALLOC_H
25 #include <malloc/malloc.h>
27 #ifdef HAVE_SYS_IOCTL_H
28 # include <sys/ioctl.h>
34 //===----------------------------------------------------------------------===//
35 //=== WARNING: Implementation here must contain only generic UNIX code that
36 //=== is guaranteed to work on *all* UNIX variants.
37 //===----------------------------------------------------------------------===//
43 Process::GetPageSize()
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);
50 #warning Cannot get the page size on this machine
52 return static_cast<unsigned>(page_size);
55 size_t Process::GetMallocUsage() {
56 #if defined(HAVE_MALLINFO)
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
64 #elif defined(HAVE_SBRK)
65 // Note this is only an approximation and more closely resembles
66 // the value returned by mallinfo in the arena field.
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;
74 #warning Cannot get malloc info on this platform
80 Process::GetTotalMemoryUsage()
82 #if defined(HAVE_MALLINFO)
83 struct mallinfo mi = ::mallinfo();
84 return mi.uordblks + mi.hblkhd;
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
89 #elif defined(HAVE_GETRUSAGE)
91 ::getrusage(RUSAGE_SELF, &usage);
92 return usage.ru_maxrss;
94 #warning Cannot get total memory size on this platform
100 Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time,
103 elapsed = TimeValue::now();
104 #if defined(HAVE_GETRUSAGE)
106 ::getrusage(RUSAGE_SELF, &usage);
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 ) );
116 #warning Cannot get usage times on this platform
117 user_time.seconds(0);
118 user_time.microseconds(0);
120 sys_time.microseconds(0);
124 int Process::GetCurrentUserId() {
128 int Process::GetCurrentGroupId() {
132 #ifdef HAVE_MACH_MACH_H
133 #include <mach/mach.h>
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() {
142 rlim.rlim_cur = rlim.rlim_max = 0;
143 setrlimit(RLIMIT_CORE, &rlim);
146 #ifdef HAVE_MACH_MACH_H
147 // Disable crash reporting on Mac OS X 10.0-10.4
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];
156 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
157 &Count, OriginalPorts, OriginalBehaviors,
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],
167 // Disable crash reporting on Mac OS X 10.5
168 signal(SIGABRT, _exit);
169 signal(SIGILL, _exit);
170 signal(SIGFPE, _exit);
171 signal(SIGSEGV, _exit);
172 signal(SIGBUS, _exit);
176 bool Process::StandardInIsUserInput() {
180 // If we don't have isatty, just return false.
184 bool Process::StandardOutIsDisplayed() {
188 // If we don't have isatty, just return false.
192 bool Process::StandardErrIsDisplayed() {
196 // If we don't have isatty, just return false.
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);
208 unsigned Columns = 0;
210 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H)
211 // Try to determine the width of the terminal.
213 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
220 unsigned Process::StandardOutColumns() {
221 if (!StandardOutIsDisplayed())
224 return getColumns(1);
227 unsigned Process::StandardErrColumns() {
228 if (!StandardErrIsDisplayed())
231 return getColumns(2);