ESA JPIP server  0.1
app_info.h
Go to the documentation of this file.
1 #ifndef _APP_INFO_H_
2 #define _APP_INFO_H_
3 
4 
5 #include <assert.h>
6 #include <iostream>
7 #include <iomanip>
8 
9 #ifndef _NO_READPROC
10 #include <proc/readproc.h>
11 #endif
12 
13 
14 using namespace std;
15 
16 
21 class AppInfo
22 {
23 private:
28  struct Data
29  {
30  int father_pid;
31  int child_pid;
34 
38  void Reset()
39  {
40  father_pid = 0;
41  child_pid = 0;
42  num_connections = 0;
43  child_iterations = 0;
44  }
45  };
46 
47  int shmid;
48  int lock_file;
50  bool is_running_;
52  double child_memory_;
53  unsigned long time_;
54  double father_memory_;
56  unsigned long child_time_;
57 
61  string GetProcStat_(int pid, int field) const;
62 
66  template<typename TYPE> TYPE GetProcStat(int pid, int field) const
67  {
68  TYPE val;
69  string str = GetProcStat_(pid, field);
70  istringstream(str) >> val;
71  return val;
72  }
73 
74 public:
79  {
80  shmid = -1;
81  lock_file = -1;
82  child_memory_ = 0;
83  father_memory_ = 0;
84  is_running_ = false;
85  available_memory_ = 0;
86  child_time_ = 0;
87  data_ptr = NULL;
88  num_threads_ = 0;
89  time_ = 0;
90  }
91 
97  bool Init();
98 
102  bool is_running() const
103  {
104  return is_running_;
105  }
106 
107  friend ostream& operator <<(ostream &out, const AppInfo &app)
108  {
109  out << "Status: " << (app.is_running() ? "running" : "stopped") << endl;
110  out << "Available memory: " << setiosflags(ios::fixed) << setprecision(2) << app.available_memory() << " MB" << endl;
111 
112  if(app.is_running()) {
113  out << "Father PID: " << app->father_pid << endl;
114  out << "Child PID: " << app->child_pid << endl;
115  out << "Child threads: " << app.num_threads() << endl;
116  out << "Child iterations: " << app->child_iterations << endl;
117  out << "Num. connections: " << app->num_connections << endl;
118  out << "Father used memory: " << setiosflags(ios::fixed) << setprecision(2) << app.father_memory() << " MB" << endl;
119  out << "Child used memory: " << setiosflags(ios::fixed) << setprecision(2) << app.child_memory() << " MB" << endl;
120  }
121 
122  return out;
123  }
124 
128  AppInfo& Update();
129 
133  double available_memory() const
134  {
135  return available_memory_;
136  }
137 
141  double father_memory() const
142  {
143  return father_memory_;
144  }
145 
149  double child_memory() const
150  {
151  return child_memory_;
152  }
153 
157  int num_threads() const
158  {
159  return num_threads_;
160  }
161 
165  unsigned long child_time() const
166  {
167  return child_time_;
168  }
169 
173  unsigned long time() const
174  {
175  return time_;
176  }
177 
178  Data *operator->() const
179  {
180  assert(data_ptr);
181  return data_ptr;
182  }
183 
184  ~AppInfo();
185 };
186 
187 
188 #endif /* _APP_INFO_H_ */
bool is_running() const
Returns true if the application is running.
Definition: app_info.h:102
unsigned long time() const
Returns the time spent by the father process.
Definition: app_info.h:173
unsigned long child_time_
Time spend by the child.
Definition: app_info.h:56
STL namespace.
unsigned long time_
Time spent by the father.
Definition: app_info.h:53
Data * operator->() const
Definition: app_info.h:178
int num_threads_
Number of active threads.
Definition: app_info.h:51
Contains the run-time information of the application.
Definition: app_info.h:21
unsigned long child_time() const
Returns the time spent by the child process.
Definition: app_info.h:165
int num_threads() const
Returns the number of active threads.
Definition: app_info.h:157
int father_pid
PID of the father process.
Definition: app_info.h:30
double available_memory() const
Returns the available memory of the system.
Definition: app_info.h:133
int child_iterations
Number of iterations done by the child.
Definition: app_info.h:33
AppInfo()
Initializes the object.
Definition: app_info.h:78
double father_memory() const
Returns the memory used by the father process.
Definition: app_info.h:141
double child_memory() const
Returns the memory used by the child process.
Definition: app_info.h:149
int shmid
Identifier of the shared memory block.
Definition: app_info.h:47
Data * data_ptr
Pointer to the shared memory block.
Definition: app_info.h:49
void Reset()
Clears the values.
Definition: app_info.h:38
Contains the data block that is maintained in shared memory.
Definition: app_info.h:28
double father_memory_
Memory used by the father process.
Definition: app_info.h:54
double child_memory_
Memory used by the child process.
Definition: app_info.h:52
int num_connections
Number of open connections.
Definition: app_info.h:32
double available_memory_
Available memory in the system.
Definition: app_info.h:55
bool is_running_
true if the application is running
Definition: app_info.h:50
int child_pid
PID of the child process.
Definition: app_info.h:31
int lock_file
Lock file.
Definition: app_info.h:48
TYPE GetProcStat(int pid, int field) const
Returns a specific field of /proc/<pid>/stat as a defined type.
Definition: app_info.h:66
ostream & operator<<(ostream &out, const Request &request)
Definition: request.cc:65