summaryrefslogtreecommitdiffstats
path: root/resize/resource_track.c
blob: f46670606aea8d706c6ffe7c2f02453ba9502a43 (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
/*
 * resource_track.c --- resource tracking
 *
 * Copyright (C) 2013 by Theodore Ts'o
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */


#include "config.h"
#include "resize2fs.h"
#include <time.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <sys/resource.h>

void init_resource_track(struct resource_track *track, const char *desc,
			 io_channel channel)
{
#ifdef HAVE_GETRUSAGE
	struct rusage r;
#endif
	io_stats io_start = 0;

	track->desc = desc;
	track->brk_start = sbrk(0);
	gettimeofday(&track->time_start, 0);
#ifdef HAVE_GETRUSAGE
#ifdef sun
	memset(&r, 0, sizeof(struct rusage));
#endif
	getrusage(RUSAGE_SELF, &r);
	track->user_start = r.ru_utime;
	track->system_start = r.ru_stime;
#else
	track->user_start.tv_sec = track->user_start.tv_usec = 0;
	track->system_start.tv_sec = track->system_start.tv_usec = 0;
#endif
	track->bytes_read = 0;
	track->bytes_written = 0;
	if (channel && channel->manager && channel->manager->get_stats)
		channel->manager->get_stats(channel, &io_start);
	if (io_start) {
		track->bytes_read = io_start->bytes_read;
		track->bytes_written = io_start->bytes_written;
	}
}

static float timeval_subtract(struct timeval *tv1,
			      struct timeval *tv2)
{
	return ((tv1->tv_sec - tv2->tv_sec) +
		((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
}

void print_resource_track(ext2_resize_t rfs, struct resource_track *track,
			  io_channel channel)
{
#ifdef HAVE_GETRUSAGE
	struct rusage r;
#endif
#ifdef HAVE_MALLINFO2
	struct mallinfo2 malloc_info;
#elif defined HAVE_MALLINFO
	struct mallinfo malloc_info;
#endif
	struct timeval time_end;

	if ((rfs->flags & RESIZE_DEBUG_RTRACK) == 0)
		return;

	gettimeofday(&time_end, 0);

	if (track->desc)
		printf("%s: ", track->desc);

#define kbytes(x)	(((unsigned long)(x) + 1023) / 1024)
#ifdef HAVE_MALLINFO2
	malloc_info = mallinfo2();
	printf("Memory used: %luk/%luk (%luk/%luk), ",
		kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
		kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
#elif defined HAVE_MALLINFO

	malloc_info = mallinfo();
	printf("Memory used: %luk/%luk (%luk/%luk), ",
		kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
		kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
#else
	printf("Memory used: %lu, ",
		(unsigned long) (((char *) sbrk(0)) -
				 ((char *) track->brk_start)));
#endif
#ifdef HAVE_GETRUSAGE
	getrusage(RUSAGE_SELF, &r);

	printf("time: %5.2f/%5.2f/%5.2f\n",
		timeval_subtract(&time_end, &track->time_start),
		timeval_subtract(&r.ru_utime, &track->user_start),
		timeval_subtract(&r.ru_stime, &track->system_start));
#else
	printf("elapsed time: %6.3f\n",
		timeval_subtract(&time_end, &track->time_start));
#endif
#define mbytes(x)	(((x) + 1048575) / 1048576)
	if (channel && channel->manager && channel->manager->get_stats) {
		io_stats delta = 0;
		unsigned long long bytes_read = 0;
		unsigned long long bytes_written = 0;

		channel->manager->get_stats(channel, &delta);
		if (delta) {
			bytes_read = delta->bytes_read - track->bytes_read;
			bytes_written = delta->bytes_written -
				track->bytes_written;
			if (bytes_read == 0 && bytes_written == 0)
				goto skip_io;
			if (track->desc)
				printf("%s: ", track->desc);
			printf("I/O read: %lluMB, write: %lluMB, "
			       "rate: %.2fMB/s\n",
			       mbytes(bytes_read),
			       mbytes(bytes_written),
			       (double)mbytes(bytes_read + bytes_written) /
			       timeval_subtract(&time_end, &track->time_start));
		}
	}
skip_io:
	fflush(stdout);
}