File: diskloadmeter.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (84 lines) | stat: -rw-r--r-- 1,635 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <iostream>
#include "diskloadmeter.h"
using namespace std;


DiskloadMeter::DiskloadMeter()
{
}


#ifdef __linux__
bool DiskloadMeter::getDiskload(Diskload & diskload)
{
  FILE *fcinfo;
  if ( (fcinfo = fopen("/proc/vmstat", "r") ) != NULL)
  {
    int entriesFound=0;
    long readkbytes=0;
    long writekbytes=0;
    while (true)
    {
      char entry[200];
      long value;

      // FIXME: limit the value of scanned chars to prevent buffer overruns
      int fsret = fscanf(fcinfo, "%[^\t ]%*[\t ]%ld\n", entry, &value);
      if (fsret == EOF)
        break;

      if (fsret == 2)
      {
        // interpret the fscanned data
        if (0 == strcmp(entry, "pgpgin"))
        {
          readkbytes=value;
          entriesFound++;
        }

        if (0 == strcmp(entry, "pgpgout"))
        {
          writekbytes=value;
          entriesFound++;
        }
      }
    }
    fclose(fcinfo);

    if (2==entriesFound)
    {
      diskload.readkbytespersec =readKBytesDeriver.setCurrentValueAndGetDerivation(readkbytes);
      diskload.writekbytespersec=writeKBytesDeriver.setCurrentValueAndGetDerivation(writekbytes);

      return true;
    }
  }
  return false;
}
#endif



#ifdef __sun__
bool DiskloadMeter::getDiskload(Diskload & diskload)
{
  diskload.readkbytespersec = -1.;
  diskload.writekbytespersec = -1.;
  return false;
}
#endif


#ifdef __FreeBSD__
bool DiskloadMeter::getDiskload(Diskload & diskload)
{
  diskload.readkbytespersec = -1.;
  diskload.writekbytespersec = -1.;
  return false;
}
#endif