1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import org.apache.hadoop.conf.Configuration; |
---|
21 | import org.apache.hadoop.util.Shell; |
---|
22 | |
---|
23 | import java.io.BufferedReader; |
---|
24 | import java.io.File; |
---|
25 | import java.io.IOException; |
---|
26 | import java.util.concurrent.atomic.AtomicLong; |
---|
27 | |
---|
28 | /** Filesystem disk space usage statistics. Uses the unix 'du' program*/ |
---|
29 | public class DU extends Shell { |
---|
30 | private String dirPath; |
---|
31 | |
---|
32 | private AtomicLong used = new AtomicLong(); |
---|
33 | private volatile boolean shouldRun = true; |
---|
34 | private Thread refreshUsed; |
---|
35 | private IOException duException = null; |
---|
36 | private long refreshInterval; |
---|
37 | |
---|
38 | /** |
---|
39 | * Keeps track of disk usage. |
---|
40 | * @param path the path to check disk usage in |
---|
41 | * @param interval refresh the disk usage at this interval |
---|
42 | * @throws IOException if we fail to refresh the disk usage |
---|
43 | */ |
---|
44 | public DU(File path, long interval) throws IOException { |
---|
45 | super(0); |
---|
46 | |
---|
47 | //we set the Shell interval to 0 so it will always run our command |
---|
48 | //and use this one to set the thread sleep interval |
---|
49 | this.refreshInterval = interval; |
---|
50 | this.dirPath = path.getCanonicalPath(); |
---|
51 | |
---|
52 | //populate the used variable |
---|
53 | run(); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Keeps track of disk usage. |
---|
58 | * @param path the path to check disk usage in |
---|
59 | * @param conf configuration object |
---|
60 | * @throws IOException if we fail to refresh the disk usage |
---|
61 | */ |
---|
62 | public DU(File path, Configuration conf) throws IOException { |
---|
63 | this(path, 600000L); |
---|
64 | //10 minutes default refresh interval |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * This thread refreshes the "used" variable. |
---|
69 | * |
---|
70 | * Future improvements could be to not permanently |
---|
71 | * run this thread, instead run when getUsed is called. |
---|
72 | **/ |
---|
73 | class DURefreshThread implements Runnable { |
---|
74 | |
---|
75 | public void run() { |
---|
76 | |
---|
77 | while(shouldRun) { |
---|
78 | |
---|
79 | try { |
---|
80 | Thread.sleep(refreshInterval); |
---|
81 | |
---|
82 | try { |
---|
83 | //update the used variable |
---|
84 | DU.this.run(); |
---|
85 | } catch (IOException e) { |
---|
86 | synchronized (DU.this) { |
---|
87 | //save the latest exception so we can return it in getUsed() |
---|
88 | duException = e; |
---|
89 | } |
---|
90 | |
---|
91 | LOG.warn("Could not get disk usage information", e); |
---|
92 | } |
---|
93 | } catch (InterruptedException e) { |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Decrease how much disk space we use. |
---|
101 | * @param value decrease by this value |
---|
102 | */ |
---|
103 | public void decDfsUsed(long value) { |
---|
104 | used.addAndGet(-value); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Increase how much disk space we use. |
---|
109 | * @param value increase by this value |
---|
110 | */ |
---|
111 | public void incDfsUsed(long value) { |
---|
112 | used.addAndGet(value); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * @return disk space used |
---|
117 | * @throws IOException if the shell command fails |
---|
118 | */ |
---|
119 | public long getUsed() throws IOException { |
---|
120 | //if the updating thread isn't started, update on demand |
---|
121 | if(refreshUsed == null) { |
---|
122 | run(); |
---|
123 | } else { |
---|
124 | synchronized (DU.this) { |
---|
125 | //if an exception was thrown in the last run, rethrow |
---|
126 | if(duException != null) { |
---|
127 | IOException tmp = duException; |
---|
128 | duException = null; |
---|
129 | throw tmp; |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | return used.longValue(); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * @return the path of which we're keeping track of disk usage |
---|
139 | */ |
---|
140 | public String getDirPath() { |
---|
141 | return dirPath; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Start the disk usage checking thread. |
---|
146 | */ |
---|
147 | public void start() { |
---|
148 | //only start the thread if the interval is sane |
---|
149 | if(refreshInterval > 0) { |
---|
150 | refreshUsed = new Thread(new DURefreshThread(), |
---|
151 | "refreshUsed-"+dirPath); |
---|
152 | refreshUsed.setDaemon(true); |
---|
153 | refreshUsed.start(); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Shut down the refreshing thread. |
---|
159 | */ |
---|
160 | public void shutdown() { |
---|
161 | this.shouldRun = false; |
---|
162 | |
---|
163 | if(this.refreshUsed != null) { |
---|
164 | this.refreshUsed.interrupt(); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | public String toString() { |
---|
169 | return |
---|
170 | "du -sk " + dirPath +"\n" + |
---|
171 | used + "\t" + dirPath; |
---|
172 | } |
---|
173 | |
---|
174 | protected String[] getExecString() { |
---|
175 | return new String[] {"du", "-sk", dirPath}; |
---|
176 | } |
---|
177 | |
---|
178 | protected void parseExecResult(BufferedReader lines) throws IOException { |
---|
179 | String line = lines.readLine(); |
---|
180 | if (line == null) { |
---|
181 | throw new IOException("Expecting a line not the end of stream"); |
---|
182 | } |
---|
183 | String[] tokens = line.split("\t"); |
---|
184 | if(tokens.length == 0) { |
---|
185 | throw new IOException("Illegal du output"); |
---|
186 | } |
---|
187 | this.used.set(Long.parseLong(tokens[0])*1024); |
---|
188 | } |
---|
189 | |
---|
190 | public static void main(String[] args) throws Exception { |
---|
191 | String path = "."; |
---|
192 | if (args.length > 0) { |
---|
193 | path = args[0]; |
---|
194 | } |
---|
195 | |
---|
196 | System.out.println(new DU(new File(path), new Configuration()).toString()); |
---|
197 | } |
---|
198 | } |
---|