summaryrefslogtreecommitdiff
path: root/src/script/study_hardlink_lifetimes.pl
blob: 012ef6009bb43522b74d908a8978439ef145f8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/perl

use strict;

my %ns;     # parent -> fn -> ino
my %nlink;  # num links to each ino
my %since;  # when it got its second link

my @ignore = ('ll_getattr','ll_setattr','ll_forget','ll_fsync','ll_readlink','ll_statfs','ll_opendir','ll_releasedir','ll_flush','ll_release','ll_open','ll_read','ll_write');

my $when;

my $sumage;
my $numage;

sub unlink {
    my ($p,$n) = @_;
    my $i = $ns{$p}->{$n};
    my $new = --$nlink{$i};
    if ($new == 1) {
	my $age = $when - $since{$i};
	#print "$since{$i} to $when on $i\t$age\n";
	delete $since{$i};

	$numage++;
	$sumage += $age;

    } elsif ($new == 0) {
	delete $nlink{$i};
    }
    delete $ns{$p}->{$n};	
}


my ($sec, $usec, $cmd);
$_ = <>;
while (1) {
    # read trace record
    chomp;
    last unless $_ eq '@';

    chomp(my $sec = <>);
    chomp(my $usec = <>);
    $when = sprintf("%d.%06d",$sec,$usec);# + ($usec / 1000000);
    #$when = "$sec.$usec";

    chomp($cmd = <>);

    #print "cmd $cmd\n";

    if ($cmd eq 'll_lookup') {
	chomp(my $p = <>);
	chomp(my $n = <>);
	chomp(my $r = <>);
	$ns{$p}->{$n} = $r;
    }

    elsif ($cmd eq 'll_create') {
	chomp(my $p = <>);
	chomp(my $n = <>);
	<>; <>; <>; 
	chomp(my $r = <>);
	$ns{$p}->{$n} = $r;
	$nlink{$r} = 1;
    }
    elsif ($cmd eq 'll_mknod') {
	chomp(my $p = <>);
	chomp(my $n = <>);
	<>; <>; 
	chomp(my $r = <>);
	$ns{$p}->{$n} = $r;
	$nlink{$r} = 1;
    }
    elsif ($cmd eq 'll_mkdir') {
	chomp(my $p = <>);
	chomp(my $n = <>);
	<>;
	chomp(my $r = <>);
	$ns{$p}->{$n} = $r;
	$nlink{$r} = 1;
    }
    elsif ($cmd eq 'll_symlink') {
	chomp(my $p = <>);
	chomp(my $n = <>);
	<>;
	chomp(my $r = <>);
	$ns{$p}->{$n} = $r;
	$nlink{$r} = 1;
    }
    elsif ($cmd eq 'll_link') {
	chomp(my $i = <>);
   	chomp(my $p = <>);
	chomp(my $n = <>);
	$ns{$p}->{$n} = $i;
	if (++$nlink{$i} == 2) {
	    $since{$i} = $when;
	}
    }
    elsif ($cmd eq 'll_unlink' ||
	   $cmd eq 'll_rmdir') {
   	chomp(my $p = <>);
	chomp(my $n = <>);
	&unlink($p, $n);
    }
    elsif ($cmd eq 'll_rename') {
   	chomp(my $p = <>);
	chomp(my $n = <>);
   	chomp(my $np = <>);
	chomp(my $nn = <>);
	if ($ns{$np}->{$nn}) {
	    &unlink($np, $nn);
	}
	$ns{$np}->{$nn} = $ns{$p}->{$n};
	delete $ns{$p}->{$n};	
    }
    
    # skip to @
    while (<>) {
	last if $_ eq "@\n";
	print "$cmd: $_"
	    unless grep {$_ eq $cmd} @ignore;
    }
}

print "num $numage .. sum $sumage .. avg lifetime " . ($sumage / $numage) . "\n";

# dump hard link inos
for my $ino (keys %nlink) {
    next if $nlink{$ino} < 2;
    print "$ino\t$nlink{$ino}\n";
}