blob: 5ab97db6046af0a369fbb60ab52c11dbdbd8b700 [file] [log] [blame]
Theodore Ts'of3db3561997-04-26 13:34:30 +00001/*
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>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010#ifdef HAVE_GETOPT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000011#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000012#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000013#include <unistd.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000014#include <sys/ioctl.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050015#ifdef HAVE_MALLOC_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000016#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050017#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000018#include <sys/resource.h>
19
20#include "et/com_err.h"
21#include "../version.h"
22
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <sys/time.h>
30
Theodore Ts'o54c637d2001-05-14 11:45:38 +000031#include "ext2fs/ext2_fs.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000032#include "ext2fs/ext2fs.h"
33
34
35extern int isatty(int);
36
37const char * device_name = NULL;
38
39/*
40 * This structure is used for keeping track of how much resources have
41 * been used for a particular pass of e2fsck.
42 */
43struct resource_track {
44 struct timeval time_start;
45 struct timeval user_start;
46 struct timeval system_start;
47 void *brk_start;
48};
49
50struct resource_track global_rtrack;
51
52void init_resource_track(struct resource_track *track)
53{
54 struct rusage r;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040055
Theodore Ts'of3db3561997-04-26 13:34:30 +000056 track->brk_start = sbrk(0);
57 gettimeofday(&track->time_start, 0);
58 getrusage(RUSAGE_SELF, &r);
59 track->user_start = r.ru_utime;
60 track->system_start = r.ru_stime;
61}
62
63static __inline__ float timeval_subtract(struct timeval *tv1,
64 struct timeval *tv2)
65{
66 return ((tv1->tv_sec - tv2->tv_sec) +
67 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
68}
69
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000070static void print_resource_track(struct resource_track *track)
Theodore Ts'of3db3561997-04-26 13:34:30 +000071{
72 struct rusage r;
73 struct timeval time_end;
74
75 gettimeofday(&time_end, 0);
76 getrusage(RUSAGE_SELF, &r);
77
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000078 printf(_("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +000079 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
80 timeval_subtract(&time_end, &track->time_start),
81 timeval_subtract(&r.ru_utime, &track->user_start),
82 timeval_subtract(&r.ru_stime, &track->system_start));
83}
84
85
86
87int main (int argc, char *argv[])
88{
89 errcode_t retval = 0;
90 int exit_value = 0;
91 int i;
92 ext2_filsys fs;
93 ext2_inode_scan scan;
Theodore Ts'o86c627e2001-01-11 15:12:14 +000094 ext2_ino_t ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +000095 struct ext2_inode inode;
96
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000097 printf(_("size of inode=%d\n"), sizeof(inode));
Theodore Ts'of3db3561997-04-26 13:34:30 +000098
99 device_name = "/dev/hda3";
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400100
Theodore Ts'of3db3561997-04-26 13:34:30 +0000101 init_resource_track(&global_rtrack);
102
103 retval = ext2fs_open(device_name, 0,
104 0, 0, unix_io_manager, &fs);
105 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000106 com_err(argv[0], retval, _("while trying to open %s"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000107 device_name);
108 exit(1);
109 }
110
111 retval = ext2fs_open_inode_scan(fs, 0, &scan);
112 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000113 com_err(argv[0], retval, _("while opening inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000114 exit(1);
115 }
116 retval = ext2fs_get_next_inode(scan, &ino, &inode);
117 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000118 com_err(argv[0], retval, _("while starting inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000119 exit(1);
120 }
121 while (ino) {
122 if (!inode.i_links_count)
123 goto next;
124 printf("%lu\n", inode.i_blocks);
125 next:
126 retval = ext2fs_get_next_inode(scan, &ino, &inode);
127 if (retval) {
128 com_err(argv[0], retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000129 _("while doing inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000130 exit(1);
131 }
132 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400133
Theodore Ts'of3db3561997-04-26 13:34:30 +0000134
135 ext2fs_close(fs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400136
Theodore Ts'of3db3561997-04-26 13:34:30 +0000137 print_resource_track(&global_rtrack);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400138
Theodore Ts'of3db3561997-04-26 13:34:30 +0000139 return exit_value;
140}