Fixed tracing setup
[distributed.git] / src / beacon.cxx
blob4f6af45d2b40108f604817895564927be5d95fe9
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 TR_CONFIG_LVL(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);
164 std::string conffile =
165 confdir +
166 std::string("/") +
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);
181 return 1;
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++) {
191 try {
192 #ifdef HAVE_LUA
193 (*iter) = new VM::Lua(mem_for_vm);
194 #endif
195 } catch (...) {
196 TR_CRT("Cannot create VM\n");
197 return 1;
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
206 try {
207 std::string t1("550e8400-e29b-41d4-a716-123456782122");
208 std::string t2;
210 std::cout << t1 << std::endl;
212 Net::UUID uuid(t1);
214 t2 = std::string(uuid);
216 std::cout << t2 << std::endl;
218 BUG_ON(t1 != t2);
220 } catch (std::exception & e) {
221 TR_DBG("Got exception '%s'\n", e.what());
222 throw e;
224 MISSING_CODE();
225 } catch (...) {
226 BUG();
229 return 0;