File: polling.h

package info (click to toggle)
gps 0.9.4-1.woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 964 kB
  • ctags: 928
  • sloc: cpp: 10,822; sh: 381; makefile: 307; ansic: 227; perl: 17
file content (196 lines) | stat: -rw-r--r-- 5,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* $Id: polling.h,v 1.5 2000/09/29 10:15:37 bergo Exp $ */

#ifndef GPS_POLLING_H
#define GPS_POLLING_H

#include <gtk/gtk.h>

/// maximum process name length
#define GPS_MAXPROCESSNAME      256
/// maximum command line length
#define GPS_MAXPROCESSLONGNAME  512
/// maximum username length
#define GPS_MAXOWNER            64
/// maximum length of a memory representation
#define GPS_MAXMEMSTRING        32
/// maximum length of a start time string
#define GPS_MAXTIMESTRING       64
/// maximum length of a hostname
#define GPS_MAXHOSTNAME         128

/* FreeBSD is a box full of surprises... */
#undef PID
#undef NAME
#undef LONGNAME
#undef OWNER
#undef MACHINE
#undef STATE
#undef CPU
#undef SIZE
#undef RSS

typedef enum { PID=0,NAME,LONGNAME,OWNER,MACHINE,
	       STATE,CPU,SIZE,RSS,NICE,
	       PRIORITY,START,
	       PPID,PGID,SID,TTY,TPGID,FLAGS,
	       SIGPENDING,SIGBLOCKED,SIGIGNORED,SIGCAUGHT,
	       RSSLIM,MINFLT,CMINFLT,MAJFLT,CMAJFLT,
	       USRJIFFIES,KRNJIFFIES,CUSRJIFFIES,CKRNJIFFIES } ProcessField;


/// represents a row in the main process list
class ProcessListItem {
 public:
  /// creates an empty instance
  ProcessListItem();

  /// use this method to set values, it checks for oversized entries.
  void setField(ProcessField f,char *value);
  
  char pid[16];
  char name[GPS_MAXPROCESSNAME];
  char longname[GPS_MAXPROCESSLONGNAME];
  char owner[GPS_MAXOWNER];
  char machine[GPS_MAXHOSTNAME];
  char state[16];
  char cpu[32];
  char size[GPS_MAXMEMSTRING];
  char rss[GPS_MAXMEMSTRING];
  char nice[16];
  char priority[16];
  char start[GPS_MAXTIMESTRING];
};

/// represents the details of a process, for use in the Details Dialog.
class ProcessItem : public ProcessListItem{
 public:
  /// creates an empty instance
  ProcessItem();
  /// use this method to set values, it checks for oversized entries.
  void setField(ProcessField f,char *value);

  char ppid[16],pgid[16],sid[16],tty[64],
    tpgid[16],flags[256];
  char sigpend[256],sigblock[256],sigign[256],sigcau[256];
  char rssl[256],minflt[32],cminflt[32],majflt[32],cmajflt[32];
  char uj[32],kj[32],cuj[32],ckj[32]; /* jiffies, user/kernel */
};

/// capabilities unique to Unix critters
class UnixHostnames {
 public:
  /// returns the local hostname
  virtual char *get_local_hostname(); /* should work under any Un*x beast */
 private:
  char hostbuffer[GPS_MAXHOSTNAME];
};

/**
 abstract representation of a poller capable of retrieving the
 list of processes in the local machine.
*/
class ProcessListPoller : public UnixHostnames {  
 public:
  /// trivial constructor
  ProcessListPoller();
  /// trivial destructor
  ~ProcessListPoller();

  /** 
      fills the process_list with the current process list of this
      machine (as a list of ProcessListItem * objects).
      Should not take more than half a second to fill the table!
      See linuxprocfs.cc / .h for a sample implementation.
      In case of trouble, returning with process_list NULL is
      acceptable.
  */
  virtual void poll()=0;
  /// clean up, called on destruction. Preferred over ~ProcessListPoller.
  virtual void terminate();

  /// the process list, as a GList (from glib) of (ProcessListItem *) nodes.
  GList *process_list;

 protected:
  /**
     deletes the current process_list, if not NULL. Usually poll()
     calls this.
  */
  void reset();
};

/**
 abstract representation of a poller capable of retrieving the
 details of a local process.
*/
class ProcessDetailsPoller : public UnixHostnames {
 public:
  /// trivial constructor
  ProcessDetailsPoller();
  /// tricial destructor
  ~ProcessDetailsPoller();

  /// abstract, must fill item with information about process whichpid.
  virtual void poll(int whichpid)=0;
  /// clean-up, preferred over ~ProcessDetailsPoller.
  virtual void terminate();

  /// the details item, filled by poll().
  ProcessItem *item;

 protected:
  /// frees item if it's not NULL.
  void reset();
};

/**
 abstract representation of a poller capable of retrieving the
 cpu usage (as a float in the range 0.0 to 1.0), memory usage
 (bytes free, bytes used) and swap usage (bytes free, bytes used).
*/
class SystemInfoProvider {
 public:
  /**
     abstract, fills the memory_*, swap_* and cpu_usage fields.
     Will be called in approximately 250 msec intervals, so it
     can't take much time to complete.
     If it can't stat SMP boxes, should call SMP_faker() before
     returning.
  */
  virtual void update()=0;

  /// returns a double in the range 0 to 1 representing memory usage.
  double mem_fraction();
  /// returns a double in the range 0 to 1 representing swap usage.
  double swap_fraction();
  
  /// clean up, called upon destruction.
  virtual void terminate();

  /// setups SMP info for one CPU only
  void SMP_faker();

  /// in bytes
  unsigned long memory_total;
  /// in bytes
  unsigned long memory_used;
  /// in bytes
  unsigned long memory_free;

  /// in bytes
  unsigned long swap_total;
  /// in bytes
  unsigned long swap_used;
  /// in bytes
  unsigned long swap_free;

  /// avg cpu load, 0 to 1. On one-cpu boxes this is the cpu load,
  /// on SMP systems this is the unweighted average among all CPUs.
  double cpu_usage;

  /// SMP extensions (max 64 CPUs)
  int CpuCount;
  double cpus_load[64];
};

#endif