blob: 16380b313ace9fdf631fe79f83eb2aa59d2d3ebe [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
Theodore Ts'od1154eb2011-09-18 17:34:37 -04005#include "config.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +00006#include <string.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <termios.h>
10#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000011#ifdef HAVE_GETOPT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000012#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000013#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000014#include <unistd.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000015#include <sys/ioctl.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050016#ifdef HAVE_MALLOC_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050018#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000019#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'o54c637d2001-05-14 11:45:38 +000032#include "ext2fs/ext2_fs.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000033#include "ext2fs/ext2fs.h"
34
35
36extern int isatty(int);
37
38const 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 */
44struct resource_track {
45 struct timeval time_start;
46 struct timeval user_start;
47 struct timeval system_start;
48 void *brk_start;
49};
50
51struct resource_track global_rtrack;
52
53void init_resource_track(struct resource_track *track)
54{
55 struct rusage r;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040056
Theodore Ts'of3db3561997-04-26 13:34:30 +000057 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
64static __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
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071static void print_resource_track(struct resource_track *track)
Theodore Ts'of3db3561997-04-26 13:34:30 +000072{
73 struct rusage r;
74 struct timeval time_end;
75
76 gettimeofday(&time_end, 0);
77 getrusage(RUSAGE_SELF, &r);
78
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000079 printf(_("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +000080 (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
88int 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;
Theodore Ts'o86c627e2001-01-11 15:12:14 +000095 ext2_ino_t ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +000096 struct ext2_inode inode;
97
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000098 printf(_("size of inode=%d\n"), sizeof(inode));
Theodore Ts'of3db3561997-04-26 13:34:30 +000099
100 device_name = "/dev/hda3";
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400101
Theodore Ts'of3db3561997-04-26 13:34:30 +0000102 init_resource_track(&global_rtrack);
103
104 retval = ext2fs_open(device_name, 0,
105 0, 0, unix_io_manager, &fs);
106 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000107 com_err(argv[0], retval, _("while trying to open %s"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000108 device_name);
109 exit(1);
110 }
111
112 retval = ext2fs_open_inode_scan(fs, 0, &scan);
113 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000114 com_err(argv[0], retval, _("while opening inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000115 exit(1);
116 }
117 retval = ext2fs_get_next_inode(scan, &ino, &inode);
118 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000119 com_err(argv[0], retval, _("while starting inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000120 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,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000130 _("while doing inode scan"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000131 exit(1);
132 }
133 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400134
Theodore Ts'of3db3561997-04-26 13:34:30 +0000135
136 ext2fs_close(fs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400137
Theodore Ts'of3db3561997-04-26 13:34:30 +0000138 print_resource_track(&global_rtrack);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400139
Theodore Ts'of3db3561997-04-26 13:34:30 +0000140 return exit_value;
141}