Переглянути джерело

Some fixes:

* Adjust profiling app
* Actually set MDB map size
* Print DB error for low-level failures
* Account for NULL inserted pointer in LSUP_graph_add.
Stefano Cossu 3 роки тому
батько
коміт
a0c107e295
5 змінених файлів з 73 додано та 33 видалено
  1. 1 1
      Makefile
  2. 4 0
      include/triple.h
  3. 45 22
      profile.c
  4. 9 8
      src/graph.c
  5. 14 2
      src/store_mdb.c

+ 1 - 1
Makefile

@@ -45,7 +45,7 @@ memcheck: test valgrind
 
 profile:
 	$(CC) \
-		$(CFLAGS)
+		$(CFLAGS) \
 		$(INCLUDE) \
 		$(LIB) \
 		$(SRC) profile.c \

+ 4 - 0
include/triple.h

@@ -25,10 +25,14 @@ typedef enum {
 LSUP_Triple *
 LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
 
+#define TRP_DUMMY LSUP_triple_new (TERM_DUMMY, TERM_DUMMY, TERM_DUMMY)
+
 
 LSUP_SerTriple *
 LSUP_striple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
 
+#define STRP_DUMMY LSUP_striple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY)
+
 
 LSUP_Triple *
 LSUP_triple_new_from_striple (const LSUP_SerTriple *sspo);

+ 45 - 22
profile.c

@@ -2,49 +2,72 @@
 #include "graph.h"
 
 #ifndef NT
-#define NT 1000000
+#define NT 100000
 #endif
 
-static int test_graph_add_batch()
+static LSUP_Triple *
+generate_triples()
 {
-    size_t nt = NT;
-
     LSUP_Triple *trp;
-    CRITICAL(trp = malloc(nt * sizeof(LSUP_Triple)));
-    for (size_t i = 0; i < nt; i++) {
-        //printf("i: %lu\n", i);
-        LSUP_uri_new (NULL, trp[i].s);
-        LSUP_uri_new (NULL, trp[i].p);
-        LSUP_uri_new (NULL, trp[i].o);
+    CRITICAL(trp = malloc(NT * sizeof(LSUP_Triple)));
+    char sstr[32], pstr[32], ostr[32];
+
+    for (size_t i = 0; i < NT; i++) {
+        sprintf(sstr, "urn:s:%lu", i);
+        sprintf(pstr, "urn:p:%lu", i);
+        sprintf(ostr, "urn:o:%lu", i);
+        LSUP_triple_init(
+                trp + i, LSUP_uri_new (sstr),
+                LSUP_uri_new (pstr), LSUP_uri_new (ostr));
     }
     TRACE(STR, "Triples generated.");
 
-    LSUP_Graph *gr;
-    LSUP_graph_new(LSUP_STORE_MEM, &gr);
-    LSUP_graph_resize(gr, nt);
+    return trp;
+}
+
+static LSUP_rc
+insert_triples (LSUP_Triple *trp)
+{
+    LSUP_Graph *gr = LSUP_graph_new(LSUP_STORE_MDB_TMP);
 
-    LSUP_graph_add_trp(gr, trp, nt, NULL);
-    TRACE(STR, "Graph populated.");
+    LSUP_rc rc = LSUP_graph_add_trp(gr, trp, NT, NULL);
+    if (rc != LSUP_OK) printf ("Graph loading interrupted: %d.\n", rc);
+    else printf ("Graph populated.\n");
 
     LSUP_graph_free(gr);
 
-    return 0;
+    return rc;
 }
 
 
 int main()
 {
+    // Set env variable to test path.
+    putenv ("LSUP_MDB_STORE_PATH=" TMPDIR "/lsup_profile_mdb");
+    // Clear out database from previous test.
+    rm_r (getenv ("LSUP_MDB_STORE_PATH"));
+
     int rc;
-    clock_t start, end;
-    double wallclock;
+    clock_t start, tc1, tc2;
+    double wallclock, rate;
 
     start = clock();
-    rc = test_graph_add_batch();
-    end = clock();
-
-    wallclock = (end - start) / CLOCKS_PER_SEC;
+    LSUP_Triple *trp = generate_triples();
+    tc1 = clock();
+    wallclock = (tc1 - start) / CLOCKS_PER_SEC;
+    printf("Time elapsed: %lf s\n", wallclock);
 
+    printf("Inserting triples.\n");
+    rc = insert_triples (trp);
+    tc2 = clock();
+    wallclock = (tc2 - tc1) / CLOCKS_PER_SEC;
     printf("Time elapsed: %lf s\n", wallclock);
 
+    wallclock = (tc2 - start) / CLOCKS_PER_SEC;
+    rate = NT / wallclock;
+    printf(
+            "%d triples created and inserted in %lf s (%lf triples/s)\n",
+            NT, wallclock, rate);
+
     return rc;
 }

+ 9 - 8
src/graph.c

@@ -252,6 +252,8 @@ LSUP_graph_size (const Graph *gr)
     return LSUP_mdbstore_size (gr->mdb_store);
 }
 
+// TODO Add add_stream_init, add_stream_iter and add_stream_done for streaming
+// functions.
 
 LSUP_rc
 LSUP_graph_add(
@@ -261,7 +263,7 @@ LSUP_graph_add(
 {
     LSUP_rc rc;
 
-    *inserted = 0;
+    if (inserted) *inserted = 0;
 
     /*
      * NOTE It is possible to pass both sets of RDF triples and buffer triples.
@@ -290,7 +292,7 @@ LSUP_graph_add(
                 LSUP_rc db_rc = LSUP_htstore_add (gr->ht_store, &sspo);
                 if (LIKELY (db_rc == LSUP_OK)) {
                     rc = LSUP_OK;
-                    (*inserted) ++;
+                    if (inserted) (*inserted) ++;
                 }
 
                 LSUP_striple_done (&sspo);
@@ -306,7 +308,7 @@ LSUP_graph_add(
 
             if (LIKELY (db_rc == LSUP_OK)) {
                 rc = LSUP_OK;
-                (*inserted) ++;
+                if (inserted) (*inserted) ++;
             }
             if (UNLIKELY (db_rc < 0)) return db_rc;
         }
@@ -322,8 +324,7 @@ LSUP_graph_add(
 
         // Serialize and insert RDF triples.
         if (trp_ct > 0) {
-            LSUP_SerTriple *sspo = LSUP_striple_new(
-                    BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
+            LSUP_SerTriple *sspo = STRP_DUMMY;
 
             for (size_t i = 0; i < trp_ct; i++) {
                 TRACE ("Inserting triple #%lu\n", i);
@@ -333,7 +334,7 @@ LSUP_graph_add(
 
                 if (LIKELY (db_rc == LSUP_OK)) {
                     rc = LSUP_OK;
-                    (*inserted) ++;
+                    if (inserted) (*inserted) ++;
                 }
 
                 if (UNLIKELY (db_rc < 0)) return db_rc;
@@ -349,7 +350,7 @@ LSUP_graph_add(
 
             if (LIKELY (db_rc == LSUP_OK)) {
                 rc = LSUP_OK;
-                (*inserted) ++;
+                if (inserted) (*inserted) ++;
             }
             if (UNLIKELY (db_rc < 0)) return db_rc;
         }
@@ -430,7 +431,7 @@ LSUP_rc
 LSUP_graph_iter_next (GraphIterator *it, LSUP_Triple *spo)
 {
     LSUP_SerTriple *sspo = NULL;
-    if (spo) sspo = LSUP_striple_new(BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
+    if (spo) sspo = STRP_DUMMY;
 
     LSUP_rc rc = graph_iter_next_buffer (it, sspo);
 

+ 14 - 2
src/store_mdb.c

@@ -279,6 +279,8 @@ LSUP_mdbstore_new (const char *path, const LSUP_Buffer *default_ctx)
     char *env_mapsize = getenv ("LSUP_MDB_MAPSIZE");
     if (env_mapsize == NULL) mapsize = DEFAULT_MAPSIZE;
     else sscanf (env_mapsize, "%lu", &mapsize);
+    printf ("Setting environment map size to %lu bytes.\n", mapsize);
+    db_rc = mdb_env_set_mapsize (store->env, mapsize);
 
     db_rc = mdb_env_set_maxdbs (store->env, N_DB);
     if (UNLIKELY (db_rc != MDB_SUCCESS)) return NULL;
@@ -403,9 +405,11 @@ LSUP_mdbstore_add_iter (MDBIterator *it, const LSUP_SerTriple *sspo)
     for (int i = 0; i < 3; i++) {
         LSUP_Buffer *st = LSUP_striple_pos (sspo, i);
 
+#ifdef DEBUG
         printf ("Inserting term: ");
         LSUP_buffer_print (st);
         printf ("\n");
+#endif
 
         spok[i] = LSUP_sterm_to_key (st);
 
@@ -418,6 +422,9 @@ LSUP_mdbstore_add_iter (MDBIterator *it, const LSUP_SerTriple *sspo)
                 it->store->txn, it->store->dbi[IDX_T_ST],
                 &it->key, &it->data, MDB_NOOVERWRITE);
         if (db_rc != MDB_SUCCESS && db_rc != MDB_KEYEXIST) {
+            fprintf (
+                    stderr, "MDB error while inserting term: %s\n",
+                    mdb_strerror(db_rc));
             return LSUP_DB_ERR;
         }
     }
@@ -437,7 +444,12 @@ LSUP_mdbstore_add_iter (MDBIterator *it, const LSUP_SerTriple *sspo)
             &it->key, &it->data, MDB_NODUPDATA);
 
     if (db_rc == MDB_KEYEXIST) return LSUP_NOACTION;
-    if (db_rc != MDB_SUCCESS) return LSUP_DB_ERR;
+    if (db_rc != MDB_SUCCESS) {
+        fprintf (
+                stderr, "MDB error while inserting triple: %s\n",
+                mdb_strerror(db_rc));
+        return LSUP_DB_ERR;
+    }
 
     // Index.
     rc = index_triple (it->store, OP_ADD, spok, it->ck);
@@ -809,7 +821,7 @@ index_triple(
     LSUP_rc rc = LSUP_NOACTION;
     MDB_val v1, v2;
 
-    printf ("Indexing triple: %lx %lx %lx\n", spok[0], spok[1], spok[2]);
+    TRACE ("Indexing triple: %lx %lx %lx\n", spok[0], spok[1], spok[2]);
 
     // Index c:spo.
     if (op == OP_REMOVE) {