Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
9.4 KiB

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