Process.h revision 239462
1239462Sdim//===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This file declares the llvm::sys::Process class.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14218885Sdim#ifndef LLVM_SYSTEM_PROCESS_H
15218885Sdim#define LLVM_SYSTEM_PROCESS_H
16218885Sdim
17218885Sdim#include "llvm/Support/TimeValue.h"
18218885Sdim
19218885Sdimnamespace llvm {
20218885Sdimnamespace sys {
21218885Sdim
22218885Sdim  /// This class provides an abstraction for getting information about the
23218885Sdim  /// currently executing process.
24218885Sdim  /// @since 1.4
25218885Sdim  /// @brief An abstraction for operating system processes.
26218885Sdim  class Process {
27218885Sdim    /// @name Accessors
28218885Sdim    /// @{
29218885Sdim    public:
30218885Sdim      /// This static function will return the operating system's virtual memory
31218885Sdim      /// page size.
32218885Sdim      /// @returns The number of bytes in a virtual memory page.
33218885Sdim      /// @brief Get the virtual memory page size
34218885Sdim      static unsigned GetPageSize();
35218885Sdim
36218885Sdim      /// This static function will return the total amount of memory allocated
37218885Sdim      /// by the process. This only counts the memory allocated via the malloc,
38218885Sdim      /// calloc and realloc functions and includes any "free" holes in the
39218885Sdim      /// allocated space.
40218885Sdim      /// @brief Return process memory usage.
41218885Sdim      static size_t GetMallocUsage();
42218885Sdim
43218885Sdim      /// This static function will return the total memory usage of the
44218885Sdim      /// process. This includes code, data, stack and mapped pages usage. Notei
45218885Sdim      /// that the value returned here is not necessarily the Running Set Size,
46218885Sdim      /// it is the total virtual memory usage, regardless of mapped state of
47218885Sdim      /// that memory.
48218885Sdim      static size_t GetTotalMemoryUsage();
49218885Sdim
50218885Sdim      /// This static function will set \p user_time to the amount of CPU time
51218885Sdim      /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
52218885Sdim      /// time spent in system (kernel) mode.  If the operating system does not
53218885Sdim      /// support collection of these metrics, a zero TimeValue will be for both
54218885Sdim      /// values.
55218885Sdim      static void GetTimeUsage(
56218885Sdim        TimeValue& elapsed,
57218885Sdim          ///< Returns the TimeValue::now() giving current time
58218885Sdim        TimeValue& user_time,
59218885Sdim          ///< Returns the current amount of user time for the process
60218885Sdim        TimeValue& sys_time
61218885Sdim          ///< Returns the current amount of system time for the process
62218885Sdim      );
63218885Sdim
64218885Sdim      /// This static function will return the process' current user id number.
65218885Sdim      /// Not all operating systems support this feature. Where it is not
66218885Sdim      /// supported, the function should return 65536 as the value.
67218885Sdim      static int GetCurrentUserId();
68218885Sdim
69218885Sdim      /// This static function will return the process' current group id number.
70218885Sdim      /// Not all operating systems support this feature. Where it is not
71218885Sdim      /// supported, the function should return 65536 as the value.
72218885Sdim      static int GetCurrentGroupId();
73218885Sdim
74218885Sdim      /// This function makes the necessary calls to the operating system to
75218885Sdim      /// prevent core files or any other kind of large memory dumps that can
76218885Sdim      /// occur when a program fails.
77218885Sdim      /// @brief Prevent core file generation.
78218885Sdim      static void PreventCoreFiles();
79218885Sdim
80218885Sdim      /// This function determines if the standard input is connected directly
81218885Sdim      /// to a user's input (keyboard probably), rather than coming from a file
82218885Sdim      /// or pipe.
83218885Sdim      static bool StandardInIsUserInput();
84218885Sdim
85218885Sdim      /// This function determines if the standard output is connected to a
86218885Sdim      /// "tty" or "console" window. That is, the output would be displayed to
87218885Sdim      /// the user rather than being put on a pipe or stored in a file.
88218885Sdim      static bool StandardOutIsDisplayed();
89218885Sdim
90218885Sdim      /// This function determines if the standard error is connected to a
91218885Sdim      /// "tty" or "console" window. That is, the output would be displayed to
92218885Sdim      /// the user rather than being put on a pipe or stored in a file.
93218885Sdim      static bool StandardErrIsDisplayed();
94218885Sdim
95218885Sdim      /// This function determines if the given file descriptor is connected to
96218885Sdim      /// a "tty" or "console" window. That is, the output would be displayed to
97218885Sdim      /// the user rather than being put on a pipe or stored in a file.
98218885Sdim      static bool FileDescriptorIsDisplayed(int fd);
99218885Sdim
100239462Sdim      /// This function determines if the given file descriptor is displayd and
101239462Sdim      /// supports colors.
102239462Sdim      static bool FileDescriptorHasColors(int fd);
103239462Sdim
104218885Sdim      /// This function determines the number of columns in the window
105218885Sdim      /// if standard output is connected to a "tty" or "console"
106218885Sdim      /// window. If standard output is not connected to a tty or
107218885Sdim      /// console, or if the number of columns cannot be determined,
108218885Sdim      /// this routine returns zero.
109218885Sdim      static unsigned StandardOutColumns();
110218885Sdim
111218885Sdim      /// This function determines the number of columns in the window
112218885Sdim      /// if standard error is connected to a "tty" or "console"
113218885Sdim      /// window. If standard error is not connected to a tty or
114218885Sdim      /// console, or if the number of columns cannot be determined,
115218885Sdim      /// this routine returns zero.
116218885Sdim      static unsigned StandardErrColumns();
117218885Sdim
118218885Sdim      /// This function determines whether the terminal connected to standard
119218885Sdim      /// output supports colors. If standard output is not connected to a
120218885Sdim      /// terminal, this function returns false.
121218885Sdim      static bool StandardOutHasColors();
122218885Sdim
123218885Sdim      /// This function determines whether the terminal connected to standard
124218885Sdim      /// error supports colors. If standard error is not connected to a
125218885Sdim      /// terminal, this function returns false.
126218885Sdim      static bool StandardErrHasColors();
127218885Sdim
128218885Sdim      /// Whether changing colors requires the output to be flushed.
129218885Sdim      /// This is needed on systems that don't support escape sequences for
130218885Sdim      /// changing colors.
131218885Sdim      static bool ColorNeedsFlush();
132218885Sdim
133218885Sdim      /// This function returns the colorcode escape sequences.
134218885Sdim      /// If ColorNeedsFlush() is true then this function will change the colors
135218885Sdim      /// and return an empty escape sequence. In that case it is the
136218885Sdim      /// responsibility of the client to flush the output stream prior to
137218885Sdim      /// calling this function.
138218885Sdim      static const char *OutputColor(char c, bool bold, bool bg);
139218885Sdim
140218885Sdim      /// Same as OutputColor, but only enables the bold attribute.
141218885Sdim      static const char *OutputBold(bool bg);
142218885Sdim
143234982Sdim      /// This function returns the escape sequence to reverse forground and
144234982Sdim      /// background colors.
145234982Sdim      static const char *OutputReverse();
146234982Sdim
147218885Sdim      /// Resets the terminals colors, or returns an escape sequence to do so.
148218885Sdim      static const char *ResetColor();
149239462Sdim
150239462Sdim      /// Get the result of a process wide random number generator. The
151239462Sdim      /// generator will be automatically seeded in non-deterministic fashion.
152239462Sdim      static unsigned GetRandomNumber();
153218885Sdim    /// @}
154218885Sdim  };
155218885Sdim}
156218885Sdim}
157218885Sdim
158218885Sdim#endif
159