1 |
dpavlin |
22 |
/* |
2 |
dpavlin |
24 |
* Copyright (C) 2005-2006 Anders Gavare. All rights reserved. |
3 |
dpavlin |
22 |
* |
4 |
|
|
* Redistribution and use in source and binary forms, with or without |
5 |
|
|
* modification, are permitted provided that the following conditions are met: |
6 |
|
|
* |
7 |
|
|
* 1. Redistributions of source code must retain the above copyright |
8 |
|
|
* notice, this list of conditions and the following disclaimer. |
9 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer in the |
11 |
|
|
* documentation and/or other materials provided with the distribution. |
12 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
13 |
|
|
* derived from this software without specific prior written permission. |
14 |
|
|
* |
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
|
* SUCH DAMAGE. |
26 |
|
|
* |
27 |
|
|
* |
28 |
dpavlin |
24 |
* $Id: ic_statistics.c,v 1.3 2006/03/30 19:36:03 debug Exp $ |
29 |
dpavlin |
22 |
* |
30 |
|
|
* This program is not optimized for speed, but it should work. |
31 |
|
|
* |
32 |
|
|
* for a in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do \ |
33 |
|
|
* ./ic_statistics $a |sort -n > statistics.$a.txt; done |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
#include <stdio.h> |
37 |
|
|
#include <stdlib.h> |
38 |
|
|
#include <string.h> |
39 |
|
|
#include <sys/types.h> |
40 |
dpavlin |
24 |
#include <inttypes.h> |
41 |
dpavlin |
22 |
|
42 |
|
|
|
43 |
|
|
struct entry { |
44 |
|
|
void **ptrs; |
45 |
|
|
long long count; |
46 |
|
|
}; |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
struct entry *entries = NULL; |
50 |
|
|
int n_entries = 0; |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
size_t *cache_s = NULL; |
54 |
|
|
char **cache_symbol = NULL; |
55 |
|
|
int n_cached_symbols = 0; |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
char *cached_size_t_to_symbol(size_t s) |
59 |
|
|
{ |
60 |
|
|
int i = 0; |
61 |
|
|
FILE *q; |
62 |
|
|
char tmp[200]; |
63 |
|
|
char *urk, *urk2; |
64 |
|
|
|
65 |
|
|
while (i < n_cached_symbols) { |
66 |
|
|
if (cache_s[i] == s) |
67 |
|
|
return cache_symbol[i]; |
68 |
|
|
i++; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
n_cached_symbols ++; |
72 |
|
|
cache_s = realloc(cache_s, sizeof(size_t) * n_cached_symbols); |
73 |
|
|
cache_symbol = realloc(cache_symbol, sizeof(char *) * n_cached_symbols); |
74 |
|
|
cache_s[n_cached_symbols - 1] = s; |
75 |
|
|
|
76 |
|
|
snprintf(tmp, sizeof(tmp), "nm ../gxemul | grep " |
77 |
dpavlin |
24 |
"%"PRIx64, (uint64_t)s); |
78 |
dpavlin |
22 |
q = popen(tmp, "r"); |
79 |
|
|
if (q == NULL) { |
80 |
|
|
perror("popen()"); |
81 |
|
|
exit(1); |
82 |
|
|
} |
83 |
|
|
fgets(tmp, sizeof(tmp), q); |
84 |
|
|
pclose(q); |
85 |
|
|
|
86 |
|
|
while (tmp[0] && (tmp[strlen(tmp)-1] == '\n' || |
87 |
|
|
tmp[strlen(tmp)-1] == '\r')) |
88 |
|
|
tmp[strlen(tmp)-1] = '\0'; |
89 |
|
|
|
90 |
|
|
urk = strrchr(tmp, ' '); |
91 |
|
|
if (urk == NULL) |
92 |
|
|
urk = tmp; |
93 |
|
|
else |
94 |
|
|
urk ++; |
95 |
|
|
|
96 |
|
|
urk2 = strstr(urk, "instr_"); |
97 |
|
|
if (urk2 != NULL) |
98 |
|
|
urk = urk2 + 6; |
99 |
|
|
|
100 |
|
|
cache_symbol[n_cached_symbols - 1] = strdup(urk); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
void print_all(int n) |
105 |
|
|
{ |
106 |
|
|
int i = 0; |
107 |
|
|
while (i < n_entries) { |
108 |
|
|
void **pp = entries[i].ptrs; |
109 |
|
|
int j = 0; |
110 |
|
|
|
111 |
|
|
printf("%lli\t", (long long)entries[i].count); |
112 |
|
|
while (j < n) { |
113 |
|
|
size_t s = (size_t)pp[j]; |
114 |
|
|
|
115 |
|
|
if (j > 0) |
116 |
|
|
printf(", "); |
117 |
|
|
printf("%s", cached_size_t_to_symbol(s)); |
118 |
|
|
|
119 |
|
|
j++; |
120 |
|
|
} |
121 |
|
|
printf("\n"); |
122 |
|
|
|
123 |
|
|
i++; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
void add_count(void **pointers, int n) |
129 |
|
|
{ |
130 |
|
|
int i = 0; |
131 |
|
|
|
132 |
|
|
/* Scan all existing entries. */ |
133 |
|
|
while (i < n_entries) { |
134 |
|
|
if (memcmp(pointers, entries[i].ptrs, sizeof(void*) * n) == 0) { |
135 |
|
|
entries[i].count ++; |
136 |
|
|
return; |
137 |
|
|
} |
138 |
|
|
i++; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
/* Add new entry: */ |
142 |
|
|
n_entries ++; |
143 |
|
|
entries = realloc(entries, sizeof(struct entry) * n_entries); |
144 |
|
|
entries[n_entries-1].ptrs = malloc(sizeof(void *) * n); |
145 |
|
|
memcpy(entries[n_entries-1].ptrs, &pointers[0], n * sizeof(void *)); |
146 |
|
|
entries[n_entries-1].count = 1; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
void try_len(FILE *f, int len) |
151 |
|
|
{ |
152 |
|
|
void **pointers; |
153 |
|
|
off_t off; |
154 |
|
|
|
155 |
|
|
pointers = malloc(sizeof(void *) * len); |
156 |
|
|
|
157 |
|
|
fseek(f, 0, SEEK_END); |
158 |
|
|
off = ftello(f); |
159 |
|
|
|
160 |
|
|
fseek(f, 0, SEEK_SET); |
161 |
|
|
if (len > 1) |
162 |
|
|
fread(&pointers[1], sizeof(void *), len-1, f); |
163 |
|
|
|
164 |
|
|
while (!feof(f)) { |
165 |
|
|
static long long yo = 0; |
166 |
|
|
yo ++; |
167 |
|
|
if ((yo & 0xfffff) == 0) { |
168 |
|
|
fprintf(stderr, "[ len=%i, %i%% done ]\n", |
169 |
|
|
len, 100 * yo * sizeof(void *) / off); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
/* Make room for next pointer value: */ |
173 |
|
|
if (len > 1) |
174 |
|
|
memmove(&pointers[0], &pointers[1], |
175 |
|
|
(len-1) * sizeof(void *)); |
176 |
|
|
|
177 |
|
|
/* Read one pointer into pointers[len-1]: */ |
178 |
|
|
fread(&pointers[len-1], sizeof(void *), 1, f); |
179 |
|
|
|
180 |
|
|
add_count(&pointers[0], len); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
free(pointers); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
int main(int argc, char *argv[]) |
188 |
|
|
{ |
189 |
|
|
FILE *f; |
190 |
|
|
int len = 1; |
191 |
|
|
|
192 |
|
|
f = fopen("instruction_call_statistics.raw", "r"); |
193 |
|
|
if (f == NULL) { |
194 |
|
|
f = fopen("../instruction_call_statistics.raw", "r"); |
195 |
|
|
if (f == NULL) { |
196 |
|
|
perror("instruction_call_statistics.raw"); |
197 |
|
|
exit(1); |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
if (argc > 1) |
202 |
|
|
len = atoi(argv[1]); |
203 |
|
|
|
204 |
|
|
if (len < 1) { |
205 |
|
|
fprintf(stderr, "bad len\n"); |
206 |
|
|
exit(1); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
try_len(f, len); |
210 |
|
|
print_all(len); |
211 |
|
|
|
212 |
|
|
return 0; |
213 |
|
|
} |
214 |
|
|
|