Browse Source

remove unused labels from graph

David Rose 21 years ago
parent
commit
3e430c58e1

+ 24 - 9
pandatool/src/pstatserver/pStatPianoRoll.cxx

@@ -288,12 +288,11 @@ void PStatPianoRoll::
 compute_page(const PStatFrameData &frame_data) {
   _start_time = frame_data.get_start();
 
-  PageData::iterator pi;
-  for (pi = _page_data.begin(); pi != _page_data.end(); ++pi) {
-    (*pi).second.clear();
-  }
-
-  size_t num_bars = _page_data.size();
+  // Clear out the page data and copy it to previous, so we can fill
+  // it up again and then check to see if we changed the set of bars
+  // this frame.
+  PageData previous;
+  _page_data.swap(previous);
 
   int num_events = frame_data.get_num_events();
   for (int i = 0; i < num_events; i++) {
@@ -302,12 +301,27 @@ compute_page(const PStatFrameData &frame_data) {
     _page_data[collector_index].add_data_point(time);
   }
 
-  if (_page_data.size() != num_bars) {
-    // If we added some new bars this time, we'll have to update our
-    // list.
+  // Now check to see if the set of bars has changed.
+  bool changed_bars = (_page_data.size() != previous.size());
+
+  if (!changed_bars) {
+    PageData::const_iterator ai, bi;
+    ai = _page_data.begin();
+    bi = previous.begin();
+    while (ai != _page_data.end() && !changed_bars) {
+      changed_bars = ((*ai).first == (*bi).first);
+      ++ai;
+      ++bi;
+    }
+  }
+
+  if (changed_bars) {
+    // If we added or removed some new bars this time, we'll have to
+    // update our list.
     const PStatClientData *client_data = _monitor->get_client_data();
 
     _labels.clear();
+    PageData::const_iterator pi;
     for (pi = _page_data.begin(); pi != _page_data.end(); ++pi) {
       int collector_index = (*pi).first;
       if (client_data->has_collector(collector_index)) {
@@ -323,6 +337,7 @@ compute_page(const PStatFrameData &frame_data) {
 
   // Finally, make sure all of the bars are closed.
   float time = frame_data.get_end();
+  PageData::iterator pi;
   for (pi = _page_data.begin(); pi != _page_data.end(); ++pi) {
     (*pi).second.finish(time);
   }

+ 15 - 0
pandatool/src/pstatserver/pStatStripChart.I

@@ -197,3 +197,18 @@ INLINE float PStatStripChart::
 pixel_to_height(int x) const {
   return _value_height * (float)(get_ysize() - x) / (float)get_ysize();
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: PStatStripChart::is_label_used
+//       Access: Protected
+//  Description: Returns true if the indicated collector appears
+//               anywhere on the chart at the current time, false
+//               otherwise.
+////////////////////////////////////////////////////////////////////
+INLINE bool PStatStripChart::
+is_label_used(int collector_index) const {
+  if (collector_index < (int)_label_usage.size()) {
+    return _label_usage[collector_index] > 0;
+  }
+  return false;
+}

+ 78 - 8
pandatool/src/pstatserver/pStatStripChart.cxx

@@ -117,6 +117,7 @@ update() {
       di = _data.begin();
       while (di != _data.end() &&
              thread_data->get_frame((*di).first).get_start() < oldest_time) {
+        dec_label_usage((*di).second);
         _data.erase(di);
         di = _data.begin();
       }
@@ -153,6 +154,7 @@ set_collector_index(int collector_index) {
     _collector_index = collector_index;
     _title_unknown = true;
     _data.clear();
+    clear_label_usage();
     force_redraw();
     update_labels();
   }
@@ -449,7 +451,7 @@ get_frame_data(int frame_number) {
   const PStatThreadData *thread_data = _view.get_thread_data();
   _view.set_to_frame(thread_data->get_frame(frame_number));
 
-  FrameData &data = _data[frame_number];
+  FrameData &fdata = _data[frame_number];
 
   const PStatViewLevel *level = _view.get_level(_collector_index);
   int num_children = level->get_num_children();
@@ -460,7 +462,7 @@ get_frame_data(int frame_number) {
     cd._i = (unsigned short)i;
     cd._net_value = child->get_net_value();
     if (cd._net_value != 0.0) {
-      data.push_back(cd);
+      fdata.push_back(cd);
     }
   }
 
@@ -471,10 +473,12 @@ get_frame_data(int frame_number) {
   cd._i = (unsigned short)num_children;
   cd._net_value = level->get_value_alone();
   if (cd._net_value != 0.0) {
-    data.push_back(cd);
+    fdata.push_back(cd);
   }
 
-  return data;
+  inc_label_usage(fdata);
+
+  return fdata;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -802,15 +806,17 @@ update_labels() {
   int num_children = level->get_num_children();
   for (int i = 0; i < num_children; i++) {
     const PStatViewLevel *child = level->get_child(i);
-    int collector = child->get_collector();
-    _labels.push_back(collector);
+    int collector_index = child->get_collector();
+    if (is_label_used(collector_index)) {
+      _labels.push_back(collector_index);
+    }
   }
 
   SortCollectorLabels2 sort_labels(get_monitor()->get_client_data());
   sort(_labels.begin(), _labels.end(), sort_labels);
 
-  int collector = level->get_collector();
-  _labels.push_back(collector);
+  int collector_index = level->get_collector();
+  _labels.push_back(collector_index);
 
   _labels_changed = true;
   _level_index = _view.get_level_index();
@@ -959,3 +965,67 @@ draw_pixels(int first_pixel, int last_pixel) {
 
   end_draw(first_pixel, last_pixel);
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: PStatStripChart::clear_label_usage
+//       Access: Private
+//  Description: Erases all elements from the label usage data.
+////////////////////////////////////////////////////////////////////
+void PStatStripChart::
+clear_label_usage() {
+  _label_usage.clear();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PStatStripChart::dec_label_usage
+//       Access: Private
+//  Description: Erases the indicated frame data from the current
+//               label usage.  This indicates that the given FrameData
+//               has fallen off the end of the chart.  This must have
+//               been proceeded by an earlier call to
+//               inc_label_usage() for the same FrameData
+////////////////////////////////////////////////////////////////////
+void PStatStripChart::
+dec_label_usage(const FrameData &fdata) {
+  FrameData::const_iterator fi;
+  for (fi = fdata.begin(); fi != fdata.end(); ++fi) {
+    const ColorData &cd = (*fi);
+    nassertv(cd._collector_index < (int)_label_usage.size());
+    nassertv(_label_usage[cd._collector_index] > 0);
+    _label_usage[cd._collector_index]--;
+    if (_label_usage[cd._collector_index] == 0) {
+      // If a label drops out of usage, it's time to regenerate
+      // labels.
+      _level_index = -1;
+    }
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PStatStripChart::inc_label_usage
+//       Access: Private
+//  Description: Records the labels named in the indicated FrameData
+//               in the table of current labels in use.  This should
+//               be called when the given FrameData has been added to
+//               the chart; it will increment the reference count for
+//               each collector named in the FrameData.  The reference
+//               count will eventually be decremented when
+//               dec_label_usage() is called later.
+////////////////////////////////////////////////////////////////////
+void PStatStripChart::
+inc_label_usage(const FrameData &fdata) {
+  FrameData::const_iterator fi;
+  for (fi = fdata.begin(); fi != fdata.end(); ++fi) {
+    const ColorData &cd = (*fi);
+    while (cd._collector_index >= (int)_label_usage.size()) {
+      _label_usage.push_back(0);
+    }
+    nassertv(_label_usage[cd._collector_index] >= 0);
+    _label_usage[cd._collector_index]++;
+    if (_label_usage[cd._collector_index] == 1) {
+      // If a label appears for the first time, it's time to
+      // regenerate labels.
+      _level_index = -1;
+    }
+  }
+}

+ 9 - 0
pandatool/src/pstatserver/pStatStripChart.h

@@ -114,10 +114,16 @@ protected:
   virtual void end_draw(int from_x, int to_x);
   virtual void idle();
 
+  INLINE bool is_label_used(int collector_index) const;
+
 private:
   void draw_frames(int first_frame, int last_frame);
   void draw_pixels(int first_pixel, int last_pixel);
 
+  void clear_label_usage();
+  void dec_label_usage(const FrameData &fdata);
+  void inc_label_usage(const FrameData &fdata);
+
   PStatView &_view;
   int _collector_index;
   bool _scroll_mode;
@@ -135,6 +141,9 @@ private:
   float _start_time;
   float _value_height;
   bool _title_unknown;
+
+  typedef vector_int LabelUsage;
+  LabelUsage _label_usage;
 };
 
 #include "pStatStripChart.I"