1239462Sdim//===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9249423Sdim/// \file
10249423Sdim///
11249423Sdim/// Provides a library for accessing information about this process and other
12249423Sdim/// processes on the operating system. Also provides means of spawning
13249423Sdim/// subprocess for commands. The design of this library is modeled after the
14249423Sdim/// proposed design of the Boost.Process library, and is design specifically to
15249423Sdim/// follow the style of standard libraries and potentially become a proposal
16249423Sdim/// for a standard library.
17249423Sdim///
18249423Sdim/// This file declares the llvm::sys::Process class which contains a collection
19249423Sdim/// of legacy static interfaces for extracting various information about the
20249423Sdim/// current process. The goal is to migrate users of this API over to the new
21249423Sdim/// interfaces.
22249423Sdim///
23218885Sdim//===----------------------------------------------------------------------===//
24218885Sdim
25249423Sdim#ifndef LLVM_SUPPORT_PROCESS_H
26249423Sdim#define LLVM_SUPPORT_PROCESS_H
27218885Sdim
28263508Sdim#include "llvm/ADT/ArrayRef.h"
29263508Sdim#include "llvm/ADT/Optional.h"
30249423Sdim#include "llvm/Config/llvm-config.h"
31263508Sdim#include "llvm/Support/Allocator.h"
32263508Sdim#include "llvm/Support/system_error.h"
33249423Sdim#include "llvm/Support/DataTypes.h"
34218885Sdim#include "llvm/Support/TimeValue.h"
35218885Sdim
36218885Sdimnamespace llvm {
37263508Sdimclass StringRef;
38263508Sdim
39218885Sdimnamespace sys {
40218885Sdim
41249423Sdimclass self_process;
42218885Sdim
43249423Sdim/// \brief Generic base class which exposes information about an operating
44249423Sdim/// system process.
45249423Sdim///
46249423Sdim/// This base class is the core interface behind any OS process. It exposes
47249423Sdim/// methods to query for generic information about a particular process.
48249423Sdim///
49249423Sdim/// Subclasses implement this interface based on the mechanisms available, and
50249423Sdim/// can optionally expose more interfaces unique to certain process kinds.
51249423Sdimclass process {
52249423Sdimprotected:
53249423Sdim  /// \brief Only specific subclasses of process objects can be destroyed.
54249423Sdim  virtual ~process();
55218885Sdim
56249423Sdimpublic:
57249423Sdim  /// \brief Operating system specific type to identify a process.
58249423Sdim  ///
59263508Sdim  /// Note that the windows one is defined to 'unsigned long' as this is the
60263508Sdim  /// documented type for DWORD on windows, and we don't want to pull in the
61249423Sdim  /// Windows headers here.
62249423Sdim#if defined(LLVM_ON_UNIX)
63249423Sdim  typedef pid_t id_type;
64249423Sdim#elif defined(LLVM_ON_WIN32)
65263508Sdim  typedef unsigned long id_type; // Must match the type of DWORD.
66249423Sdim#else
67249423Sdim#error Unsupported operating system.
68249423Sdim#endif
69218885Sdim
70249423Sdim  /// \brief Get the operating system specific identifier for this process.
71249423Sdim  virtual id_type get_id() = 0;
72218885Sdim
73249423Sdim  /// \brief Get the user time consumed by this process.
74249423Sdim  ///
75249423Sdim  /// Note that this is often an approximation and may be zero on platforms
76249423Sdim  /// where we don't have good support for the functionality.
77249423Sdim  virtual TimeValue get_user_time() const = 0;
78218885Sdim
79249423Sdim  /// \brief Get the system time consumed by this process.
80249423Sdim  ///
81249423Sdim  /// Note that this is often an approximation and may be zero on platforms
82249423Sdim  /// where we don't have good support for the functionality.
83249423Sdim  virtual TimeValue get_system_time() const = 0;
84218885Sdim
85249423Sdim  /// \brief Get the wall time consumed by this process.
86249423Sdim  ///
87249423Sdim  /// Note that this is often an approximation and may be zero on platforms
88249423Sdim  /// where we don't have good support for the functionality.
89249423Sdim  virtual TimeValue get_wall_time() const = 0;
90218885Sdim
91249423Sdim  /// \name Static factory routines for processes.
92249423Sdim  /// @{
93218885Sdim
94249423Sdim  /// \brief Get the process object for the current process.
95249423Sdim  static self_process *get_self();
96218885Sdim
97249423Sdim  /// @}
98218885Sdim
99249423Sdim};
100218885Sdim
101249423Sdim/// \brief The specific class representing the current process.
102249423Sdim///
103249423Sdim/// The current process can both specialize the implementation of the routines
104249423Sdim/// and can expose certain information not available for other OS processes.
105249423Sdimclass self_process : public process {
106249423Sdim  friend class process;
107239462Sdim
108249423Sdim  /// \brief Private destructor, as users shouldn't create objects of this
109249423Sdim  /// type.
110249423Sdim  virtual ~self_process();
111218885Sdim
112249423Sdimpublic:
113249423Sdim  virtual id_type get_id();
114249423Sdim  virtual TimeValue get_user_time() const;
115249423Sdim  virtual TimeValue get_system_time() const;
116249423Sdim  virtual TimeValue get_wall_time() const;
117218885Sdim
118249423Sdim  /// \name Process configuration (sysconf on POSIX)
119249423Sdim  /// @{
120218885Sdim
121249423Sdim  /// \brief Get the virtual memory page size.
122249423Sdim  ///
123249423Sdim  /// Query the operating system for this process's page size.
124249423Sdim  size_t page_size() const { return PageSize; };
125218885Sdim
126249423Sdim  /// @}
127218885Sdim
128249423Sdimprivate:
129249423Sdim  /// \name Cached process state.
130249423Sdim  /// @{
131218885Sdim
132249423Sdim  /// \brief Cached page size, this cannot vary during the life of the process.
133249423Sdim  size_t PageSize;
134218885Sdim
135249423Sdim  /// @}
136234982Sdim
137249423Sdim  /// \brief Constructor, used by \c process::get_self() only.
138249423Sdim  self_process();
139249423Sdim};
140239462Sdim
141249423Sdim
142249423Sdim/// \brief A collection of legacy interfaces for querying information about the
143249423Sdim/// current executing process.
144249423Sdimclass Process {
145249423Sdimpublic:
146249423Sdim  /// \brief Return process memory usage.
147249423Sdim  /// This static function will return the total amount of memory allocated
148249423Sdim  /// by the process. This only counts the memory allocated via the malloc,
149249423Sdim  /// calloc and realloc functions and includes any "free" holes in the
150249423Sdim  /// allocated space.
151249423Sdim  static size_t GetMallocUsage();
152249423Sdim
153249423Sdim  /// This static function will set \p user_time to the amount of CPU time
154249423Sdim  /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
155249423Sdim  /// time spent in system (kernel) mode.  If the operating system does not
156249423Sdim  /// support collection of these metrics, a zero TimeValue will be for both
157249423Sdim  /// values.
158249423Sdim  /// \param elapsed Returns the TimeValue::now() giving current time
159249423Sdim  /// \param user_time Returns the current amount of user time for the process
160249423Sdim  /// \param sys_time Returns the current amount of system time for the process
161249423Sdim  static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
162249423Sdim                           TimeValue &sys_time);
163249423Sdim
164249423Sdim  /// This function makes the necessary calls to the operating system to
165249423Sdim  /// prevent core files or any other kind of large memory dumps that can
166249423Sdim  /// occur when a program fails.
167249423Sdim  /// @brief Prevent core file generation.
168249423Sdim  static void PreventCoreFiles();
169249423Sdim
170263508Sdim  // This function returns the environment variable \arg name's value as a UTF-8
171263508Sdim  // string. \arg Name is assumed to be in UTF-8 encoding too.
172263508Sdim  static Optional<std::string> GetEnv(StringRef name);
173263508Sdim
174263508Sdim  /// This function returns a SmallVector containing the arguments passed from
175263508Sdim  /// the operating system to the program.  This function expects to be handed
176263508Sdim  /// the vector passed in from main.
177263508Sdim  static error_code
178263508Sdim  GetArgumentVector(SmallVectorImpl<const char *> &Args,
179263508Sdim                    ArrayRef<const char *> ArgsFromMain,
180263508Sdim                    SpecificBumpPtrAllocator<char> &ArgAllocator);
181263508Sdim
182249423Sdim  /// This function determines if the standard input is connected directly
183249423Sdim  /// to a user's input (keyboard probably), rather than coming from a file
184249423Sdim  /// or pipe.
185249423Sdim  static bool StandardInIsUserInput();
186249423Sdim
187249423Sdim  /// This function determines if the standard output is connected to a
188249423Sdim  /// "tty" or "console" window. That is, the output would be displayed to
189249423Sdim  /// the user rather than being put on a pipe or stored in a file.
190249423Sdim  static bool StandardOutIsDisplayed();
191249423Sdim
192249423Sdim  /// This function determines if the standard error is connected to a
193249423Sdim  /// "tty" or "console" window. That is, the output would be displayed to
194249423Sdim  /// the user rather than being put on a pipe or stored in a file.
195249423Sdim  static bool StandardErrIsDisplayed();
196249423Sdim
197249423Sdim  /// This function determines if the given file descriptor is connected to
198249423Sdim  /// a "tty" or "console" window. That is, the output would be displayed to
199249423Sdim  /// the user rather than being put on a pipe or stored in a file.
200249423Sdim  static bool FileDescriptorIsDisplayed(int fd);
201249423Sdim
202249423Sdim  /// This function determines if the given file descriptor is displayd and
203249423Sdim  /// supports colors.
204249423Sdim  static bool FileDescriptorHasColors(int fd);
205249423Sdim
206249423Sdim  /// This function determines the number of columns in the window
207249423Sdim  /// if standard output is connected to a "tty" or "console"
208249423Sdim  /// window. If standard output is not connected to a tty or
209249423Sdim  /// console, or if the number of columns cannot be determined,
210249423Sdim  /// this routine returns zero.
211249423Sdim  static unsigned StandardOutColumns();
212249423Sdim
213249423Sdim  /// This function determines the number of columns in the window
214249423Sdim  /// if standard error is connected to a "tty" or "console"
215249423Sdim  /// window. If standard error is not connected to a tty or
216249423Sdim  /// console, or if the number of columns cannot be determined,
217249423Sdim  /// this routine returns zero.
218249423Sdim  static unsigned StandardErrColumns();
219249423Sdim
220249423Sdim  /// This function determines whether the terminal connected to standard
221249423Sdim  /// output supports colors. If standard output is not connected to a
222249423Sdim  /// terminal, this function returns false.
223249423Sdim  static bool StandardOutHasColors();
224249423Sdim
225249423Sdim  /// This function determines whether the terminal connected to standard
226249423Sdim  /// error supports colors. If standard error is not connected to a
227249423Sdim  /// terminal, this function returns false.
228249423Sdim  static bool StandardErrHasColors();
229249423Sdim
230263508Sdim  /// Enables or disables whether ANSI escape sequences are used to output
231263508Sdim  /// colors. This only has an effect on Windows.
232263508Sdim  /// Note: Setting this option is not thread-safe and should only be done
233263508Sdim  /// during initialization.
234263508Sdim  static void UseANSIEscapeCodes(bool enable);
235263508Sdim
236249423Sdim  /// Whether changing colors requires the output to be flushed.
237249423Sdim  /// This is needed on systems that don't support escape sequences for
238249423Sdim  /// changing colors.
239249423Sdim  static bool ColorNeedsFlush();
240249423Sdim
241249423Sdim  /// This function returns the colorcode escape sequences.
242249423Sdim  /// If ColorNeedsFlush() is true then this function will change the colors
243249423Sdim  /// and return an empty escape sequence. In that case it is the
244249423Sdim  /// responsibility of the client to flush the output stream prior to
245249423Sdim  /// calling this function.
246249423Sdim  static const char *OutputColor(char c, bool bold, bool bg);
247249423Sdim
248249423Sdim  /// Same as OutputColor, but only enables the bold attribute.
249249423Sdim  static const char *OutputBold(bool bg);
250249423Sdim
251249423Sdim  /// This function returns the escape sequence to reverse forground and
252249423Sdim  /// background colors.
253249423Sdim  static const char *OutputReverse();
254249423Sdim
255249423Sdim  /// Resets the terminals colors, or returns an escape sequence to do so.
256249423Sdim  static const char *ResetColor();
257249423Sdim
258249423Sdim  /// Get the result of a process wide random number generator. The
259249423Sdim  /// generator will be automatically seeded in non-deterministic fashion.
260249423Sdim  static unsigned GetRandomNumber();
261249423Sdim};
262249423Sdim
263218885Sdim}
264218885Sdim}
265218885Sdim
266218885Sdim#endif
267