1 //===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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
23 //===----------------------------------------------------------------------===//
25 #ifndef LLVM_SYSTEM_PROCESS_H
26 #define LLVM_SYSTEM_PROCESS_H
28 #include "llvm/Support/TimeValue.h"
33 /// \brief A collection of legacy interfaces for querying information about the
34 /// current executing process.
37 /// \brief Get the virtual memory page size
38 /// This static function will return the operating system's virtual memory
40 /// \returns The number of bytes in a virtual memory page.
41 static unsigned GetPageSize();
43 /// \brief Return process memory usage.
44 /// This static function will return the total amount of memory allocated
45 /// by the process. This only counts the memory allocated via the malloc,
46 /// calloc and realloc functions and includes any "free" holes in the
48 static size_t GetMallocUsage();
50 /// This static function will return the total memory usage of the
51 /// process. This includes code, data, stack and mapped pages usage. Notei
52 /// that the value returned here is not necessarily the Running Set Size,
53 /// it is the total virtual memory usage, regardless of mapped state of
55 static size_t GetTotalMemoryUsage();
57 /// This static function will set \p user_time to the amount of CPU time
58 /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
59 /// time spent in system (kernel) mode. If the operating system does not
60 /// support collection of these metrics, a zero TimeValue will be for both
62 /// \param elapsed Returns the TimeValue::now() giving current time
63 /// \param user_time Returns the current amount of user time for the process
64 /// \param sys_time Returns the current amount of system time for the process
65 static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
68 /// This static function will return the process' current user id number.
69 /// Not all operating systems support this feature. Where it is not
70 /// supported, the function should return 65536 as the value.
71 static int GetCurrentUserId();
73 /// This static function will return the process' current group id number.
74 /// Not all operating systems support this feature. Where it is not
75 /// supported, the function should return 65536 as the value.
76 static int GetCurrentGroupId();
78 /// This function makes the necessary calls to the operating system to
79 /// prevent core files or any other kind of large memory dumps that can
80 /// occur when a program fails.
81 /// @brief Prevent core file generation.
82 static void PreventCoreFiles();
84 /// This function determines if the standard input is connected directly
85 /// to a user's input (keyboard probably), rather than coming from a file
87 static bool StandardInIsUserInput();
89 /// This function determines if the standard output is connected to a
90 /// "tty" or "console" window. That is, the output would be displayed to
91 /// the user rather than being put on a pipe or stored in a file.
92 static bool StandardOutIsDisplayed();
94 /// This function determines if the standard error is connected to a
95 /// "tty" or "console" window. That is, the output would be displayed to
96 /// the user rather than being put on a pipe or stored in a file.
97 static bool StandardErrIsDisplayed();
99 /// This function determines if the given file descriptor is connected to
100 /// a "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 FileDescriptorIsDisplayed(int fd);
104 /// This function determines if the given file descriptor is displayd and
106 static bool FileDescriptorHasColors(int fd);
108 /// This function determines the number of columns in the window
109 /// if standard output is connected to a "tty" or "console"
110 /// window. If standard output is not connected to a tty or
111 /// console, or if the number of columns cannot be determined,
112 /// this routine returns zero.
113 static unsigned StandardOutColumns();
115 /// This function determines the number of columns in the window
116 /// if standard error is connected to a "tty" or "console"
117 /// window. If standard error is not connected to a tty or
118 /// console, or if the number of columns cannot be determined,
119 /// this routine returns zero.
120 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();
127 /// This function determines whether the terminal connected to standard
128 /// error supports colors. If standard error is not connected to a
129 /// terminal, this function returns false.
130 static bool StandardErrHasColors();
132 /// Whether changing colors requires the output to be flushed.
133 /// This is needed on systems that don't support escape sequences for
135 static bool ColorNeedsFlush();
137 /// This function returns the colorcode escape sequences.
138 /// If ColorNeedsFlush() is true then this function will change the colors
139 /// and return an empty escape sequence. In that case it is the
140 /// responsibility of the client to flush the output stream prior to
141 /// calling this function.
142 static const char *OutputColor(char c, bool bold, bool bg);
144 /// Same as OutputColor, but only enables the bold attribute.
145 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();
151 /// Resets the terminals colors, or returns an escape sequence to do so.
152 static const char *ResetColor();
154 /// Get the result of a process wide random number generator. The
155 /// generator will be automatically seeded in non-deterministic fashion.
156 static unsigned GetRandomNumber();