6c7c0ff9fede1520a36fab8f34e700f95f3f0218
[picodrive.git] / platform / linux / pprof.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/types.h>
6 #include <sys/ipc.h>
7 #include <sys/shm.h>
8 #include <sys/mman.h>
9
10 #include <pico/pico_int.h>
11
12 int rc_mem[pp_total_points];
13
14 struct pp_counters *pp_counters;
15 int *refcounts = rc_mem;
16 static int shmemid;
17
18 static unsigned long devMem;
19 volatile unsigned long *gp2x_memregl;
20 volatile unsigned short *gp2x_memregs;
21
22 void pprof_init(void)
23 {
24         int this_is_new_shmem = 1;
25         key_t shmemkey;
26         void *shmem;
27
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
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
56 //#ifndef PPROF_TOOL
57         shmemid = shmget(shmemkey, sizeof(*pp_counters),
58                 IPC_CREAT | IPC_EXCL | 0644);
59         if (shmemid == -1)
60 //#endif
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),
104         IT(s68k),
105         IT(mem68),
106         IT(z80),
107         IT(msh2),
108         IT(ssh2),
109         IT(memsh),
110         IT(dummy),
111 };
112
113 int main(int argc, char *argv[])
114 {
115         pp_type old[pp_total_points], new[pp_total_points];
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                 {
138                         pp_type idiff = new[i] - old[i];
139                         pp_type bdiff = (new[base] - old[base]) | 1;
140                         printf("%6.2f ", (double)idiff * 100.0 / bdiff);
141                 }
142                 printf("\n");
143                 fflush(stdout);
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