1 /*
2 * resource_track.c --- resource tracking
3 *
4 * Copyright (C) 2013 by Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12
13 #include "config.h"
14 #include "resize2fs.h"
15 #include <time.h>
16 #ifdef HAVE_MALLOC_H
17 #include <malloc.h>
18 #endif
19 #include <sys/resource.h>
20
init_resource_track(struct resource_track * track,const char * desc,io_channel channel)21 void init_resource_track(struct resource_track *track, const char *desc,
22 io_channel channel)
23 {
24 #ifdef HAVE_GETRUSAGE
25 struct rusage r;
26 #endif
27 io_stats io_start = 0;
28
29 track->desc = desc;
30 track->brk_start = sbrk(0);
31 gettimeofday(&track->time_start, 0);
32 #ifdef HAVE_GETRUSAGE
33 #ifdef sun
34 memset(&r, 0, sizeof(struct rusage));
35 #endif
36 getrusage(RUSAGE_SELF, &r);
37 track->user_start = r.ru_utime;
38 track->system_start = r.ru_stime;
39 #else
40 track->user_start.tv_sec = track->user_start.tv_usec = 0;
41 track->system_start.tv_sec = track->system_start.tv_usec = 0;
42 #endif
43 track->bytes_read = 0;
44 track->bytes_written = 0;
45 if (channel && channel->manager && channel->manager->get_stats)
46 channel->manager->get_stats(channel, &io_start);
47 if (io_start) {
48 track->bytes_read = io_start->bytes_read;
49 track->bytes_written = io_start->bytes_written;
50 }
51 }
52
timeval_subtract(struct timeval * tv1,struct timeval * tv2)53 static float timeval_subtract(struct timeval *tv1,
54 struct timeval *tv2)
55 {
56 return ((tv1->tv_sec - tv2->tv_sec) +
57 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
58 }
59
print_resource_track(ext2_resize_t rfs,struct resource_track * track,io_channel channel)60 void print_resource_track(ext2_resize_t rfs, struct resource_track *track,
61 io_channel channel)
62 {
63 #ifdef HAVE_GETRUSAGE
64 struct rusage r;
65 #endif
66 #ifdef HAVE_MALLINFO
67 struct mallinfo malloc_info;
68 #endif
69 struct timeval time_end;
70
71 if ((rfs->flags & RESIZE_DEBUG_RTRACK) == 0)
72 return;
73
74 gettimeofday(&time_end, 0);
75
76 if (track->desc)
77 printf("%s: ", track->desc);
78
79 #ifdef HAVE_MALLINFO
80 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
81
82 malloc_info = mallinfo();
83 printf("Memory used: %luk/%luk (%luk/%luk), ",
84 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
85 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
86 #else
87 printf("Memory used: %lu, ",
88 (unsigned long) (((char *) sbrk(0)) -
89 ((char *) track->brk_start)));
90 #endif
91 #ifdef HAVE_GETRUSAGE
92 getrusage(RUSAGE_SELF, &r);
93
94 printf("time: %5.2f/%5.2f/%5.2f\n",
95 timeval_subtract(&time_end, &track->time_start),
96 timeval_subtract(&r.ru_utime, &track->user_start),
97 timeval_subtract(&r.ru_stime, &track->system_start));
98 #else
99 printf("elapsed time: %6.3f\n",
100 timeval_subtract(&time_end, &track->time_start));
101 #endif
102 #define mbytes(x) (((x) + 1048575) / 1048576)
103 if (channel && channel->manager && channel->manager->get_stats) {
104 io_stats delta = 0;
105 unsigned long long bytes_read = 0;
106 unsigned long long bytes_written = 0;
107
108 channel->manager->get_stats(channel, &delta);
109 if (delta) {
110 bytes_read = delta->bytes_read - track->bytes_read;
111 bytes_written = delta->bytes_written -
112 track->bytes_written;
113 if (bytes_read == 0 && bytes_written == 0)
114 goto skip_io;
115 if (track->desc)
116 printf("%s: ", track->desc);
117 printf("I/O read: %lluMB, write: %lluMB, "
118 "rate: %.2fMB/s\n",
119 mbytes(bytes_read),
120 mbytes(bytes_written),
121 (double)mbytes(bytes_read + bytes_written) /
122 timeval_subtract(&time_end, &track->time_start));
123 }
124 }
125 skip_io:
126 fflush(stdout);
127 }
128
129