re-import all libretro code from it's fork
[picodrive.git] / platform / linux / pprof.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/ipc.h>
6 #include <sys/shm.h>
7
8 #include <pico/pico_int.h>
9
10 struct pp_counters *pp_counters;
11 static int shmemid;
12
13 void pprof_init(void)
14 {
15         int this_is_new_shmem = 1;
16         key_t shmemkey;
17         void *shmem;
18
19 #ifndef PPROF_TOOL
20         unsigned int tmp = pprof_get_one();
21         printf("pprof: measured diff is %u\n", pprof_get_one() - tmp);
22 #endif
23
24         shmemkey = ftok(".", 0x02ABC32E);
25         if (shmemkey == -1)
26         {
27                 perror("pprof: ftok failed");
28                 return;
29         }
30
31 #ifndef PPROF_TOOL
32         shmemid = shmget(shmemkey, sizeof(*pp_counters),
33                 IPC_CREAT | IPC_EXCL | 0644);
34         if (shmemid == -1)
35 #endif
36         {
37                 shmemid = shmget(shmemkey, sizeof(*pp_counters),
38                                 0644);
39                 if (shmemid == -1)
40                 {
41                         perror("pprof: shmget failed");
42                         return;
43                 }
44                 this_is_new_shmem = 0;
45         }
46
47         shmem = shmat(shmemid, NULL, 0);
48         if (shmem == (void *)-1)
49         {
50                 perror("pprof: shmat failed");
51                 return;
52         }
53
54         pp_counters = shmem;
55         if (this_is_new_shmem) {
56                 memset(pp_counters, 0, sizeof(*pp_counters));
57                 printf("pprof: pp_counters cleared.\n");
58         }
59 }
60
61 void pprof_finish(void)
62 {
63         shmdt(pp_counters);
64         shmctl(shmemid, IPC_RMID, NULL);
65 }
66
67 #ifdef PPROF_TOOL
68
69 #define IT(n) { pp_##n, #n }
70 static const struct {
71         enum pprof_points pp;
72         const char *name;
73 } pp_tab[] = {
74         IT(main),
75         IT(frame),
76         IT(draw),
77         IT(sound),
78         IT(m68k),
79         IT(z80),
80         IT(msh2),
81         IT(ssh2),
82         IT(dummy),
83 };
84
85 int main(int argc, char *argv[])
86 {
87         unsigned long long old[pp_total_points], new[pp_total_points];
88         int base = 0;
89         int l, i;
90
91         pprof_init();
92         if (pp_counters == NULL)
93                 return 1;
94
95         if (argc >= 2)
96                 base = atoi(argv[1]);
97
98         memset(old, 0, sizeof(old));
99         for (l = 0; ; l++)
100         {
101                 if ((l & 0x1f) == 0) {
102                         for (i = 0; i < ARRAY_SIZE(pp_tab); i++)
103                                 printf("%6s ", pp_tab[i].name);
104                         printf("\n");
105                 }
106
107                 memcpy(new, pp_counters->counter, sizeof(new));
108                 for (i = 0; i < ARRAY_SIZE(pp_tab); i++)
109                 {
110                         unsigned long long idiff = new[i] - old[i];
111                         unsigned long long bdiff = (new[base] - old[base]) | 1;
112                         printf("%6.2f ", (double)idiff * 100.0 / bdiff);
113                 }
114                 printf("\n");
115                 memcpy(old, new, sizeof(old));
116
117                 if (argc < 3)
118                         break;
119                 usleep(atoi(argv[2]));
120         }
121
122         return 0;
123 }
124
125 #endif // PPROF_TOOL
126