QDP++
qdp_profile.cc
Go to the documentation of this file.
1// -*- C++ -*-
2
8
9#include "qdp.h"
10#include <time.h>
11
12#if defined(QDP_USE_PROFILING)
13#include <stack>
14#include <queue>
15#endif
16
17
18namespace QDP {
19
20static int prof_level = 0;
21static int prog_prof_level = 0;
22#ifdef QDP_USE_PROFILING
23static bool prof_init = false;
24#endif
27
30{
31 struct timeval tp;
32 gettimeofday(&tp,NULL);
33 return((unsigned long)tp.tv_sec * 1000000 + (unsigned long)tp.tv_usec);
34 //struct tms buf;
35 //times(&buf);
36 //return buf.tms_utime + buf.tms_stime;
37 // return (double)clock()/CLOCKS_PER_SEC;
38}
39
40
41//--------------------------------------------------------------------------------------
42// Selectively turn on profiling
43//--------------------------------------------------------------------------------------
44
45#if ! defined(QDP_USE_PROFILING)
46
47//--------------------------------------------------------------------------------------
48// No profiling
49//--------------------------------------------------------------------------------------
50
51void initProfile(const std::string& file, const std::string& caller, int line) {}
52void closeProfile() {}
53void printProfile() {}
54int setProfileLevel(int n) {return prof_level;}
56
57void pushProfileInfo(int level, const std::string& file, const std::string& caller, int line) {}
59
60#else // Profiling enabled
61
62//--------------------------------------------------------------------------------------
63// Profiling enabled
64//--------------------------------------------------------------------------------------
65
66// A stack to hold profile info
67std::stack<QDPProfileInfo_t> infostack;
68
69// A queue for the profile data
70std::queue<QDPProfileHead_t> profqueue;
71
72
73
74void
75QDPProfile_t::init()
76{
77 time = 0;
78 expr = "";
79 count = 0;
80 next = 0;
81}
82
83void
84QDPProfile_t::print()
85{
86 if ((getProfileLevel() & 2) > 0)
87 {
88 QDPIO::cout << expr
89 << "\t[" << time << "]" << std::endl;
90 }
91}
92
93void
94initProfile(const std::string& file, const std::string& caller, int line)
95{
96 if (prof_init)
97 return;
98
99 pushProfileInfo(0, file, caller, line);
100 prof_init = true;
101}
102
103void
105{
106 if (! prof_init)
107 return;
108
110}
111
112void
114{
115 if (profqueue.size() == 0)
116 return;
117
118 while(! profqueue.empty())
119 {
120 QDPProfileHead_t& head = profqueue.front();
121
122 if (head.start != 0)
123 {
124 QDPProfile_t *qp = head.start;
125
126 QDPIO::cout << std::endl
127 << "func = " << head.info.caller
128 << " line = " << head.info.line
129 << " file = " << head.info.file
130 << std::endl;
131
132 while(qp)
133 {
134 if (qp->count > 0)
135 {
136// QDPIO::cout << " " << qp->count << " [" << qp->time << "] " << qp->expr << std::endl;
137
138 // Gag, I still have not really grokked the fricken SL iostream field
139 // width manipulators. Drop down to low tech just to get out
140 // the bleaping thing. Note: the real problem is the QDPIO::cout class
141 // is not really a stream instantiation, hence the needed member functions
142 // are not there. Sigh.
143 char lin[80]; // more than adequate
144 sprintf(lin, " %7d [%8d] ", qp->count, qp->time);
145 QDPIO::cout << lin << qp->expr << std::endl;
146 }
147
148 qp = qp->next;
149 }
150 }
151
152 profqueue.pop();
153 }
154}
155
156int
157setProfileLevel(int n)
158{
159 int old = prof_level;
160 prof_level = n;
161 return old;
162}
163
164int
166{
167 int old = prog_prof_level;
168 prog_prof_level = n;
169 return old;
170}
171
172void pushProfileInfo(int level, const std::string& file, const std::string& caller, int line)
173{
174 QDPProfileInfo_t info(level, file, caller, line);
175 infostack.push(info);
176
177 setProfileLevel(level);
178
179 QDPProfileHead_t head(infostack.top());
180 profqueue.push(head);
181
182 if ((level & 2) > 0)
183 {
184 QDPIO::cout << std::endl
185 << "func = " << head.info.caller
186 << " line = " << head.info.line
187 << " file = " << head.info.file
188 << std::endl;
189 }
190}
191
192void popProfileInfo()
193{
194 if (infostack.empty())
195 {
196 QDPIO::cerr << "popProfileInfo: invalid pop" << std::endl;
197 QDP_abort(1);
198 }
199
200 QDPProfileInfo_t& info = infostack.top();
201 setProfileLevel(info.level);
202
203 infostack.pop();
204}
205
206void
207registerProfile(QDPProfile_t* qp)
208{
209 if (profqueue.empty())
210 {
211 QDPIO::cerr << "registerProfile: profile queue empty" << std::endl;
212 QDP_abort(1);
213 }
214
215 QDPProfileHead_t& head = profqueue.back();
216 if (head.start == 0)
217 {
218 head.start = head.end = qp;
219 }
220 else
221 {
222 head.end->next = qp;
223 head.end = qp;
224 }
225}
226
227#endif // ! defined(QDP_USE_PROFILING)
228
229} // namespace QDP;
StandardOutputStream cout
Definition qdp_stdio.cc:21
StandardOutputStream cerr
Definition qdp_stdio.cc:22
Yet another random number generator.
void printProfile()
int getProgramProfileLevel()
int getProfileLevel()
void popProfileInfo()
QDPTime_t getClockTime()
Get the wallclock time.
void closeProfile()
int setProfileLevel(int n)
void pushProfileInfo(int level, const std::string &file, const std::string &caller, int line)
int setProgramProfileLevel(int n)
static int prog_prof_level
void QDP_abort(int status)
Panic button.
void initProfile(const std::string &file, const std::string &caller, int line)
static int prof_level
unsigned long QDPTime_t
Definition qdp_profile.h:14
Primary include file for QDP.