Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / utils / logWalker / Thread.cpp
blob72fca9285b1797bee954475c4a97e79063adb53d
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 (void)
49 PeerProcess *pp = 0;
50 this->new_connection_.pop (pp);
51 return pp;
54 PeerProcess *
55 Thread::peek_new_connection (void) 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 (void) const
71 return this->pending_local_addr_;
74 void
75 Thread::handle_request (void)
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 (void) const
121 return static_cast<long> (this->max_depth_);
124 long
125 Thread::id (void) const
127 return this->id_;
130 const ACE_CString &
131 Thread::alias (void) 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");
147 void
148 Thread::incoming_from (PeerProcess *pp)
150 this->incoming_ = pp;
153 PeerProcess *
154 Thread::incoming (void) const
156 return this->incoming_;
159 void
160 Thread::active_handle (long handle)
162 this->active_handle_ = handle;
165 long
166 Thread::active_handle (void) const
168 return this->active_handle_;
171 void
172 Thread::set_dup (Thread *other, bool set_other)
174 this->target_dup_ = other;
175 if (set_other)
177 other->set_dup (this, false);
181 void
182 Thread::clear_dup (void)
184 this->target_dup_ = 0;
187 bool
188 Thread::has_dup (void)
190 return this->target_dup_ != 0;
193 void
194 Thread::swap_target (void)
196 if (target_dup_ != 0 && target_dup_->giop_target() != 0)
198 this->giop_target_->swap (target_dup_->giop_target());
199 this->target_dup_->clear_dup ();
200 this->target_dup_ = 0;
202 else
204 if (target_dup_ == 0)
205 ACE_ERROR ((LM_ERROR, "Thread::swap_target, target_dup_ == 0\n"));
206 else
207 ACE_ERROR ((LM_ERROR, "Thread::swap_target, target_dup_.id = %d, giop_target == 0\n", target_dup_->id()));
211 GIOP_Buffer *
212 Thread::giop_target (void)
214 return this->giop_target_;
217 void
218 Thread::set_giop_target (GIOP_Buffer *buffer)
220 this->giop_target_ = buffer;
221 if (this->target_dup_ != 0)
223 this->target_dup_->clear_dup();
224 this->target_dup_ = 0;
228 void
229 Thread::add_invocation (Invocation *inv)
231 this->invocations_.insert_tail (inv);
234 void
235 Thread::push_invocation (Invocation *inv)
237 this->current_invocation_.push(inv);
240 void
241 Thread::pop_invocation (void)
243 Invocation *inv;
244 this->current_invocation_.pop (inv);
247 Invocation *
248 Thread::current_invocation (void) const
250 Invocation *inv = 0;
251 if (this->current_invocation_.size() > 0)
252 this->current_invocation_.top(inv);
253 return inv;
256 void
257 Thread::dump_summary (ostream &strm)
259 strm << " " << this->alias_ << " tid = 0x" << hex << this->id_
260 << " (" << dec << this->id_
261 << ")\tfirst line " << this->first_line_ << " \t"
262 << this->client_encounters_ << " requests sent "
263 << this->server_encounters_ << " requests received";
264 if (this->count_nesting () > 0 && this->max_depth_ > 0)
266 strm <<", with " << this->nested_ << " nested upcalls, max depth "
267 << this->max_depth_ << endl;
269 if (this->first_time_.length() )
271 strm << " first encountered " << this->first_time_
272 << " last encountered " << this->last_time_;
275 strm << "\n" << endl;
278 void
279 Thread::get_summary (long &sent_reqs,
280 long &recv_reqs,
281 size_t &sent_size,
282 size_t &recv_size)
285 for (ACE_DLList_Iterator <Invocation> i(this->invocations_);
286 !i.done();
287 i.advance())
289 Invocation *inv = 0;
290 i.next(inv);
291 if (inv->sent_request())
293 ++sent_reqs;
294 sent_size += inv->request_bytes();
296 else
298 ++recv_reqs;
299 recv_size += inv->request_bytes();
304 void
305 Thread::dump_invocations (ostream &strm)
307 size_t total_request_bytes = 0;
308 strm << this->alias_ << " handled " << this->invocations_.size()
309 << " invocations" << endl;
311 std::stack<Invocation *> nested;
312 for (ACE_DLList_Iterator <Invocation> i (this->invocations_);
313 !i.done();
314 i.advance())
316 Invocation *inv = 0;
317 i.next(inv);
318 size_t level = 0;
320 while (!nested.empty ())
322 if (nested.top()->contains (inv->req_line ()))
324 level = nested.size();
325 if (level > this->nested_)
326 this->nested_ = level;
327 break;
329 nested.pop();
331 nested.push(inv);
333 inv->dump_detail (strm, level, Invocation::Dump_Proc, false);
334 total_request_bytes += inv->request_bytes();
336 strm << "total request octet count: " << total_request_bytes;
339 void
340 Thread::dump_incidents (ostream &strm, const ACE_Time_Value& relstart)
342 if (this->nested_ == 0)
343 return;
344 strm << "\n" << this->alias_ << " handled " << this->invocations_.size()
345 << " invocations with a max nesting level of " << this->nested_ << endl;
347 std::deque<Invocation *> nested_queue;
348 for (ACE_DLList_Iterator <Invocation> i (this->invocations_);
349 !i.done();
350 i.advance())
352 Invocation *inv = 0;
353 i.next(inv);
354 size_t level = nested_queue.size();
356 while (!nested_queue.empty ())
358 Invocation *prev = nested_queue.back ();
359 if (prev->contains (inv->req_line ()))
361 break;
363 nested_queue.pop_back ();
364 level--;
365 prev->dump_finish_line (strm, level, relstart);
367 if (nested_queue.size() > 1)
369 size_t inv_line = inv->req_line ();
370 for (std::deque<Invocation *>::iterator j = nested_queue.begin ();
371 j != nested_queue.end ();
372 j++)
374 if ((*j)->repl_line () < inv_line)
376 (*j)->dump_finish_line (strm, level, relstart);
380 nested_queue.push_back (inv);
381 inv->dump_start_line (strm, level, relstart);
385 size_t
386 Thread::count_nesting (void)
388 std::stack<Invocation *> nested;
389 for (ACE_DLList_Iterator <Invocation> i (this->invocations_);
390 !i.done();
391 i.advance())
393 Invocation *inv = 0;
394 i.next(inv);
395 size_t level = 0;
396 while (!nested.empty ())
398 level = nested.size();
399 if (level > this->nested_)
400 this->nested_ = level;
401 if (nested.top()->contains (inv->req_line ()))
403 break;
405 nested.pop ();
407 nested.push (inv);
409 return this->nested_;