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