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