aboutsummaryrefslogtreecommitdiffstats
path: root/src/stats.c
blob: 1fb7c03d31c3d772acb4ca2d868e1a27550b4af2 (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
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
#include "project.h"

uint64_t stats_inodes;
uint64_t stats_ea_blocks;
uint64_t stats_bytes;
static uint64_t last_bytes;


struct timeval start_time, last_time;

static int first = 1;



static int do_time (char *ptr, double t)
{
  double h, m, s;

  if (t < 60.0)
    return sprintf (ptr, "%.1fs", t);

  if (t < 3600.0) {
    m = floor (t / 60.0);
    t -= 60 * m;
    s = floor (t);
    t -= s;
    t *= 10;
    return sprintf (ptr, "%d:%02d.%dm", (int) m, (int) s, (int) t);
  }

  h = floor (t / 3600.0);
  t -= 3600 * h;
  m = floor (t / 60.0);
  t -= 60 * m;
  s = floor (t);
  t -= s;
  t *= 10;
  return sprintf (ptr, "%d:%02d:%02d.%dh", (int) h, (int) m, (int) s,
                  (int) t);
}

static int do_rate (char *ptr, double r)
{
  if (r < 1024.0)
    return sprintf (ptr, "%.1f b", r);

  if (r < 1048576.0)
    return sprintf (ptr, "%.1f K", r / 1024.0);

  if (r < 1073741824.0)
    return sprintf (ptr, "%.1f M", r / 1048576.0);

  return sprintf (ptr, "%.1f G", r / 1073741824.0);
}


void stats (int force)
{
  struct timeval now, diff1, diff2;
  char charbuf[128], *ptr = charbuf;
  double totaltime, totalbytes, intertime, interbytes;

  gettimeofday (&now, NULL);


  if (first) {
    first = 0;
    last_time = now;
    start_time = now;
    return;
  }


  timersub (&now, &last_time, &diff1);

  if ((!force) && (diff1.tv_sec < 10))
    return;

  timersub (&now, &start_time, &diff2);

  intertime = (double) diff1.tv_usec;
  intertime /= 1000000.0;
  intertime += (double) diff1.tv_sec;

  totaltime = (double) diff2.tv_usec;
  totaltime /= 1000000.0;
  totaltime += (double) diff2.tv_sec;

  totalbytes = (double) stats_bytes;
  interbytes = stats_bytes - (double) last_bytes;

  last_bytes = stats_bytes;
  last_time = now;

  intertime += 0.001;
  totaltime += 0.001;

  ptr += do_time (ptr, totaltime);
  ptr += sprintf (ptr, " rate:");
  ptr += do_rate (ptr, interbytes / intertime);
  ptr += sprintf (ptr, "/");
  ptr += do_rate (ptr, totalbytes / totaltime);
  ptr += sprintf (ptr, " bytes:");
  ptr += do_rate (ptr, totalbytes);
  ptr += sprintf (ptr, " inodes %lld ea_blocks %lld\n",
                  (long long int) stats_inodes, (long long int) stats_ea_blocks);
  fputs (charbuf, stderr);

}