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    bench_init();
56
57//    init_dmalloc();
58    // we'll be needing this...
59    vfs_mkdir("/tmp");
60    chdir(ECLIPSE_DIR);
61
62    // make sure, that dlsym has the right table to the statically compiled-in
63    // shared libraries...
64    dlopen_set_params(funcs, sizeof(funcs) / sizeof(*funcs));
65
66    // now set the right values for the eclipse-clp engine
67    ec_set_option_int(EC_OPTION_IO, MEMORY_IO);
68    ec_set_option_ptr(EC_OPTION_ECLIPSEDIR, ECLIPSE_DIR);
69    ec_set_option_long(EC_OPTION_GLOBALSIZE, MEMORY_SIZE);
70    //ec_set_option_long(EC_OPTION_PRIVATESIZE, MEMORY_SIZE);
71
72
73    struct skb_query_state* sqs = malloc(sizeof(struct skb_query_state));
74
75    // ec_.m.vm_flags |= 8;
76    SKB_DEBUG("before ec init\n");
77    int n = ec_init();
78    if (n != 0) {
79        SKB_DEBUG("skb_main: ec_init() failed. Return code = %d\n", n);
80    } else {
81        SKB_DEBUG("skb_main: ec_init() succeeded.\n");
82    }
83    err = execute_query("set_flag(print_depth,100).", sqs);
84    if (err_is_fail(err)) {
85        USER_PANIC_ERR(err, "skb failed.");
86    }
87
88    if(disp_get_core_id() == 0) {
89        //debug_printf("oct_server_init\n");
90        //execute_string("set_flag(gc, off).");
91        //execute_string("set_flag(gc_policy, fixed).");
92        //execute_string("set_flat(gc_interval, 536870912)."); // 512 mb
93        //execute_string("set_flag(gc_interval_dict, 10000).");
94        //execute_string("set_flag(enable_interrupts, off).");
95        //execute_string("set_flag(debug_compile, off).");
96        //execute_string("set_flag(debugging, nodebug).");
97        //bench_init();
98
99        // octopus related stuff
100        err = execute_query("[objects3].", sqs);
101        if (err_is_fail(err)) {
102            USER_PANIC_ERR(err, "skb failed.");
103        }
104        err = execute_query("[pubsub3].", sqs);
105        if (err_is_fail(err)) {
106            USER_PANIC_ERR(err, "skb failed.");
107        }
108        err = execute_query("[bindings].", sqs);
109        if (err_is_fail(err)) {
110            USER_PANIC_ERR(err, "skb failed.");
111        }
112        dident e = ec_did("eclipse", 0);
113        //ec_external(ec_did("notify_client", 2), p_notify_client, e);
114        ec_external(ec_did("trigger_watch", 6), p_trigger_watch, e);
115        ec_external(ec_did("save_index", 3), p_save_index, e);
116        ec_external(ec_did("remove_index", 3), p_remove_index, e);
117        ec_external(ec_did("index_intersect", 4), p_index_intersect, e);
118        ec_external(ec_did("bitfield_add", 3), p_bitfield_add, e);
119        ec_external(ec_did("bitfield_remove", 3), p_bitfield_remove, e);
120        ec_external(ec_did("bitfield_union", 4), p_bitfield_union, e);
121        ec_external(ec_did("match", 3), (int (*)()) ec_regmatch, e);
122        ec_external(ec_did("split", 4), (int (*)()) ec_regsplit, e);
123        // end
124
125        errval_t err = oct_server_init();
126        assert(err_is_ok(err));
127    }
128    if (disp_get_core_id() == 0) {
129        skb_server_init();
130        SKB_DEBUG("skb initialized\n");
131    }
132
133    // SKB Hardware related
134    err = execute_query("[queries].", sqs);
135    if (err_is_fail(err)) {
136        USER_PANIC_ERR(err, "skb failed.");
137    }
138    // execute_string("get_local_affinity(1,B,L),write(output,[B,L]).");
139    // execute_string("lib(branch_and_bound).");
140    // execute_string("minimize(member(X,[4,1,2]),X),write(output,X).");
141
142    free(sqs);
143    sqs = NULL;
144
145    messages_handler_loop();
146}
147
148
149int skb_init(void)
150{
151    int n;
152
153    SKB_DEBUG("initialize eclipse\n");
154    n = ec_init();
155
156    if (n != 0) {
157        SKB_DEBUG("skb_main: ec_init() failed.");
158    }
159    return (0);
160}
161
162
163void execute_string(char *string)
164{
165    char    buf[RESULT_BUF_SIZE];
166    int n;
167
168    ec_post_string(string);
169    int res = 7; //means that we have to flush the output.
170    while (res == 7) {
171        res = ec_resume();
172        if (res && (res != 7)) {
173            SKB_DEBUG("res = %d\n", res);
174        }
175
176        //give back the result and the error messages.
177        //in case there is no message, a '.' is still returned
178        n = ec_queue_read(1, buf, RESULT_BUF_SIZE);
179        if ((n >=0) && (n < RESULT_BUF_SIZE)) {
180            buf[n] = 0;
181        }
182        if (n) {
183            SKB_DEBUG("eclipse returned: %.*s",  n, buf);
184        }
185
186        n = ec_queue_read(2, buf, RESULT_BUF_SIZE);
187        if ((n >=0) && (n < RESULT_BUF_SIZE)) {
188            buf[n] = 0;
189        }
190        if (n) {
191            SKB_DEBUG("eclipse error returned:  %.*s",  n, buf);
192        }
193    }
194}
195