1 //===- Unix/Process.cpp - Unix Process Implementation --------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source 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>
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only generic UNIX code that
30 //=== is guaranteed to work on *all* UNIX variants.
31 //===----------------------------------------------------------------------===//
37 Process::GetPageSize()
39 #if defined(HAVE_GETPAGESIZE)
40 static const int page_size = ::getpagesize();
41 #elif defined(HAVE_SYSCONF)
42 static long page_size = ::sysconf(_SC_PAGE_SIZE);
44 #warning Cannot get the page size on this machine
46 return static_cast<unsigned>(page_size);
49 size_t Process::GetMallocUsage() {
50 #if defined(HAVE_MALLINFO)
54 #elif defined(HAVE_MSTATS) && defined(HAVE_MALLOC_MALLOC_H)
55 return mstats().bytes_used; // darwin
56 #elif defined(HAVE_SBRK)
57 // Note this is only an approximation and more closely resembles
58 // the value returned by mallinfo in the arena field.
59 static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0));
60 char *EndOfMemory = (char*)sbrk(0);
61 if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1))
62 return EndOfMemory - StartOfMemory;
66 #warning Cannot get malloc info on this platform
72 Process::GetTotalMemoryUsage()
74 #if defined(HAVE_MALLINFO)
75 struct mallinfo mi = ::mallinfo();
76 return mi.uordblks + mi.hblkhd;
77 #elif defined(HAVE_MSTATS) && defined(HAVE_MALLOC_MALLOC_H)
78 return mstats().bytes_total; // darwin
79 #elif defined(HAVE_GETRUSAGE)
81 ::getrusage(RUSAGE_SELF, &usage);
82 return usage.ru_maxrss;
84 #warning Cannot get total memory size on this platform
90 Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time,
93 elapsed = TimeValue::now();
94 #if defined(HAVE_GETRUSAGE)
96 ::getrusage(RUSAGE_SELF, &usage);
97 user_time = TimeValue(
98 static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ),
99 static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec *
100 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
101 sys_time = TimeValue(
102 static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ),
103 static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec *
104 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
106 #warning Cannot get usage times on this platform
107 user_time.seconds(0);
108 user_time.microseconds(0);
110 sys_time.microseconds(0);
114 int Process::GetCurrentUserId()
119 int Process::GetCurrentGroupId()
124 // Some LLVM programs such as bugpoint produce core files as a normal part of
125 // their operation. To prevent the disk from filling up, this function
126 // does what's necessary to prevent their generation.
127 void Process::PreventCoreFiles() {
130 rlim.rlim_cur = rlim.rlim_max = 0;
131 int res = setrlimit(RLIMIT_CORE, &rlim);
133 ThrowErrno("Can't prevent core file generation");
137 bool Process::StandardInIsUserInput() {
141 // If we don't have isatty, just return false.
145 bool Process::StandardOutIsDisplayed() {
149 // If we don't have isatty, just return false.
153 bool Process::StandardErrIsDisplayed() {
157 // If we don't have isatty, just return false.