Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / e2fsck / iscan.c
1 /*
2  * Test to see how quickly we can scan the inode table (not doing
3  * anything else)
4  */
5
6 #include "config.h"
7 #include <string.h>
8 #include <fcntl.h>
9 #include <ctype.h>
10 #include <termios.h>
11 #include <time.h>
12 #ifdef HAVE_GETOPT_H
13 #include <getopt.h>
14 #endif
15 #include <unistd.h>
16 #ifdef HAVE_ERRNO_H
17 #include <errno.h>
18 #endif
19 #include <sys/ioctl.h>
20 #ifdef HAVE_MALLOC_H
21 #include <malloc.h>
22 #endif
23 #include <sys/time.h>
24 #include <sys/resource.h>
25
26 #if EXT2_FLAT_INCLUDES
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29 #include "blkid.h"
30 #else
31 #include "ext2fs/ext2_fs.h"
32 #include "ext2fs/ext2fs.h"
33 #include "blkid/blkid.h"
34 #endif
35
36 #include "et/com_err.h"
37 #include "../version.h"
38
39 struct resource_track {
40         struct timeval time_start;
41         struct timeval user_start;
42         struct timeval system_start;
43         void    *brk_start;
44         unsigned long long bytes_read;
45         unsigned long long bytes_written;
46 };
47
48 extern int isatty(int);
49
50 const char * program_name = "iscan";
51 const char * device_name = NULL;
52
53 int yflag = 0;
54 int nflag = 0;
55 int preen = 0;
56 int inode_buffer_blocks = 0;
57 int invalid_bitmaps = 0;
58
59 struct resource_track   global_rtrack;
60
61 void init_resource_track(struct resource_track *track, io_channel channel)
62 {
63 #ifdef HAVE_GETRUSAGE
64         struct rusage r;
65 #endif
66         io_stats io_start = 0;
67
68         track->brk_start = sbrk(0);
69         gettimeofday(&track->time_start, 0);
70 #ifdef HAVE_GETRUSAGE
71 #ifdef sun
72         memset(&r, 0, sizeof(struct rusage));
73 #endif
74         getrusage(RUSAGE_SELF, &r);
75         track->user_start = r.ru_utime;
76         track->system_start = r.ru_stime;
77 #else
78         track->user_start.tv_sec = track->user_start.tv_usec = 0;
79         track->system_start.tv_sec = track->system_start.tv_usec = 0;
80 #endif
81         track->bytes_read = 0;
82         track->bytes_written = 0;
83         if (channel && channel->manager && channel->manager->get_stats)
84                 channel->manager->get_stats(channel, &io_start);
85         if (io_start) {
86                 track->bytes_read = io_start->bytes_read;
87                 track->bytes_written = io_start->bytes_written;
88         }
89 }
90
91 static float timeval_subtract(struct timeval *tv1,
92                                        struct timeval *tv2)
93 {
94         return ((tv1->tv_sec - tv2->tv_sec) +
95                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
96 }
97
98 void print_resource_track(const char *desc,
99                           struct resource_track *track, io_channel channel)
100 {
101 #ifdef HAVE_GETRUSAGE
102         struct rusage r;
103 #endif
104         struct timeval time_end;
105
106         gettimeofday(&time_end, 0);
107
108         if (desc)
109                 printf("%s: ", desc);
110
111 #define kbytes(x)       (((unsigned long long)(x) + 1023) / 1024)
112 #ifdef HAVE_MALLINFO2
113         if (1) {
114                 struct mallinfo2 malloc_info = mallinfo2();
115
116                 printf("Memory used: %lluk/%lluk (%lluk/%lluk), ",
117                        kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
118                        kbytes(malloc_info.uordblks),
119                        kbytes(malloc_info.fordblks));
120         } else
121 #elif defined HAVE_MALLINFO
122         /* don't use mallinfo() if over 2GB used, since it returns "int" */
123         if ((unsigned long)((char *)sbrk(0) - (char *)track->brk_start) <
124             2UL << 30) {
125                 struct mallinfo malloc_info = mallinfo();
126
127                 printf("Memory used: %lluk/%lluk (%lluk/%lluk), ",
128                        kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
129                        kbytes(malloc_info.uordblks),
130                        kbytes(malloc_info.fordblks));
131         } else
132 #endif
133                 printf("Memory used: %lluk, ",
134                        kbytes(((char *)sbrk(0)) - ((char *)track->brk_start)));
135
136 #ifdef HAVE_GETRUSAGE
137         getrusage(RUSAGE_SELF, &r);
138
139         printf("time: %5.2f/%5.2f/%5.2f\n",
140                timeval_subtract(&time_end, &track->time_start),
141                timeval_subtract(&r.ru_utime, &track->user_start),
142                timeval_subtract(&r.ru_stime, &track->system_start));
143 #else
144         printf("elapsed time: %6.3f\n",
145                timeval_subtract(&time_end, &track->time_start));
146 #endif
147 #define mbytes(x)       (((x) + 1048575) / 1048576)
148         if (channel && channel->manager && channel->manager->get_stats) {
149                 io_stats delta = 0;
150                 unsigned long long bytes_read = 0;
151                 unsigned long long bytes_written = 0;
152
153                 if (desc)
154                         printf("%s: ", desc);
155
156                 channel->manager->get_stats(channel, &delta);
157                 if (delta) {
158                         bytes_read = delta->bytes_read - track->bytes_read;
159                         bytes_written = delta->bytes_written -
160                                 track->bytes_written;
161                 }
162                 printf("I/O read: %lluMB, write: %lluMB, "
163                        "rate: %.2fMB/s\n",
164                        mbytes(bytes_read), mbytes(bytes_written),
165                        (double)mbytes(bytes_read + bytes_written) /
166                        timeval_subtract(&time_end, &track->time_start));
167         }
168 }
169
170 static void usage(void)
171 {
172         fprintf(stderr,
173                 "Usage: %s [-F] [-I inode_buffer_blocks] device\n",
174                 program_name);
175         exit(1);
176 }
177
178 static void PRS(int argc, char *argv[])
179 {
180         int             flush = 0;
181         int             c;
182 #ifdef MTRACE
183         extern void     *mallwatch;
184 #endif
185         errcode_t       retval;
186
187         setbuf(stdout, NULL);
188         setbuf(stderr, NULL);
189         initialize_ext2_error_table();
190
191         if (argc && *argv)
192                 program_name = *argv;
193         while ((c = getopt (argc, argv, "FI")) != EOF)
194                 switch (c) {
195                 case 'F':
196                         flush = 1;
197                         break;
198                 case 'I':
199                         inode_buffer_blocks = atoi(optarg);
200                         break;
201                 default:
202                         usage ();
203                 }
204         device_name = argv[optind];
205         if (flush) {
206                 int     fd = open(device_name, O_RDONLY, 0);
207
208                 if (fd < 0) {
209                         com_err("open", errno,
210                             "while opening %s for flushing", device_name);
211                         exit(1);
212                 }
213                 if ((retval = ext2fs_sync_device(fd, 1))) {
214                         com_err("ext2fs_sync_device", retval,
215                                 "while trying to flush %s", device_name);
216                         exit(1);
217                 }
218                 close(fd);
219         }
220 }
221
222 int main (int argc, char *argv[])
223 {
224         errcode_t       retval = 0;
225         int             exit_value = 0;
226         ext2_filsys     fs;
227         ext2_ino_t      ino;
228         __u32   num_inodes = 0;
229         struct ext2_inode inode;
230         ext2_inode_scan scan;
231
232         PRS(argc, argv);
233
234         retval = ext2fs_open(device_name, 0,
235                              0, 0, unix_io_manager, &fs);
236         if (retval) {
237                 com_err(program_name, retval, "while trying to open '%s'",
238                         device_name);
239                 exit(1);
240         }
241
242         init_resource_track(&global_rtrack, fs->io);
243
244         retval = ext2fs_open_inode_scan(fs, inode_buffer_blocks, &scan);
245         if (retval) {
246                 com_err(program_name, retval, "while opening inode scan");
247                 exit(1);
248         }
249
250         while (1) {
251                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
252                 if (retval) {
253                         com_err(program_name, retval,
254                                 "while getting next inode");
255                         exit(1);
256                 }
257                 if (ino == 0)
258                         break;
259                 num_inodes++;
260         }
261
262         print_resource_track(NULL, &global_rtrack, fs->io);
263         printf("%u inodes scanned.\n", num_inodes);
264
265         exit(0);
266 }