| 1 |
package fun; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.FileReader; |
| 5 |
import java.text.SimpleDateFormat; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Arrays; |
| 8 |
import java.util.Date; |
| 9 |
import java.util.TimeZone; |
| 10 |
import java.util.regex.Pattern; |
| 11 |
|
| 12 |
public class Foo3 { |
| 13 |
|
| 14 |
public static void main(String[] args) throws Exception{ |
| 15 |
final boolean slow=args.length<2 ? false : "slow".equalsIgnoreCase(args[1]); |
| 16 |
final BufferedReader reader=new BufferedReader(new FileReader(args[0])); |
| 17 |
long sum=0, lines=0, chars=0; |
| 18 |
|
| 19 |
LongList sizes=new LongList(100000000); |
| 20 |
ArrayList<Pair> list=new ArrayList<Pair>(10000000); |
| 21 |
for(String line=reader.readLine(); line!=null; line=reader.readLine()) { |
| 22 |
final Pair p=(slow ? processSlow(line) : processFast(line)); |
| 23 |
if(p!=null) { |
| 24 |
sum+=p.size; |
| 25 |
lines++; |
| 26 |
chars+=line.length(); |
| 27 |
list.add(p); |
| 28 |
sizes.add(p.size); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
reader.close(); |
| 33 |
|
| 34 |
psort(list); |
| 35 |
final long tebi=1024L*1024L*1024L*1024L; |
| 36 |
final long tera=1000L*1000L*1000L*1000L; |
| 37 |
final int[] idxArray=new int[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 95}; |
| 38 |
final int[] pairArray=idxArray.clone(); |
| 39 |
for(int i=0; i<idxArray.length; i++) {pairArray[i]=(int)(idxArray[i]*.01*list.size());} |
| 40 |
long tsum=0; |
| 41 |
for(int i=0, nextIdx=0, nextPair=pairArray[0]; i<list.size() && nextIdx<idxArray.length; i++) { |
| 42 |
Pair p=list.get(i); |
| 43 |
tsum+=p.size; |
| 44 |
if(i>=nextPair) { |
| 45 |
String s=idxArray[nextIdx]+" percent of files have not been accessed since: "+ |
| 46 |
timeString(p.time*1000)+" ("+(tsum/tebi)+" tebibytes)"; |
| 47 |
System.out.println(s); |
| 48 |
nextIdx++; |
| 49 |
if(nextIdx<idxArray.length) {nextPair=pairArray[nextIdx];} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
sizes.sort(); |
| 54 |
long mean=sum/sizes.size; |
| 55 |
long median=sizes.get((int)(sizes.size*0.5)); |
| 56 |
System.out.println("total size: \t"+(sum/tera)+" TB \t("+sum+")"+"\t"+"("+((sum/tebi))+" tebibytes)"); |
| 57 |
System.out.println("mean size: \t"+mean+" bytes"); |
| 58 |
System.out.println("P50 size: \t"+median+" bytes"); |
| 59 |
System.out.println("P80 size: \t"+sizes.get((int)(sizes.size*0.8))+" bytes"); |
| 60 |
System.out.println("P90 size: \t"+sizes.get((int)(sizes.size*0.9))+" bytes"); |
| 61 |
System.out.println("P95 size: \t"+sizes.get((int)(sizes.size*0.95))+" bytes"); |
| 62 |
} |
| 63 |
|
| 64 |
static <T extends Comparable<? super T>> void psort(ArrayList<T> list) { |
| 65 |
@SuppressWarnings("unchecked") |
| 66 |
T[] array=list.toArray((T[])new Comparable[0]); |
| 67 |
list.clear(); |
| 68 |
Arrays.parallelSort(array); |
| 69 |
for(T r : array){list.add(r);} |
| 70 |
} |
| 71 |
|
| 72 |
static String timeString(long time){ |
| 73 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 74 |
sdf.setTimeZone(TimeZone.getTimeZone("PST")); |
| 75 |
return sdf.format(new Date(time)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static Pair processSlow(String line) { |
| 87 |
String[] split=pipePattern.split(line); |
| 88 |
if(split[6].charAt(0)!='F') {return null;} |
| 89 |
long size=Long.parseLong(split[3]); |
| 90 |
long time=Long.parseLong(split[11]); |
| 91 |
return new Pair(size, time); |
| 92 |
} |
| 93 |
|
| 94 |
static Pair processFast(String line) { |
| 95 |
final int delimiter='|'; |
| 96 |
final int len=line.length(); |
| 97 |
int a=0, b=0; |
| 98 |
|
| 99 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 100 |
b++; |
| 101 |
a=b; |
| 102 |
|
| 103 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 104 |
b++; |
| 105 |
a=b; |
| 106 |
|
| 107 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 108 |
b++; |
| 109 |
a=b; |
| 110 |
|
| 111 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 112 |
assert(b>a) : "Missing term size: '"+new String(line)+"'"; |
| 113 |
long size=parseLong(line, a, b); |
| 114 |
b++; |
| 115 |
a=b; |
| 116 |
|
| 117 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 118 |
b++; |
| 119 |
a=b; |
| 120 |
|
| 121 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 122 |
b++; |
| 123 |
a=b; |
| 124 |
|
| 125 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 126 |
assert(b>a) : "Missing term type: '"+new String(line)+"'"; |
| 127 |
if(line.charAt(a)!='F') {return null;} |
| 128 |
b++; |
| 129 |
a=b; |
| 130 |
|
| 131 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 132 |
b++; |
| 133 |
a=b; |
| 134 |
|
| 135 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 136 |
b++; |
| 137 |
a=b; |
| 138 |
|
| 139 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 140 |
b++; |
| 141 |
a=b; |
| 142 |
|
| 143 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 144 |
b++; |
| 145 |
a=b; |
| 146 |
|
| 147 |
while(b<len && line.charAt(b)!=delimiter){b++;} |
| 148 |
assert(b>a) : "Missing term access time: '"+new String(line)+"'"; |
| 149 |
long time=parseLong(line, a, b); |
| 150 |
b++; |
| 151 |
a=b; |
| 152 |
|
| 153 |
return new Pair(size, time); |
| 154 |
} |
| 155 |
|
| 156 |
static long parseLong(String array, int a, int b){ |
| 157 |
assert(b>a); |
| 158 |
long r=0; |
| 159 |
final byte z='0'; |
| 160 |
long mult=1; |
| 161 |
if(array.charAt(a)=='-'){mult=-1; a++;} |
| 162 |
for(; a<b; a++){ |
| 163 |
int x=(array.charAt(a)-z); |
| 164 |
assert(x<10 && x>=0) : x+" = "+array.charAt(a)+"\narray="+new String(array)+", start="+a+", stop="+b; |
| 165 |
r=(r*10)+x; |
| 166 |
} |
| 167 |
return r*mult; |
| 168 |
} |
| 169 |
|
| 170 |
public static final long min(long x, long y){return x<y ? x : y;} |
| 171 |
|
| 172 |
private static class Pair implements Comparable<Pair> { |
| 173 |
|
| 174 |
Pair(long size_, long time_){ |
| 175 |
size=size_; |
| 176 |
time=time_; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public int compareTo(Pair b) { |
| 181 |
return time>b.time ? 1 : time<b.time ? -1 : 0; |
| 182 |
} |
| 183 |
|
| 184 |
final long size, time; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static final int LOWER_BITS=31; |
| 201 |
static final int MANTISSA_BITS=24; |
| 202 |
static final int EXP_BITS=LOWER_BITS-MANTISSA_BITS; |
| 203 |
static final int UPPER_BITS=64-MANTISSA_BITS; |
| 204 |
static final long LOWER_MASK=~((-1L)<<LOWER_BITS); |
| 205 |
static final long MANTISSA_MASK=~((-1L)<<MANTISSA_BITS); |
| 206 |
static final long compress(long raw) { |
| 207 |
if(raw<=MANTISSA_MASK){return raw;} |
| 208 |
int leading=Long.numberOfLeadingZeros(raw); |
| 209 |
int exp=UPPER_BITS-leading; |
| 210 |
assert(exp>=1); |
| 211 |
return (raw>>>exp)|(exp<<MANTISSA_BITS); |
| 212 |
} |
| 213 |
static final long decompress(long f) { |
| 214 |
if(f<=MANTISSA_MASK){return f;} |
| 215 |
int exp=(int)(f>>>MANTISSA_BITS); |
| 216 |
assert(exp>=1); |
| 217 |
return (f&MANTISSA_MASK)<<exp; |
| 218 |
} |
| 219 |
static final long combine(long time, long size) { |
| 220 |
return (time<<LOWER_BITS) | compress(size); |
| 221 |
} |
| 222 |
static final long getTime(long combined) { |
| 223 |
return combined>>>LOWER_BITS; |
| 224 |
} |
| 225 |
static final long getSize(long combined) { |
| 226 |
return decompress(combined&LOWER_MASK); |
| 227 |
} |
| 228 |
|
| 229 |
static class LongList{ |
| 230 |
|
| 231 |
public LongList(int initial){ |
| 232 |
assert(initial>0) : initial; |
| 233 |
array=new long[initial]; |
| 234 |
} |
| 235 |
|
| 236 |
public final long get(int loc){ |
| 237 |
return array[loc]; |
| 238 |
} |
| 239 |
|
| 240 |
public final void add(long x){ |
| 241 |
if(size>=array.length){ |
| 242 |
resize(size*2L+1); |
| 243 |
} |
| 244 |
array[size]=x; |
| 245 |
size++; |
| 246 |
} |
| 247 |
|
| 248 |
private final void resize(final long size2){ |
| 249 |
assert(size2>size) : size+", "+size2; |
| 250 |
final int size3=(int)min(MAX_ARRAY_LEN, size2); |
| 251 |
assert(size3>size) : "Overflow: "+size+", "+size2+" -> "+size3; |
| 252 |
array=Arrays.copyOf(array, size3); |
| 253 |
} |
| 254 |
|
| 255 |
public void sort() { |
| 256 |
if(size>1){Arrays.parallelSort(array, 0, size);} |
| 257 |
} |
| 258 |
|
| 259 |
public long[] array; |
| 260 |
|
| 261 |
public int size=0; |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
static final Pattern pipePattern=Pattern.compile("\\|"); |
| 266 |
static final int MAX_ARRAY_LEN=Integer.MAX_VALUE-20; |
| 267 |
|
| 268 |
} |