11a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca// This file contains a Windows implementation of CpuWrapper.
21a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca// Note: Windows XP, Windows Server 2003 are the minimum requirements.
31a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca//       The requirements are due to the implementation being based on
41a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca//       WMI.
51a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca/*
61a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
71a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *
81a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *  Use of this source code is governed by a BSD-style license
91a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *  that can be found in the LICENSE file in the root of the source
101a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *  tree. An additional intellectual property rights grant can be found
111a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *  in the file PATENTS.  All contributing project authors may
121a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca *  be found in the AUTHORS file in the root of the source tree.
131a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca */
141a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
151a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CPU_WINDOWS_NO_CPOL_H_
161a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CPU_WINDOWS_NO_CPOL_H_
171a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
181a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca#include "cpu_wrapper.h"
191a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
201a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca#include <Wbemidl.h>
211a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
221a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecanamespace webrtc {
231a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecaclass ConditionVariableWrapper;
241a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecaclass CriticalSectionWrapper;
251a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecaclass EventWrapper;
261a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecaclass ThreadWrapper;
271a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
281a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecaclass CpuWindows : public CpuWrapper
291a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca{
301a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecapublic:
311a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    virtual WebRtc_Word32 CpuUsage();
321a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    virtual WebRtc_Word32 CpuUsage(WebRtc_Word8* /*pProcessName*/,
33395edbc5151b2ce9dd77a22d104ce886e9326354José Fonseca                                   WebRtc_UWord32 /*length*/) {return -1;}
341a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    virtual WebRtc_Word32 CpuUsage(WebRtc_UWord32  /*dwProcessID*/) {return -1;}
351a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
361a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    virtual WebRtc_Word32 CpuUsageMultiCore(WebRtc_UWord32& num_cores,
371a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca                                            WebRtc_UWord32*& cpu_usage);
381a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
391a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    virtual void Reset() {}
407844b4e730604e613a88f536c4aeee5c02d300fdJosé Fonseca    virtual void Stop() {}
417844b4e730604e613a88f536c4aeee5c02d300fdJosé Fonseca
421a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    CpuWindows();
43395edbc5151b2ce9dd77a22d104ce886e9326354José Fonseca    virtual ~CpuWindows();
441a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonsecaprivate:
451a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool AllocateComplexDataTypes();
461a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    void DeAllocateComplexDataTypes();
471a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
481a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    void StartPollingCpu();
491a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool StopPollingCpu();
501a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
511a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    static bool Process(void* thread_object);
521a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool ProcessImpl();
531a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
541a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool CreateWmiConnection();
551a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool CreatePerfOsRefresher();
561a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool CreatePerfOsCpuHandles();
571a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool Initialize();
581a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool Terminate();
591a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
601a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    bool UpdateCpuUsage();
61f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell
62f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    ThreadWrapper* cpu_polling_thread;
63f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell
64f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    bool initialize_;
65f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    bool has_initialized_;
66f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    CriticalSectionWrapper* init_crit_;
67f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    ConditionVariableWrapper* init_cond_;
68f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell
69f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    bool terminate_;
70f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    bool has_terminated_;
71f5d4274b4a8effc70c238060c3728aea629663dfKeith Whitwell    CriticalSectionWrapper* terminate_crit_;
721a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    ConditionVariableWrapper* terminate_cond_;
731a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
741a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    // For sleep with wake-up functionality.
751a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    EventWrapper* sleep_event;
761a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
771a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    // Will be an array. Just care about CPU 0 for now.
781a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    WebRtc_UWord32* cpu_usage_;
791a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
801a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    // One IWbemObjectAccess for each processor and one for the total.
811a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    // 0-n-1 is the individual processors.
821a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    // n is the total.
83befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell    IWbemObjectAccess** wbem_enum_access_;
84befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell    DWORD number_of_objects_;
85befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell
86befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell    // Cpu timestamp
87befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell    long cpu_usage_handle_;
88befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell    unsigned __int64* previous_processor_timestamp_;
89befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell
90befa4ff50ec4728de70c04532f8c7342fbd70147Keith Whitwell    // Timestamp
911a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    long timestamp_sys_100_ns_handle_;
921a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    unsigned __int64* previous_100ns_timestamp_;
931a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
949425a702bef7d3f601e9ddc2b801f00a3d52dbb8Keith Whitwell    IWbemServices* wbem_service_;
959425a702bef7d3f601e9ddc2b801f00a3d52dbb8Keith Whitwell    IWbemServices* wbem_service_proxy_;
969425a702bef7d3f601e9ddc2b801f00a3d52dbb8Keith Whitwell
971a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    IWbemRefresher* wbem_refresher_;
98488dd2c1912132fe7ee5e81b05fb64ba62a46098Brian Paul
991a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca    IWbemHiPerfEnum* wbem_enum_;
1001a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca
101740e50c60f03d194aafab93d5251699964800979Brian Paul};
102740e50c60f03d194aafab93d5251699964800979Brian Paul} // namespace webrtc
1031a8daf0627dde44aaa7c40786782618d4d5a6a36José Fonseca#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CPU_WINDOWS_NO_CPOL_H_
104740e50c60f03d194aafab93d5251699964800979Brian Paul