Processor Counter Monitor
utils.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009-2018, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the dis
9 tribution.
10  * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNES
13 S FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDI
14 NG, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRI
15 CT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 */
17 // written by Roman Dementiev
18 
19 
24 #ifndef PCM_UTILS_HEADER
25 #define PCM_UTILS_HEADER
26 
27 #include <cstdio>
28 #include <cstring>
29 #include <fstream>
30 #include <time.h>
31 #include "types.h"
32 
33 #ifndef _MSC_VER
34 #include <csignal>
35 #include <ctime>
36 #include <cmath>
37 #endif
38 
39 void exit_cleanup(void);
40 void set_signal_handlers(void);
41 void restore_signal_handlers(void);
42 #ifndef _MSC_VER
43 void sigINT_handler(int signum);
44 void sigHUP_handler(int signum);
45 void sigUSR_handler(int signum);
46 void sigSTOP_handler(int signum);
47 void sigCONT_handler(int signum);
48 #endif
49 
50 void set_post_cleanup_callback(void(*cb)(void));
51 
52 #ifdef _MSC_VER
53 inline void win_usleep(int delay_us)
54 {
55  uint64 t1 = 0, t2 = 0, freq = 0;
56  uint64 wait_tick;
57  QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
58  wait_tick = freq * delay_us / 1000000ULL;
59  QueryPerformanceCounter((LARGE_INTEGER *)&t1);
60  do {
61  QueryPerformanceCounter((LARGE_INTEGER *)&t2);
62  _mm_pause();
63  } while ((t2 - t1) < wait_tick);
64 }
65 #endif
66 
67 inline void MySleep(int delay)
68 {
69 #ifdef _MSC_VER
70  if (delay) Sleep(delay * 1000);
71 #else
72  ::sleep(delay);
73 #endif
74 }
75 
76 inline void MySleepMs(int delay_ms)
77 {
78 #ifdef _MSC_VER
79  if (delay_ms) Sleep((DWORD)delay_ms);
80 #else
81  struct timespec sleep_intrval;
82  double complete_seconds;
83  sleep_intrval.tv_nsec = static_cast<long>(1000000000.0 * (::modf(delay_ms / 1000.0, &complete_seconds)));
84  sleep_intrval.tv_sec = static_cast<time_t>(complete_seconds);
85  ::nanosleep(&sleep_intrval, NULL);
86 #endif
87 }
88 
89 inline void MySleepUs(int delay_us)
90 {
91 #ifdef _MSC_VER
92  if (delay_us) win_usleep(delay_us);
93 #else
94  ::usleep(delay_us);
95 
96 #endif
97 }
98 
99 void MySystem(char * sysCmd, char ** argc);
100 
101 #ifdef _MSC_VER
102 #pragma warning (disable : 4068 ) // disable unknown pragma warning
103 #endif
104 
105 #ifdef __GCC__
106 #pragma GCC diagnostic push
107 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
108 #elif defined __clang__
109 #pragma clang diagnostic push
110 #pragma clang diagnostic ignored "-Woverloaded-virtual"
111 #endif
112 struct null_stream : public std::streambuf
113 {
114  void overflow(char) { }
115 };
116 #ifdef __GCC__
117 #pragma GCC diagnostic pop
118 #elif defined __clang__
119 #pragma clang diagnostic pop
120 #endif
121 
122 template <class IntType>
123 inline std::string unit_format(IntType n)
124 {
125  char buffer[1024];
126  if (n <= 9999ULL)
127  {
128  snprintf(buffer, 1024, "%4d ", int32(n));
129  return buffer;
130  }
131  if (n <= 9999999ULL)
132  {
133  snprintf(buffer, 1024, "%4d K", int32(n / 1000ULL));
134  return buffer;
135  }
136  if (n <= 9999999999ULL)
137  {
138  snprintf(buffer, 1024, "%4d M", int32(n / 1000000ULL));
139  return buffer;
140  }
141  if (n <= 9999999999999ULL)
142  {
143  snprintf(buffer, 1024, "%4d G", int32(n / 1000000000ULL));
144  return buffer;
145  }
146 
147  snprintf(buffer, 1024, "%4d T", int32(n / (1000000000ULL * 1000ULL)));
148  return buffer;
149 }
150 
151 void print_cpu_details();
152 
153 #define PCM_UNUSED(x) (void)(x)
154 
155 #define PCM_COMPILE_ASSERT(condition) \
156  typedef char pcm_compile_assert_failed[(condition) ? 1 : -1]; \
157  pcm_compile_assert_failed pcm_compile_assert_failed_; \
158  PCM_UNUSED(pcm_compile_assert_failed_);
159 
160 #ifdef _MSC_VER
161 class ThreadGroupTempAffinity
162 {
163  GROUP_AFFINITY PreviousGroupAffinity;
164 
165  ThreadGroupTempAffinity(); // forbidden
166  ThreadGroupTempAffinity(const ThreadGroupTempAffinity &); // forbidden
167  ThreadGroupTempAffinity & operator = (const ThreadGroupTempAffinity &); // forbidden
168 
169 public:
170  ThreadGroupTempAffinity(uint32 core_id);
171  ~ThreadGroupTempAffinity();
172 };
173 #endif
174 
175 
176 
177 // a secure (but partial) alternative for sscanf
178 // see example usage in pcm-core.cpp
179 typedef std::istringstream pcm_sscanf;
180 
181 class s_expect : public std::string
182 {
183 public:
184  explicit s_expect(const char * s) : std::string(s) {}
185  explicit s_expect(const std::string & s) : std::string(s) {}
186  friend std::istream & operator >> (std::istream & istr, s_expect && s);
187  friend std::istream & operator >> (std::istream && istr, s_expect && s);
188 private:
189 
190  void match(std::istream & istr) const
191  {
192  istr >> std::noskipws;
193  const auto len = length();
194  char * buffer = new char[len + 2];
195  buffer[0] = 0;
196  istr.get(buffer, len+1);
197  if (*this != std::string(buffer))
198  {
199  istr.setstate(std::ios_base::failbit);
200  }
201  delete [] buffer;
202  }
203 };
204 
205 inline std::istream & operator >> (std::istream & istr, s_expect && s)
206 {
207  s.match(istr);
208  return istr;
209 }
210 
211 inline std::istream & operator >> (std::istream && istr, s_expect && s)
212 {
213  s.match(istr);
214  return istr;
215 }
216 
217 inline tm pcm_localtime()
218 {
219  time_t now = time(NULL);
220  tm result;
221 #ifdef _MSC_VER
222  localtime_s(&result, &now);
223 #else
224  localtime_r(&now, &result);
225 #endif
226  return result;
227 }
228 
229 #endif
void sigCONT_handler(int signum)
handles signals that lead to update of configuration such as SIGCONT
Definition: utils.cpp:231
Internal type and constant definitions.
void set_signal_handlers(void)
install various handlers for system signals
Definition: utils.cpp:240
Definition: utils.h:181
void sigSTOP_handler(int signum)
handles signals that lead to update of configuration such as SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU
Definition: utils.cpp:209
void MySystem(char *sysCmd, char **argc)
launches external program in a separate process
Definition: utils.cpp:379
void sigUSR_handler(int signum)
handles signals that lead to update of configuration such as SIGUSR1 and SIGUSR2. for the future exte...
Definition: utils.cpp:197
void sigHUP_handler(int signum)
handles signals that lead to restart the application such as SIGHUP. for example to re-read environme...
Definition: utils.cpp:183
void restore_signal_handlers(void)
Restores default signal handlers under Linux/UNIX.
Definition: utils.cpp:337
void sigINT_handler(int signum)
handles signals that lead to termination of the program such as SIGINT, SIGQUIT, SIGABRT, SIGSEGV, SIGTERM, SIGCHLD this function specifically works when the client aplication launched by pcm – terminates
Definition: utils.cpp:164
void exit_cleanup(void)
handler of exit() call
Definition: utils.cpp:35
Definition: utils.h:112