feisty meow concerns codebase  2.140
memory_hog.cpp
Go to the documentation of this file.
1 // Name : memory_hog
3 // Author : Chris Koeritz
5 // Copyright (c) 2012-$now By Author. This program is free software; you can
6 // redistribute it and/or modify it under the terms of the GNU General Public
7 // License as published by the Free Software Foundation:
8 // http://www.gnu.org/licenses/gpl.html
9 // or under the terms of the GNU Library license:
10 // http://www.gnu.org/licenses/lgpl.html
11 // at your preference. Those licenses describe your legal rights to this
12 // software, and no other rights or warranties apply.
13 // Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
15 
17 
26 #include <basis/enhance_cpp.h>
29 #include <unit_test/unit_base.h>
30 
31 using namespace application;
32 using namespace basis;
33 using namespace loggers;
34 //using namespace processes;
35 using namespace structures;
36 using namespace unit_test;
37 
39  // this many milliseconds elapses between checks on shutdown conditions.
40 
41 #define BASE_LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
42 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
43  // define a macro that will send diagnostic output to the app's logger.
44 
46 
47 class application_example : virtual public unit_base, virtual public application_shell
48 {
49 public:
50  application_example();
51  ~application_example();
52 
53  DEFINE_CLASS_NAME("application_example");
54 
55  virtual void handle_timer();
57 
58  virtual void handle_startup();
60 
61  virtual void handle_shutdown();
63 
64  virtual int execute();
66 
67  int print_instructions();
69 
70 private:
71  singleton_application _app_lock;
72 };
73 
75 
76 application_example::application_example()
78  _app_lock(static_class_name())
79 {}
80 
81 application_example::~application_example()
82 {}
83 
85 {
86  FUNCDEF("print_instructions");
87  BASE_LOG("\
88 \nUsage: memory_hog {memorySize}\n\
89 \n\
90  This application will consume a requested amount of memory until either\n\
91 (1) the applications is interrupted or terminated, or (2) the application\n\
92 is no longer given memory when it requests it (whether due to account limits\n\
93 or to a lack of remaining allocatable memory), or (3) the requested amount\n\
94 has been allocated.\n\
95  The application will then sit back idly and occasionally riffle through its\n\
96 big bag of memory in an attempt to keep most of it in physical memory rather\n\
97 than virtual memory.\n\
98  This is not a hostile act unless used unwisely; this program is intended\n\
99 to get an unloaded machine into a predictable memory state.\n\
100  **DO NOT use this program without system administrator permission.**"
101 );
102  return 1;
103 }
104 
105 void application_example::handle_startup()
106 {
107  FUNCDEF("handle_startup");
108  LOG("starting up now.");
109 }
110 
111 void application_example::handle_shutdown()
112 {
113  FUNCDEF("handle_shutdown");
114  LOG("shutting down now.");
115 }
116 
117 void application_example::handle_timer()
118 {
119  FUNCDEF("handle_timer");
120  LOG("timer blip.");
121 }
122 
123 int application_example::execute()
124 {
125  FUNCDEF("execute");
127 
128 LOG(a_sprintf("cmd line has %d entries", cmds.entries()));
129 
130  if (cmds.entries() < 1) {
131  BASE_LOG("You need to provide a number on the command line....");
132  return print_instructions();
133  }
134 
135  astring size_as_text = cmds.get(0).text();
136 
137 LOG(a_sprintf("hoo hah! got a text thingy of: %s", size_as_text.s()));
138 
139  if (size_as_text.find('.') < 0) {
140  // we don't see this as floating point, but we will move on as if it's
141  // an integer and can become floating point.
142  size_as_text += ".0";
143  }
144 
145  double how_fat = size_as_text.convert(double(0.0));
146 
147 LOG(a_sprintf("got a number from user of: %f", how_fat));
148 
149 //okay, now that we've got that handled,
150 // we want to have a hash table of byte arrays.
151 // int hash or whatever? we want it indexable and fast access on a unique id.
152 // that will be our consumption tactic.
153 // we will allocate some chunk size in the byte arrays. maybe max of a meg.
154 // then we just add as many byte arrays as we need to to get to the requested amount.
155 // don't just allocate them all at the same size; vary it a bunch, like say 20% of
156 // our maximum allotance.
157 // so randomly pull memory until we get what we want.
158 // if we get a memory failure, slack off until we can try to get more.
159 // if we cannot get memory within certain number of failures, report this and bail out.
160 // we do not want to crash the machine.
161 // once we've got our memory, start playing with it.
162 // every so often,
163 // pick a random number of items to play with,
164 // go to each item (which is a byte array),
165 // pick a random segment of the byte array to look at,
166 // read the contents of the memory there. that's it, nothing stressful.
167 // repeat the memory play until forever.
168 // enhancement, allow them to provide a run time. time out after that elapses.
169 
170  return 0;
171 }
172 
174 
175 HOOPLE_MAIN(application_example, )
176 
int print_instructions(bool good, const astring &program_name)
Definition: checker.cpp:45
The application_shell is a base object for console programs.
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Definition: astring.h:113
virtual char get(int index) const
a constant peek at the string's internals at the specified index.
Definition: astring.cpp:138
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:757
int find(char to_find, int position=0, bool reverse=false) const
Locates "to_find" in "this".
Definition: astring.cpp:574
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
#define BASE_LOG(to_print)
Definition: memory_hog.cpp:41
#define LOG(to_print)
Definition: memory_hog.cpp:42
const int MEMORY_CHECKING_INTERVAL
Definition: memory_hog.cpp:38
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
const int SECOND_ms
Number of milliseconds in a second.
Definition: definitions.h:120
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define static_class_name()