f6c49d38 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
c1d15f73 |
4 | #include <fcntl.h> |
f6c49d38 |
5 | #include <sys/types.h> |
6 | #include <sys/ipc.h> |
7 | #include <sys/shm.h> |
c1d15f73 |
8 | #include <sys/mman.h> |
f6c49d38 |
9 | |
10 | #include <pico/pico_int.h> |
11 | |
c1d15f73 |
12 | int rc_mem[pp_total_points]; |
13 | |
f6c49d38 |
14 | struct pp_counters *pp_counters; |
c1d15f73 |
15 | int *refcounts = rc_mem; |
f6c49d38 |
16 | static int shmemid; |
17 | |
c1d15f73 |
18 | static unsigned long devMem; |
19 | volatile unsigned long *gp2x_memregl; |
20 | volatile unsigned short *gp2x_memregs; |
21 | |
f6c49d38 |
22 | void pprof_init(void) |
23 | { |
24 | int this_is_new_shmem = 1; |
25 | key_t shmemkey; |
26 | void *shmem; |
27 | |
c1d15f73 |
28 | #if 0 |
29 | devMem = open("/dev/mem", O_RDWR); |
30 | if (devMem == -1) |
31 | { |
32 | perror("pprof: open failed"); |
33 | return; |
34 | } |
35 | gp2x_memregl = (unsigned long *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, devMem, 0xc0000000); |
36 | if (gp2x_memregl == (unsigned long *)-1) |
37 | { |
38 | perror("pprof: mmap failed"); |
39 | return; |
40 | } |
41 | gp2x_memregs = (unsigned short *)gp2x_memregl; |
42 | #endif |
43 | |
f6c49d38 |
44 | #ifndef PPROF_TOOL |
45 | unsigned int tmp = pprof_get_one(); |
46 | printf("pprof: measured diff is %u\n", pprof_get_one() - tmp); |
47 | #endif |
48 | |
49 | shmemkey = ftok(".", 0x02ABC32E); |
50 | if (shmemkey == -1) |
51 | { |
52 | perror("pprof: ftok failed"); |
53 | return; |
54 | } |
55 | |
c1d15f73 |
56 | //#ifndef PPROF_TOOL |
f6c49d38 |
57 | shmemid = shmget(shmemkey, sizeof(*pp_counters), |
58 | IPC_CREAT | IPC_EXCL | 0644); |
59 | if (shmemid == -1) |
c1d15f73 |
60 | //#endif |
f6c49d38 |
61 | { |
62 | shmemid = shmget(shmemkey, sizeof(*pp_counters), |
63 | 0644); |
64 | if (shmemid == -1) |
65 | { |
66 | perror("pprof: shmget failed"); |
67 | return; |
68 | } |
69 | this_is_new_shmem = 0; |
70 | } |
71 | |
72 | shmem = shmat(shmemid, NULL, 0); |
73 | if (shmem == (void *)-1) |
74 | { |
75 | perror("pprof: shmat failed"); |
76 | return; |
77 | } |
78 | |
79 | pp_counters = shmem; |
80 | if (this_is_new_shmem) { |
81 | memset(pp_counters, 0, sizeof(*pp_counters)); |
82 | printf("pprof: pp_counters cleared.\n"); |
83 | } |
84 | } |
85 | |
86 | void pprof_finish(void) |
87 | { |
88 | shmdt(pp_counters); |
89 | shmctl(shmemid, IPC_RMID, NULL); |
90 | } |
91 | |
92 | #ifdef PPROF_TOOL |
93 | |
94 | #define IT(n) { pp_##n, #n } |
95 | static const struct { |
96 | enum pprof_points pp; |
97 | const char *name; |
98 | } pp_tab[] = { |
99 | IT(main), |
100 | IT(frame), |
101 | IT(draw), |
102 | IT(sound), |
103 | IT(m68k), |
c1d15f73 |
104 | IT(s68k), |
105 | IT(mem68), |
f6c49d38 |
106 | IT(z80), |
107 | IT(msh2), |
108 | IT(ssh2), |
c1d15f73 |
109 | IT(memsh), |
f6c49d38 |
110 | IT(dummy), |
111 | }; |
112 | |
113 | int main(int argc, char *argv[]) |
114 | { |
c1d15f73 |
115 | pp_type old[pp_total_points], new[pp_total_points]; |
f6c49d38 |
116 | int base = 0; |
117 | int l, i; |
118 | |
119 | pprof_init(); |
120 | if (pp_counters == NULL) |
121 | return 1; |
122 | |
123 | if (argc >= 2) |
124 | base = atoi(argv[1]); |
125 | |
126 | memset(old, 0, sizeof(old)); |
127 | for (l = 0; ; l++) |
128 | { |
129 | if ((l & 0x1f) == 0) { |
130 | for (i = 0; i < ARRAY_SIZE(pp_tab); i++) |
131 | printf("%6s ", pp_tab[i].name); |
132 | printf("\n"); |
133 | } |
134 | |
135 | memcpy(new, pp_counters->counter, sizeof(new)); |
136 | for (i = 0; i < ARRAY_SIZE(pp_tab); i++) |
137 | { |
c1d15f73 |
138 | pp_type idiff = new[i] - old[i]; |
139 | pp_type bdiff = (new[base] - old[base]) | 1; |
f6c49d38 |
140 | printf("%6.2f ", (double)idiff * 100.0 / bdiff); |
141 | } |
142 | printf("\n"); |
c1d15f73 |
143 | fflush(stdout); |
f6c49d38 |
144 | memcpy(old, new, sizeof(old)); |
145 | |
146 | if (argc < 3) |
147 | break; |
148 | usleep(atoi(argv[2])); |
149 | } |
150 | |
151 | return 0; |
152 | } |
153 | |
154 | #endif // PPROF_TOOL |
155 | |