1 | // $Id: PoolCostInfo.java,v 1.7 2007-07-26 13:43:32 tigran Exp $ |
2 | |
3 | package diskCacheV111.pools ; |
4 | import java.util.* ; |
5 | |
6 | public class PoolCostInfo implements java.io.Serializable { |
7 | |
8 | static final long serialVersionUID = 5181562551679185500L; |
9 | |
10 | private PoolQueueInfo _store = null , _restore = null , |
11 | _mover = null , _p2p = null , |
12 | _p2pClient = null ; |
13 | private Map<String, NamedPoolQueueInfo> _extendedMoverHash = null ; |
14 | private String _defaultQueueName = null ; |
15 | private PoolSpaceInfo _space = null ; |
16 | private final String _poolName ; |
17 | public PoolCostInfo( String poolName ){ _poolName = poolName ; } |
18 | public String getPoolName(){ return _poolName ; } |
19 | public class NamedPoolQueueInfo extends PoolQueueInfo { |
20 | |
21 | static final long serialVersionUID = -7097362707394583875L; |
22 | |
23 | private String _name = null ; |
24 | private NamedPoolQueueInfo( String name , int active , int maxActive , int queued ){ |
25 | super( active , maxActive , queued ) ; |
26 | _name = name ; |
27 | |
28 | } |
29 | public String getName(){ return _name ; } |
30 | @Override |
31 | public String toString(){ |
32 | return _name+"={"+super.toString()+"}" ; |
33 | } |
34 | } |
35 | public class PoolQueueInfo implements java.io.Serializable { |
36 | |
37 | static final long serialVersionUID = 1304697767284208011L; |
38 | |
39 | private int _active = 0; |
40 | private int _maxActive =0 ; |
41 | private int _queued = 0 ; |
42 | |
43 | private PoolQueueInfo( int active , int maxActive , int queued ){ |
44 | _active = active ; |
45 | _maxActive = maxActive ; |
46 | _queued = queued ; |
47 | |
48 | } |
49 | @Override |
50 | public String toString(){ return "a="+_active+";m="+_maxActive+";q="+_queued ; } |
51 | public int getActive(){ return _active ; } |
52 | public int getMaxActive(){ return _maxActive ; } |
53 | public int getQueued(){ return _queued ; } |
54 | public void modifyQueue( int diff ){ |
55 | |
56 | int total = Math.max(0, _active + _queued + diff); |
57 | |
58 | _active = Math.min( total , _maxActive ) ; |
59 | |
60 | _queued = Math.max( 0 , total - _maxActive ) ; |
61 | } |
62 | } |
63 | public PoolQueueInfo getStoreQueue(){ return _store ; } |
64 | public PoolQueueInfo getRestoreQueue(){ return _restore ; } |
65 | public PoolQueueInfo getMoverQueue(){ return _mover ; } |
66 | public PoolQueueInfo getP2pQueue(){ return _p2p ; } |
67 | public PoolQueueInfo getP2pClientQueue(){ return _p2pClient ; } |
68 | public PoolSpaceInfo getSpaceInfo(){ return _space ; } |
69 | |
70 | public class PoolSpaceInfo implements java.io.Serializable { |
71 | |
72 | static final long serialVersionUID = -8966065301943351970L; |
73 | |
74 | |
75 | |
76 | private long _total = 0 , _free = 0 , _precious = 0 , _removable = 0 , _lru = 0 ; |
77 | private long _gap = 0 ; |
78 | private double _breakEven = 0; |
79 | |
80 | private PoolSpaceInfo( long total , long free , long precious , long removable ){ |
81 | this(total, free, precious, removable, 0); |
82 | } |
83 | |
84 | private PoolSpaceInfo( long total , long free , long precious , long removable , long lru ){ |
85 | |
86 | if( total < free ) { |
87 | throw new IllegalArgumentException("total >= free"); |
88 | } |
89 | |
90 | if( total < precious ) { |
91 | throw new IllegalArgumentException("total >= precious"); |
92 | } |
93 | |
94 | if( total < removable ) { |
95 | throw new IllegalArgumentException("total >= removable"); |
96 | } |
97 | |
98 | _total = total ; |
99 | _free = free ; |
100 | _precious = precious ; |
101 | _removable = removable ; |
102 | _lru = lru ; |
103 | } |
104 | public void setParameter( double breakEven , long gap ){ |
105 | _breakEven = breakEven ; |
106 | _gap = gap ; |
107 | } |
108 | @Override |
109 | public String toString(){ |
110 | return "t="+_total+ |
111 | ";f="+_free+ |
112 | ";p="+_precious+ |
113 | ";r="+_removable+ |
114 | ";lru="+_lru+ |
115 | ";{g="+_gap+";b="+_breakEven+"}" ; |
116 | } |
117 | public long getFreeSpace(){ return _free ; } |
118 | public long getTotalSpace(){ return _total ; } |
119 | public long getPreciousSpace(){ return _precious ; } |
120 | public long getRemovableSpace(){ return _removable ; } |
121 | public long getUsedSpace(){ return _total - _free ; } |
122 | public long getPinnedSpace(){ return _total - _free - _precious - _removable ; } |
123 | public long getGap(){ return _gap ; } |
124 | public double getBreakEven(){ return _breakEven ; } |
125 | public long getLRUSeconds(){ return _lru ; } |
126 | |
127 | public void modifyPinnedSpace(long diff) |
128 | { |
129 | _free = Math.max(0, Math.min(_free - diff, _total - _removable - _precious)); |
130 | } |
131 | } |
132 | |
133 | // |
134 | /// the setters |
135 | // |
136 | public void setSpaceUsage( long total , long free , long precious , long removable ){ |
137 | _space = new PoolSpaceInfo( total , free , precious , removable ) ; |
138 | } |
139 | public void setSpaceUsage( long total , long free , long precious , long removable , long lru ){ |
140 | _space = new PoolSpaceInfo( total , free , precious , removable , lru ) ; |
141 | } |
142 | public void setQueueSizes( int moverActive , int moverMaxActive , int moverQueued , |
143 | int restoreActive , int restoreMaxActive , int restoreQueued , |
144 | int storeActive , int storeMaxActive , int storeQueued ){ |
145 | |
146 | _mover = new PoolQueueInfo( moverActive , moverMaxActive , moverQueued ) ; |
147 | _restore = new PoolQueueInfo( restoreActive , restoreMaxActive , restoreQueued ) ; |
148 | _store = new PoolQueueInfo( storeActive , storeMaxActive , storeQueued ) ; |
149 | |
150 | } |
151 | public void addExtendedMoverQueueSizes( String name , int moverActive , int moverMaxActive , int moverQueued ){ |
152 | if( _extendedMoverHash == null ){ |
153 | _defaultQueueName = name ; |
154 | _extendedMoverHash = new HashMap<String, NamedPoolQueueInfo>() ; |
155 | } |
156 | _extendedMoverHash.put( name, new NamedPoolQueueInfo( name, moverActive, moverMaxActive, moverQueued )); |
157 | } |
158 | public Map<String, NamedPoolQueueInfo> getExtendedMoverHash(){ return _extendedMoverHash ; } |
159 | public String getDefaultQueueName(){ return _defaultQueueName ; } |
160 | public void setP2pServerQueueSizes( int p2pActive , int p2pMaxActive , int p2pQueued ){ |
161 | _p2p = new PoolQueueInfo( p2pActive , p2pMaxActive , p2pQueued ) ; |
162 | } |
163 | public void setP2pClientQueueSizes( int p2pClientActive , int p2pClientMaxActive , int p2pClientQueued ){ |
164 | _p2pClient = new PoolQueueInfo( p2pClientActive , p2pClientMaxActive , p2pClientQueued ) ; |
165 | } |
166 | @Override |
167 | public String toString() { |
168 | StringBuffer sb = new StringBuffer() ; |
169 | |
170 | sb.append(_poolName).append("={R={").append(_restore.toString()). |
171 | append("};S={").append(_store.toString()). |
172 | append("};M={").append(_mover.toString()) ; |
173 | if( _p2p != null )sb.append("};PS={").append(_p2p.toString()) ; |
174 | if( _p2pClient != null )sb.append("};PC={").append(_p2pClient.toString()) ; |
175 | sb.append("};SP={").append(_space.toString()).append("};"); |
176 | if( _extendedMoverHash != null ){ |
177 | sb.append("XM={"); |
178 | for( NamedPoolQueueInfo namedPoolQueueInfo : _extendedMoverHash.values() ){ |
179 | sb.append( namedPoolQueueInfo.toString() ).append(";"); |
180 | } |
181 | sb.append("};"); |
182 | } |
183 | sb.append("}"); |
184 | |
185 | return sb.toString(); |
186 | } |
187 | |
188 | |
189 | |
190 | } |