librsync  2.0.2
stats.c
Go to the documentation of this file.
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
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 /** \file stats.c stats reporting functions.
21  *
22  * \todo Other things to show in statistics: number of input and output bytes,
23  * number of times we blocked waiting for input or output, number of blocks. */
24 
25 #include "config.h"
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #ifdef HAVE_SYS_FILE_H
33 # include <sys/file.h>
34 #endif
35 #include <string.h>
36 
37 #include "librsync.h"
38 #include "trace.h"
39 
40 int rs_log_stats(rs_stats_t const *stats)
41 {
42  char buf[1000];
43 
44  rs_format_stats(stats, buf, sizeof buf - 1);
45  rs_log(RS_LOG_INFO | RS_LOG_NONAME, "%s", buf);
46  return 0;
47 }
48 
49 char *rs_format_stats(rs_stats_t const *stats, char *buf, size_t size)
50 {
51  char const *op = stats->op;
52  int len, sec;
53  double mbps_in, mbps_out;
54 
55  if (!op)
56  op = "noop";
57 
58  len = snprintf(buf, size, "%s statistics: ", op);
59 
60  if (stats->lit_cmds) {
61  len +=
62  snprintf(buf + len, size - len,
63  "literal[%d cmds, " FMT_LONG " bytes, " FMT_LONG
64  " cmdbytes] ", stats->lit_cmds, stats->lit_bytes,
65  stats->lit_cmdbytes);
66  }
67 
68  if (stats->sig_cmds) {
69  len +=
70  snprintf(buf + len, size - len,
71  "in-place-signature[" FMT_LONG " cmds, " FMT_LONG
72  " bytes] ", stats->sig_cmds, stats->sig_bytes);
73  }
74 
75  if (stats->copy_cmds || stats->false_matches) {
76  len +=
77  snprintf(buf + len, size - len,
78  "copy[" FMT_LONG " cmds, " FMT_LONG " bytes, " FMT_LONG
79  " cmdbytes, %d false]", stats->copy_cmds,
80  stats->copy_bytes, stats->copy_cmdbytes,
81  stats->false_matches);
82  }
83 
84  if (stats->sig_blocks) {
85  len +=
86  snprintf(buf + len, size - len,
87  "signature[" FMT_LONG " blocks, " FMT_SIZE
88  " bytes per block]", stats->sig_blocks, stats->block_len);
89  }
90 
91  sec = (stats->end - stats->start);
92  if (sec == 0)
93  sec = 1; // avoid division by zero
94  mbps_in = stats->in_bytes / 1e6 / sec;
95  mbps_out = stats->out_bytes / 1e6 / sec;
96  len +=
97  snprintf(buf + len, size - len,
98  " speed[%.1f MB (%.1f MB/s) in, %.1f MB (%.1f MB/s) out, %d sec]",
99  (stats->in_bytes / 1e6), mbps_in, (stats->out_bytes / 1e6),
100  mbps_out, sec);
101 
102  return buf;
103 }
logging functions.
rs_long_t lit_cmdbytes
Number of bytes used in literal command headers.
Definition: librsync.h:196
rs_long_t sig_blocks
Number of blocks described by the signature.
Definition: librsync.h:203
rs_long_t out_bytes
Total bytes written to output.
Definition: librsync.h:209
Don't show function name in message.
Definition: trace.h:90
rs_long_t in_bytes
Total bytes read from input.
Definition: librsync.h:208
Public header for librsync.
int lit_cmds
Number of literal commands.
Definition: librsync.h:194
Informational.
Definition: librsync.h:107
char const * op
Human-readable name of current operation.
Definition: librsync.h:192
rs_long_t lit_bytes
Number of literal bytes.
Definition: librsync.h:195
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:191
char * rs_format_stats(rs_stats_t const *stats, char *buf, size_t size)
Return a human-readable representation of statistics.
Definition: stats.c:49
int rs_log_stats(rs_stats_t const *stats)
Write statistics into the current log as text.
Definition: stats.c:40