Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / utils / logWalker / Thread.cpp
blob75708efce0e466e46403c95b55419bcb3dd74e66
1 #include "Thread.h"
2 #include "Invocation.h"
3 #include "PeerProcess.h"
4 #include "GIOP_Buffer.h"
5 #include "ace/OS_NS_stdio.h"
6 #include <stack>
7 #include <deque>
9 Thread::Thread (long tid, const char *alias, size_t offset)
10 : id_(tid),
11 alias_ (alias),
12 max_depth_ (0),
13 client_encounters_ (0),
14 server_encounters_ (0),
15 nested_ (0),
16 pending_(),
17 incoming_(0),
18 new_connection_(),
19 giop_target_(0),
20 target_dup_(0),
21 current_invocation_ (),
22 active_handle_ (0),
23 first_line_ (offset),
24 first_time_ (),
25 last_time_ ()
29 void
30 Thread::add_time (const ACE_CString &time)
32 if (time.length())
34 last_time_ = time;
35 if (first_time_.length() == 0)
36 first_time_ = time;
40 void
41 Thread::push_new_connection (PeerProcess *pp)
43 this->new_connection_.push (pp);
46 PeerProcess *
47 Thread::pop_new_connection ()
49 PeerProcess *pp = 0;
50 this->new_connection_.pop (pp);
51 return pp;
54 PeerProcess *
55 Thread::peek_new_connection () const
57 PeerProcess *pp = 0;
58 this->new_connection_.top (pp);
59 return pp;
62 void
63 Thread::pending_local_addr (const ACE_CString &addr)
65 this->pending_local_addr_ = addr;
68 const ACE_CString &
69 Thread::pending_local_addr () const
71 return this->pending_local_addr_;
74 void
75 Thread::handle_request ()
77 this->server_encounters_++;
78 if (this->pending_.size() > 1)
79 this->nested_++;
82 void
83 Thread::enter_wait (PeerProcess *pp)
85 this->pending_.push (pp);
86 this->client_encounters_++;
87 if (this->pending_.size() > this->max_depth_)
88 this->max_depth_ = this->pending_.size();
89 if (this->pending_.size() > 1)
90 this->nested_++;
93 void
94 Thread::exit_wait (PeerProcess *pp, size_t linenum)
96 PeerProcess *old;
97 if (this->pending_.pop(old) == -1)
98 return;
99 while (old != pp)
101 ACE_ERROR ((LM_ERROR,
102 "Line %d, Ending an invocation to peer %s, but most recent started"
103 " is to peer %s\n", linenum, pp->id(), old->id()));
104 // this->pending_.push(old);
105 if (this->pending_.pop(old) == -1)
106 return;
108 if (this->pending_.top (old) == 0)
110 this->active_handle (old->last_transport ()->handle_);
112 else
114 this->active_handle (0);
118 long
119 Thread::max_depth () const
121 return static_cast<long> (this->max_depth_);
124 long
125 Thread::id () const
127 return this->id_;
130 const ACE_CString &
131 Thread::alias () const
133 return this->alias_;
136 void
137 Thread::split_filename (char *buff, size_t len) const
139 strncpy (buff, this->alias_.c_str(), len);
140 char *c = strchr(buff, '[');
141 *c = '_';
142 c = strchr (c, ']');
143 strcpy (c, ".txt");
146 void
147 Thread::incoming_from (PeerProcess *pp)
149 this->incoming_ = pp;
152 PeerProcess *
153 Thread::incoming () const
155 return this->incoming_;
158 void
159 Thread::active_handle (long handle)
161 this->active_handle_ = handle;
164 long
165 Thread::active_handle () const
167 return this->active_handle_;
170 void
171 Thread::set_dup (Thread *other, bool set_other)
173 this->target_dup_ = other;
174 if (set_other)
176 other->set_dup (this, false);
180 void
181 Thread::clear_dup ()
183 this->target_dup_ = 0;
186 bool
187 Thread::has_dup ()
189 return this->target_dup_ != 0;
192 void
193 Thread::swap_target ()
195 if (target_dup_ != 0 && target_dup_->giop_target() != 0)
197 this->giop_target_->swap (target_dup_->giop_target());
198 this->target_dup_->clear_dup ();
199 this->target_dup_ = 0;
201 else
203 if (target_dup_ == 0)
204 ACE_ERROR ((LM_ERROR, "Thread::swap_target, target_dup_ == 0\n"));
205 else
206 ACE_ERROR ((LM_ERROR, "Thread::swap_target, target_dup_.id = %d, giop_target == 0\n", target_dup_->id()));
210 GIOP_Buffer *
211 Thread::giop_target ()
213 return this->giop_target_;
216 void
217 Thread::set_giop_target (GIOP_Buffer *buffer)
219 this->giop_target_ = buffer;
220 if (this->target_dup_ != 0)
222 this->target_dup_->clear_dup();
223 this->target_dup_ = 0;
227 void
228 Thread::add_invocation (Invocation *inv)
230 this->invocations_.insert_tail (inv);
233 void
234 Thread::push_invocation (Invocation *inv)
236 this->current_invocation_.push(inv);
239 void
240 Thread::pop_invocation ()
242 Invocation *inv;
243 this->current_invocation_.pop (inv);
246 Invocation *
247 Thread::current_invocation () const
249 Invocation *inv = 0;
250 if (this->current_invocation_.size() > 0)
251 this->current_invocation_.top(inv);
252 return inv;
255 void
256 Thread::dump_summary (ostream &strm)
258 strm << " " << this->alias_ << " tid = 0x" << hex << this->id_
259 << " (" << dec << this->id_
260 << ")\tfirst line " << this->first_line_ << " \t"
261 << this->client_encounters_ << " requests sent "
262 << this->server_encounters_ << " requests received";
263 if (this->count_nesting () > 0 && this->max_depth_ > 0)
265 strm <<", with " << this->nested_ << " nested upcalls, max depth "
266 << this->max_depth_ << endl;
268 if (this->first_time_.length() )
270 strm << " first encountered " << this->first_time_
271 << " last encountered " << this->last_time_;
274 strm << "\n" << endl;
277 void
278 Thread::get_summary (long &sent_reqs,
279 long &recv_reqs,
280 size_t &sent_size,
281 size_t &recv_size)
283 for (ACE_DLList_Iterator <Invocation> i(this->invocations_);
284 !i.done();
285 i.advance())
287 Invocation *inv = 0;
288 i.next(inv);
289 if (inv->sent_request())
291 ++sent_reqs;
292 sent_size += inv->request_bytes();
294 else
296 ++recv_reqs;
297 recv_size += inv->request_bytes();
302 void
303 Thread::dump_invocations (ostream &strm)
305 size_t total_request_bytes = 0;
306 strm << this->alias_ << " handled " << this->invocations_.size()
307 << " invocations" << endl;
309 std::stack<Invocation *> nested;
310 for (ACE_DLList_Iterator <Invocation> i (this->invocations_);
311 !i.done();
312 i.advance())
314 Invocation *inv = 0;
315 i.next(inv);
316 size_t level = 0;
318 while (!nested.empty ())
320 if (nested.top()->contains (inv->req_line ()))
322 level = nested.size();
323 if (level > this->nested_)
324 this->nested_ = level;
325 break;
327 nested.pop();
329 nested.push(inv);
331 inv->dump_detail (strm, level, Invocation::Dump_Proc, false);
332 total_request_bytes += inv->request_bytes();
334 strm << "total request octet count: " << total_request_bytes;
337 void
338 Thread::dump_incidents (ostream &strm, const ACE_Time_Value& relstart)
340 if (this->nested_ == 0)
341 return;
342 strm << "\n" << this->alias_ << " handled " << this->invocations_.size()
343 << " invocations with a max nesting level of " << this->nested_ << endl;
345 std::deque<Invocation *> nested_queue;
346 for (ACE_DLList_Iterator <Invocation> i (this->invocations_);
347 !i.done();
348 i.advance())
350 Invocation *inv = 0;
351 i.next(inv);
352 size_t level = nested_queue.size();
354 while (!nested_queue.empty ())
356 Invocation *prev = nested_queue.back ();
357 if (prev->contains (inv->req_line ()))
359 break;
361 nested_queue.pop_back ();
362 level--;
363 prev->dump_finish_line (strm, level, relstart);
365 if (nested_queue.size() > 1)
367 size_t inv_line = inv->req_line ();
368 for (std::deque<Invocation *>::iterator j = nested_queue.begin ();
369 j != nested_queue.end ();
370 j++)
372 if ((*j)->repl_line () < inv_line)
374 (*j)->dump_finish_line (strm, level, relstart);
378 nested_queue.push_back (inv);
379 inv->dump_start_line (strm, level, relstart);
383 size_t
384 Thread::count_nesting ()
386 std::stack<Invocation *> nested;
387 for (ACE_DLList_Iterator <Invocation> i (this->invocations_);
388 !i.done();
389 i.advance())
391 Invocation *inv = 0;
392 i.next(inv);
393 size_t level = 0;
394 while (!nested.empty ())
396 level = nested.size();
397 if (level > this->nested_)
398 this->nested_ = level;
399 if (nested.top()->contains (inv->req_line ()))
401 break;
403 nested.pop ();
405 nested.push (inv);
407 return this->nested_;