File: statistics.c

package info (click to toggle)
udpcast 20040531-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 372 kB
  • ctags: 733
  • sloc: ansic: 5,816; perl: 159; makefile: 114
file content (119 lines) | stat: -rw-r--r-- 2,679 bytes parent folder | download
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
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#include <sys/time.h>
#include <unistd.h>
#include "log.h"
#include "statistics.h"
#include "util.h"


struct receiver_stats {
    struct timeval tv_start;
    int bytesOrig;
    long long totalBytes;
    int timerStarted;
    int fd;
};

receiver_stats_t allocReadStats(int fd) {
    receiver_stats_t rs =  MALLOC(struct receiver_stats);
    rs->fd = fd;
    return rs;
}

void receiverStatsAddBytes(receiver_stats_t rs, long bytes) {
    if(rs != NULL)
	rs->totalBytes += bytes;
}

void receiverStatsStartTimer(receiver_stats_t rs) {
    if(rs != NULL && !rs->timerStarted) {
	gettimeofday(&rs->tv_start, 0);
	rs->timerStarted = 1;
    }
}

static void printFilePosition(int fd) {
#ifndef __CYGWIN__
    if(fd != -1) {
	loff_t offset = lseek64(fd, 0, SEEK_CUR);
	printLongNum(offset);
    }
#endif
}

void displayReceiverStats(receiver_stats_t rs) {
    long long timePassed;
    struct timeval tv_now;

    if(rs == NULL)
	return;

    gettimeofday(&tv_now, 0);

    fprintf(stderr, "bytes=");
    printLongNum(rs->totalBytes);
    fprintf(stderr, " (");
    
    timePassed = tv_now.tv_sec - rs->tv_start.tv_sec;
    timePassed *= 1000000;
    timePassed += tv_now.tv_usec - rs->tv_start.tv_usec;
    if (timePassed != 0) {
	int mbps = (int) (rs->totalBytes * 800 / timePassed);
	fprintf(stderr, "%3d.%02d", mbps / 100, mbps % 100);
    } else {
	fprintf(stderr, "***.**");
    }
    fprintf(stderr, " Mbps)");
    printFilePosition(rs->fd);
    fprintf(stderr, "\r");
    fflush(stderr);
}


struct sender_stats {
    unsigned long long totalBytes;
    int retransmissions;
    int fd;
    int clNo;
};

sender_stats_t allocSenderStats(int fd) {
    sender_stats_t ss = MALLOC(struct sender_stats);
    ss->fd = fd;
    return ss;
}

void senderStatsAddBytes(sender_stats_t ss, long bytes) {
    if(ss != NULL)
	ss->totalBytes += bytes;
}

void senderStatsAddRetransmissions(sender_stats_t ss, int retransmissions) {
    if(ss != NULL)
	ss->retransmissions += retransmissions;
}


void displaySenderStats(sender_stats_t ss, int blockSize, int sliceSize) {
    int blocks, percent;
    
    if(ss == NULL)
	return;
    blocks = (ss->totalBytes + blockSize - 1) / blockSize;
    percent = 1000 * ss->retransmissions / blocks;
    
    fprintf(stderr, "bytes=");
    printLongNum(ss->totalBytes);
    fprintf(stderr, "re-xmits=%06d (%3d.%01d%%) slice=%04d ",
	    ss->retransmissions, percent / 10, percent % 10, sliceSize);
    printFilePosition(ss->fd);
    fprintf(stderr, "- %3d\r", ss->clNo);
    fflush(stderr);
}

void senderSetAnswered(sender_stats_t ss, int clNo) {
    if(ss != NULL)
	ss->clNo = clNo;
}