From fe1222a90dd809560e787ce09391d5cf97bbe6fe Mon Sep 17 00:00:00 2001
From: Thomas Geymayer <tomgey@gmail.com>
Date: Thu, 15 Nov 2012 11:55:25 +0100
Subject: [PATCH] Optional profiling commands using gperftools

---
 CMakeLists.txt           |  3 +++
 src/Main/CMakeLists.txt  |  4 +++
 src/Main/fg_commands.cxx | 53 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3013e9566..fe0af0500 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -223,6 +223,9 @@ check_include_file(unistd.h   HAVE_UNISTD_H)
 check_include_file(sys/time.h HAVE_SYS_TIME_H)
 check_include_file(windows.h  HAVE_WINDOWS_H)
 
+# check optionally supported dependencies
+find_package(GooglePerfTools)
+
 if(ENABLE_RTI)
     find_package(RTI)
     if(RTI_FOUND)
diff --git a/src/Main/CMakeLists.txt b/src/Main/CMakeLists.txt
index 6ea3419e4..72ed8634a 100644
--- a/src/Main/CMakeLists.txt
+++ b/src/Main/CMakeLists.txt
@@ -90,6 +90,10 @@ if(ENABLE_JSBSIM)
     target_link_libraries(fgfs JSBSim)
 endif()
 
+if(GOOGLE_PERFTOOLS_FOUND)
+    target_link_libraries(fgfs profiler)
+endif()
+
 target_link_libraries(fgfs
 	${SQLITE3_LIBRARY}
 	${SIMGEAR_LIBRARIES}
diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx
index 75a5e2fbf..1e32ef6b2 100644
--- a/src/Main/fg_commands.cxx
+++ b/src/Main/fg_commands.cxx
@@ -53,12 +53,15 @@
 
 #include <boost/scoped_array.hpp>
 
+#if GOOGLE_PERFTOOLS_FOUND == YES
+# include <google/profiler.h>
+#endif
+
 using std::string;
 using std::ifstream;
 using std::ofstream;
 
 
-
 ////////////////////////////////////////////////////////////////////////
 // Static helper functions.
 ////////////////////////////////////////////////////////////////////////
@@ -1446,7 +1449,48 @@ do_release_cockpit_button (const SGPropertyNode *arg)
 
   return true;
 }
-  
+
+// Optional profiling commands using gperftools:
+// http://code.google.com/p/gperftools/
+
+#if GOOGLE_PERFTOOLS_FOUND != YES
+static void
+no_profiling_support()
+{
+  SG_LOG
+  (
+    SG_GENERAL,
+    SG_WARN,
+    "No profiling support! Install gperftools and reconfigure/rebuild fgfs."
+  );
+}
+#endif
+
+static bool
+do_profiler_start(const SGPropertyNode *arg)
+{
+#if GOOGLE_PERFTOOLS_FOUND == YES
+  const char *filename = arg->getStringValue("filename", "fgfs.profile");
+  ProfilerStart(filename);
+  return true;
+#else
+  no_profiling_support();
+  return false;
+#endif
+}
+
+static bool
+do_profiler_stop(const SGPropertyNode *arg)
+{
+#if GOOGLE_PERFTOOLS_FOUND == YES
+  ProfilerStop();
+  return true;
+#else
+  no_profiling_support();
+  return false;
+#endif
+}
+
 ////////////////////////////////////////////////////////////////////////
 // Command setup.
 ////////////////////////////////////////////////////////////////////////
@@ -1521,6 +1565,11 @@ static struct {
     { "reload-shaders", do_reload_shaders },
     { "reload-materials", do_materials_reload },
 
+#if GOOGLE_PERFTOOLS_FOUND == YES
+    { "profiler-start", do_profiler_start },
+    { "profiler-stop",  do_profiler_stop },
+#endif
+
     { 0, 0 }			// zero-terminated
 };