Fawkes API  Fawkes Development Version
thread.cpp
00001 
00002 /***************************************************************************
00003  *  thread.cpp - Fawkes TimeTrackerMainLoop Plugin Thread
00004  *
00005  *  Created: Fri Jun 29 11:56:48 2007 (on flight to RoboCup 2007, Atlanta)
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "thread.h"
00024 
00025 #include <core/exceptions/system.h>
00026 #include <utils/time/tracker.h>
00027 
00028 using namespace fawkes;
00029 
00030 /** @class TimeTrackerMainLoopThread <plugins/worldmodel/wm_thread.h>
00031  * Main thread of time tracker main loop plugin.
00032  * @author Tim Niemueller
00033  */
00034 
00035 /** Constructor. */
00036 TimeTrackerMainLoopThread::TimeTrackerMainLoopThread()
00037   : Thread("TimeTrackerMainLoopThread", Thread::OPMODE_WAITFORWAKEUP)
00038 {
00039 }
00040 
00041 
00042 /** Destructor. */
00043 TimeTrackerMainLoopThread::~TimeTrackerMainLoopThread()
00044 {
00045 }
00046 
00047 
00048 void
00049 TimeTrackerMainLoopThread::init()
00050 {
00051   __loop_start = new Time(clock);
00052   __loop_end   = new Time(clock);
00053 
00054   try {
00055     __output_interval = config->get_uint("/ttmainloop/output_interval");
00056   } catch (Exception &e) {
00057     __output_interval = 5.0;
00058     logger->log_info(name(), "Output interval not set, using 5 seconds.");
00059   }
00060 
00061 
00062   __last_outp_time = new Time(clock);
00063   __now            = new Time(clock);
00064   __last_outp_time->stamp();
00065 
00066   __tt = new TimeTracker("time.log");
00067   __tt_loopcount        = 0;
00068   __ttc_pre_loop        = __tt->add_class("Pre Loop");
00069   __ttc_sensor_acquire  = __tt->add_class("Sensor Acquire");
00070   __ttc_sensor_prepare  = __tt->add_class("Sensor Prepare");
00071   __ttc_sensor_process  = __tt->add_class("Sensor Process");
00072   __ttc_worldstate      = __tt->add_class("World State");
00073   __ttc_think           = __tt->add_class("Think");
00074   __ttc_skill           = __tt->add_class("Skill");
00075   __ttc_act             = __tt->add_class("Act");
00076   __ttc_post_loop       = __tt->add_class("Post Loop");
00077   __ttc_netproc         = __tt->add_class("Net Proc");
00078   __ttc_full_loop       = __tt->add_class("Full Loop");
00079   __ttc_real_loop       = __tt->add_class("Real Loop");
00080 }
00081 
00082 
00083 #define TIMETRACK_START(c1, c2, c3)             \
00084   __tt->ping_start(c1);                         \
00085   __tt->ping_start(c2);                         \
00086   __tt->ping_start(c3);
00087 
00088 #define TIMETRACK_INTER(c1, c2)                 \
00089  __tt->ping_end(c1);                            \
00090  __tt->ping_start(c2);
00091 
00092 #define TIMETRACK_END(c)                        \
00093   __tt->ping_end(c);
00094 
00095 void
00096 TimeTrackerMainLoopThread::finalize()
00097 {
00098   delete __tt;
00099 }
00100 
00101 void
00102 TimeTrackerMainLoopThread::loop()
00103 {
00104   Thread::CancelState old_state;
00105   set_cancel_state(CANCEL_DISABLED, &old_state);
00106 
00107   TIMETRACK_START(__ttc_real_loop, __ttc_full_loop, __ttc_pre_loop);
00108 
00109   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP );
00110 
00111   TIMETRACK_INTER(__ttc_pre_loop, __ttc_sensor_acquire)
00112 
00113   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE );
00114 
00115   TIMETRACK_INTER(__ttc_sensor_acquire, __ttc_sensor_prepare)
00116 
00117   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE );
00118 
00119   TIMETRACK_INTER(__ttc_sensor_prepare, __ttc_sensor_process)
00120 
00121   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS );
00122 
00123   TIMETRACK_INTER(__ttc_sensor_process, __ttc_worldstate)
00124 
00125   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE );
00126 
00127   TIMETRACK_INTER(__ttc_worldstate, __ttc_think)
00128 
00129   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_THINK );
00130 
00131   TIMETRACK_INTER(__ttc_think, __ttc_skill)
00132 
00133   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SKILL );
00134 
00135   TIMETRACK_INTER(__ttc_skill, __ttc_act)
00136 
00137   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_ACT );
00138   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC );
00139 
00140   TIMETRACK_INTER(__ttc_act, __ttc_post_loop)
00141 
00142   blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP );
00143 
00144   TIMETRACK_INTER(__ttc_post_loop, __ttc_netproc)
00145 
00146   TIMETRACK_END(__ttc_netproc);
00147   TIMETRACK_END(__ttc_real_loop);
00148 
00149   set_cancel_state(old_state);
00150 
00151   test_cancel();
00152 
00153   __now->stamp();
00154   if ( (*__now - __last_outp_time) >= __output_interval ) {
00155     __tt->print_to_stdout();
00156     __tt->print_to_file();
00157     __tt->reset();
00158     *__last_outp_time = *__now;
00159   }
00160 
00161 }