1/*
2 * Copyright (c) 2007, 2008, 2009, 2011, 2016, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetsstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10
11#include <stdio.h>
12#include <unistd.h>
13
14#include <barrelfish/barrelfish.h>
15#include <vfs/vfs.h>
16#include <sys/types.h>
17#include <eclipse.h>
18#include <include/skb_server.h>
19#include <include/skb_debug.h>
20
21#include <octopus_server/init.h>
22
23#include <bench/bench.h>
24
25#include "octopus/predicates.h"
26#include "shared_lib_dict.h"
27
28#define MEMORY_SIZE 32*1024*1024
29#define ECLIPSE_DIR "/skb"
30
31#define RESULT_BUF_SIZE 1024
32
33int skb_init(void);
34void execute_string(char *string);
35
36/* XXX: used dlmalloc for benchmarking octopus */
37#include <dmalloc/dmalloc.h>
38typedef void *(*alt_malloc_t)(size_t bytes);
39extern alt_malloc_t alt_malloc;
40typedef void (*alt_free_t)(void *p);
41extern alt_free_t alt_free;
42typedef void *(*alt_realloc_t)(void *p, size_t bytes);
43extern alt_realloc_t alt_realloc;
44//static void init_dmalloc(void)
45//{
46//    alt_malloc = &dlmalloc;
47//    alt_free = &dlfree;
48//    alt_realloc = &dlrealloc;
49//}
50
51int main(int argc, char**argv)
52{
53    errval_t err;
54    vfs_init();
55//    init_dmalloc();
56    // we'll be needing this...
57    vfs_mkdir("/tmp");
58    chdir(ECLIPSE_DIR);
59
60    // make sure, that dlsym has the right table to the statically compiled-in
61    // shared libraries...
62    dlopen_set_params(funcs, sizeof(funcs) / sizeof(*funcs));
63
64    // now set the right values for the eclipse-clp engine
65    ec_set_option_int(EC_OPTION_IO, MEMORY_IO);
66    ec_set_option_ptr(EC_OPTION_ECLIPSEDIR, ECLIPSE_DIR);
67    ec_set_option_long(EC_OPTION_GLOBALSIZE, MEMORY_SIZE);
68    //ec_set_option_long(EC_OPTION_PRIVATESIZE, MEMORY_SIZE);
69
70
71    struct skb_query_state* sqs = malloc(sizeof(struct skb_query_state));
72
73    // ec_.m.vm_flags |= 8;
74    SKB_DEBUG("before ec init\n");
75    int n = ec_init();
76    if (n != 0) {
77        SKB_DEBUG("skb_main: ec_init() failed. Return code = %d\n", n);
78    } else {
79        SKB_DEBUG("skb_main: ec_init() succeeded.\n");
80    }
81    err = execute_query("set_flag(print_depth,100).", sqs);
82    if (err_is_fail(err)) {
83        USER_PANIC_ERR(err, "skb failed.");
84    }
85
86    if(disp_get_core_id() == 0) {
87        //debug_printf("oct_server_init\n");
88        //execute_string("set_flag(gc, off).");
89        //execute_string("set_flag(gc_policy, fixed).");
90        //execute_string("set_flat(gc_interval, 536870912)."); // 512 mb
91        //execute_string("set_flag(gc_interval_dict, 10000).");
92        //execute_string("set_flag(enable_interrupts, off).");
93        //execute_string("set_flag(debug_compile, off).");
94        //execute_string("set_flag(debugging, nodebug).");
95        //bench_init();
96
97        // octopus related stuff
98        err = execute_query("[objects3].", sqs);
99        if (err_is_fail(err)) {
100            USER_PANIC_ERR(err, "skb failed.");
101        }
102        err = execute_query("[pubsub3].", sqs);
103        if (err_is_fail(err)) {
104            USER_PANIC_ERR(err, "skb failed.");
105        }
106        err = execute_query("[bindings].", sqs);
107        if (err_is_fail(err)) {
108            USER_PANIC_ERR(err, "skb failed.");
109        }
110        dident e = ec_did("eclipse", 0);
111        //ec_external(ec_did("notify_client", 2), p_notify_client, e);
112        ec_external(ec_did("trigger_watch", 6), p_trigger_watch, e);
113        ec_external(ec_did("save_index", 3), p_save_index, e);
114        ec_external(ec_did("remove_index", 3), p_remove_index, e);
115        ec_external(ec_did("index_intersect", 4), p_index_intersect, e);
116        ec_external(ec_did("bitfield_add", 3), p_bitfield_add, e);
117        ec_external(ec_did("bitfield_remove", 3), p_bitfield_remove, e);
118        ec_external(ec_did("bitfield_union", 4), p_bitfield_union, e);
119        ec_external(ec_did("match", 3), (int (*)()) ec_regmatch, e);
120        ec_external(ec_did("split", 4), (int (*)()) ec_regsplit, e);
121        // end
122
123        errval_t err = oct_server_init();
124        assert(err_is_ok(err));
125    }
126    if (disp_get_core_id() == 0) {
127        skb_server_init();
128        SKB_DEBUG("skb initialized\n");
129    }
130
131    // SKB Hardware related
132    err = execute_query("[queries].", sqs);
133    if (err_is_fail(err)) {
134        USER_PANIC_ERR(err, "skb failed.");
135    }
136    // execute_string("get_local_affinity(1,B,L),write(output,[B,L]).");
137    // execute_string("lib(branch_and_bound).");
138    // execute_string("minimize(member(X,[4,1,2]),X),write(output,X).");
139
140    free(sqs);
141    sqs = NULL;
142
143    messages_handler_loop();
144}
145
146
147int skb_init(void)
148{
149    int n;
150
151    SKB_DEBUG("initialize eclipse\n");
152    n = ec_init();
153
154    if (n != 0) {
155        SKB_DEBUG("skb_main: ec_init() failed.");
156    }
157    return (0);
158}
159
160
161void execute_string(char *string)
162{
163    char    buf[RESULT_BUF_SIZE];
164    int n;
165
166    ec_post_string(string);
167    int res = 7; //means that we have to flush the output.
168    while (res == 7) {
169        res = ec_resume();
170        if (res && (res != 7)) {
171            SKB_DEBUG("res = %d\n", res);
172        }
173
174        //give back the result and the error messages.
175        //in case there is no message, a '.' is still returned
176        n = ec_queue_read(1, buf, RESULT_BUF_SIZE);
177        if ((n >=0) && (n < RESULT_BUF_SIZE)) {
178            buf[n] = 0;
179        }
180        if (n) {
181            SKB_DEBUG("eclipse returned: %.*s",  n, buf);
182        }
183
184        n = ec_queue_read(2, buf, RESULT_BUF_SIZE);
185        if ((n >=0) && (n < RESULT_BUF_SIZE)) {
186            buf[n] = 0;
187        }
188        if (n) {
189            SKB_DEBUG("eclipse error returned:  %.*s",  n, buf);
190        }
191    }
192}
193