summaryrefslogtreecommitdiff
path: root/pssbench/main.cpp
blob: 160bf6cb5f3dfadd9263e23ba96572bb432550a5 (plain)
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
#include <stdio.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>

const char* smaps_file = "smaps";
bool verbose = false;
int iterations = 1;
int bufsz = -1;

int64_t
get_pss(int pid)
{
  char filename[64];
  snprintf(filename, sizeof(filename), "/proc/%" PRId32 "/%s", pid,
           smaps_file);
  if (verbose)
    fprintf(stderr, "smaps:[%s]\n", filename);

  FILE * file = fopen(filename, "r");
  if (!file) {
    return (int64_t) -1;
  }

  if (bufsz >= 0) {
    if (setvbuf(file, NULL, _IOFBF, bufsz)) {
      fprintf(stderr, "setvbuf failed: %s\n", strerror(errno));
      exit(1);
    }
  }

  // Tally up all of the Pss from the various maps
  char line[256];
  int64_t pss = 0;
  while (fgets(line, sizeof(line), file)) {
    int64_t v;
    if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) {
      if (verbose)
        fprintf(stderr, "pss line: %llu\n", (unsigned long long) v);
      pss += v;
    }
  }

  fclose(file);

  // Return the Pss value in bytes, not kilobytes
  return pss * 1024;
}

int
main(int argc, char** argv)
{
  int c;
  while ((c = getopt(argc, argv, "n:rvb:")) != -1) {
    switch (c) {
      case 'r':
        smaps_file = "smaps_rollup";
        break;
      case 'v':
        verbose = true;
        break;
      case 'n':
        iterations = atoi(optarg);
        break;
      case 'b':
        bufsz = atoi(optarg);
        break;
      default:
        return 1;
    }
  }

  if (argv[optind] == NULL) {
    fprintf(stderr, "pssbench: no PID given\n");
    return 1;
  }
  int pid = atoi(argv[optind]);
  int64_t pss = 0;
  for (int i = 0; i < iterations; ++i)
    pss = get_pss(pid);
  fflush(NULL);

  printf("iterations:%d pid:%d pss:%lld\n", iterations, pid, (long long)pss);
  return 0;
}