Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pstatclient/pStatClient.h

Go to the documentation of this file.
00001 // Filename: pStatClient.h
00002 // Created by:  drose (09Jul00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef PSTATCLIENT_H
00020 #define PSTATCLIENT_H
00021 
00022 #include <pandabase.h>
00023 
00024 #include "pStatFrameData.h"
00025 
00026 #include <clockObject.h>
00027 #include <luse.h>
00028 #include "pmap.h"
00029 
00030 #ifdef HAVE_NET
00031 #include <connectionManager.h>
00032 #include <queuedConnectionReader.h>
00033 #include <connectionWriter.h>
00034 #include <netAddress.h>
00035 #endif
00036 
00037 class PStatServerControlMessage;
00038 class PStatCollector;
00039 class PStatCollectorDef;
00040 class PStatThread;
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //       Class : PStatClient
00044 // Description : Manages the communications to report statistics via a
00045 //               network connection to a remote PStatServer.
00046 //
00047 //               Normally, there is only one PStatClient in the world,
00048 //               although it is possible to have multiple PStatClients
00049 //               if extraordinary circumstances require in.  Since
00050 //               each PStatCollector registers itself with the
00051 //               PStatClient when it is created, having multiple
00052 //               PStatClients requires special care when constructing
00053 //               the various PStatCollectors.
00054 //
00055 //               If DO_PSTATS is not defined, we don't want to use
00056 //               stats at all.  This class is therefore defined as a
00057 //               stub class.
00058 ////////////////////////////////////////////////////////////////////
00059 #ifdef DO_PSTATS
00060 class EXPCL_PANDA PStatClient : public ConnectionManager {
00061 public:
00062   PStatClient();
00063   ~PStatClient();
00064 
00065   INLINE void set_client_name(const string &name);
00066   INLINE string get_client_name() const;
00067   INLINE void set_max_rate(float rate);
00068   INLINE float get_max_rate() const;
00069 
00070   int get_num_collectors() const;
00071   PStatCollector get_collector(int index) const;
00072   const PStatCollectorDef &get_collector_def(int index) const;
00073   string get_collector_name(int index) const;
00074   string get_collector_fullname(int index) const;
00075 
00076   int get_num_threads() const;
00077   PStatThread get_thread(int index) const;
00078   string get_thread_name(int index) const;
00079 
00080   const ClockObject &get_clock() const;
00081   PStatThread get_main_thread() const;
00082 
00083 PUBLISHED:
00084   INLINE static bool connect(const string &hostname = string(), int port = -1);
00085   INLINE static void disconnect();
00086   INLINE static bool is_connected();
00087 
00088   INLINE static void resume_after_pause();
00089 
00090 public:
00091   static PStatClient *get_global_pstats();
00092 
00093   static void main_tick();
00094   void client_main_tick();
00095   bool client_connect(string hostname, int port);
00096   void client_disconnect();
00097   bool client_is_connected() const;
00098 
00099   void client_resume_after_pause();
00100 
00101 private:
00102   PStatCollector make_collector_with_relname(int parent_index, string relname);
00103   PStatCollector make_collector_with_name(int parent_index, const string &name);
00104   PStatThread make_thread(const string &name);
00105 
00106   bool is_active(int collector_index, int thread_index) const;
00107 
00108   void start(int collector_index, int thread_index);
00109   void start(int collector_index, int thread_index, float as_of);
00110   void stop(int collector_index, int thread_index);
00111   void stop(int collector_index, int thread_index, float as_of);
00112 
00113   void clear_level(int collector_index, int thread_index);
00114   void set_level(int collector_index, int thread_index, float level);
00115   void add_level(int collector_index, int thread_index, float increment);
00116   float get_level(int collector_index, int thread_index) const;
00117 
00118   void new_frame(int thread_index);
00119   void transmit_frame_data(int thread_index);
00120 
00121   void transmit_control_data();
00122 
00123   // Stats collecting stuff
00124   ClockObject _clock;
00125 
00126   typedef pmap<string, int> ThingsByName;
00127   ThingsByName _threads_by_name;
00128 
00129   // This is for the data that is per-collector, per-thread.  A vector
00130   // of these is stored in each Collector object, below, indexed by
00131   // thread index.
00132   class PerThreadData {
00133   public:
00134     PerThreadData();
00135     bool _has_level;
00136     float _level;
00137     int _nested_count;
00138   };
00139   typedef pvector<PerThreadData> PerThread;
00140 
00141   // This is where the meat of the Collector data is stored.  (All the
00142   // stuff in PStatCollector and PStatCollectorDef is just fluff.)
00143   class Collector {
00144   public:
00145     PStatCollectorDef *_def;
00146     ThingsByName _children;
00147 
00148     PerThread _per_thread;
00149   };
00150   typedef pvector<Collector> Collectors;
00151   Collectors _collectors;
00152 
00153   // This defines a single thread, i.e. a separate chain of execution,
00154   // independent of all other threads.  Timing and level data are
00155   // maintained separately for each thread.
00156   class Thread {
00157   public:
00158     string _name;
00159     PStatFrameData _frame_data;
00160     bool _is_active;
00161     int _frame_number;
00162     float _next_packet;
00163   };
00164   typedef pvector<Thread> Threads;
00165   Threads _threads;
00166 
00167 
00168 private:
00169   // Networking stuff
00170   string get_hostname();
00171   void send_hello();
00172   void report_new_collectors();
00173   void report_new_threads();
00174   void handle_server_control_message(const PStatServerControlMessage &message);
00175 
00176   virtual void connection_reset(const PT(Connection) &connection);
00177 
00178   bool _is_connected;
00179   bool _got_udp_port;
00180 
00181   NetAddress _server;
00182   QueuedConnectionReader _reader;
00183   ConnectionWriter _writer;
00184 
00185   PT(Connection) _tcp_connection;
00186   PT(Connection) _udp_connection;
00187 
00188   int _collectors_reported;
00189   int _threads_reported;
00190 
00191   string _hostname;
00192   string _client_name;
00193   float _max_rate;
00194 
00195   static PStatClient *_global_pstats;
00196   friend class PStatCollector;
00197   friend class PStatThread;
00198 };
00199 
00200 #include "pStatClient.I"
00201 
00202 #else  // DO_PSTATS
00203 
00204 class EXPCL_PANDA PStatClient {
00205 public:
00206   PStatClient() { }
00207   ~PStatClient() { }
00208 
00209 PUBLISHED:
00210   INLINE static bool connect(const string & = string(), int = -1) { return false; }
00211   INLINE static void disconnect() { }
00212   INLINE static bool is_connected() { return false; }
00213   INLINE static void resume_after_pause() { }
00214 
00215 public:
00216   static void main_tick() { }
00217 };
00218 
00219 #endif  // DO_PSTATS
00220 
00221 #endif
00222 

Generated on Fri May 2 00:43:27 2003 for Panda by doxygen1.3