Flesh out a page size accessor in the new API.
[oota-llvm.git] / include / llvm / Support / Process.h
1 //===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
11 /// Provides a library for accessing information about this process and other
12 /// processes on the operating system. Also provides means of spawning
13 /// subprocess for commands. The design of this library is modeled after the
14 /// proposed design of the Boost.Process library, and is design specifically to
15 /// follow the style of standard libraries and potentially become a proposal
16 /// for a standard library.
17 ///
18 /// This file declares the llvm::sys::Process class which contains a collection
19 /// of legacy static interfaces for extracting various information about the
20 /// current process. The goal is to migrate users of this API over to the new
21 /// interfaces.
22 ///
23 //===----------------------------------------------------------------------===//
24
25 #ifndef LLVM_SYSTEM_PROCESS_H
26 #define LLVM_SYSTEM_PROCESS_H
27
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/Support/DataTypes.h"
30 #include "llvm/Support/TimeValue.h"
31
32 namespace llvm {
33 namespace sys {
34
35 class self_process;
36
37 /// \brief Generic base class which exposes information about an operating
38 /// system process.
39 ///
40 /// This base class is the core interface behind any OS process. It exposes
41 /// methods to query for generic information about a particular process.
42 ///
43 /// Subclasses implement this interface based on the mechanisms available, and
44 /// can optionally expose more interfaces unique to certain process kinds.
45 class process {
46 protected:
47   /// \brief Only specific subclasses of process objects can be destroyed.
48   virtual ~process();
49
50 public:
51   /// \brief Operating system specific type to identify a process.
52   ///
53   /// Note that the windows one is defined to 'void *' as this is the
54   /// documented type for HANDLE on windows, and we don't want to pull in the
55   /// Windows headers here.
56 #if defined(LLVM_ON_UNIX)
57   typedef pid_t id_type;
58 #elif defined(LLVM_ON_WIN32)
59   typedef void *id_type; // Must match the type of HANDLE.
60 #else
61 #error Unsupported operating system.
62 #endif
63
64   /// \brief Get the operating system specific identifier for this process.
65   virtual id_type get_id() = 0;
66
67
68   /// \name Static factory routines for processes.
69   /// @{
70
71   /// \brief Get the process object for the current process.
72   static self_process *get_self();
73
74   /// @}
75
76 };
77
78 /// \brief The specific class representing the current process.
79 ///
80 /// The current process can both specialize the implementation of the routines
81 /// and can expose certain information not available for other OS processes.
82 class self_process : public process {
83   friend class process;
84
85   /// \brief Private destructor, as users shouldn't create objects of this
86   /// type.
87   virtual ~self_process();
88
89 public:
90   virtual id_type get_id();
91
92   /// \name Process configuration (sysconf on POSIX)
93   /// @{
94
95   /// \brief Get the virtual memory page size.
96   ///
97   /// Query the operating system for this process's page size.
98   size_t page_size() const { return PageSize; };
99
100   /// @}
101
102 private:
103   /// \name Cached process state.
104   /// @{
105
106   /// \brief Cached page size, this cannot vary during the life of the process.
107   size_t PageSize;
108
109   /// @}
110
111   /// \brief Constructor, used by \c process::get_self() only.
112   self_process();
113 };
114
115
116 /// \brief A collection of legacy interfaces for querying information about the
117 /// current executing process.
118 class Process {
119 public:
120   /// \brief Get the virtual memory page size
121   /// This static function will return the operating system's virtual memory
122   /// page size.
123   /// \returns The number of bytes in a virtual memory page.
124   static unsigned GetPageSize();
125
126   /// \brief Return process memory usage.
127   /// This static function will return the total amount of memory allocated
128   /// by the process. This only counts the memory allocated via the malloc,
129   /// calloc and realloc functions and includes any "free" holes in the
130   /// allocated space.
131   static size_t GetMallocUsage();
132
133   /// This static function will set \p user_time to the amount of CPU time
134   /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
135   /// time spent in system (kernel) mode.  If the operating system does not
136   /// support collection of these metrics, a zero TimeValue will be for both
137   /// values.
138   /// \param elapsed Returns the TimeValue::now() giving current time
139   /// \param user_time Returns the current amount of user time for the process
140   /// \param sys_time Returns the current amount of system time for the process
141   static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
142                            TimeValue &sys_time);
143
144   /// This static function will return the process' current user id number.
145   /// Not all operating systems support this feature. Where it is not
146   /// supported, the function should return 65536 as the value.
147   static int GetCurrentUserId();
148
149   /// This static function will return the process' current group id number.
150   /// Not all operating systems support this feature. Where it is not
151   /// supported, the function should return 65536 as the value.
152   static int GetCurrentGroupId();
153
154   /// This function makes the necessary calls to the operating system to
155   /// prevent core files or any other kind of large memory dumps that can
156   /// occur when a program fails.
157   /// @brief Prevent core file generation.
158   static void PreventCoreFiles();
159
160   /// This function determines if the standard input is connected directly
161   /// to a user's input (keyboard probably), rather than coming from a file
162   /// or pipe.
163   static bool StandardInIsUserInput();
164
165   /// This function determines if the standard output is connected to a
166   /// "tty" or "console" window. That is, the output would be displayed to
167   /// the user rather than being put on a pipe or stored in a file.
168   static bool StandardOutIsDisplayed();
169
170   /// This function determines if the standard error is connected to a
171   /// "tty" or "console" window. That is, the output would be displayed to
172   /// the user rather than being put on a pipe or stored in a file.
173   static bool StandardErrIsDisplayed();
174
175   /// This function determines if the given file descriptor is connected to
176   /// a "tty" or "console" window. That is, the output would be displayed to
177   /// the user rather than being put on a pipe or stored in a file.
178   static bool FileDescriptorIsDisplayed(int fd);
179
180   /// This function determines if the given file descriptor is displayd and
181   /// supports colors.
182   static bool FileDescriptorHasColors(int fd);
183
184   /// This function determines the number of columns in the window
185   /// if standard output is connected to a "tty" or "console"
186   /// window. If standard output is not connected to a tty or
187   /// console, or if the number of columns cannot be determined,
188   /// this routine returns zero.
189   static unsigned StandardOutColumns();
190
191   /// This function determines the number of columns in the window
192   /// if standard error is connected to a "tty" or "console"
193   /// window. If standard error is not connected to a tty or
194   /// console, or if the number of columns cannot be determined,
195   /// this routine returns zero.
196   static unsigned StandardErrColumns();
197
198   /// This function determines whether the terminal connected to standard
199   /// output supports colors. If standard output is not connected to a
200   /// terminal, this function returns false.
201   static bool StandardOutHasColors();
202
203   /// This function determines whether the terminal connected to standard
204   /// error supports colors. If standard error is not connected to a
205   /// terminal, this function returns false.
206   static bool StandardErrHasColors();
207
208   /// Whether changing colors requires the output to be flushed.
209   /// This is needed on systems that don't support escape sequences for
210   /// changing colors.
211   static bool ColorNeedsFlush();
212
213   /// This function returns the colorcode escape sequences.
214   /// If ColorNeedsFlush() is true then this function will change the colors
215   /// and return an empty escape sequence. In that case it is the
216   /// responsibility of the client to flush the output stream prior to
217   /// calling this function.
218   static const char *OutputColor(char c, bool bold, bool bg);
219
220   /// Same as OutputColor, but only enables the bold attribute.
221   static const char *OutputBold(bool bg);
222
223   /// This function returns the escape sequence to reverse forground and
224   /// background colors.
225   static const char *OutputReverse();
226
227   /// Resets the terminals colors, or returns an escape sequence to do so.
228   static const char *ResetColor();
229
230   /// Get the result of a process wide random number generator. The
231   /// generator will be automatically seeded in non-deterministic fashion.
232   static unsigned GetRandomNumber();
233 };
234
235 }
236 }
237
238 #endif