]>
Commit | Line | Data |
---|---|---|
1eb31c48 TT |
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 | ||
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 | ||
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 | ||
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 | |
97079a79 LC |
66 | #ifdef HAVE_MALLINFO2 |
67 | struct mallinfo2 malloc_info; | |
68 | #elif defined HAVE_MALLINFO | |
69 | struct mallinfo malloc_info; | |
1eb31c48 TT |
70 | #endif |
71 | struct timeval time_end; | |
72 | ||
73 | if ((rfs->flags & RESIZE_DEBUG_RTRACK) == 0) | |
74 | return; | |
75 | ||
76 | gettimeofday(&time_end, 0); | |
77 | ||
78 | if (track->desc) | |
79 | printf("%s: ", track->desc); | |
80 | ||
1eb31c48 | 81 | #define kbytes(x) (((unsigned long)(x) + 1023) / 1024) |
97079a79 LC |
82 | #ifdef HAVE_MALLINFO2 |
83 | malloc_info = mallinfo2(); | |
84 | printf("Memory used: %luk/%luk (%luk/%luk), ", | |
85 | kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd), | |
86 | kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks)); | |
87 | #elif defined HAVE_MALLINFO | |
1eb31c48 TT |
88 | |
89 | malloc_info = mallinfo(); | |
90 | printf("Memory used: %luk/%luk (%luk/%luk), ", | |
91 | kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd), | |
92 | kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks)); | |
93 | #else | |
94 | printf("Memory used: %lu, ", | |
95 | (unsigned long) (((char *) sbrk(0)) - | |
96 | ((char *) track->brk_start))); | |
97 | #endif | |
98 | #ifdef HAVE_GETRUSAGE | |
99 | getrusage(RUSAGE_SELF, &r); | |
100 | ||
101 | printf("time: %5.2f/%5.2f/%5.2f\n", | |
102 | timeval_subtract(&time_end, &track->time_start), | |
103 | timeval_subtract(&r.ru_utime, &track->user_start), | |
104 | timeval_subtract(&r.ru_stime, &track->system_start)); | |
105 | #else | |
106 | printf("elapsed time: %6.3f\n", | |
107 | timeval_subtract(&time_end, &track->time_start)); | |
108 | #endif | |
109 | #define mbytes(x) (((x) + 1048575) / 1048576) | |
110 | if (channel && channel->manager && channel->manager->get_stats) { | |
111 | io_stats delta = 0; | |
112 | unsigned long long bytes_read = 0; | |
113 | unsigned long long bytes_written = 0; | |
114 | ||
115 | channel->manager->get_stats(channel, &delta); | |
116 | if (delta) { | |
117 | bytes_read = delta->bytes_read - track->bytes_read; | |
118 | bytes_written = delta->bytes_written - | |
119 | track->bytes_written; | |
120 | if (bytes_read == 0 && bytes_written == 0) | |
121 | goto skip_io; | |
122 | if (track->desc) | |
123 | printf("%s: ", track->desc); | |
124 | printf("I/O read: %lluMB, write: %lluMB, " | |
125 | "rate: %.2fMB/s\n", | |
126 | mbytes(bytes_read), | |
127 | mbytes(bytes_written), | |
128 | (double)mbytes(bytes_read + bytes_written) / | |
129 | timeval_subtract(&time_end, &track->time_start)); | |
130 | } | |
131 | } | |
132 | skip_io: | |
133 | fflush(stdout); | |
134 | } | |
135 |