Re-sort all of the includes with ./utils/sort_includes.py so that
[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_SUPPORT_PROCESS_H
26 #define LLVM_SUPPORT_PROCESS_H
27
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/Optional.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/Support/Allocator.h"
32 #include "llvm/Support/DataTypes.h"
33 #include "llvm/Support/TimeValue.h"
34 #include "llvm/Support/system_error.h"
35
36 namespace llvm {
37 class StringRef;
38
39 namespace sys {
40
41 class self_process;
42
43 /// \brief Generic base class which exposes information about an operating
44 /// system process.
45 ///
46 /// This base class is the core interface behind any OS process. It exposes
47 /// methods to query for generic information about a particular process.
48 ///
49 /// Subclasses implement this interface based on the mechanisms available, and
50 /// can optionally expose more interfaces unique to certain process kinds.
51 class process {
52 protected:
53   /// \brief Only specific subclasses of process objects can be destroyed.
54   virtual ~process();
55
56 public:
57   /// \brief Operating system specific type to identify a process.
58   ///
59   /// Note that the windows one is defined to 'unsigned long' as this is the
60   /// documented type for DWORD on windows, and we don't want to pull in the
61   /// Windows headers here.
62 #if defined(LLVM_ON_UNIX)
63   typedef pid_t id_type;
64 #elif defined(LLVM_ON_WIN32)
65   typedef unsigned long id_type; // Must match the type of DWORD.
66 #else
67 #error Unsupported operating system.
68 #endif
69
70   /// \brief Get the operating system specific identifier for this process.
71   virtual id_type get_id() = 0;
72
73   /// \brief Get the user time consumed by this process.
74   ///
75   /// Note that this is often an approximation and may be zero on platforms
76   /// where we don't have good support for the functionality.
77   virtual TimeValue get_user_time() const = 0;
78
79   /// \brief Get the system time consumed by this process.
80   ///
81   /// Note that this is often an approximation and may be zero on platforms
82   /// where we don't have good support for the functionality.
83   virtual TimeValue get_system_time() const = 0;
84
85   /// \brief Get the wall time consumed by this process.
86   ///
87   /// Note that this is often an approximation and may be zero on platforms
88   /// where we don't have good support for the functionality.
89   virtual TimeValue get_wall_time() const = 0;
90
91   /// \name Static factory routines for processes.
92   /// @{
93
94   /// \brief Get the process object for the current process.
95   static self_process *get_self();
96
97   /// @}
98
99 };
100
101 /// \brief The specific class representing the current process.
102 ///
103 /// The current process can both specialize the implementation of the routines
104 /// and can expose certain information not available for other OS processes.
105 class self_process : public process {
106   friend class process;
107
108   /// \brief Private destructor, as users shouldn't create objects of this
109   /// type.
110   virtual ~self_process();
111
112 public:
113   virtual id_type get_id();
114   virtual TimeValue get_user_time() const;
115   virtual TimeValue get_system_time() const;
116   virtual TimeValue get_wall_time() const;
117
118   /// \name Process configuration (sysconf on POSIX)
119   /// @{
120
121   /// \brief Get the virtual memory page size.
122   ///
123   /// Query the operating system for this process's page size.
124   size_t page_size() const { return PageSize; };
125
126   /// @}
127
128 private:
129   /// \name Cached process state.
130   /// @{
131
132   /// \brief Cached page size, this cannot vary during the life of the process.
133   size_t PageSize;
134
135   /// @}
136
137   /// \brief Constructor, used by \c process::get_self() only.
138   self_process();
139 };
140
141
142 /// \brief A collection of legacy interfaces for querying information about the
143 /// current executing process.
144 class Process {
145 public:
146   /// \brief Return process memory usage.
147   /// This static function will return the total amount of memory allocated
148   /// by the process. This only counts the memory allocated via the malloc,
149   /// calloc and realloc functions and includes any "free" holes in the
150   /// allocated space.
151   static size_t GetMallocUsage();
152
153   /// This static function will set \p user_time to the amount of CPU time
154   /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
155   /// time spent in system (kernel) mode.  If the operating system does not
156   /// support collection of these metrics, a zero TimeValue will be for both
157   /// values.
158   /// \param elapsed Returns the TimeValue::now() giving current time
159   /// \param user_time Returns the current amount of user time for the process
160   /// \param sys_time Returns the current amount of system time for the process
161   static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
162                            TimeValue &sys_time);
163
164   /// This function makes the necessary calls to the operating system to
165   /// prevent core files or any other kind of large memory dumps that can
166   /// occur when a program fails.
167   /// @brief Prevent core file generation.
168   static void PreventCoreFiles();
169
170   // This function returns the environment variable \arg name's value as a UTF-8
171   // string. \arg Name is assumed to be in UTF-8 encoding too.
172   static Optional<std::string> GetEnv(StringRef name);
173
174   /// This function returns a SmallVector containing the arguments passed from
175   /// the operating system to the program.  This function expects to be handed
176   /// the vector passed in from main.
177   static error_code
178   GetArgumentVector(SmallVectorImpl<const char *> &Args,
179                     ArrayRef<const char *> ArgsFromMain,
180                     SpecificBumpPtrAllocator<char> &ArgAllocator);
181
182   /// This function determines if the standard input is connected directly
183   /// to a user's input (keyboard probably), rather than coming from a file
184   /// or pipe.
185   static bool StandardInIsUserInput();
186
187   /// This function determines if the standard output is connected to a
188   /// "tty" or "console" window. That is, the output would be displayed to
189   /// the user rather than being put on a pipe or stored in a file.
190   static bool StandardOutIsDisplayed();
191
192   /// This function determines if the standard error is connected to a
193   /// "tty" or "console" window. That is, the output would be displayed to
194   /// the user rather than being put on a pipe or stored in a file.
195   static bool StandardErrIsDisplayed();
196
197   /// This function determines if the given file descriptor is connected to
198   /// a "tty" or "console" window. That is, the output would be displayed to
199   /// the user rather than being put on a pipe or stored in a file.
200   static bool FileDescriptorIsDisplayed(int fd);
201
202   /// This function determines if the given file descriptor is displayd and
203   /// supports colors.
204   static bool FileDescriptorHasColors(int fd);
205
206   /// This function determines the number of columns in the window
207   /// if standard output is connected to a "tty" or "console"
208   /// window. If standard output is not connected to a tty or
209   /// console, or if the number of columns cannot be determined,
210   /// this routine returns zero.
211   static unsigned StandardOutColumns();
212
213   /// This function determines the number of columns in the window
214   /// if standard error is connected to a "tty" or "console"
215   /// window. If standard error is not connected to a tty or
216   /// console, or if the number of columns cannot be determined,
217   /// this routine returns zero.
218   static unsigned StandardErrColumns();
219
220   /// This function determines whether the terminal connected to standard
221   /// output supports colors. If standard output is not connected to a
222   /// terminal, this function returns false.
223   static bool StandardOutHasColors();
224
225   /// This function determines whether the terminal connected to standard
226   /// error supports colors. If standard error is not connected to a
227   /// terminal, this function returns false.
228   static bool StandardErrHasColors();
229
230   /// Enables or disables whether ANSI escape sequences are used to output
231   /// colors. This only has an effect on Windows.
232   /// Note: Setting this option is not thread-safe and should only be done
233   /// during initialization.
234   static void UseANSIEscapeCodes(bool enable);
235
236   /// Whether changing colors requires the output to be flushed.
237   /// This is needed on systems that don't support escape sequences for
238   /// changing colors.
239   static bool ColorNeedsFlush();
240
241   /// This function returns the colorcode escape sequences.
242   /// If ColorNeedsFlush() is true then this function will change the colors
243   /// and return an empty escape sequence. In that case it is the
244   /// responsibility of the client to flush the output stream prior to
245   /// calling this function.
246   static const char *OutputColor(char c, bool bold, bool bg);
247
248   /// Same as OutputColor, but only enables the bold attribute.
249   static const char *OutputBold(bool bg);
250
251   /// This function returns the escape sequence to reverse forground and
252   /// background colors.
253   static const char *OutputReverse();
254
255   /// Resets the terminals colors, or returns an escape sequence to do so.
256   static const char *ResetColor();
257
258   /// Get the result of a process wide random number generator. The
259   /// generator will be automatically seeded in non-deterministic fashion.
260   static unsigned GetRandomNumber();
261 };
262
263 }
264 }
265
266 #endif