17 #include "llvm/Config/config.h" 24 #ifdef HAVE_SYS_TIME_H 27 #ifdef HAVE_SYS_RESOURCE_H 28 #include <sys/resource.h> 30 #ifdef HAVE_SYS_STAT_H 38 #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \ 42 #if defined(HAVE_MALLCTL) 43 #include <malloc_np.h> 45 #ifdef HAVE_MALLOC_MALLOC_H 46 #include <malloc/malloc.h> 48 #ifdef HAVE_SYS_IOCTL_H 49 # include <sys/ioctl.h> 63 static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsageTimes() {
64 #if defined(HAVE_GETRUSAGE) 66 ::getrusage(RUSAGE_SELF, &RU);
69 #warning Cannot get usage times on this platform 70 return { std::chrono::microseconds::zero(), std::chrono::microseconds::zero() };
77 #if defined(HAVE_GETPAGESIZE) 78 static const int page_size = ::getpagesize();
79 #elif defined(HAVE_SYSCONF) 80 static long page_size = ::sysconf(_SC_PAGE_SIZE);
82 #error Cannot get the page size on this machine 84 return static_cast<unsigned>(page_size);
88 #if defined(HAVE_MALLINFO) 92 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H) 93 malloc_statistics_t
Stats;
94 malloc_zone_statistics(malloc_default_zone(), &Stats);
95 return Stats.size_in_use;
96 #elif defined(HAVE_MALLCTL) 99 if (mallctl(
"stats.allocated", &alloc, &sz, NULL, 0) == 0)
102 #elif defined(HAVE_SBRK) 105 static char *StartOfMemory =
reinterpret_cast<char*
>(::sbrk(0));
106 char *EndOfMemory = (
char*)sbrk(0);
107 if (EndOfMemory != ((
char*)-1) && StartOfMemory != ((
char*)-1))
108 return EndOfMemory - StartOfMemory;
111 #warning Cannot get malloc info on this platform 117 std::chrono::nanoseconds &sys_time) {
119 std::tie(user_time, sys_time) = getRUsageTimes();
122 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) 123 #include <mach/mach.h> 132 rlim.rlim_cur = rlim.rlim_max = 0;
133 setrlimit(RLIMIT_CORE, &rlim);
136 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__) 140 mach_msg_type_number_t Count = 0;
141 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
142 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
143 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
144 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
146 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
147 &Count, OriginalPorts, OriginalBehaviors,
149 if (err == KERN_SUCCESS) {
151 for (
unsigned i = 0; i != Count; ++i)
152 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
153 MACH_PORT_NULL, OriginalBehaviors[i],
158 signal(SIGABRT, _exit);
159 signal(SIGILL, _exit);
160 signal(SIGFPE, _exit);
161 signal(SIGSEGV, _exit);
162 signal(SIGBUS, _exit);
169 std::string NameStr = Name.
str();
170 const char *Val = ::getenv(NameStr.c_str());
173 return std::string(Val);
179 FDCloser(
int &FD) : FD(FD), KeepOpen(
false) {}
180 void keepOpen() { KeepOpen =
true; }
182 if (!KeepOpen && FD >= 0)
187 FDCloser(
const FDCloser &) =
delete;
188 void operator=(
const FDCloser &) =
delete;
197 FDCloser FDC(NullFD);
198 const int StandardFDs[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
199 for (
int StandardFD : StandardFDs) {
203 assert(errno &&
"expected errno to be set if fstat failed!");
206 return std::error_code(errno, std::generic_category());
211 assert(errno == EBADF &&
"expected errno to have EBADF at this point!");
216 auto Open = [&]() { return ::open(
"/dev/null", O_RDWR); };
218 return std::error_code(errno, std::generic_category());
221 if (NullFD == StandardFD)
223 else if (dup2(NullFD, StandardFD) < 0)
224 return std::error_code(errno, std::generic_category());
226 return std::error_code();
232 if (sigfillset(&FullSet) < 0)
233 return std::error_code(errno, std::generic_category());
236 #if LLVM_ENABLE_THREADS 237 if (
int EC = pthread_sigmask(SIG_SETMASK, &FullSet, &SavedSet))
238 return std::error_code(EC, std::generic_category());
240 if (sigprocmask(SIG_SETMASK, &FullSet, &SavedSet) < 0)
241 return std::error_code(errno, std::generic_category());
246 int ErrnoFromClose = 0;
248 ErrnoFromClose = errno;
251 #if LLVM_ENABLE_THREADS 252 EC = pthread_sigmask(SIG_SETMASK, &SavedSet,
nullptr);
254 if (sigprocmask(SIG_SETMASK, &SavedSet,
nullptr) < 0)
260 return std::error_code(ErrnoFromClose, std::generic_category());
261 return std::error_code(EC, std::generic_category());
285 static unsigned getColumns(
int FileID) {
287 if (
const char *ColumnsStr = std::getenv(
"COLUMNS")) {
288 int Columns = std::atoi(ColumnsStr);
293 unsigned Columns = 0;
295 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) 298 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
309 return getColumns(1);
316 return getColumns(2);
323 extern "C" int setupterm(
char *term,
int filedes,
int *errret);
324 extern "C" struct term *set_curterm(
struct term *termp);
325 extern "C" int del_curterm(
struct term *termp);
326 extern "C" int tigetnum(
char *capname);
333 static bool terminalHasColors(
int fd) {
339 if (setupterm(
nullptr, fd, &errret) != 0)
357 bool HasColors = tigetnum(const_cast<char *>(
"colors")) > 0;
361 struct term *termp = set_curterm(
nullptr);
362 (void)del_curterm(termp);
370 if (
const char *TermStr = std::getenv(
"TERM")) {
373 .
Case(
"cygwin",
true)
427 #if !HAVE_DECL_ARC4RANDOM 428 static unsigned GetRandomNumberSeed() {
430 int urandomFD = open(
"/dev/urandom", O_RDONLY);
432 if (urandomFD != -1) {
436 int count =
read(urandomFD, (
void *)&seed,
sizeof(seed));
441 if (count ==
sizeof(seed))
448 return hash_combine(Now.time_since_epoch().count(), ::getpid());
453 #if HAVE_DECL_ARC4RANDOM 456 static int x = (
static_cast<void>(::srand(GetRandomNumberSeed())), 0);
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
This class represents lattice values for constants.
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
static const char * OutputColor(char c, bool bold, bool bg)
This function returns the colorcode escape sequences.
static bool StandardErrHasColors()
This function determines whether the terminal connected to standard error supports colors...
static bool FileDescriptorHasColors(int fd)
This function determines if the given file descriptor is displayd and supports colors.
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
static std::error_code FixupStandardFileDescriptors()
amdgpu Simplify well known AMD library false Value Value const Twine & Name
static bool ColorNeedsFlush()
Whether changing colors requires the output to be flushed.
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
static const char colorcodes[2][2][8][10]
block placement Basic Block Placement Stats
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
static bool StandardInIsUserInput()
This function determines if the standard input is connected directly to a user's input (keyboard prob...
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static bool StandardOutIsDisplayed()
This function determines if the standard output is connected to a "tty" or "console" window...
static void UseANSIEscapeCodes(bool enable)
Enables or disables whether ANSI escape sequences are used to output colors.
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
A switch()-like statement whose cases are string literals.
static unsigned StandardOutColumns()
This function determines the number of columns in the window if standard output is connected to a "tt...
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
auto RetryAfterSignal(const FailT &Fail, const Fun &F, const Args &... As) -> decltype(F(As...))
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & EndsWith(StringLiteral S, T Value)
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & StartsWith(StringLiteral S, T Value)
static size_t GetMallocUsage()
Return process memory usage.
static unsigned getPageSize()
static bool StandardErrIsDisplayed()
This function determines if the standard error is connected to a "tty" or "console" window...
static void PreventCoreFiles()
This function makes the necessary calls to the operating system to prevent core files or any other ki...
static unsigned StandardErrColumns()
This function determines the number of columns in the window if standard error is connected to a "tty...
static bool StandardOutHasColors()
This function determines whether the terminal connected to standard output supports colors...
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
std::chrono::microseconds toDuration(const struct timeval &TV)
Convert a struct timeval to a duration.
static void GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time, std::chrono::nanoseconds &sys_time)
This static function will set user_time to the amount of CPU time spent in user (non-kernel) mode and...
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static std::error_code SafelyCloseFileDescriptor(int FD)
StringRef - Represent a constant reference to a string, i.e.
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
static Optional< std::string > GetEnv(StringRef name)