eidors_cache

PURPOSE ^

Control eidors_caching

SYNOPSIS ^

function retval=eidors_cache( command, limit )

DESCRIPTION ^

 Control eidors_caching
 Usage: eidors_cache( command, limit )

 USAGE:
   eidors_cache( 'clear_all' ) 
   eidors_cache  clear

   eidors_cache( 'clear_old'. timestamp );
   eidors_cache( 'clear_new', timestamp );
      - clear all variables older (or newer) than timestamp
      - example:
         time_now= now;
         lots_of_eidors_calcs % don't need to cache this stuff
         eidors_cache('clear_new',time_now);

   eidors_cache( 'clear_max', memory_in_bytes );
      - clear cache so it has less than memory_in_bytes

   eidors_cache( 'cache_size', memory_in_bytes );
      - set max cache size to be memory_in_bytes
      - without 2nd arg will return current cache_size

   eidors_cache( 'clear_model_library' );
      - clear the eidors model library directory
      - NOTE: this function is experimental. 
      - TODO: add a way to specify what to clear
  
   eidors_cache( 'boost_priority', value)
      - modify the priority of the next cached items
      - low priority variables will be deleted first when memory is tight

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function retval=eidors_cache( command, limit )
0002 % Control eidors_caching
0003 % Usage: eidors_cache( command, limit )
0004 %
0005 % USAGE:
0006 %   eidors_cache( 'clear_all' )
0007 %   eidors_cache  clear
0008 %
0009 %   eidors_cache( 'clear_old'. timestamp );
0010 %   eidors_cache( 'clear_new', timestamp );
0011 %      - clear all variables older (or newer) than timestamp
0012 %      - example:
0013 %         time_now= now;
0014 %         lots_of_eidors_calcs % don't need to cache this stuff
0015 %         eidors_cache('clear_new',time_now);
0016 %
0017 %   eidors_cache( 'clear_max', memory_in_bytes );
0018 %      - clear cache so it has less than memory_in_bytes
0019 %
0020 %   eidors_cache( 'cache_size', memory_in_bytes );
0021 %      - set max cache size to be memory_in_bytes
0022 %      - without 2nd arg will return current cache_size
0023 %
0024 %   eidors_cache( 'clear_model_library' );
0025 %      - clear the eidors model library directory
0026 %      - NOTE: this function is experimental.
0027 %      - TODO: add a way to specify what to clear
0028 %
0029 %   eidors_cache( 'boost_priority', value)
0030 %      - modify the priority of the next cached items
0031 %      - low priority variables will be deleted first when memory is tight
0032 
0033 % (C) 2005 Andy Adler. Licensed under GPL version 2
0034 % $Id: eidors_cache.html 2819 2011-09-07 16:43:11Z aadler $
0035 
0036 % Comments
0037 % Want to clear specific structures
0038 %      to clear old variables
0039 %      to clear specific parts of structures
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; % default priority
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), ... %today
0084              sizes(i), prios(i), priidx(i), objid{i} ); 
0085       end
0086 
0087    case 'clear_max'
0088 % This will remove just in order of priidx.
0089 %     remove_objids( objid, sizes,  find(cumsum(sizes) > limit) );
0090 % Remove in order of time + priority
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       %TODO: add ways to select what to delete
0109       delete([eidors_objects.model_cache,'/*.mat']);
0110    
0111    otherwise
0112       error('command %s not understood',command);
0113 end
0114 
0115 % priidx is priority index, where 1 prio is 0.1 of a day
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; % old
0131          end
0132          try
0133             prios(idx) = obj.priority;
0134          catch
0135             prios(idx) = 0; % default
0136          end
0137 
0138          ww= whos('obj');
0139          sizes(idx) = ww.bytes;
0140 
0141          idx= idx+1;
0142       end
0143    end
0144 
0145    % sort from recent (high) to old (low)
0146    [times, idx] = sort(-times);
0147    times= -times;
0148    objid= objid(idx);
0149    sizes= sizes(idx);
0150    prios= prios(idx);
0151    % sort from high to low priority and recent (high) to old (low)
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 );

Generated on Tue 09-Aug-2011 11:38:31 by m2html © 2005