librsync  2.0.1
stats.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * Copyright (C) 2000, 2001 by Martin Pool <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 
21 #include "config.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_SYS_FILE_H
29 #include <sys/file.h>
30 #endif
31 #include <string.h>
32 
33 #include "librsync.h"
34 #include "trace.h"
35 
36 /*
37  * TODO: Other things to show in statistics:
38  *
39  * Number of input and output bytes.
40  *
41  * Number of times we blocked waiting for input or output.
42  *
43  * Number of blocks.
44  */
45 
46 int
47 rs_log_stats(rs_stats_t const *stats)
48 {
49  char buf[1000];
50 
51  rs_format_stats(stats, buf, sizeof buf - 1);
52  rs_log(RS_LOG_INFO|RS_LOG_NONAME, "%s", buf);
53  return 0;
54 }
55 
56 
57 
58 char *
60  char *buf, size_t size)
61 {
62  char const *op = stats->op;
63  int len, sec;
64  double mbps_in, mbps_out;
65 
66  if (!op)
67  op = "noop";
68 
69  len = snprintf(buf, size, "%s statistics: ", op);
70 
71  if (stats->lit_cmds) {
72  len += snprintf(buf+len, size-len,
73  "literal[%d cmds, " PRINTF_FORMAT_U64 " bytes, " PRINTF_FORMAT_U64 " cmdbytes] ",
74  stats->lit_cmds,
75  PRINTF_CAST_U64(stats->lit_bytes),
76  PRINTF_CAST_U64(stats->lit_cmdbytes));
77  }
78 
79  if (stats->sig_cmds) {
80  len += snprintf(buf+len, size-len,
81  "in-place-signature[" PRINTF_FORMAT_U64 " cmds, " PRINTF_FORMAT_U64 " bytes] ",
82  PRINTF_CAST_U64(stats->sig_cmds),
83  PRINTF_CAST_U64(stats->sig_bytes));
84  }
85 
86  if (stats->copy_cmds || stats->false_matches) {
87  len += snprintf(buf+len, size-len,
88  "copy[" PRINTF_FORMAT_U64 " cmds, " PRINTF_FORMAT_U64 " bytes, " PRINTF_FORMAT_U64 " false, " PRINTF_FORMAT_U64 " cmdbytes]",
89  PRINTF_CAST_U64(stats->copy_cmds),
90  PRINTF_CAST_U64(stats->copy_bytes),
91  PRINTF_CAST_U64(stats->false_matches),
92  PRINTF_CAST_U64(stats->copy_cmdbytes));
93  }
94 
95 
96  if (stats->sig_blocks) {
97  len += snprintf(buf+len, size-len,
98  "signature[" PRINTF_FORMAT_U64 " blocks, " PRINTF_FORMAT_U64 " bytes per block]",
99  PRINTF_CAST_U64(stats->sig_blocks),
100  PRINTF_CAST_U64(stats->block_len));
101  }
102 
103  sec = (stats->end - stats->start);
104  if (sec == 0) sec = 1; // avoid division by zero
105  mbps_in = stats->in_bytes / 1e6 / sec;
106  mbps_out = stats->out_bytes / 1e6 / sec;
107  len += snprintf(buf+len, size-len,
108  " speed[%.1f MB (%.1f MB/s) in, %.1f MB (%.1f MB/s) out, %d sec]",
109  (stats->in_bytes/1e6), mbps_in,
110  (stats->out_bytes/1e6), mbps_out, sec);
111 
112  return buf;
113 }
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:240
rs_long_t sig_blocks
Number of blocks described by the signature.
Definition: librsync.h:247
rs_long_t out_bytes
Total bytes written to output.
Definition: librsync.h:253
rs_long_t in_bytes
Total bytes read from input.
Definition: librsync.h:252
Public header for librsync.
int lit_cmds
Number of literal commands.
Definition: librsync.h:238
Informational.
Definition: librsync.h:125
char const * op
Human-readable name of current operation.
Definition: librsync.h:236
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:239
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:235
char * rs_format_stats(rs_stats_t const *stats, char *buf, size_t size)
Return a human-readable representation of statistics.
Definition: stats.c:59
int rs_log_stats(rs_stats_t const *stats)
Write statistics into the current log as text.
Definition: stats.c:47