[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | // created: 2012/10/04
|
---|
| 6 | // filename: Thread_impl.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: classe implementant un thread rt ou non
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "Thread_impl.h"
|
---|
| 19 | #include "Thread.h"
|
---|
| 20 | #include "IODevice.h"
|
---|
| 21 | #include "IODevice_impl.h"
|
---|
| 22 | #include "ConditionVariable.h"
|
---|
| 23 | #include "config.h"
|
---|
| 24 | #include "FrameworkManager.h"
|
---|
| 25 | #include <string.h>
|
---|
| 26 | #ifdef __XENO__
|
---|
| 27 | #include <rtdk.h>
|
---|
[118] | 28 | #define TH_NAME getFrameworkManager()->ObjectName() + "-" + self->ObjectName()
|
---|
| 29 | #else//__XENO__
|
---|
[2] | 30 | #include <sys/resource.h>
|
---|
[38] | 31 | #include <unistd.h>
|
---|
| 32 | #include <sys/syscall.h>
|
---|
[118] | 33 | #endif//__XENO__
|
---|
[2] | 34 |
|
---|
| 35 | using std::string;
|
---|
| 36 | using namespace flair::core;
|
---|
| 37 |
|
---|
[15] | 38 | Thread_impl::Thread_impl(Thread *self, uint8_t priority) {
|
---|
| 39 | isRunning = false;
|
---|
| 40 | tobestopped = false;
|
---|
| 41 | is_suspended = false;
|
---|
| 42 | period_set = false;
|
---|
[2] | 43 |
|
---|
[15] | 44 | this->self = self;
|
---|
| 45 | cond = new ConditionVariable(self, self->ObjectName());
|
---|
[2] | 46 |
|
---|
[15] | 47 | if (priority < MIN_THREAD_PRIORITY) {
|
---|
| 48 | priority = MIN_THREAD_PRIORITY;
|
---|
| 49 | // printf("Thread::Thread, %s: priority set to
|
---|
| 50 | // %i\n",self->ObjectName().c_str(), priority);
|
---|
| 51 | }
|
---|
| 52 | if (priority > MAX_THREAD_PRIORITY) {
|
---|
| 53 | priority = MAX_THREAD_PRIORITY;
|
---|
| 54 | self->Warn("priority set to %i\n", MAX_THREAD_PRIORITY);
|
---|
| 55 | }
|
---|
[2] | 56 |
|
---|
[15] | 57 | this->priority = priority;
|
---|
| 58 | period = 100 * 1000 * 1000; // 100ms par defaut
|
---|
[186] | 59 | min_latency = 1000 * 1000 * 1000;
|
---|
| 60 | max_latency = 0;
|
---|
| 61 | mean_latency = 0;
|
---|
[15] | 62 | last = 0;
|
---|
| 63 | cpt = 0;
|
---|
[118] | 64 |
|
---|
| 65 | #ifdef __XENO__
|
---|
| 66 | // Perform auto-init of rt_print buffers if the task doesn't do so
|
---|
| 67 | rt_print_auto_init(1);
|
---|
| 68 | // Initialise the rt_print buffer for this task explicitly
|
---|
| 69 | rt_print_init(512, (TH_NAME).c_str());
|
---|
| 70 | #endif //__XENO__
|
---|
[2] | 71 | }
|
---|
| 72 |
|
---|
[15] | 73 | Thread_impl::~Thread_impl() {
|
---|
| 74 | SafeStop();
|
---|
| 75 | Join();
|
---|
[2] | 76 | }
|
---|
| 77 |
|
---|
[15] | 78 | void Thread_impl::Start(void) {
|
---|
| 79 | int status;
|
---|
[133] | 80 | char errorMsg[256];
|
---|
[2] | 81 |
|
---|
[15] | 82 | isRunning = true;
|
---|
[20] | 83 | tobestopped = false;
|
---|
| 84 | is_suspended = false;
|
---|
[2] | 85 |
|
---|
| 86 | #ifdef __XENO__
|
---|
[118] | 87 | string th_name =TH_NAME;
|
---|
[2] | 88 |
|
---|
| 89 | #ifdef RT_STACK_SIZE
|
---|
[15] | 90 | status = rt_task_create(&task_rt, th_name.c_str(), RT_STACK_SIZE,
|
---|
| 91 | (int)priority, T_JOINABLE);
|
---|
[2] | 92 | #else
|
---|
[15] | 93 | status =
|
---|
| 94 | rt_task_create(&task_rt, th_name.c_str(), 0, (int)priority, T_JOINABLE);
|
---|
| 95 | #endif // RT_STACK_SIZE
|
---|
| 96 | if (status != 0) {
|
---|
[133] | 97 | self->Err("rt_task_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 98 | } else {
|
---|
| 99 | //_printf("rt_task_create ok %s\n",th_name);
|
---|
| 100 | }
|
---|
[2] | 101 |
|
---|
[15] | 102 | status = rt_task_start(&task_rt, &main_rt, (void *)this);
|
---|
| 103 | if (status != 0) {
|
---|
[133] | 104 | self->Err("rt_task_start error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 105 | } else {
|
---|
| 106 | //_printf("rt_task_start ok %s\n",th_name);
|
---|
| 107 | }
|
---|
[2] | 108 |
|
---|
| 109 | #else //__XENO__
|
---|
| 110 |
|
---|
[15] | 111 | // Initialize thread creation attributes
|
---|
| 112 | pthread_attr_t attr;
|
---|
| 113 | if (pthread_attr_init(&attr) != 0) {
|
---|
| 114 | self->Err("Error pthread_attr_init\n");
|
---|
| 115 | }
|
---|
[2] | 116 |
|
---|
| 117 | #ifdef NRT_STACK_SIZE
|
---|
[15] | 118 | if (pthread_attr_setstacksize(&attr, NRT_STACK_SIZE) != 0) {
|
---|
| 119 | self->Err("Error pthread_attr_setstacksize\n");
|
---|
| 120 | }
|
---|
| 121 | #endif // NRT_STACK_SIZE
|
---|
[2] | 122 |
|
---|
[15] | 123 | if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0) {
|
---|
| 124 | self->Err("Error pthread_attr_setinheritsched\n");
|
---|
| 125 | }
|
---|
[2] | 126 |
|
---|
[15] | 127 | if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) {
|
---|
| 128 | self->Err("Error pthread_attr_setschedpolicy\n");
|
---|
| 129 | }
|
---|
[2] | 130 |
|
---|
[15] | 131 | struct sched_param parm;
|
---|
| 132 | parm.sched_priority = priority;
|
---|
| 133 | if (pthread_attr_setschedparam(&attr, &parm) != 0) {
|
---|
| 134 | self->Err("Error pthread_attr_setschedparam\n");
|
---|
| 135 | }
|
---|
[2] | 136 |
|
---|
[15] | 137 | next_time = GetTime() + period;
|
---|
[2] | 138 |
|
---|
[15] | 139 | if (pthread_create(&task_nrt, &attr, main_nrt, (void *)this) != 0) {
|
---|
| 140 | self->Err("pthread_create error\n");
|
---|
| 141 | }
|
---|
[2] | 142 |
|
---|
[15] | 143 | if (pthread_attr_destroy(&attr) != 0) {
|
---|
| 144 | self->Err("Error pthread_attr_destroy\n");
|
---|
| 145 | }
|
---|
[2] | 146 |
|
---|
| 147 | #endif //__XENO__
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | #ifdef __XENO__
|
---|
[15] | 151 | void Thread_impl::main_rt(void *arg) {
|
---|
| 152 | Thread_impl *caller = (Thread_impl *)arg;
|
---|
[2] | 153 |
|
---|
[15] | 154 | caller->self->Run();
|
---|
[2] | 155 |
|
---|
[15] | 156 | caller->PrintStats();
|
---|
[2] | 157 | }
|
---|
| 158 | #else
|
---|
[38] | 159 | void* Thread_impl::main_nrt(void * arg)
|
---|
| 160 | {
|
---|
| 161 | Thread_impl *caller = (Thread_impl*)arg;
|
---|
| 162 | /*string th_name=getFrameworkManager()->ObjectName()+ "-" + caller->self->ObjectName();
|
---|
| 163 | caller->self->Info("pthread '%s' created with TID %x\n",th_name.c_str(),(pid_t)syscall(SYS_gettid));*/
|
---|
[2] | 164 |
|
---|
[15] | 165 | caller->self->Run();
|
---|
[2] | 166 |
|
---|
[15] | 167 | caller->PrintStats();
|
---|
[2] | 168 |
|
---|
[15] | 169 | pthread_exit(0);
|
---|
[2] | 170 | }
|
---|
| 171 | #endif //__XENO__
|
---|
| 172 |
|
---|
| 173 | void Thread_impl::SetPeriodUS(uint32_t period) {
|
---|
[15] | 174 | if (period == 0) {
|
---|
| 175 | self->Err("Period must be>0\n");
|
---|
| 176 | return;
|
---|
| 177 | }
|
---|
[2] | 178 |
|
---|
| 179 | #ifdef __XENO__
|
---|
[15] | 180 | int status = rt_task_set_periodic(&task_rt, TM_NOW, period * 1000);
|
---|
[133] | 181 | if (status != 0) {
|
---|
| 182 | char errorMsg[256];
|
---|
| 183 | self->Err("Error rt_task_set_periodic %s\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 184 | }
|
---|
[2] | 185 | #else
|
---|
[15] | 186 | next_time -= period;
|
---|
| 187 | next_time += period * 1000;
|
---|
[2] | 188 | #endif
|
---|
[15] | 189 | this->period = period * 1000;
|
---|
| 190 | period_set = true;
|
---|
[2] | 191 | }
|
---|
| 192 |
|
---|
[15] | 193 | uint32_t Thread_impl::GetPeriodUS() const { return this->period / 1000; }
|
---|
[2] | 194 |
|
---|
| 195 | void Thread_impl::SetPeriodMS(uint32_t period) {
|
---|
[15] | 196 | if (period == 0) {
|
---|
| 197 | self->Err("Period must be>0\n");
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
[2] | 200 |
|
---|
| 201 | #ifdef __XENO__
|
---|
[15] | 202 | int status = rt_task_set_periodic(&task_rt, TM_NOW, period * 1000 * 1000);
|
---|
[133] | 203 | if (status != 0) {
|
---|
| 204 | char errorMsg[256];
|
---|
| 205 | self->Err("Error rt_task_set_periodic %s\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 206 | }
|
---|
[2] | 207 | #else
|
---|
[15] | 208 | next_time -= period;
|
---|
| 209 | next_time += period * 1000 * 1000;
|
---|
[2] | 210 | #endif
|
---|
[15] | 211 | this->period = period * 1000 * 1000;
|
---|
| 212 | period_set = true;
|
---|
[2] | 213 | }
|
---|
| 214 |
|
---|
[15] | 215 | uint32_t Thread_impl::GetPeriodMS() const { return this->period / 1000 / 1000; }
|
---|
[2] | 216 |
|
---|
[15] | 217 | void Thread_impl::WaitPeriod(void) {
|
---|
| 218 | if (period_set == false) {
|
---|
| 219 | self->Err("Period must be set befaore calling this method\n");
|
---|
| 220 | return;
|
---|
| 221 | }
|
---|
[2] | 222 | #ifdef __XENO__
|
---|
[15] | 223 | unsigned long overruns_r;
|
---|
| 224 | int status = rt_task_wait_period(&overruns_r);
|
---|
[133] | 225 | if (status != 0) {
|
---|
| 226 | char errorMsg[256];
|
---|
| 227 | self->Err("Error rt_task_wait_period %s\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 228 | }
|
---|
| 229 | if (status == -ETIMEDOUT) {
|
---|
[15] | 230 | self->Err("overrun: %lld\n", overruns_r);
|
---|
[133] | 231 | }
|
---|
[2] | 232 | #else
|
---|
[15] | 233 | self->SleepUntil(next_time);
|
---|
| 234 | next_time += period;
|
---|
[2] | 235 | #endif
|
---|
[186] | 236 | ComputeLatency(GetTime());
|
---|
[2] | 237 | }
|
---|
| 238 |
|
---|
| 239 | void Thread_impl::Suspend(void) {
|
---|
[15] | 240 | if (isRunning == false) {
|
---|
| 241 | self->Err("thread is not started\n");
|
---|
| 242 | return;
|
---|
| 243 | }
|
---|
[2] | 244 |
|
---|
[15] | 245 | cond->GetMutex(), is_suspended = true;
|
---|
| 246 | cond->CondWait();
|
---|
| 247 | is_suspended = false;
|
---|
| 248 | cond->ReleaseMutex();
|
---|
[2] | 249 | }
|
---|
| 250 |
|
---|
| 251 | bool Thread_impl::SuspendUntil(Time date) {
|
---|
[15] | 252 | if (isRunning == false) {
|
---|
| 253 | self->Err("thread is not started\n");
|
---|
| 254 | return false;
|
---|
| 255 | }
|
---|
[2] | 256 |
|
---|
[15] | 257 | cond->GetMutex(), is_suspended = true;
|
---|
| 258 | bool success = cond->CondWaitUntil(date);
|
---|
| 259 | is_suspended = false;
|
---|
| 260 | cond->ReleaseMutex();
|
---|
| 261 | return success;
|
---|
[2] | 262 | }
|
---|
| 263 |
|
---|
[15] | 264 | bool Thread_impl::IsSuspended(void) {
|
---|
| 265 | bool result;
|
---|
[2] | 266 |
|
---|
[15] | 267 | cond->GetMutex();
|
---|
| 268 | result = is_suspended;
|
---|
| 269 | cond->ReleaseMutex();
|
---|
[2] | 270 |
|
---|
[15] | 271 | return result;
|
---|
[2] | 272 | }
|
---|
| 273 |
|
---|
[15] | 274 | void Thread_impl::Resume(void) {
|
---|
| 275 | if (isRunning == false) {
|
---|
| 276 | self->Err("thread is not started\n");
|
---|
| 277 | return;
|
---|
| 278 | }
|
---|
[2] | 279 |
|
---|
[15] | 280 | cond->GetMutex();
|
---|
| 281 | if (is_suspended == true) {
|
---|
| 282 | cond->CondSignal();
|
---|
| 283 | } else {
|
---|
| 284 | self->Err("thread is not suspended\n");
|
---|
| 285 | }
|
---|
| 286 | cond->ReleaseMutex();
|
---|
[2] | 287 | }
|
---|
| 288 |
|
---|
[15] | 289 | int Thread_impl::WaitUpdate(const IODevice *device) {
|
---|
| 290 | int status = 0;
|
---|
[2] | 291 |
|
---|
[15] | 292 | if (IsSuspended() == true) {
|
---|
| 293 | self->Err("thread is already supended\n");
|
---|
| 294 | status = -1;
|
---|
| 295 | } else {
|
---|
| 296 | cond->GetMutex();
|
---|
| 297 |
|
---|
| 298 | if (device->pimpl_->SetToWake(self) == 0) {
|
---|
| 299 | is_suspended = true;
|
---|
| 300 | cond->CondWait();
|
---|
| 301 | is_suspended = false;
|
---|
| 302 | } else {
|
---|
| 303 | self->Err("%s is already waiting an update\n",
|
---|
| 304 | device->ObjectName().c_str());
|
---|
| 305 | status = -1;
|
---|
[2] | 306 | }
|
---|
| 307 |
|
---|
[15] | 308 | cond->ReleaseMutex();
|
---|
| 309 | }
|
---|
[2] | 310 |
|
---|
[15] | 311 | return status;
|
---|
[2] | 312 | }
|
---|
| 313 |
|
---|
[15] | 314 | void Thread_impl::PrintStats(void) {
|
---|
[2] | 315 | #ifdef __XENO__
|
---|
[15] | 316 | RT_TASK_INFO info;
|
---|
[2] | 317 |
|
---|
[15] | 318 | int status = rt_task_inquire(NULL, &info);
|
---|
| 319 | if (status != 0) {
|
---|
[133] | 320 | char errorMsg[256];
|
---|
| 321 | self->Err("Error rt_task_inquire %s\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 322 | } else
|
---|
[2] | 323 | #endif
|
---|
[15] | 324 | {
|
---|
[2] | 325 | #ifndef __XENO__
|
---|
[186] | 326 | if(last!=0)
|
---|
[2] | 327 | #endif
|
---|
[15] | 328 | { Printf("Thread::%s :\n", self->ObjectName().c_str()); }
|
---|
[2] | 329 | #ifdef __XENO__
|
---|
[15] | 330 | Printf(" number of context switches: %i\n", info.ctxswitches);
|
---|
| 331 | Printf(" number of primary->secondary mode switch: %i\n",
|
---|
| 332 | info.modeswitches);
|
---|
| 333 | // printf("number of page faults: %i\n",info.pagefaults);
|
---|
| 334 | Printf(" execution time (ms) in primary mode: %lld\n",
|
---|
| 335 | info.exectime / 1000000);
|
---|
[2] | 336 | #else
|
---|
| 337 | /*
|
---|
| 338 | struct rusage r_usage;
|
---|
| 339 | getrusage(RUSAGE_THREAD,&r_usage);
|
---|
| 340 | printf(" memory usage = %ld\n",r_usage.ru_maxrss);
|
---|
[15] | 341 | printf("RUSAGE :ru_utime => %lld [sec] : %lld [usec], :ru_stime => %lld
|
---|
| 342 | [sec] : %lld [usec] \n",
|
---|
[2] | 343 | (int64_t)r_usage.ru_utime.tv_sec, (int64_t)r_usage.ru_utime.tv_usec,
|
---|
[15] | 344 | (int64_t)r_usage.ru_stime.tv_sec,
|
---|
| 345 | (int64_t)r_usage.ru_stime.tv_usec);*/
|
---|
[2] | 346 | #endif
|
---|
[15] | 347 | if (last != 0) {
|
---|
[186] | 348 | Printf(" min latency (ns): %lld\n", min_latency);
|
---|
| 349 | Printf(" max latency (ns): %lld\n", max_latency);
|
---|
| 350 | Printf(" latency moy (ns): %lld\n", mean_latency / cpt);
|
---|
[15] | 351 | Printf(" itertions: %lld\n", cpt);
|
---|
[2] | 352 | }
|
---|
[15] | 353 | }
|
---|
[2] | 354 | }
|
---|
| 355 |
|
---|
[15] | 356 | void Thread_impl::Join(void) {
|
---|
| 357 | if (isRunning == true) {
|
---|
| 358 | int status;
|
---|
[133] | 359 | char errorMsg[256];
|
---|
[2] | 360 |
|
---|
| 361 | #ifdef __XENO__
|
---|
[15] | 362 | status = rt_task_join(&task_rt);
|
---|
[2] | 363 | #else
|
---|
[15] | 364 | status = pthread_join(task_nrt, NULL);
|
---|
[2] | 365 | #endif
|
---|
[15] | 366 | if (status != 0)
|
---|
[133] | 367 | self->Err("error %s\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 368 | isRunning = false;
|
---|
| 369 | }
|
---|
[2] | 370 | }
|
---|
| 371 |
|
---|
[186] | 372 | void Thread_impl::ComputeLatency(Time time) {
|
---|
[15] | 373 | Time diff, delta;
|
---|
| 374 | diff = time - last;
|
---|
[2] | 375 |
|
---|
[15] | 376 | if (diff >= period) {
|
---|
| 377 | delta = diff - period;
|
---|
| 378 | } else {
|
---|
| 379 | delta = period - diff;
|
---|
| 380 | }
|
---|
| 381 | // if(delta==0) rt_printf("%lld %lld\n",time,last);
|
---|
| 382 | last = time;
|
---|
| 383 | if (diff == time)
|
---|
| 384 | return;
|
---|
[2] | 385 |
|
---|
[186] | 386 | if (delta > max_latency)
|
---|
| 387 | max_latency = delta;
|
---|
| 388 | if (delta < min_latency)
|
---|
| 389 | min_latency = delta;
|
---|
| 390 | mean_latency += delta;
|
---|
[15] | 391 | cpt++;
|
---|
[2] | 392 |
|
---|
[186] | 393 | // Printf("Thread::%s latency moy (ns):
|
---|
| 394 | // %lld\n",self->ObjectName().c_str(),mean_latency/cpt);
|
---|
[2] | 395 | }
|
---|
| 396 |
|
---|
[15] | 397 | void Thread_impl::SafeStop(void) {
|
---|
| 398 | tobestopped = true;
|
---|
| 399 | if (IsSuspended())
|
---|
| 400 | Resume();
|
---|
[2] | 401 | }
|
---|
| 402 |
|
---|
[15] | 403 | bool Thread_impl::ToBeStopped(void) { return tobestopped; }
|
---|