diff mbox

[for-1.5,2/2] qom: add casting statistics

Message ID 1368477106-7579-2-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori May 13, 2013, 8:31 p.m. UTC
This is not meant to be applied but included as a reference.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 qom/object.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
diff mbox

Patch

diff --git a/qom/object.c b/qom/object.c
index ec88231..2bfdcda 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -136,12 +136,33 @@  TypeImpl *type_register_static(const TypeInfo *info)
     return type_register(info);
 }
 
+#ifdef CONFIG_QOM_CAST_STATS
+typedef struct CastStats
+{
+    uint64_t total;
+    uint64_t miss;
+    uint64_t hit;
+    uint64_t rebalance;
+    uint64_t lookups;
+    uint64_t class;
+} CastStats;
+
+#define CAST_STATS_DUMP_INTERVAL 1000000
+
+static CastStats cast_stats;
+static qemu_timeval cast_stats_ts;
+#endif
+
 static TypeImpl *type_get_by_name(const char *name)
 {
     if (name == NULL) {
         return NULL;
     }
 
+#ifdef CONFIG_QOM_CAST_STATS
+    cast_stats.lookups++;
+#endif
+
     return type_table_lookup(name);
 }
 
@@ -432,6 +453,43 @@  Object *object_dynamic_cast(Object *obj, const char *typename)
     return NULL;
 }
 
+#ifdef CONFIG_QOM_CAST_STATS
+static void cast_stats_dump(void)
+{
+    qemu_timeval ts;
+    uint64_t delta;
+    int64_t udelta;
+
+    qemu_gettimeofday(&ts);
+
+    delta = ts.tv_sec - cast_stats_ts.tv_sec;
+    udelta = ts.tv_usec - cast_stats_ts.tv_usec;
+    if (udelta < 0) {
+        delta += 1;
+        udelta += 1000000;
+    }
+
+    delta *= 1000000;
+    delta += udelta;
+
+    printf("casts/sec: %f\n", (double)(cast_stats.total * 1000000) / delta);
+    printf("hit/sec: %f\n", (double)(cast_stats.hit * 1000000) / delta);
+    printf("miss/sec: %f\n", (double)(cast_stats.miss * 1000000) / delta);
+    printf("rebalance/sec: %f\n", (double)(cast_stats.rebalance * 1000000) / delta);
+    printf("classes/sec: %f\n", (double)(cast_stats.class * 1000000) / delta);
+    printf("lookups/sec: %f\n", (double)(cast_stats.lookups * 1000000) / delta);
+    printf("\n");
+
+    cast_stats.total = 1;
+    cast_stats.lookups = 0;
+    cast_stats.miss = 0;
+    cast_stats.hit = 0;
+    cast_stats.rebalance = 0;
+    cast_stats.class = 0;
+    cast_stats_ts = ts;
+}
+#endif
+
 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
                                    const char *file, int line, const char *func)
 {
@@ -442,8 +500,17 @@  Object *object_dynamic_cast_assert(Object *obj, const char *typename,
     int i;
     Object *inst;
 
+#ifdef CONFIG_QOM_CAST_STATS
+    if (cast_stats.total == 0) {
+        qemu_gettimeofday(&cast_stats_ts);
+    }
+#endif
+
     for (i = 0; i < OBJECT_CLASS_CAST_CACHE; i++) {
         if (obj->class->cast_cache[i] == typename) {
+#ifdef CONFIG_QOM_CAST_STATS
+            cast_stats.hit++;
+#endif
             goto out;
         }
     }
@@ -463,9 +530,19 @@  Object *object_dynamic_cast_assert(Object *obj, const char *typename,
             obj->class->cast_cache[i - 1] = obj->class->cast_cache[i];
         }
         obj->class->cast_cache[i - 1] = typename;
+#ifdef CONFIG_QOM_CAST_STATS
+        cast_stats.miss++;
+#endif
     }
 
 out:
+#ifdef CONFIG_QOM_CAST_STATS
+    cast_stats.total++;
+
+    if (cast_stats.total == CAST_STATS_DUMP_INTERVAL) {
+        cast_stats_dump();
+    }
+#endif
 #endif
     return obj;
 }
@@ -531,8 +608,15 @@  ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
 #ifdef CONFIG_QOM_CAST_DEBUG
     int i;
 
+#ifdef CONFIG_QOM_CAST_STATS
+    cast_stats.class++;
+#endif
+
     for (i = 0; i < OBJECT_CLASS_CAST_CACHE; i++) {
         if (class->cast_cache[i] == typename) {
+#ifdef CONFIG_QOM_CAST_STATS
+            cast_stats.hit++;
+#endif
             ret = class;
             goto out;
         }
@@ -556,6 +640,10 @@  ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
             class->cast_cache[i - 1] = class->cast_cache[i];
         }
         class->cast_cache[i - 1] = typename;
+
+#ifdef CONFIG_QOM_CAST_STATS
+        cast_stats.miss++;
+#endif
     }
 out:
 #endif