xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

swapmeter.cc
Go to the documentation of this file.
1 //
2 // Initial port performed by Greg Onufer (exodus@cheers.bungi.com)
3 //
4 #include "swapmeter.h"
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include <sys/stat.h>
9 #include <sys/swap.h>
10 #include <iostream>
11 
12 
13 SwapMeter::SwapMeter(XOSView *parent, kstat_ctl_t *_kc)
14  : FieldMeterGraph(parent, 2, "SWAP", "USED/FREE")
15 {
16  pagesize = sysconf(_SC_PAGESIZE);
17 }
18 
20 {
21 }
22 
24 {
26 
27  setfieldcolor(0, parent_->getResource("swapUsedColor"));
28  setfieldcolor(1, parent_->getResource("swapFreeColor"));
29  priority_ = atoi(parent_->getResource("swapPriority"));
30  dodecay_ = parent_->isResourceTrue("swapDecay");
31  useGraph_ = parent_->isResourceTrue("swapGraph");
32  SetUsedFormat(parent_->getResource("swapUsedFormat"));
33 }
34 
35 void SwapMeter::checkevent(void)
36 {
37  getswapinfo();
38  drawfields();
39 }
40 
41 void SwapMeter::getswapinfo(void)
42 {
43  swaptbl_t *swaps;
44  char *names;
45 
46  total_ = fields_[0] = fields_[1] = 0;
47  int numswap = swapctl(SC_GETNSWP, NULL);
48  if (numswap < 0) {
49  std::cerr << "Can not determine number of swap spaces." << std::endl;
50  parent_->done(1);
51  return;
52  }
53  if (numswap > 0) {
54  swaps = (swaptbl_t *)malloc(sizeof(swaptbl_t) + numswap * sizeof(swapent_t));
55  names = (char *)calloc(numswap + 1, PATH_MAX);
56  if (!swaps || !names) {
57  std::cerr << "malloc failed." << std::endl;
58  parent_->done(1);
59  return;
60  }
61  swaps->swt_n = numswap;
62  for (int i = 0; i <= numswap; i++)
63  swaps->swt_ent[i].ste_path = names + (i * PATH_MAX);
64 
65  if (swapctl(SC_LIST, swaps) < 0) {
66  std::cerr << "Can not get list of swap spaces." << std::endl;
67  parent_->done(1);
68  return;
69  }
70  for (int i = 0; i < numswap; i++) {
71  total_ += swaps->swt_ent[i].ste_pages;
72  fields_[1] += swaps->swt_ent[i].ste_free;
73  XOSDEBUG("%s: %ld kB (%ld kB free)\n",
74  swaps->swt_ent[i].ste_path,
75  swaps->swt_ent[i].ste_pages * (pagesize / 1024),
76  swaps->swt_ent[i].ste_free * (pagesize / 1024));
77  }
78  fields_[0] = total_ - fields_[1];
79  free(swaps);
80  free(names);
81  }
82 
84 }
virtual void drawfields(int mandatory=0)
virtual void checkResources(void)
double total_
Definition: fieldmeter.h:41
void setfieldcolor(int field, const char *color)
Definition: fieldmeter.cc:108
void setUsed(double val, double total)
Definition: fieldmeter.cc:76
double * fields_
Definition: fieldmeter.h:40
void SetUsedFormat(const char *const str)
Definition: fieldmeter.cc:60
int priority_
Definition: meter.h:54
XOSView * parent_
Definition: meter.h:52
void checkResources(void)
Definition: swapmeter.cc:29
size_t pagesize
Definition: swapmeter.h:25
void checkevent(void)
Definition: swapmeter.cc:40
void getswapinfo(void)
Definition: swapmeter.cc:45
SwapMeter(XOSView *parent)
Definition: swapmeter.cc:21
~SwapMeter(void)
Definition: swapmeter.cc:26
int done(void)
Definition: xwin.h:29
int isResourceTrue(const char *name)
Definition: xwin.h:89
const char * getResource(const char *name)
Definition: xwin.cc:362
#define XOSDEBUG(...)
Definition: xosview.h:84