1245431Sdim//===- 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//===----------------------------------------------------------------------===//
9252723Sdim/// \file
10252723Sdim///
11252723Sdim/// Provides a library for accessing information about this process and other
12252723Sdim/// processes on the operating system. Also provides means of spawning
13252723Sdim/// subprocess for commands. The design of this library is modeled after the
14252723Sdim/// proposed design of the Boost.Process library, and is design specifically to
15252723Sdim/// follow the style of standard libraries and potentially become a proposal
16252723Sdim/// for a standard library.
17252723Sdim///
18252723Sdim/// This file declares the llvm::sys::Process class which contains a collection
19252723Sdim/// of legacy static interfaces for extracting various information about the
20252723Sdim/// current process. The goal is to migrate users of this API over to the new
21252723Sdim/// interfaces.
22252723Sdim///
23218885Sdim//===----------------------------------------------------------------------===//
24218885Sdim
25252723Sdim#ifndef LLVM_SUPPORT_PROCESS_H
26252723Sdim#define LLVM_SUPPORT_PROCESS_H
27218885Sdim
28263509Sdim#include "llvm/ADT/ArrayRef.h"
29263509Sdim#include "llvm/ADT/Optional.h"
30252723Sdim#include "llvm/Config/llvm-config.h"
31263509Sdim#include "llvm/Support/Allocator.h"
32263509Sdim#include "llvm/Support/system_error.h"
33252723Sdim#include "llvm/Support/DataTypes.h"
34218885Sdim#include "llvm/Support/TimeValue.h"
35218885Sdim
36218885Sdimnamespace llvm {
37263509Sdimclass StringRef;
38263509Sdim
39218885Sdimnamespace sys {
40218885Sdim
41252723Sdimclass self_process;
42218885Sdim
43252723Sdim/// \brief Generic base class which exposes information about an operating
44252723Sdim/// system process.
45252723Sdim///
46252723Sdim/// This base class is the core interface behind any OS process. It exposes
47252723Sdim/// methods to query for generic information about a particular process.
48252723Sdim///
49252723Sdim/// Subclasses implement this interface based on the mechanisms available, and
50252723Sdim/// can optionally expose more interfaces unique to certain process kinds.
51252723Sdimclass process {
52252723Sdimprotected:
53252723Sdim  /// \brief Only specific subclasses of process objects can be destroyed.
54252723Sdim  virtual ~process();
55218885Sdim
56252723Sdimpublic:
57252723Sdim  /// \brief Operating system specific type to identify a process.
58252723Sdim  ///
59263509Sdim  /// Note that the windows one is defined to 'unsigned long' as this is the
60263509Sdim  /// documented type for DWORD on windows, and we don't want to pull in the
61252723Sdim  /// Windows headers here.
62252723Sdim#if defined(LLVM_ON_UNIX)
63252723Sdim  typedef pid_t id_type;
64252723Sdim#elif defined(LLVM_ON_WIN32)
65263509Sdim  typedef unsigned long id_type; // Must match the type of DWORD.
66252723Sdim#else
67252723Sdim#error Unsupported operating system.
68252723Sdim#endif
69218885Sdim
70252723Sdim  /// \brief Get the operating system specific identifier for this process.
71252723Sdim  virtual id_type get_id() = 0;
72218885Sdim
73252723Sdim  /// \brief Get the user time consumed by this process.
74252723Sdim  ///
75252723Sdim  /// Note that this is often an approximation and may be zero on platforms
76252723Sdim  /// where we don't have good support for the functionality.
77252723Sdim  virtual TimeValue get_user_time() const = 0;
78218885Sdim
79252723Sdim  /// \brief Get the system time consumed by this process.
80252723Sdim  ///
81252723Sdim  /// Note that this is often an approximation and may be zero on platforms
82252723Sdim  /// where we don't have good support for the functionality.
83252723Sdim  virtual TimeValue get_system_time() const = 0;
84218885Sdim
85252723Sdim  /// \brief Get the wall time consumed by this process.
86252723Sdim  ///
87252723Sdim  /// Note that this is often an approximation and may be zero on platforms
88252723Sdim  /// where we don't have good support for the functionality.
89252723Sdim  virtual TimeValue get_wall_time() const = 0;
90218885Sdim
91252723Sdim  /// \name Static factory routines for processes.
92252723Sdim  /// @{
93218885Sdim
94252723Sdim  /// \brief Get the process object for the current process.
95252723Sdim  static self_process *get_self();
96218885Sdim
97252723Sdim  /// @}
98218885Sdim
99252723Sdim};
100218885Sdim
101252723Sdim/// \brief The specific class representing the current process.
102252723Sdim///
103252723Sdim/// The current process can both specialize the implementation of the routines
104252723Sdim/// and can expose certain information not available for other OS processes.
105252723Sdimclass self_process : public process {
106252723Sdim  friend class process;
107245431Sdim
108252723Sdim  /// \brief Private destructor, as users shouldn't create objects of this
109252723Sdim  /// type.
110252723Sdim  virtual ~self_process();
111218885Sdim
112252723Sdimpublic:
113252723Sdim  virtual id_type get_id();
114252723Sdim  virtual TimeValue get_user_time() const;
115252723Sdim  virtual TimeValue get_system_time() const;
116252723Sdim  virtual TimeValue get_wall_time() const;
117218885Sdim
118252723Sdim  /// \name Process configuration (sysconf on POSIX)
119252723Sdim  /// @{
120218885Sdim
121252723Sdim  /// \brief Get the virtual memory page size.
122252723Sdim  ///
123252723Sdim  /// Query the operating system for this process's page size.
124252723Sdim  size_t page_size() const { return PageSize; };
125218885Sdim
126252723Sdim  /// @}
127218885Sdim
128252723Sdimprivate:
129252723Sdim  /// \name Cached process state.
130252723Sdim  /// @{
131218885Sdim
132252723Sdim  /// \brief Cached page size, this cannot vary during the life of the process.
133252723Sdim  size_t PageSize;
134218885Sdim
135252723Sdim  /// @}
136235633Sdim
137252723Sdim  /// \brief Constructor, used by \c process::get_self() only.
138252723Sdim  self_process();
139252723Sdim};
140245431Sdim
141252723Sdim
142252723Sdim/// \brief A collection of legacy interfaces for querying information about the
143252723Sdim/// current executing process.
144252723Sdimclass Process {
145252723Sdimpublic:
146252723Sdim  /// \brief Return process memory usage.
147252723Sdim  /// This static function will return the total amount of memory allocated
148252723Sdim  /// by the process. This only counts the memory allocated via the malloc,
149252723Sdim  /// calloc and realloc functions and includes any "free" holes in the
150252723Sdim  /// allocated space.
151252723Sdim  static size_t GetMallocUsage();
152252723Sdim
153252723Sdim  /// This static function will set \p user_time to the amount of CPU time
154252723Sdim  /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
155252723Sdim  /// time spent in system (kernel) mode.  If the operating system does not
156252723Sdim  /// support collection of these metrics, a zero TimeValue will be for both
157252723Sdim  /// values.
158252723Sdim  /// \param elapsed Returns the TimeValue::now() giving current time
159252723Sdim  /// \param user_time Returns the current amount of user time for the process
160252723Sdim  /// \param sys_time Returns the current amount of system time for the process
161252723Sdim  static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
162252723Sdim                           TimeValue &sys_time);
163252723Sdim
164252723Sdim  /// This function makes the necessary calls to the operating system to
165252723Sdim  /// prevent core files or any other kind of large memory dumps that can
166252723Sdim  /// occur when a program fails.
167252723Sdim  /// @brief Prevent core file generation.
168252723Sdim  static void PreventCoreFiles();
169252723Sdim
170263509Sdim  // This function returns the environment variable \arg name's value as a UTF-8
171263509Sdim  // string. \arg Name is assumed to be in UTF-8 encoding too.
172263509Sdim  static Optional<std::string> GetEnv(StringRef name);
173263509Sdim
174263509Sdim  /// This function returns a SmallVector containing the arguments passed from
175263509Sdim  /// the operating system to the program.  This function expects to be handed
176263509Sdim  /// the vector passed in from main.
177263509Sdim  static error_code
178263509Sdim  GetArgumentVector(SmallVectorImpl<const char *> &Args,
179263509Sdim                    ArrayRef<const char *> ArgsFromMain,
180263509Sdim                    SpecificBumpPtrAllocator<char> &ArgAllocator);
181263509Sdim
182252723Sdim  /// This function determines if the standard input is connected directly
183252723Sdim  /// to a user's input (keyboard probably), rather than coming from a file
184252723Sdim  /// or pipe.
185252723Sdim  static bool StandardInIsUserInput();
186252723Sdim
187252723Sdim  /// This function determines if the standard output is connected to a
188252723Sdim  /// "tty" or "console" window. That is, the output would be displayed to
189252723Sdim  /// the user rather than being put on a pipe or stored in a file.
190252723Sdim  static bool StandardOutIsDisplayed();
191252723Sdim
192252723Sdim  /// This function determines if the standard error is connected to a
193252723Sdim  /// "tty" or "console" window. That is, the output would be displayed to
194252723Sdim  /// the user rather than being put on a pipe or stored in a file.
195252723Sdim  static bool StandardErrIsDisplayed();
196252723Sdim
197252723Sdim  /// This function determines if the given file descriptor is connected to
198252723Sdim  /// a "tty" or "console" window. That is, the output would be displayed to
199252723Sdim  /// the user rather than being put on a pipe or stored in a file.
200252723Sdim  static bool FileDescriptorIsDisplayed(int fd);
201252723Sdim
202252723Sdim  /// This function determines if the given file descriptor is displayd and
203252723Sdim  /// supports colors.
204252723Sdim  static bool FileDescriptorHasColors(int fd);
205252723Sdim
206252723Sdim  /// This function determines the number of columns in the window
207252723Sdim  /// if standard output is connected to a "tty" or "console"
208252723Sdim  /// window. If standard output is not connected to a tty or
209252723Sdim  /// console, or if the number of columns cannot be determined,
210252723Sdim  /// this routine returns zero.
211252723Sdim  static unsigned StandardOutColumns();
212252723Sdim
213252723Sdim  /// This function determines the number of columns in the window
214252723Sdim  /// if standard error is connected to a "tty" or "console"
215252723Sdim  /// window. If standard error is not connected to a tty or
216252723Sdim  /// console, or if the number of columns cannot be determined,
217252723Sdim  /// this routine returns zero.
218252723Sdim  static unsigned StandardErrColumns();
219252723Sdim
220252723Sdim  /// This function determines whether the terminal connected to standard
221252723Sdim  /// output supports colors. If standard output is not connected to a
222252723Sdim  /// terminal, this function returns false.
223252723Sdim  static bool StandardOutHasColors();
224252723Sdim
225252723Sdim  /// This function determines whether the terminal connected to standard
226252723Sdim  /// error supports colors. If standard error is not connected to a
227252723Sdim  /// terminal, this function returns false.
228252723Sdim  static bool StandardErrHasColors();
229252723Sdim
230263509Sdim  /// Enables or disables whether ANSI escape sequences are used to output
231263509Sdim  /// colors. This only has an effect on Windows.
232263509Sdim  /// Note: Setting this option is not thread-safe and should only be done
233263509Sdim  /// during initialization.
234263509Sdim  static void UseANSIEscapeCodes(bool enable);
235263509Sdim
236252723Sdim  /// Whether changing colors requires the output to be flushed.
237252723Sdim  /// This is needed on systems that don't support escape sequences for
238252723Sdim  /// changing colors.
239252723Sdim  static bool ColorNeedsFlush();
240252723Sdim
241252723Sdim  /// This function returns the colorcode escape sequences.
242252723Sdim  /// If ColorNeedsFlush() is true then this function will change the colors
243252723Sdim  /// and return an empty escape sequence. In that case it is the
244252723Sdim  /// responsibility of the client to flush the output stream prior to
245252723Sdim  /// calling this function.
246252723Sdim  static const char *OutputColor(char c, bool bold, bool bg);
247252723Sdim
248252723Sdim  /// Same as OutputColor, but only enables the bold attribute.
249252723Sdim  static const char *OutputBold(bool bg);
250252723Sdim
251252723Sdim  /// This function returns the escape sequence to reverse forground and
252252723Sdim  /// background colors.
253252723Sdim  static const char *OutputReverse();
254252723Sdim
255252723Sdim  /// Resets the terminals colors, or returns an escape sequence to do so.
256252723Sdim  static const char *ResetColor();
257252723Sdim
258252723Sdim  /// Get the result of a process wide random number generator. The
259252723Sdim  /// generator will be automatically seeded in non-deterministic fashion.
260252723Sdim  static unsigned GetRandomNumber();
261252723Sdim};
262252723Sdim
263218885Sdim}
264218885Sdim}
265218885Sdim
266218885Sdim#endif
267