Panda3D
textMonitor.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file textMonitor.cxx
10  * @author drose
11  * @date 2000-07-12
12  */
13 
14 #include "textMonitor.h"
15 #include "textStats.h"
16 #include "pStatCollectorDef.h"
17 #include "pStatFrameData.h"
18 #include "indent.h"
19 #include <stdio.h> // sprintf
20 
21 /**
22  *
23  */
24 TextMonitor::
25 TextMonitor(TextStats *server, std::ostream *outStream, bool show_raw_data ) : PStatMonitor(server) {
26  _outStream = outStream; //[PECI]
27  _show_raw_data = show_raw_data;
28 }
29 
30 /**
31  * Returns the server that owns this monitor.
32  */
36 }
37 
38 /**
39  * Should be redefined to return a descriptive name for the type of
40  * PStatsMonitor this is.
41  */
42 std::string TextMonitor::
44  return "Text Stats";
45 }
46 
47 /**
48  * Called when the "hello" message has been received from the client. At this
49  * time, the client's hostname and program name will be known.
50  */
51 void TextMonitor::
53  nout << "Now connected to " << get_client_progname() << " on host "
54  << get_client_hostname() << "\n";
55 }
56 
57 /**
58  * Like got_hello(), this is called when the "hello" message has been received
59  * from the client. At this time, the client's hostname and program name will
60  * be known. However, the client appears to be an incompatible version and
61  * the connection will be terminated; the monitor should issue a message to
62  * that effect.
63  */
64 void TextMonitor::
65 got_bad_version(int client_major, int client_minor,
66  int server_major, int server_minor) {
67  nout
68  << "Rejected connection by " << get_client_progname()
69  << " from " << get_client_hostname()
70  << ". Client uses PStats version "
71  << client_major << "." << client_minor
72  << ", while server expects PStats version "
73  << server_major << "." << server_minor << ".\n";
74 }
75 
76 /**
77  * Called as each frame's data is made available. There is no gurantee the
78  * frames will arrive in order, or that all of them will arrive at all. The
79  * monitor should be prepared to accept frames received out-of-order or
80  * missing.
81  */
82 void TextMonitor::
83 new_data(int thread_index, int frame_number) {
84  PStatView &view = get_view(thread_index);
85  const PStatThreadData *thread_data = view.get_thread_data();
86 
87  if (frame_number == thread_data->get_latest_frame_number()) {
88  view.set_to_frame(frame_number);
89 
90  if (view.all_collectors_known()) {
91  const PStatClientData *client_data = get_client_data();
92 
93  (*_outStream) << "\rThread "
94  << client_data->get_thread_name(thread_index)
95  << " frame " << frame_number << ", "
96  << view.get_net_value() * 1000.0 << " ms ("
97  << thread_data->get_frame_rate() << " Hz):\n";
98 
99  if (_show_raw_data) {
100  const PStatFrameData &frame_data = thread_data->get_frame(frame_number);
101  (*_outStream) << "raw data:\n";
102  int num_events = frame_data.get_num_events();
103  for (int i = 0; i < num_events; ++i) {
104  // The iomanipulators are much too clumsy.
105  char formatted[32];
106  sprintf(formatted, "%15.06lf", frame_data.get_time(i));
107  (*_outStream) << formatted;
108 
109  if (frame_data.is_start(i)) {
110  (*_outStream) << " start ";
111  } else {
112  (*_outStream) << " stop ";
113  }
114 
115  int collector_index = frame_data.get_time_collector(i);
116  (*_outStream) << client_data->get_collector_fullname(collector_index) << "\n";
117  }
118  }
119 
120  const PStatViewLevel *level = view.get_top_level();
121  int num_children = level->get_num_children();
122  for (int i = 0; i < num_children; i++) {
123  show_ms(level->get_child(i), 2);
124  }
125 
126  int num_toplevel_collectors = client_data->get_num_toplevel_collectors();
127  for (int tc = 0; tc < num_toplevel_collectors; tc++) {
128  int collector = client_data->get_toplevel_collector(tc);
129  if (client_data->has_collector(collector) &&
130  client_data->get_collector_has_level(collector, thread_index)) {
131 
132  PStatView &level_view = get_level_view(collector, thread_index);
133  level_view.set_to_frame(frame_number);
134  const PStatViewLevel *level = level_view.get_top_level();
135  show_level(level, 2);
136  }
137  }
138  }
139  }
140  _outStream->flush();
141 }
142 
143 
144 /**
145  * Called whenever the connection to the client has been lost. This is a
146  * permanent state change. The monitor should update its display to represent
147  * this, and may choose to close down automatically.
148  */
149 void TextMonitor::
151  nout << "Lost connection.\n";
152 }
153 
154 /**
155  * Should be redefined to return true if this monitor class can handle running
156  * in a sub-thread.
157  *
158  * This is not related to the question of whether it can handle multiple
159  * different PStatThreadDatas; this is strictly a question of whether or not
160  * the monitor itself wants to run in a sub-thread.
161  */
162 bool TextMonitor::
164  return false;
165 }
166 
167 /**
168  *
169  */
170 void TextMonitor::
171 show_ms(const PStatViewLevel *level, int indent_level) {
172  int collector_index = level->get_collector();
173 
174  const PStatClientData *client_data = get_client_data();
175  const PStatCollectorDef &def = client_data->get_collector_def(collector_index);
176 
177  indent((*_outStream), indent_level)
178  << def._name << " = " << level->get_net_value() * 1000.0 << " ms\n" ;
179 
180  int num_children = level->get_num_children();
181  for (int i = 0; i < num_children; i++) {
182  show_ms(level->get_child(i), indent_level + 2);
183  }
184 }
185 
186 /**
187  *
188  */
189 void TextMonitor::
190 show_level(const PStatViewLevel *level, int indent_level) {
191  int collector_index = level->get_collector();
192 
193  const PStatClientData *client_data = get_client_data();
194  const PStatCollectorDef &def = client_data->get_collector_def(collector_index);
195 
196  indent((*_outStream), indent_level)
197  << def._name << " = " << level->get_net_value() << " "
198  << def._level_units << "\n";
199 
200  int num_children = level->get_num_children();
201  for (int i = 0; i < num_children; i++) {
202  show_level(level->get_child(i), indent_level + 2);
203  }
204 }
const PStatViewLevel * get_top_level()
Returns a pointer to the level that corresponds to the Collector we've constrained to.
Definition: pStatView.cxx:239
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_toplevel_collector(int index) const
Returns the collector index of the nth toplevel collector.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_to_frame(const PStatFrameData &frame_data)
Supplies the View with the data for the current frame.
Definition: pStatView.cxx:194
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PStatView & get_level_view(int collector_index, int thread_index)
Returns a view on the level value (as opposed to elapsed time) for the given collector over the given...
const PStatViewLevel * get_child(int n) const
Returns the nth child of this Level/Collector.
const PStatFrameData & get_frame(int frame_number) const
Returns a FrameData structure associated with the indicated frame number.
The data associated with a particular client, but not with any one particular frame or thread: the li...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
const PStatCollectorDef & get_collector_def(int index) const
Returns the nth collector definition.
std::string get_collector_fullname(int index) const
Returns the "full name" of the indicated collector.
std::string get_client_hostname() const
Returns the hostname of the client we're connected to, if known.
Definition: pStatMonitor.I:58
virtual void lost_connection()
Called whenever the connection to the client has been lost.
virtual std::string get_monitor_name()
Should be redefined to return a descriptive name for the type of PStatsMonitor this is.
Definition: textMonitor.cxx:43
TextStats * get_server()
Returns the server that owns this monitor.
Definition: textMonitor.cxx:34
double get_net_value() const
Returns the total value accounted for by the frame (or by whatever Collector we are constrained to).
Definition: pStatView.cxx:222
This is a single level value, or band of color, within a View.
A simple, scrolling-text stats server.
Definition: textStats.h:29
double get_net_value() const
Returns the total level value (or elapsed time) represented by this Collector, including all values i...
This is an abstract class that presents the interface to any number of different front-ends for the s...
Definition: pStatMonitor.h:39
bool has_collector(int index) const
Returns true if the indicated collector has been defined by the client already, false otherwise.
virtual void got_bad_version(int client_major, int client_minor, int server_major, int server_minor)
Like got_hello(), this is called when the "hello" message has been received from the client.
Definition: textMonitor.cxx:65
PStatServer * get_server()
Returns the server that owns this monitor.
Definition: pStatMonitor.I:18
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
Contains the raw timing and level data for a single frame.
double get_frame_rate() const
Computes the average frame rate over the past pstats_average_time seconds, by counting up the number ...
double get_time(size_t n) const
Returns the timestamp of the nth event, in seconds elapsed since some undefined epoch (which is guara...
A View boils down the frame data to a linear list of times spent in a number of different Collectors,...
Definition: pStatView.h:31
PStatView & get_view(int thread_index)
Returns a view on the given thread index.
bool all_collectors_known() const
After a call to set_to_frame(), this returns true if all collectors in the FrameData are known by the...
Definition: pStatView.cxx:212
std::string get_client_progname() const
Returns the program name of the client we're connected to, if known.
Definition: pStatMonitor.I:69
A collection of FrameData structures for recently-received frames within a particular thread.
int get_time_collector(size_t n) const
Returns the index of the collector associated with the nth event.
const PStatThreadData * get_thread_data()
Returns the current PStatThreadData associated with the view.
Definition: pStatView.I:19
bool is_start(size_t n) const
Returns true if the nth event represents a start event, or false if it represents a stop event.
int get_collector() const
Returns the Collector index associated with this level.
size_t get_num_events() const
Returns the number of individual events stored in the FrameData.
const PStatClientData * get_client_data() const
Returns the client data associated with this monitor.
Definition: pStatMonitor.I:26
int get_num_children() const
Returns the number of children of this Level/Collector.
virtual void got_hello()
Called when the "hello" message has been received from the client.
Definition: textMonitor.cxx:52
Defines the details about the Collectors: the name, the suggested color, etc.
virtual bool is_thread_safe()
Should be redefined to return true if this monitor class can handle running in a sub-thread.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void new_data(int thread_index, int frame_number)
Called as each frame's data is made available.
Definition: textMonitor.cxx:83
int get_latest_frame_number() const
Returns the frame number of the most recent frame stored in the data.
int get_num_toplevel_collectors() const
Returns the total number of collectors that are toplevel collectors.
std::string get_thread_name(int index) const
Returns the name of the indicated thread.
bool get_collector_has_level(int index, int thread_index) const
Returns whether the given collector has level data (and consequently, whether it should appear on the...