-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatTimers.c
122 lines (100 loc) · 3.47 KB
/
FlatTimers.c
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
/*
* This file is part of the pebil project.
*
* Copyright (c) 2010, University of California Regents
* All rights reserved.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <InstrumentationCommon.h>
#define NUM_PRINT 10
int64_t ticksPerSecond;
struct funcInfo* funcInfos = NULL;
int32_t numberOfFunctions = 0;
char** functionNames = NULL;
struct funcInfo
{
uint64_t count;
uint64_t timer_start;
uint64_t timer_total;
uint64_t idx;
};
void printFunctionInfo(int i){
#ifdef EXCLUDE_TIMER
PRINT_INSTR(stdout, "%s: %lld executions", functionNames[funcInfos[i].idx], funcInfos[i].count);
#else
PRINT_INSTR(stdout, "%s: %lld executions, %.6f seconds", functionNames[funcInfos[i].idx], funcInfos[i].count, ((double)((double)funcInfos[i].timer_total/(double)ticksPerSecond)));
#endif
}
int compareFuncInfos(const void* f1, const void* f2){
struct funcInfo* func1 = (struct funcInfo*)f1;
struct funcInfo* func2 = (struct funcInfo*)f2;
if (func1->timer_total > func2->timer_total){
return -1;
} else if (func1->timer_total < func2->timer_total){
return 1;
}
return 0;
}
int32_t program_entry(int32_t* numFunctions, char** funcNames){
int i;
numberOfFunctions = *numFunctions;
functionNames = funcNames;
ticksPerSecond = CLOCK_RATE_HZ;
PRINT_INSTR(stdout, "%lld ticks per second", ticksPerSecond);
if (!funcInfos){
PRINT_DEBUG("allocating %d funcInfos", numberOfFunctions);
funcInfos = malloc(sizeof(struct funcInfo) * numberOfFunctions);
bzero(funcInfos, sizeof(struct funcInfo) * numberOfFunctions);
}
for (i = 0; i < numberOfFunctions; i++){
funcInfos[i].idx = i;
}
assert(funcInfos);
}
int32_t program_exit(){
PRINT_INSTR(stdout, "Printing the %d most time-consuming function calls", NUM_PRINT);
int32_t i, j;
qsort((void*)funcInfos, numberOfFunctions, sizeof(struct funcInfo), compareFuncInfos);
for (i = 0; i < numberOfFunctions; i++){
if (funcInfos[i].count){
if (i < NUM_PRINT){
printFunctionInfo(i);
}
}
}
free(funcInfos);
}
int32_t function_entry(int64_t* functionIndex){
// PRINT_INSTR(stdout, "%s entr", functionNames[*functionIndex]);
int64_t currentRecord = *functionIndex;
#ifdef EXCLUDE_TIMER
funcInfos[currentRecord].timer_start = 0;
#else
funcInfos[currentRecord].timer_start = read_timestamp_counter();
#endif
}
int32_t function_exit(int64_t* functionIndex){
uint64_t tstop;
#ifdef EXCLUDE_TIMER
tstop = 1;
#else
tstop = read_timestamp_counter();
#endif
int64_t currentRecord = *functionIndex;
int64_t tadd = tstop - funcInfos[currentRecord].timer_start;
// PRINT_INSTR(stdout, "%s exit -- add %lld", functionNames[*functionIndex], tadd);
funcInfos[currentRecord].count++;
funcInfos[currentRecord].timer_total += tadd;
}