Branch: Tag:

1999-06-08

1999-06-08 15:10:54 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Nolonger uses simulate.

Rev: server/bin/garbagecollector.pike:1.15

1:   //#include <stdio.h> - #include <simulate.h> + //#include <simulate.h>      /*    * name = "Proxy Garbage Collector";    * doc = "This is the proxy garbage collector";    */    - string cvs_version = "$Id: garbagecollector.pike,v 1.14 1999/03/05 01:32:36 grubba Exp $"; + string cvs_version = "$Id: garbagecollector.pike,v 1.15 1999/06/08 15:10:54 grubba Exp $";      //#define DEBUG   
28:      int _num(string from)   { -  int c; -  sscanf(from[strlen(from)-8..], "%x", c); -  return c; +  return (int)("0x"+from[strlen(from)-8..]);   }      
46:    mapping log;       file = lp+"cachelog"+_order(num); -  if(!(s=read_bytes(file))) +  if(!(s=Stdio.read_bytes(file)))    {    mkdir(lp); -  s=read_bytes(file); +  s=Stdio.read_bytes(file);    }    if(!s) {    if(first_log == num)
61:       if(catch(log = decode_value( s ))) {   #ifdef DEBUG -  perror("Could not decode cachelog file ("+file+") - removed\n"); +  werror("Could not decode cachelog file ("+file+") - removed\n");   #endif    rm_log(num);    return 0;
81:    rm(file);    if(sizeof(log)<=0)    return; -  if(!write_file(file, encode_value(log))) -  perror("Could not write cachelog file ("+file+")\n"); +  if(!Stdio.write_file(file, encode_value(log))) +  werror("Could not write cachelog file ("+file+")\n");   }      #define BLOCK_SIZE 2048
101:    disk_i_max, disk_i_used, disk_i_avail, disk_i_capacity, disk_time;   string disk_name, disk_type;    - #define LOGGER(x) if(gc_log)gc_log->write(x);else perror(x) + #define LOGGER(x) if(gc_log)gc_log->write(x);else werror(x)   object gc_log;      string disk_info()
160:    string file, s;    file = lp+"cache_status";    mkdir(lp); -  if(file_size(file)<=0) { -  perror("read_cache_status: "+file+" is missing\n"); +  if(Stdio.file_size(file)<=0) { +  werror("read_cache_status: "+file+" is missing\n");    return 0;    } -  if(!(s=read_bytes(file))) { -  perror("read_cache_status: "+file+" could not be read\n"); +  if(!(s=Stdio.read_bytes(file))) { +  werror("read_cache_status: "+file+" could not be read\n");    rm(file);    return 0;    }    if(catch(status = decode_value(s))) { -  perror("read_cache_status: "+file+" could not be decoded\n"); +  werror("read_cache_status: "+file+" could not be decoded\n");    rm(file);    return 0;    }
184:    lastgc = status->lastgc;       if((last_log < first_log) || -  (max_cache_size>0&&cache_size <= 0)|| -  (max_num_files>0&&num_files <= 0)|| +  (cache_size <= 0)|| +  (num_files <= 0)||    (first_log <= 0)) { -  perror("read_cache_status: "+file+" contains rubbish\n"); +  werror("read_cache_status: "+file+" contains rubbish\n");    rm(file);    return 0;    }
205:       mkdir(lp);    -  allfiles = map_array(get_dir(lp), lambda(string s) { +  allfiles = Array.map(get_dir(lp), lambda(string s) {    if(search(s, "cachelog") != -1) return s;    return 0;    }) - ({ 0 });
237: Inside #if 0
   log = ([]);       if(last_log < 0) -  perror("CACHE: Failed to read existing logfile.\n"); +  werror("CACHE: Failed to read existing logfile.\n");   #endif   }   
256:    status->removed_files = removed_files;    status->lastgc = lastgc;    -  if(!write_file(file+"+", encode_value(status))) -  perror("write_cache_status: "+file+"+"+" could not be written\n"); +  if(!Stdio.write_file(file+"+", encode_value(status))) +  werror("write_cache_status: "+file+"+"+" could not be written\n");    if(!mv(file+"+", file)) -  perror("write_cache_status: "+file+" could not be written\n"); +  werror("write_cache_status: "+file+" could not be written\n");   }      void write_log()
270:    mkdir(lp);    file = lp+"cachelog"+_order(last_log);    rm(file); -  if (!write_file(file, encode_value(log))) -  perror("Could not write cachelog file ("+file+")\n"); +  if (!Stdio.write_file(file, encode_value(log))) +  werror("Could not write cachelog file ("+file+")\n");    log = ([]);    write_cache_status();   }      void update(string file, int tim, int|void score)   { - //perror(file+" "+(time(1)-tim)+" seconds old, "+score+" \"bonus\" seconds.\n"); + //werror(file+" "+(time(1)-tim)+" seconds old, "+score+" \"bonus\" seconds.\n");    if((search(file, ".done")!=-1)&&log[file-".done"]) {    m_delete(log, file-".done");    num_files--;
369:    first_log = last_log = 0; // Well, lets start again then.       foreach(dirs, dir) -  if(file_size(dir)<-1 && dir!="logs") +  if(Stdio.file_size(dir)<-1 && dir!="logs")    find_all_files_in(dir+"/");       last_log++; -  if((file_size(lp+"cachelog"+_order(1))>0) && +  if((Stdio.file_size(lp+"cachelog"+_order(1))>0) &&    !mv(lp+"cachelog"+_order(1), lp+"cachelog"+_order(last_log))) -  perror("find_all_files_and_log_it - mv failed\n"); +  werror("find_all_files_and_log_it - mv failed\n");       write_cache_status();    current_cache_message();
392:       write_log();    - // perror("Collect. first_log="+first_log+"; last_log="+last_log+"\n"); + // werror("Collect. first_log="+first_log+"; last_log="+last_log+"\n");    r = catch {    while((amnt>0)&&(first_log <= last_log))    {    mapping rl; - // perror("Collecting log "+i+"\n"); - // perror("Collect. first_log="+first_log+"; last_log="+last_log+"\n"); + // werror("Collecting log "+i+"\n"); + // werror("Collect. first_log="+first_log+"; last_log="+last_log+"\n");    if(rl = parse_log(first_log))    {    logsize = sizeof(rl);
410:    };    if(r)    { -  perror("Error while garbagecollecting: "+r[0]+"\n" +  werror("Error while garbagecollecting: "+r[0]+"\n"    +describe_backtrace(r[1]));    return;    }
437:    num_files--;       // len is in units of BLOCK_SIZE bytes. -  if((max_cache_size>0) && ((int)((float)cache_size)) > max_cache_size) +  if(((int)((float)cache_size)) > max_cache_size)    gc(cache_size);    else if((max_num_files>0) && (num_files > max_num_files))    gc(cache_normal_garb);
447:      void cache_stream_closed()   { -  perror("garbagecollector: cache command stream closed. Exiting\n"); +  werror("garbagecollector: cache command stream closed. Exiting\n");   #ifdef LPC_GARB    write_log();   #endif
459:    mixed res;    if(!arrayp(what))    { -  perror(sprintf("Got strange command (%O)\n", what)); +  werror(sprintf("Got strange command (%O)\n", what));    return 0;    }      #ifdef DEBUG - // perror(sprintf("Got command %O\n", what)); + // werror(sprintf("Got command %O\n", what));   #endif       return this_object()[what[0]](@what[1..]);
475:   {    cmd = _cache+cmd;    - // perror("Got some data: '"+cmd+"'\n"); + // werror("Got some data: '"+cmd+"'\n");       while(strlen(cmd))    {
483:       if(strlen(cmd) < 8) break; // More needed.    -  sscanf(cmd[..7]-" ", "%x", l); +  l = (int)("0x"+(cmd[..7]-" "));       if(strlen(cmd) < l+8) break; // More needed   
491:       array err;    if(err=catch(do_command( decode_value( cmd[..l-1] ) ))) -  stderr->write(describe_backtrace( err )); +  Stdio.stderr->write(describe_backtrace( err ));       cmd=cmd[l..]; // Remove the 'command' field of this command    } - // perror("data parsed ("+strlen(cmd)+" bytes in cache).\n"); + // werror("data parsed ("+strlen(cmd)+" bytes in cache).\n");       _cache=cmd;   }
506:   {    array s;   #ifdef DEBUG - // perror("remove one file? "+fname+" --- "); + // werror("remove one file? "+fname+" --- ");   #endif    s=stat_cache[fname];    if(!s)
550:       catch {   #ifdef DEBUG -  // perror("really_gc ("+(int)BLOCK_TO_KB(amnt)+" Kb)\n"); +  // werror("really_gc ("+(int)BLOCK_TO_KB(amnt)+" Kb)\n");   #endif    collect(amnt, remove_one_file);    write_cache_status();    current_cache_message();   #ifdef DEBUG -  // perror("--------- ("+(int)BLOCK_TO_KB(removed)+" Kb really removed)\n"); +  // werror("--------- ("+(int)BLOCK_TO_KB(removed)+" Kb really removed)\n");   #endif    };    stat_cache = ([]);
580:    ctime(garbage_time)-"\n");       rm("statistics"); -  write_file("statistics", +  Stdio.write_file("statistics",    sprintf("Cache(%s):\n"    "\t%1d files%s\n"    "\t%1.3f MB (%1.2f%%)\n"
592:    sprintf(" (%1.2f%%)",    (float)cache_size*100/max_cache_size):"",    ((float)BLOCK_TO_KB(cache_size))/(1024.0), -  max_cache_size>0?(float)cache_size*100/max_cache_size:0.0, +  (float)cache_size*100/max_cache_size,    gc_info, disk_info()));   }   
616:       gc_log = Stdio.File();    if(!gc_log->open(lf, "rwac")) { -  perror("init_log_file("+lf+"): open failed\n"); +  werror("init_log_file("+lf+"): open failed\n");    destruct(gc_log);    return;    }
677:    init_log_file(gc_lf);      #ifdef DEBUG -  perror("Initalizing cache, cache-dir is "+cdir+"\n"); +  werror("Initalizing cache, cache-dir is "+cdir+"\n");   #endif    cd(cdir);    cache_normal_garb = cng*(1048576/BLOCK_SIZE);
692:    if(last_log < 10)    find_all_files_and_log_it();    -  if(file_size(lp+"cachelog"+_order(1))>=0) { +  if(Stdio.file_size(lp+"cachelog"+_order(1))>=0) {    LOGGER("Found rechecking unfinished ...\n");    find_all_files_and_log_it();    }
710:      int main()   { -  object st = File("stdin"); -  st->set_id(stdin); +  object st = Stdio.File("stdin"); +  st->set_id(Stdio.stdin);    st->set_nonblocking( got_command, 0, cache_stream_closed );    return -1;   }