Text archives Help
- From: bigler@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r1309 - in trunk: Core Core/Util Engine/PixelSamplers UserInterface
- Date: Mon, 19 Mar 2007 16:19:31 -0700 (MST)
Author: bigler
Date: Mon Mar 19 16:19:30 2007
New Revision: 1309
Added:
trunk/Core/Util/CPUTime.cc
trunk/Core/Util/CPUTime.h
Modified:
trunk/Core/CMakeLists.txt
trunk/Engine/PixelSamplers/TimeViewSampler.cc
trunk/UserInterface/XWindowUI.cc
trunk/UserInterface/XWindowUI.h
Log:
Core/CMakeLists.txt
Added CPUTime.
Core/Util/CPUTime.cc
Core/Util/CPUTime.h
Replacement for SCIRun::Time::currentSeconds and currentTicks.
Engine/PixelSamplers/TimeViewSampler.cc
Added switch to use CPUTime instead of SCIRun::Time. Currently
defaults to CPUTime.
Normalize deltaTime by the size of the Fragment.
UserInterface/XWindowUI.cc
UserInterface/XWindowUI.h
Added ability to turn the timeView on and off by pressing 't'.
Modified: trunk/Core/CMakeLists.txt
==============================================================================
--- trunk/Core/CMakeLists.txt (original)
+++ trunk/Core/CMakeLists.txt Mon Mar 19 16:19:30 2007
@@ -56,12 +56,15 @@
SET (CORE_SOURCES ${CORE_SOURCES}
Util/Args.h
Util/Args.cc
+ Util/CPUTime.h
+ Util/CPUTime.cc
Util/Endian.h
Util/LargeFile.h
Util/LargeFile.cc
Util/Stat.h
Util/ThreadStorage.h
- Util/ThreadStorage.cc)
+ Util/ThreadStorage.cc
+ )
ADD_LIBRARY (Manta_Core ${CORE_SOURCES})
Added: trunk/Core/Util/CPUTime.cc
==============================================================================
--- (empty file)
+++ trunk/Core/Util/CPUTime.cc Mon Mar 19 16:19:30 2007
@@ -0,0 +1,121 @@
+#include <Core/Util/CPUTime.h>
+#include <Core/Thread/ThreadError.h>
+
+#if defined(__APPLE__) && defined(__POWERPC__)
+#include <ppc_intrinsics.h>
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#elif _WIN32
+#include <time.h>
+#else
+#include <sys/time.h>
+#endif
+
+using namespace Manta;
+
+static bool initialized=false;
+static double secondsPerTick_val = 0;
+
+void CPUTime::initialize()
+{
+ initialized = true;
+ resetScale();
+}
+
+void CPUTime::resetScale()
+{
+#if defined(__APPLE__) && defined(__POWERPC__)
+ mach_timebase_info_data_t time_info;
+ mach_timebase_info(&time_info);
+
+ // Scales to nanoseconds without 1e-9f
+ secondsPerTick_val = (1e-9*static_cast<double>(time_info.numer))/
+ static_cast<double>(time_info.denom);
+#elif defined(_WIN32)
+ secondsPerTick_val = 1.0/static_cast<double>(CLOCKS_PER_SEC);
+#else
+ FILE *fp = fopen("/proc/cpuinfo","r");
+ char input[255];
+ if (!fp)
+ throw SCIRun::ThreadError(std::string("CPUTime::resetScale failed:
couldn't find /proc/cpuinfo. Is this not a linux machine?"));
+ while (!feof(fp) && fgets(input, 255, fp)) {
+ if (strstr(input,"cpu MHz")) {
+ char *p = strchr(input,':');
+ double MHz = 0.0;
+ if (p)
+ MHz = atof(p+1);
+ secondsPerTick_val = 1000. / MHz * 1e-9;
+ break;
+ }
+ }
+ fclose(fp);
+#endif
+}
+
+//////////
+// Return the current CPU time, in terms of clock ticks.
+// Time zero is at some arbitrary point in the past.
+#if defined(__APPLE__) && defined(__POWERPC__)
+/////////////////////////////
+// Apple (PowerPC)
+/////////////////////////////
+CPUTime::SysClock CPUTime::currentTicks()
+{
+ if (!initialized) initialize();
+
+ unsigned int tbl, tbu0, tbu1;
+
+ do {
+ __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
+ __asm__ __volatile__ ("mftb %0" : "=r"(tbl));
+ __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
+ } while (tbu0 != tbu1);
+
+ return ((static_cast<unsigned long long>(tbu0) << 32) | tbl);
+}
+#elif defined (_WIN32)
+/////////////////////////////
+// Win32
+/////////////////////////////
+CPUTime::SysClock CPUTime::currentTicks()
+{
+ if (!initialized) initialize();
+ return clock();
+}
+#else
+/////////////////////////////
+// Linux and Apple (x86)
+/////////////////////////////
+CPUTime::SysClock CPUTime::currentTicks()
+{
+ if (!initialized) initialize();
+ unsigned int a, d;
+ asm volatile("rdtsc" : "=a" (a), "=d" (d));
+ return static_cast<unsigned long long>(a) |
+ (static_cast<unsigned long long>(d) << 32);
+}
+#endif // defined(__APPLE__) && defined(__POWERPC__)
+
+//////////
+// Return the current CPU time, in terms of seconds.
+// This is slower than currentTicks(). Time zero is at
+// some arbitrary point in the past.
+double CPUTime::currentSeconds()
+{
+ return currentTicks()*secondsPerTick_val;
+}
+
+//////////
+// Return the conversion from seconds to ticks.
+double CPUTime::ticksPerSecond()
+{
+ return 1.0/secondsPerTick_val;
+}
+
+//////////
+// Return the conversion from ticks to seconds.
+double CPUTime::secondsPerTick()
+{
+ return secondsPerTick_val;
+}
+
Added: trunk/Core/Util/CPUTime.h
==============================================================================
--- (empty file)
+++ trunk/Core/Util/CPUTime.h Mon Mar 19 16:19:30 2007
@@ -0,0 +1,50 @@
+#ifndef MANTA_UTIL_CPUTIME_H__
+#define MANTA_UTIL_CPUTIME_H__
+
+namespace Manta {
+
+ // This uses the cycle counter of the processor. Different
+ // processors in the system will have different values for this. If
+ // you process moves across processors, then the delta time you
+ // measure will likely be incorrect. This is mostly for fine
+ // grained measurements where the process is likely to be on the
+ // same processor. For more global things you should use the
+ // SCIRun::Time interface.
+
+ // Also note that if you processors' speeds change (i.e. processors
+ // scaling) or if you are in a heterogenous environment, you will
+ // likely get spurious results.
+ class CPUTime {
+ public:
+ typedef unsigned long long SysClock;
+
+ //////////
+ // Return the current CPU time, in terms of clock ticks.
+ // Time zero is at some arbitrary point in the past.
+ static SysClock currentTicks();
+
+ //////////
+ // Return the current CPU time, in terms of seconds.
+ // This is slower than currentTicks(). Time zero is at
+ // some arbitrary point in the past.
+ static double currentSeconds();
+
+ //////////
+ // Return the conversion from seconds to ticks.
+ static double ticksPerSecond();
+
+ //////////
+ // Return the conversion from ticks to seconds.
+ static double secondsPerTick();
+
+ //////////
+ // Requeries the processor speed to recompute the ticksPerSecond.
+ static void resetScale();
+ private:
+ CPUTime();
+ static void initialize();
+ };
+
+} // end namespace Manta
+
+#endif // #ifndef MANTA_UTIL_CPUTIME_H__
Modified: trunk/Engine/PixelSamplers/TimeViewSampler.cc
==============================================================================
--- trunk/Engine/PixelSamplers/TimeViewSampler.cc (original)
+++ trunk/Engine/PixelSamplers/TimeViewSampler.cc Mon Mar 19 16:19:30
2007
@@ -8,7 +8,12 @@
#include <Core/Util/Args.h>
#include <Core/Exceptions/IllegalArgument.h>
-#include <SCIRun/Core/Thread/Time.h>
+#define USE_SCIRUN_TIME 0
+#if USE_SCIRUN_TIME
+# include <SCIRun/Core/Thread/Time.h>
+#else
+# include <Core/Util/CPUTime.h>
+#endif
#include <iostream>
@@ -21,7 +26,7 @@
TimeViewSampler::TimeViewSampler(const vector<string>& args)
: child(0),
- scale(1.e4),
+ scale(1.e5),
active(true)
{
int argc = static_cast<int>(args.size());
@@ -43,7 +48,7 @@
void TimeViewSampler::usage()
{
cerr << "TimeViewSampler args:\n";
- cerr << "\t-scale <Real> - defaults to 10000 (1e4)\n";
+ cerr << "\t-scale <Real> - defaults to 100,000 (1e5)\n";
}
TimeViewSampler::~TimeViewSampler()
@@ -70,13 +75,25 @@
{
double startTime;
if (active) {
+#if USE_SCIRUN_TIME
startTime = SCIRun::Time::currentSeconds();
+#else
+ startTime = CPUTime::currentSeconds();
+#endif
}
if (child) child->renderFragment(context, fragment);
if (active) {
+#if USE_SCIRUN_TIME
double endTime = SCIRun::Time::currentSeconds();
+#else
+ double endTime = CPUTime::currentSeconds();
+#endif
double deltaTime = (endTime - startTime);
- ColorComponent color = deltaTime*scale;
+ // Make sure to normalize the color based on the number of
+ // fragments. Note that if there are no fragments (must be a
+ // really rare case), you will end up with a inf for color.
+ // That's OK, because you won't use it in that case.
+ ColorComponent color = deltaTime*scale/(fragment.end()-fragment.begin());
for(int i=fragment.begin();i<fragment.end();++i) {
for ( int c = 0; c < Color::NumComponents; c++ )
fragment.color[c][i] = color;
Modified: trunk/UserInterface/XWindowUI.cc
==============================================================================
--- trunk/UserInterface/XWindowUI.cc (original)
+++ trunk/UserInterface/XWindowUI.cc Mon Mar 19 16:19:30 2007
@@ -10,6 +10,7 @@
#include <Interface/Scene.h>
#include <Interface/Object.h>
#include <Interface/RayPacket.h>
+#include <Engine/PixelSamplers/TimeViewSampler.h>
#include <Core/Exceptions/ErrnoException.h>
#include <Core/Exceptions/InternalError.h>
#include <Core/Exceptions/IllegalArgument.h>
@@ -68,7 +69,7 @@
XWindowUI::XWindowUI(const vector<string>& args, MantaInterface
*rtrt_interface)
: rtrt_interface(rtrt_interface), xlock("XWindowUI display lock"),
- xsema("XWindowUI semaphore", 0), path( 0 ),
+ xsema("XWindowUI semaphore", 0), path( 0 ), timeView(0),
quitting(false)
{
bool quit=false;
@@ -502,6 +503,9 @@
register_key(0, XStringToKeysym("c"),
"output camera",
Callback::create(this, &XWindowUI::output_camera));
+ register_key(0, XStringToKeysym("t"),
+ "time profile display",
+ Callback::create(this, &XWindowUI::timeView_keyboard));
register_key(0, XStringToKeysym("Escape"),
"quit after this frame, press again to immediately quit",
Callback::create(this, &XWindowUI::quitkey));
@@ -636,6 +640,43 @@
rtrt_interface->addTransaction("reset path",
Callback::create(path, &CameraPath::reset));
+}
+
+void XWindowUI::toggleTimeView()
+{
+ PixelSampler* ps = rtrt_interface->getPixelSampler();
+ // Check to see if there is a TimeViewSampler already running
+ TimeViewSampler* tvps = dynamic_cast<TimeViewSampler*>(ps);
+ if (tvps != NULL) {
+ // We have a winner. Let's swap it out.
+ // TODO: (bigler) should we delete the old one?
+ timeView = tvps;
+ ps = tvps->getPixelSampler();
+ // If the child is NULL, don't stuff it back in.
+ if (ps != NULL) {
+ rtrt_interface->setPixelSampler(ps);
+ } else {
+ cerr << "TimeViewSampler has no child, so it can't be turned off\n";
+ }
+ } else {
+ // The pixel sampler in the engine isn't a TimeViewSampler, so
+ // let's make it a child of one.
+ if (timeView == NULL) {
+ // Allocate one
+ timeView = new TimeViewSampler(vector<string>());
+ }
+ timeView->setPixelSampler(ps);
+ rtrt_interface->setPixelSampler(timeView);
+ }
+}
+
+void XWindowUI::timeView_keyboard(unsigned int /*state*/,
+ unsigned long /*key*/,
+ int /*channel*/)
+{
+ rtrt_interface->addTransaction("TimeView toggle",
+ Callback::create(this,
+
&XWindowUI::toggleTimeView));
}
void XWindowUI::output_camera(unsigned int, unsigned long, int channel)
Modified: trunk/UserInterface/XWindowUI.h
==============================================================================
--- trunk/UserInterface/XWindowUI.h (original)
+++ trunk/UserInterface/XWindowUI.h Mon Mar 19 16:19:30 2007
@@ -24,6 +24,7 @@
class TrackBall;
class XWindow;
class CameraPath;
+ class TimeViewSampler;
class XWindowUI: public UserInterface, public SetupCallback, public
SCIRun::Runnable {
public:
@@ -112,6 +113,13 @@
// closed by the window manager.
CameraPath *path;
+
+ // TimeViewSampler stuff
+ void timeView_keyboard(unsigned int /*state*/,
+ unsigned long /*key*/,
+ int /*channel*/);
+ void toggleTimeView();
+ TimeViewSampler* timeView;
SCIRun::Mutex xlock;
SCIRun::Semaphore xsema;
- [MANTA] r1309 - in trunk: Core Core/Util Engine/PixelSamplers UserInterface, bigler, 03/19/2007
Archive powered by MHonArc 2.6.16.