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_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
55 malloc_statistics_t Stats;
56 malloc_zone_statistics(malloc_default_zone(), &Stats);
57 return Stats.size_in_use; // darwin
58 #elif defined(HAVE_SBRK)
59 // Note this is only an approximation and more closely resembles
60 // the value returned by mallinfo in the arena field.
61 static char *StartOfMemory = reinterpret_cast<char*>(::sbrk(0));
62 char *EndOfMemory = (char*)sbrk(0);
63 if (EndOfMemory != ((char*)-1) && StartOfMemory != ((char*)-1))
64 return EndOfMemory - StartOfMemory;
68 #warning Cannot get malloc info on this platform
74 Process::GetTotalMemoryUsage()
76 #if defined(HAVE_MALLINFO)
77 struct mallinfo mi = ::mallinfo();
78 return mi.uordblks + mi.hblkhd;
79 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
80 malloc_statistics_t Stats;
81 malloc_zone_statistics(malloc_default_zone(), &Stats);
82 return Stats.size_allocated; // darwin
83 #elif defined(HAVE_GETRUSAGE)
85 ::getrusage(RUSAGE_SELF, &usage);
86 return usage.ru_maxrss;
88 #warning Cannot get total memory size on this platform
94 Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time,
97 elapsed = TimeValue::now();
98 #if defined(HAVE_GETRUSAGE)
100 ::getrusage(RUSAGE_SELF, &usage);
101 user_time = TimeValue(
102 static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ),
103 static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec *
104 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
105 sys_time = TimeValue(
106 static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ),
107 static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec *
108 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
110 #warning Cannot get usage times on this platform
111 user_time.seconds(0);
112 user_time.microseconds(0);
114 sys_time.microseconds(0);
118 int Process::GetCurrentUserId()
123 int Process::GetCurrentGroupId()
128 // Some LLVM programs such as bugpoint produce core files as a normal part of
129 // their operation. To prevent the disk from filling up, this function
130 // does what's necessary to prevent their generation.
131 void Process::PreventCoreFiles() {
134 rlim.rlim_cur = rlim.rlim_max = 0;
135 setrlimit(RLIMIT_CORE, &rlim);
139 bool Process::StandardInIsUserInput() {
143 // If we don't have isatty, just return false.
147 bool Process::StandardOutIsDisplayed() {
151 // If we don't have isatty, just return false.
155 bool Process::StandardErrIsDisplayed() {
159 // If we don't have isatty, just return false.