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