LLVM API Documentation

Process.h
Go to the documentation of this file.
00001 //===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 ///
00011 /// Provides a library for accessing information about this process and other
00012 /// processes on the operating system. Also provides means of spawning
00013 /// subprocess for commands. The design of this library is modeled after the
00014 /// proposed design of the Boost.Process library, and is design specifically to
00015 /// follow the style of standard libraries and potentially become a proposal
00016 /// for a standard library.
00017 ///
00018 /// This file declares the llvm::sys::Process class which contains a collection
00019 /// of legacy static interfaces for extracting various information about the
00020 /// current process. The goal is to migrate users of this API over to the new
00021 /// interfaces.
00022 ///
00023 //===----------------------------------------------------------------------===//
00024 
00025 #ifndef LLVM_SUPPORT_PROCESS_H
00026 #define LLVM_SUPPORT_PROCESS_H
00027 
00028 #include "llvm/ADT/ArrayRef.h"
00029 #include "llvm/ADT/Optional.h"
00030 #include "llvm/Config/llvm-config.h"
00031 #include "llvm/Support/Allocator.h"
00032 #include "llvm/Support/DataTypes.h"
00033 #include "llvm/Support/TimeValue.h"
00034 #include <system_error>
00035 
00036 namespace llvm {
00037 class StringRef;
00038 
00039 namespace sys {
00040 
00041 class self_process;
00042 
00043 /// \brief Generic base class which exposes information about an operating
00044 /// system process.
00045 ///
00046 /// This base class is the core interface behind any OS process. It exposes
00047 /// methods to query for generic information about a particular process.
00048 ///
00049 /// Subclasses implement this interface based on the mechanisms available, and
00050 /// can optionally expose more interfaces unique to certain process kinds.
00051 class process {
00052 protected:
00053   /// \brief Only specific subclasses of process objects can be destroyed.
00054   virtual ~process();
00055 
00056 public:
00057   /// \brief Operating system specific type to identify a process.
00058   ///
00059   /// Note that the windows one is defined to 'unsigned long' as this is the
00060   /// documented type for DWORD on windows, and we don't want to pull in the
00061   /// Windows headers here.
00062 #if defined(LLVM_ON_UNIX)
00063   typedef pid_t id_type;
00064 #elif defined(LLVM_ON_WIN32)
00065   typedef unsigned long id_type; // Must match the type of DWORD.
00066 #else
00067 #error Unsupported operating system.
00068 #endif
00069 
00070   /// \brief Get the operating system specific identifier for this process.
00071   virtual id_type get_id() = 0;
00072 
00073   /// \brief Get the user time consumed by this process.
00074   ///
00075   /// Note that this is often an approximation and may be zero on platforms
00076   /// where we don't have good support for the functionality.
00077   virtual TimeValue get_user_time() const = 0;
00078 
00079   /// \brief Get the system time consumed by this process.
00080   ///
00081   /// Note that this is often an approximation and may be zero on platforms
00082   /// where we don't have good support for the functionality.
00083   virtual TimeValue get_system_time() const = 0;
00084 
00085   /// \brief Get the wall time consumed by this process.
00086   ///
00087   /// Note that this is often an approximation and may be zero on platforms
00088   /// where we don't have good support for the functionality.
00089   virtual TimeValue get_wall_time() const = 0;
00090 
00091   /// \name Static factory routines for processes.
00092   /// @{
00093 
00094   /// \brief Get the process object for the current process.
00095   static self_process *get_self();
00096 
00097   /// @}
00098 
00099 };
00100 
00101 /// \brief The specific class representing the current process.
00102 ///
00103 /// The current process can both specialize the implementation of the routines
00104 /// and can expose certain information not available for other OS processes.
00105 class self_process : public process {
00106   friend class process;
00107 
00108   /// \brief Private destructor, as users shouldn't create objects of this
00109   /// type.
00110   virtual ~self_process();
00111 
00112 public:
00113   id_type get_id() override;
00114   TimeValue get_user_time() const override;
00115   TimeValue get_system_time() const override;
00116   TimeValue get_wall_time() const override;
00117 
00118   /// \name Process configuration (sysconf on POSIX)
00119   /// @{
00120 
00121   /// \brief Get the virtual memory page size.
00122   ///
00123   /// Query the operating system for this process's page size.
00124   size_t page_size() const { return PageSize; };
00125 
00126   /// @}
00127 
00128 private:
00129   /// \name Cached process state.
00130   /// @{
00131 
00132   /// \brief Cached page size, this cannot vary during the life of the process.
00133   size_t PageSize;
00134 
00135   /// @}
00136 
00137   /// \brief Constructor, used by \c process::get_self() only.
00138   self_process();
00139 };
00140 
00141 
00142 /// \brief A collection of legacy interfaces for querying information about the
00143 /// current executing process.
00144 class Process {
00145 public:
00146   /// \brief Return process memory usage.
00147   /// This static function will return the total amount of memory allocated
00148   /// by the process. This only counts the memory allocated via the malloc,
00149   /// calloc and realloc functions and includes any "free" holes in the
00150   /// allocated space.
00151   static size_t GetMallocUsage();
00152 
00153   /// This static function will set \p user_time to the amount of CPU time
00154   /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
00155   /// time spent in system (kernel) mode.  If the operating system does not
00156   /// support collection of these metrics, a zero TimeValue will be for both
00157   /// values.
00158   /// \param elapsed Returns the TimeValue::now() giving current time
00159   /// \param user_time Returns the current amount of user time for the process
00160   /// \param sys_time Returns the current amount of system time for the process
00161   static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
00162                            TimeValue &sys_time);
00163 
00164   /// This function makes the necessary calls to the operating system to
00165   /// prevent core files or any other kind of large memory dumps that can
00166   /// occur when a program fails.
00167   /// @brief Prevent core file generation.
00168   static void PreventCoreFiles();
00169 
00170   // This function returns the environment variable \arg name's value as a UTF-8
00171   // string. \arg Name is assumed to be in UTF-8 encoding too.
00172   static Optional<std::string> GetEnv(StringRef name);
00173 
00174   /// This function searches for an existing file in the list of directories
00175   /// in a PATH like environment variable, and returns the first file found,
00176   /// according to the order of the entries in the PATH like environment
00177   /// variable.
00178   static Optional<std::string> FindInEnvPath(const std::string& EnvName,
00179                                              const std::string& FileName);
00180 
00181   /// This function returns a SmallVector containing the arguments passed from
00182   /// the operating system to the program.  This function expects to be handed
00183   /// the vector passed in from main.
00184   static std::error_code
00185   GetArgumentVector(SmallVectorImpl<const char *> &Args,
00186                     ArrayRef<const char *> ArgsFromMain,
00187                     SpecificBumpPtrAllocator<char> &ArgAllocator);
00188 
00189   /// This function determines if the standard input is connected directly
00190   /// to a user's input (keyboard probably), rather than coming from a file
00191   /// or pipe.
00192   static bool StandardInIsUserInput();
00193 
00194   /// This function determines if the standard output is connected to a
00195   /// "tty" or "console" window. That is, the output would be displayed to
00196   /// the user rather than being put on a pipe or stored in a file.
00197   static bool StandardOutIsDisplayed();
00198 
00199   /// This function determines if the standard error is connected to a
00200   /// "tty" or "console" window. That is, the output would be displayed to
00201   /// the user rather than being put on a pipe or stored in a file.
00202   static bool StandardErrIsDisplayed();
00203 
00204   /// This function determines if the given file descriptor is connected to
00205   /// a "tty" or "console" window. That is, the output would be displayed to
00206   /// the user rather than being put on a pipe or stored in a file.
00207   static bool FileDescriptorIsDisplayed(int fd);
00208 
00209   /// This function determines if the given file descriptor is displayd and
00210   /// supports colors.
00211   static bool FileDescriptorHasColors(int fd);
00212 
00213   /// This function determines the number of columns in the window
00214   /// if standard output is connected to a "tty" or "console"
00215   /// window. If standard output is not connected to a tty or
00216   /// console, or if the number of columns cannot be determined,
00217   /// this routine returns zero.
00218   static unsigned StandardOutColumns();
00219 
00220   /// This function determines the number of columns in the window
00221   /// if standard error is connected to a "tty" or "console"
00222   /// window. If standard error is not connected to a tty or
00223   /// console, or if the number of columns cannot be determined,
00224   /// this routine returns zero.
00225   static unsigned StandardErrColumns();
00226 
00227   /// This function determines whether the terminal connected to standard
00228   /// output supports colors. If standard output is not connected to a
00229   /// terminal, this function returns false.
00230   static bool StandardOutHasColors();
00231 
00232   /// This function determines whether the terminal connected to standard
00233   /// error supports colors. If standard error is not connected to a
00234   /// terminal, this function returns false.
00235   static bool StandardErrHasColors();
00236 
00237   /// Enables or disables whether ANSI escape sequences are used to output
00238   /// colors. This only has an effect on Windows.
00239   /// Note: Setting this option is not thread-safe and should only be done
00240   /// during initialization.
00241   static void UseANSIEscapeCodes(bool enable);
00242 
00243   /// Whether changing colors requires the output to be flushed.
00244   /// This is needed on systems that don't support escape sequences for
00245   /// changing colors.
00246   static bool ColorNeedsFlush();
00247 
00248   /// This function returns the colorcode escape sequences.
00249   /// If ColorNeedsFlush() is true then this function will change the colors
00250   /// and return an empty escape sequence. In that case it is the
00251   /// responsibility of the client to flush the output stream prior to
00252   /// calling this function.
00253   static const char *OutputColor(char c, bool bold, bool bg);
00254 
00255   /// Same as OutputColor, but only enables the bold attribute.
00256   static const char *OutputBold(bool bg);
00257 
00258   /// This function returns the escape sequence to reverse forground and
00259   /// background colors.
00260   static const char *OutputReverse();
00261 
00262   /// Resets the terminals colors, or returns an escape sequence to do so.
00263   static const char *ResetColor();
00264 
00265   /// Get the result of a process wide random number generator. The
00266   /// generator will be automatically seeded in non-deterministic fashion.
00267   static unsigned GetRandomNumber();
00268 };
00269 
00270 }
00271 }
00272 
00273 #endif