]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/include/llvm/Support/Process.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / 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/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   /// \brief Get the user time consumed by this process.
68   ///
69   /// Note that this is often an approximation and may be zero on platforms
70   /// where we don't have good support for the functionality.
71   virtual TimeValue get_user_time() const = 0;
72
73   /// \brief Get the system 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_system_time() const = 0;
78
79   /// \brief Get the wall 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_wall_time() const = 0;
84
85   /// \name Static factory routines for processes.
86   /// @{
87
88   /// \brief Get the process object for the current process.
89   static self_process *get_self();
90
91   /// @}
92
93 };
94
95 /// \brief The specific class representing the current process.
96 ///
97 /// The current process can both specialize the implementation of the routines
98 /// and can expose certain information not available for other OS processes.
99 class self_process : public process {
100   friend class process;
101
102   /// \brief Private destructor, as users shouldn't create objects of this
103   /// type.
104   virtual ~self_process();
105
106 public:
107   virtual id_type get_id();
108   virtual TimeValue get_user_time() const;
109   virtual TimeValue get_system_time() const;
110   virtual TimeValue get_wall_time() const;
111
112   /// \name Process configuration (sysconf on POSIX)
113   /// @{
114
115   /// \brief Get the virtual memory page size.
116   ///
117   /// Query the operating system for this process's page size.
118   size_t page_size() const { return PageSize; };
119
120   /// @}
121
122 private:
123   /// \name Cached process state.
124   /// @{
125
126   /// \brief Cached page size, this cannot vary during the life of the process.
127   size_t PageSize;
128
129   /// @}
130
131   /// \brief Constructor, used by \c process::get_self() only.
132   self_process();
133 };
134
135
136 /// \brief A collection of legacy interfaces for querying information about the
137 /// current executing process.
138 class Process {
139 public:
140   /// \brief Return process memory usage.
141   /// This static function will return the total amount of memory allocated
142   /// by the process. This only counts the memory allocated via the malloc,
143   /// calloc and realloc functions and includes any "free" holes in the
144   /// allocated space.
145   static size_t GetMallocUsage();
146
147   /// This static function will set \p user_time to the amount of CPU time
148   /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
149   /// time spent in system (kernel) mode.  If the operating system does not
150   /// support collection of these metrics, a zero TimeValue will be for both
151   /// values.
152   /// \param elapsed Returns the TimeValue::now() giving current time
153   /// \param user_time Returns the current amount of user time for the process
154   /// \param sys_time Returns the current amount of system time for the process
155   static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
156                            TimeValue &sys_time);
157
158   /// This static function will return the process' current user id number.
159   /// Not all operating systems support this feature. Where it is not
160   /// supported, the function should return 65536 as the value.
161   static int GetCurrentUserId();
162
163   /// This static function will return the process' current group id number.
164   /// Not all operating systems support this feature. Where it is not
165   /// supported, the function should return 65536 as the value.
166   static int GetCurrentGroupId();
167
168   /// This function makes the necessary calls to the operating system to
169   /// prevent core files or any other kind of large memory dumps that can
170   /// occur when a program fails.
171   /// @brief Prevent core file generation.
172   static void PreventCoreFiles();
173
174   /// This function determines if the standard input is connected directly
175   /// to a user's input (keyboard probably), rather than coming from a file
176   /// or pipe.
177   static bool StandardInIsUserInput();
178
179   /// This function determines if the standard output is connected to a
180   /// "tty" or "console" window. That is, the output would be displayed to
181   /// the user rather than being put on a pipe or stored in a file.
182   static bool StandardOutIsDisplayed();
183
184   /// This function determines if the standard error is connected to a
185   /// "tty" or "console" window. That is, the output would be displayed to
186   /// the user rather than being put on a pipe or stored in a file.
187   static bool StandardErrIsDisplayed();
188
189   /// This function determines if the given file descriptor is connected to
190   /// a "tty" or "console" window. That is, the output would be displayed to
191   /// the user rather than being put on a pipe or stored in a file.
192   static bool FileDescriptorIsDisplayed(int fd);
193
194   /// This function determines if the given file descriptor is displayd and
195   /// supports colors.
196   static bool FileDescriptorHasColors(int fd);
197
198   /// This function determines the number of columns in the window
199   /// if standard output is connected to a "tty" or "console"
200   /// window. If standard output is not connected to a tty or
201   /// console, or if the number of columns cannot be determined,
202   /// this routine returns zero.
203   static unsigned StandardOutColumns();
204
205   /// This function determines the number of columns in the window
206   /// if standard error is connected to a "tty" or "console"
207   /// window. If standard error is not connected to a tty or
208   /// console, or if the number of columns cannot be determined,
209   /// this routine returns zero.
210   static unsigned StandardErrColumns();
211
212   /// This function determines whether the terminal connected to standard
213   /// output supports colors. If standard output is not connected to a
214   /// terminal, this function returns false.
215   static bool StandardOutHasColors();
216
217   /// This function determines whether the terminal connected to standard
218   /// error supports colors. If standard error is not connected to a
219   /// terminal, this function returns false.
220   static bool StandardErrHasColors();
221
222   /// Whether changing colors requires the output to be flushed.
223   /// This is needed on systems that don't support escape sequences for
224   /// changing colors.
225   static bool ColorNeedsFlush();
226
227   /// This function returns the colorcode escape sequences.
228   /// If ColorNeedsFlush() is true then this function will change the colors
229   /// and return an empty escape sequence. In that case it is the
230   /// responsibility of the client to flush the output stream prior to
231   /// calling this function.
232   static const char *OutputColor(char c, bool bold, bool bg);
233
234   /// Same as OutputColor, but only enables the bold attribute.
235   static const char *OutputBold(bool bg);
236
237   /// This function returns the escape sequence to reverse forground and
238   /// background colors.
239   static const char *OutputReverse();
240
241   /// Resets the terminals colors, or returns an escape sequence to do so.
242   static const char *ResetColor();
243
244   /// Get the result of a process wide random number generator. The
245   /// generator will be automatically seeded in non-deterministic fashion.
246   static unsigned GetRandomNumber();
247 };
248
249 }
250 }
251
252 #endif