Fawkes API
Fawkes Development Version
|
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 = __tt->add_class("Sensor"); 00070 __ttc_worldstate = __tt->add_class("World State"); 00071 __ttc_think = __tt->add_class("Think"); 00072 __ttc_skill = __tt->add_class("Skill"); 00073 __ttc_act = __tt->add_class("Act"); 00074 __ttc_post_loop = __tt->add_class("Post Loop"); 00075 __ttc_netproc = __tt->add_class("Net Proc"); 00076 __ttc_full_loop = __tt->add_class("Full Loop"); 00077 __ttc_real_loop = __tt->add_class("Real Loop"); 00078 } 00079 00080 00081 #define TIMETRACK_START(c1, c2, c3) \ 00082 __tt->ping_start(c1); \ 00083 __tt->ping_start(c2); \ 00084 __tt->ping_start(c3); 00085 00086 #define TIMETRACK_INTER(c1, c2) \ 00087 __tt->ping_end(c1); \ 00088 __tt->ping_start(c2); 00089 00090 #define TIMETRACK_END(c) \ 00091 __tt->ping_end(c); 00092 00093 void 00094 TimeTrackerMainLoopThread::finalize() 00095 { 00096 delete __tt; 00097 } 00098 00099 void 00100 TimeTrackerMainLoopThread::loop() 00101 { 00102 Thread::CancelState old_state; 00103 set_cancel_state(CANCEL_DISABLED, &old_state); 00104 00105 TIMETRACK_START(__ttc_real_loop, __ttc_full_loop, __ttc_pre_loop); 00106 00107 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP ); 00108 00109 TIMETRACK_INTER(__ttc_pre_loop, __ttc_sensor) 00110 00111 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR ); 00112 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS ); 00113 00114 TIMETRACK_INTER(__ttc_sensor, __ttc_worldstate) 00115 00116 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE ); 00117 00118 TIMETRACK_INTER(__ttc_worldstate, __ttc_think) 00119 00120 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_THINK ); 00121 00122 TIMETRACK_INTER(__ttc_think, __ttc_skill) 00123 00124 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SKILL ); 00125 00126 TIMETRACK_INTER(__ttc_skill, __ttc_act) 00127 00128 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_ACT ); 00129 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC ); 00130 00131 TIMETRACK_INTER(__ttc_act, __ttc_post_loop) 00132 00133 blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP ); 00134 00135 TIMETRACK_INTER(__ttc_post_loop, __ttc_netproc) 00136 00137 TIMETRACK_END(__ttc_netproc); 00138 TIMETRACK_END(__ttc_real_loop); 00139 00140 set_cancel_state(old_state); 00141 00142 test_cancel(); 00143 00144 __now->stamp(); 00145 if ( (*__now - __last_outp_time) >= __output_interval ) { 00146 __tt->print_to_stdout(); 00147 __tt->print_to_file(); 00148 __tt->reset(); 00149 *__last_outp_time = *__now; 00150 } 00151 00152 }