[Forensics-changes] [yara] 22/135: Implement profiling support

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:27:28 UTC 2017


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag v3.1.0
in repository yara.

commit 89cde68f41da712465f4317622248043f83c5060
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Wed May 14 12:22:18 2014 +0200

    Implement profiling support
---
 libyara/arena.h           |  3 +++
 libyara/exec.c            |  9 +++++++
 libyara/libyara.sym       |  3 ++-
 libyara/parser.c          |  8 +++++++
 libyara/rules.c           | 46 ++++++++++++++++++++++++++++++++++++
 libyara/yara.h            | 16 ++++++++++++-
 threading.c               |  6 +----
 yara-python/yara-python.c | 60 +++++++++++++++++++++++++++++++++++++++++++++--
 yara.c                    |  5 +++-
 9 files changed, 146 insertions(+), 10 deletions(-)

diff --git a/libyara/arena.h b/libyara/arena.h
index 1bdcd0e..7a1c1d1 100644
--- a/libyara/arena.h
+++ b/libyara/arena.h
@@ -105,6 +105,9 @@ int yr_arena_load(
 int yr_arena_duplicate(
     YR_ARENA* arena,
     YR_ARENA** duplicated);
+    
+void yr_arena_print(
+    YR_ARENA* arena);
 
 #endif
 
diff --git a/libyara/exec.c b/libyara/exec.c
index f032dd7..e8373ed 100644
--- a/libyara/exec.c
+++ b/libyara/exec.c
@@ -94,6 +94,10 @@ int yr_execute_code(
   int cycle = 0;
   int tidx = yr_get_tidx();
 
+  #ifdef PROFILING_ENABLED
+  clock_t start = clock();
+  #endif
+
   while(1)
   {
     switch(*ip)
@@ -309,6 +313,11 @@ int yr_execute_code(
         ip += sizeof(uint64_t);
         if (r1)
           rule->t_flags[tidx] |= RULE_TFLAGS_MATCH;
+
+        #ifdef PROFILING_ENABLED
+        rule->clock_ticks += clock() - start;
+        start = clock();
+        #endif
         break;
 
       case EXT_INT:
diff --git a/libyara/libyara.sym b/libyara/libyara.sym
index f377341..761b73c 100644
--- a/libyara/libyara.sym
+++ b/libyara/libyara.sym
@@ -23,4 +23,5 @@ yr_rules_load
 yr_rules_destroy
 yr_rules_define_integer_variable
 yr_rules_define_boolean_variable
-yr_rules_define_string_variable
\ No newline at end of file
+yr_rules_define_string_variable
+yr_rules_print_profiling_info
\ No newline at end of file
diff --git a/libyara/parser.c b/libyara/parser.c
index f639261..b9e5f98 100644
--- a/libyara/parser.c
+++ b/libyara/parser.c
@@ -285,6 +285,10 @@ int _yr_parser_write_string(
   (*string)->g_flags = flags;
   (*string)->chained_to = NULL;
 
+  #ifdef PROFILING_ENABLED
+  (*string)->clock_ticks = 0;
+  #endif
+
   memset((*string)->matches, 0,
          sizeof((*string)->matches));
 
@@ -669,6 +673,10 @@ int yr_parser_reduce_rule_declaration(
   rule->metas = metas;
   rule->ns = compiler->current_namespace;
 
+  #ifdef PROFILING_ENABLED
+  rule->clock_ticks = 0;
+  #endif
+
   compiler->current_rule_flags = 0;
   compiler->current_rule_strings = NULL;
 
diff --git a/libyara/rules.c b/libyara/rules.c
index eae9041..a776503 100644
--- a/libyara/rules.c
+++ b/libyara/rules.c
@@ -848,6 +848,10 @@ inline int _yr_scan_verify_match(
 {
   YR_STRING* string = ac_match->string;
 
+  #ifdef PROFILING_ENABLED
+  clock_t start = clock();
+  #endif
+
   if (data_size - offset <= 0)
     return ERROR_SUCCESS;
 
@@ -867,6 +871,10 @@ inline int _yr_scan_verify_match(
         ac_match, data, data_size, offset, matches_arena));
   }
 
+  #ifdef PROFILING_ENABLED
+  string->clock_ticks += clock() - start;
+  #endif
+
   return ERROR_SUCCESS;
 }
 
@@ -1008,6 +1016,44 @@ void _yr_rules_clean_matches(
 }
 
 
+#ifdef PROFILING_ENABLED
+void yr_rules_print_profiling_info(
+    YR_RULES* rules)
+{
+  YR_RULE* rule;
+  YR_STRING* string;
+
+  clock_t clock_ticks;
+
+  printf("===== PROFILING_ENABLED INFORMATION =====\n");
+
+  rule = rules->rules_list_head;
+
+  while (!RULE_IS_NULL(rule))
+  {
+    clock_ticks = rule->clock_ticks;
+    string = rule->strings;
+
+    while (!STRING_IS_NULL(string))
+    {
+      clock_ticks += string->clock_ticks;
+      string++;
+    }
+
+    printf(
+        "%s:%s: %li\n",
+        rule->ns->name,
+        rule->identifier,
+        clock_ticks);
+
+    rule++;
+  }
+
+  printf("================================\n");
+}
+#endif
+
+
 int yr_rules_scan_mem_block(
     YR_RULES* rules,
     uint8_t* data,
diff --git a/libyara/yara.h b/libyara/yara.h
index ca10117..6cfe315 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -1,4 +1,4 @@
-/*
+  /*
 Copyright (c) 2007-2013. The YARA Authors. All Rights Reserved.
 
 Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,8 @@ limitations under the License.
 #include <stdint.h>
 #include <setjmp.h>
 
+//#define PROFILING_ENABLED
+
 #ifdef WIN32
 #include <windows.h>
 typedef HANDLE mutex_t;
@@ -374,6 +376,10 @@ typedef struct _YR_STRING
   YR_MATCHES matches[MAX_THREADS];
   YR_MATCHES unconfirmed_matches[MAX_THREADS];
 
+  #ifdef PROFILING_ENABLED
+  uint64_t clock_ticks;
+  #endif
+
 } YR_STRING;
 
 
@@ -388,6 +394,10 @@ typedef struct _YR_RULE
   DECLARE_REFERENCE(YR_STRING*, strings);
   DECLARE_REFERENCE(YR_NAMESPACE*, ns);
 
+  #ifdef PROFILING_ENABLED
+  uint64_t clock_ticks;
+  #endif
+
 } YR_RULE;
 
 
@@ -749,5 +759,9 @@ int yr_rules_define_string_variable(
     const char* identifier,
     const char* value);
 
+
+void yr_rules_print_profiling_info(
+    YR_RULES* rules);
+
 #endif
 
diff --git a/threading.c b/threading.c
index 0b08578..dd8e1d3 100644
--- a/threading.c
+++ b/threading.c
@@ -124,7 +124,7 @@ void semaphore_release(
 {
   #ifdef WIN32
   ReleaseSemaphore(*semaphore, 1, NULL);
-  #else
+  #else   
   sem_post(*semaphore);
   #endif
 }
@@ -156,7 +156,3 @@ void thread_join(
   pthread_join(*thread, NULL);
   #endif
 }
-
-
-
-
diff --git a/yara-python/yara-python.c b/yara-python/yara-python.c
index ac9a3c5..848b07b 100644
--- a/yara-python/yara-python.c
+++ b/yara-python/yara-python.c
@@ -25,6 +25,7 @@ limitations under the License.
 #define PyBytes_Check PyString_Check
 #endif
 
+#include <time.h>
 #include <yara.h>
 
 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
@@ -211,6 +212,10 @@ static PyObject * Rules_save(
     PyObject *self,
     PyObject *args);
 
+static PyObject * Rules_profiling_info(
+    PyObject *self,
+    PyObject *args);
+
 static PyObject * Rules_getattro(
     PyObject *self,
     PyObject *name);
@@ -228,6 +233,11 @@ static PyMethodDef Rules_methods[] =
     METH_VARARGS
   },
   {
+    "profiling_info",
+    (PyCFunction) Rules_profiling_info,
+    METH_NOARGS
+  },
+  {
     NULL,
     NULL
   }
@@ -930,8 +940,7 @@ static PyObject * Rules_save(
     if (error != ERROR_SUCCESS)
       return handle_error(error, filepath);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+    Py_RETURN_NONE;
   }
   else
   {
@@ -942,6 +951,53 @@ static PyObject * Rules_save(
 }
 
 
+static PyObject * Rules_profiling_info(
+    PyObject *self,
+    PyObject *args)
+{
+
+#if PROFILING_ENABLED
+  PyObject* object;
+  PyObject* result;
+
+  YR_RULES* rules = ((Rules*) self)->rules;
+  YR_RULE* rule;
+  YR_STRING* string;
+
+  char key[512];
+  uint64_t clock_ticks;
+
+  result = PyDict_New();
+
+  rule = rules->rules_list_head;
+
+  while (!RULE_IS_NULL(rule))
+  {
+    clock_ticks = rule->clock_ticks;
+    string = rule->strings;
+
+    while (!STRING_IS_NULL(string))
+    {
+      clock_ticks += string->clock_ticks;
+      string++;
+    }
+
+    snprintf(key, sizeof(key), "%s:%s", rule->ns->name, rule->identifier);
+
+    object = PyLong_FromLongLong(clock_ticks);
+    PyDict_SetItemString(result, key, object);
+    Py_DECREF(object);
+
+    rule++;
+  }
+
+  return result;
+#else
+  return PyErr_Format(YaraError, "libyara compiled without profiling support");
+#endif
+}
+
+
 static PyObject * Rules_getattro(
     PyObject *self,
     PyObject *name)
diff --git a/yara.c b/yara.c
index e2e08a3..36d21cf 100644
--- a/yara.c
+++ b/yara.c
@@ -1069,6 +1069,10 @@ int main(
     }
   }
 
+  #ifdef PROFILING_ENABLED
+  yr_rules_print_profiling_info(rules);
+  #endif
+
   yr_rules_destroy(rules);
   yr_finalize();
 
@@ -1077,4 +1081,3 @@ int main(
 
   return EXIT_SUCCESS;
 }
-

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list