Process.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/ADT/ArrayRef.h"
  27. #include "llvm/ADT/Optional.h"
  28. #include "llvm/Config/llvm-config.h"
  29. #include "llvm/Support/Allocator.h"
  30. #include "llvm/Support/DataTypes.h"
  31. #include "llvm/Support/TimeValue.h"
  32. #include <system_error>
  33. namespace llvm {
  34. class StringRef;
  35. namespace sys {
  36. /// \brief A collection of legacy interfaces for querying information about the
  37. /// current executing process.
  38. class Process {
  39. public:
  40. static unsigned getPageSize();
  41. /// \brief Return process memory usage.
  42. /// This static function will return the total amount of memory allocated
  43. /// by the process. This only counts the memory allocated via the malloc,
  44. /// calloc and realloc functions and includes any "free" holes in the
  45. /// allocated space.
  46. static size_t GetMallocUsage();
  47. /// This static function will set \p user_time to the amount of CPU time
  48. /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
  49. /// time spent in system (kernel) mode. If the operating system does not
  50. /// support collection of these metrics, a zero TimeValue will be for both
  51. /// values.
  52. /// \param elapsed Returns the TimeValue::now() giving current time
  53. /// \param user_time Returns the current amount of user time for the process
  54. /// \param sys_time Returns the current amount of system time for the process
  55. static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
  56. TimeValue &sys_time);
  57. /// This function makes the necessary calls to the operating system to
  58. /// prevent core files or any other kind of large memory dumps that can
  59. /// occur when a program fails.
  60. /// @brief Prevent core file generation.
  61. static void PreventCoreFiles();
  62. // This function returns the environment variable \arg name's value as a UTF-8
  63. // string. \arg Name is assumed to be in UTF-8 encoding too.
  64. static Optional<std::string> GetEnv(StringRef name);
  65. /// This function searches for an existing file in the list of directories
  66. /// in a PATH like environment variable, and returns the first file found,
  67. /// according to the order of the entries in the PATH like environment
  68. /// variable.
  69. static Optional<std::string> FindInEnvPath(const std::string& EnvName,
  70. const std::string& FileName);
  71. /// This function returns a SmallVector containing the arguments passed from
  72. /// the operating system to the program. This function expects to be handed
  73. /// the vector passed in from main.
  74. static std::error_code
  75. GetArgumentVector(SmallVectorImpl<const char *> &Args,
  76. ArrayRef<const char *> ArgsFromMain,
  77. SpecificBumpPtrAllocator<char> &ArgAllocator);
  78. // This functions ensures that the standard file descriptors (input, output,
  79. // and error) are properly mapped to a file descriptor before we use any of
  80. // them. This should only be called by standalone programs, library
  81. // components should not call this.
  82. static std::error_code FixupStandardFileDescriptors();
  83. // This function safely closes a file descriptor. It is not safe to retry
  84. // close(2) when it returns with errno equivalent to EINTR; this is because
  85. // *nixen cannot agree if the file descriptor is, in fact, closed when this
  86. // occurs.
  87. //
  88. // N.B. Some operating systems, due to thread cancellation, cannot properly
  89. // guarantee that it will or will not be closed one way or the other!
  90. static std::error_code SafelyCloseFileDescriptor(int FD);
  91. /// This function determines if the standard input is connected directly
  92. /// to a user's input (keyboard probably), rather than coming from a file
  93. /// or pipe.
  94. static bool StandardInIsUserInput();
  95. /// This function determines if the standard output is connected to a
  96. /// "tty" or "console" window. That is, the output would be displayed to
  97. /// the user rather than being put on a pipe or stored in a file.
  98. static bool StandardOutIsDisplayed();
  99. /// This function determines if the standard error is connected to a
  100. /// "tty" or "console" window. That is, the output would be displayed to
  101. /// the user rather than being put on a pipe or stored in a file.
  102. static bool StandardErrIsDisplayed();
  103. /// This function determines if the given file descriptor is connected to
  104. /// a "tty" or "console" window. That is, the output would be displayed to
  105. /// the user rather than being put on a pipe or stored in a file.
  106. static bool FileDescriptorIsDisplayed(int fd);
  107. /// This function determines if the given file descriptor is displayd and
  108. /// supports colors.
  109. static bool FileDescriptorHasColors(int fd);
  110. /// This function determines the number of columns in the window
  111. /// if standard output is connected to a "tty" or "console"
  112. /// window. If standard output is not connected to a tty or
  113. /// console, or if the number of columns cannot be determined,
  114. /// this routine returns zero.
  115. static unsigned StandardOutColumns();
  116. /// This function determines the number of columns in the window
  117. /// if standard error is connected to a "tty" or "console"
  118. /// window. If standard error is not connected to a tty or
  119. /// console, or if the number of columns cannot be determined,
  120. /// this routine returns zero.
  121. static unsigned StandardErrColumns();
  122. /// This function determines whether the terminal connected to standard
  123. /// output supports colors. If standard output is not connected to a
  124. /// terminal, this function returns false.
  125. static bool StandardOutHasColors();
  126. /// This function determines whether the terminal connected to standard
  127. /// error supports colors. If standard error is not connected to a
  128. /// terminal, this function returns false.
  129. static bool StandardErrHasColors();
  130. /// Enables or disables whether ANSI escape sequences are used to output
  131. /// colors. This only has an effect on Windows.
  132. /// Note: Setting this option is not thread-safe and should only be done
  133. /// during initialization.
  134. static void UseANSIEscapeCodes(bool enable);
  135. /// Whether changing colors requires the output to be flushed.
  136. /// This is needed on systems that don't support escape sequences for
  137. /// changing colors.
  138. static bool ColorNeedsFlush();
  139. /// This function returns the colorcode escape sequences.
  140. /// If ColorNeedsFlush() is true then this function will change the colors
  141. /// and return an empty escape sequence. In that case it is the
  142. /// responsibility of the client to flush the output stream prior to
  143. /// calling this function.
  144. static const char *OutputColor(char c, bool bold, bool bg);
  145. /// Same as OutputColor, but only enables the bold attribute.
  146. static const char *OutputBold(bool bg);
  147. /// This function returns the escape sequence to reverse forground and
  148. /// background colors.
  149. static const char *OutputReverse();
  150. /// Resets the terminals colors, or returns an escape sequence to do so.
  151. static const char *ResetColor();
  152. /// Get the result of a process wide random number generator. The
  153. /// generator will be automatically seeded in non-deterministic fashion.
  154. static unsigned GetRandomNumber();
  155. };
  156. }
  157. }
  158. #endif