0001 function retval=eidors_cache( command, limit )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 global eidors_objects;
0042 if nargin<1
0043 fprintf('EIDORS_CACHE: current max memory = %5.1fMB\n', ...
0044 eidors_objects.max_cache_size/1e6);
0045 ww= whos('eidors_objects');
0046 fprintf('EIDORS_CACHE: cache memory used = %5.1fMB\n', ...
0047 ww.bytes/1e6);
0048 fprintf('EIDORS_CACHE: current priority = %d\n', ...
0049 eidors_objects.cache_priority);
0050 return;
0051 elseif nargin>=2
0052 if ischar(limit); limit= str2num(limit); end
0053 end
0054
0055
0056
0057 switch command
0058 case {'clear_all','clear'}
0059 [objid, times, sizes, prios, priidx] = get_names_times;
0060 remove_objids( objid, sizes, 1:length(sizes) );
0061
0062 case 'cache_size'
0063 if nargin==2
0064 eidors_objects.max_cache_size = limit;
0065 else
0066 retval= eidors_objects.max_cache_size;
0067 end
0068
0069 case 'boost_priority'
0070 try
0071 retval= eidors_objects.cache_priority;
0072 catch
0073 retval= 0;
0074 end
0075 if nargin==2
0076 retval = retval + limit;
0077 end
0078 eidors_objects.cache_priority = retval;
0079
0080 case 'show_objs'
0081 [objid, times, sizes, prios, priidx] = get_names_times;
0082 for i=1:length(times)
0083 fprintf('t=%9.7f b=%9.0d p=%02d, i=%03d: %s\n', rem(times(i),1), ...
0084 sizes(i), prios(i), priidx(i), objid{i} );
0085 end
0086
0087 case 'clear_max'
0088
0089
0090
0091 [objid, times, sizes, prios, priidx] = get_names_times;
0092 tot= cumsum(sizes(priidx));
0093 remove = find(tot > limit);
0094 rmidx= priidx(remove);
0095 remove_objids( objid, sizes, rmidx);
0096
0097 case 'clear_old'
0098 [objid, times, sizes, prios, priidx] = get_names_times;
0099 remove_objids( objid, sizes, ...
0100 find(times < limit) );
0101
0102 case 'clear_new'
0103 [objid, times, sizes, prios, priidx] = get_names_times;
0104 remove_objids( objid, sizes, ...
0105 find(times > limit) );
0106
0107 case 'clear_model_library'
0108
0109 delete([eidors_objects.model_cache,'/*.mat']);
0110
0111 otherwise
0112 error('command %s not understood',command);
0113 end
0114
0115
0116 function [objid, times, sizes, prios, priidx] = get_names_times;
0117 objid={}; times=[]; sizes=[]; pri=[]; prios=[]; priidx=[];
0118 global eidors_objects;
0119 if isempty(eidors_objects); return; end
0120 idx=1;
0121 for fn= fieldnames(eidors_objects)'
0122 fn1= fn{1};
0123 if fn1(1:3)== 'id_'
0124 objid{idx}= fn1;
0125
0126 obj = getfield(eidors_objects, fn1 );
0127 try
0128 times(idx) = obj.last_used;
0129 catch
0130 times(idx) = 0;
0131 end
0132 try
0133 prios(idx) = obj.priority;
0134 catch
0135 prios(idx) = 0;
0136 end
0137
0138 ww= whos('obj');
0139 sizes(idx) = ww.bytes;
0140
0141 idx= idx+1;
0142 end
0143 end
0144
0145
0146 [times, idx] = sort(-times);
0147 times= -times;
0148 objid= objid(idx);
0149 sizes= sizes(idx);
0150 prios= prios(idx);
0151
0152 [jnk, priidx] = sortrows([-prios(:), -times(:)]);
0153
0154 function remove_objids( objid, sizes, idx)
0155 global eidors_objects;
0156 eidors_objects= rmfield( eidors_objects, objid( idx ) );
0157 eidors_msg('eidors_cache: removing %d objects with %d bytes', ...
0158 length(idx), sum(sizes(idx)), 3 );