HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sysutil.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 /////////////////////////////////////////////////////////////////////////
33 /// @file sysutil.h
34 ///
35 /// @brief Platform-independent utilities for various OS, hardware, and
36 /// system resource functionality, all in namespace Sysutil.
37 /////////////////////////////////////////////////////////////////////////
38 
39 
40 #pragma once
41 
42 #include <ctime>
43 #include <string>
44 
45 #ifdef __MINGW32__
46 # include <malloc.h> // for alloca
47 #endif
48 
49 #include <OpenImageIO/export.h>
51 #include <OpenImageIO/platform.h>
53 
54 // Allow client software to know if this version has Sysutil::stacktrace().
55 #define OIIO_HAS_STACKTRACE 1
56 
57 
58 
60 
61 /// @namespace Sysutil
62 ///
63 /// @brief Platform-independent utilities for various OS, hardware, and
64 /// system resource functionality.
65 namespace Sysutil {
66 
67 /// The amount of memory currently being used by this process, in bytes.
68 /// If resident==true (the default), it will report just the resident
69 /// set in RAM; if resident==false, it returns the full virtual arena
70 /// (which can be misleading because gcc allocates quite a bit of
71 /// virtual, but not actually resident until malloced, memory per
72 /// thread).
73 OIIO_API size_t
74 memory_used(bool resident = true);
75 
76 /// The amount of physical RAM on this machine, in bytes.
77 /// If it can't figure it out, it will return 0.
78 OIIO_API size_t
80 
81 /// Convert calendar time pointed by 'time' into local time and save it in
82 /// 'converted_time' variable
83 OIIO_API void
84 get_local_time(const time_t* time, struct tm* converted_time);
85 
86 /// Return the full path of the currently-running executable program.
87 ///
90 
91 /// Return the value of an environment variable (or the empty string_view
92 /// if it is not found in the environment.)
95 
96 /// Sleep for the given number of microseconds.
97 ///
98 OIIO_API void
99 usleep(unsigned long useconds);
100 
101 /// Try to put the process into the background so it doesn't continue to
102 /// tie up any shell that it was launched from. The arguments are the
103 /// argc/argv that describe the program and its command line arguments.
104 /// Return true if successful, false if it was unable to do so.
105 OIIO_API bool
106 put_in_background(int argc, char* argv[]);
107 
108 /// Number of virtual cores available on this platform (including
109 /// hyperthreads).
110 OIIO_API unsigned int
112 
113 /// Number of full hardware cores available on this platform (does not
114 /// include hyperthreads). This is not always accurate and on some
115 /// platforms will return the number of virtual cores.
116 OIIO_API unsigned int
118 
119 /// Get the maximum number of open file handles allowed on this system.
120 OIIO_API size_t
122 
123 /// Return a string containing a readable stack trace from the point where
124 /// it was called. Return an empty string if not supported on this platform.
126 stacktrace();
127 
128 /// Turn on automatic stacktrace dump to the named file if the program
129 /// crashes. Return true if this is properly set up, false if it is not
130 /// possible on this platform. The name may be "stdout" or "stderr" to
131 /// merely print the trace to stdout or stderr, respectively. If the name
132 /// is "", it will disable the auto-stacktrace printing.
133 OIIO_API bool
135 
136 /// Try to figure out how many columns wide the terminal window is. May not
137 /// be correct on all systems, will default to 80 if it can't figure it out.
138 OIIO_API int
140 
141 /// Try to figure out how many rows tall the terminal window is. May not be
142 /// correct on all systems, will default to 24 if it can't figure it out.
143 OIIO_API int
144 terminal_rows();
145 
146 
147 /// Term object encapsulates information about terminal output for the sake
148 /// of constructing ANSI escape sequences.
149 class OIIO_API Term {
150 public:
151  /// Default ctr: assume ANSI escape sequences are ok.
152  Term() {}
153  /// Construct from a FILE*: ANSI codes ok if the file describes a
154  /// live console, otherwise they will be supressed.
155  Term(FILE* file);
156  /// Construct from a stream: ANSI codes ok if the file describes a
157  /// live console, otherwise they will be supressed.
158  Term(const std::ostream& stream);
159 
160  /// ansi("appearance") returns the ANSI escape sequence for the named
161  /// command (if ANSI codes are ok, otherwise it will return the empty
162  /// string). Accepted commands include: "default", "bold", "underscore",
163  /// "blink", "reverse", "concealed", "black", "red", "green", "yellow",
164  /// "blue", "magenta", "cyan", "white", "black_bg", "red_bg",
165  /// "green_bg", "yellow_bg", "blue_bg", "magenta_bg", "cyan_bg",
166  /// "white_bg". Commands may be combined with "," for example:
167  /// "bold,green,white_bg".
168  std::string ansi(string_view command) const;
169 
170  /// ansi("appearance", "text") returns the text, with the formatting
171  /// command, then the text, then the formatting command to return to
172  /// default appearance.
174  {
175  return std::string(ansi(command)) + std::string(text) + ansi("default");
176  }
177 
178  /// Extended color control: take RGB values from 0-255
179  std::string ansi_fgcolor(int r, int g, int b);
180  std::string ansi_bgcolor(int r, int g, int b);
181 
182  bool is_console() const { return m_is_console; }
183 
184 private:
185  bool m_is_console = true; // Default: assume ANSI escape sequences ok.
186 };
187 
188 
189 } // namespace Sysutil
190 
OIIO_API unsigned int hardware_concurrency()
OIIO_API void usleep(unsigned long useconds)
OIIO_API string_view getenv(string_view name)
GT_API const UT_StringHolder filename
GLuint GLuint stream
Definition: glew.h:7265
GLuint const GLchar * name
Definition: glew.h:1814
GT_API const UT_StringHolder time
OIIO_API unsigned int physical_concurrency()
Term()
Default ctr: assume ANSI escape sequences are ok.
Definition: sysutil.h:152
OIIO_API void get_local_time(const time_t *time, struct tm *converted_time)
Platform-related macros.
OIIO_API std::string this_program_path()
OIIO_API size_t memory_used(bool resident=true)
OIIO_API std::string stacktrace()
OIIO_API int terminal_columns()
bool is_console() const
Definition: sysutil.h:182
std::string ansi(string_view command, string_view text) const
Definition: sysutil.h:173
OIIO_API bool put_in_background(int argc, char *argv[])
OIIO_API bool setup_crash_stacktrace(string_view filename)
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
GLsizei const GLchar *const * string
Definition: glew.h:1844
OIIO_API size_t physical_memory()
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
GLint GLint GLint GLint GLsizei GLsizei GLsizei GLboolean resident
Definition: glcorearb.h:3385
OIIO_API size_t max_open_files()
Get the maximum number of open file handles allowed on this system.
OIIO_API int terminal_rows()
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65
GLboolean GLboolean g
Definition: glew.h:9477
#define OIIO_API
Definition: export.h:91