t_memory_limiter.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_memory_limiter                                               *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Tests that the memory_limiter is keeping track of the memory users       *
00009 *  accurately.                                                                *
00010 *                                                                             *
00011 *******************************************************************************
00012 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00013 * redistribute it and/or modify it under the terms of the GNU General Public  *
00014 * License as published by the Free Software Foundation; either version 2 of   *
00015 * the License or (at your option) any later version.  This is online at:      *
00016 *     http://www.fsf.org/copyleft/gpl.html                                    *
00017 * Please send any updates to: fred@gruntose.com                               *
00018 \*****************************************************************************/
00019 
00020 //#define DEBUG_MEMORY_LIMITER
00021   // uncomment for debugging version.
00022 
00023 #include <basis/chaos.h>
00024 #include <basis/guards.h>
00025 #include <basis/istring.h>
00026 #include <basis/set.cpp>
00027 #include <data_struct/memory_limiter.h>
00028 #include <data_struct/static_memory_gremlin.h>
00029 #include <mechanisms/time_stamp.h>
00030 #include <opsystem/application_shell.h>
00031 #include <opsystem/ini_config.h>
00032 
00033 #ifdef DEBUG_MEMORY_LIMITER
00034   #include <stdio.h>
00035 #endif
00036 
00037 HOOPLE_STARTUP_CODE;
00038 
00039 const int MAXIMUM_MEM_OVERALL = 1 * GIGABYTE;
00040 const int MAXIMUM_MEM_PER_OWNER = 20 * MEGABYTE;
00041 
00042 const int RUN_TIME = 5 * MINUTE_ms;
00043 
00045 
00046 class test_memory_limiter : public application_shell
00047 {
00048 public:
00049   test_memory_limiter() : application_shell(class_name()) {}
00050 
00051   IMPLEMENT_CLASS_NAME("test_memory_limiter");
00052 
00053   virtual int execute();
00054 };
00055 
00057 
00058 struct mem_record {
00059   int parent;
00060   int allocated;
00061 
00062   mem_record(int parent_in = 0, int allocated_in = 0)
00063       : parent(parent_in), allocated(allocated_in) {}
00064 };
00065 
00066 struct memorial : array<mem_record> {};
00067 
00069 
00070 int test_memory_limiter::execute()
00071 {
00072   time_stamp when_to_leave(RUN_TIME);
00073   time_stamp start;
00074   memorial wtc;
00075   memory_limiter to_test(MAXIMUM_MEM_OVERALL, MAXIMUM_MEM_PER_OWNER);
00076   int allocations = 0;
00077   int deletions = 0;
00078   u_int total_allocated = 0;
00079   u_int total_deleted = 0;
00080   while (time_stamp() < when_to_leave) {
00081     int to_do = randomizer().inclusive(1, 100);
00082     if (to_do < 50) {
00083       // add a new record.
00084       int alloc = randomizer().inclusive(1, 1 * MEGABYTE);
00085 //isolate min max alloc
00086       int parent = randomizer().inclusive(1, 120);
00087 //isolate min max parents
00088 
00089       if (!to_test.okay_allocation(parent, alloc))
00090         continue;  // no space right now.
00091       wtc += mem_record(parent, alloc);
00092       allocations++;
00093       total_allocated += alloc;
00094     } else if (to_do < 88) {
00095       // remove an existing record.
00096       if (!wtc.length()) continue;  // nothing to remove.
00097       int indy = randomizer().inclusive(0, wtc.length() - 1);
00098       mem_record to_gone = wtc[indy];
00099       wtc.zap(indy, indy);
00100       if (!to_test.record_deletion(to_gone.parent, to_gone.allocated)) {
00101         log(to_test.text_form());
00102         deadly_error(class_name(), "deletion check", "failed to record deletion!");
00103       }
00104       deletions++;
00105       total_deleted += to_gone.allocated;
00106     } else {
00107 //do something funky, like allocate part of one into another...
00108     }
00109   }
00110 
00111   // now clear everything left in our list.
00112   for (int i = 0; i < wtc.length(); i++) {
00113     mem_record to_gone = wtc[i];
00114     if (!to_test.record_deletion(to_gone.parent, to_gone.allocated)) {
00115       log(to_test.text_form());
00116       deadly_error(class_name(), "final cleaning", "failed to record deletion!");
00117     }
00118     deletions++;
00119     total_deleted += to_gone.allocated;
00120   }
00121 
00122   // now check that the memory limiter has returned to camber.
00123 
00124   if (to_test.overall_usage()) {
00125     log(to_test.text_form());
00126     deadly_error(class_name(), "final checks", "there is still memory in use!");
00127   }
00128 
00129   if (to_test.overall_space_left() != MAXIMUM_MEM_OVERALL) {
00130     log(to_test.text_form());
00131     deadly_error(class_name(), "final checks", "the free space is not correct!");
00132   }
00133 
00134   int_set remaining = to_test.individuals_listed();
00135   if (remaining.elements()) {
00136     log(to_test.text_form());
00137     deadly_error(class_name(), "final checks",
00138         "there were still uncleared individuals!");
00139   }
00140 
00141   time_stamp end;
00142 
00143   log("stats for this run:");
00144   log(istring(istring::SPRINTF, "\trun time %f ms",
00145       end.value() - start.value()));
00146   log(istring(istring::SPRINTF, "\tallocations %d, total memory allocated %d",
00147       allocations, total_allocated));
00148   log(istring(istring::SPRINTF, "\tdeletions %d, total memory deleted %d",
00149       deletions, total_deleted));
00150 
00151   guards::alert_message("memory_limiter:: works for those functions tested.");
00152   return 0;
00153 }
00154 
00156 
00157 int main(int formal(argc), char *formal(argv)[])
00158 {
00159   test_memory_limiter to_test;
00160   return to_test.execute();
00161 }
00162 

Generated on Fri Nov 21 04:30:08 2008 for HOOPLE Libraries by  doxygen 1.5.1