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