blob: 32733d61366863bf47d203584d2b44eb265d3598 [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//
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.
7//
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
Reid Spencer27dafe12004-09-11 04:56:56 +000027
28//===----------------------------------------------------------------------===//
29//=== WARNING: Implementation here must contain only generic UNIX code that
30//=== is guaranteed to work on *all* UNIX variants.
31//===----------------------------------------------------------------------===//
32
33namespace llvm {
34using namespace sys;
35
36unsigned
Reid Spencer721d9aa2004-12-20 00:59:28 +000037Process::GetPageSize()
38{
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);
43#else
44#warning Cannot get the page size on this machine
45#endif
Reid Spencer27dafe12004-09-11 04:56:56 +000046 return static_cast<unsigned>(page_size);
47}
48
Chris Lattner513a9542005-11-14 07:00:29 +000049size_t Process::GetMallocUsage() {
Reid Spencerbc1ee842004-12-20 16:06:44 +000050#if defined(HAVE_MALLINFO)
Reid Spencer721d9aa2004-12-20 00:59:28 +000051 struct mallinfo mi;
52 mi = ::mallinfo();
53 return mi.uordblks;
Chris Lattnerc590e9a2005-11-14 07:27:56 +000054#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
Reid Spencerbc1ee842004-12-20 16:06:44 +000058#elif defined(HAVE_SBRK)
Reid Spencer721d9aa2004-12-20 00:59:28 +000059 // Note this is only an approximation and more closely resembles
60 // the value returned by mallinfo in the arena field.
Chris Lattner513a9542005-11-14 07:00:29 +000061 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;
Reid Spencer721d9aa2004-12-20 00:59:28 +000065 else
66 return 0;
67#else
68#warning Cannot get malloc info on this platform
69 return 0;
70#endif
71}
72
Jeff Cohene269a1a2005-01-08 20:15:57 +000073size_t
Reid Spencer721d9aa2004-12-20 00:59:28 +000074Process::GetTotalMemoryUsage()
75{
Reid Spencerbc1ee842004-12-20 16:06:44 +000076#if defined(HAVE_MALLINFO)
Reid Spencer721d9aa2004-12-20 00:59:28 +000077 struct mallinfo mi = ::mallinfo();
78 return mi.uordblks + mi.hblkhd;
Chris Lattnerc590e9a2005-11-14 07:27:56 +000079#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
Reid Spencered5e7bf2004-12-20 16:33:37 +000083#elif defined(HAVE_GETRUSAGE)
84 struct rusage usage;
85 ::getrusage(RUSAGE_SELF, &usage);
86 return usage.ru_maxrss;
Reid Spencer721d9aa2004-12-20 00:59:28 +000087#else
88#warning Cannot get total memory size on this platform
89 return 0;
90#endif
91}
92
93void
94Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time,
95 TimeValue& sys_time)
96{
97 elapsed = TimeValue::now();
Chris Lattner095cc372006-05-12 18:20:39 +000098#if defined(HAVE_GETRUSAGE)
Reid Spencer721d9aa2004-12-20 00:59:28 +000099 struct rusage usage;
100 ::getrusage(RUSAGE_SELF, &usage);
Reid Spencer8b662892004-12-20 21:43:33 +0000101 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 ) );
Reid Spencer721d9aa2004-12-20 00:59:28 +0000109#else
110#warning Cannot get usage times on this platform
111 user_time.seconds(0);
112 user_time.microseconds(0);
113 sys_time.seconds(0);
114 sys_time.microseconds(0);
115#endif
116}
117
Reid Spencer93b34732005-04-21 16:12:57 +0000118int Process::GetCurrentUserId()
119{
120 return getuid();
121}
122
123int Process::GetCurrentGroupId()
124{
125 return getgid();
126}
127
Reid Spencer68fdcc12004-12-27 06:17:27 +0000128// 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.
131void Process::PreventCoreFiles() {
132#if HAVE_SETRLIMIT
133 struct rlimit rlim;
134 rlim.rlim_cur = rlim.rlim_max = 0;
135 int res = setrlimit(RLIMIT_CORE, &rlim);
136 if (res != 0)
137 ThrowErrno("Can't prevent core file generation");
138#endif
139}
Reid Spencer721d9aa2004-12-20 00:59:28 +0000140
Reid Spencera01aade2005-01-01 22:29:26 +0000141bool Process::StandardInIsUserInput() {
142#if HAVE_ISATTY
143 return isatty(0);
144#endif
145 // If we don't have isatty, just return false.
146 return false;
147}
148
149bool Process::StandardOutIsDisplayed() {
150#if HAVE_ISATTY
151 return isatty(1);
152#endif
153 // If we don't have isatty, just return false.
154 return false;
155}
156
157bool Process::StandardErrIsDisplayed() {
158#if HAVE_ISATTY
159 return isatty(2);
160#endif
161 // If we don't have isatty, just return false.
162 return false;
163}
164
Reid Spencer27dafe12004-09-11 04:56:56 +0000165}