Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Support/Process.h -----------------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// |
| 10 | /// Provides a library for accessing information about this process and other |
| 11 | /// processes on the operating system. Also provides means of spawning |
| 12 | /// subprocess for commands. The design of this library is modeled after the |
| 13 | /// proposed design of the Boost.Process library, and is design specifically to |
| 14 | /// follow the style of standard libraries and potentially become a proposal |
| 15 | /// for a standard library. |
| 16 | /// |
| 17 | /// This file declares the llvm::sys::Process class which contains a collection |
| 18 | /// of legacy static interfaces for extracting various information about the |
| 19 | /// current process. The goal is to migrate users of this API over to the new |
| 20 | /// interfaces. |
| 21 | /// |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #ifndef LLVM_SUPPORT_PROCESS_H |
| 25 | #define LLVM_SUPPORT_PROCESS_H |
| 26 | |
| 27 | #include "llvm/ADT/Optional.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 28 | #include "llvm/Support/Allocator.h" |
| 29 | #include "llvm/Support/Chrono.h" |
| 30 | #include "llvm/Support/DataTypes.h" |
| 31 | #include <system_error> |
| 32 | |
| 33 | namespace llvm { |
| 34 | template <typename T> class ArrayRef; |
| 35 | class StringRef; |
| 36 | |
| 37 | namespace sys { |
| 38 | |
| 39 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | /// A collection of legacy interfaces for querying information about the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | /// current executing process. |
| 42 | class Process { |
| 43 | public: |
| 44 | static unsigned getPageSize(); |
| 45 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 46 | /// Return process memory usage. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | /// This static function will return the total amount of memory allocated |
| 48 | /// by the process. This only counts the memory allocated via the malloc, |
| 49 | /// calloc and realloc functions and includes any "free" holes in the |
| 50 | /// allocated space. |
| 51 | static size_t GetMallocUsage(); |
| 52 | |
| 53 | /// This static function will set \p user_time to the amount of CPU time |
| 54 | /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU |
| 55 | /// time spent in system (kernel) mode. If the operating system does not |
| 56 | /// support collection of these metrics, a zero duration will be for both |
| 57 | /// values. |
| 58 | /// \param elapsed Returns the system_clock::now() giving current time |
| 59 | /// \param user_time Returns the current amount of user time for the process |
| 60 | /// \param sys_time Returns the current amount of system time for the process |
| 61 | static void GetTimeUsage(TimePoint<> &elapsed, |
| 62 | std::chrono::nanoseconds &user_time, |
| 63 | std::chrono::nanoseconds &sys_time); |
| 64 | |
| 65 | /// This function makes the necessary calls to the operating system to |
| 66 | /// prevent core files or any other kind of large memory dumps that can |
| 67 | /// occur when a program fails. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 68 | /// Prevent core file generation. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 69 | static void PreventCoreFiles(); |
| 70 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | /// true if PreventCoreFiles has been called, false otherwise. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | static bool AreCoreFilesPrevented(); |
| 73 | |
| 74 | // This function returns the environment variable \arg name's value as a UTF-8 |
| 75 | // string. \arg Name is assumed to be in UTF-8 encoding too. |
| 76 | static Optional<std::string> GetEnv(StringRef name); |
| 77 | |
| 78 | /// This function searches for an existing file in the list of directories |
| 79 | /// in a PATH like environment variable, and returns the first file found, |
| 80 | /// according to the order of the entries in the PATH like environment |
| 81 | /// variable. If an ignore list is specified, then any folder which is in |
| 82 | /// the PATH like environment variable but is also in IgnoreList is not |
| 83 | /// considered. |
| 84 | static Optional<std::string> FindInEnvPath(StringRef EnvName, |
| 85 | StringRef FileName, |
| 86 | ArrayRef<std::string> IgnoreList); |
| 87 | |
| 88 | static Optional<std::string> FindInEnvPath(StringRef EnvName, |
| 89 | StringRef FileName); |
| 90 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 91 | // This functions ensures that the standard file descriptors (input, output, |
| 92 | // and error) are properly mapped to a file descriptor before we use any of |
| 93 | // them. This should only be called by standalone programs, library |
| 94 | // components should not call this. |
| 95 | static std::error_code FixupStandardFileDescriptors(); |
| 96 | |
| 97 | // This function safely closes a file descriptor. It is not safe to retry |
| 98 | // close(2) when it returns with errno equivalent to EINTR; this is because |
| 99 | // *nixen cannot agree if the file descriptor is, in fact, closed when this |
| 100 | // occurs. |
| 101 | // |
| 102 | // N.B. Some operating systems, due to thread cancellation, cannot properly |
| 103 | // guarantee that it will or will not be closed one way or the other! |
| 104 | static std::error_code SafelyCloseFileDescriptor(int FD); |
| 105 | |
| 106 | /// This function determines if the standard input is connected directly |
| 107 | /// to a user's input (keyboard probably), rather than coming from a file |
| 108 | /// or pipe. |
| 109 | static bool StandardInIsUserInput(); |
| 110 | |
| 111 | /// This function determines if the standard output is connected to a |
| 112 | /// "tty" or "console" window. That is, the output would be displayed to |
| 113 | /// the user rather than being put on a pipe or stored in a file. |
| 114 | static bool StandardOutIsDisplayed(); |
| 115 | |
| 116 | /// This function determines if the standard error is connected to a |
| 117 | /// "tty" or "console" window. That is, the output would be displayed to |
| 118 | /// the user rather than being put on a pipe or stored in a file. |
| 119 | static bool StandardErrIsDisplayed(); |
| 120 | |
| 121 | /// This function determines if the given file descriptor is connected to |
| 122 | /// a "tty" or "console" window. That is, the output would be displayed to |
| 123 | /// the user rather than being put on a pipe or stored in a file. |
| 124 | static bool FileDescriptorIsDisplayed(int fd); |
| 125 | |
| 126 | /// This function determines if the given file descriptor is displayd and |
| 127 | /// supports colors. |
| 128 | static bool FileDescriptorHasColors(int fd); |
| 129 | |
| 130 | /// This function determines the number of columns in the window |
| 131 | /// if standard output is connected to a "tty" or "console" |
| 132 | /// window. If standard output is not connected to a tty or |
| 133 | /// console, or if the number of columns cannot be determined, |
| 134 | /// this routine returns zero. |
| 135 | static unsigned StandardOutColumns(); |
| 136 | |
| 137 | /// This function determines the number of columns in the window |
| 138 | /// if standard error is connected to a "tty" or "console" |
| 139 | /// window. If standard error is not connected to a tty or |
| 140 | /// console, or if the number of columns cannot be determined, |
| 141 | /// this routine returns zero. |
| 142 | static unsigned StandardErrColumns(); |
| 143 | |
| 144 | /// This function determines whether the terminal connected to standard |
| 145 | /// output supports colors. If standard output is not connected to a |
| 146 | /// terminal, this function returns false. |
| 147 | static bool StandardOutHasColors(); |
| 148 | |
| 149 | /// This function determines whether the terminal connected to standard |
| 150 | /// error supports colors. If standard error is not connected to a |
| 151 | /// terminal, this function returns false. |
| 152 | static bool StandardErrHasColors(); |
| 153 | |
| 154 | /// Enables or disables whether ANSI escape sequences are used to output |
| 155 | /// colors. This only has an effect on Windows. |
| 156 | /// Note: Setting this option is not thread-safe and should only be done |
| 157 | /// during initialization. |
| 158 | static void UseANSIEscapeCodes(bool enable); |
| 159 | |
| 160 | /// Whether changing colors requires the output to be flushed. |
| 161 | /// This is needed on systems that don't support escape sequences for |
| 162 | /// changing colors. |
| 163 | static bool ColorNeedsFlush(); |
| 164 | |
| 165 | /// This function returns the colorcode escape sequences. |
| 166 | /// If ColorNeedsFlush() is true then this function will change the colors |
| 167 | /// and return an empty escape sequence. In that case it is the |
| 168 | /// responsibility of the client to flush the output stream prior to |
| 169 | /// calling this function. |
| 170 | static const char *OutputColor(char c, bool bold, bool bg); |
| 171 | |
| 172 | /// Same as OutputColor, but only enables the bold attribute. |
| 173 | static const char *OutputBold(bool bg); |
| 174 | |
| 175 | /// This function returns the escape sequence to reverse forground and |
| 176 | /// background colors. |
| 177 | static const char *OutputReverse(); |
| 178 | |
| 179 | /// Resets the terminals colors, or returns an escape sequence to do so. |
| 180 | static const char *ResetColor(); |
| 181 | |
| 182 | /// Get the result of a process wide random number generator. The |
| 183 | /// generator will be automatically seeded in non-deterministic fashion. |
| 184 | static unsigned GetRandomNumber(); |
| 185 | }; |
| 186 | |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #endif |