1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - profile.c                                               *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2014 Bobby Smiles                                       *
5  *   Copyright (C) 2012 CasualJames                                        *
6  *   Copyright (C) 2002 Hacktarux                                          *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
22  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23 
24 #ifdef PROFILE
25 #include "profile.h"
26 
27 #include "../api/m64p_types.h"
28 #include "../api/callbacks.h"
29 
30 static long long int time_in_section[NUM_TIMED_SECTIONS];
31 static long long int last_start[NUM_TIMED_SECTIONS];
32 
33 #if defined(WIN32) && !defined(__MINGW32__)
34   // timing
35   #include <windows.h>
get_time(void)36   static long long int get_time(void)
37   {
38       LARGE_INTEGER counter;
39       QueryPerformanceCounter(&counter);
40       return counter.QuadPart;
41   }
time_to_nsec(long long int time)42   static long long int time_to_nsec(long long int time)
43   {
44       static LARGE_INTEGER freq = { 0 };
45       if (freq.QuadPart == 0)
46           QueryPerformanceFrequency(&freq);
47       return time * 1000000000 / freq.QuadPart;
48   }
49 
50 #else  /* Not WIN32 */
51   // timing
52   #include <time.h>
get_time(void)53   static long long int get_time(void)
54   {
55      struct timespec ts;
56      clock_gettime(CLOCK_MONOTONIC, &ts);
57      return (long long int)ts.tv_sec * 1000000000 + ts.tv_nsec;
58   }
time_to_nsec(long long int time)59   static long long int time_to_nsec(long long int time)
60   {
61       return time;
62   }
63 #endif
64 
timed_section_start(enum timed_section section)65 void timed_section_start(enum timed_section section)
66 {
67    last_start[section] = get_time();
68 }
69 
timed_section_end(enum timed_section section)70 void timed_section_end(enum timed_section section)
71 {
72    long long int end = get_time();
73    time_in_section[section] += end - last_start[section];
74 }
75 
timed_sections_refresh()76 void timed_sections_refresh()
77 {
78    long long int curr_time = get_time();
79    if(time_to_nsec(curr_time - last_start[TIMED_SECTION_ALL]) >= 2000000000)
80    {
81       time_in_section[TIMED_SECTION_ALL] = curr_time - last_start[TIMED_SECTION_ALL];
82       DebugMessage(M64MSG_INFO, "gfx=%f%% - audio=%f%% - compiler=%f%%, idle=%f%%",
83          100.0 * (double)time_in_section[TIMED_SECTION_GFX] / time_in_section[TIMED_SECTION_ALL],
84          100.0 * (double)time_in_section[TIMED_SECTION_AUDIO] / time_in_section[TIMED_SECTION_ALL],
85          100.0 * (double)time_in_section[TIMED_SECTION_COMPILER] / time_in_section[TIMED_SECTION_ALL],
86          100.0 * (double)time_in_section[TIMED_SECTION_IDLE] / time_in_section[TIMED_SECTION_ALL]);
87       DebugMessage(M64MSG_INFO, "gfx=%llins - audio=%llins - compiler %llins - idle=%llins",
88          time_to_nsec(time_in_section[TIMED_SECTION_GFX]),
89          time_to_nsec(time_in_section[TIMED_SECTION_AUDIO]),
90          time_to_nsec(time_in_section[TIMED_SECTION_COMPILER]),
91          time_to_nsec(time_in_section[TIMED_SECTION_IDLE]));
92       time_in_section[TIMED_SECTION_GFX] = 0;
93       time_in_section[TIMED_SECTION_AUDIO] = 0;
94       time_in_section[TIMED_SECTION_COMPILER] = 0;
95       time_in_section[TIMED_SECTION_IDLE] = 0;
96       last_start[TIMED_SECTION_ALL] = curr_time;
97    }
98 }
99 
100 #endif
101 
102