File: sb_cpu.c

package info (click to toggle)
sysbench 0.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,204 kB
  • ctags: 1,143
  • sloc: ansic: 9,139; sh: 8,933; xml: 1,610; makefile: 114
file content (170 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (3)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef _WIN32
# include "sb_win.h"
#endif

#ifdef HAVE_MATH_H
# include <math.h>
#endif

#include "sysbench.h"

/* CPU test arguments */
static sb_arg_t cpu_args[] =
{
  {"cpu-max-prime", "upper limit for primes generator", SB_ARG_TYPE_INT, "10000"},
  {NULL, NULL, SB_ARG_TYPE_NULL, NULL}
};

/* CPU test operations */
static int cpu_init(void);
static void cpu_print_mode(void);
static sb_request_t cpu_get_request(int);
static int cpu_execute_request(sb_request_t *, int);
static void cpu_print_stats(void);
static int cpu_done(void);

static sb_test_t cpu_test =
{
  "cpu",
  "CPU performance test",
  {
    cpu_init,
    NULL,
    NULL,
    cpu_print_mode,
    cpu_get_request,
    cpu_execute_request,
    cpu_print_stats,
    NULL,
    NULL,
    cpu_done
  },
  {
    NULL,
    NULL,
    NULL,
    NULL
  },
  cpu_args,
  {NULL, NULL}
};

/* Upper limit for primes */
static unsigned int    max_prime;
/* Request counter */
static unsigned int    req_performed;
/* Counter mutex */
static pthread_mutex_t request_mutex;

int register_test_cpu(sb_list_t * tests)
{
  SB_LIST_ADD_TAIL(&cpu_test.listitem, tests);

  return 0;
}

int cpu_init(void)
{
  max_prime = sb_get_value_int("cpu-max-prime");
  if (max_prime <= 0)
  {
    log_text(LOG_FATAL, "Invalid value of cpu-max-prime: %d.", max_prime);
    return 1;
  }

  req_performed = 0;

  pthread_mutex_init(&request_mutex, NULL);
  
  return 0;
}


sb_request_t cpu_get_request(int tid)
{
  sb_request_t req;

  (void)tid; /* unused */
  
  if (req_performed >= sb_globals.max_requests)
  {
    req.type = SB_REQ_TYPE_NULL;
    return req;
  }
  req.type = SB_REQ_TYPE_CPU;
  pthread_mutex_lock(&request_mutex);
  req_performed++;
  pthread_mutex_unlock(&request_mutex);

  return req;
}

int cpu_execute_request(sb_request_t *r, int thread_id)
{
  unsigned long long c;
  unsigned long long l,t;
  unsigned long long n=0;
  log_msg_t           msg;
  log_msg_oper_t      op_msg;
  
  (void)r; /* unused */

  /* Prepare log message */
  msg.type = LOG_MSG_TYPE_OPER;
  msg.data = &op_msg;
  
  /* So far we're using very simple test prime number tests in 64bit */
  LOG_EVENT_START(msg, thread_id);

  for(c=3; c < max_prime; c++)  
  {
    t = sqrt(c);
    for(l = 2; l <= t; l++)
      if (c % l == 0)
        break;
    if (l > t )
      n++; 
  }

  LOG_EVENT_STOP(msg, thread_id);

  return 0;
}

void cpu_print_mode(void)
{
  log_text(LOG_INFO, "Doing CPU performance benchmark\n");  
}

void cpu_print_stats(void)
{
  log_text(LOG_NOTICE, "Maximum prime number checked in CPU test: %d\n",
           max_prime);
}

int cpu_done(void)
{
  pthread_mutex_destroy(&request_mutex);

  return 0;
}