Fawkes API  Fawkes Development Version
thread.cpp
1 
2 /***************************************************************************
3  * thread.cpp - Fawkes TimeTrackerMainLoop Plugin Thread
4  *
5  * Created: Fri Jun 29 11:56:48 2007 (on flight to RoboCup 2007, Atlanta)
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "thread.h"
24 
25 #include <core/exceptions/system.h>
26 #include <utils/time/tracker.h>
27 
28 using namespace fawkes;
29 
30 /** @class TimeTrackerMainLoopThread <plugins/ttmainloop/thread.h>
31  * Main thread of time tracker main loop plugin.
32  * @author Tim Niemueller
33  */
34 
35 /** Constructor. */
37  : Thread("TimeTrackerMainLoopThread", Thread::OPMODE_WAITFORWAKEUP)
38 {
39 }
40 
41 
42 /** Destructor. */
44 {
45 }
46 
47 
48 void
50 {
51  __loop_start = new Time(clock);
52  __loop_end = new Time(clock);
53 
54  try {
55  __output_interval = config->get_uint("/ttmainloop/output_interval");
56  } catch (Exception &e) {
57  __output_interval = 5.0;
58  logger->log_info(name(), "Output interval not set, using 5 seconds.");
59  }
60 
61 
62  __last_outp_time = new Time(clock);
63  __now = new Time(clock);
64  __last_outp_time->stamp();
65 
66  __tt = new TimeTracker("time.log");
67  __tt_loopcount = 0;
68  __ttc_pre_loop = __tt->add_class("Pre Loop");
69  __ttc_sensor_acquire = __tt->add_class("Sensor Acquire");
70  __ttc_sensor_prepare = __tt->add_class("Sensor Prepare");
71  __ttc_sensor_process = __tt->add_class("Sensor Process");
72  __ttc_worldstate = __tt->add_class("World State");
73  __ttc_think = __tt->add_class("Think");
74  __ttc_skill = __tt->add_class("Skill");
75  __ttc_act = __tt->add_class("Act");
76  __ttc_post_loop = __tt->add_class("Post Loop");
77  __ttc_netproc = __tt->add_class("Net Proc");
78  __ttc_full_loop = __tt->add_class("Full Loop");
79  __ttc_real_loop = __tt->add_class("Real Loop");
80 }
81 
82 
83 #define TIMETRACK_START(c1, c2, c3) \
84  __tt->ping_start(c1); \
85  __tt->ping_start(c2); \
86  __tt->ping_start(c3);
87 
88 #define TIMETRACK_INTER(c1, c2) \
89  __tt->ping_end(c1); \
90  __tt->ping_start(c2);
91 
92 #define TIMETRACK_END(c) \
93  __tt->ping_end(c);
94 
95 void
97 {
98  delete __tt;
99 }
100 
101 void
103 {
104  Thread::CancelState old_state;
105  set_cancel_state(CANCEL_DISABLED, &old_state);
106 
107  TIMETRACK_START(__ttc_real_loop, __ttc_full_loop, __ttc_pre_loop);
108 
109  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP );
110 
111  TIMETRACK_INTER(__ttc_pre_loop, __ttc_sensor_acquire)
112 
113  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE );
114 
115  TIMETRACK_INTER(__ttc_sensor_acquire, __ttc_sensor_prepare)
116 
117  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE );
118 
119  TIMETRACK_INTER(__ttc_sensor_prepare, __ttc_sensor_process)
120 
121  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS );
122 
123  TIMETRACK_INTER(__ttc_sensor_process, __ttc_worldstate)
124 
125  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE );
126 
127  TIMETRACK_INTER(__ttc_worldstate, __ttc_think)
128 
129  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_THINK );
130 
131  TIMETRACK_INTER(__ttc_think, __ttc_skill)
132 
133  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_SKILL );
134 
135  TIMETRACK_INTER(__ttc_skill, __ttc_act)
136 
137  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_ACT );
138  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC );
139 
140  TIMETRACK_INTER(__ttc_act, __ttc_post_loop)
141 
142  blocked_timing_executor->wakeup_and_wait( BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP );
143 
144  TIMETRACK_INTER(__ttc_post_loop, __ttc_netproc)
145 
146  TIMETRACK_END(__ttc_netproc);
147  TIMETRACK_END(__ttc_real_loop);
148 
149  set_cancel_state(old_state);
150 
151  test_cancel();
152 
153  __now->stamp();
154  if ( (*__now - __last_outp_time) >= __output_interval ) {
155  __tt->print_to_stdout();
156  __tt->print_to_file();
157  __tt->reset();
158  *__last_outp_time = *__now;
159  }
160 
161 }
BlockedTimingExecutor * blocked_timing_executor
This is a blocked timing executor instance which can be used to run threads with the BlockedTimingAsp...
Definition: mainloop.h:46
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)=0
Wakeup thread for given hook and wait for completion.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
TimeTrackerMainLoopThread()
Constructor.
Definition: thread.cpp:36
Fawkes library namespace.
virtual void loop()
Code to execute in the thread.
Definition: thread.cpp:102
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:96
A class for handling time.
Definition: time.h:91
void print_to_file()
Print data to file suitable for gnuplot.
Definition: tracker.cpp:442
thread cannot be cancelled
Definition: thread.h:62
virtual ~TimeTrackerMainLoopThread()
Destructor.
Definition: thread.cpp:43
Thread class encapsulation of pthreads.
Definition: thread.h:42
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
Definition: thread.cpp:1350
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:45
Base class for exceptions in Fawkes.
Definition: exception.h:36
unsigned int add_class(std::string name)
Add a new class.
Definition: tracker.cpp:156
Time tracking utility.
Definition: tracker.h:38
const char * name() const
Get name of thread.
Definition: thread.h:95
void reset(std::string comment="")
Reset times.
Definition: tracker.cpp:116
void print_to_stdout()
Print results to stdout.
Definition: tracker.cpp:317
void test_cancel()
Set cancellation point.
Definition: thread.cpp:889
Time & stamp()
Set this time to the current time.
Definition: time.cpp:783
virtual void init()
Initialize the thread.
Definition: thread.cpp:49
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
CancelState
Cancel state.
Definition: thread.h:60