=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / utils / logWalker / Session.cpp
blob62d0e24da56e2d85b967d4c607f3ce4eaf0fc519
1 #include "Session.h"
2 #include "HostProcess.h"
3 #include "PeerProcess.h"
4 #include "Log.h"
5 #include "ace/OS_NS_strings.h"
6 #include "ace/SString.h"
7 #include "ace/OS_NS_sys_stat.h"
9 long
10 Session::tao_version_ = 220;
12 AltAddresses
13 Session::alt_addrs_;
16 Session::Session ()
17 : split_details_ (false)
19 ACE_CString n ("localhost");
20 ACE_CString v ("127.0.0.1");
21 this->alt_addrs_.bind (n,v);
24 Session::~Session ()
26 for (Processes::iterator i = processes_.begin();
27 i != processes_.end();
28 i++)
30 delete i->item();
34 bool
35 Session::set_tao_version (ACE_TCHAR *str)
37 if (ACE_OS::strncmp(str, ACE_TEXT("1.5"), 3)== 0)
38 tao_version_ = 150;
39 else if (ACE_OS::strncmp (str, ACE_TEXT("1.6"), 3) == 0)
40 tao_version_ = 160;
41 else if (ACE_OS::strncmp (str, ACE_TEXT("1.7"), 3) == 0)
42 tao_version_ = 170;
43 else if (ACE_OS::strncmp (str, ACE_TEXT("1.8"), 3) == 0)
44 tao_version_ = 180;
45 else if (ACE_OS::strncmp (str, ACE_TEXT("2.0"), 3) == 0)
46 tao_version_ = 200;
47 else if (ACE_OS::strncmp (str, ACE_TEXT("2.2"), 3) == 0)
48 tao_version_ = 220;
49 else
50 return false;
51 return true;
54 long
55 Session::tao_version ()
57 return tao_version_;
60 void
61 Session::add_process (HostProcess *proc)
63 if (processes_.bind(proc->pid(),proc) != 0)
64 ACE_ERROR ((LM_ERROR,
65 "Session::add_process could not bind pid %d\n",
66 proc->pid()));
67 if (procs_by_name_.bind(proc->proc_name(), proc) != 0)
68 ACE_ERROR ((LM_ERROR,
69 "Session::add_process could not bind procname %s\n",
70 proc->proc_name().c_str()));
73 void
74 Session::alternate_address (const char *addrspec)
76 const char *equal = ACE_OS::strchr(addrspec,'=');
77 if (equal == 0)
78 return;
79 ACE_CString name (addrspec,(equal - addrspec));
80 ACE_CString value (equal+1);
81 Session::alt_addrs_.bind(name,value);
84 bool
85 Session::is_equivalent (const ACE_CString &primary,
86 const ACE_CString &alternate)
88 ACE_CString test(primary);
89 ACE_CString alt;
90 if (Session::alt_addrs_.find(test,alt) == 0)
92 return alt == alternate;
94 return false;
97 void
98 Session::default_service (const char *addrspec)
100 const char *equal = ACE_OS::strchr(addrspec,'=');
101 if (equal == 0)
102 return;
103 ACE_CString name (addrspec,(equal - addrspec));
104 Endpoint ep (equal+1);
106 static long next_def_pid = 0;
107 --next_def_pid;
108 HostProcess *hp = new HostProcess ("defaulted",next_def_pid);
109 hp->proc_name(name);
110 hp->add_listen_endpoint (ep);
111 this->processes_.bind(next_def_pid,hp);
112 this->procs_by_name_.bind(name,hp);
115 HostProcess *
116 Session::find_process (long pid)
118 Processes::ENTRY *entry = 0;
119 if (this->processes_.find(pid,entry) == 0)
120 return entry->item();
121 else
122 return 0;
125 HostProcess *
126 Session::find_host_i (const Endpoint &endpoint, bool server)
128 for (Processes::ITERATOR i (this->processes_); !i.done(); i.advance())
130 Processes::ENTRY *entry;
131 if (i.next(entry) == 0)
132 break;
133 if (entry->item()->has_endpoint(endpoint, server))
134 return entry->item();
136 return 0;
139 HostProcess *
140 Session::find_host (const Endpoint &endpoint, bool server)
142 HostProcess *result = find_host_i (endpoint, server);
143 if (result != 0)
144 return result;
146 Endpoint test (endpoint);
147 ACE_CString alternate;
148 if (Session::alt_addrs_.find(endpoint.host_,alternate) == 0)
150 test.host_ = alternate;
151 return find_host_i (test, server);
153 return 0;
156 void
157 Session::make_dir (const char *dirname, bool split)
159 this->base_dir_ = dirname;
160 this->split_details_ = split;
163 void
164 Session::outfile (const char *o)
166 this->outfile_ = o;
169 bool
170 Session::has_dir ()
172 return this->base_dir_.length() > 0;
175 bool
176 Session::has_outfile ()
178 return this->outfile_.length() > 0;
181 ostream *
182 Session::stream_for ( ostream *oldstream, HostProcess *hp, const char *sub, const char *detail)
184 if (this->has_dir())
186 ACE_CString outname = this->base_dir_;
188 if (oldstream == 0 && hp == 0)
190 if (ACE_OS::mkdir(outname.c_str()) != 0 && errno != EEXIST)
191 ACE_ERROR ((LM_ERROR,
192 "Session::stream_for unable to make dir %C, %p\n",
193 outname.c_str(), "mkdir"));
195 delete oldstream;
196 outname += ACE_DIRECTORY_SEPARATOR_CHAR;
197 if (hp != 0)
199 outname += hp->proc_name();
200 if (ACE_OS::mkdir(outname.c_str()) != 0 && errno != EEXIST)
201 ACE_ERROR ((LM_ERROR,
202 "Session::stream_for unable to make dir %C, %p\n",
203 outname.c_str(), "mkdir"));
205 outname += ACE_DIRECTORY_SEPARATOR_CHAR;
207 outname += (sub == 0) ? "summary.txt" : sub;
208 if (detail != 0)
210 if (ACE_OS::mkdir(outname.c_str()) != 0 && errno != EEXIST)
211 ACE_ERROR ((LM_ERROR,
212 "Session::stream_for unable to make dir %C, %p\n",
213 outname.c_str(), "mkdir"));
215 outname += ACE_DIRECTORY_SEPARATOR_CHAR;
216 outname += detail;
218 return new ofstream (outname.c_str());
221 if (oldstream != 0)
222 return oldstream;
223 if (this->has_outfile())
224 return new ofstream(this->outfile_.c_str());
225 else
226 return &cout;
229 void
230 Session::dump ()
232 bool single = !this->has_dir();
233 ostream *strm = this->stream_for(0);
234 // report session metrics
236 if (single)
238 this->split_details_ = false;
239 *strm << "Session summary report: "
240 << this->processes_.current_size() << " Processes detected." << endl;
242 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
244 Procs_By_Name::ENTRY *entry;
245 if (i.next(entry) == 0)
246 continue;
247 entry->item()->dump_summary (*strm);
250 if (single)
251 *strm << "\n\n\nSession detail threads report: " << endl;
252 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
254 Procs_By_Name::ENTRY *entry;
255 if (i.next(entry) == 0)
256 continue;
257 strm = this->split_details_ ?
258 stream_for (strm,entry->item(),"threads", "summary.txt"):
259 stream_for (strm,entry->item(),"threads.txt");
260 entry->item()->dump_thread_summary (*strm);
263 if (single)
264 *strm << "\n\n\nSession detail peer process report: " << endl;
265 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
267 Procs_By_Name::ENTRY *entry;
268 if (i.next(entry) == 0)
269 continue;
270 strm = this->split_details_ ?
271 stream_for (strm,entry->item(),"peers", "summary.txt") :
272 stream_for (strm,entry->item(),"peer_processes.txt");
273 entry->item()->dump_peer_summary (*strm);
276 if (single)
277 *strm << "\n\n\nSession detail object report: " << endl;
278 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
280 Procs_By_Name::ENTRY *entry;
281 if (i.next(entry) == 0)
282 continue;
283 strm = stream_for (strm,entry->item(),"objects.txt");
284 entry->item()->dump_object_detail (*strm);
287 if (single)
288 *strm << "\n\n\nSession detail invocation by peer process report: " << endl;
289 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
291 Procs_By_Name::ENTRY *entry;
292 if (i.next(entry) == 0)
293 continue;
294 if (this->split_details_)
296 entry->item()->split_peer_invocations (this);
298 else
300 strm = stream_for (strm,entry->item(),"invocation_by_peer.txt");
301 entry->item()->dump_invocation_detail (*strm);
305 if (single)
306 *strm << "\n\n\nSession detail invocation by thread report: " << endl;
308 ACE_Time_Value start = ACE_Time_Value::zero;
309 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
311 Procs_By_Name::ENTRY *entry;
312 if (i.next(entry) == 0)
313 continue;
314 if (start == ACE_Time_Value::zero ||
315 entry->item()->start_time().msec() < start.msec())
317 start = entry->item()->start_time();
321 for (Procs_By_Name::ITERATOR i (this->procs_by_name_); !i.done(); i.advance())
323 Procs_By_Name::ENTRY *entry;
324 if (i.next(entry) == 0)
325 continue;
326 if (this->split_details_)
328 entry->item()->split_thread_invocations (this, start);
330 else
332 strm = stream_for (strm,entry->item(),"invocation_by_thread.txt");
333 entry->item()->dump_thread_invocations (*strm, start);
336 if (this->has_outfile() || this->has_dir())
337 delete strm;
341 // iterate over the collection of host processes to associate peer
342 // processes via endpoints.
343 void
344 Session::reconcile ()
346 for (Processes::ITERATOR i (this->processes_); !i.done(); i.advance())
348 Processes::ENTRY *entry;
349 if (i.next(entry) == 0)
350 continue;
351 entry->item()->reconcile_peers(this);