Provide an implementation of the GetCurrentUserId and GetCurrentGroupId
[oota-llvm.git] / lib / System / Unix / Process.inc
1 //===- 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
14 #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
24
25 //===----------------------------------------------------------------------===//
26 //=== WARNING: Implementation here must contain only generic UNIX code that
27 //===          is guaranteed to work on *all* UNIX variants.
28 //===----------------------------------------------------------------------===//
29
30 namespace llvm {
31 using namespace sys;
32
33 unsigned 
34 Process::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
43   return static_cast<unsigned>(page_size);
44 }
45
46 #if defined(HAVE_SBRK)
47 static char* som = reinterpret_cast<char*>(::sbrk(0));
48 #endif
49
50 size_t 
51 Process::GetMallocUsage()
52 {
53 #if defined(HAVE_MALLINFO)
54   struct mallinfo mi;
55   mi = ::mallinfo();
56   return mi.uordblks;
57 #elif defined(HAVE_SBRK)
58   // Note this is only an approximation and more closely resembles
59   // the value returned by mallinfo in the arena field.
60   char * eom = (char*) sbrk(0);
61   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
71 size_t
72 Process::GetTotalMemoryUsage()
73 {
74 #if defined(HAVE_MALLINFO)
75   struct mallinfo mi = ::mallinfo();
76   return mi.uordblks + mi.hblkhd;
77 #elif defined(HAVE_GETRUSAGE)
78   struct rusage usage;
79   ::getrusage(RUSAGE_SELF, &usage);
80   return usage.ru_maxrss;
81 #else
82 #warning Cannot get total memory size on this platform
83   return 0;
84 #endif
85 }
86
87 void
88 Process::GetTimeUsage(TimeValue& elapsed, TimeValue& user_time, 
89                       TimeValue& sys_time)
90 {
91   elapsed = TimeValue::now();
92 #if defined(HAVE_GETRUSAGE)
93   struct rusage usage;
94   ::getrusage(RUSAGE_SELF, &usage);
95   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 ) );
103 #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
112 int Process::GetCurrentUserId()
113 {
114   return getuid();
115 }
116
117 int Process::GetCurrentGroupId()
118 {
119   return getgid();
120 }
121
122 // Some LLVM programs such as bugpoint produce core files as a normal part of
123 // their operation. To prevent the disk from filling up, this function
124 // does what's necessary to prevent their generation.
125 void Process::PreventCoreFiles() {
126 #if HAVE_SETRLIMIT
127   struct rlimit rlim;
128   rlim.rlim_cur = rlim.rlim_max = 0;
129   int res = setrlimit(RLIMIT_CORE, &rlim);
130   if (res != 0)
131     ThrowErrno("Can't prevent core file generation");
132 #endif
133 }
134
135 bool Process::StandardInIsUserInput() {
136 #if HAVE_ISATTY
137   return isatty(0);
138 #endif
139   // If we don't have isatty, just return false.
140   return false;
141 }
142
143 bool Process::StandardOutIsDisplayed() {
144 #if HAVE_ISATTY
145   return isatty(1);
146 #endif
147   // If we don't have isatty, just return false.
148   return false;
149 }
150
151 bool Process::StandardErrIsDisplayed() {
152 #if HAVE_ISATTY
153   return isatty(2);
154 #endif
155   // If we don't have isatty, just return false.
156   return false;
157 }
158
159 }
160 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab