blob: cccd3ffcb67bbd6f218da5aae5ec62d2ee38fbb1 [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
Reid Spencer27dafe12004-09-11 04:56:56 +000024
25//===----------------------------------------------------------------------===//
26//=== WARNING: Implementation here must contain only generic UNIX code that
27//=== is guaranteed to work on *all* UNIX variants.
28//===----------------------------------------------------------------------===//
29
30namespace llvm {
31using namespace sys;
32
33unsigned
Reid Spencer721d9aa2004-12-20 00:59:28 +000034Process::GetPageSize()
35{
36#if defined(HAVE_GETPAGESIZE)
37 static const int page_size = ::getpagesize();
38#elif defined(HAVE_SYSCONF)
39 static long page_size = ::sysconf(_SC_PAGE_SIZE);
40#else
41#warning Cannot get the page size on this machine
42#endif
Reid Spencer27dafe12004-09-11 04:56:56 +000043 return static_cast<unsigned>(page_size);
44}
45
Reid Spencer721d9aa2004-12-20 00:59:28 +000046#if defined(HAVE_SBRK)
47static char* som = reinterpret_cast<char*>(::sbrk(0));
48#endif
49
50uint64_t
51Process::GetMallocUsage()
52{
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;
Reid Spencerbc1ee842004-12-20 16:06:44 +000057#elif defined(HAVE_SBRK)
Reid Spencer721d9aa2004-12-20 00:59:28 +000058 // Note this is only an approximation and more closely resembles
59 // the value returned by mallinfo in the arena field.
Reid Spencer92ced2f2004-12-31 05:53:27 +000060 char * eom = (char*) sbrk(0);
Reid Spencer721d9aa2004-12-20 00:59:28 +000061 if (eom != ((char*)-1) && som != ((char*)-1))
62 return eom - som;
63 else
64 return 0;
65#else
66#warning Cannot get malloc info on this platform
67 return 0;
68#endif
69}
70
71uint64_t
72Process::GetTotalMemoryUsage()
73{
Reid Spencerbc1ee842004-12-20 16:06:44 +000074#if defined(HAVE_MALLINFO)
Reid Spencer721d9aa2004-12-20 00:59:28 +000075 struct mallinfo mi = ::mallinfo();
76 return mi.uordblks + mi.hblkhd;
Reid Spencered5e7bf2004-12-20 16:33:37 +000077#elif defined(HAVE_GETRUSAGE)
78 struct rusage usage;
79 ::getrusage(RUSAGE_SELF, &usage);
80 return usage.ru_maxrss;
Reid Spencer721d9aa2004-12-20 00:59:28 +000081#else
82#warning Cannot get total memory size on this platform
83 return 0;
84#endif
85}
86
87void
88Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time,
89 TimeValue& sys_time)
90{
91 elapsed = TimeValue::now();
Reid Spencerbc1ee842004-12-20 16:06:44 +000092#if defined(HAVE_GETRUSAGE)
Reid Spencer721d9aa2004-12-20 00:59:28 +000093 struct rusage usage;
94 ::getrusage(RUSAGE_SELF, &usage);
Reid Spencer8b662892004-12-20 21:43:33 +000095 user_time = TimeValue(
96 static_cast<TimeValue::SecondsType>( usage.ru_utime.tv_sec ),
97 static_cast<TimeValue::NanoSecondsType>( usage.ru_utime.tv_usec *
98 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
99 sys_time = TimeValue(
100 static_cast<TimeValue::SecondsType>( usage.ru_stime.tv_sec ),
101 static_cast<TimeValue::NanoSecondsType>( usage.ru_stime.tv_usec *
102 TimeValue::NANOSECONDS_PER_MICROSECOND ) );
Reid Spencer721d9aa2004-12-20 00:59:28 +0000103#else
104#warning Cannot get usage times on this platform
105 user_time.seconds(0);
106 user_time.microseconds(0);
107 sys_time.seconds(0);
108 sys_time.microseconds(0);
109#endif
110}
111
Reid Spencer68fdcc12004-12-27 06:17:27 +0000112// Some LLVM programs such as bugpoint produce core files as a normal part of
113// their operation. To prevent the disk from filling up, this function
114// does what's necessary to prevent their generation.
115void Process::PreventCoreFiles() {
116#if HAVE_SETRLIMIT
117 struct rlimit rlim;
118 rlim.rlim_cur = rlim.rlim_max = 0;
119 int res = setrlimit(RLIMIT_CORE, &rlim);
120 if (res != 0)
121 ThrowErrno("Can't prevent core file generation");
122#endif
123}
Reid Spencer721d9aa2004-12-20 00:59:28 +0000124
Reid Spencera01aade2005-01-01 22:29:26 +0000125bool Process::StandardInIsUserInput() {
126#if HAVE_ISATTY
127 return isatty(0);
128#endif
129 // If we don't have isatty, just return false.
130 return false;
131}
132
133bool Process::StandardOutIsDisplayed() {
134#if HAVE_ISATTY
135 return isatty(1);
136#endif
137 // If we don't have isatty, just return false.
138 return false;
139}
140
141bool Process::StandardErrIsDisplayed() {
142#if HAVE_ISATTY
143 return isatty(2);
144#endif
145 // If we don't have isatty, just return false.
146 return false;
147}
148
Reid Spencer27dafe12004-09-11 04:56:56 +0000149}
150// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab