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