Whamcloud - gitweb
852ff86b1cde9a167cfd545a17fa09bdde4a19db
[tools/e2fsprogs.git] / e2fsck / scantest.c
1 /*
2  * scantest.c - test the speed of the inode scan routine
3  */
4
5 #include "config.h"
6 #include <string.h>
7 #include <fcntl.h>
8 #include <ctype.h>
9 #include <termios.h>
10 #include <time.h>
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
14 #include <unistd.h>
15 #ifdef HAVE_MNTENT_H
16 #include <mntent.h>
17 #endif
18 #include <sys/ioctl.h>
19 #ifdef HAVE_MALLOC_H
20 #include <malloc.h>
21 #endif
22 #include <sys/resource.h>
23
24 #include "et/com_err.h"
25 #include "../version.h"
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34
35 #include "ext2fs/ext2_fs.h"
36 #include "ext2fs/ext2fs.h"
37
38
39 extern int isatty(int);
40
41 const char * device_name = NULL;
42
43 /*
44  * This structure is used for keeping track of how much resources have
45  * been used for a particular pass of e2fsck.
46  */
47 struct resource_track {
48         struct timeval time_start;
49         struct timeval user_start;
50         struct timeval system_start;
51         void    *brk_start;
52 };
53
54 struct resource_track   global_rtrack;
55
56 void init_resource_track(struct resource_track *track)
57 {
58         struct rusage r;
59
60         track->brk_start = sbrk(0);
61         gettimeofday(&track->time_start, 0);
62         getrusage(RUSAGE_SELF, &r);
63         track->user_start = r.ru_utime;
64         track->system_start = r.ru_stime;
65 }
66
67 static __inline__ float timeval_subtract(struct timeval *tv1,
68                                          struct timeval *tv2)
69 {
70         return ((tv1->tv_sec - tv2->tv_sec) +
71                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
72 }
73
74 static void print_resource_track(struct resource_track *track)
75 {
76         struct rusage r;
77         struct timeval time_end;
78
79         gettimeofday(&time_end, 0);
80         getrusage(RUSAGE_SELF, &r);
81
82         printf(_("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n"),
83                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
84                timeval_subtract(&time_end, &track->time_start),
85                timeval_subtract(&r.ru_utime, &track->user_start),
86                timeval_subtract(&r.ru_stime, &track->system_start));
87 }
88
89
90
91 int main (int argc, char *argv[])
92 {
93         errcode_t       retval = 0;
94         int             exit_value = 0;
95         int             i;
96         ext2_filsys     fs;
97         ext2_inode_scan scan;
98         ext2_ino_t      ino;
99         struct ext2_inode inode;
100
101         printf(_("size of inode=%d\n"), sizeof(inode));
102
103         device_name = "/dev/hda3";
104
105         init_resource_track(&global_rtrack);
106
107         retval = ext2fs_open(device_name, 0,
108                              0, 0, unix_io_manager, &fs);
109         if (retval) {
110                 com_err(argv[0], retval, _("while trying to open %s"),
111                         device_name);
112                 exit(1);
113         }
114
115         retval = ext2fs_open_inode_scan(fs, 0, &scan);
116         if (retval) {
117                 com_err(argv[0], retval, _("while opening inode scan"));
118                 exit(1);
119         }
120         retval = ext2fs_get_next_inode(scan, &ino, &inode);
121         if (retval) {
122                 com_err(argv[0], retval, _("while starting inode scan"));
123                 exit(1);
124         }
125         while (ino) {
126                 if (!inode.i_links_count)
127                         goto next;
128                 printf("%lu\n", inode.i_blocks);
129         next:
130                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
131                 if (retval) {
132                         com_err(argv[0], retval,
133                                 _("while doing inode scan"));
134                         exit(1);
135                 }
136         }
137
138
139         ext2fs_close(fs);
140
141         print_resource_track(&global_rtrack);
142
143         return exit_value;
144 }