Rearranged beacon tracing
[distributed.git] / src / beacon.cxx
blob3c979d6aad4110c34d98e16bae30420413622e9c
1 //
2 // Copyright (C) 2008 Francesco Salvestrini
3 //
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.
8 //
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.
19 #include "config.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <cstdlib>
24 #include <string>
25 #include <vector>
26 #include <iostream>
28 #include "misc/debug.h"
29 #include "misc/constant.h"
30 #include "misc/environment.h"
31 #include "misc/utility.h"
32 #include "vm/vm.h"
33 #ifdef HAVE_LUA
34 #include "vm/lua.h"
35 #endif
36 #include "net/uuid.h"
38 #define PROGRAM_NAME "beacon"
40 void version(void)
42 std::cout
43 << PROGRAM_NAME << " (" << PACKAGE_NAME << ") " << PACKAGE_VERSION << std::endl
44 << std::endl
45 << "Copyright (C) 2008 Francesco Salvestrini" << std::endl
46 << 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;
53 void help(void)
55 std::cout
56 << "Usage: " << PROGRAM_NAME << " [OPTION]... "<< std::endl
57 << 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
64 << std::endl
65 << "Report bugs to <" << PACKAGE_BUGREPORT << ">" << std::endl;
68 void hint(const std::string & message)
70 BUG_ON(message.size() == 0);
72 std::cout
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);
82 try {
83 int max_mem = 1; // XXX FIXME: Change to a costant
84 int max_jobs = 1; // XXX FIXME: Change to a costant
86 int c;
87 //int digit_optind = 0;
88 while (1) {
89 //int this_option_optind = optind ? optind : 1;
90 int option_index = 0;
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' },
98 { 0, 0, 0, 0 }
101 c = getopt_long(argc, argv, "m:j:dVh",
102 long_options, &option_index);
103 if (c == -1) {
104 break;
107 switch (c) {
108 case 'm':
109 max_mem = atoi(optarg);
110 if (max_mem <= 0) {
111 hint("Wrong max memory value");
112 return 1;
116 // XXX FIXME:
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;
123 break;
124 case 'j':
125 max_jobs = atoi(optarg);
126 if (max_jobs <= 0) {
127 hint("Wrong max jobs value");
128 return 1;
130 break;
131 case 'd':
132 trace_level = TR_LVL_DEBUG;
133 break;
134 case 'V':
135 version();
136 return 0;
137 case 'h':
138 help();
139 return 0;
140 case '?':
141 hint("Unrecognized option");
142 return 1;
144 default:
145 BUG();
146 return 1;
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 =
160 homedir +
161 std::string("/") +
162 std::string(".") +
163 std::string(PACKAGE_TARNAME);
165 // Dump (acquired and derived) infos
166 TR_DBG("Max mem: '%d'\n", max_mem);
167 TR_DBG("Max jobs '%d'\n", max_jobs);
168 TR_DBG("Mem for VM '%d'\n", mem_for_vm);
169 TR_DBG("Home directory: '%s'\n", homedir.c_str());
170 TR_DBG("Configuration directory: '%s'\n", confdir.c_str());
172 if (mem_for_vm < MIN_MEM_FOR_VM) {
173 TR_CRT("Memory for single VM is too small "
174 "(%d byte%s each, %d minimum)\n",
175 mem_for_vm, PLURAL(mem_for_vm), MIN_MEM_FOR_VM);
176 return 1;
179 TR_DBG("Building %d VM%s\n", max_jobs, PLURAL(max_jobs));
181 std::vector<VM::VM *> vms;
182 vms.resize(max_jobs);
184 std::vector<VM::VM *>::iterator iter;
185 for (iter = vms.begin(); iter != vms.end(); iter++) {
186 try {
187 #ifdef HAVE_LUA
188 (*iter) = new VM::Lua(mem_for_vm);
189 #endif
190 } catch (...) {
191 TR_CRT("Cannot create VM\n");
192 return 1;
194 TR_DBG("VM %p initialized\n", (*iter));
197 size_t slack = max_mem - max_jobs * mem_for_vm;
198 TR_DBG("Memory slack is %d byte%s\n", slack, PLURAL(slack));
200 // Do the main loop here
201 try {
202 std::string t1("550e8400-e29b-41d4-a716-123456782122");
203 std::string t2;
205 std::cout << t1 << std::endl;
207 Net::UUID uuid(t1);
209 t2 = std::string(uuid);
211 std::cout << t2 << std::endl;
213 BUG_ON(t1 != t2);
215 } catch (std::exception & e) {
216 TR_DBG("Got exception '%s'\n", e.what());
217 throw e;
219 MISSING_CODE();
220 } catch (...) {
221 BUG();
224 return 0;