Small fix for time in loading.
[SXSI/XMLTree.git] / XMLTree.cpp
index 61f6a4d..dfda61a 100644 (file)
@@ -1,7 +1,64 @@
-\r
 #include "basics.h"\r
 #include <cstring>\r
+#include <sstream>\r
 #include "XMLTree.h"\r
+#include <sys/time.h>\r
+#include <time.h>\r
+#include <sys/stat.h> \r
+#include <unistd.h>\r
+\r
+static double tLoading = 0;\r
+\r
+static unsigned int cLoading = 0;\r
+static struct timeval tmpv1;\r
+static struct timeval tmpv2;\r
+static string mem1;\r
+static string mem2;\r
+\r
+void read_procmem(string& memstr) {\r
+  std::string buf;\r
+  pid_t pid = getpid();\r
+  std::stringstream path;\r
+  path <<  "/proc/" << pid << "/status";\r
+  std::ifstream infile;\r
+  infile.open (path.str().c_str(), std::ifstream::in);\r
+  while (infile.good()){\r
+    getline(infile,buf);\r
+    if (infile.eof()) {\r
+      memstr = "Could not read memory";\r
+      return;\r
+    };\r
+    int idx = buf.find("VmRSS");\r
+    if (idx == 0){\r
+      memstr = buf;\r
+      return;\r
+    };\r
+  };\r
+  memstr = "Could not read memory";\r
+  return;\r
+\r
+}\r
+\r
+#define STARTTIMER()   do {                                            \\r
+  gettimeofday(&tmpv1,NULL);                                           \\r
+  read_procmem(mem1);                                                  \\r
+  } while (0)                                                          \\r
+\r
+#define STOPTIMER(x)   do {                                            \\r
+    read_procmem(mem2);                                                        \\r
+    gettimeofday(&tmpv2,NULL);                                         \\r
+    (t##x) = ((tmpv2.tv_sec  - tmpv1.tv_sec) * 1000000.0 +             \\r
+                      (tmpv2.tv_usec  - tmpv1.tv_usec))/1000.0;        \\r
+    (c##x)= (c##x)+1;                                                  \\r
+  } while (0)\r
+\r
+#define PRINTTIME(s,x) do {                                            \\r
+    std::cerr << (s) << " : " << (t##x) << "ms" << std::endl;          \\r
+    std::cerr << "Mem use before: " << mem1 << std::endl;              \\r
+    std::cerr << "Mem use after: " << mem2 << std::endl;               \\r
+    std::cerr.flush();                                                 \\r
+  } while (0)                                                          \\r
+\r
 \r
 // functions to convert tag positions to the corresponding tree node and viceversa. \r
 // These are implemented in order to be able to change the tree and Tags representations, \r
@@ -26,7 +83,7 @@ inline int node2tagpos(treeNode x)
 \r
 \r
 XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,\r
-            TextCollection * const TC, vector<string> * const CT, bool dis_tc)\r
+            TextCollection * const TC, bool dis_tc)\r
 \r
  {\r
     // creates the data structure for the tree topology\r
@@ -58,7 +115,7 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
     \r
     Text = (TextCollection*) TC;\r
 \r
-    CachedText = (vector<string>*) CT;\r
+\r
     EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32);\r
     free(empty_texts_bmp);\r
     empty_texts_bmp = NULL;\r
@@ -139,18 +196,6 @@ void XMLTree::Save(int fd)
     \r
     // stores the texts   \r
     if (!disable_tc) {\r
-      int st = CachedText->size();\r
-\r
-      ufwrite(&st, sizeof(int),1,fp);\r
-      for (int i = 0; i< CachedText->size(); i++){\r
-       st = CachedText->at(i).size();\r
-\r
-       ufwrite(&st, sizeof(int),1,fp);\r
-\r
-       ufwrite(CachedText->at(i).c_str(),sizeof(char),1+CachedText->at(i).size(),fp);\r
-\r
-      };\r
-      \r
       Text->Save(fp);\r
     };\r
 \r
@@ -172,11 +217,14 @@ XMLTree *XMLTree::Load(int fd)
     fp = fdopen(fd, "r");\r
 \r
     XML_Tree = new XMLTree();\r
-\r
+    STARTTIMER();\r
     // Load the tree structure\r
     XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
 \r
     loadTree(XML_Tree->Par, fp); \r
+    STOPTIMER(Loading);\r
+    PRINTTIME("Loading parenthesis struct", Loading);\r
+    STARTTIMER();\r
 \r
     XML_Tree->TagName = new vector<string>();\r
     XML_Tree->tIdMap = new std::unordered_map<string,int>();\r
@@ -198,7 +246,9 @@ XMLTree *XMLTree::Load(int fd)
        XML_Tree->tIdMap->insert(std::make_pair(s,i));\r
        \r
     };\r
-\r
+    STOPTIMER(Loading);\r
+    PRINTTIME("Loading tag names struct", Loading);\r
+    STARTTIMER();\r
 \r
     // loads the tag structure\r
     XML_Tree->Tags = static_sequence::load(fp);\r
@@ -217,6 +267,9 @@ XMLTree *XMLTree::Load(int fd)
     delete [] seq;\r
     /// End ugly tests\r
     \r
+    STOPTIMER(Loading);\r
+    PRINTTIME("Loading tag struct", Loading);\r
+    STARTTIMER();\r
 \r
     // loads the flags\r
     \r
@@ -224,28 +277,22 @@ XMLTree *XMLTree::Load(int fd)
 \r
     XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
 \r
+\r
+    STOPTIMER(Loading);\r
+    PRINTTIME("Loading text bitvector struct", Loading);\r
+    STARTTIMER();\r
+\r
     // Not used  \r
     int sample_rate_text = 64;\r
     // loads the texts\r
     if (!XML_Tree->disable_tc){\r
-      XML_Tree->CachedText = new vector<string>;\r
-      int sst;\r
-      int st;\r
-      ufread(&sst, sizeof(int),1,fp);\r
-      \r
-      for (int i=0;i<sst;i++){\r
-       ufread(&st, sizeof(int),1,fp);\r
-       char* str = (char*) malloc(sizeof(char)*st+1);\r
-       ufread(str,sizeof(char),st+1,fp);\r
-       string cppstr = str;\r
-       XML_Tree->CachedText->push_back(cppstr);\r
-       free(str);\r
-\r
-      };\r
       XML_Tree->Text = TextCollection::Load(fp,sample_rate_text);\r
     }\r
     else XML_Tree->Text = NULL;\r
-    \r
+    STOPTIMER(Loading);\r
+    PRINTTIME("Loading TextCollection", Loading);\r
+    STARTTIMER();\r
+\r
     return XML_Tree;\r
  }\r
 \r
@@ -640,9 +687,10 @@ DocID XMLTree::MyText(treeNode x)
 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
 int XMLTree::TextXMLId(DocID d) \r
  {\r
-   int s = EBVector->select1(d+1);\r
+   NULLT_IF(d == NULLT);\r
+     int s = EBVector->select1(d+1);\r
    return rank_open(Par, s) + d + 1; // +1 because root has preorder 1\r
\r
+   \r
  }\r
 \r
 // NodeXMLId(x): returns the preorder of node x in the tree consisting \r
@@ -650,6 +698,7 @@ int XMLTree::TextXMLId(DocID d)
 // preorder 0;\r
 int XMLTree::NodeXMLId(treeNode x) \r
  {\r
+   NULLT_IF(x == NULLT);\r
    if (x == Root()) return 1; // root node has preorder 1\r
    return rank_open(Par, x) + EBVector->rank1(x-1);\r
  }\r