-
Notifications
You must be signed in to change notification settings - Fork 131
/
MemoryWatch.java
217 lines (202 loc) · 6.28 KB
/
MemoryWatch.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package dmg.cells.services ;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;
import dmg.cells.nucleus.CellAdapter;
import dmg.cells.nucleus.CellNucleus;
import org.dcache.util.Args;
public class MemoryWatch extends CellAdapter implements Runnable {
private static final Logger _log =
LoggerFactory.getLogger(MemoryWatch.class);
private CellNucleus _nucleus;
private Args _args;
private long _update = 10 ;
private final Object _lock = new Object() ;
private Thread _queryThread;
private Runtime _runtime = Runtime.getRuntime() ;
private boolean _output;
private String _outputFile;
private int _generations = 2 ;
private int _current;
private int _maxFileSize = 1024 * 1024 ;
public MemoryWatch(String name, String args)
{
super(name, args);
_nucleus = getNucleus();
_args = getArgs();
}
@Override
protected void startUp() throws Exception
{
String var;
//
// update
//
if ((var = _args.getOpt("update")) != null) {
try {
_update = Integer.parseInt(var);
} catch (Exception ee) {
_log.warn("Update not accepted : " + var);
}
}
//
// filesize
//
if ((var = _args.getOpt("maxFilesize")) != null) {
try {
_maxFileSize = Integer.parseInt(var);
} catch (Exception ee) {
_log.warn("New 'maxFilesize' not accepted : " + var);
}
}
//
// generations
//
if ((var = _args.getOpt("generations")) != null) {
try {
_generations = Integer.parseInt(var);
} catch (Exception ee) {
_log.warn("New 'generations' not accepted : " + var);
}
}
if ((var = _args.getOpt("output")) != null) {
_output = true;
if (!var.equals("")) {
_outputFile = var;
}
}
//
// and now the worker.
//
_queryThread = _nucleus.newThread(this, "queryThread");
}
@Override
protected void started()
{
_queryThread.start();
}
@Override
protected void cleanUp()
{
if (_queryThread != null) {
_queryThread.interrupt();
}
}
public void say(String str ){
if( _output ){
if( _outputFile != null ){
try{
PrintWriter pw;
String name = _outputFile + "." +( _current % _generations ) ;
File f = new File(name) ;
if( f.exists() && ( f.length() > _maxFileSize ) ){
_current++ ;
name = _outputFile + "." +( _current % _generations ) ;
pw = new PrintWriter(
new FileWriter( name , false ) ) ;
}else{
pw = new PrintWriter(
new FileWriter( name , true ) ) ;
}
try{
pw.println(str) ;
}catch(Exception ee){
}finally{
try{ pw.close() ; }catch(Exception eeee ){}
}
}catch(Exception xx){}
}else{
_log.info(str) ;
}
}
}
@Override
@SuppressFBWarnings(
value="DM_GC",
justification="Although bad practice, the GC call is part of the design of the cell"
)
public void run(){
while( ! Thread.interrupted() ){
_runtime.gc() ;
long fm = _runtime.freeMemory() ;
long tm = _runtime.totalMemory() ;
say( " free "+fm+ " total "+tm+" used "+(tm-fm)+
" "+(new Date()).toString() ) ;
try{
long update ;
synchronized(_lock){
update = _update * 1000 ;
}
Thread.sleep( update ) ;
}catch(InterruptedException e ){
break ;
}
}
say( "Update thread finished" ) ;
}
@Override
public void getInfo( PrintWriter pw ){
super.getInfo(pw);
pw.println("Output : "+
(_output?
""+(_outputFile==null?"<stdout>":_outputFile):
"disabled" ) ) ;
pw.println("Update : "+_update+" seconds" ) ;
}
public static final String hh_set_generations = "<outputfileGenerations(1...10)>" ;
public String ac_set_generations_$_1( Args args )
{
int g = Integer.parseInt( args.argv(0) ) ;
if( ( g < 1 ) || ( g > 10 ) ) {
throw new
IllegalArgumentException("Generations not in range (1...10)");
}
_generations = g ;
return "OutputFile generations = "+_generations ;
}
public static final String hh_set_maxFilesize = "<output filesize limit(>10k)>" ;
public String ac_set_maxFilesize_$_1( Args args )
{
int g = Integer.parseInt( args.argv(0) ) ;
if( g < 1024 ) {
throw new
IllegalArgumentException("maxFilesize not in range (>1k)");
}
_maxFileSize = g ;
return "Maximum output filesize = "+_maxFileSize ;
}
public static final String hh_set_output = "off|on|<filename>" ;
public String ac_set_output_$_1( Args args ){
String what = args.argv(0) ;
switch (what) {
case "off":
_output = false;
_outputFile = null;
break;
case "on":
_output = true;
_outputFile = null;
break;
default:
_outputFile = what;
_output = true;
break;
}
return "Output "+
(_output?
"set to "+(_outputFile==null?"stdout":_outputFile):
"disabled" );
}
public static final String hh_set_update = "<updateTime/sec>" ;
public String ac_set_update_$_1( Args args )throws NumberFormatException {
synchronized( _lock ) {
_update = Integer.parseInt( args.argv(0) ) ;
}
return "Update time set to "+_update+" seconds" ;
}
public String ac_gc( Args args ){ _runtime.gc() ; return "" ; }
}