Start sketching out a roadmap for better subprocess management in the
[oota-llvm.git] / include / llvm / Support / Process.h
1 //===- 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_SYSTEM_PROCESS_H
26 #define LLVM_SYSTEM_PROCESS_H
27
28 #include "llvm/Support/TimeValue.h"
29
30 namespace llvm {
31 namespace sys {
32
33 /// \brief A collection of legacy interfaces for querying information about the
34 /// current executing process.
35 class Process {
36 public:
37   /// \brief Get the virtual memory page size
38   /// This static function will return the operating system's virtual memory
39   /// page size.
40   /// \returns The number of bytes in a virtual memory page.
41   static unsigned GetPageSize();
42
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
47   /// allocated space.
48   static size_t GetMallocUsage();
49
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
54   /// that memory.
55   static size_t GetTotalMemoryUsage();
56
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
61   /// values.
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,
66                            TimeValue &sys_time);
67
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();
72
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();
77
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();
83
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
86   /// or pipe.
87   static bool StandardInIsUserInput();
88
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();
93
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();
98
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);
103
104   /// This function determines if the given file descriptor is displayd and
105   /// supports colors.
106   static bool FileDescriptorHasColors(int fd);
107
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();
114
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();
121
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();
126
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();
131
132   /// Whether changing colors requires the output to be flushed.
133   /// This is needed on systems that don't support escape sequences for
134   /// changing colors.
135   static bool ColorNeedsFlush();
136
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);
143
144   /// Same as OutputColor, but only enables the bold attribute.
145   static const char *OutputBold(bool bg);
146
147   /// This function returns the escape sequence to reverse forground and
148   /// background colors.
149   static const char *OutputReverse();
150
151   /// Resets the terminals colors, or returns an escape sequence to do so.
152   static const char *ResetColor();
153
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();
157 };
158
159 }
160 }
161
162 #endif