blob: 370947641d31ea59cdbf17081f5ff7493b1029ff [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'o50e1e101997-04-26 13:58:21 +000014#ifdef HAVE_MNTENT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000015#include <mntent.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000016#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <sys/ioctl.h>
18#include <malloc.h>
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
Theodore Ts'of3db3561997-04-26 13:34:30 +000032#include <linux/ext2_fs.h>
33
34#include "ext2fs/ext2fs.h"
35
36
37extern int isatty(int);
38
39const char * device_name = NULL;
40
41/*
42 * This structure is used for keeping track of how much resources have
43 * been used for a particular pass of e2fsck.
44 */
45struct resource_track {
46 struct timeval time_start;
47 struct timeval user_start;
48 struct timeval system_start;
49 void *brk_start;
50};
51
52struct resource_track global_rtrack;
53
54void init_resource_track(struct resource_track *track)
55{
56 struct rusage r;
57
58 track->brk_start = sbrk(0);
59 gettimeofday(&track->time_start, 0);
60 getrusage(RUSAGE_SELF, &r);
61 track->user_start = r.ru_utime;
62 track->system_start = r.ru_stime;
63}
64
65static __inline__ float timeval_subtract(struct timeval *tv1,
66 struct timeval *tv2)
67{
68 return ((tv1->tv_sec - tv2->tv_sec) +
69 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
70}
71
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000072static void print_resource_track(struct resource_track *track)
Theodore Ts'of3db3561997-04-26 13:34:30 +000073{
74 struct rusage r;
75 struct timeval time_end;
76
77 gettimeofday(&time_end, 0);
78 getrusage(RUSAGE_SELF, &r);
79
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000080 printf(_("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +000081 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
82 timeval_subtract(&time_end, &track->time_start),
83 timeval_subtract(&r.ru_utime, &track->user_start),
84 timeval_subtract(&r.ru_stime, &track->system_start));
85}
86
87
88
89int main (int argc, char *argv[])
90{
91 errcode_t retval = 0;
92 int exit_value = 0;
93 int i;
94 ext2_filsys fs;
95 ext2_inode_scan scan;
Theodore Ts'o86c627e2001-01-11 15:12:14 +000096 ext2_ino_t ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +000097 struct ext2_inode inode;
98
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000099 printf(_("size of inode=%d\n"), sizeof(inode));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000100
101 device_name = "/dev/hda3";
102
103 init_resource_track(&global_rtrack);
104
105 retval = ext2fs_open(device_name, 0,
106 0, 0, unix_io_manager, &fs);
107 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000108 com_err(argv[0], retval, _("while trying to open %s"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000109 device_name);
110 exit(1);
111 }
112
113 retval = ext2fs_open_inode_scan(fs, 0, &scan);
114 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000115 com_err(argv[0], retval, _("while opening inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000116 exit(1);
117 }
118 retval = ext2fs_get_next_inode(scan, &ino, &inode);
119 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000120 com_err(argv[0], retval, _("while starting inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121 exit(1);
122 }
123 while (ino) {
124 if (!inode.i_links_count)
125 goto next;
126 printf("%lu\n", inode.i_blocks);
127 next:
128 retval = ext2fs_get_next_inode(scan, &ino, &inode);
129 if (retval) {
130 com_err(argv[0], retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000131 _("while doing inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000132 exit(1);
133 }
134 }
135
136
137 ext2fs_close(fs);
138
139 print_resource_track(&global_rtrack);
140
141 return exit_value;
142}