-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_counter_profile.cpp
71 lines (56 loc) · 1.49 KB
/
stat_counter_profile.cpp
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
#include "stat_counter.hpp"
#include "ticks_clock.hpp"
#include <tr1/functional>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
namespace{
using std::tr1::bind;
using base::Stat_Counter;
using base::TicksClock;
const int MEGA_REPEAT_TIME = 1000000;
static double getTotal(TicksClock::Ticks start,
TicksClock::Ticks end) {
double duration = end - start;
double time = duration / TicksClock::ticksPerSecond();
return time;
}
Stat_Counter *counter;
int thread_num;
pthread_t *threads;
void *test(void * thread_id){
//long tid = (long) thread_id;
counter->count_register_thread();
TicksClock::Ticks start = TicksClock::getTicks();
for(int i = 0 ; i < MEGA_REPEAT_TIME; i++){
counter->inc_count();
}
TicksClock::Ticks end = TicksClock::getTicks();
double duration = getTotal(start,end)*1e9/MEGA_REPEAT_TIME;
counter->count_unregister_thread();
printf("average:%f\n",duration);
pthread_exit((void*) thread_id);
}
} // end of namespace
int main(int argc, char *argv[]){
if(argc < 2){
thread_num = 8;
}
else thread_num = atoi(argv[1]);
counter = new Stat_Counter(thread_num);
threads = new pthread_t[thread_num];
int rc;
for(int i = 0 ; i < thread_num; i++){
rc = pthread_create(&threads[i],NULL, test, (void *)i);
if (rc) {
printf("ERROR; ");
exit(-1);
}
}
for(int i = 0 ; i < thread_num; i++){
pthread_join(threads[i], NULL);
}
printf("%d\n", counter->read_count());
delete []threads;
delete counter;
}