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
28using 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/** Destructor. */
43{
44}
45
46void
48{
49 loop_start_ = new Time(clock);
50 loop_end_ = new Time(clock);
51
52 try {
53 output_interval_ = config->get_uint("/ttmainloop/output_interval");
54 } catch (Exception &e) {
55 output_interval_ = 5.0;
56 logger->log_info(name(), "Output interval not set, using 5 seconds.");
57 }
58
59 last_outp_time_ = new Time(clock);
60 now_ = new Time(clock);
61 last_outp_time_->stamp();
62
63 tt_ = new TimeTracker("time.log");
64 tt_loopcount_ = 0;
65 ttc_pre_loop_ = tt_->add_class("Pre Loop");
66 ttc_sensor_acquire_ = tt_->add_class("Sensor Acquire");
67 ttc_sensor_prepare_ = tt_->add_class("Sensor Prepare");
68 ttc_sensor_process_ = tt_->add_class("Sensor Process");
69 ttc_worldstate_ = tt_->add_class("World State");
70 ttc_think_ = tt_->add_class("Think");
71 ttc_skill_ = tt_->add_class("Skill");
72 ttc_act_ = tt_->add_class("Act");
73 ttc_post_loop_ = tt_->add_class("Post Loop");
74 ttc_netproc_ = tt_->add_class("Net Proc");
75 ttc_full_loop_ = tt_->add_class("Full Loop");
76 ttc_real_loop_ = tt_->add_class("Real Loop");
77}
78
79#define TIMETRACK_START(c1, c2, c3) \
80 tt_->ping_start(c1); \
81 tt_->ping_start(c2); \
82 tt_->ping_start(c3);
83
84#define TIMETRACK_INTER(c1, c2) \
85 tt_->ping_end(c1); \
86 tt_->ping_start(c2);
87
88#define TIMETRACK_END(c) tt_->ping_end(c);
89
90void
92{
93 delete loop_start_;
94 delete loop_end_;
95 delete last_outp_time_;
96 delete now_;
97 delete tt_;
98}
99
100void
102{
103 Thread::CancelState old_state;
105
106 TIMETRACK_START(ttc_real_loop_, ttc_full_loop_, ttc_pre_loop_);
107
108 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP);
109
110 TIMETRACK_INTER(ttc_pre_loop_, ttc_sensor_acquire_)
111
112 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE);
113
114 TIMETRACK_INTER(ttc_sensor_acquire_, ttc_sensor_prepare_)
115
116 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE);
117
118 TIMETRACK_INTER(ttc_sensor_prepare_, ttc_sensor_process_)
119
120 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS);
121
122 TIMETRACK_INTER(ttc_sensor_process_, ttc_worldstate_)
123
124 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE);
125
126 TIMETRACK_INTER(ttc_worldstate_, ttc_think_)
127
128 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_THINK);
129
130 TIMETRACK_INTER(ttc_think_, ttc_skill_)
131
132 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_SKILL);
133
134 TIMETRACK_INTER(ttc_skill_, ttc_act_)
135
136 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_ACT);
137 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC);
138
139 TIMETRACK_INTER(ttc_act_, ttc_post_loop_)
140
141 blocked_timing_executor->wakeup_and_wait(BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP);
142
143 TIMETRACK_INTER(ttc_post_loop_, ttc_netproc_)
144
145 TIMETRACK_END(ttc_netproc_);
146 TIMETRACK_END(ttc_real_loop_);
147
148 set_cancel_state(old_state);
149
150 test_cancel();
151
152 now_->stamp();
153 if ((*now_ - last_outp_time_) >= output_interval_) {
154 tt_->print_to_stdout();
155 tt_->print_to_file();
156 tt_->reset();
157 *last_outp_time_ = *now_;
158 }
159}
virtual void finalize()
Finalize the thread.
Definition: thread.cpp:91
TimeTrackerMainLoopThread()
Constructor.
Definition: thread.cpp:36
virtual void loop()
Code to execute in the thread.
Definition: thread.cpp:101
virtual void init()
Initialize the thread.
Definition: thread.cpp:47
virtual ~TimeTrackerMainLoopThread()
Destructor.
Definition: thread.cpp:42
virtual void wakeup_and_wait(BlockedTimingAspect::WakeupHook hook, unsigned int timeout_usec=0)=0
Wakeup thread for given hook and wait for completion.
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:42
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
BlockedTimingExecutor * blocked_timing_executor
This is a blocked timing executor instance which can be used to run threads with the BlockedTimingAsp...
Definition: mainloop.h:43
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
CancelState
Cancel state.
Definition: thread.h:64
@ CANCEL_DISABLED
thread cannot be cancelled
Definition: thread.h:66
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
Definition: thread.cpp:1396
void test_cancel()
Set cancellation point.
Definition: thread.cpp:871
Time tracking utility.
Definition: tracker.h:37
void print_to_stdout()
Print results to stdout.
Definition: tracker.cpp:307
void print_to_file()
Print data to file suitable for gnuplot.
Definition: tracker.cpp:426
unsigned int add_class(std::string name)
Add a new class.
Definition: tracker.cpp:149
void reset(std::string comment="")
Reset times.
Definition: tracker.cpp:110
A class for handling time.
Definition: time.h:93
Time & stamp()
Set this time to the current time.
Definition: time.cpp:704
Fawkes library namespace.