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