[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDPerformanceCounter.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "DVDPerformanceCounter.h"
22 #include "DVDMessageQueue.h"
23 #include "utils/TimeUtils.h"
24
25 #include "dvd_config.h"
26
27 #ifdef DVDDEBUG_WITH_PERFORMANCE_COUNTER
28 #include <xbdm.h>
29 #endif
30
31 HRESULT __stdcall DVDPerformanceCounterAudioQueue(PLARGE_INTEGER numerator, PLARGE_INTEGER demoninator)
32 {
33   numerator->QuadPart = 0LL;
34   //g_dvdPerformanceCounter.Lock();
35   if (g_dvdPerformanceCounter.m_pAudioQueue)
36   {
37     int iSize     = g_dvdPerformanceCounter.m_pAudioQueue->GetDataSize();
38     int iMaxSize  = g_dvdPerformanceCounter.m_pAudioQueue->GetMaxDataSize();
39     if (iMaxSize > 0)
40     {
41       int iPercent  = (iSize * 100) / iMaxSize;
42       if (iPercent > 100) iPercent = 100;
43       numerator->QuadPart = iPercent;
44     }
45   }
46   //g_dvdPerformanceCounter.Unlock();
47   return S_OK;
48 }
49
50 HRESULT __stdcall DVDPerformanceCounterVideoQueue(PLARGE_INTEGER numerator, PLARGE_INTEGER demoninator)
51 {
52   numerator->QuadPart = 0LL;
53   //g_dvdPerformanceCounter.Lock();
54   if (g_dvdPerformanceCounter.m_pVideoQueue)
55   {
56     int iSize     = g_dvdPerformanceCounter.m_pVideoQueue->GetDataSize();
57     int iMaxSize  = g_dvdPerformanceCounter.m_pVideoQueue->GetMaxDataSize();
58     if (iMaxSize > 0)
59     {
60       int iPercent  = (iSize * 100) / iMaxSize;
61       if (iPercent > 100) iPercent = 100;
62       numerator->QuadPart = iPercent;
63     }
64   }
65   //g_dvdPerformanceCounter.Unlock();
66   return S_OK;
67 }
68
69 inline int64_t get_thread_cpu_usage(ProcessPerformance* p)
70 {
71   if (p->thread)
72   {
73     ULARGE_INTEGER old_time_thread;
74     ULARGE_INTEGER old_time_system;
75
76     old_time_thread.QuadPart = p->timer_thread.QuadPart;
77     old_time_system.QuadPart = p->timer_system.QuadPart;
78
79     p->timer_thread.QuadPart = p->thread->GetAbsoluteUsage();
80     p->timer_system.QuadPart = CurrentHostCounter();
81
82     int64_t threadTime = (p->timer_thread.QuadPart - old_time_thread.QuadPart);
83     int64_t systemTime = (p->timer_system.QuadPart - old_time_system.QuadPart);
84
85     if (systemTime > 0 && threadTime > 0) return ((threadTime * 100) / systemTime);
86   }
87   return 0LL;
88 }
89
90 HRESULT __stdcall DVDPerformanceCounterVideoDecodePerformance(PLARGE_INTEGER numerator, PLARGE_INTEGER demoninator)
91 {
92   //g_dvdPerformanceCounter.Lock();
93   numerator->QuadPart = get_thread_cpu_usage(&g_dvdPerformanceCounter.m_videoDecodePerformance);
94   //g_dvdPerformanceCounter.Unlock();
95   return S_OK;
96 }
97
98 HRESULT __stdcall DVDPerformanceCounterAudioDecodePerformance(PLARGE_INTEGER numerator, PLARGE_INTEGER demoninator)
99 {
100   //g_dvdPerformanceCounter.Lock();
101   numerator->QuadPart = get_thread_cpu_usage(&g_dvdPerformanceCounter.m_audioDecodePerformance);
102   //g_dvdPerformanceCounter.Unlock();
103   return S_OK;
104 }
105
106 HRESULT __stdcall DVDPerformanceCounterMainPerformance(PLARGE_INTEGER numerator, PLARGE_INTEGER demoninator)
107 {
108   //g_dvdPerformanceCounter.Lock();
109   numerator->QuadPart = get_thread_cpu_usage(&g_dvdPerformanceCounter.m_mainPerformance);
110   //g_dvdPerformanceCounter.Unlock();
111   return S_OK;
112 }
113
114 CDVDPerformanceCounter g_dvdPerformanceCounter;
115
116 CDVDPerformanceCounter::CDVDPerformanceCounter()
117 {
118   m_pAudioQueue = NULL;
119   m_pVideoQueue = NULL;
120
121   memset(&m_videoDecodePerformance, 0, sizeof(m_videoDecodePerformance)); // video decoding
122   memset(&m_audioDecodePerformance, 0, sizeof(m_audioDecodePerformance)); // audio decoding + output to audio device
123   memset(&m_mainPerformance,        0, sizeof(m_mainPerformance));        // reading files, demuxing, decoding of subtitles + menu overlays
124
125   Initialize();
126 }
127
128 CDVDPerformanceCounter::~CDVDPerformanceCounter()
129 {
130   DeInitialize();
131 }
132
133 bool CDVDPerformanceCounter::Initialize()
134 {
135   CSingleLock lock(m_critSection);
136
137 #ifdef DVDDEBUG_WITH_PERFORMANCE_COUNTER
138
139   DmRegisterPerformanceCounter("DVDAudioQueue",               DMCOUNT_SYNC, DVDPerformanceCounterAudioQueue);
140   DmRegisterPerformanceCounter("DVDVideoQueue",               DMCOUNT_SYNC, DVDPerformanceCounterVideoQueue);
141   DmRegisterPerformanceCounter("DVDVideoDecodePerformance",   DMCOUNT_SYNC, DVDPerformanceCounterVideoDecodePerformance);
142   DmRegisterPerformanceCounter("DVDAudioDecodePerformance",   DMCOUNT_SYNC, DVDPerformanceCounterAudioDecodePerformance);
143   DmRegisterPerformanceCounter("DVDMainPerformance",          DMCOUNT_SYNC, DVDPerformanceCounterMainPerformance);
144
145 #endif
146
147   return true;
148 }
149
150 void CDVDPerformanceCounter::DeInitialize()
151 {
152
153 }
154