]> git.cworth.org Git - apitrace/commitdiff
Compress names better.
authorJosé Fonseca <jfonseca@vmware.com>
Thu, 25 Nov 2010 09:36:04 +0000 (09:36 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Thu, 25 Nov 2010 09:36:04 +0000 (09:36 +0000)
log.cpp
log.hpp
trace.py
trace_parser.hpp

diff --git a/log.cpp b/log.cpp
index 11d09934c5cb059b7a3cbfdc082f6ab1d84819c5..e373f698682ee90ad56d20a6d84b49c287efcbea 100644 (file)
--- a/log.cpp
+++ b/log.cpp
@@ -30,6 +30,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <map>
+
 #include <zlib.h>
 
 #include "os.hpp"
@@ -148,6 +150,22 @@ WriteString(const char *str) {
    Write(str, len);
 }
 
+typedef std::map<const char *, size_t> namemap;
+static namemap names;
+
+static inline void 
+WriteName(const char *name) {
+   namemap::iterator it = names.find(name);
+   if (it == names.end()) {
+       size_t name_id = names.size();
+       WriteUInt(name_id);
+       WriteString(name);
+       names[name] = name_id;
+   } else {
+       WriteUInt(it->second);
+   }
+}
+
 void Open(void) {
     if (!g_gzFile) {
         _Open("trace");
@@ -163,7 +181,7 @@ void BeginCall(const char *function) {
    OS::AcquireMutex();
    Open();
    ++reentrancy;
-   WriteString(function);
+   WriteName(function);
 }
 
 void EndCall(void) {
@@ -176,7 +194,7 @@ void EndCall(void) {
 void BeginArg(unsigned index, const char *name) {
    WriteByte(Trace::CALL_ARG);
    WriteUInt(index);
-   WriteString(name);
+   WriteName(name);
 }
 
 void BeginReturn(void) {
@@ -188,17 +206,13 @@ void BeginArray(size_t length) {
    WriteUInt(length);
 }
 
-void BeginStruct(const char *name) {
+void BeginStruct(size_t length) {
    WriteByte(Trace::TYPE_STRUCT);
-   (void)name;
-}
-
-void EndStruct(void) {
-   WriteString("");
+   WriteUInt(length);
 }
 
 void BeginMember(const char *name) {
-   WriteString(name);
+   WriteName(name);
 }
 
 void BeginBitmask(void) {
@@ -280,7 +294,7 @@ void LiteralBlob(const void *data, size_t size) {
 
 void LiteralNamedConstant(const char *name, long long value) {
    WriteByte(Trace::TYPE_CONST);
-   WriteString(name);
+   WriteName(name);
    LiteralSInt(value);
 }
 
diff --git a/log.hpp b/log.hpp
index b6fc2b35353a09f6cb8a769cfa929b42cf0966c9..c012d786ac4cbd6ffcfebcad62dfa779041bfd6a 100644 (file)
--- a/log.hpp
+++ b/log.hpp
@@ -46,8 +46,8 @@ namespace Log {
     inline void BeginElement(void) {}
     inline void EndElement(void) {}
 
-    void BeginStruct(const char *name);
-    void EndStruct(void);
+    void BeginStruct(size_t length);
+    inline void EndStruct(void) {}
 
     void BeginMember(const char *name);
     inline void EndMember(void) {}
index b50d0a6734a5bbf5272b4e97310a6547e70999a4..ab55555c5ec5d12e0225c140d603b9448ccde484 100644 (file)
--- a/trace.py
+++ b/trace.py
@@ -51,7 +51,7 @@ class DumpDeclarator(base.OnceVisitor):
         for type, name in struct.members:
             self.visit(type)
         print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr)
-        print '    Log::BeginStruct("%s");' % struct.name
+        print '    Log::BeginStruct(%u);' % len(struct.members)
         for type, name in struct.members:
             print '    Log::BeginMember("%s");' % (name,)
             dump_instance(type, 'value.%s' % (name,))
index 97dcf657b71f30ad132bbd21bb3cd6f448bb9a72..39fcfda92450158070bbe5d2e01626d4d85dbd6a 100644 (file)
@@ -30,6 +30,8 @@
 #include <cassert>
 
 #include <iostream>
+#include <map>
+#include <string>
 
 #include <zlib.h>
 
@@ -44,6 +46,10 @@ class Parser
 {
 protected:
    gzFile file;
+
+   typedef std::map<size_t, std::string> namemap;
+   namemap names;
+
 public:
    Parser() {
       file = NULL;
@@ -79,7 +85,7 @@ public:
 
    Call *parse_call(void) {
       Call *call = new Call;
-      call->name = read_string();
+      call->name = read_name();
       do {
          int c = read_byte();
          switch(c) {
@@ -104,7 +110,7 @@ public:
    
    void parse_arg(Call *call) {
       unsigned index = read_uint();
-      std::string name = read_string();
+      std::string name = read_name();
       Value *value = parse_value();
       if (index >= call->args.size()) {
           call->args.resize(index + 1);
@@ -176,7 +182,7 @@ public:
    }
    
    Value *parse_const() {
-      std::string name = read_string();
+      std::string name = read_name();
       Value *value = parse_value();
       return new Const(name, value);
    }
@@ -194,7 +200,7 @@ public:
             value |= read_uint();
             break;
          case Trace::TYPE_CONST:
-            read_string();
+            read_name();
             break;
          case Trace::TYPE_NULL:
             goto done;
@@ -227,13 +233,12 @@ done:
    }
    
    Value *parse_struct() {
-      std::string name;
+      size_t length = read_uint();
       /* XXX */
-      name = read_string();
-      while(name.length()) {
+      for (size_t i; i < length; ++i) {
+         std::string name = read_name();
          Value *value = parse_value();
          std::cout << "  " << name << " = " << value << "\n";
-         name = read_string();
       }
       return NULL;
    }
@@ -244,6 +249,18 @@ done:
       /* XXX */
       return new UInt(addr);
    }
+
+   std::string read_name(void) {
+       std::string name;
+       size_t id = read_uint();
+       if (id >= names.size()) {
+           name = read_string();
+           names[id] = name;
+           return name;
+       } else {
+           return names[id];
+       }
+   }
    
    std::string read_string(void) {
       size_t len = read_uint();