Intel(r) Performance Counter Monitor
utils.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009-2013, 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 "cpucounters.h"
31 
32 #ifndef _MSC_VER
33 #include <csignal>
34 #include <ctime>
35 #include <cmath>
36 #endif
37 
38 void exit_cleanup(void);
39 void set_signal_handlers(void);
40 void restore_signal_handlers(void);
41 #ifndef _MSC_VER
42 void sigINT_handler(int signum);
43 void sigHUP_handler(int signum);
44 void sigUSR_handler(int signum);
45 void sigSTOP_handler(int signum);
46 void sigCONT_handler(int signum);
47 #endif
48 
49 #ifdef _MSC_VER
50 inline void win_usleep(int delay_us)
51 {
52  uint64 t1 = 0, t2 = 0, freq = 0;
53  uint64 wait_tick;
54  QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
55  wait_tick = freq * delay_us / 1000000ULL;
56  QueryPerformanceCounter((LARGE_INTEGER *) &t1);
57  do {
58  QueryPerformanceCounter((LARGE_INTEGER *) &t2);
59  YieldProcessor();
60  } while ((t2-t1) < wait_tick);
61 }
62 #endif
63 
64 inline void MySleep(int delay)
65 {
66 #ifdef _MSC_VER
67  if(delay) Sleep(delay*1000);
68 #else
69  ::sleep(delay);
70 #endif
71 }
72 
73 inline void MySleepMs(int delay_ms)
74 {
75 #ifdef _MSC_VER
76  if(delay_ms) Sleep(delay_ms);
77 #else
78  struct timespec sleep_intrval;
79  double complete_seconds;
80  sleep_intrval.tv_nsec = static_cast<long>(1000000000.0*(::modf(delay_ms/1000.0,&complete_seconds)));
81  sleep_intrval.tv_sec = static_cast<time_t>(complete_seconds);
82  ::nanosleep(&sleep_intrval, NULL);
83 #endif
84 }
85 
86 inline void MySleepUs(int delay_us)
87 {
88 #ifdef _MSC_VER
89  if(delay_us) win_usleep(delay_us);
90 #else
91  ::usleep(delay_us);
92 
93 #endif
94 }
95 
96 void MySystem(char * sysCmd, char ** argc);
97 
98 struct null_stream : public std::streambuf
99 {
100  void overflow(char) { }
101 };
102 
103 template <class IntType>
104 inline std::string unit_format(IntType n)
105 {
106  char buffer[1024];
107  if (n <= 9999ULL)
108  {
109  sprintf(buffer, "%4d ", int32(n));
110  return buffer;
111  }
112  if (n <= 9999999ULL)
113  {
114  sprintf(buffer, "%4d K", int32(n / 1000ULL));
115  return buffer;
116  }
117  if (n <= 9999999999ULL)
118  {
119  sprintf(buffer, "%4d M", int32(n / 1000000ULL));
120  return buffer;
121  }
122  if (n <= 9999999999999ULL)
123  {
124  sprintf(buffer, "%4d G", int32(n / 1000000000ULL));
125  return buffer;
126  }
127 
128  sprintf(buffer, "%4d T", int32(n / (1000000000ULL * 1000ULL)));
129  return buffer;
130 }
131 
132 
133 #define PCM_UNUSED(x) (void)(x)
134 
135 #define PCM_COMPILE_ASSERT(condition) \
136  typedef char pcm_compile_assert_failed [ (condition) ? 1 : -1 ]; \
137  pcm_compile_assert_failed pcm_compile_assert_failed_; \
138  PCM_UNUSED(pcm_compile_assert_failed_);
139 
140 #ifdef COMPILE_FOR_WINDOWS_7
141 #ifdef _MSC_VER
142 class ThreadGroupTempAffinity
143 {
144  GROUP_AFFINITY PreviousGroupAffinity;
145 
146  ThreadGroupTempAffinity(); // forbidden
147  ThreadGroupTempAffinity(const ThreadGroupTempAffinity &); // forbidden
148 public:
149  ThreadGroupTempAffinity(uint32 core_id);
150  ~ThreadGroupTempAffinity();
151 };
152 #endif
153 #endif
154 
155 #endif
156 
void sigCONT_handler(int signum)
handles signals that lead to update of configuration such as SIGCONT
Definition: utils.cpp:214
void set_signal_handlers(void)
install various handlers for system signals
Definition: utils.cpp:223
Main CPU counters header.
void sigSTOP_handler(int signum)
handles signals that lead to update of configuration such as SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU
Definition: utils.cpp:192
void MySystem(char *sysCmd, char **argc)
launches external program in a separate process
Definition: utils.cpp:333
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:180
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:166
void restore_signal_handlers(void)
Restores default signal handlers under Linux/UNIX.
Definition: utils.cpp:296
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:147
void exit_cleanup(void)
handler of exit() call
Definition: utils.cpp:33
Definition: utils.h:98