00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00084 int alloc = randomizer().inclusive(1, 1 * MEGABYTE);
00085
00086 int parent = randomizer().inclusive(1, 120);
00087
00088
00089 if (!to_test.okay_allocation(parent, alloc))
00090 continue;
00091 wtc += mem_record(parent, alloc);
00092 allocations++;
00093 total_allocated += alloc;
00094 } else if (to_do < 88) {
00095
00096 if (!wtc.length()) continue;
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
00108 }
00109 }
00110
00111
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
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