2 // Copyright (C) 2008 Francesco Salvestrini
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "misc/debug.h"
29 #include "misc/constant.h"
30 #include "misc/environment.h"
31 #include "misc/utility.h"
38 #define PROGRAM_NAME "beacon"
43 << PROGRAM_NAME
<< " (" << PACKAGE_NAME
<< ") " << PACKAGE_VERSION
<< std::endl
45 << "Copyright (C) 2008 Francesco Salvestrini" << std::endl
47 << "This is free software. You may redistribute copies of it under the terms of" << std::endl
48 << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>." << std::endl
49 << "There is NO WARRANTY, to the extent permitted by law." << std::endl
;
56 << "Usage: " << PROGRAM_NAME
<< " [OPTION]... "<< std::endl
58 << "Options: " << std::endl
59 << " -m, --max-mem maximum usable memory (in KB)" << std::endl
60 << " -j, --max-jobs maximum number of parallel jobs" << std::endl
61 << " -d, --debug enable debugging traces" << std::endl
62 << " -h, --help print this help, then exit" << std::endl
63 << " -V, --version print version number, then exit" << std::endl
65 << "Report bugs to <" << PACKAGE_BUGREPORT
<< ">" << std::endl
;
68 void hint(const std::string
& message
)
70 BUG_ON(message
.size() == 0);
73 << message
<< std::endl
74 << "Try `" << PROGRAM_NAME
<< " -h' for more information." << std::endl
;
77 int main(int argc
, char * argv
[])
79 TR_CONFIG_LVL(TR_LVL_DEFAULT
);
80 TR_CONFIG_PFX(PROGRAM_NAME
);
83 int max_mem
= 1; // XXX FIXME: Change to a costant
84 int max_jobs
= 1; // XXX FIXME: Change to a costant
87 //int digit_optind = 0;
89 //int this_option_optind = optind ? optind : 1;
92 static struct option long_options
[] = {
93 { "max-mem", 1, 0, 'm' },
94 { "max-jobs", 1, 0, 'j' },
95 { "debug", 0, 0, 'd' },
96 { "version", 0, 0, 'V' },
97 { "help", 0, 0, 'h' },
101 c
= getopt_long(argc
, argv
, "m:j:dVh",
102 long_options
, &option_index
);
109 max_mem
= atoi(optarg
);
111 hint("Wrong max memory value");
117 // The current solution is ugly.
118 // Add modifiers (KB, MB, GB) to
119 // this option in order to use
120 // different memory granularity.
122 max_mem
= max_mem
* 1024;
125 max_jobs
= atoi(optarg
);
127 hint("Wrong max jobs value");
132 TR_CONFIG_LVL(TR_LVL_DEBUG
);
141 hint("Unrecognized option");
150 // XXX FIXME: Add options related checks here
152 BUG_ON(max_mem
<= 0);
153 BUG_ON(max_jobs
<= 0);
155 size_t mem_for_vm
= max_mem
/ max_jobs
;
157 // Build configuration file path
158 std::string homedir
= Environment::get("HOME");
159 std::string confdir
=
163 std::string(PACKAGE_TARNAME
);
164 std::string conffile
=
167 std::string(PROGRAM_NAME
);
169 // Dump (acquired and derived) infos
170 TR_DBG("Max mem: '%d'\n", max_mem
);
171 TR_DBG("Max jobs '%d'\n", max_jobs
);
172 TR_DBG("Mem for VM '%d'\n", mem_for_vm
);
173 TR_DBG("Home directory: '%s'\n", homedir
.c_str());
174 TR_DBG("Configuration directory: '%s'\n", confdir
.c_str());
175 TR_DBG("Configuration file: '%s'\n", conffile
.c_str());
177 if (mem_for_vm
< MIN_MEM_FOR_VM
) {
178 TR_CRT("Memory for single VM is too small "
179 "(%d byte%s each, %d minimum)\n",
180 mem_for_vm
, PLURAL(mem_for_vm
), MIN_MEM_FOR_VM
);
184 TR_DBG("Building %d VM%s\n", max_jobs
, PLURAL(max_jobs
));
186 std::vector
<VM::VM
*> vms
;
187 vms
.resize(max_jobs
);
189 std::vector
<VM::VM
*>::iterator iter
;
190 for (iter
= vms
.begin(); iter
!= vms
.end(); iter
++) {
193 (*iter
) = new VM::Lua(mem_for_vm
);
196 TR_CRT("Cannot create VM\n");
199 TR_DBG("VM %p initialized\n", (*iter
));
202 size_t slack
= max_mem
- max_jobs
* mem_for_vm
;
203 TR_DBG("Memory slack is %d byte%s\n", slack
, PLURAL(slack
));
205 // Do the main loop here
207 std::string
t1("550e8400-e29b-41d4-a716-123456782122");
210 std::cout
<< t1
<< std::endl
;
214 t2
= std::string(uuid
);
216 std::cout
<< t2
<< std::endl
;
220 } catch (std::exception
& e
) {
221 TR_DBG("Got exception '%s'\n", e
.what());