20 #ifdef HAVE_SYS_TIME_H
23 #ifdef HAVE_SYS_RESOURCE_H
24 #include <sys/resource.h>
28 #if defined(HAVE_MALLOC_H) && !defined(__DragonFly__) && \
29 !defined(__OpenBSD__) && !defined(__Bitrig__)
32 #ifdef HAVE_MALLOC_MALLOC_H
33 #include <malloc/malloc.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
55 static std::pair<TimeValue, TimeValue> getRUsageTimes() {
56 #if defined(HAVE_GETRUSAGE)
58 ::getrusage(RUSAGE_SELF, &RU);
59 return std::make_pair(
61 static_cast<TimeValue::SecondsType>(RU.ru_utime.tv_sec),
62 static_cast<TimeValue::NanoSecondsType>(
65 static_cast<TimeValue::SecondsType>(RU.ru_stime.tv_sec),
66 static_cast<TimeValue::NanoSecondsType>(
69 #warning Cannot get usage times on this platform
70 return std::make_pair(TimeValue(), TimeValue());
75 #if _POSIX_TIMERS > 0 && _POSIX_CPUTIME > 0
78 if (::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &TS) == 0)
79 return TimeValue(static_cast<TimeValue::SecondsType>(TS.tv_sec),
80 static_cast<TimeValue::NanoSecondsType>(TS.tv_nsec));
84 return getRUsageTimes().first;
89 return getRUsageTimes().second;
94 static unsigned getPageSize() {
95 #if defined(HAVE_GETPAGESIZE)
96 const int page_size = ::getpagesize();
97 #elif defined(HAVE_SYSCONF)
98 long page_size = ::sysconf(_SC_PAGE_SIZE);
100 #warning Cannot get the page size on this machine
102 return static_cast<unsigned>(page_size);
107 self_process::self_process() : PageSize(getPageSize()) {
112 #if defined(HAVE_MALLINFO)
116 #elif defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
117 malloc_statistics_t
Stats;
118 malloc_zone_statistics(malloc_default_zone(), &Stats);
119 return Stats.size_in_use;
120 #elif defined(HAVE_SBRK)
123 static char *StartOfMemory =
reinterpret_cast<char*
>(::sbrk(0));
124 char *EndOfMemory = (
char*)sbrk(0);
125 if (EndOfMemory != ((
char*)-1) && StartOfMemory != ((
char*)-1))
126 return EndOfMemory - StartOfMemory;
130 #warning Cannot get malloc info on this platform
136 TimeValue &sys_time) {
138 llvm::tie(user_time, sys_time) = getRUsageTimes();
141 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
142 #include <mach/mach.h>
151 rlim.rlim_cur = rlim.rlim_max = 0;
152 setrlimit(RLIMIT_CORE, &rlim);
155 #if defined(HAVE_MACH_MACH_H) && !defined(__GNU__)
159 mach_msg_type_number_t Count = 0;
160 exception_mask_t OriginalMasks[EXC_TYPES_COUNT];
161 exception_port_t OriginalPorts[EXC_TYPES_COUNT];
162 exception_behavior_t OriginalBehaviors[EXC_TYPES_COUNT];
163 thread_state_flavor_t OriginalFlavors[EXC_TYPES_COUNT];
165 task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, OriginalMasks,
166 &Count, OriginalPorts, OriginalBehaviors,
168 if (err == KERN_SUCCESS) {
170 for (
unsigned i = 0; i != Count; ++i)
171 task_set_exception_ports(mach_task_self(), OriginalMasks[i],
172 MACH_PORT_NULL, OriginalBehaviors[i],
177 signal(SIGABRT, _exit);
178 signal(SIGILL, _exit);
179 signal(SIGFPE, _exit);
180 signal(SIGSEGV, _exit);
181 signal(SIGBUS, _exit);
186 std::string NameStr = Name.
str();
187 const char *Val =
::getenv(NameStr.c_str());
190 return std::string(Val);
222 static unsigned getColumns(
int FileID) {
224 if (
const char *ColumnsStr =
std::getenv(
"COLUMNS")) {
230 unsigned Columns = 0;
232 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H)
235 if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
246 return getColumns(1);
253 return getColumns(2);
260 extern "C" int setupterm(
char *term,
int filedes,
int *errret);
261 extern "C" struct term *set_curterm(
struct term *termp);
262 extern "C" int del_curterm(
struct term *termp);
263 extern "C" int tigetnum(
char *capname);
266 static bool terminalHasColors(
int fd) {
273 if (setupterm((
char *)0, fd, &errret) != 0)
291 bool HasColors = tigetnum(const_cast<char *>(
"colors")) > 0;
295 struct term *termp = set_curterm((
struct term *)0);
296 (void)del_curterm(termp);
346 #if !defined(HAVE_ARC4RANDOM)
347 static unsigned GetRandomNumberSeed() {
349 if (FILE *RandomSource = ::
fopen(
"/dev/urandom",
"r")) {
351 int count =
::fread((
void *)&seed,
sizeof(seed), 1, RandomSource);
362 return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid());
367 #if defined(HAVE_ARC4RANDOM)
370 static int x = (::srand(GetRandomNumberSeed()), 0);
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
static TimeValue now()
Creates a TimeValue with the current time (UTC).
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
virtual TimeValue get_system_time() const
Get the system time consumed by this process.
static const char * OutputColor(char c, bool bold, bool bg)
static bool StandardErrHasColors()
static bool FileDescriptorHasColors(int fd)
std::string str() const
str - Get the contents as an std::string.
FILE *fopen(const char *filename, const char *mode);.
static bool FileDescriptorIsDisplayed(int fd)
int fclose(FILE *stream);
virtual id_type get_id()
Get the operating system specific identifier for this process.
static bool ColorNeedsFlush()
static const char * OutputReverse()
static const char colorcodes[2][2][8][10]
block placement Basic Block Placement Stats
int atoi(const char *str);
static bool StandardInIsUserInput()
static bool StandardOutIsDisplayed()
static void UseANSIEscapeCodes(bool enable)
Guard a section of code with a Mutex.
static unsigned StandardOutColumns()
static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, TimeValue &sys_time)
hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6)
virtual TimeValue get_user_time() const
Get the user time consumed by this process.
pid_t id_type
Operating system specific type to identify a process.
void append(in_iter in_start, in_iter in_end)
static size_t GetMallocUsage()
Return process memory usage. This static function will return the total amount of memory allocated by...
static bool StandardErrIsDisplayed()
static void PreventCoreFiles()
Prevent core file generation.
static unsigned StandardErrColumns()
static bool StandardOutHasColors()
static unsigned GetRandomNumber()
static error_code success()
static error_code GetArgumentVector(SmallVectorImpl< const char * > &Args, ArrayRef< const char * > ArgsFromMain, SpecificBumpPtrAllocator< char > &ArgAllocator)
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
char *getenv(const char *name);
tier< T1, T2 > tie(T1 &f, T2 &s)
static Optional< std::string > GetEnv(StringRef name)