153ca1f3190680f3e86aebe0f72f7918d63f71e0dCharles Davis//===- llvm/Support/Process.h ------------------------------------*- C++ -*-===//
263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
3e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//                     The LLVM Compiler Infrastructure
4e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
8e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//===----------------------------------------------------------------------===//
9e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//
10e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer// This file declares the llvm::sys::Process class.
11e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//
12e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//===----------------------------------------------------------------------===//
13e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
14e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer#ifndef LLVM_SYSTEM_PROCESS_H
15e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer#define LLVM_SYSTEM_PROCESS_H
16e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
171f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/TimeValue.h"
18e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer
19e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencernamespace llvm {
20e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencernamespace sys {
21e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
22e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer  /// This class provides an abstraction for getting information about the
2363b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman  /// currently executing process.
24e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer  /// @since 1.4
25e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer  /// @brief An abstraction for operating system processes.
26e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer  class Process {
27e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer    /// @name Accessors
28e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer    /// @{
29e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer    public:
30e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer      /// This static function will return the operating system's virtual memory
31e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer      /// page size.
32e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer      /// @returns The number of bytes in a virtual memory page.
33e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer      /// @brief Get the virtual memory page size
34e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer      static unsigned GetPageSize();
35e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
36e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// This static function will return the total amount of memory allocated
37e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// by the process. This only counts the memory allocated via the malloc,
3863b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// calloc and realloc functions and includes any "free" holes in the
3963b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// allocated space.
40e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// @brief Return process memory usage.
41e269a1ac1cd795135e91e42527a9814f4807c75aJeff Cohen      static size_t GetMallocUsage();
42e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer
4363b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// This static function will return the total memory usage of the
44e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// process. This includes code, data, stack and mapped pages usage. Notei
45e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// that the value returned here is not necessarily the Running Set Size,
46e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// it is the total virtual memory usage, regardless of mapped state of
47e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// that memory.
48e269a1ac1cd795135e91e42527a9814f4807c75aJeff Cohen      static size_t GetTotalMemoryUsage();
49e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer
5063b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// This static function will set \p user_time to the amount of CPU time
51e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
52e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// time spent in system (kernel) mode.  If the operating system does not
53e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// support collection of these metrics, a zero TimeValue will be for both
54e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      /// values.
55e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      static void GetTimeUsage(
56e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer        TimeValue& elapsed,
57e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer          ///< Returns the TimeValue::now() giving current time
5863b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman        TimeValue& user_time,
59e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer          ///< Returns the current amount of user time for the process
60e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer        TimeValue& sys_time
61e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer          ///< Returns the current amount of system time for the process
62e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer      );
63e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer
6478202b4c5080bd639ee644048580a22f0bda43feReid Spencer      /// This static function will return the process' current user id number.
6563b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// Not all operating systems support this feature. Where it is not
6663b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// supported, the function should return 65536 as the value.
6778202b4c5080bd639ee644048580a22f0bda43feReid Spencer      static int GetCurrentUserId();
689eb59ec548b861d6ede05b4e6dc22aabf645e665Jeff Cohen
6978202b4c5080bd639ee644048580a22f0bda43feReid Spencer      /// This static function will return the process' current group id number.
7063b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// Not all operating systems support this feature. Where it is not
7163b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// supported, the function should return 65536 as the value.
7278202b4c5080bd639ee644048580a22f0bda43feReid Spencer      static int GetCurrentGroupId();
7378202b4c5080bd639ee644048580a22f0bda43feReid Spencer
7463b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// This function makes the necessary calls to the operating system to
7563b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// prevent core files or any other kind of large memory dumps that can
7668fdcc123b781fcea17f9c7bffdfdc8c41654324Reid Spencer      /// occur when a program fails.
7768fdcc123b781fcea17f9c7bffdfdc8c41654324Reid Spencer      /// @brief Prevent core file generation.
7868fdcc123b781fcea17f9c7bffdfdc8c41654324Reid Spencer      static void PreventCoreFiles();
7968fdcc123b781fcea17f9c7bffdfdc8c41654324Reid Spencer
80a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// This function determines if the standard input is connected directly
81a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// to a user's input (keyboard probably), rather than coming from a file
82a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// or pipe.
83a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      static bool StandardInIsUserInput();
84a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer
8563b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// This function determines if the standard output is connected to a
86a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// "tty" or "console" window. That is, the output would be displayed to
87a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// the user rather than being put on a pipe or stored in a file.
88a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      static bool StandardOutIsDisplayed();
89a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer
9063b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman      /// This function determines if the standard error is connected to a
91a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// "tty" or "console" window. That is, the output would be displayed to
92a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      /// the user rather than being put on a pipe or stored in a file.
93a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer      static bool StandardErrIsDisplayed();
94a01aade75df50eb00c5d0be03594757f9abb3709Reid Spencer
95ec080467f5b322441055de1f6cd4f08edc23d7dfDan Gohman      /// This function determines if the given file descriptor is connected to
96ec080467f5b322441055de1f6cd4f08edc23d7dfDan Gohman      /// a "tty" or "console" window. That is, the output would be displayed to
97ec080467f5b322441055de1f6cd4f08edc23d7dfDan Gohman      /// the user rather than being put on a pipe or stored in a file.
98ec080467f5b322441055de1f6cd4f08edc23d7dfDan Gohman      static bool FileDescriptorIsDisplayed(int fd);
99ec080467f5b322441055de1f6cd4f08edc23d7dfDan Gohman
10001746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// This function determines the number of columns in the window
10101746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// if standard output is connected to a "tty" or "console"
10201746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// window. If standard output is not connected to a tty or
10301746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// console, or if the number of columns cannot be determined,
10401746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// this routine returns zero.
10501746745f1287effa1772ef51b973988afcea699Douglas Gregor      static unsigned StandardOutColumns();
10601746745f1287effa1772ef51b973988afcea699Douglas Gregor
10701746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// This function determines the number of columns in the window
10801746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// if standard error is connected to a "tty" or "console"
10901746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// window. If standard error is not connected to a tty or
11001746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// console, or if the number of columns cannot be determined,
11101746745f1287effa1772ef51b973988afcea699Douglas Gregor      /// this routine returns zero.
11201746745f1287effa1772ef51b973988afcea699Douglas Gregor      static unsigned StandardErrColumns();
113e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin
114e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// This function determines whether the terminal connected to standard
115e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// output supports colors. If standard output is not connected to a
116e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// terminal, this function returns false.
117e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      static bool StandardOutHasColors();
118e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin
119e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// This function determines whether the terminal connected to standard
120e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// error supports colors. If standard error is not connected to a
121e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// terminal, this function returns false.
122e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      static bool StandardErrHasColors();
123e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin
124e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// Whether changing colors requires the output to be flushed.
125e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// This is needed on systems that don't support escape sequences for
126e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// changing colors.
127e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      static bool ColorNeedsFlush();
128e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin
1296c8db3416536ef2917c186eba873fce904ad8c1dTorok Edwin      /// This function returns the colorcode escape sequences.
130e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// If ColorNeedsFlush() is true then this function will change the colors
131e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// and return an empty escape sequence. In that case it is the
132e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// responsibility of the client to flush the output stream prior to
133e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// calling this function.
134e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      static const char *OutputColor(char c, bool bold, bool bg);
135e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin
136e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// Same as OutputColor, but only enables the bold attribute.
137e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      static const char *OutputBold(bool bg);
138e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin
139e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      /// Resets the terminals colors, or returns an escape sequence to do so.
140e8ebb0fe1bba0fdff7475d98e1f8a04804b0b056Torok Edwin      static const char *ResetColor();
141bb0a6126cf1372e3ba90f0f6ff16f7f4d0381105Daniel Dunbar
142bb0a6126cf1372e3ba90f0f6ff16f7f4d0381105Daniel Dunbar      /// Change the program working directory to that given by \arg Path.
143bb0a6126cf1372e3ba90f0f6ff16f7f4d0381105Daniel Dunbar      static void SetWorkingDirectory(std::string Path);
144e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer    /// @}
145e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer  };
146e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer}
147e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer}
148e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
149e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer#endif
150