summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/RtEC/test_driver/cpuload.cpp
blob: 79912ef36bd8750f1e08555ab7c0aae83caa1358 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// $Id$

#include "cpuload.h"

#include <stdio.h>
#include <sys/time.h>
#include <signal.h>

namespace
{
  int calibration_time = 1000; //msec
  unsigned long loop_count=0;
  int calibration_done = 0;
  volatile int done = 0;
  unsigned long per_iter_loop_count;
  unsigned long calibrated_loop_count;
}

static void
handle_signal (int signum)
{
  switch (signum)
    {
    case SIGVTALRM:
      //printf ("VTALRM recvd\n");
      per_iter_loop_count = loop_count;
      done = 1;
      break;

    default:
      printf ("signal %d unexpected\n", signum);
    }
}

int is_prime (const unsigned long n,
              const unsigned long min_factor,
              const unsigned long max_factor)
{
  if (n > 3)
    for (unsigned long factor = min_factor;
         factor <= max_factor;
         ++factor)
      if (n / factor * factor == n)
        return factor;

  return 0;
}

void do_computation (unsigned long& loop_count)
{
  static unsigned long prime_number = 961989999;
  ++loop_count;

  is_prime (prime_number, 2, prime_number/2);
}

unsigned long do_calibrate ()
{
  per_iter_loop_count = 0;
  loop_count = 0;

  signal (SIGVTALRM, handle_signal);
  itimerval timerval;

  timerval.it_value.tv_sec = 0;
  timerval.it_value.tv_usec = calibration_time*1000;

  if (setitimer(ITIMER_VIRTUAL, &timerval, 0) != 0)
    {
      printf("setitimer failed\n");
    }

  for (;!done;)
    {
      do_computation (loop_count);
    }
  //printf("iter done\n");
  done = 0;
  return per_iter_loop_count;
}

void CPULoad::run (struct timeval& tv)
{
  unsigned long loops=0;
  unsigned long iters = (unsigned long)
    (((float)calibrated_loop_count/(float)(calibration_time*1000)) *
     (tv.tv_usec + tv.tv_sec*1000000));
  iters = iters + 1;

  // printf("tv = %lu sec %lu usec, iters = %lu\n",
  //     tv.tv_sec, tv.tv_usec, iters);

  for (;iters>0;--iters)
    {
      //loops is really not used here. It is used only during calibration
      do_computation (loops);
    }
}

void CPULoad::calibrate (int num_iters)
{
  if (calibration_done)
    return;

  printf("calibrating ...\n");
  for (int i=0; i<num_iters; ++i)
    {
      unsigned long tmp = do_calibrate ();
      //printf("calibration ended calibrated (%dmsec) loop_count = %lu\n",
      //     calibration_time, tmp);

      if (calibrated_loop_count == 0)
        {
          calibrated_loop_count = tmp;
        }
      else
        {
          //calibrated_loop_count = (calibrated_loop_count + tmp)/2;
          if (calibrated_loop_count < tmp)
            calibrated_loop_count = tmp;
        }
      loop_count = 0;
    }

  calibration_done = 1;
  signal (SIGVTALRM, SIG_DFL);
  //printf("calibration done\n");
}