summaryrefslogtreecommitdiff
path: root/TAO/tests/Cubit/TAO/MT_Cubit/Util_Thread.cpp
blob: 5bb97e47e64c160d3a445a8a1ad325d794ae06e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// $Id$

#include "Util_Thread.h"

ACE_RCSID(MT_Cubit, Util_Thread, "$Id$")

Util_Thread::Util_Thread (Task_State *ts,
                          ACE_Thread_Manager *thr_mgr)
  : ACE_MT (ACE_Task<ACE_NULL_SYNCH> (thr_mgr)),
    done_ (0),
    number_of_computations_ (0),
    ts_ (ts)
{
}

int
Util_Thread::svc (void)
{
  ACE_hthread_t thr_handle;
  ACE_Thread::self (thr_handle);
  int prio;

  // thr_getprio () on the current thread should never fail.
  ACE_OS::thr_getprio (thr_handle, prio);

  ACE_DEBUG ((LM_DEBUG,
              "(%t) Utilization Thread created with priority %d, "
              "waiting for threads to finish binding\n", prio));

  // this barrier synchronizes the utilization thread with
  // the client threads
  // i.e., the Util_thread should wait until all the
  // clients have finished binding, and only then
  // start measuring the utilization.
  this->ts_->barrier_->wait ();

  ACE_DEBUG ((LM_DEBUG,
              "(%t) )))))))) "
              "utilization test STARTED at %D\n"));

  this->ts_->utilization_task_started_ = 1;

  this->ts_->timer_.start ();

  this->run_computations ();

  this->ts_->timer_.stop ();

  ACE_DEBUG ((LM_DEBUG,
              "(%t) (((((((( " 
	      "utilization test ENDED at %D\n"));

  return 0;
}

u_long
Util_Thread::get_number_of_computations (void)
{
  return this->number_of_computations_;
}

// computation performed by the Utilization thread.  We need this in a
// separate function to get it's execution time.
//inline
void
Util_Thread::computation (void)
{
  // This is the number that the Util_Thread uses to check for
  // primality.
  const u_long CUBIT_PRIME_NUMBER = 509UL;

  // See if this number is prime.  2 and CUBIT_PRIME_NUMBER / 2 are
  // the recommended values for min_factor and max_factor, as
  // explained in ACE.h (is_prime).
  ACE::is_prime (CUBIT_PRIME_NUMBER,
                 2UL,
                 CUBIT_PRIME_NUMBER / 2);
}

// Perform repeated prime factor computations on an arbitrary number.
// And you thought your life was boring... :-)
int
Util_Thread::run_computations (void)
{
  while (this->done_ == 0)
    {
      // bound the number of computations, since we can potentially
      // block the machine if this thread never leaves the loop. 
      if (this->number_of_computations_ > (ts_->loop_count_ * 1000)) // magic number
	{
	  ACE_DEBUG ((LM_DEBUG,
		      "\t(%t) utilization test breaking loop so machine won't block.\n"));
	  break;
	}
      this->computation ();
      this->number_of_computations_ ++;
      //            ACE_OS::thr_yield (); // Shouldn't need this.  And I'm not sure
                            // if it really helps.

      if (ts_->utilization_task_started_ == 0)
	break;
    }

  return 0;
}