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.
29 #include "libs/misc/debug.h"
30 #include "libs/misc/constant.h"
31 #include "libs/misc/environment.h"
32 #include "libs/misc/utility.h"
33 #include "libs/conf/configuration.h"
34 #include "libs/vm/vm.h"
36 #include "libs/vm/lua.h"
38 #include "libs/net/uuid.h"
40 #define PROGRAM_NAME "beacon"
45 << PROGRAM_NAME
<< " (" << PACKAGE_NAME
<< ") " << PACKAGE_VERSION
<< std::endl
47 << "Copyright (C) 2008 Francesco Salvestrini" << std::endl
49 << "This is free software. You may redistribute copies of it under the terms of" << std::endl
50 << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>." << std::endl
51 << "There is NO WARRANTY, to the extent permitted by law." << std::endl
;
55 #define DEFAULT_MAX_MEM 16 * KB
56 #define DEFAULT_MAX_JOBS 8
61 << "Usage: " << PROGRAM_NAME
<< " [OPTION]... " << std::endl
63 << "Options: " << std::endl
64 << " -m, --max-mem maximum usable memory in KB (default " << DEFAULT_MAX_MEM
/ KB
<< " KB)" << std::endl
65 << " -j, --max-jobs maximum number of parallel jobs (default " << DEFAULT_MAX_JOBS
<< ")" << std::endl
66 << " -d, --debug enable debugging traces" << std::endl
67 << " -h, --help print this help, then exit" << std::endl
68 << " -V, --version print version number, then exit" << std::endl
70 << "Report bugs to <" << PACKAGE_BUGREPORT
<< ">" << std::endl
;
73 void hint(const std::string
& message
)
75 BUG_ON(message
.size() == 0);
78 << message
<< std::endl
79 << "Try `" << PROGRAM_NAME
<< " -h' for more information." << std::endl
;
82 int main(int argc
, char * argv
[])
84 TR_CONFIG_LVL(TR_LVL_DEFAULT
);
85 TR_CONFIG_PFX(PROGRAM_NAME
);
88 std::string conffile
= "";
90 int max_mem
= DEFAULT_MAX_MEM
;
91 bool max_mem_set
= false;
92 int max_jobs
= DEFAULT_MAX_JOBS
;
93 bool max_jobs_set
= false;
96 //int digit_optind = 0;
98 //int this_option_optind = optind ? optind : 1;
101 static struct option long_options
[] = {
102 { "config", 1, 0, 'c' },
103 { "max-mem", 1, 0, 'm' },
104 { "max-jobs", 1, 0, 'j' },
105 { "debug", 0, 0, 'd' },
106 { "verbose", 0, 0, 'v' },
107 { "version", 0, 0, 'V' },
108 { "help", 0, 0, 'h' },
112 c
= getopt_long(argc
, argv
, "c:m:j:dvVh",
113 long_options
, &option_index
);
125 // The current solution is ugly.
126 // Add modifiers (KB, MB, GB) to
127 // this option in order to use
128 // different memory granularity.
130 max_mem
= atoi(optarg
);
132 hint("Wrong max memory value");
135 max_mem
= max_mem
* 1024;
139 max_jobs
= atoi(optarg
);
141 hint("Wrong max jobs value");
147 TR_CONFIG_LVL(TR_LVL_DEBUG
);
150 TR_CONFIG_LVL(TR_LVL_VERBOSE
);
159 hint("Unrecognized option");
168 // Options related checks
169 BUG_ON(max_mem
<= 0);
170 BUG_ON(max_jobs
<= 0);
172 // Build configuration file path
173 if (conffile
.size() == 0) {
174 std::string homedir
= Environment::get("HOME");
175 std::string confdir
=
179 std::string(PACKAGE_TARNAME
);
183 std::string(PROGRAM_NAME
);
185 TR_DBG("Configuration file overridden\n");
188 BUG_ON(conffile
.size() == 0);
190 TR_DBG("Initial (configuration file) values:\n");
191 TR_DBG(" Max mem: '%d'\n", max_mem
);
192 TR_DBG(" Max jobs: '%d'\n", max_jobs
);
194 // Read configuration file (if available)
196 TR_DBG("Reading configuration file from '%s'\n",
199 Configuration::File config
;
200 std::ifstream
instream(conffile
.c_str());
205 if (config
.get
<int>(conf_max_mem
, "max-mem")) {
206 TR_DBG("Found 'max-mem' key, value '%d'\n",
209 // Check gathered configuration
210 if (conf_max_mem
< 0) {
211 TR_ERR("Wrong max-mem value in "
212 "configuration file");
217 TR_DBG("Updating 'max-mem' key\n");
219 // Configuration value not specified
220 // in command line ...
221 max_mem
= conf_max_mem
;
226 if (config
.get
<int>(conf_max_jobs
, "max-jobs")) {
227 TR_DBG("Found 'max-jobs' key, value '%d'\n",
230 // Check gathered configuration
231 if (conf_max_jobs
< 0) {
232 TR_ERR("Wrong max-jobs value in "
233 "configuration file");
238 TR_DBG("Updating 'max-jobs' key\n");
240 // Configuration value not specified
241 // in command line ...
242 max_jobs
= conf_max_jobs
;
245 } catch (std::exception
& e
) {
246 TR_ERR("%s\n", e
.what());
251 // Options related checks
252 BUG_ON(max_mem
<= 0);
253 BUG_ON(max_jobs
<= 0);
255 TR_DBG("Final (configuration file) values:\n");
256 TR_DBG(" Max mem: '%d'\n", max_mem
);
257 TR_DBG(" Max jobs: '%d'\n", max_jobs
);
259 size_t mem_for_vm
= max_mem
/ max_jobs
;
261 // Dump (acquired and derived) infos
262 TR_DBG("Max mem: '%d'\n", max_mem
);
263 TR_DBG("Max jobs '%d'\n", max_jobs
);
264 TR_DBG("Mem for VM '%d'\n", mem_for_vm
);
265 TR_DBG("Configuration file: '%s'\n", conffile
.c_str());
267 if (mem_for_vm
< MIN_MEM_FOR_VM
) {
268 TR_CRT("Memory for single VM is too small "
269 "(%d byte%s each, %d minimum)\n",
270 mem_for_vm
, PLURAL(mem_for_vm
), MIN_MEM_FOR_VM
);
274 TR_DBG("Building %d VM%s\n", max_jobs
, PLURAL(max_jobs
));
276 std::vector
<VM::VM
*> vms
;
277 vms
.resize(max_jobs
);
279 std::vector
<VM::VM
*>::iterator iter
;
280 for (iter
= vms
.begin(); iter
!= vms
.end(); iter
++) {
283 (*iter
) = new VM::Lua(mem_for_vm
);
286 TR_CRT("Cannot create VM\n");
289 TR_DBG("VM %p initialized\n", (*iter
));
292 size_t slack
= max_mem
- max_jobs
* mem_for_vm
;
293 TR_DBG("Memory slack is %d byte%s\n", slack
, PLURAL(slack
));
295 // Do the main loop here
297 std::string
t0("7d444840-9dc0-11d1-b245-5ffdce74fad2");
298 std::string
t1("e902893a-9d22-3c7e-a7b8-d6e313b71d9f");
301 std::cout
<< t1
<< std::endl
;
305 t2
= std::string(uuid0
);
309 t2
= std::string(uuid1
);
311 std::cout
<< t2
<< std::endl
;
315 for (size_t i
= 0; i
< 5; i
++) {
316 Net::UUID
uuidRand(Net::UUID::IETF_RFC4122_RANDOM
);
318 t2
= std::string(uuidRand
);
321 << "--- RANDOM #" << i
322 << ":" << t2
<< std::endl
;
325 Net::UUID
uuidTB(Net::UUID::UNKNOWN
/*IETF_RFC4122_TIMEBASED*/);
327 } catch (Net::UUID::invalid_version
& e
) {
328 TR_ERR("Got Net::UUID::invalid_version "
329 "exception 'Version 0x%x %s'\n",
333 } catch (Net::UUID::formatting_problem
& e
) {
334 TR_ERR("Got Net::UUID::formatting_problem "
335 "exception 'Version 0x%x %s %d'\n",
340 } catch (Net::UUID::invalid_hexfield
& e
) {
341 TR_ERR("Got Net::UUID::invalid_hexfield "
345 } catch (Net::UUID::generic_problem
& e
) {
346 TR_ERR("Got Net::UUID::generic_problem "
350 } catch (std::exception
& e
) {
351 TR_ERR("Got exception '%s'\n", e
.what());